diff --git a/platforms/common/include/px4_platform_common/board_common.h b/platforms/common/include/px4_platform_common/board_common.h index 2cbd524766..14b0a8d009 100644 --- a/platforms/common/include/px4_platform_common/board_common.h +++ b/platforms/common/include/px4_platform_common/board_common.h @@ -587,6 +587,51 @@ int board_power_off(int status); int board_reset(int status); #endif +/**************************************************************************** + * Name: board_configure_reset + * + * Description: + * Configures the device that maintains the state shared by the + * application and boot loader. This is usually an RTC. + * + * Input Parameters: + * mode - The type of reset. See reset_mode_e + * + * Returned Value: + * 0 for Success + * 1 if invalid argument + * + ****************************************************************************/ + +#ifdef CONFIG_BOARDCTL_RESET + +typedef enum reset_mode_e { + BOARD_RESET_MODE_CLEAR = 0, /* Clear the mode */ + 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 */ +} reset_mode_e; + +int board_configure_reset(reset_mode_e mode, uint32_t arg); +#endif + +/**************************************************************************** + * Name: board_booted_by_px4 + * + * Description: + * Determines if the the boot loader was PX4 + * + * Input Parameters: + * none + * + * Returned Value: + * true if booted byt a PX4 bootloader. + * + ****************************************************************************/ + +bool board_booted_by_px4(void); + /************************************************************************************ * Name: board_query_manifest * 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 ef65d94ab7..7dc3c14942 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 @@ -38,6 +38,7 @@ */ #include +#include #include #include #include @@ -45,20 +46,53 @@ #ifdef CONFIG_BOARDCTL_RESET -static int board_reset_enter_bootloader() +/**************************************************************************** + * Name: board_configure_reset + * + * Description: + * Configures the device that maintains the state shared by the + * application and boot loader. This is usually an RTC. + * + * Input Parameters: + * mode - The type of reset. See reset_mode_e + * + * Returned Value: + * 0 for Success + * 1 if invalid argument + * + ****************************************************************************/ + +static const uint32_t modes[] = { + /* to tb */ + /* BOARD_RESET_MODE_CLEAR 5 y */ 0, + /* BOARD_RESET_MODE_BOOT_TO_BL 0 n */ 0xb0070001, + /* 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 +}; + +int board_configure_reset(reset_mode_e mode, uint32_t arg) { - stm32_pwr_enablebkp(true); + int rv = -1; - uint32_t regvalue = 0xb007b007; + if (mode < arraySize(modes)) { + + stm32_pwr_enablebkp(true); + + arg = mode == BOARD_RESET_MODE_CAN_BL ? arg & ~0xff : 0; + + // Check if we can to use the new register definition -// Check if we can to use the new register definition #ifndef STM32_RTC_BK0R - *(uint32_t *)STM32_BKP_BASE = regvalue; + *(uint32_t *)STM32_BKP_BASE = modes[mode] | arg; #else - *(uint32_t *)STM32_RTC_BK0R = regvalue; + *(uint32_t *)STM32_RTC_BK0R = modes[mode] | arg; #endif - stm32_pwr_enablebkp(false); - return OK; + stm32_pwr_enablebkp(false); + rv = OK; + } + + return rv; } /**************************************************************************** @@ -84,7 +118,7 @@ static int board_reset_enter_bootloader() int board_reset(int status) { if (status == 1) { - board_reset_enter_bootloader(); + board_configure_reset(BOARD_RESET_MODE_BOOT_TO_BL, 0); } #if defined(BOARD_HAS_ON_RESET) @@ -96,3 +130,25 @@ int board_reset(int status) } #endif /* CONFIG_BOARDCTL_RESET */ + +/**************************************************************************** + * Name: board_booted_by_px4 + * + * Description: + * Determines if the the boot loader was PX4 + * + * Input Parameters: + * none + * + * Returned Value: + * true if booted byt a PX4 bootloader. + * + ****************************************************************************/ +bool board_booted_by_px4(void) +{ + uint32_t *vectors = (uint32_t *) STM32_FLASH_BASE; + + /* Nuttx uses common vector */ + + return (vectors[2] == vectors[3]) && (vectors[4] == vectors[5]); +}