Skip to content

Commit 07844c7

Browse files
committed
Fix #30 - make vector table read only
1 parent 7d578fd commit 07844c7

File tree

10 files changed

+23
-20
lines changed

10 files changed

+23
-20
lines changed

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ English | [中文](README_zh-CN.md)
77

88
This guide is written for developers who wish to start programming
99
microcontrollers using a GCC compiler and a datasheet, without using any
10-
framework! This guide explains the fundamentals - and it helps to
11-
understand how embedded frameworks (Cube, Keil, Arduino, etc) work.
10+
framework. This guide explains the fundamentals, and helps to understand how
11+
embedded frameworks like Cube, Keil, Arduino, and others, work.
1212

1313
The guide covers the following topics: memory and registers, interrupt vector
1414
table, startup code, linker script, build automation using `make`, GPIO
@@ -332,7 +332,7 @@ __attribute__((naked, noreturn)) void _reset(void) {
332332
extern void _estack(void); // Defined in link.ld
333333
334334
// 16 standard and 91 STM32-specific handlers
335-
__attribute__((section(".vectors"))) void (*tab[16 + 91])(void) = {
335+
__attribute__((section(".vectors"))) void (*const tab[16 + 91])(void) = {
336336
_estack, _reset
337337
};
338338
```
@@ -341,10 +341,10 @@ For function `_reset()`, we have used GCC-specific attributes `naked` and
341341
`noreturn` - they mean, standard function's prologue and epilogue should not
342342
be created by the compiler, and that function does not return.
343343

344-
The `void (*tab[16 + 91])(void)` expression means: define an array of 16 + 91
345-
pointers to functions, that return nothing (void) and take to arguments. Each
346-
such function is an IRQ handler (Interrupt ReQuest handler). An array of those
347-
handlers is called a vector table.
344+
The `void (*const tab[16 + 91])(void)` expression means: define an array of 16
345+
+ 91 pointers to functions, that return nothing (void) and take to arguments.
346+
Each such function is an IRQ handler (Interrupt ReQuest handler). An array of
347+
those handlers is called a vector table.
348348

349349
The vector table `tab` we put in a separate section called `.vectors` - that we
350350
need later to tell the linker to put that section right at the beginning of the
@@ -893,7 +893,7 @@ updated by interrupt handlers, or by the hardware, declare as `volatile`**.
893893
Now we should add `SysTick_Handler()` interrupt handler to the vector table:
894894
895895
```c
896-
__attribute__((section(".vectors"))) void (*tab[16 + 91])(void) = {
896+
__attribute__((section(".vectors"))) void (*const tab[16 + 91])(void) = {
897897
0, _reset, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, SysTick_Handler};
898898
```
899899
@@ -1583,7 +1583,7 @@ WEAK_ALIAS void NMI_Handler(void);
15831583
WEAK_ALIAS void HardFault_Handler(void);
15841584
WEAK_ALIAS void MemManage_Handler(void);
15851585
...
1586-
__attribute__((section(".vectors"))) void (*tab[16 + 91])(void) = {
1586+
__attribute__((section(".vectors"))) void (*const tab[16 + 91])(void) = {
15871587
0, _reset, NMI_Handler, HardFault_Handler, MemManage_Handler,
15881588
...
15891589
```

step-0-minimal/main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ __attribute__((naked, noreturn)) void _reset(void) {
1919
extern void _estack(void); // Defined in link.ld
2020

2121
// 16 standard and 91 STM32-specific handlers
22-
__attribute__((section(".vectors"))) void (*tab[16 + 91])(void) = {_estack,
23-
_reset};
22+
__attribute__((section(".vectors"))) void (*const tab[16 + 91])(void) = {
23+
_estack, _reset};

step-1-blinky/main.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,5 @@ __attribute__((naked, noreturn)) void _reset(void) {
6969
extern void _estack(void); // Defined in link.ld
7070

7171
// 16 standard and 91 STM32-specific handlers
72-
__attribute__((section(".vectors"))) void (*tab[16 + 91])(void) = {_estack, _reset};
72+
__attribute__((section(".vectors"))) void (*const tab[16 + 91])(void) = {
73+
_estack, _reset};

step-2-systick/main.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ static inline void spin(volatile uint32_t count) {
5656
}
5757

5858
static volatile uint32_t s_ticks;
59-
void SysTick_Handler(void) { s_ticks++; }
59+
void SysTick_Handler(void) {
60+
s_ticks++;
61+
}
6062

6163
// t: expiration time, prd: period, now: current time. Return true if expired
6264
bool timer_expired(uint32_t *t, uint32_t prd, uint32_t now) {
@@ -99,5 +101,5 @@ __attribute__((naked, noreturn)) void _reset(void) {
99101
extern void _estack(void); // Defined in link.ld
100102

101103
// 16 standard and 91 STM32-specific handlers
102-
__attribute__((section(".vectors"))) void (*tab[16 + 91])(void) = {
104+
__attribute__((section(".vectors"))) void (*const tab[16 + 91])(void) = {
103105
_estack, _reset, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, SysTick_Handler};

step-3-uart/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,5 +156,5 @@ __attribute__((naked, noreturn)) void _reset(void) {
156156
extern void _estack(void); // Defined in link.ld
157157

158158
// 16 standard and 91 STM32-specific handlers
159-
__attribute__((section(".vectors"))) void (*tab[16 + 91])(void) = {
159+
__attribute__((section(".vectors"))) void (*const tab[16 + 91])(void) = {
160160
_estack, _reset, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, SysTick_Handler};

step-4-printf/startup.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ extern void SysTick_Handler(void); // Defined in main.c
1818
extern void _estack(void); // Defined in link.ld
1919

2020
// 16 standard and 91 STM32-specific handlers
21-
__attribute__((section(".vectors"))) void (*tab[16 + 91])(void) = {
21+
__attribute__((section(".vectors"))) void (*const tab[16 + 91])(void) = {
2222
_estack, _reset, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, SysTick_Handler};

step-5-cmsis/startup.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ extern void SysTick_Handler(void); // Defined in main.c
1818
extern void _estack(void); // Defined in link.ld
1919

2020
// 16 standard and 91 STM32-specific handlers
21-
__attribute__((section(".vectors"))) void (*tab[16 + 91])(void) = {
21+
__attribute__((section(".vectors"))) void (*const tab[16 + 91])(void) = {
2222
_estack, _reset, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, SysTick_Handler};

step-6-clock/startup.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ extern void SysTick_Handler(void); // Defined in main.c
1818
extern void _estack(void); // Defined in link.ld
1919

2020
// 16 standard and 91 STM32-specific handlers
21-
__attribute__((section(".vectors"))) void (*tab[16 + 91])(void) = {
21+
__attribute__((section(".vectors"))) void (*const tab[16 + 91])(void) = {
2222
_estack, _reset, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, SysTick_Handler};

step-7-webserver/ek-tm4c1294xl/startup.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ WEAK_ALIAS void I2C9_Handler(void);
130130
extern void _estack();
131131

132132
// IRQ table
133-
__attribute__((section(".vectors"))) void (*tab[16 + 114])(void) = {
133+
__attribute__((section(".vectors"))) void (*const tab[16 + 114])(void) = {
134134
// Cortex interrupts
135135
_estack, _reset, NMI_Handler, HardFault_Handler, MemManage_Handler,
136136
BusFault_Handler, UsageFault_Handler, 0, 0, 0, 0, SVC_Handler,

step-7-webserver/nucleo-f429zi/startup.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ WEAK_ALIAS void DMA2D_IRQHandler(void);
123123
extern void _estack(void); // Defined in link.ld
124124

125125
// IRQ table
126-
__attribute__((section(".vectors"))) void (*tab[16 + 91])(void) = {
126+
__attribute__((section(".vectors"))) void (*const tab[16 + 91])(void) = {
127127
// Cortex interrupts
128128
_estack, _reset, NMI_Handler, HardFault_Handler, MemManage_Handler,
129129
BusFault_Handler, UsageFault_Handler, 0, 0, 0, 0, SVC_Handler,

0 commit comments

Comments
 (0)