basic implemtation general purpose clock and tinyprintf
[cortex-from-scratch] / systick.c
1 #include <stdbool.h>
2 #include <stddef.h>
3 #include <stdint.h>
4
5 #include <sys/robsys.h>
6 #include <sys/mmap.h>
7
8 #include <lib/regfunc.h>
9 #include <lib/stdio.h>
10
11 struct interrupt_frame {
12
13         uint32_t r0; // N-32
14         uint32_t r1;
15         uint32_t r2;
16         uint32_t r3;
17         uint32_t r12;
18         uint32_t lr;
19         uint32_t pc;
20         uint32_t psr; // N-4
21 };
22
23 //__attribute__ ((interrupt))
24 void * systick_handler(/* struct interrupt_frame * frame */) {
25
26 //      cputs("TICKING\n");
27 //      for(;;);
28 }
29
30
31 void systick_init() {
32
33         /* Every time the counter counts down to zero
34          * a systick exception is asserted. Systick has
35          * exception number 15. in the vector table  */
36         ivt_set_gate(15, systick_handler, 0); 
37
38         /* Get calibration and set this to 1 sec
39          * !Most boards have a 1 ms or 10 ms 
40          * calibration value */
41         int calib = (*STK_CALIB << 0) * 500;
42
43         /* The counter reload registers counts down to zero
44          * and then it is restores the value */
45         rwrite(STK_RELOAD, calib);
46         
47         /* Enable the counter and enable the interrupt
48          * associated with it */
49         rsetbit(STK_CTRL, 0);
50         rsetbit(STK_CTRL, 1);
51
52
53 }