diff --git a/stm32-modules/flex-stacker/firmware/motor_control/motor_hardware.c b/stm32-modules/flex-stacker/firmware/motor_control/motor_hardware.c index 32de262b3..f00f8dc96 100644 --- a/stm32-modules/flex-stacker/firmware/motor_control/motor_hardware.c +++ b/stm32-modules/flex-stacker/firmware/motor_control/motor_hardware.c @@ -26,39 +26,6 @@ extern "C" { TIM_HandleTypeDef htim17; TIM_HandleTypeDef htim20; TIM_HandleTypeDef htim3; -uint16_t reset_reason; - -enum RCC_FLAGS { - _NONE, - // high speed internal clock ready - HSIRDY, // = 1 - // high speed external clock ready - HSERDY, // = 2 - // main phase-locked loop clock ready - PLLRDY, // = 3 - // hsi48 clock ready - HSI48RDY, // = 4 - // low-speed external clock ready - LSERDY, // = 5 - // lse clock security system failure - LSECSSD, // = 6 - // low-speed internal clock ready - LSIRDY, // = 7 - // brown out - BORRST, // = 8 - // option byte-loader reset - OBLRST, // = 9 - // pin reset - PINRST, // = 10 - // software reset - SFTRST, // = 11 - // independent watchdog - IWDGRST, // = 12 - // window watchdog - WWDGRST, // = 13 - // low power reset - LPWRRST, // = 14 -}; typedef struct PinConfig { void* port; @@ -333,75 +300,7 @@ void tim3_init(TIM_HandleTypeDef* htim) { HAL_NVIC_EnableIRQ(TIM3_IRQn); } -static void save_reset_reason() { - // check various reset flags to see if the HAL RCC - // reset flag matches any of them - reset_reason = 0; - - // high speed internal clock ready - if (__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY)) { - reset_reason |= HSIRDY; - } - // high speed external clock ready - else if (__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY)) { - reset_reason |= HSERDY; - } - // main phase-locked loop clock ready - else if (__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY)) { - reset_reason |= PLLRDY; - } - // hsi48 clock ready - else if (__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY)) { - reset_reason |= HSI48RDY; - } - // low-speed external clock ready - else if (__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY)) { - reset_reason |= LSERDY; - } - // lse clock security system failure - else if (__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY)) { - reset_reason |= LSECSSD; - } - // low-speed internal clock ready - else if (__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY)) { - reset_reason |= LSIRDY; - } - // brown out - else if (__HAL_RCC_GET_FLAG(RCC_FLAG_BORRST)) { - reset_reason |= BORRST; - } - // option byte-loader reset - else if (__HAL_RCC_GET_FLAG(RCC_FLAG_OBLRST)) { - reset_reason |= OBLRST; - } - // pin reset - else if (__HAL_RCC_GET_FLAG(RCC_FLAG_PINRST)) { - reset_reason |= PINRST; - } - // software reset - else if (__HAL_RCC_GET_FLAG(RCC_FLAG_SFTRST)) { - reset_reason |= SFTRST; - } - // independent watchdog - else if (__HAL_RCC_GET_FLAG(RCC_FLAG_IWDGRST)) { - reset_reason |= IWDGRST; - } - // window watchdog - else if (__HAL_RCC_GET_FLAG(RCC_FLAG_WWDGRST)) { - reset_reason |= WWDGRST; - } - // low power reset - else if (__HAL_RCC_GET_FLAG(RCC_FLAG_LPWRRST)) { - reset_reason |= LPWRRST; - } -} - -uint16_t motor_hardware_reset_reason() { - return reset_reason; -} - void motor_hardware_init(void){ - save_reset_reason(); if (!_motor_hardware.initialized) { motor_hardware_gpio_init(); tim17_init(&_motor_hardware.motor_x.timer); diff --git a/stm32-modules/flex-stacker/firmware/motor_control/motor_policy.cpp b/stm32-modules/flex-stacker/firmware/motor_control/motor_policy.cpp index 34a6df23c..9c225dd8d 100644 --- a/stm32-modules/flex-stacker/firmware/motor_control/motor_policy.cpp +++ b/stm32-modules/flex-stacker/firmware/motor_control/motor_policy.cpp @@ -58,9 +58,4 @@ auto MotorPolicy::is_diag0_pin(uint16_t pin) -> bool { // NOLINTNEXTLINE(readability-convert-member-functions-to-static) auto MotorPolicy::is_estop_pin(uint16_t pin) -> bool { return hw_is_estop_pin(pin); -} - -// NOLINTNEXTLINE(readability-convert-member-functions-to-static) -auto MotorPolicy::last_reset_reason() const -> uint16_t { - return motor_hardware_reset_reason(); } \ No newline at end of file diff --git a/stm32-modules/flex-stacker/firmware/system/system_hardware.c b/stm32-modules/flex-stacker/firmware/system/system_hardware.c index 233642fc1..0405f8a64 100644 --- a/stm32-modules/flex-stacker/firmware/system/system_hardware.c +++ b/stm32-modules/flex-stacker/firmware/system/system_hardware.c @@ -13,6 +13,106 @@ // address 4 in the bootable region is the address of the first instruction that // should run, aka the data that should be loaded into $pc. const uint32_t *const sysmem_boot_loc = (uint32_t*)SYSMEM_BOOT; +uint16_t reset_reason; + +enum RCC_FLAGS { + _NONE, + // high speed internal clock ready + HSIRDY, // = 1 + // high speed external clock ready + HSERDY, // = 2 + // main phase-locked loop clock ready + PLLRDY, // = 3 + // hsi48 clock ready + HSI48RDY, // = 4 + // low-speed external clock ready + LSERDY, // = 5 + // lse clock security system failure + LSECSSD, // = 6 + // low-speed internal clock ready + LSIRDY, // = 7 + // brown out + BORRST, // = 8 + // option byte-loader reset + OBLRST, // = 9 + // pin reset + PINRST, // = 10 + // software reset + SFTRST, // = 11 + // independent watchdog + IWDGRST, // = 12 + // window watchdog + WWDGRST, // = 13 + // low power reset + LPWRRST, // = 14 +}; + +static void save_reset_reason() { + // check various reset flags to see if the HAL RCC + // reset flag matches any of them + reset_reason = 0; + + // high speed internal clock ready + if (__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY)) { + reset_reason |= HSIRDY; + } + // high speed external clock ready + else if (__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY)) { + reset_reason |= HSERDY; + } + // main phase-locked loop clock ready + else if (__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY)) { + reset_reason |= PLLRDY; + } + // hsi48 clock ready + else if (__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY)) { + reset_reason |= HSI48RDY; + } + // low-speed external clock ready + else if (__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY)) { + reset_reason |= LSERDY; + } + // lse clock security system failure + else if (__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY)) { + reset_reason |= LSECSSD; + } + // low-speed internal clock ready + else if (__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY)) { + reset_reason |= LSIRDY; + } + // brown out + else if (__HAL_RCC_GET_FLAG(RCC_FLAG_BORRST)) { + reset_reason |= BORRST; + } + // option byte-loader reset + else if (__HAL_RCC_GET_FLAG(RCC_FLAG_OBLRST)) { + reset_reason |= OBLRST; + } + // pin reset + else if (__HAL_RCC_GET_FLAG(RCC_FLAG_PINRST)) { + reset_reason |= PINRST; + } + // software reset + else if (__HAL_RCC_GET_FLAG(RCC_FLAG_SFTRST)) { + reset_reason |= SFTRST; + } + // independent watchdog + else if (__HAL_RCC_GET_FLAG(RCC_FLAG_IWDGRST)) { + reset_reason |= IWDGRST; + } + // window watchdog + else if (__HAL_RCC_GET_FLAG(RCC_FLAG_WWDGRST)) { + reset_reason |= WWDGRST; + } + // low power reset + else if (__HAL_RCC_GET_FLAG(RCC_FLAG_LPWRRST)) { + reset_reason |= LPWRRST; + } +} + +uint16_t system_hardware_reset_reason() { + return reset_reason; +} /** PUBLIC FUNCTION IMPLEMENTATION */ @@ -60,6 +160,7 @@ void system_hardware_enter_bootloader(void) { } void system_hardware_gpio_init(void) { + save_reset_reason(); GPIO_InitTypeDef init = {0}; /* GPIO Ports Clock Enable */ diff --git a/stm32-modules/flex-stacker/firmware/system/system_policy.cpp b/stm32-modules/flex-stacker/firmware/system/system_policy.cpp index e6382f49a..33062a93e 100644 --- a/stm32-modules/flex-stacker/firmware/system/system_policy.cpp +++ b/stm32-modules/flex-stacker/firmware/system/system_policy.cpp @@ -65,3 +65,8 @@ auto SystemPolicy::initialize() -> void { system_hardware_gpio_init(); } auto SystemPolicy::get_door_closed() -> bool { return system_hardware_read_door_closed(); } + +// NOLINTNEXTLINE(readability-convert-member-functions-to-static) +auto SystemPolicy::last_reset_reason() const -> uint16_t { + return system_hardware_reset_reason(); +} diff --git a/stm32-modules/include/flex-stacker/firmware/motor_policy.hpp b/stm32-modules/include/flex-stacker/firmware/motor_policy.hpp index ec189d3e5..749ca66df 100644 --- a/stm32-modules/include/flex-stacker/firmware/motor_policy.hpp +++ b/stm32-modules/include/flex-stacker/firmware/motor_policy.hpp @@ -20,7 +20,6 @@ class MotorPolicy { auto check_estop() -> bool; auto is_diag0_pin(uint16_t pin) -> bool; auto is_estop_pin(uint16_t pin) -> bool; - [[nodiscard]] auto last_reset_reason() const -> uint16_t; }; } // namespace motor_policy diff --git a/stm32-modules/include/flex-stacker/firmware/system_hardware.h b/stm32-modules/include/flex-stacker/firmware/system_hardware.h index 51741b2ce..83ae9d0af 100644 --- a/stm32-modules/include/flex-stacker/firmware/system_hardware.h +++ b/stm32-modules/include/flex-stacker/firmware/system_hardware.h @@ -5,6 +5,7 @@ extern "C" { #endif // __cplusplus #include +#include #define HOPPER_DOR_CLOSED_PIN GPIO_PIN_13 #define HOPPER_DOR_CLOSED_GPIO_PORT GPIOC @@ -15,6 +16,7 @@ extern "C" { void system_hardware_enter_bootloader(void); void system_hardware_gpio_init(void); bool system_hardware_read_door_closed(void); +uint16_t system_hardware_reset_reason(void); #ifdef __cplusplus } // extern "C" diff --git a/stm32-modules/include/flex-stacker/firmware/system_policy.hpp b/stm32-modules/include/flex-stacker/firmware/system_policy.hpp index 87b7745fa..cb19a3f3d 100644 --- a/stm32-modules/include/flex-stacker/firmware/system_policy.hpp +++ b/stm32-modules/include/flex-stacker/firmware/system_policy.hpp @@ -23,4 +23,5 @@ class SystemPolicy { -> errors::ErrorCode; auto get_serial_number() -> std::array; auto get_door_closed() -> bool; + auto last_reset_reason() const -> uint16_t; }; diff --git a/stm32-modules/include/flex-stacker/flex-stacker/messages.hpp b/stm32-modules/include/flex-stacker/flex-stacker/messages.hpp index edc55c724..c721f04c8 100644 --- a/stm32-modules/include/flex-stacker/flex-stacker/messages.hpp +++ b/stm32-modules/include/flex-stacker/flex-stacker/messages.hpp @@ -292,7 +292,7 @@ using HostCommsMessage = using SystemMessage = ::std::variant; + GetDoorClosedMessage, GetResetReasonMessage>; using MotorDriverMessage = ::std::variant; + HomeMotorMessage, GetPlatformSensorsMessage, GetEstopMessage>; }; // namespace messages diff --git a/stm32-modules/include/flex-stacker/flex-stacker/motor_task.hpp b/stm32-modules/include/flex-stacker/flex-stacker/motor_task.hpp index 2c4f73fc3..09c81066b 100644 --- a/stm32-modules/include/flex-stacker/flex-stacker/motor_task.hpp +++ b/stm32-modules/include/flex-stacker/flex-stacker/motor_task.hpp @@ -424,17 +424,6 @@ class MotorTask { _l_controller.stop_movement(0, false); } - template - auto visit_message(const messages::GetResetReasonMessage& msg, - Policy& policy) -> void { - auto reason = policy.last_reset_reason(); - - auto response = messages::GetResetReasonResponse{ - .responding_to_id = msg.id, .reason = reason}; - static_cast(_task_registry->send_to_address( - response, Queues::HostCommsAddress)); - } - /** * @brief Move the motor to the limit switch; apply fast moves to XZ motors * whenever possible. diff --git a/stm32-modules/include/flex-stacker/flex-stacker/system_task.hpp b/stm32-modules/include/flex-stacker/flex-stacker/system_task.hpp index 33efd3c10..e89e8f891 100644 --- a/stm32-modules/include/flex-stacker/flex-stacker/system_task.hpp +++ b/stm32-modules/include/flex-stacker/flex-stacker/system_task.hpp @@ -86,6 +86,17 @@ class SystemTask { static_cast(_task_registry->send(response)); } + template + auto visit_message(const messages::GetResetReasonMessage& msg, + Policy& policy) -> void { + auto reason = policy.last_reset_reason(); + + auto response = messages::GetResetReasonResponse{ + .responding_to_id = msg.id, .reason = reason}; + static_cast( + _task_registry->send(response, Queues::HostCommsAddress)); + } + template auto visit_message(const messages::SetSerialNumberMessage& message, Policy& policy) {