blob: 8976d3177814fb2560e7de6c5eab88f72980ba2e [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
44static void do_boot(struct boot_rsp *rsp)
45{
46 uint32_t app_addr = 0;
47
48 app_addr = (rsp->br_image_off + rsp->br_hdr->ih_hdr_size);
49
50 BOOT_LOG_INF("Starting User Application on CM4 (wait)...");
Bohdan Kovalchuka333a452020-07-09 16:55:58 +030051 BOOT_LOG_INF("Start Address: 0x%08x", app_addr);
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +020052 Cy_SysLib_Delay(100);
53
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +020054 Cy_SysEnableCM4(app_addr);
55
56 while (1)
57 {
58 __WFI() ;
59 }
60}
61
62int main(void)
63{
Bohdan Kovalchuka333a452020-07-09 16:55:58 +030064 struct boot_rsp rsp;
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +020065
Bohdan Kovalchuk77256522020-04-15 18:03:43 +030066 init_cycfg_clocks();
67 init_cycfg_peripherals();
68 init_cycfg_pins();
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +020069 /* enable interrupts */
70 __enable_irq();
71
Bohdan Kovalchuk77256522020-04-15 18:03:43 +030072 /* Initialize retarget-io to use the debug UART port (CYBSP_UART_HW) */
73 cy_retarget_io_pdl_init(115200u);
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +020074
75 BOOT_LOG_INF("MCUBoot Bootloader Started");
76
Bohdan Kovalchuk0324f1b2020-05-26 08:04:24 -050077#ifdef CY_BOOT_USE_EXTERNAL_FLASH
Bohdan Kovalchuka333a452020-07-09 16:55:58 +030078 cy_rslt_t rc = !CY_RSLT_SUCCESS;
79
80 #undef MCUBOOT_MAX_IMG_SECTORS
81 /* redefine number of sectors as there 2MB will be
82 * available on PSoC062-2M in case of external
83 * memory usage */
84 #define MCUBOOT_MAX_IMG_SECTORS 4096
Bohdan Kovalchuk0324f1b2020-05-26 08:04:24 -050085 int smif_id = 1; /* Assume SlaveSelect_0 is used for External Memory */
86 /* Acceptable values are:
87 * 0 - SMIF disabled (no external memory);
88 * 1, 2, 3 or 4 - slave select line memory module is connected to.
89 */
90 rc = qspi_init_sfdp(smif_id);
91 if(rc == CY_SMIF_SUCCESS)
92 {
93 BOOT_LOG_INF("External Memory initialized w/ SFDP.");
94 }
95 else
96 {
97 BOOT_LOG_ERR("External Memory initialization w/ SFDP FAILED: 0x%02x", (int)rc);
98 }
99 if(0 == rc)
100#endif
101 {
102 if (boot_go(&rsp) == 0) {
103 BOOT_LOG_INF("User Application validated successfully");
104 do_boot(&rsp);
105 } else
106 BOOT_LOG_INF("MCUBoot Bootloader found none of bootable images") ;
107 }
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +0200108 return 0;
109}