simple timer input push/pull, output open-drain
[cortex-from-scratch] / systick.c
1 /* (CC-BY-NC-SA) ROBIN KRENS - ROBIN @ ROBINKRENS.NL
2  * 
3  * $LOG$
4  * 2019/8/14 - ROBIN KRENS      
5  * Initial version 
6  * 
7  * $DESCRIPTION$
8  * SysTick of Cortex M* MCUs. Have a look at the more complex RTC 
9  * in case more accurate timing is needed.
10  *
11  * 
12  * */
13
14 #include <stdbool.h>
15 #include <stddef.h>
16 #include <stdint.h>
17
18 #include <sys/robsys.h>
19 #include <sys/mmap.h>
20
21 #include <lib/regfunc.h>
22 #include <lib/tinyprintf.h>
23
24 struct interrupt_frame {
25
26         uint32_t r0; // N-32
27         uint32_t r1;
28         uint32_t r2;
29         uint32_t r3;
30         uint32_t r12;
31         uint32_t lr;
32         uint32_t pc;
33         uint32_t psr; // N-4
34 };
35
36 //__attribute__ ((interrupt))
37 void * systick_handler(/* struct interrupt_frame * frame */) {
38
39         printf("Ticking...\n");
40 }
41
42
43 void systick_init() {
44
45         /* Every time the counter counts down to zero
46          * a systick exception is invoked. Systick has
47          * exception number 15. in the vector table  */
48         ivt_set_gate(15, systick_handler, 0); 
49
50         /* Get calibration and set this to 1 sec
51          * !Most boards have a 1 ms or 10 ms 
52          * calibration value */
53         int calib = (*STK_CALIB << 0) * 500;
54
55         /* The counter reload registers counts down to zero
56          * and then it is restores the value */
57         rwrite(STK_RELOAD, calib);
58         
59         /* Enable the counter and enable the interrupt
60          * associated with it */
61         rsetbit(STK_CTRL, 0);
62         rsetbit(STK_CTRL, 1);
63
64
65 }