Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2020 Cypress Semiconductor Corporation |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 3 | * Copyright (c) 2021 Infineon Technologies AG |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 4 | * |
| 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | */ |
| 7 | /* |
| 8 | * Licensed to the Apache Software Foundation (ASF) under one |
| 9 | * or more contributor license agreements. See the NOTICE file |
| 10 | * distributed with this work for additional information |
| 11 | * regarding copyright ownership. The ASF licenses this file |
| 12 | * to you under the Apache License, Version 2.0 (the |
| 13 | * "License"); you may not use this file except in compliance |
| 14 | * with the License. You may obtain a copy of the License at |
| 15 | * |
| 16 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 17 | * |
| 18 | * Unless required by applicable law or agreed to in writing, |
| 19 | * software distributed under the License is distributed on an |
| 20 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 21 | * KIND, either express or implied. See the License for the |
| 22 | * specific language governing permissions and limitations |
| 23 | * under the License. |
| 24 | */ |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 25 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 26 | #ifdef CYW20829 |
| 27 | #include <inttypes.h> |
| 28 | #include "cybsp.h" |
| 29 | #include "cycfg_pins.h" |
| 30 | #include "cyhal_wdt.h" |
| 31 | #else |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 32 | #include "system_psoc6.h" |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 33 | #endif /* CYW20829 */ |
| 34 | |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 35 | #include "cy_pdl.h" |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 36 | #include "cy_retarget_io.h" |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 37 | #include "cyhal.h" |
Roman Okhrimenko | 0c7aebc | 2020-09-02 13:37:51 +0300 | [diff] [blame] | 38 | #include "watchdog.h" |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 39 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 40 | #include "flash_qspi.h" |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 41 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 42 | #if !(SWAP_DISABLED) && defined(UPGRADE_IMAGE) |
| 43 | #include "set_img_ok.h" |
| 44 | #endif |
| 45 | |
| 46 | /* Define pins for UART debug output */ |
| 47 | #ifdef CYW20829 |
| 48 | #define CY_DEBUG_UART_TX (CYBSP_DEBUG_UART_TX) |
| 49 | #define CY_DEBUG_UART_RX (CYBSP_DEBUG_UART_RX) |
| 50 | #else |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 51 | #define CY_DEBUG_UART_TX (P5_1) |
| 52 | #define CY_DEBUG_UART_RX (P5_0) |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 53 | #endif /* CYW20829 */ |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 54 | |
| 55 | #if defined(PSOC_062_2M) |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 56 | #define LED_PORT GPIO_PRT13 |
| 57 | #define LED_PIN 7U |
Roman Okhrimenko | 4bc2810 | 2021-02-01 19:31:41 +0200 | [diff] [blame] | 58 | #elif defined(PSOC_062_1M) |
| 59 | #define LED_PORT GPIO_PRT13 |
| 60 | #define LED_PIN 7U |
| 61 | #elif defined(PSOC_062_512K) |
| 62 | #define LED_PORT GPIO_PRT11 |
| 63 | #define LED_PIN 1U |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 64 | #elif defined(CYW20829) |
| 65 | #define LED_PORT GPIO_PRT0 |
| 66 | #define LED_PIN 0U |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 67 | #endif |
| 68 | |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 69 | const cy_stc_gpio_pin_config_t LED_config = |
| 70 | { |
| 71 | .outVal = 1, |
| 72 | .driveMode = CY_GPIO_DM_STRONG_IN_OFF, |
| 73 | .hsiom = HSIOM_SEL_GPIO, |
| 74 | .intEdge = CY_GPIO_INTR_DISABLE, |
| 75 | .intMask = 0UL, |
| 76 | .vtrip = CY_GPIO_VTRIP_CMOS, |
| 77 | .slewRate = CY_GPIO_SLEW_FAST, |
| 78 | .driveSel = CY_GPIO_DRIVE_FULL, |
| 79 | .vregEn = 0UL, |
| 80 | .ibufMode = 0UL, |
| 81 | .vtripSel = 0UL, |
| 82 | .vrefSel = 0UL, |
| 83 | .vohSel = 0UL, |
| 84 | }; |
| 85 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 86 | uint32_t smif_id = 1; /* Assume SlaveSelect_0 is used for External Memory */ |
Roman Okhrimenko | 0c7aebc | 2020-09-02 13:37:51 +0300 | [diff] [blame] | 87 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 88 | #ifdef BOOT_IMAGE |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 89 | #define BLINK_PERIOD (1000u) |
| 90 | #define GREETING_MESSAGE_VER "[BlinkyApp] BlinkyApp v1.0 [CM4]\r\n" |
| 91 | #define GREETING_MESSAGE_INFO "[BlinkyApp] Red led blinks with 1 sec period\r\n" |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 92 | #elif defined(UPGRADE_IMAGE) |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 93 | #define BLINK_PERIOD (250u) |
| 94 | #define GREETING_MESSAGE_VER "[BlinkyApp] BlinkyApp v2.0 [+]\r\n" |
| 95 | #define GREETING_MESSAGE_INFO "[BlinkyApp] Red led blinks with 0.25 sec period\r\n" |
| 96 | #else |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 97 | #error "[BlinkyApp] Please specify type of image: -DBOOT_IMAGE or -DUPGRADE_IMAGE\r\n" |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 98 | #endif |
| 99 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 100 | #define WATCHDOG_FREE_MESSAGE "[BlinkyApp] Turn off watchdog timer\r\n" |
| 101 | |
| 102 | static void check_result(int res) |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 103 | { |
Roman Okhrimenko | 0c7aebc | 2020-09-02 13:37:51 +0300 | [diff] [blame] | 104 | if (res != CY_RSLT_SUCCESS) { |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 105 | CY_ASSERT(0); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 106 | /* Loop forever... */ |
| 107 | for (;;) {} |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
| 111 | void test_app_init_hardware(void) |
| 112 | { |
| 113 | /* enable interrupts */ |
| 114 | __enable_irq(); |
| 115 | |
| 116 | /* Disabling watchdog so it will not interrupt normal flow later */ |
| 117 | Cy_GPIO_Pin_Init(LED_PORT, LED_PIN, &LED_config); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 118 | |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 119 | /* Initialize retarget-io to use the debug UART port */ |
| 120 | check_result(cy_retarget_io_init(CY_DEBUG_UART_TX, CY_DEBUG_UART_RX, |
| 121 | CY_RETARGET_IO_BAUDRATE)); |
| 122 | |
| 123 | printf("\n===========================\r\n"); |
| 124 | printf(GREETING_MESSAGE_VER); |
| 125 | printf("===========================\r\n"); |
| 126 | |
| 127 | printf("[BlinkyApp] GPIO initialized \r\n"); |
| 128 | printf("[BlinkyApp] UART initialized \r\n"); |
| 129 | printf("[BlinkyApp] Retarget I/O set to 115200 baudrate \r\n"); |
| 130 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 131 | #ifdef CYW20829 |
| 132 | cy_en_smif_status_t rc = CY_SMIF_CMD_NOT_FOUND; |
| 133 | |
| 134 | rc = qspi_init_sfdp(smif_id); |
| 135 | if (CY_SMIF_SUCCESS == rc) { |
| 136 | printf("[BlinkyApp] External Memory initialized w/ SFDP. \r\n"); |
| 137 | } |
| 138 | else { |
| 139 | printf("[BlinkyApp] External Memory initialization w/ SFDP FAILED: 0x%" PRIx32 " \r\n", (uint32_t)rc); |
| 140 | } |
| 141 | #endif /* CYW20829 */ |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | int main(void) |
| 145 | { |
| 146 | uint32_t blinky_period = BLINK_PERIOD; |
| 147 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 148 | #if defined CYW20829 |
| 149 | cybsp_init(); |
| 150 | #endif /* CYW20829 */ |
| 151 | |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 152 | test_app_init_hardware(); |
| 153 | |
| 154 | printf(GREETING_MESSAGE_INFO); |
| 155 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 156 | /* Disable watchdog timer to mark successful start up of application. |
| 157 | * For PSOC6 WDT is disabled in SystemInit() function. |
| 158 | */ |
Roman Okhrimenko | 0c7aebc | 2020-09-02 13:37:51 +0300 | [diff] [blame] | 159 | printf(WATCHDOG_FREE_MESSAGE); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 160 | #ifdef CYW20829 |
| 161 | cyhal_wdt_t *cyw20829_wdt = NULL; |
| 162 | cyhal_wdt_free(cyw20829_wdt); |
| 163 | #else |
Roman Okhrimenko | 0c7aebc | 2020-09-02 13:37:51 +0300 | [diff] [blame] | 164 | cy_wdg_free(); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 165 | #endif /* CYW20829 */ |
Roman Okhrimenko | 0c7aebc | 2020-09-02 13:37:51 +0300 | [diff] [blame] | 166 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 167 | #if !(SWAP_DISABLED) && defined(UPGRADE_IMAGE) |
| 168 | int rc = -1; |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 169 | |
| 170 | printf("[BlinkyApp] Try to set img_ok to confirm upgrade image\r\n"); |
| 171 | |
| 172 | /* Write Image OK flag to the slot trailer, so MCUBoot-loader |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 173 | * will not revert new image |
| 174 | */ |
| 175 | rc = set_img_ok(IMG_OK_ADDR, USER_SWAP_IMAGE_OK); |
| 176 | |
| 177 | if (IMG_OK_ALREADY_SET == rc) { |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 178 | printf("[BlinkyApp] Img_ok is already set in trailer\r\n"); |
| 179 | } |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 180 | else if (IMG_OK_SET_SUCCESS == rc) { |
| 181 | printf("[BlinkyApp] SWAP Status : Image OK was set at 0x%08x.\r\n", IMG_OK_ADDR); |
| 182 | } |
| 183 | else { |
| 184 | printf("[BlinkyApp] SWAP Status : Failed to set Image OK.\r\n"); |
| 185 | } |
| 186 | |
| 187 | #endif /* !(SWAP_DISABLED) && defined(UPGRADE_IMAGE) */ |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 188 | |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 189 | for (;;) |
| 190 | { |
| 191 | /* Toggle the user LED periodically */ |
| 192 | Cy_SysLib_Delay(blinky_period/2); |
| 193 | |
| 194 | /* Invert the USER LED state */ |
| 195 | Cy_GPIO_Inv(LED_PORT, LED_PIN); |
| 196 | } |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 197 | |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 198 | return 0; |
| 199 | } |