Skip to content

Commit

Permalink
Enable HardFault handler on all boards (#1055)
Browse files Browse the repository at this point in the history
### Summary
<!-- Quick summary of changes, optional -->

On ARM cores, when you do something very very bad an interrupt is fired
which is handled by each board's `HardFault_Handler`. When doing so, the
core's registers are pushed to the stack. The previous leads have
written some black magic to get these values viewable via a hooked up
debugger. I enabled this handler callback for all boards, because it was
previously only enabled on the BMS and DCM.

### Changelist 
<!-- Give a list of the changes covered in this PR. This will help both
you and the reviewer keep this PR within scope. -->

Add hard fault handler to board `stm32f4xx_it.c` files.

### Testing Done
<!-- Outline the testing that was done to demonstrate the changes are
solid. This could be unit tests, integration tests, testing on the car,
etc. Include relevant code snippets, screenshots, etc as needed. -->

N/A, but this is only changing the PDM, FSM, and DIM with code that has
been on the BMS and DCM for months. Plus, this only caused a change in
behavior after a critical fault, so the risk is low.

### Resolved Issues
<!-- Link any issues that this PR resolved like so: `Resolves #1, #2,
and #5` (Note: Using this format, Github will automatically close the
issue(s) when this PR is merged in). -->

### Checklist
*Please change `[ ]` to `[x]` when you are ready.*
- [x] I have read and followed the code conventions detailed in
[README.md](../README.md) (*This will save time for both you and the
reviewer!*).
- [x] If this pull request is longer then **500** lines, I have provided
*explicit* justification in the summary above explaining why I *cannot*
break this up into multiple pull requests (*Small PR's are faster and
less painful for everyone involved!*).
  • Loading branch information
gtaharaedmonds authored Nov 16, 2023
1 parent eac0a83 commit 73c12c6
Show file tree
Hide file tree
Showing 12 changed files with 28 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "hw_hardFaultHandler.h"
#include "hw_hal.h"
#include "Io_SharedHardFaultHandler.h"
#include "Io_SharedMacros.h"

void Io_SharedHardFaultHandler_Init(void)
void hw_hardFaultHandler_init(void)
{
// Div-by-zero exception is disabled by default and must be enabled manually
SCB->CCR |= SCB_CCR_DIV_0_TRP_Msk;
Expand All @@ -12,7 +12,7 @@ void Io_SharedHardFaultHandler_Init(void)
// transmit the data over CAN
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
void Io_SharedHardFaultHandler_LogInformation(uint32_t *fault_stack)
void hw_hardFaultHandler_logInfo(uint32_t *fault_stack)
{
// Registers pushed onto the stack frame before entering hard fault handler
volatile uint32_t stacked_r0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@
// can be stored in either MSP (main stack pointer) or PSP (process stack
// pointer). The following assembly function determines which stack pointer was
// used was then hard fault occured, and passes it as an argument to
// Io_SharedHardFaultHandler_LogInformation().
// hw_hardFaultHandler_LogInformation().
//
// Note: The MSP/PSP architecture is specific to the Cortex-M microcontroller
// family.
#define Io_SharedHardFaultHandler_HandleHardFault() \
#define hw_hardFaultHandler_handleFault() \
__asm volatile(" tst lr, #4 \n" \
" ite eq \n" \
" mrseq r0, msp \n" \
" mrsne r0, psp \n" \
" ldr r1, [r0, #24] \n" \
" b Io_SharedHardFaultHandler_LogInformation \n");
" b hw_hardFaultHandler_logInfo \n");

/**
* @brief Add the naked attributes to HardFault_Handler function declaration.
Expand All @@ -37,11 +37,11 @@ __attribute__((naked)) void HardFault_Handler(void);
* @note Try to call this as early as possible to catch as many exceptions as
* possible.
*/
void Io_SharedHardFaultHandler_Init(void);
void hw_hardFaultHandler_init(void);

/**
* @brief Log information that can help us identify what caused the hard fault.
* @param fault_stack Pointer to the stack that was used when the hard fault
* occurred.
*/
void Io_SharedHardFaultHandler_LogInformation(uint32_t *fault_stack);
void hw_hardFaultHandler_logInfo(uint32_t *fault_stack);
4 changes: 2 additions & 2 deletions firmware/thruna/BMS/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include "Io_CanRx.h"
#include "Io_SharedSoftwareWatchdog.h"
#include "Io_SharedCan.h"
#include "Io_SharedHardFaultHandler.h"
#include "hw_hardFaultHandler.h"
#include "Io_StackWaterMark.h"
#include "Io_SoftwareWatchdog.h"
#include "Io_Imd.h"
Expand Down Expand Up @@ -267,7 +267,7 @@ int main(void)
HAL_ADC_Start_DMA(&hadc1, (uint32_t *)Io_Adc_GetRawAdcValues(), hadc1.Init.NbrOfConversion);
HAL_TIM_Base_Start(&htim13);

Io_SharedHardFaultHandler_Init();
hw_hardFaultHandler_init();
Io_SharedSoftwareWatchdog_Init(Io_HardwareWatchdog_Refresh, Io_SoftwareWatchdog_TimeoutCallback);
Io_SharedCan_Init(&hcan1, CanTxQueueOverflowCallBack, CanRxQueueOverflowCallBack);
Io_CanTx_Init(Io_SharedCan_TxMessageQueueSendtoBack);
Expand Down
4 changes: 2 additions & 2 deletions firmware/thruna/BMS/Src/stm32f4xx_it.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include "stm32f4xx_it.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "Io_SharedHardFaultHandler.h"
#include "hw_hardFaultHandler.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
Expand Down Expand Up @@ -87,7 +87,7 @@ void NMI_Handler(void)
void HardFault_Handler(void)
{
/* USER CODE BEGIN HardFault_IRQn 0 */
Io_SharedHardFaultHandler_HandleHardFault();
hw_hardFaultHandler_handleFault();
/* USER CODE END HardFault_IRQn 0 */
while (1)
{
Expand Down
4 changes: 2 additions & 2 deletions firmware/thruna/DCM/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include "Io_CanTx.h"
#include "Io_CanRx.h"
#include "Io_SharedCan.h"
#include "Io_SharedHardFaultHandler.h"
#include "hw_hardFaultHandler.h"
#include "Io_StackWaterMark.h"
#include "Io_SoftwareWatchdog.h"
#include "Io_SharedHeartbeatMonitor.h"
Expand Down Expand Up @@ -215,7 +215,7 @@ int main(void)
// Configure and initialize SEGGER SystemView.
SEGGER_SYSVIEW_Conf();

Io_SharedHardFaultHandler_Init();
hw_hardFaultHandler_init();
Io_SharedSoftwareWatchdog_Init(Io_HardwareWatchdog_Refresh, Io_SoftwareWatchdog_TimeoutCallback);
Io_SharedCan_Init(&hcan1, CanTxQueueOverflowCallBack, CanRxQueueOverflowCallBack);
Io_CanTx_Init(Io_SharedCan_TxMessageQueueSendtoBack);
Expand Down
4 changes: 2 additions & 2 deletions firmware/thruna/DCM/Src/stm32f4xx_it.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include "stm32f4xx_it.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "Io_SharedHardFaultHandler.h"
#include "hw_hardFaultHandler.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
Expand Down Expand Up @@ -87,7 +87,7 @@ void NMI_Handler(void)
void HardFault_Handler(void)
{
/* USER CODE BEGIN HardFault_IRQn 0 */
Io_SharedHardFaultHandler_HandleHardFault();
hw_hardFaultHandler_handleFault();
/* USER CODE END HardFault_IRQn 0 */
while (1)
{
Expand Down
4 changes: 2 additions & 2 deletions firmware/thruna/DIM/src/cubemx/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
#include "Io_CanRx.h"
#include "Io_SharedCan.h"
#include "Io_SharedErrorHandlerOverride.h"
#include "Io_SharedHardFaultHandler.h"
#include "hw_hardFaultHandler.h"
#include "Io_SharedHeartbeatMonitor.h"

#include "io_time.h"
Expand Down Expand Up @@ -348,7 +348,7 @@ int main(void)

SEGGER_SYSVIEW_Conf();

Io_SharedHardFaultHandler_Init();
hw_hardFaultHandler_init();
Io_SharedSoftwareWatchdog_Init(io_watchdogConfig_refresh, io_watchdogConfig_timeoutCallback);
Io_SharedCan_Init(&hcan1, CanTxQueueOverflowCallBack, CanRxQueueOverflowCallBack);
Io_CanTx_Init(Io_SharedCan_TxMessageQueueSendtoBack);
Expand Down
3 changes: 2 additions & 1 deletion firmware/thruna/DIM/src/cubemx/Src/stm32f4xx_it.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "stm32f4xx_it.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "hw_hardFaultHandler.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
Expand Down Expand Up @@ -86,7 +87,7 @@ void NMI_Handler(void)
void HardFault_Handler(void)
{
/* USER CODE BEGIN HardFault_IRQn 0 */

hw_hardFaultHandler_handleFault();
/* USER CODE END HardFault_IRQn 0 */
while (1)
{
Expand Down
4 changes: 2 additions & 2 deletions firmware/thruna/FSM/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#include "Io_CanRx.h"
#include "Io_SharedSoftwareWatchdog.h"
#include "Io_SharedCan.h"
#include "Io_SharedHardFaultHandler.h"
#include "hw_hardFaultHandler.h"
#include "Io_StackWaterMark.h"
#include "Io_SoftwareWatchdog.h"
#include "Io_Coolant.h"
Expand Down Expand Up @@ -248,7 +248,7 @@ int main(void)
HAL_ADC_Start_DMA(&hadc1, (uint32_t *)Io_Adc_GetRawAdcValues(), hadc1.Init.NbrOfConversion);
HAL_TIM_Base_Start(&htim3);

Io_SharedHardFaultHandler_Init();
hw_hardFaultHandler_init();
Io_SharedSoftwareWatchdog_Init(Io_HardwareWatchdog_Refresh, Io_SoftwareWatchdog_TimeoutCallback);
Io_SharedCan_Init(&hcan1, CanTxQueueOverflowCallBack, CanRxQueueOverflowCallBack);
Io_CanTx_Init(Io_SharedCan_TxMessageQueueSendtoBack);
Expand Down
3 changes: 2 additions & 1 deletion firmware/thruna/FSM/Src/stm32f4xx_it.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "stm32f4xx_it.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "hw_hardFaultHandler.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
Expand Down Expand Up @@ -89,7 +90,7 @@ void NMI_Handler(void)
void HardFault_Handler(void)
{
/* USER CODE BEGIN HardFault_IRQn 0 */

hw_hardFaultHandler_handleFault();
/* USER CODE END HardFault_IRQn 0 */
while (1)
{
Expand Down
4 changes: 2 additions & 2 deletions firmware/thruna/PDM/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include "Io_CanRx.h"
#include "Io_SharedSoftwareWatchdog.h"
#include "Io_SharedCan.h"
#include "Io_SharedHardFaultHandler.h"
#include "hw_hardFaultHandler.h"
#include "Io_StackWaterMark.h"
#include "Io_SoftwareWatchdog.h"
#include "Io_VoltageSense.h"
Expand Down Expand Up @@ -245,7 +245,7 @@ int main(void)
HAL_ADC_Start_DMA(&hadc1, (uint32_t *)Io_Adc_GetRawAdcValues(), hadc1.Init.NbrOfConversion);
HAL_TIM_Base_Start(&htim3);

Io_SharedHardFaultHandler_Init();
hw_hardFaultHandler_init();
Io_SharedSoftwareWatchdog_Init(Io_HardwareWatchdog_Refresh, Io_SoftwareWatchdog_TimeoutCallback);
Io_SharedCan_Init(&hcan1, CanTxQueueOverflowCallBack, CanRxQueueOverflowCallBack);
Io_CanTx_Init(Io_SharedCan_TxMessageQueueSendtoBack);
Expand Down
3 changes: 2 additions & 1 deletion firmware/thruna/PDM/Src/stm32f4xx_it.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "stm32f4xx_it.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "hw_hardFaultHandler.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
Expand Down Expand Up @@ -85,7 +86,7 @@ void NMI_Handler(void)
void HardFault_Handler(void)
{
/* USER CODE BEGIN HardFault_IRQn 0 */

hw_hardFaultHandler_handleFault();
/* USER CODE END HardFault_IRQn 0 */
while (1)
{
Expand Down

0 comments on commit 73c12c6

Please sign in to comment.