diff --git a/platforms/common/include/px4_platform_common/board_common.h b/platforms/common/include/px4_platform_common/board_common.h index aaf47003164..bc85723b58c 100644 --- a/platforms/common/include/px4_platform_common/board_common.h +++ b/platforms/common/include/px4_platform_common/board_common.h @@ -704,7 +704,8 @@ typedef enum reset_mode_e { BOARD_RESET_MODE_BOOT_TO_BL = 1, /* Reboot and stay in the bootloader */ BOARD_RESET_MODE_BOOT_TO_VALID_APP = 2, /* Reboot to a valid app or stay in bootloader */ BOARD_RESET_MODE_CAN_BL = 3, /* Used to pass a node ID and stay in the can bootloader */ - BOARD_RESET_MODE_RTC_BOOT_FWOK = 4 /* Set by a a watch dogged application after running > 30 Seconds */ + BOARD_RESET_MODE_RTC_BOOT_FWOK = 4, /* Set by a a watch dogged application after running > 30 Seconds */ + BOARD_RESET_MODE_BOOT_TO_ISP = 5 /* Reboot into the chip's ROM bootloader */ } reset_mode_e; int board_configure_reset(reset_mode_e mode, uint32_t arg); diff --git a/platforms/nuttx/src/bootloader/stm/stm32_common/main.c b/platforms/nuttx/src/bootloader/stm/stm32_common/main.c index cb1ef0e8f0e..b6bc2f4214f 100644 --- a/platforms/nuttx/src/bootloader/stm/stm32_common/main.c +++ b/platforms/nuttx/src/bootloader/stm/stm32_common/main.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -105,6 +106,7 @@ struct boardinfo board_info = { static void board_init(void); #define BOOT_RTC_SIGNATURE 0xb007b007 +#define BOOT_RTC_SIGNATURE_ISP 0xb007df00 /* BOARD_RESET_MODE_BOOT_TO_ISP */ #define BOOT_RTC_REG MMIO32(RTC_BASE + 0x50) /* State of an inserted USB cable */ @@ -656,6 +658,39 @@ arch_do_jump(const uint32_t *app_base) for (;;) ; } +#if defined(CONFIG_ARCH_CHIP_STM32H7) +/* NuttX's __start calls this before any clock setup, so the chip is still as reset left it, which is how the ROM's + * DFU bootloader expects to find it. The app asks for it with reboot -i. + */ +void stm32_preclock_hook(void); +void +stm32_preclock_hook(void) +{ + /* the backup registers sit behind the RTC's bus clock */ + modifyreg32(STM32_RCC_APB4ENR, 0, RCC_APB4ENR_RTCAPBEN); + + if (board_get_rtc_signature() == BOOT_RTC_SIGNATURE_ISP) { + board_set_rtc_signature(0); + + /* The ROM listens for a USART1 sync byte on PB15 before it starts USB. Low there when it enables the USART, it + * takes a start bit and waits on USART1 for good. PB15 is a data line on boards that use it, so pull it up and + * give it time to rise. */ + modifyreg32(STM32_RCC_AHB4ENR, 0, RCC_AHB4ENR_GPIOBEN); + modifyreg32(STM32_GPIOB_PUPDR, GPIO_PUPDR_MASK(15), GPIO_PUPDR_PULLUP << GPIO_PUPDR_SHIFT(15)); + modifyreg32(STM32_GPIOB_MODER, GPIO_MODER_MASK(15), GPIO_MODER_INPUT << GPIO_MODER_SHIFT(15)); + + for (volatile int i = 0; i < 10000 && !(getreg32(STM32_GPIOB_IDR) & (1 << 15)); i++) {} + + const uint32_t *isp_base = (const uint32_t *)0x1FF09800; + + asm volatile( + "msr msp, %0 \n" + "bx %1 \n" + : : "r"(isp_base[0]), "r"(isp_base[1]) :); + } +} +#endif + int bootloader_main(int argc, char *argv[]) { diff --git a/platforms/nuttx/src/px4/stm/stm32_common/board_reset/board_reset.cpp b/platforms/nuttx/src/px4/stm/stm32_common/board_reset/board_reset.cpp index 28b5ba5f848..e75d5248f16 100644 --- a/platforms/nuttx/src/px4/stm/stm32_common/board_reset/board_reset.cpp +++ b/platforms/nuttx/src/px4/stm/stm32_common/board_reset/board_reset.cpp @@ -43,6 +43,7 @@ #include #include #include +#include #include #ifdef CONFIG_BOARDCTL_RESET @@ -69,7 +70,8 @@ static const uint32_t modes[] = { /* BOARD_RESET_MODE_BOOT_TO_BL 0 n */ 0xb007b007, /* BOARD_RESET_MODE_BOOT_TO_VALID_APP 0 y */ 0xb0070002, /* BOARD_RESET_MODE_CAN_BL 10 n */ 0xb0080000, - /* BOARD_RESET_MODE_RTC_BOOT_FWOK 0 n */ 0xb0093a26 + /* BOARD_RESET_MODE_RTC_BOOT_FWOK 0 n */ 0xb0093a26, + /* BOARD_RESET_MODE_BOOT_TO_ISP - n */ 0xb007df00 }; int board_configure_reset(reset_mode_e mode, uint32_t arg) @@ -120,12 +122,25 @@ int board_reset(int status) { if (status == REBOOT_TO_BOOTLOADER) { board_configure_reset(BOARD_RESET_MODE_BOOT_TO_BL, 0); + + } else if (status == REBOOT_TO_ISP) { + board_configure_reset(BOARD_RESET_MODE_BOOT_TO_ISP, 0); } #if defined(BOARD_HAS_ON_RESET) board_on_reset(status); #endif +#if defined(CONFIG_ARCH_CHIP_STM32H7) + + if (status == REBOOT_TO_ISP) { + /* The ROM bootloader's USB never enumerates after a reset taken with the OTG clocks on */ + up_irq_save(); + modifyreg32(STM32_RCC_AHB1ENR, RCC_AHB1ENR_OTGHSEN | RCC_AHB1ENR_OTGFSEN, 0); + } + +#endif + up_systemreset(); return 0; } diff --git a/platforms/nuttx/src/px4/stm/stm32h7/include/px4_arch/micro_hal.h b/platforms/nuttx/src/px4/stm/stm32h7/include/px4_arch/micro_hal.h index 9da7566a3a8..f159f9e8555 100644 --- a/platforms/nuttx/src/px4/stm/stm32h7/include/px4_arch/micro_hal.h +++ b/platforms/nuttx/src/px4/stm/stm32h7/include/px4_arch/micro_hal.h @@ -39,6 +39,10 @@ __BEGIN_DECLS #define PX4_SOC_ARCH_ID PX4_SOC_ARCH_ID_STM32H7 +// The PX4 bootloader hands over to the ROM's DFU bootloader on BOARD_RESET_MODE_BOOT_TO_ISP (reboot -i). +// An older bootloader starts the app again instead. +#define BOARD_HAS_ISP_BOOTLOADER 1 + #define STM32_RCC_APB1ENR STM32_RCC_APB1LENR #define STM32_RCC_APB1RSTR STM32_RCC_APB1LRSTR