blob: eb0ee11c6267a9b1610eb085fd8a6c62cac7d506 [file] [log] [blame]
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +02001/***************************************************************************//**
2* \file main.c
3* \version 1.0
4********************************************************************************
5* \copyright
6* SPDX-License-Identifier: Apache-2.0
7*
8* Licensed under the Apache License, Version 2.0 (the "License");
9* you may not use this file except in compliance with the License.
10* You may obtain a copy of the License at
11*
12* http://www.apache.org/licenses/LICENSE-2.0
13*
14* Unless required by applicable law or agreed to in writing, software
15* distributed under the License is distributed on an "AS IS" BASIS,
16* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17* See the License for the specific language governing permissions and
18* limitations under the License.
19*******************************************************************************/
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +020020/* Cypress pdl headers */
21#include "cy_pdl.h"
Bohdan Kovalchuk77256522020-04-15 18:03:43 +030022#include "cy_retarget_io_pdl.h"
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +020023#include "cy_result.h"
24
Bohdan Kovalchuk0324f1b2020-05-26 08:04:24 -050025#include "cycfg_clocks.h"
26#include "cycfg_peripherals.h"
27#include "cycfg_pins.h"
28
29#include "flash_qspi.h"
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +020030#include "sysflash/sysflash.h"
31#include "flash_map_backend/flash_map_backend.h"
32
33#include "bootutil/image.h"
34#include "bootutil/bootutil.h"
35#include "bootutil/sign_key.h"
36
37#include "bootutil/bootutil_log.h"
38
39/* Define pins for UART debug output */
Bohdan Kovalchuk77256522020-04-15 18:03:43 +030040#define CYBSP_UART_ENABLED 1U
41#define CYBSP_UART_HW SCB5
42#define CYBSP_UART_IRQ scb_5_interrupt_IRQn
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +020043
dmiv8672c8e2020-09-16 12:59:20 +030044#ifdef CY_BOOT_USE_EXTERNAL_FLASH
45/* Choose SMIF slot number (slave select).
46 * Acceptable values are:
47 * 0 - SMIF disabled (no external memory);
48 * 1, 2, 3 or 4 - slave select line memory module is connected to.
49 */
50uint32_t smif_id = 1; /* Assume SlaveSelect_0 is used for External Memory */
51#endif
52
53extern cy_stc_scb_uart_context_t CYBSP_UART_context;
54
55/* Parameter structures for callback function */
56static cy_stc_syspm_callback_params_t deep_sleep_clbk_params =
57{
58 CYBSP_UART_HW,
59 &CYBSP_UART_context
60};
61
62static cy_stc_syspm_callback_params_t deep_sleep_sysclk_pm_clbk_param =
63{
64 NULL,
65 NULL
66};
67
68/* Callback structure */
69cy_stc_syspm_callback_t uart_deep_sleep =
70{
71 &Cy_SCB_UART_DeepSleepCallback,
72 CY_SYSPM_DEEPSLEEP,
73 CY_SYSPM_SKIP_BEFORE_TRANSITION ,
74 &deep_sleep_clbk_params,
75 NULL,
76 NULL,
77 0
78};
79
80cy_stc_syspm_callback_t clk_deep_sleep =
81{
82 &Cy_SysClk_DeepSleepCallback,
83 CY_SYSPM_DEEPSLEEP,
84 CY_SYSPM_SKIP_BEFORE_TRANSITION ,
85 &deep_sleep_sysclk_pm_clbk_param,
86 NULL,
87 NULL,
88 0
89};
90
91void hw_deinit(void);
92
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +020093static void do_boot(struct boot_rsp *rsp)
94{
95 uint32_t app_addr = 0;
96
97 app_addr = (rsp->br_image_off + rsp->br_hdr->ih_hdr_size);
98
99 BOOT_LOG_INF("Starting User Application on CM4 (wait)...");
Bohdan Kovalchuk8416f352020-07-16 10:29:58 +0300100 BOOT_LOG_INF("Start Address: 0x%08lx", app_addr);
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +0200101 Cy_SysLib_Delay(100);
102
dmiv8672c8e2020-09-16 12:59:20 +0300103 hw_deinit();
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +0200104
dmiv8672c8e2020-09-16 12:59:20 +0300105 Cy_SysEnableCM4(app_addr);
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +0200106}
107
108int main(void)
109{
Bohdan Kovalchuka333a452020-07-09 16:55:58 +0300110 struct boot_rsp rsp;
dmiv8672c8e2020-09-16 12:59:20 +0300111 cy_rslt_t result = CY_RSLT_TYPE_ERROR;
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +0200112
Bohdan Kovalchuk77256522020-04-15 18:03:43 +0300113 init_cycfg_clocks();
114 init_cycfg_peripherals();
115 init_cycfg_pins();
dmiv8672c8e2020-09-16 12:59:20 +0300116
117 /* register callback funtions that manage peripherals before going to deep sleep */
118 if (!Cy_SysPm_RegisterCallback(&uart_deep_sleep) ||
119 !Cy_SysPm_RegisterCallback(&clk_deep_sleep))
120 {
121 CY_ASSERT(0);
122 }
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +0200123 /* enable interrupts */
124 __enable_irq();
125
Bohdan Kovalchuk77256522020-04-15 18:03:43 +0300126 /* Initialize retarget-io to use the debug UART port (CYBSP_UART_HW) */
dmiv8672c8e2020-09-16 12:59:20 +0300127 result = cy_retarget_io_pdl_init(115200u);
128
129 if (result != CY_RSLT_SUCCESS)
130 {
131 CY_ASSERT(0);
132 }
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +0200133
134 BOOT_LOG_INF("MCUBoot Bootloader Started");
135
Bohdan Kovalchuk0324f1b2020-05-26 08:04:24 -0500136#ifdef CY_BOOT_USE_EXTERNAL_FLASH
Bohdan Kovalchuka333a452020-07-09 16:55:58 +0300137 cy_rslt_t rc = !CY_RSLT_SUCCESS;
138
139 #undef MCUBOOT_MAX_IMG_SECTORS
140 /* redefine number of sectors as there 2MB will be
141 * available on PSoC062-2M in case of external
142 * memory usage */
143 #define MCUBOOT_MAX_IMG_SECTORS 4096
Bohdan Kovalchuk0324f1b2020-05-26 08:04:24 -0500144 rc = qspi_init_sfdp(smif_id);
dmiv8672c8e2020-09-16 12:59:20 +0300145 if (rc == CY_SMIF_SUCCESS)
Bohdan Kovalchuk0324f1b2020-05-26 08:04:24 -0500146 {
147 BOOT_LOG_INF("External Memory initialized w/ SFDP.");
148 }
149 else
150 {
151 BOOT_LOG_ERR("External Memory initialization w/ SFDP FAILED: 0x%02x", (int)rc);
152 }
dmiv8672c8e2020-09-16 12:59:20 +0300153 if (0 == rc)
Bohdan Kovalchuk0324f1b2020-05-26 08:04:24 -0500154#endif
155 {
dmiv8672c8e2020-09-16 12:59:20 +0300156 if (boot_go(&rsp) == 0)
157 {
Bohdan Kovalchuk0324f1b2020-05-26 08:04:24 -0500158 BOOT_LOG_INF("User Application validated successfully");
159 do_boot(&rsp);
dmiv8672c8e2020-09-16 12:59:20 +0300160 }
161 else
162 {
163 BOOT_LOG_INF("MCUBoot Bootloader found none of bootable images");
164 }
Bohdan Kovalchuk0324f1b2020-05-26 08:04:24 -0500165 }
dmiv8672c8e2020-09-16 12:59:20 +0300166
167 while (1)
168 {
169 Cy_SysPm_CpuEnterDeepSleep(CY_SYSPM_WAIT_FOR_INTERRUPT);
170 }
171
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +0200172 return 0;
173}
dmiv8672c8e2020-09-16 12:59:20 +0300174
175void hw_deinit(void)
176{
177 cy_retarget_io_pdl_deinit();
178 Cy_GPIO_Port_Deinit(CYBSP_UART_RX_PORT);
179 Cy_GPIO_Port_Deinit(CYBSP_UART_TX_PORT);
180
181#ifdef CY_BOOT_USE_EXTERNAL_FLASH
182 qspi_deinit(smif_id);
183#endif
184}