SRTOS
Loading...
Searching...
No Matches
fault.c
Go to the documentation of this file.
1
8
9#include "fault.h"
10
11__attribute ((naked)) uint32_t *
12systemGet_Fault_SP (__attribute__ ((unused)) uint32_t faultLR)
13{
14 __asm volatile ("TST r0, #4\n"
15 "ITE eq\n"
16 "MRSEQ r0, msp\n"
17 "MRSNE r0, psp\n"
18 "BX lr\n");
19}
20
21void
22systemHandle_Fault (uint32_t *faultSP)
23{
26
27 while (FLASH_SR & (1U << FLASH_SR_BSY_BIT))
28 ;
29
30 /* PSIZE -> 0b10 which is a 32 bit parallelism size */
32 FLASH_CR &= ~(1U << (FLASH_CR_PSIZE_BIT_START + 1));
33 FLASH_CR |= (1U << (FLASH_CR_PSIZE_BIT_START + 1));
34
35 while (FLASH_SR & (1U << FLASH_SR_BSY_BIT))
36 ;
37
38 /* Activate Sector Erase */
39 FLASH_CR |= (1U << FLASH_CR_SER_BIT);
40
41 /* Erase Sector 7 (FAULT_DATA) */
42 FLASH_CR &= ~(0xFU << FLASH_CR_SNB_BIT_START);
44
45 FLASH_CR |= (1U << FLASH_CR_STRT_BIT);
46
47 while (FLASH_SR & (1U << FLASH_SR_BSY_BIT))
48 ;
49
50 FLASH_CR |= (1U << FLASH_CR_PG_BIT);
51
52 /*
53 * Push order:
54 * r0
55 * r1
56 * r2
57 * r3
58 * r12
59 * lr
60 * pc
61 * psr
62 * */
63
64 volatile uint32_t *curWriteAddr
65 = (volatile uint32_t *)FAULT_DATA_FLASH_START_ADDR;
66 for (int i = 0; i < 8; i++)
67 {
68 *(curWriteAddr) = faultSP[i];
69 curWriteAddr += 1;
70 }
71
72 while (FLASH_SR & (1U << FLASH_SR_BSY_BIT))
73 ;
74
75 while (1)
76 ;
77}
78
79__attribute__ ((naked)) void
80HardFault_Handler ()
81{
82 __asm volatile ("MOV r0, lr\n"
83 "BL systemGet_Fault_SP\n"
84 "LDR r1, =systemHandle_Fault\n"
85 "BX r1\n");
86}
__attribute__((naked))
This function will get the fault Stack Pointer and call the fault handler.
Definition fault.c:79
__attribute((naked))
Returns the Stack Pointer after a fault.
Definition fault.c:11
void systemHandle_Fault(uint32_t *faultSP)
This function will handle a system fault. The default behavior is to log the fault SP to non-volatile...
Definition fault.c:22
Fault handling interface for SRTOS.
#define FLASH_CR_PSIZE_BIT_START
Definition mcu_macros.h:35
#define FLASH_SR_BSY_BIT
Definition mcu_macros.h:33
#define FLASH_SR
Definition mcu_macros.h:32
#define FLASH_KEYR
Definition mcu_macros.h:29
#define FLASH_CR_SNB_BIT_START
Definition mcu_macros.h:37
#define FLASH_CR_PG_BIT
Definition mcu_macros.h:39
#define FLASH_UNLOCK_KEY2
Definition mcu_macros.h:31
#define FLASH_CR_SER_BIT
Definition mcu_macros.h:36
#define FLASH_CR_STRT_BIT
Definition mcu_macros.h:38
#define FLASH_UNLOCK_KEY1
Definition mcu_macros.h:30
#define FLASH_CR
Definition mcu_macros.h:34
#define FAULT_DATA_FLASH_START_ADDR
Definition mcu_macros.h:40