From b5d7add0fd6cb6ac25f91f7471b75d689a9ca0ad Mon Sep 17 00:00:00 2001 From: pennam Date: Mon, 4 Apr 2022 14:22:32 +0200 Subject: [PATCH 01/16] Add PIN defines to configure DFU, QSPI, LEDs and reset/enable pins --- app/board.h | 72 ++++++++++++++++++++++++++++++++++++++++++ app/default_bd.cpp | 2 +- app/dfu/usbd_conf.c | 77 ++++++++++++++++++++++----------------------- app/dfu/usbd_desc.c | 9 +++--- app/main.cpp | 18 ++++++----- 5 files changed, 126 insertions(+), 52 deletions(-) diff --git a/app/board.h b/app/board.h index 60bffea..0c6513c 100644 --- a/app/board.h +++ b/app/board.h @@ -77,6 +77,78 @@ #define BOARD_EXTCLOCK 25 #endif +#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 + +#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) + int target_debug_init(void); int target_loop(void); int target_debug(void); diff --git a/app/default_bd.cpp b/app/default_bd.cpp index 56a834b..b37f494 100644 --- a/app/default_bd.cpp +++ b/app/default_bd.cpp @@ -396,7 +396,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..6687f68 100644 --- a/app/dfu/usbd_conf.c +++ b/app/dfu/usbd_conf.c @@ -49,12 +49,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 +103,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_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/main.cpp b/app/main.cpp index 4f73c12..ed18c93 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -54,11 +54,11 @@ 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); +DigitalOut red(BOARD_RED_LED, 1); +DigitalOut green(BOARD_GREEN_LED, 1); +DigitalOut blue(BOARD_BLUE_LED, 1); -DigitalIn boot_sel(PI_8,PullDown); +DigitalIn boot_sel(BOARD_BOOT_SEL,PullDown); Ticker swap_ticker; @@ -185,16 +185,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); + I2C i2c(BOARD_I2C_SDA, BOARD_I2C_SCL); char data[2]; From e848dce708abc307554ff81b7ca079f4889d331e Mon Sep 17 00:00:00 2001 From: pennam Date: Mon, 4 Apr 2022 14:28:33 +0200 Subject: [PATCH 02/16] Remove function declarations from board.h Move target_loop definition accordingly Move and rename target_empty_keys() from main.cpp to bootutil_extra.c --- app/board.h | 6 -- app/bootutil/bootutil_extra.c | 18 +++++ app/bootutil/bootutil_extra.h | 10 +++ app/default_bd.cpp | 5 +- app/main.cpp | 141 +++++++++++++++------------------- 5 files changed, 93 insertions(+), 87 deletions(-) diff --git a/app/board.h b/app/board.h index 0c6513c..fa32bfb 100644 --- a/app/board.h +++ b/app/board.h @@ -149,10 +149,4 @@ #define BOARD_USB_OTG_HS_DIR_ALTERNATE (GPIO_AF10_OTG2_HS) #define BOARD_USB_OTG_HS_DIR_GPIO (GPIOI) -int target_debug_init(void); -int target_loop(void); -int target_debug(void); -int target_led_off(void); -int target_empty_keys(void); - #endif /* __TARGET_INIT_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 b37f494..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; diff --git a/app/main.cpp b/app/main.cpp index ed18c93..f5725dc 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -22,6 +22,7 @@ #include "board.h" #include "ota.h" #include "rtc.h" +#include "bootutil/bootutil_extra.h" #include "bootutil/bootutil_log.h" #include "bootutil/bootutil.h" #include "bootutil/image.h" @@ -107,24 +108,6 @@ 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) { RTCInit(); debug_enabled = ((RTCGetBKPRegister(RTC_BKP_DR7) & 0x00000001) || boot_sel); @@ -139,6 +122,66 @@ int target_led_off(void) { 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); + } + + return 0; +} + int start_secure_application(void) { int rc; @@ -277,7 +320,7 @@ 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 */ @@ -320,64 +363,4 @@ int main(void) { 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); - } - - return 0; -} - #endif From 75b42cb6f48e6cc352501d7b0aad71ba974013ad Mon Sep 17 00:00:00 2001 From: pennam Date: Mon, 4 Apr 2022 14:32:26 +0200 Subject: [PATCH 03/16] Rename target_loop() in start_dfu() and make function static --- app/main.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/main.cpp b/app/main.cpp index f5725dc..f6bcd49 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -132,7 +132,7 @@ extern "C" { uint8_t SetSysClock_PLL_HSE(uint8_t bypass, bool lowspeed); } -int target_loop(void) { +static int start_dfu(void) { RTCSetBKPRegister(RTC_BKP_DR0, 0); SetSysClock_PLL_HSE(1, false); @@ -358,7 +358,7 @@ int main(void) { start_secure_application(); } } - target_loop(); + start_dfu(); return 0; } From 2e96a0995961f6014bc96c532ae39067638185e8 Mon Sep 17 00:00:00 2001 From: pennam Date: Mon, 4 Apr 2022 14:52:08 +0200 Subject: [PATCH 04/16] Move PMIC initialization in a separate function and file --- app/main.cpp | 71 +--------------------------------- app/power/power.cpp | 92 +++++++++++++++++++++++++++++++++++++++++++++ app/power/power.h | 24 ++++++++++++ 3 files changed, 118 insertions(+), 69 deletions(-) create mode 100644 app/power/power.cpp create mode 100644 app/power/power.h diff --git a/app/main.cpp b/app/main.cpp index f6bcd49..34460a3 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -22,6 +22,7 @@ #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" @@ -239,75 +240,7 @@ int main(void) { HAL_FLASH_Unlock(); - 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)); - -#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); 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 From 77df2c76977fe210a055be4c92c2ebab9a2896a4 Mon Sep 17 00:00:00 2001 From: pennam Date: Mon, 4 Apr 2022 14:53:21 +0200 Subject: [PATCH 05/16] Export init_Memories() from usb_dfu_flash.h --- app/dfu/usbd_dfu_flash.h | 1 + app/main.cpp | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) 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 34460a3..916a899 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -126,7 +126,6 @@ int target_led_off(void) { #if MCUBOOT_APPLICATION_DFU USBD_HandleTypeDef USBD_Device; extern PCD_HandleTypeDef hpcd; -extern void init_Memories(void); #endif extern "C" { From 1daa19a04f5fa6c0921477256fb446b5b9051b6f Mon Sep 17 00:00:00 2001 From: pennam Date: Mon, 4 Apr 2022 15:31:42 +0200 Subject: [PATCH 06/16] Remove double_tap_flag from LED_pulse(...) function --- app/main.cpp | 6 ------ 1 file changed, 6 deletions(-) diff --git a/app/main.cpp b/app/main.cpp index 916a899..aed9e29 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -50,7 +50,6 @@ 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; @@ -77,11 +76,6 @@ static inline void LED_pulse(DigitalOut* led) 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; From f3221b2e4e8d36883c9246330f0025d5e3483d8e Mon Sep 17 00:00:00 2001 From: pennam Date: Mon, 4 Apr 2022 15:37:21 +0200 Subject: [PATCH 07/16] Uniform LED related function names --- app/main.cpp | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/app/main.cpp b/app/main.cpp index aed9e29..b7d4804 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -65,13 +65,19 @@ 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) { if (divisor++ % 40) { return; } @@ -109,13 +115,7 @@ int target_debug_init(void) { return 0; } -int target_led_off(void) { - swap_ticker.detach(); - red = 1; - green = 1; - blue = 1; - return 0; -} + #if MCUBOOT_APPLICATION_DFU USBD_HandleTypeDef USBD_Device; @@ -132,7 +132,7 @@ static int start_dfu(void) { SetSysClock_PLL_HSE(1, false); SystemCoreClockUpdate(); - target_led_off(); + led_swap_feedback_off(); //turnDownEthernet(); @@ -170,7 +170,7 @@ static int start_dfu(void) { HAL_PCD_IRQHandler(&hpcd); } #endif - LED_pulse(&green); + led_pulse(&green); } return 0; @@ -197,7 +197,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 @@ -279,7 +279,7 @@ int main(void) { } 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(); } From e171f9d556f946c5d9e03cd4f3b4eafc3e44fd14 Mon Sep 17 00:00:00 2001 From: pennam Date: Mon, 4 Apr 2022 15:43:37 +0200 Subject: [PATCH 08/16] Move led pulse variables inside led_pulse() function --- app/main.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/app/main.cpp b/app/main.cpp index b7d4804..fadd776 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -50,10 +50,7 @@ volatile const uint8_t bootloader_data[] __attribute__ ((section (".bootloader_v volatile const uint8_t bootloader_identifier[] __attribute__ ((section (".bootloader_identification"), used)) = "MCUboot Arduino"; -volatile uint8_t ledKeepValue = 0; -volatile uint8_t ledTargetValue = 20; -volatile int8_t ledDirection = 1; -volatile int divisor = 0; + DigitalOut red(BOARD_RED_LED, 1); DigitalOut green(BOARD_GREEN_LED, 1); @@ -78,6 +75,11 @@ static void led_swap_feedback() { } 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; } From 2126f7bb9c70d1a96f2cb8d725459424d3b11975 Mon Sep 17 00:00:00 2001 From: pennam Date: Mon, 4 Apr 2022 15:55:34 +0200 Subject: [PATCH 09/16] Cosmetics remove empty/double newlines --- app/main.cpp | 8 -------- 1 file changed, 8 deletions(-) diff --git a/app/main.cpp b/app/main.cpp index fadd776..3f80fe9 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -50,16 +50,12 @@ volatile const uint8_t bootloader_data[] __attribute__ ((section (".bootloader_v volatile const uint8_t bootloader_identifier[] __attribute__ ((section (".bootloader_identification"), used)) = "MCUboot Arduino"; - - 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 void led_swap_feedback_off(void) { @@ -117,8 +113,6 @@ int target_debug_init(void) { return 0; } - - #if MCUBOOT_APPLICATION_DFU USBD_HandleTypeDef USBD_Device; extern PCD_HandleTypeDef hpcd; @@ -179,7 +173,6 @@ static int start_dfu(void) { } int start_secure_application(void) { - int rc; BOOT_LOG_INF("Starting MCUboot"); @@ -209,7 +202,6 @@ int start_secure_application(void) { } int main(void) { - target_debug_init(); BOOT_LOG_INF("Starting Arduino bootloader"); From 7b12ae8d9edbee9016f29bc2cacf14a751c4ebd3 Mon Sep 17 00:00:00 2001 From: pennam Date: Mon, 4 Apr 2022 15:57:10 +0200 Subject: [PATCH 10/16] Rename target_debug_init() to debug_init() --- app/main.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/main.cpp b/app/main.cpp index 3f80fe9..02316ee 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -107,7 +107,7 @@ static bool valid_application() { } -int target_debug_init(void) { +static int debug_init(void) { RTCInit(); debug_enabled = ((RTCGetBKPRegister(RTC_BKP_DR7) & 0x00000001) || boot_sel); return 0; @@ -202,7 +202,7 @@ int start_secure_application(void) { } int main(void) { - target_debug_init(); + debug_init(); BOOT_LOG_INF("Starting Arduino bootloader"); From aa2d71a7f4d03fd578b1e7187423d21abff705dd Mon Sep 17 00:00:00 2001 From: pennam Date: Mon, 4 Apr 2022 16:08:41 +0200 Subject: [PATCH 11/16] Add start_ota function to make code more readable --- app/main.cpp | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/app/main.cpp b/app/main.cpp index 02316ee..a4e6db2 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -201,6 +201,15 @@ int start_secure_application(void) { mbed_start_application(address); } +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); +} + int main(void) { debug_init(); @@ -243,13 +252,8 @@ int main(void) { 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"); From 6a124defc3da959866c9a83be08d988c49587559 Mon Sep 17 00:00:00 2001 From: pennam Date: Mon, 4 Apr 2022 16:09:20 +0200 Subject: [PATCH 12/16] Add header to rtc.c file --- app/rtc/rtc.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) 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" From 9762c870ad7a7677bc9647ffe990eaba6a526490 Mon Sep 17 00:00:00 2001 From: pennam Date: Mon, 4 Apr 2022 16:48:05 +0200 Subject: [PATCH 13/16] Cleanup includes in board.h and fix dfu an main build accordingly --- app/board.h | 16 +--------------- app/dfu/usbd_conf.c | 3 +++ app/dfu/usbd_conf.h | 3 ++- app/main.cpp | 9 +++++++-- 4 files changed, 13 insertions(+), 18 deletions(-) diff --git a/app/board.h b/app/board.h index fa32bfb..ad79c93 100644 --- a/app/board.h +++ b/app/board.h @@ -19,21 +19,7 @@ #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 - -#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 diff --git a/app/dfu/usbd_conf.c b/app/dfu/usbd_conf.c index 6687f68..7e51cf1 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 ------------------------------------------------------------ */ diff --git a/app/dfu/usbd_conf.h b/app/dfu/usbd_conf.h index 8c11db9..e127311 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) diff --git a/app/main.cpp b/app/main.cpp index a4e6db2..a4f77bb 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -29,6 +29,11 @@ #include "bootutil/image.h" #include "mbedtls/platform.h" +#if MCUBOOT_APPLICATION_DFU +#include "usbd_desc.h" +#include "usbd_dfu_flash.h" +#endif + // 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) @@ -268,9 +273,9 @@ 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"); } From ca42da6c3af32e8ed13b0e06647fae03434da347 Mon Sep 17 00:00:00 2001 From: pennam Date: Mon, 4 Apr 2022 16:49:50 +0200 Subject: [PATCH 14/16] Fix board.h include guards define names --- app/board.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/board.h b/app/board.h index ad79c93..c7e14f4 100644 --- a/app/board.h +++ b/app/board.h @@ -16,8 +16,8 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -#ifndef __TARGET_INIT_H -#define __TARGET_INIT_H +#ifndef __BOARD_H +#define __BOARD_H #define BOARD_APP_DEFAULT_ADD 0x08040000 @@ -135,4 +135,4 @@ #define BOARD_USB_OTG_HS_DIR_ALTERNATE (GPIO_AF10_OTG2_HS) #define BOARD_USB_OTG_HS_DIR_GPIO (GPIOI) -#endif /* __TARGET_INIT_H */ +#endif /* __BOARD_H */ From f584c7319478d9ab39077cabaa80f3fe612030d3 Mon Sep 17 00:00:00 2001 From: pennam Date: Mon, 4 Apr 2022 17:19:38 +0200 Subject: [PATCH 15/16] Add function to get PCD handle and use it in dfu loop --- app/dfu/usbd_conf.c | 4 ++++ app/dfu/usbd_conf.h | 1 + app/main.cpp | 17 +++++++---------- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/app/dfu/usbd_conf.c b/app/dfu/usbd_conf.c index 7e51cf1..da6a0d6 100644 --- a/app/dfu/usbd_conf.c +++ b/app/dfu/usbd_conf.c @@ -37,6 +37,10 @@ PCD_HandleTypeDef hpcd; /******************************************************************************* PCD BSP Routines *******************************************************************************/ +PCD_HandleTypeDef * HAL_PCD_GetHandle(void) +{ + return &hpcd; +} /** * @brief Initializes the PCD MSP. diff --git a/app/dfu/usbd_conf.h b/app/dfu/usbd_conf.h index e127311..17c986e 100644 --- a/app/dfu/usbd_conf.h +++ b/app/dfu/usbd_conf.h @@ -81,6 +81,7 @@ #endif /* Exported functions ------------------------------------------------------- */ +extern PCD_HandleTypeDef * HAL_PCD_GetHandle(void); #endif /* __USBD_CONF_H */ diff --git a/app/main.cpp b/app/main.cpp index a4f77bb..f220d28 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -39,6 +39,8 @@ #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, BOOTLOADER_VERSION, @@ -55,6 +57,10 @@ volatile const uint8_t bootloader_data[] __attribute__ ((section (".bootloader_v volatile const uint8_t bootloader_identifier[] __attribute__ ((section (".bootloader_identification"), used)) = "MCUboot Arduino"; +#if MCUBOOT_APPLICATION_DFU +USBD_HandleTypeDef USBD_Device; +#endif + DigitalOut red(BOARD_RED_LED, 1); DigitalOut green(BOARD_GREEN_LED, 1); DigitalOut blue(BOARD_BLUE_LED, 1); @@ -118,15 +124,6 @@ static int debug_init(void) { return 0; } -#if MCUBOOT_APPLICATION_DFU -USBD_HandleTypeDef USBD_Device; -extern PCD_HandleTypeDef hpcd; -#endif - -extern "C" { - uint8_t SetSysClock_PLL_HSE(uint8_t bypass, bool lowspeed); -} - static int start_dfu(void) { RTCSetBKPRegister(RTC_BKP_DR0, 0); @@ -168,7 +165,7 @@ static int start_dfu(void) { #else // USE_USB_FS if (USB_OTG_FS->GINTSTS & USB_OTG_FS->GINTMSK) { #endif - HAL_PCD_IRQHandler(&hpcd); + HAL_PCD_IRQHandler(HAL_PCD_GetHandle()); } #endif led_pulse(&green); From 9f42fa28c1bccac2c7d73264246b3dc56794c436 Mon Sep 17 00:00:00 2001 From: pennam Date: Mon, 4 Apr 2022 17:25:48 +0200 Subject: [PATCH 16/16] Fix comment about CLOCK_SOURCE configuration in mbed_app.json file --- app/main.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/app/main.cpp b/app/main.cpp index f220d28..e6d533b 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -34,10 +34,12 @@ #include "usbd_dfu_flash.h" #endif -// 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 +/* + * 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);