further ivt and isr implementation
[cortex-from-scratch] / systick.c
1 #include <stdbool.h>
2 #include <stddef.h>
3 #include <stdint.h>
4 #include <stm32.h>
5 #include <mmap.h>
6
7 struct interrupt_frame {
8
9         uint32_t r0; // N-32
10         uint32_t r1;
11         uint32_t r2;
12         uint32_t r3;
13         uint32_t r12;
14         uint32_t lr;
15         uint32_t pc;
16         uint32_t psr; // N-4
17 };
18
19 __attribute__ ((interrupt))
20 void * systick_handler(struct interrupt_frame * frame) {
21
22         uint32_t volatile status;
23         uart_puts("TICKING\n");
24 }
25
26
27 void systick_init() {
28
29         /* Enable the counter and enable the interrupt
30          * associated with it */
31         *STK_CTRL = (volatile uint32_t) 0x00000003;
32
33         /* The counter reload register here holds 
34          * 0x1000 -- that's 4096 clock cycles -- if 
35          * it is down to zero it is restores the value */
36         *STK_RELOAD = (volatile uint32_t) 0x00400000; 
37
38         /* Every time the counter counts down to zero
39          * a systick exception is asserted. Systick has
40          * exception number 15. in the vector table  */
41         ivt_set_gate(15, systick_handler, 0); 
42 }