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)..."); |
| 51 | Cy_SysLib_Delay(100); |
| 52 | |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 53 | Cy_SysEnableCM4(app_addr); |
| 54 | |
| 55 | while (1) |
| 56 | { |
| 57 | __WFI() ; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | int main(void) |
| 62 | { |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 63 | struct boot_rsp rsp ; |
Bohdan Kovalchuk | 0324f1b | 2020-05-26 08:04:24 -0500 | [diff] [blame] | 64 | cy_rslt_t rc = !CY_RSLT_SUCCESS; |
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 |
| 78 | int smif_id = 1; /* Assume SlaveSelect_0 is used for External Memory */ |
| 79 | /* Acceptable values are: |
| 80 | * 0 - SMIF disabled (no external memory); |
| 81 | * 1, 2, 3 or 4 - slave select line memory module is connected to. |
| 82 | */ |
| 83 | rc = qspi_init_sfdp(smif_id); |
| 84 | if(rc == CY_SMIF_SUCCESS) |
| 85 | { |
| 86 | BOOT_LOG_INF("External Memory initialized w/ SFDP."); |
| 87 | } |
| 88 | else |
| 89 | { |
| 90 | BOOT_LOG_ERR("External Memory initialization w/ SFDP FAILED: 0x%02x", (int)rc); |
| 91 | } |
| 92 | if(0 == rc) |
| 93 | #endif |
| 94 | { |
| 95 | if (boot_go(&rsp) == 0) { |
| 96 | BOOT_LOG_INF("User Application validated successfully"); |
| 97 | do_boot(&rsp); |
| 98 | } else |
| 99 | BOOT_LOG_INF("MCUBoot Bootloader found none of bootable images") ; |
| 100 | } |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 101 | return 0; |
| 102 | } |