Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2020 Cypress Semiconductor Corporation |
| 3 | * |
| 4 | * SPDX-License-Identifier: Apache-2.0 |
| 5 | */ |
| 6 | /* |
| 7 | * Licensed to the Apache Software Foundation (ASF) under one |
| 8 | * or more contributor license agreements. See the NOTICE file |
| 9 | * distributed with this work for additional information |
| 10 | * regarding copyright ownership. The ASF licenses this file |
| 11 | * to you under the Apache License, Version 2.0 (the |
| 12 | * "License"); you may not use this file except in compliance |
| 13 | * with the License. You may obtain a copy of the License at |
| 14 | * |
| 15 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 16 | * |
| 17 | * Unless required by applicable law or agreed to in writing, |
| 18 | * software distributed under the License is distributed on an |
| 19 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 20 | * KIND, either express or implied. See the License for the |
| 21 | * specific language governing permissions and limitations |
| 22 | * under the License. |
| 23 | */ |
| 24 | /*******************************************************************************/ |
| 25 | |
| 26 | #include "system_psoc6.h" |
| 27 | #include "cy_pdl.h" |
| 28 | #include "cyhal.h" |
| 29 | #include "cy_retarget_io.h" |
Roman Okhrimenko | 0c7aebc | 2020-09-02 13:37:51 +0300 | [diff] [blame] | 30 | #include "watchdog.h" |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 31 | |
| 32 | /* Define pins for UART debug output */ |
| 33 | |
| 34 | #define CY_DEBUG_UART_TX (P5_1) |
| 35 | #define CY_DEBUG_UART_RX (P5_0) |
| 36 | |
| 37 | #if defined(PSOC_062_2M) |
| 38 | #warning "Check if User LED is correct for your target board." |
| 39 | #define LED_PORT GPIO_PRT13 |
| 40 | #define LED_PIN 7U |
Roman Okhrimenko | 4bc2810 | 2021-02-01 19:31:41 +0200 | [diff] [blame] | 41 | #elif defined(PSOC_062_1M) |
| 42 | #define LED_PORT GPIO_PRT13 |
| 43 | #define LED_PIN 7U |
| 44 | #elif defined(PSOC_062_512K) |
| 45 | #define LED_PORT GPIO_PRT11 |
| 46 | #define LED_PIN 1U |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 47 | #endif |
| 48 | |
| 49 | #define LED_NUM 5U |
| 50 | #define LED_DRIVEMODE CY_GPIO_DM_STRONG_IN_OFF |
| 51 | #define LED_INIT_DRIVESTATE 1 |
| 52 | |
| 53 | const cy_stc_gpio_pin_config_t LED_config = |
| 54 | { |
| 55 | .outVal = 1, |
| 56 | .driveMode = CY_GPIO_DM_STRONG_IN_OFF, |
| 57 | .hsiom = HSIOM_SEL_GPIO, |
| 58 | .intEdge = CY_GPIO_INTR_DISABLE, |
| 59 | .intMask = 0UL, |
| 60 | .vtrip = CY_GPIO_VTRIP_CMOS, |
| 61 | .slewRate = CY_GPIO_SLEW_FAST, |
| 62 | .driveSel = CY_GPIO_DRIVE_FULL, |
| 63 | .vregEn = 0UL, |
| 64 | .ibufMode = 0UL, |
| 65 | .vtripSel = 0UL, |
| 66 | .vrefSel = 0UL, |
| 67 | .vohSel = 0UL, |
| 68 | }; |
| 69 | |
Roman Okhrimenko | 0c7aebc | 2020-09-02 13:37:51 +0300 | [diff] [blame] | 70 | #define WATCHDOG_UPD_MESSAGE "[BlinkyApp] Update watchdog timer started in MCUBootApp to mark successful start of user app\r\n" |
| 71 | #define WATCHDOG_FREE_MESSAGE "[BlinkyApp] Turn off watchdog timer\r\n" |
| 72 | |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 73 | #ifdef BOOT_IMG |
| 74 | #define BLINK_PERIOD (1000u) |
| 75 | #define GREETING_MESSAGE_VER "[BlinkyApp] BlinkyApp v1.0 [CM4]\r\n" |
| 76 | #define GREETING_MESSAGE_INFO "[BlinkyApp] Red led blinks with 1 sec period\r\n" |
Roman Okhrimenko | 0c7aebc | 2020-09-02 13:37:51 +0300 | [diff] [blame] | 77 | #elif defined(UPGRADE_IMG) |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 78 | #define BLINK_PERIOD (250u) |
| 79 | #define GREETING_MESSAGE_VER "[BlinkyApp] BlinkyApp v2.0 [+]\r\n" |
| 80 | #define GREETING_MESSAGE_INFO "[BlinkyApp] Red led blinks with 0.25 sec period\r\n" |
| 81 | #else |
| 82 | #error "[BlinkyApp] Please specify type of image: -DBOOT_IMG or -DUPGRADE_IMG\r\n" |
| 83 | #endif |
| 84 | |
| 85 | void check_result(int res) |
| 86 | { |
Roman Okhrimenko | 0c7aebc | 2020-09-02 13:37:51 +0300 | [diff] [blame] | 87 | if (res != CY_RSLT_SUCCESS) { |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 88 | CY_ASSERT(0); |
| 89 | } |
| 90 | } |
| 91 | |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 92 | /* |
| 93 | * Writes 1 byte `src` into flash memory at `address` |
| 94 | * It does a sequence of RD/Modify/WR of data in a Flash Row. |
| 95 | */ |
| 96 | int flash_write_byte(uint32_t address, uint8_t src) |
| 97 | { |
| 98 | cy_en_flashdrv_status_t rc = CY_FLASH_DRV_SUCCESS; |
| 99 | uint32_t row_addr = 0; |
| 100 | uint8_t row_buff[512]; |
| 101 | |
| 102 | /* accepting arbitrary address */ |
| 103 | row_addr = (address/CY_FLASH_SIZEOF_ROW)*CY_FLASH_SIZEOF_ROW; |
| 104 | |
| 105 | /* preserving Row */ |
| 106 | memcpy(row_buff, (void *)row_addr, sizeof(row_buff)); |
| 107 | |
| 108 | /* Modifying the target byte */ |
| 109 | row_buff[address%CY_FLASH_SIZEOF_ROW] = src; |
| 110 | |
| 111 | /* Programming updated row back */ |
| 112 | rc = Cy_Flash_WriteRow(row_addr, (const uint32_t *)row_buff); |
| 113 | |
| 114 | return (int) rc; |
| 115 | } |
| 116 | |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 117 | void test_app_init_hardware(void) |
| 118 | { |
| 119 | /* enable interrupts */ |
| 120 | __enable_irq(); |
| 121 | |
| 122 | /* Disabling watchdog so it will not interrupt normal flow later */ |
| 123 | Cy_GPIO_Pin_Init(LED_PORT, LED_PIN, &LED_config); |
| 124 | /* Initialize retarget-io to use the debug UART port */ |
| 125 | check_result(cy_retarget_io_init(CY_DEBUG_UART_TX, CY_DEBUG_UART_RX, |
| 126 | CY_RETARGET_IO_BAUDRATE)); |
| 127 | |
| 128 | printf("\n===========================\r\n"); |
| 129 | printf(GREETING_MESSAGE_VER); |
| 130 | printf("===========================\r\n"); |
| 131 | |
| 132 | printf("[BlinkyApp] GPIO initialized \r\n"); |
| 133 | printf("[BlinkyApp] UART initialized \r\n"); |
| 134 | printf("[BlinkyApp] Retarget I/O set to 115200 baudrate \r\n"); |
| 135 | |
| 136 | } |
| 137 | |
| 138 | int main(void) |
| 139 | { |
| 140 | uint32_t blinky_period = BLINK_PERIOD; |
| 141 | |
| 142 | test_app_init_hardware(); |
| 143 | |
| 144 | printf(GREETING_MESSAGE_INFO); |
| 145 | |
Roman Okhrimenko | 0c7aebc | 2020-09-02 13:37:51 +0300 | [diff] [blame] | 146 | /* Update watchdog timer to mark successful start up of application */ |
| 147 | printf(WATCHDOG_UPD_MESSAGE); |
| 148 | cy_wdg_kick(); |
| 149 | printf(WATCHDOG_FREE_MESSAGE); |
| 150 | cy_wdg_free(); |
| 151 | |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 152 | #if defined(SWAP_ENABLED) && defined(UPGRADE_IMG) |
| 153 | |
| 154 | #define USER_SWAP_IMAGE_OK_OFFS (24) |
| 155 | #define USER_SWAP_IMAGE_OK (1) |
| 156 | uint32_t img_ok_addr; |
| 157 | int rc; |
| 158 | |
| 159 | printf("[BlinkyApp] Try to set img_ok to confirm upgrade image\r\n"); |
| 160 | |
| 161 | /* Write Image OK flag to the slot trailer, so MCUBoot-loader |
| 162 | * will not revert new image */ |
| 163 | img_ok_addr = USER_APP_START + USER_APP_SIZE - USER_SWAP_IMAGE_OK_OFFS; |
| 164 | if (*((uint8_t *)img_ok_addr) != USER_SWAP_IMAGE_OK) |
| 165 | { |
| 166 | rc = flash_write_byte(img_ok_addr, USER_SWAP_IMAGE_OK); |
| 167 | if (0 == rc) |
| 168 | { |
| 169 | printf("[BlinkyApp] SWAP Status : Image OK was set at 0x%08lx.\r\n", img_ok_addr); |
| 170 | } |
| 171 | else |
| 172 | { |
| 173 | printf("[BlinkyApp] SWAP Status : Failed to set Image OK.\r\n"); |
| 174 | } |
| 175 | } else |
| 176 | { |
| 177 | printf("[BlinkyApp] Img_ok is already set in trailer\r\n"); |
| 178 | } |
| 179 | #endif |
| 180 | |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 181 | for (;;) |
| 182 | { |
| 183 | /* Toggle the user LED periodically */ |
| 184 | Cy_SysLib_Delay(blinky_period/2); |
| 185 | |
| 186 | /* Invert the USER LED state */ |
| 187 | Cy_GPIO_Inv(LED_PORT, LED_PIN); |
| 188 | } |
| 189 | return 0; |
| 190 | } |