aboutsummaryrefslogtreecommitdiffstats
path: root/demos/STM32/RT-STM32F407-DISCOVERY-fault_handlers/crash_test.c
blob: bf11436a48e400ce7971ac1abf2b6a3912bbb897 (plain)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* Copyright (C) 2018  Adam Green (https://github.com/adamgreen)
				 2019 Diego Ismirlian (dismirlian(at)google's mail)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

/*
	This code is heavily based on the work of Adam Green (https://github.com/adamgreen)
*/


#include <ch.h>
#include <hal.h>

// Assembly language routines defined in tests.S
void testMspMultipleOf8(void);
void testMspNotMultipleOf8(void);
void testPspMultipleOf8(void);
void testInitFPURegisters(void);
void testBreakpoints(void);

static void enable8ByteStackAlignment(void);
static void crashWithFPUDisabled(void);
static void crashWithFPUAutoStackingDisabled(void);
static void crashWithFPUAutoStackEnabled(void);
static void crashWithFPULazyAutoStacking(void);

#define CRASH_CATCHER_READ_FAULT()          (*(volatile unsigned int*)0x55555550)
#define CRASH_CATCHER_WRITE_FAULT()         (*(volatile unsigned int*)0x55555550 = 0x0)
#define CRASH_CATCHER_INVALID_INSTRUCTION() { __asm volatile (".word 0xDE00"); }

/* Macro used to insert hardcoded breakpoint into user's code. */
#define CRASH_CATCHER_BREAKPOINT()          { __asm volatile ("bkpt #0"); }

int crash(int option) {

	enable8ByteStackAlignment();

	switch (option) {
	case 1:
		testMspMultipleOf8();
		break;
	case 2:
		testMspNotMultipleOf8();
		break;
	case 3:
		testPspMultipleOf8();
		break;
	case 4:
		CRASH_CATCHER_READ_FAULT();
		break;
	case 5:
		CRASH_CATCHER_WRITE_FAULT();
		break;
	case 6:
		crashWithFPUDisabled();
		break;
	case 7:
		crashWithFPUAutoStackingDisabled();
		break;
	case 8:
		crashWithFPUAutoStackEnabled();
		break;
	case 9:
		crashWithFPULazyAutoStacking();
		break;
	case 10:
		testBreakpoints();
		break;
	default:
		break;
	}

	return 0;
}

static void enable8ByteStackAlignment()
{
    SCB->CCR |= SCB_CCR_STKALIGN_Msk;
}

#if CORTEX_HAS_FPU
static void disableFPU(void)
{
    static const uint32_t FPCA = 1 << 2;
    SCB->CPACR &= ~(0xF << 20);
    __set_CONTROL(__get_CONTROL() & ~FPCA);
}

static void enableFPU(void)
{
    SCB->CPACR |= (0xF << 20);
}

static void crashWithFPUDisabled(void)
{
    disableFPU();
    __disable_irq();
    testInitFPURegisters();
    CRASH_CATCHER_READ_FAULT();
}

static const uint32_t ASPEN = 1 << 31;
static const uint32_t LSPEN = 1 << 30;

static void crashWithFPUAutoStackingDisabled(void)
{
    disableFPU();
    FPU->FPCCR &= ~(ASPEN | LSPEN);
    enableFPU();
    __disable_irq();
    testInitFPURegisters();
    CRASH_CATCHER_READ_FAULT();
}

static void crashWithFPUAutoStackEnabled(void)
{
    disableFPU();
        FPU->FPCCR |= ASPEN;
        FPU->FPCCR &= ~LSPEN;
    enableFPU();
    __disable_irq();
    testInitFPURegisters();
    CRASH_CATCHER_READ_FAULT();
}

static void crashWithFPULazyAutoStacking(void)
{
    disableFPU();
    FPU->FPCCR |= (ASPEN | LSPEN);
    enableFPU();
    __disable_irq();
    testInitFPURegisters();
    CRASH_CATCHER_READ_FAULT();
}

#else

static void crashWithFPUDisabled(void)
{
    return;
}

static void crashWithFPUAutoStackingDisabled(void)
{
    return;
}

static void crashWithFPUAutoStackEnabled(void)
{
    return;
}

static void crashWithFPULazyAutoStacking(void)
{
    return;
}

#endif // !defined(CORTEX_M4)