Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 1 | /***************************************************************************//** |
| 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 Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 20 | /* Cypress pdl headers */ |
| 21 | #include "cy_pdl.h" |
Bohdan Kovalchuk | 7725652 | 2020-04-15 18:03:43 +0300 | [diff] [blame] | 22 | #include "cy_retarget_io_pdl.h" |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 23 | #include "cy_result.h" |
| 24 | |
Bohdan Kovalchuk | 0324f1b | 2020-05-26 08:04:24 -0500 | [diff] [blame] | 25 | #include "cycfg_clocks.h" |
| 26 | #include "cycfg_peripherals.h" |
| 27 | #include "cycfg_pins.h" |
| 28 | |
| 29 | #include "flash_qspi.h" |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 30 | #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 Kovalchuk | 7725652 | 2020-04-15 18:03:43 +0300 | [diff] [blame] | 40 | #define CYBSP_UART_ENABLED 1U |
| 41 | #define CYBSP_UART_HW SCB5 |
| 42 | #define CYBSP_UART_IRQ scb_5_interrupt_IRQn |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 43 | |
| 44 | static 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 Kovalchuk | a333a45 | 2020-07-09 16:55:58 +0300 | [diff] [blame^] | 51 | BOOT_LOG_INF("Start Address: 0x%08x", app_addr); |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 52 | Cy_SysLib_Delay(100); |
| 53 | |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 54 | Cy_SysEnableCM4(app_addr); |
| 55 | |
| 56 | while (1) |
| 57 | { |
| 58 | __WFI() ; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | int main(void) |
| 63 | { |
Bohdan Kovalchuk | a333a45 | 2020-07-09 16:55:58 +0300 | [diff] [blame^] | 64 | struct boot_rsp rsp; |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 65 | |
Bohdan Kovalchuk | 7725652 | 2020-04-15 18:03:43 +0300 | [diff] [blame] | 66 | init_cycfg_clocks(); |
| 67 | init_cycfg_peripherals(); |
| 68 | init_cycfg_pins(); |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 69 | /* enable interrupts */ |
| 70 | __enable_irq(); |
| 71 | |
Bohdan Kovalchuk | 7725652 | 2020-04-15 18:03:43 +0300 | [diff] [blame] | 72 | /* Initialize retarget-io to use the debug UART port (CYBSP_UART_HW) */ |
| 73 | cy_retarget_io_pdl_init(115200u); |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 74 | |
| 75 | BOOT_LOG_INF("MCUBoot Bootloader Started"); |
| 76 | |
Bohdan Kovalchuk | 0324f1b | 2020-05-26 08:04:24 -0500 | [diff] [blame] | 77 | #ifdef CY_BOOT_USE_EXTERNAL_FLASH |
Bohdan Kovalchuk | a333a45 | 2020-07-09 16:55:58 +0300 | [diff] [blame^] | 78 | 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 Kovalchuk | 0324f1b | 2020-05-26 08:04:24 -0500 | [diff] [blame] | 85 | 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 Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 108 | return 0; |
| 109 | } |