blob: 59cee6a4b7db2864ad3fa5a277f9dafdbbb11d6f [file] [log] [blame]
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +02001/*
2 * Copyright (c) 2020 Cypress Semiconductor Corporation
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003 * Copyright (c) 2021 Infineon Technologies AG
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +02004 *
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 Okhrimenko89ecdac2020-02-28 17:05:55 +020025
Roman Okhrimenko977b3752022-03-31 14:40:48 +030026#ifdef CYW20829
27#include <inttypes.h>
28#include "cybsp.h"
29#include "cycfg_pins.h"
30#include "cyhal_wdt.h"
31#else
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +020032#include "system_psoc6.h"
Roman Okhrimenko977b3752022-03-31 14:40:48 +030033#endif /* CYW20829 */
34
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +020035#include "cy_pdl.h"
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +020036#include "cy_retarget_io.h"
Roman Okhrimenko977b3752022-03-31 14:40:48 +030037#include "cyhal.h"
Roman Okhrimenko0c7aebc2020-09-02 13:37:51 +030038#include "watchdog.h"
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +020039
Roman Okhrimenko977b3752022-03-31 14:40:48 +030040#include "flash_qspi.h"
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +020041
Roman Okhrimenko977b3752022-03-31 14:40:48 +030042#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 Okhrimenko89ecdac2020-02-28 17:05:55 +020051#define CY_DEBUG_UART_TX (P5_1)
52#define CY_DEBUG_UART_RX (P5_0)
Roman Okhrimenko977b3752022-03-31 14:40:48 +030053#endif /* CYW20829 */
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +020054
55#if defined(PSOC_062_2M)
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +020056#define LED_PORT GPIO_PRT13
57#define LED_PIN 7U
Roman Okhrimenko4bc28102021-02-01 19:31:41 +020058#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 Okhrimenko977b3752022-03-31 14:40:48 +030064#elif defined(CYW20829)
65#define LED_PORT GPIO_PRT0
66#define LED_PIN 0U
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +020067#endif
68
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +020069const 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 Okhrimenko977b3752022-03-31 14:40:48 +030086uint32_t smif_id = 1; /* Assume SlaveSelect_0 is used for External Memory */
Roman Okhrimenko0c7aebc2020-09-02 13:37:51 +030087
Roman Okhrimenko977b3752022-03-31 14:40:48 +030088#ifdef BOOT_IMAGE
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +020089 #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 Okhrimenko977b3752022-03-31 14:40:48 +030092#elif defined(UPGRADE_IMAGE)
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +020093 #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 Okhrimenko977b3752022-03-31 14:40:48 +030097 #error "[BlinkyApp] Please specify type of image: -DBOOT_IMAGE or -DUPGRADE_IMAGE\r\n"
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +020098#endif
99
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300100#define WATCHDOG_FREE_MESSAGE "[BlinkyApp] Turn off watchdog timer\r\n"
101
102static void check_result(int res)
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +0200103{
Roman Okhrimenko0c7aebc2020-09-02 13:37:51 +0300104 if (res != CY_RSLT_SUCCESS) {
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +0200105 CY_ASSERT(0);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300106 /* Loop forever... */
107 for (;;) {}
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +0200108 }
109}
110
111void 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 Okhrimenko977b3752022-03-31 14:40:48 +0300118
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +0200119 /* 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 Okhrimenko977b3752022-03-31 14:40:48 +0300131#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 Okhrimenko89ecdac2020-02-28 17:05:55 +0200142}
143
144int main(void)
145{
146 uint32_t blinky_period = BLINK_PERIOD;
147
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300148#if defined CYW20829
149 cybsp_init();
150#endif /* CYW20829 */
151
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +0200152 test_app_init_hardware();
153
154 printf(GREETING_MESSAGE_INFO);
155
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300156 /* Disable watchdog timer to mark successful start up of application.
157 * For PSOC6 WDT is disabled in SystemInit() function.
158 */
Roman Okhrimenko0c7aebc2020-09-02 13:37:51 +0300159 printf(WATCHDOG_FREE_MESSAGE);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300160#ifdef CYW20829
161 cyhal_wdt_t *cyw20829_wdt = NULL;
162 cyhal_wdt_free(cyw20829_wdt);
163#else
Roman Okhrimenko0c7aebc2020-09-02 13:37:51 +0300164 cy_wdg_free();
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300165#endif /* CYW20829 */
Roman Okhrimenko0c7aebc2020-09-02 13:37:51 +0300166
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300167#if !(SWAP_DISABLED) && defined(UPGRADE_IMAGE)
168 int rc = -1;
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200169
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 Okhrimenko977b3752022-03-31 14:40:48 +0300173 * 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 Okhrimenko13f79ed2021-03-11 19:05:41 +0200178 printf("[BlinkyApp] Img_ok is already set in trailer\r\n");
179 }
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300180 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 Okhrimenko13f79ed2021-03-11 19:05:41 +0200188
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +0200189 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 Okhrimenko977b3752022-03-31 14:40:48 +0300197
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +0200198 return 0;
199}