implementation of interrupts, SysTick exception
[cortex-from-scratch] / ivt.c
1 #include <stdbool.h>
2 #include <stddef.h>
3 #include <stdint.h>
4 #include <stm32.h>
5 #include <mmap.h>
6
7 /* 
8  * Vector table, each entry contains an interrupt
9  * service routine:  * 
10  *
11  * interrupt vector 1-15: processor exceptions
12  * interrupt vector 16-32: irq0 - irq ..
13  * */
14
15 uint32_t ivt[32];
16
17
18 void ivt_set_gate(unsigned char num, void * isr(), short pri) {
19
20         ivt[num] = (uint32_t) isr;
21         *NVIC_ISER0 = (1 << ((uint32_t)(num) & 0x1F));
22         /* Priorities */
23 }
24
25
26 /* Dummy interrupt */
27 void * dummy_isr() {
28
29 }
30
31 /* Initialize interrupt vector  */
32 void ivt_init() {
33
34         /* clear entiry IVT, in SRAM location for SRAM + .data (in .bss section) */
35         memset(&ivt, 0, (sizeof(uint32_t) * 87));
36
37         // stack top is loaded from the first entry table on boot/reset
38         // don't need to relocate or init this here
39         extern void * reset,  * nmi, * hardfault;
40
41         ivt_set_gate(1, dummy_isr, 0);
42         ivt_set_gate(2, dummy_isr, 0);
43         ivt_set_gate(3, dummy_isr, 0);
44
45         /* the vector table starts at 0x0. Since the address 0x0 point to 
46          * bootcode, it is on ROM or FLASH. The vector table can be
47          * relocated to other memory locations. We can do this by setting 
48          * a register in the NVIC called the vector table offset register */
49
50         *SCB_VTOR = (volatile uint32_t) &ivt; 
51
52 }