diff --git a/app/board.h b/app/board.h index 60bffea..c7e14f4 100644 --- a/app/board.h +++ b/app/board.h @@ -16,24 +16,10 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -#ifndef __TARGET_INIT_H -#define __TARGET_INIT_H - -#include "stm32h7xx_hal.h" -#include "stm32h7xx_hal_mdma.h" -#include "stm32h7xx_hal_qspi.h" -#if MCUBOOT_APPLICATION_DFU -#include "usbd_core.h" -#include "usbd_desc.h" -#include "usbd_dfu.h" -#include "usbd_dfu_flash.h" -#endif +#ifndef __BOARD_H +#define __BOARD_H -#if MCUBOOT_APPLICATION_DFU -#define APP_DEFAULT_ADD USBD_DFU_APP_DEFAULT_ADD -#else -#define APP_DEFAULT_ADD 0x08040000 -#endif +#define BOARD_APP_DEFAULT_ADD 0x08040000 #define BOOTLOADER_CONFIG_MAGIC 0xA0 #define BOOTLOADER_VERSION 4 @@ -77,10 +63,76 @@ #define BOARD_EXTCLOCK 25 #endif -int target_debug_init(void); -int target_loop(void); -int target_debug(void); -int target_led_off(void); -int target_empty_keys(void); +#define BOARD_GREEN_LED PK_6 +#define BOARD_RED_LED PK_5 +#define BOARD_BLUE_LED PK_7 + +#define BOARD_BOOT_SEL PI_8 +#define BOARD_USB_RESET PJ_4 + +#ifdef BOARD_HAS_VIDEO +#define BOARD_VIDEO_ENABLE PJ_2 +#define BOARD_VIDEO_RESET PJ_3 +#endif -#endif /* __TARGET_INIT_H */ +#define BOARD_I2C_SCL PB_6 +#define BOARD_I2C_SDA PB_7 + +#define BOARD_USBD_VID 0x2341 +#define BOARD_USBD_PID 0x035B + +#define BOARD_USBD_STRING "Portenta H7 MCUboot" + +#define BOARD_QSPI_SO0 PD_11 +#define BOARD_QSPI_SO1 PD_12 +#define BOARD_QSPI_SO2 PF_7 +#define BOARD_QSPI_SO3 PD_13 +#define BOARD_QSPI_SCK PF_10 +#define BOARD_QSPI_CS PG_6 + +#define BOARD_USB_OTG_FS_DM_DP_PIN (GPIO_PIN_11 | GPIO_PIN_12) +#define BOARD_USB_OTG_FS_DM_DP_MODE (GPIO_MODE_AF_PP) +#define BOARD_USB_OTG_FS_DM_DP_PULL (GPIO_NOPULL) +#define BOARD_USB_OTG_FS_DM_DP_SPEED (GPIO_SPEED_FREQ_VERY_HIGH) +#define BOARD_USB_OTG_FS_DM_DP_ALTERNATE (GPIO_AF10_OTG1_FS) +#define BOARD_USB_OTG_FS_DM_DP_GPIO (GPIOA) + +#define BOARD_USB_OTG_HS_CLK_PIN (GPIO_PIN_5) +#define BOARD_USB_OTG_HS_CLK_MODE (GPIO_MODE_AF_PP) +#define BOARD_USB_OTG_HS_CLK_PULL (GPIO_NOPULL) +#define BOARD_USB_OTG_HS_CLK_SPEED (GPIO_SPEED_FREQ_VERY_HIGH) +#define BOARD_USB_OTG_HS_CLK_ALTERNATE (GPIO_AF10_OTG2_HS) +#define BOARD_USB_OTG_HS_CLK_GPIO (GPIOA) + +#define BOARD_USB_OTG_HS_D0_PIN (GPIO_PIN_3) +#define BOARD_USB_OTG_HS_D0_MODE (GPIO_MODE_AF_PP) +#define BOARD_USB_OTG_HS_D0_PULL (GPIO_NOPULL) +#define BOARD_USB_OTG_HS_D0_SPEED (GPIO_SPEED_FREQ_VERY_HIGH) +#define BOARD_USB_OTG_HS_D0_ALTERNATE (GPIO_AF10_OTG2_HS) +#define BOARD_USB_OTG_HS_D0_GPIO (GPIOA) + +#define BOARD_USB_OTG_HS_D1_D7_PIN (GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_5 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13) +#define BOARD_USB_OTG_HS_D1_D7_MODE (GPIO_MODE_AF_PP) +#define BOARD_USB_OTG_HS_D1_D7_PULL (GPIO_NOPULL) +#define BOARD_USB_OTG_HS_D1_D7_ALTERNATE (GPIO_AF10_OTG2_HS) +#define BOARD_USB_OTG_HS_D1_D7_GPIO (GPIOB) + +#define BOARD_USB_OTG_HS_STP_PIN (GPIO_PIN_0) +#define BOARD_USB_OTG_HS_STP_MODE (GPIO_MODE_AF_PP) +#define BOARD_USB_OTG_HS_STP_PULL (GPIO_NOPULL) +#define BOARD_USB_OTG_HS_STP_ALTERNATE (GPIO_AF10_OTG2_HS) +#define BOARD_USB_OTG_HS_STP_GPIO (GPIOC) + +#define BOARD_USB_OTG_HS_NXT_PIN (GPIO_PIN_4) +#define BOARD_USB_OTG_HS_NXT_MODE (GPIO_MODE_AF_PP) +#define BOARD_USB_OTG_HS_NXT_PULL (GPIO_NOPULL) +#define BOARD_USB_OTG_HS_NXT_ALTERNATE (GPIO_AF10_OTG2_HS) +#define BOARD_USB_OTG_HS_NXT_GPIO (GPIOH) + +#define BOARD_USB_OTG_HS_DIR_PIN (GPIO_PIN_11) +#define BOARD_USB_OTG_HS_DIR_MODE (GPIO_MODE_AF_PP) +#define BOARD_USB_OTG_HS_DIR_PULL (GPIO_NOPULL) +#define BOARD_USB_OTG_HS_DIR_ALTERNATE (GPIO_AF10_OTG2_HS) +#define BOARD_USB_OTG_HS_DIR_GPIO (GPIOI) + +#endif /* __BOARD_H */ diff --git a/app/bootutil/bootutil_extra.c b/app/bootutil/bootutil_extra.c index a29fbe9..2d2595a 100644 --- a/app/bootutil/bootutil_extra.c +++ b/app/bootutil/bootutil_extra.c @@ -31,3 +31,21 @@ int boot_set_debug(int enable) { return RTCSetBKPRegister(RTC_BKP_DR7, rtc_reg); } + +int boot_empty_keys() { + unsigned int i; + uint8_t* encript_key = (uint8_t*)(0x08000300); + uint8_t* signing_key = (uint8_t*)(0x08000400); + + for(i = 0; i < 256; i++) { + if(encript_key[i] != 0xFF) + return 0; + } + + for(i = 0; i < 256; i++) { + if(signing_key[i] != 0xFF) + return 0; + } + + return 1; +} diff --git a/app/bootutil/bootutil_extra.h b/app/bootutil/bootutil_extra.h index fae946f..2906db2 100644 --- a/app/bootutil/bootutil_extra.h +++ b/app/bootutil/bootutil_extra.h @@ -21,4 +21,14 @@ int boot_set_debug(int enable); +#ifdef __cplusplus +extern "C" { +#endif + +int boot_empty_keys(); + +#ifdef __cplusplus +} +#endif + #endif //__BOOTUTIL_EXTRA_H diff --git a/app/default_bd.cpp b/app/default_bd.cpp index 56a834b..8baf7a1 100644 --- a/app/default_bd.cpp +++ b/app/default_bd.cpp @@ -21,6 +21,7 @@ #include "ota.h" #include "rtc.h" #include "board.h" +#include "bootutil/bootutil_extra.h" #include "bootutil/bootutil_log.h" #include "SlicingBlockDevice.h" @@ -362,7 +363,7 @@ static void initBlockTable(void) { mbed::BlockDevice* get_secondary_bd(void) { - if(!target_empty_keys()) { + if(!boot_empty_keys()) { if(!BlockTableLoaded) { initBlockTable(); BlockTableLoaded = true; @@ -379,7 +380,7 @@ mbed::BlockDevice* get_secondary_bd(void) { mbed::BlockDevice* get_scratch_bd(void) { - if(!target_empty_keys()) { + if(!boot_empty_keys()) { if(!BlockTableLoaded) { initBlockTable(); BlockTableLoaded = true; @@ -396,7 +397,7 @@ mbed::BlockDevice* get_scratch_bd(void) { mbed::BlockDevice* BlockDevice::get_default_instance() { - static QSPIFBlockDevice default_bd(PD_11, PD_12, PF_7, PD_13, PF_10, PG_6, QSPIF_POLARITY_MODE_1, 40000000); + static QSPIFBlockDevice default_bd(BOARD_QSPI_SO0, BOARD_QSPI_SO1, BOARD_QSPI_SO2, BOARD_QSPI_SO3, BOARD_QSPI_SCK, BOARD_QSPI_CS, QSPIF_POLARITY_MODE_1, 40000000); return &default_bd; } diff --git a/app/dfu/usbd_conf.c b/app/dfu/usbd_conf.c index b8c028e..da6a0d6 100644 --- a/app/dfu/usbd_conf.c +++ b/app/dfu/usbd_conf.c @@ -21,6 +21,9 @@ /* Includes ------------------------------------------------------------------ */ #include "board.h" +#include "usbd_core.h" +#include "usbd_desc.h" +#include "usbd_conf.h" /* Private typedef ----------------------------------------------------------- */ /* Private define ------------------------------------------------------------ */ @@ -34,6 +37,10 @@ PCD_HandleTypeDef hpcd; /******************************************************************************* PCD BSP Routines *******************************************************************************/ +PCD_HandleTypeDef * HAL_PCD_GetHandle(void) +{ + return &hpcd; +} /** * @brief Initializes the PCD MSP. @@ -49,12 +56,12 @@ void HAL_PCD_MspInit(PCD_HandleTypeDef * hpcd) __HAL_RCC_GPIOA_CLK_ENABLE(); /* Configure DM DP Pins */ - GPIO_InitStruct.Pin = (GPIO_PIN_11 | GPIO_PIN_12); - GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; - GPIO_InitStruct.Alternate = GPIO_AF10_OTG1_FS; - HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + GPIO_InitStruct.Pin = BOARD_USB_OTG_FS_DM_DP_PIN; + GPIO_InitStruct.Mode = BOARD_USB_OTG_FS_DM_DP_MODE; + GPIO_InitStruct.Pull = BOARD_USB_OTG_FS_DM_DP_PULL; + GPIO_InitStruct.Speed = BOARD_USB_OTG_FS_DM_DP_SPEED; + GPIO_InitStruct.Alternate = BOARD_USB_OTG_FS_DM_DP_ALTERNATE; + HAL_GPIO_Init(BOARD_USB_OTG_FS_DM_DP_GPIO, &GPIO_InitStruct); RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0}; @@ -103,49 +110,48 @@ void HAL_PCD_MspInit(PCD_HandleTypeDef * hpcd) __GPIOI_CLK_ENABLE(); /* CLK */ - GPIO_InitStruct.Pin = GPIO_PIN_5; - GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; - GPIO_InitStruct.Alternate = GPIO_AF10_OTG2_HS; - HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + GPIO_InitStruct.Pin = BOARD_USB_OTG_HS_CLK_PIN; + GPIO_InitStruct.Mode = BOARD_USB_OTG_HS_CLK_MODE; + GPIO_InitStruct.Pull = BOARD_USB_OTG_HS_CLK_PULL; + GPIO_InitStruct.Speed = BOARD_USB_OTG_HS_CLK_SPEED; + GPIO_InitStruct.Alternate = BOARD_USB_OTG_HS_CLK_ALTERNATE; + HAL_GPIO_Init(BOARD_USB_OTG_HS_CLK_GPIO, &GPIO_InitStruct); /* D0 */ - GPIO_InitStruct.Pin = GPIO_PIN_3; - GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; - GPIO_InitStruct.Alternate = GPIO_AF10_OTG2_HS; - HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + GPIO_InitStruct.Pin = BOARD_USB_OTG_HS_D0_PIN; + GPIO_InitStruct.Mode = BOARD_USB_OTG_HS_D0_MODE; + GPIO_InitStruct.Pull = BOARD_USB_OTG_HS_D0_PULL; + GPIO_InitStruct.Speed = BOARD_USB_OTG_HS_D0_SPEED; + GPIO_InitStruct.Alternate = BOARD_USB_OTG_HS_D0_ALTERNATE; + HAL_GPIO_Init(BOARD_USB_OTG_HS_D0_GPIO, &GPIO_InitStruct); /* D1 D2 D3 D4 D5 D6 D7 */ - GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_5 | - GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13; - GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Alternate = GPIO_AF10_OTG2_HS; - HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + GPIO_InitStruct.Pin = BOARD_USB_OTG_HS_D1_D7_PIN; + GPIO_InitStruct.Mode = BOARD_USB_OTG_HS_D1_D7_MODE; + GPIO_InitStruct.Pull = BOARD_USB_OTG_HS_D1_D7_PULL; + GPIO_InitStruct.Alternate = BOARD_USB_OTG_HS_D1_D7_ALTERNATE; + HAL_GPIO_Init(BOARD_USB_OTG_HS_D1_D7_GPIO, &GPIO_InitStruct); /* STP */ - GPIO_InitStruct.Pin = GPIO_PIN_0; - GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Alternate = GPIO_AF10_OTG2_HS; - HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); + GPIO_InitStruct.Pin = BOARD_USB_OTG_HS_STP_PIN; + GPIO_InitStruct.Mode = BOARD_USB_OTG_HS_STP_MODE; + GPIO_InitStruct.Pull = BOARD_USB_OTG_HS_STP_PULL; + GPIO_InitStruct.Alternate = BOARD_USB_OTG_HS_STP_ALTERNATE; + HAL_GPIO_Init(BOARD_USB_OTG_HS_STP_GPIO, &GPIO_InitStruct); /* NXT */ - GPIO_InitStruct.Pin = GPIO_PIN_4; - GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Alternate = GPIO_AF10_OTG2_HS; - HAL_GPIO_Init(GPIOH, &GPIO_InitStruct); + GPIO_InitStruct.Pin = BOARD_USB_OTG_HS_NXT_PIN; + GPIO_InitStruct.Mode = BOARD_USB_OTG_HS_NXT_MODE; + GPIO_InitStruct.Pull = BOARD_USB_OTG_HS_NXT_PULL; + GPIO_InitStruct.Alternate = BOARD_USB_OTG_HS_NXT_ALTERNATE; + HAL_GPIO_Init(BOARD_USB_OTG_HS_NXT_GPIO, &GPIO_InitStruct); /* DIR */ - GPIO_InitStruct.Pin = GPIO_PIN_11; - GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Alternate = GPIO_AF10_OTG2_HS; - HAL_GPIO_Init(GPIOI, &GPIO_InitStruct); + GPIO_InitStruct.Pin = BOARD_USB_OTG_HS_DIR_PIN; + GPIO_InitStruct.Mode = BOARD_USB_OTG_HS_DIR_MODE; + GPIO_InitStruct.Pull = BOARD_USB_OTG_HS_DIR_PULL; + GPIO_InitStruct.Alternate = BOARD_USB_OTG_HS_DIR_ALTERNATE; + HAL_GPIO_Init(BOARD_USB_OTG_HS_DIR_GPIO, &GPIO_InitStruct); __HAL_RCC_USB1_OTG_HS_ULPI_CLK_ENABLE(); /* Enable USB HS Clocks */ diff --git a/app/dfu/usbd_conf.h b/app/dfu/usbd_conf.h index 8c11db9..17c986e 100644 --- a/app/dfu/usbd_conf.h +++ b/app/dfu/usbd_conf.h @@ -22,6 +22,7 @@ #define __USBD_CONF_H /* Includes ------------------------------------------------------------------*/ +#include "board.h" #include "stm32h7xx_hal.h" #include #include @@ -40,7 +41,7 @@ /* DFU Class Config */ #define USBD_DFU_MAX_ITF_NUM 4 #define USBD_DFU_XFER_SIZE 4096 /* Max DFU Packet Size = 4096 bytes */ -#define USBD_DFU_APP_DEFAULT_ADD 0x08040000 /* The first sector (32 KB) is reserved for DFU code */ +#define USBD_DFU_APP_DEFAULT_ADD BOARD_APP_DEFAULT_ADD /* The first sector (32 KB) is reserved for DFU code */ #define USBD_DFU_MAX_NB_OF_SECTORS 16 /* Max number of sectors */ #define CDC_CLASS_DESC_SIZE (8 + 58) @@ -80,6 +81,7 @@ #endif /* Exported functions ------------------------------------------------------- */ +extern PCD_HandleTypeDef * HAL_PCD_GetHandle(void); #endif /* __USBD_CONF_H */ diff --git a/app/dfu/usbd_desc.c b/app/dfu/usbd_desc.c index 16a3196..2cc07a7 100644 --- a/app/dfu/usbd_desc.c +++ b/app/dfu/usbd_desc.c @@ -20,18 +20,19 @@ #if MCUBOOT_APPLICATION_HOOKS && MCUBOOT_APPLICATION_DFU /* Includes ------------------------------------------------------------------ */ +#include "board.h" #include "usbd_core.h" #include "usbd_desc.h" #include "usbd_conf.h" /* Private typedef ----------------------------------------------------------- */ /* Private define ------------------------------------------------------------ */ -#define USBD_VID 0x2341 -#define USBD_PID 0x035B +#define USBD_VID BOARD_USBD_VID +#define USBD_PID BOARD_USBD_PID #define USBD_LANGID_STRING 0x409 #define USBD_MANUFACTURER_STRING "Arduino SA" -#define USBD_PRODUCT_HS_STRING "Portenta MCUBoot" -#define USBD_PRODUCT_FS_STRING "Portenta MCUBoot" +#define USBD_PRODUCT_HS_STRING BOARD_USBD_STRING +#define USBD_PRODUCT_FS_STRING BOARD_USBD_STRING #define USBD_CONFIGURATION_HS_STRING "DFU Config" #define USBD_INTERFACE_HS_STRING "DFU Interface" #define USBD_CONFIGURATION_FS_STRING "DFU Config" diff --git a/app/dfu/usbd_dfu_flash.h b/app/dfu/usbd_dfu_flash.h index 09e18fb..7f1eb42 100644 --- a/app/dfu/usbd_dfu_flash.h +++ b/app/dfu/usbd_dfu_flash.h @@ -53,6 +53,7 @@ extern USBD_DFU_MediaTypeDef USBD_DFU_Flash_fops; #define FLASH_END_ADDR (uint32_t)(0x081FFFFF) /* Exported functions ------------------------------------------------------- */ +extern void init_Memories(void); #endif /* __USBD_DFU_FLASH_H_ */ diff --git a/app/main.cpp b/app/main.cpp index 4f73c12..e6d533b 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -22,15 +22,26 @@ #include "board.h" #include "ota.h" #include "rtc.h" +#include "power.h" +#include "bootutil/bootutil_extra.h" #include "bootutil/bootutil_log.h" #include "bootutil/bootutil.h" #include "bootutil/image.h" #include "mbedtls/platform.h" -// clock source is selected with CLOCK_SOURCE in json config -#define USE_PLL_HSE_EXTC 0x8 // Use external clock (ST Link MCO) -#define USE_PLL_HSE_XTAL 0x4 // Use external xtal (X3 on board - not provided by default) -#define USE_PLL_HSI 0x2 // Use HSI internal clock +#if MCUBOOT_APPLICATION_DFU +#include "usbd_desc.h" +#include "usbd_dfu_flash.h" +#endif + +/* + * CLOCK_SOURCE is configured with "target.clock_source" in mbed_app.json file + */ + #define USE_PLL_HSE_EXTC 0x8 // Use external clock (ST Link MCO) + #define USE_PLL_HSE_XTAL 0x4 // Use external xtal (X3 on board - not provided by default) + #define USE_PLL_HSI 0x2 // Use HSI internal clock + +extern "C" uint8_t SetSysClock_PLL_HSE(uint8_t bypass, bool lowspeed); volatile const uint8_t bootloader_data[] __attribute__ ((section (".bootloader_version"), used)) = { BOOTLOADER_CONFIG_MAGIC, @@ -48,38 +59,40 @@ volatile const uint8_t bootloader_data[] __attribute__ ((section (".bootloader_v volatile const uint8_t bootloader_identifier[] __attribute__ ((section (".bootloader_identification"), used)) = "MCUboot Arduino"; -static bool double_tap_flag = true; -volatile uint8_t ledKeepValue = 0; -volatile uint8_t ledTargetValue = 20; -volatile int8_t ledDirection = 1; -volatile int divisor = 0; - -DigitalOut red(PK_5, 1); -DigitalOut green(PK_6, 1); -DigitalOut blue(PK_7, 1); +#if MCUBOOT_APPLICATION_DFU +USBD_HandleTypeDef USBD_Device; +#endif -DigitalIn boot_sel(PI_8,PullDown); +DigitalOut red(BOARD_RED_LED, 1); +DigitalOut green(BOARD_GREEN_LED, 1); +DigitalOut blue(BOARD_BLUE_LED, 1); +DigitalIn boot_sel(BOARD_BOOT_SEL,PullDown); Ticker swap_ticker; - bool debug_enabled = false; -static inline void swap_feedback() { +static void led_swap_feedback_off(void) { + swap_ticker.detach(); + red = 1; + green = 1; + blue = 1; +} + +static void led_swap_feedback() { blue = !blue; red = !red; } -static inline void LED_pulse(DigitalOut* led) -{ +static void led_pulse(DigitalOut* led) { + static uint8_t ledKeepValue = 0; + static uint8_t ledTargetValue = 20; + static int8_t ledDirection = 1; + static int divisor = 0; + if (divisor++ % 40) { return; } - if (HAL_GetTick() > 500 && double_tap_flag && RTCGetBKPRegister(RTC_BKP_DR0) == 0xDF59) { - RTCSetBKPRegister(RTC_BKP_DR0, 0); - double_tap_flag = false; - } - if (ledKeepValue == 0) { ledTargetValue += ledDirection; *led = !*led; @@ -107,40 +120,63 @@ static bool valid_application() { } -int target_empty_keys() { - unsigned int i; - uint8_t* encript_key = (uint8_t*)(0x08000300); - uint8_t* signing_key = (uint8_t*)(0x08000400); - - for(i = 0; i < 256; i++) { - if(encript_key[i] != 0xFF) - return 0; - } - - for(i = 0; i < 256; i++) { - if(signing_key[i] != 0xFF) - return 0; - } - - return 1; -} - -int target_debug_init(void) { +static int debug_init(void) { RTCInit(); debug_enabled = ((RTCGetBKPRegister(RTC_BKP_DR7) & 0x00000001) || boot_sel); return 0; } -int target_led_off(void) { - swap_ticker.detach(); - red = 1; - green = 1; - blue = 1; +static int start_dfu(void) { + RTCSetBKPRegister(RTC_BKP_DR0, 0); + + SetSysClock_PLL_HSE(1, false); + SystemCoreClockUpdate(); + + led_swap_feedback_off(); + + //turnDownEthernet(); + +#if MCUBOOT_APPLICATION_DFU + init_Memories(); + + /* Otherwise enters DFU mode to allow user programming his application */ + /* Init Device Library */ + USBD_Init(&USBD_Device, &DFU_Desc, 0); + + /* Add Supported Class */ + USBD_RegisterClass(&USBD_Device, USBD_DFU_CLASS); + + /* Add DFU Media interface */ + USBD_DFU_RegisterMedia(&USBD_Device, &USBD_DFU_Flash_fops); + + /* Start Device Process */ + USBD_Start(&USBD_Device); + + /* Set USBHS Interrupt to the lowest priority */ + // HAL_NVIC_SetPriority(OTG_HS_IRQn, 1, 0); + + /* Enable USBHS Interrupt */ + HAL_NVIC_DisableIRQ(OTG_HS_IRQn); + HAL_NVIC_DisableIRQ(OTG_FS_IRQn); +#endif + + while(1) { +#if MCUBOOT_APPLICATION_DFU +#ifdef USE_USB_HS + if (USB_OTG_HS->GINTSTS & USB_OTG_HS->GINTMSK) { +#else // USE_USB_FS + if (USB_OTG_FS->GINTSTS & USB_OTG_FS->GINTMSK) { +#endif + HAL_PCD_IRQHandler(HAL_PCD_GetHandle()); + } +#endif + led_pulse(&green); + } + return 0; } int start_secure_application(void) { - int rc; BOOT_LOG_INF("Starting MCUboot"); @@ -160,7 +196,7 @@ int start_secure_application(void) { return -1; } - target_led_off(); + led_swap_feedback_off(); // Run the application in the primary slot // Add header size offset to calculate the actual start address of application @@ -169,9 +205,17 @@ int start_secure_application(void) { mbed_start_application(address); } -int main(void) { +static int start_ota(void) { + // DR1 contains the backing storage type, DR2 the offset in case of raw device / MBR + storageType storage_type = (storageType)RTCGetBKPRegister(RTC_BKP_DR1); + uint32_t offset = RTCGetBKPRegister(RTC_BKP_DR2); + uint32_t update_size = RTCGetBKPRegister(RTC_BKP_DR3); + BOOT_LOG_INF("Start OTA 0x%X 0x%X 0x%X", storage_type, offset, update_size); + return tryOTA(storage_type, offset, update_size); +} - target_debug_init(); +int main(void) { + debug_init(); BOOT_LOG_INF("Starting Arduino bootloader"); @@ -185,84 +229,18 @@ int main(void) { HAL_Delay(500); } - DigitalOut usb_reset(PJ_4, 0); - DigitalOut video_enable(PJ_2, 0); - DigitalOut video_reset(PJ_3, 0); + DigitalOut usb_reset(BOARD_USB_RESET, 0); +#if BOARD_HAS_VIDEO + DigitalOut video_enable(BOARD_VIDEO_ENABLE, 0); + DigitalOut video_reset(BOARD_VIDEO_RESET, 0); +#endif //Ticker pulse; //DigitalOut eth_rst(PJ_15, 1); HAL_FLASH_Unlock(); - I2C i2c(PB_7, PB_6); - - char data[2]; - - // LDO2 to 1.8V - data[0]=0x4F; - data[1]=0x0; - i2c.write(8 << 1, data, sizeof(data)); - data[0]=0x50; - data[1]=0xF; - i2c.write(8 << 1, data, sizeof(data)); - - // LDO1 to 1.0V - data[0]=0x4c; - data[1]=0x5; - i2c.write(8 << 1, data, sizeof(data)); - data[0]=0x4d; - data[1]=0x3; - i2c.write(8 << 1, data, sizeof(data)); - - // LDO3 to 1.2V - data[0]=0x52; - data[1]=0x9; - i2c.write(8 << 1, data, sizeof(data)); - data[0]=0x53; - data[1]=0xF; - i2c.write(8 << 1, data, sizeof(data)); - - HAL_Delay(10); - - data[0]=0x9C; - data[1]=(1 << 7); - i2c.write(8 << 1, data, sizeof(data)); - - // Disable charger led - data[0]=0x9E; - data[1]=(1 << 5); - i2c.write(8 << 1, data, sizeof(data)); - - HAL_Delay(10); - - // SW3: set 2A as current limit - // Helps keeping the rail up at wifi startup - data[0]=0x42; - data[1]=(2); - i2c.write(8 << 1, data, sizeof(data)); - - HAL_Delay(10); - - // Change VBUS INPUT CURRENT LIMIT to 1.5A - data[0]=0x94; - data[1]=(20 << 3); - i2c.write(8 << 1, data, sizeof(data)); - -#if 1 - // SW2 to 3.3V (SW2_VOLT) - data[0]=0x3B; - data[1]=0xF; - i2c.write(8 << 1, data, sizeof(data)); - - // SW1 to 3.0V (SW1_VOLT) - data[0]=0x35; - data[1]=0xF; - i2c.write(8 << 1, data, sizeof(data)); - - //data[0]=0x36; - //data[1]=(2); - //i2c.write(8 << 1, data, sizeof(data)); -#endif + power_init(); HAL_Delay(10); @@ -275,16 +253,11 @@ int main(void) { HAL_Delay(10); if (magic != 0xDF59) { - if (target_empty_keys()) { + if (boot_empty_keys()) { BOOT_LOG_INF("Secure keys not configured"); if ( magic == 0x07AA ) { - /* Try unsecure OTA */ - // DR1 contains the backing storage type, DR2 the offset in case of raw device / MBR - storageType storage_type = (storageType)RTCGetBKPRegister(RTC_BKP_DR1); - uint32_t offset = RTCGetBKPRegister(RTC_BKP_DR2); - uint32_t update_size = RTCGetBKPRegister(RTC_BKP_DR3); - BOOT_LOG_INF("Start OTA 0x%X 0x%X 0x%X", storage_type, offset, update_size); - int ota_result = tryOTA(storage_type, offset, update_size); + /* Start OTA */ + int ota_result = start_ota(); if (ota_result == 0) { // clean reboot with success flag BOOT_LOG_INF("Sketch updated"); @@ -299,81 +272,21 @@ int main(void) { if (valid_application()) { /* Boot Sketch */ - BOOT_LOG_INF("Booting sketch at 0x%x\n", APP_DEFAULT_ADD); + BOOT_LOG_INF("Booting sketch at 0x%x\n", BOARD_APP_DEFAULT_ADD); RTCSetBKPRegister(RTC_BKP_DR0, 0); - mbed_start_application(APP_DEFAULT_ADD); + mbed_start_application(BOARD_APP_DEFAULT_ADD); } else { BOOT_LOG_INF("No sketch found"); } } else { /* MCUboot secure boot */ - swap_ticker.attach(&swap_feedback, 250ms); + swap_ticker.attach(&led_swap_feedback, 250ms); RTCSetBKPRegister(RTC_BKP_DR0, 0); start_secure_application(); } } - target_loop(); - - return 0; -} - -#if MCUBOOT_APPLICATION_DFU -USBD_HandleTypeDef USBD_Device; -extern PCD_HandleTypeDef hpcd; -extern void init_Memories(void); -#endif - -extern "C" { - uint8_t SetSysClock_PLL_HSE(uint8_t bypass, bool lowspeed); -} - -int target_loop(void) { - RTCSetBKPRegister(RTC_BKP_DR0, 0); - - SetSysClock_PLL_HSE(1, false); - SystemCoreClockUpdate(); - - target_led_off(); - - //turnDownEthernet(); - -#if MCUBOOT_APPLICATION_DFU - init_Memories(); - - /* Otherwise enters DFU mode to allow user programming his application */ - /* Init Device Library */ - USBD_Init(&USBD_Device, &DFU_Desc, 0); - - /* Add Supported Class */ - USBD_RegisterClass(&USBD_Device, USBD_DFU_CLASS); - - /* Add DFU Media interface */ - USBD_DFU_RegisterMedia(&USBD_Device, &USBD_DFU_Flash_fops); - - /* Start Device Process */ - USBD_Start(&USBD_Device); - - /* Set USBHS Interrupt to the lowest priority */ - // HAL_NVIC_SetPriority(OTG_HS_IRQn, 1, 0); - - /* Enable USBHS Interrupt */ - HAL_NVIC_DisableIRQ(OTG_HS_IRQn); - HAL_NVIC_DisableIRQ(OTG_FS_IRQn); -#endif - - while(1) { -#if MCUBOOT_APPLICATION_DFU -#ifdef USE_USB_HS - if (USB_OTG_HS->GINTSTS & USB_OTG_HS->GINTMSK) { -#else // USE_USB_FS - if (USB_OTG_FS->GINTSTS & USB_OTG_FS->GINTMSK) { -#endif - HAL_PCD_IRQHandler(&hpcd); - } -#endif - LED_pulse(&green); - } + start_dfu(); return 0; } diff --git a/app/power/power.cpp b/app/power/power.cpp new file mode 100644 index 0000000..1bdebff --- /dev/null +++ b/app/power/power.cpp @@ -0,0 +1,92 @@ +/* + Copyright (c) 2022 Arduino SA. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include "power.h" +#include "board.h" +#include "mbed.h" + +static void portenta_power_init() { + I2C i2c(BOARD_I2C_SDA, BOARD_I2C_SCL); + + char data[2]; + + // LDO2 to 1.8V + data[0]=0x4F; + data[1]=0x0; + i2c.write(8 << 1, data, sizeof(data)); + data[0]=0x50; + data[1]=0xF; + i2c.write(8 << 1, data, sizeof(data)); + + // LDO1 to 1.0V + data[0]=0x4c; + data[1]=0x5; + i2c.write(8 << 1, data, sizeof(data)); + data[0]=0x4d; + data[1]=0x3; + i2c.write(8 << 1, data, sizeof(data)); + + // LDO3 to 1.2V + data[0]=0x52; + data[1]=0x9; + i2c.write(8 << 1, data, sizeof(data)); + data[0]=0x53; + data[1]=0xF; + i2c.write(8 << 1, data, sizeof(data)); + + HAL_Delay(10); + + data[0]=0x9C; + data[1]=(1 << 7); + i2c.write(8 << 1, data, sizeof(data)); + + // Disable charger led + data[0]=0x9E; + data[1]=(1 << 5); + i2c.write(8 << 1, data, sizeof(data)); + + HAL_Delay(10); + + // SW3: set 2A as current limit + // Helps keeping the rail up at wifi startup + data[0]=0x42; + data[1]=(2); + i2c.write(8 << 1, data, sizeof(data)); + + HAL_Delay(10); + + // Change VBUS INPUT CURRENT LIMIT to 1.5A + data[0]=0x94; + data[1]=(20 << 3); + i2c.write(8 << 1, data, sizeof(data)); + + // SW2 to 3.3V (SW2_VOLT) + data[0]=0x3B; + data[1]=0xF; + i2c.write(8 << 1, data, sizeof(data)); + + // SW1 to 3.0V (SW1_VOLT) + data[0]=0x35; + data[1]=0xF; + i2c.write(8 << 1, data, sizeof(data)); +} + + +void power_init() { + portenta_power_init(); +} \ No newline at end of file diff --git a/app/power/power.h b/app/power/power.h new file mode 100644 index 0000000..f6028c3 --- /dev/null +++ b/app/power/power.h @@ -0,0 +1,24 @@ +/* + Copyright (c) 2022 Arduino SA. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef __POWER_H +#define __POWER_H + +void power_init(); + +#endif //__POWER_H \ No newline at end of file diff --git a/app/rtc/rtc.c b/app/rtc/rtc.c index 9b8dbd5..4d1777b 100644 --- a/app/rtc/rtc.c +++ b/app/rtc/rtc.c @@ -1,3 +1,21 @@ +/* + Copyright (c) 2022 Arduino SA. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + #if MCUBOOT_APPLICATION_HOOKS #include "rtc.h"