diff --git a/Libs/Cyphal b/Libs/Cyphal index 937de75..2e63e03 160000 --- a/Libs/Cyphal +++ b/Libs/Cyphal @@ -1 +1 @@ -Subproject commit 937de755a697727035fb533f5a2209e2382bc6b3 +Subproject commit 2e63e0329777a33a1dc533a06c0535837a7ba09a diff --git a/Libs/Dronecan b/Libs/Dronecan index 074b791..caa015b 160000 --- a/Libs/Dronecan +++ b/Libs/Dronecan @@ -1 +1 @@ -Subproject commit 074b791f7824d0fcd50e7a5ff9b859483228c009 +Subproject commit caa015bd8ad5721a9595b401f26b643cd52b3e2c diff --git a/Src/common/application.cpp b/Src/common/application.cpp index 8fb7468..9f8d451 100644 --- a/Src/common/application.cpp +++ b/Src/common/application.cpp @@ -104,6 +104,6 @@ __attribute__((noreturn)) void application_entry_point() { while (true) { ModuleManager::process(); blink_board_led(); - WatchdogPeriphery::refresh(); + HAL::Watchdog::refresh(); } } diff --git a/Src/peripheral/iwdg/iwdg.hpp b/Src/peripheral/iwdg/iwdg.hpp index 96d2feb..f61cfef 100644 --- a/Src/peripheral/iwdg/iwdg.hpp +++ b/Src/peripheral/iwdg/iwdg.hpp @@ -7,13 +7,26 @@ #ifndef SRC_PERIPHERY_IWDG_HPP_ #define SRC_PERIPHERY_IWDG_HPP_ -class WatchdogPeriphery { +namespace HAL { + +class Watchdog { public: /** - * @brief Refresh the IWDG - * Reload IWDG counter with value defined in the reload register + * @brief Reload IWDG counter with value defined in the reload register */ static void refresh(); + + /** + * @brief Block all refresh calls, so the application will be rebooted soon + */ + static void request_reboot() { + reboot_required = true; + } + +private: + static inline bool reboot_required{false}; }; +} // namespace HAL + #endif // SRC_PERIPHERY_IWDG_HPP_ diff --git a/Src/platform/stm32f103/iwdg.cpp b/Src/platform/stm32/iwdg/iwdg.cpp similarity index 75% rename from Src/platform/stm32f103/iwdg.cpp rename to Src/platform/stm32/iwdg/iwdg.cpp index a7de615..bd972f4 100644 --- a/Src/platform/stm32f103/iwdg.cpp +++ b/Src/platform/stm32/iwdg/iwdg.cpp @@ -11,8 +11,14 @@ extern IWDG_HandleTypeDef hiwdg; #endif // HAL_IWDG_MODULE_ENABLED -void WatchdogPeriphery::refresh() { +namespace HAL { + +void Watchdog::refresh() { + if (!reboot_required) { #ifdef HAL_IWDG_MODULE_ENABLED - HAL_IWDG_Refresh(&hiwdg); + HAL_IWDG_Refresh(&hiwdg); #endif // HAL_IWDG_MODULE_ENABLED + } } + +} // namespace HAL diff --git a/Src/platform/stm32f103/platform_specific.cpp b/Src/platform/stm32/platform_specific.cpp similarity index 66% rename from Src/platform/stm32f103/platform_specific.cpp rename to Src/platform/stm32/platform_specific.cpp index 559c7c0..42308a9 100644 --- a/Src/platform/stm32f103/platform_specific.cpp +++ b/Src/platform/stm32/platform_specific.cpp @@ -6,12 +6,13 @@ #include #include "main.h" +#include "peripheral/iwdg/iwdg.hpp" #ifdef __cplusplus extern "C" { #endif -void uavcanReadUniqueID(uint8_t out_uid[4]) { +void platformSpecificReadUniqueID(uint8_t out_uid[4]) { const uint32_t UNIQUE_ID_16_BYTES[4] = { HAL_GetUIDw0(), HAL_GetUIDw1(), @@ -21,11 +22,16 @@ void uavcanReadUniqueID(uint8_t out_uid[4]) { memcpy(out_uid, UNIQUE_ID_16_BYTES, 16); } -void uavcanRestartNode() { +bool platformSpecificRequestRestart() { + HAL::Watchdog::request_reboot(); + return true; +} + +void platformSpecificRebootForce() { HAL_NVIC_SystemReset(); } -uint32_t uavcanGetTimeMs() { +uint32_t platformSpecificGetTimeMs() { return HAL_GetTick(); } diff --git a/Src/platform/stm32f103/CMakeLists.txt b/Src/platform/stm32f103/CMakeLists.txt index f367eaf..3988f6f 100644 --- a/Src/platform/stm32f103/CMakeLists.txt +++ b/Src/platform/stm32f103/CMakeLists.txt @@ -13,10 +13,10 @@ add_executable(${EXECUTABLE} ${CMAKE_CURRENT_LIST_DIR}/adc.cpp ${ROOT_DIR}/Src/platform/stm32/pwm/pwm.cpp ${CMAKE_CURRENT_LIST_DIR}/pwm.cpp - ${CMAKE_CURRENT_LIST_DIR}/iwdg.cpp + ${ROOT_DIR}/Src/platform/stm32/iwdg/iwdg.cpp ${CMAKE_CURRENT_LIST_DIR}/led.cpp ${CMAKE_CURRENT_LIST_DIR}/temperature_sensor.cpp - ${CMAKE_CURRENT_LIST_DIR}/platform_specific.cpp + ${ROOT_DIR}/Src/platform/stm32/platform_specific.cpp ${coreSources} ${driversSources} diff --git a/Src/platform/stm32g0b1/CMakeLists.txt b/Src/platform/stm32g0b1/CMakeLists.txt index 0dc9ead..c8e821a 100644 --- a/Src/platform/stm32g0b1/CMakeLists.txt +++ b/Src/platform/stm32g0b1/CMakeLists.txt @@ -20,10 +20,10 @@ add_executable(${EXECUTABLE} ${CMAKE_CURRENT_LIST_DIR}/gpio.cpp ${PLATFORM_DIR}/stm32g0b1/pwm.cpp ${PLATFORM_DIR}/stm32g0b1/spi.cpp - ${PLATFORM_DIR}/stm32f103/iwdg.cpp + ${ROOT_DIR}/Src/platform/stm32/iwdg/iwdg.cpp ${PLATFORM_DIR}/stm32f103/led.cpp ${PLATFORM_DIR}/stm32f103/temperature_sensor.cpp - ${PLATFORM_DIR}/stm32f103/platform_specific.cpp + ${ROOT_DIR}/Src/platform/stm32/platform_specific.cpp ${coreSources} ${driversSources} diff --git a/Src/platform/ubuntu/iwdg.cpp b/Src/platform/ubuntu/iwdg.cpp index b4776a9..390ac7d 100644 --- a/Src/platform/ubuntu/iwdg.cpp +++ b/Src/platform/ubuntu/iwdg.cpp @@ -5,7 +5,20 @@ */ #include "peripheral/iwdg/iwdg.hpp" +#include "main.h" -void WatchdogPeriphery::refresh() { - // do nothing +namespace HAL { + +static uint32_t watchdog_deadline_ms = 500; + +void Watchdog::refresh() { + if (!reboot_required) { + watchdog_deadline_ms = platformSpecificGetTimeMs() + 500; + } + + if (platformSpecificGetTimeMs() > watchdog_deadline_ms) { + platformSpecificRebootForce(); + } } + +} // namespace HAL diff --git a/Src/platform/ubuntu/main.h b/Src/platform/ubuntu/main.h index 4421699..b3a7db2 100644 --- a/Src/platform/ubuntu/main.h +++ b/Src/platform/ubuntu/main.h @@ -19,9 +19,10 @@ static inline uint32_t HAL_GetUIDw2() {return 0;} uint32_t HAL_GetTick(); void HAL_NVIC_SystemReset(); -void uavcanRestartNode(); +void platformSpecificRebootForce(); +bool platformSpecificRequestRestart(); -uint32_t uavcanGetTimeMs(); +uint32_t platformSpecificGetTimeMs(); #ifdef __cplusplus } diff --git a/Src/platform/ubuntu/platform_specific.cpp b/Src/platform/ubuntu/platform_specific.cpp index ccda9de..ccf715d 100644 --- a/Src/platform/ubuntu/platform_specific.cpp +++ b/Src/platform/ubuntu/platform_specific.cpp @@ -12,6 +12,7 @@ #include "main.h" #include "application.hpp" #include "rom.h" +#include "peripheral/iwdg/iwdg.hpp" int main() { std::cout << "The app has been started." << std::endl; @@ -20,11 +21,16 @@ int main() { return 0; } -void uavcanReadUniqueID(uint8_t out_uid[4]) { +void platformSpecificReadUniqueID(uint8_t out_uid[4]) { memset(out_uid, 0x00, 16); } -void uavcanRestartNode() { +bool platformSpecificRequestRestart() { + HAL::Watchdog::request_reboot(); + return true; +} + +void platformSpecificRebootForce() { constexpr const char* EXECUTABLE_SYMBOLIC_LINK = "/proc/self/exe"; constexpr const int MAX_PATH_LENGTH = 1024; @@ -47,7 +53,7 @@ void uavcanRestartNode() { exit(1); } -uint32_t uavcanGetTimeMs() { +uint32_t platformSpecificGetTimeMs() { return HAL_GetTick(); }