blob: 84bbd013b231e022863315ef14237f2621296333 [file] [log] [blame]
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +02001/*
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"
30
31/* Define pins for UART debug output */
32
33#define CY_DEBUG_UART_TX (P5_1)
34#define CY_DEBUG_UART_RX (P5_0)
35
36#if defined(PSOC_062_2M)
37#warning "Check if User LED is correct for your target board."
38#define LED_PORT GPIO_PRT13
39#define LED_PIN 7U
40#endif
41
42#define LED_NUM 5U
43#define LED_DRIVEMODE CY_GPIO_DM_STRONG_IN_OFF
44#define LED_INIT_DRIVESTATE 1
45
46const cy_stc_gpio_pin_config_t LED_config =
47{
48 .outVal = 1,
49 .driveMode = CY_GPIO_DM_STRONG_IN_OFF,
50 .hsiom = HSIOM_SEL_GPIO,
51 .intEdge = CY_GPIO_INTR_DISABLE,
52 .intMask = 0UL,
53 .vtrip = CY_GPIO_VTRIP_CMOS,
54 .slewRate = CY_GPIO_SLEW_FAST,
55 .driveSel = CY_GPIO_DRIVE_FULL,
56 .vregEn = 0UL,
57 .ibufMode = 0UL,
58 .vtripSel = 0UL,
59 .vrefSel = 0UL,
60 .vohSel = 0UL,
61};
62
63#ifdef BOOT_IMG
64 #define BLINK_PERIOD (1000u)
65 #define GREETING_MESSAGE_VER "[BlinkyApp] BlinkyApp v1.0 [CM4]\r\n"
66 #define GREETING_MESSAGE_INFO "[BlinkyApp] Red led blinks with 1 sec period\r\n"
67#elif UPGRADE_IMG
68 #define BLINK_PERIOD (250u)
69 #define GREETING_MESSAGE_VER "[BlinkyApp] BlinkyApp v2.0 [+]\r\n"
70 #define GREETING_MESSAGE_INFO "[BlinkyApp] Red led blinks with 0.25 sec period\r\n"
71#else
72 #error "[BlinkyApp] Please specify type of image: -DBOOT_IMG or -DUPGRADE_IMG\r\n"
73#endif
74
75void check_result(int res)
76{
77 if (res != CY_RSLT_SUCCESS)
78 {
79 CY_ASSERT(0);
80 }
81}
82
Bohdan Kovalchuk7460ae52020-07-21 17:03:57 +030083/*
84* Writes 1 byte `src` into flash memory at `address`
85* It does a sequence of RD/Modify/WR of data in a Flash Row.
86 */
87int flash_write_byte(uint32_t address, uint8_t src)
88{
89 cy_en_flashdrv_status_t rc = CY_FLASH_DRV_SUCCESS;
90 uint32_t row_addr = 0;
91 uint8_t row_buff[512];
92
93 /* accepting arbitrary address */
94 row_addr = (address/CY_FLASH_SIZEOF_ROW)*CY_FLASH_SIZEOF_ROW;
95
96 /* preserving Row */
97 memcpy(row_buff, (void *)row_addr, sizeof(row_buff));
98
99 /* Modifying the target byte */
100 row_buff[address%CY_FLASH_SIZEOF_ROW] = src;
101
102 /* Programming updated row back */
103 rc = Cy_Flash_WriteRow(row_addr, (const uint32_t *)row_buff);
104
105 return (int) rc;
106}
107
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +0200108void test_app_init_hardware(void)
109{
110 /* enable interrupts */
111 __enable_irq();
112
113 /* Disabling watchdog so it will not interrupt normal flow later */
114 Cy_GPIO_Pin_Init(LED_PORT, LED_PIN, &LED_config);
115 /* Initialize retarget-io to use the debug UART port */
116 check_result(cy_retarget_io_init(CY_DEBUG_UART_TX, CY_DEBUG_UART_RX,
117 CY_RETARGET_IO_BAUDRATE));
118
119 printf("\n===========================\r\n");
120 printf(GREETING_MESSAGE_VER);
121 printf("===========================\r\n");
122
123 printf("[BlinkyApp] GPIO initialized \r\n");
124 printf("[BlinkyApp] UART initialized \r\n");
125 printf("[BlinkyApp] Retarget I/O set to 115200 baudrate \r\n");
126
127}
128
129int main(void)
130{
131 uint32_t blinky_period = BLINK_PERIOD;
132
133 test_app_init_hardware();
134
135 printf(GREETING_MESSAGE_INFO);
136
Bohdan Kovalchuk7460ae52020-07-21 17:03:57 +0300137#if defined(SWAP_ENABLED) && defined(UPGRADE_IMG)
138
139 #define USER_SWAP_IMAGE_OK_OFFS (24)
140 #define USER_SWAP_IMAGE_OK (1)
141 uint32_t img_ok_addr;
142 int rc;
143
144 printf("[BlinkyApp] Try to set img_ok to confirm upgrade image\r\n");
145
146 /* Write Image OK flag to the slot trailer, so MCUBoot-loader
147 * will not revert new image */
148 img_ok_addr = USER_APP_START + USER_APP_SIZE - USER_SWAP_IMAGE_OK_OFFS;
149 if (*((uint8_t *)img_ok_addr) != USER_SWAP_IMAGE_OK)
150 {
151 rc = flash_write_byte(img_ok_addr, USER_SWAP_IMAGE_OK);
152 if (0 == rc)
153 {
154 printf("[BlinkyApp] SWAP Status : Image OK was set at 0x%08lx.\r\n", img_ok_addr);
155 }
156 else
157 {
158 printf("[BlinkyApp] SWAP Status : Failed to set Image OK.\r\n");
159 }
160 } else
161 {
162 printf("[BlinkyApp] Img_ok is already set in trailer\r\n");
163 }
164#endif
165
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +0200166 for (;;)
167 {
168 /* Toggle the user LED periodically */
169 Cy_SysLib_Delay(blinky_period/2);
170
171 /* Invert the USER LED state */
172 Cy_GPIO_Inv(LED_PORT, LED_PIN);
173 }
174 return 0;
175}