Skip to content

Commit

Permalink
move reset reason to system task
Browse files Browse the repository at this point in the history
  • Loading branch information
caila-marashaj committed Dec 5, 2024
1 parent 91beeb1 commit ceb5763
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 121 deletions.
101 changes: 0 additions & 101 deletions stm32-modules/flex-stacker/firmware/motor_control/motor_hardware.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
101 changes: 101 additions & 0 deletions stm32-modules/flex-stacker/firmware/system/system_hardware.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand Down Expand Up @@ -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 */
Expand Down
5 changes: 5 additions & 0 deletions stm32-modules/flex-stacker/firmware/system/system_policy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions stm32-modules/include/flex-stacker/firmware/system_hardware.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ extern "C" {
#endif // __cplusplus

#include <stdbool.h>
#include <stdint.h>

#define HOPPER_DOR_CLOSED_PIN GPIO_PIN_13
#define HOPPER_DOR_CLOSED_GPIO_PORT GPIOC
Expand All @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ class SystemPolicy {
-> errors::ErrorCode;
auto get_serial_number() -> std::array<char, SYSTEM_SERIAL_NUMBER_LENGTH>;
auto get_door_closed() -> bool;
auto last_reset_reason() const -> uint16_t;
};
5 changes: 2 additions & 3 deletions stm32-modules/include/flex-stacker/flex-stacker/messages.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ using HostCommsMessage =
using SystemMessage =
::std::variant<std::monostate, AcknowledgePrevious, GetSystemInfoMessage,
SetSerialNumberMessage, EnterBootloaderMessage,
GetDoorClosedMessage>;
GetDoorClosedMessage, GetResetReasonMessage>;

using MotorDriverMessage =
::std::variant<std::monostate, SetTMCRegisterMessage, GetTMCRegisterMessage,
Expand All @@ -305,7 +305,6 @@ using MotorMessage = ::std::variant<
MoveToLimitSwitchMessage, StopMotorMessage, MoveCompleteMessage,
GetLimitSwitchesMessage, MoveMotorInMmMessage, SetMicrostepsMessage,
GetMoveParamsMessage, SetDiag0IRQMessage, GPIOInterruptMessage,
HomeMotorMessage, GetPlatformSensorsMessage, GetEstopMessage,
GetResetReasonMessage>;
HomeMotorMessage, GetPlatformSensorsMessage, GetEstopMessage>;

}; // namespace messages
11 changes: 0 additions & 11 deletions stm32-modules/include/flex-stacker/flex-stacker/motor_task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -424,17 +424,6 @@ class MotorTask {
_l_controller.stop_movement(0, false);
}

template <MotorControlPolicy Policy>
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<void>(_task_registry->send_to_address(
response, Queues::HostCommsAddress));
}

/**
* @brief Move the motor to the limit switch; apply fast moves to XZ motors
* whenever possible.
Expand Down
11 changes: 11 additions & 0 deletions stm32-modules/include/flex-stacker/flex-stacker/system_task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,17 @@ class SystemTask {
static_cast<void>(_task_registry->send(response));
}

template <SystemExecutionPolicy Policy>
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<void>(
_task_registry->send(response, Queues::HostCommsAddress));
}

template <SystemExecutionPolicy Policy>
auto visit_message(const messages::SetSerialNumberMessage& message,
Policy& policy) {
Expand Down

0 comments on commit ceb5763

Please sign in to comment.