-
Notifications
You must be signed in to change notification settings - Fork 518
/
Copy pathnrf_it.c
130 lines (114 loc) · 3.81 KB
/
nrf_it.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* Copyright (c) 2018 Particle Industries, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#include "nrf_it.h"
#include <stdbool.h>
#include <nrf52840.h>
#include <nrf_rtc.h>
#include <nrf_nvic.h>
#include "logging.h"
#include "hw_config.h"
#include "button_hal.h"
#include "hal_platform_nrf52840_config.h"
#include "security_mode.h"
extern void Timing_Decrement(void);
void HardFault_Handler( void ) __attribute__( ( naked ) );
__attribute__((externally_visible)) void prvGetRegistersFromStack( uint32_t *pulFaultStackAddress )
{
/* These are volatile to try and prevent the compiler/linker optimising them
away as the variables never actually get used. If the debugger won't show the
values of the variables, make them global my moving their declaration outside
of this function. */
volatile uint32_t r0;
volatile uint32_t r1;
volatile uint32_t r2;
volatile uint32_t r3;
volatile uint32_t r12;
volatile uint32_t lr; /* Link register. */
volatile uint32_t pc; /* Program counter. */
volatile uint32_t psr;/* Program status register. */
r0 = pulFaultStackAddress[ 0 ];
r1 = pulFaultStackAddress[ 1 ];
r2 = pulFaultStackAddress[ 2 ];
r3 = pulFaultStackAddress[ 3 ];
r12 = pulFaultStackAddress[ 4 ];
lr = pulFaultStackAddress[ 5 ];
pc = pulFaultStackAddress[ 6 ];
psr = pulFaultStackAddress[ 7 ];
/* Silence "variable set but not used" error */
if (false) {
(void)r0; (void)r1; (void)r2; (void)r3; (void)r12; (void)lr; (void)pc; (void)psr;
}
if (SCB->CFSR & (1<<25) /* DIVBYZERO */) {
// stay consistent with the core and cause 5 flashes
UsageFault_Handler();
}
else {
PANIC(HardFault,"HardFault");
/* Go to infinite loop when Hard Fault exception occurs */
while (1)
{
}
}
}
void HardFault_Handler(void)
{
__asm volatile
(
" tst lr, #4 \n"
" ite eq \n"
" mrseq r0, msp \n"
" mrsne r0, psp \n"
" ldr r1, [r0, #24] \n"
" ldr r2, handler2_address_const \n"
" bx r2 \n"
" .balign 4 \n"
" handler2_address_const: .word prvGetRegistersFromStack \n"
);
}
void MemManage_Handler(void)
{
/* Go to infinite loop when Memory Manage exception occurs */
PANIC(MemManage,"MemManage");
while (1)
{
}
}
void BusFault_Handler(void)
{
/* Go to infinite loop when Bus Fault exception occurs */
PANIC(BusFault,"BusFault");
while (1)
{
}
}
void UsageFault_Handler(void)
{
/* Go to infinite loop when Usage Fault exception occurs */
PANIC(UsageFault,"UsageFault");
while (1)
{
}
}
void SysTick_Handler(void)
{
System1MsTick();
security_mode_notify_system_tick();
Timing_Decrement();
#if HAL_PLATFORM_BUTTON_DEBOUNCE_IN_SYSTICK
hal_button_timer_handler();
#endif
}