blob: d148215f5e449606014182ad2837c6a59e43d654 [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
dmiv8672c8e2020-09-16 12:59:20 +030053
54void hw_deinit(void);
55
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +020056static void do_boot(struct boot_rsp *rsp)
57{
58 uint32_t app_addr = 0;
59
60 app_addr = (rsp->br_image_off + rsp->br_hdr->ih_hdr_size);
61
62 BOOT_LOG_INF("Starting User Application on CM4 (wait)...");
Bohdan Kovalchuk8416f352020-07-16 10:29:58 +030063 BOOT_LOG_INF("Start Address: 0x%08lx", app_addr);
Roman Okhrimenkoff026122020-09-23 12:58:07 +030064 BOOT_LOG_INF("Deinitializing hardware...");
65
66 cy_retarget_io_wait_tx_complete(CYBSP_UART_HW, 10);
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +020067
dmiv8672c8e2020-09-16 12:59:20 +030068 hw_deinit();
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +020069
dmiv8672c8e2020-09-16 12:59:20 +030070 Cy_SysEnableCM4(app_addr);
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +020071}
72
73int main(void)
74{
Bohdan Kovalchuka333a452020-07-09 16:55:58 +030075 struct boot_rsp rsp;
Roman Okhrimenkoff026122020-09-23 12:58:07 +030076 cy_rslt_t rc = CY_RSLT_TYPE_ERROR;
77 bool boot_succeeded = false;
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +020078
Bohdan Kovalchuk77256522020-04-15 18:03:43 +030079 init_cycfg_clocks();
80 init_cycfg_peripherals();
81 init_cycfg_pins();
dmiv8672c8e2020-09-16 12:59:20 +030082
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +020083 /* enable interrupts */
84 __enable_irq();
85
Bohdan Kovalchuk77256522020-04-15 18:03:43 +030086 /* Initialize retarget-io to use the debug UART port (CYBSP_UART_HW) */
Roman Okhrimenkoff026122020-09-23 12:58:07 +030087 rc = cy_retarget_io_pdl_init(115200u);
dmiv8672c8e2020-09-16 12:59:20 +030088
Roman Okhrimenkoff026122020-09-23 12:58:07 +030089 if (rc != CY_RSLT_SUCCESS)
dmiv8672c8e2020-09-16 12:59:20 +030090 {
91 CY_ASSERT(0);
92 }
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +020093
94 BOOT_LOG_INF("MCUBoot Bootloader Started");
95
Bohdan Kovalchuk0324f1b2020-05-26 08:04:24 -050096#ifdef CY_BOOT_USE_EXTERNAL_FLASH
Roman Okhrimenkoff026122020-09-23 12:58:07 +030097 rc = CY_SMIF_CMD_NOT_FOUND;
Bohdan Kovalchuka333a452020-07-09 16:55:58 +030098
99 #undef MCUBOOT_MAX_IMG_SECTORS
100 /* redefine number of sectors as there 2MB will be
101 * available on PSoC062-2M in case of external
102 * memory usage */
103 #define MCUBOOT_MAX_IMG_SECTORS 4096
Bohdan Kovalchuk0324f1b2020-05-26 08:04:24 -0500104 rc = qspi_init_sfdp(smif_id);
dmiv8672c8e2020-09-16 12:59:20 +0300105 if (rc == CY_SMIF_SUCCESS)
Bohdan Kovalchuk0324f1b2020-05-26 08:04:24 -0500106 {
107 BOOT_LOG_INF("External Memory initialized w/ SFDP.");
108 }
109 else
110 {
111 BOOT_LOG_ERR("External Memory initialization w/ SFDP FAILED: 0x%02x", (int)rc);
112 }
Roman Okhrimenkoff026122020-09-23 12:58:07 +0300113 if (CY_SMIF_SUCCESS == rc)
Bohdan Kovalchuk0324f1b2020-05-26 08:04:24 -0500114#endif
115 {
dmiv8672c8e2020-09-16 12:59:20 +0300116 if (boot_go(&rsp) == 0)
117 {
Bohdan Kovalchuk0324f1b2020-05-26 08:04:24 -0500118 BOOT_LOG_INF("User Application validated successfully");
119 do_boot(&rsp);
Roman Okhrimenkoff026122020-09-23 12:58:07 +0300120 boot_succeeded = true;
dmiv8672c8e2020-09-16 12:59:20 +0300121 }
122 else
123 {
124 BOOT_LOG_INF("MCUBoot Bootloader found none of bootable images");
125 }
Bohdan Kovalchuk0324f1b2020-05-26 08:04:24 -0500126 }
dmiv8672c8e2020-09-16 12:59:20 +0300127
128 while (1)
129 {
Roman Okhrimenkoff026122020-09-23 12:58:07 +0300130 if (boot_succeeded) {
131 Cy_SysPm_CpuEnterDeepSleep(CY_SYSPM_WAIT_FOR_INTERRUPT);
132 }
133 else {
134 __WFI();
135 }
dmiv8672c8e2020-09-16 12:59:20 +0300136 }
137
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +0200138 return 0;
139}
dmiv8672c8e2020-09-16 12:59:20 +0300140
141void hw_deinit(void)
142{
143 cy_retarget_io_pdl_deinit();
144 Cy_GPIO_Port_Deinit(CYBSP_UART_RX_PORT);
145 Cy_GPIO_Port_Deinit(CYBSP_UART_TX_PORT);
146
147#ifdef CY_BOOT_USE_EXTERNAL_FLASH
148 qspi_deinit(smif_id);
149#endif
150}