Merge branch 'master' of https://github.com/robinkrens/cortex-from-scratch
[cortex-from-scratch] / systick.c
index 48065ef..589e286 100644 (file)
--- a/systick.c
+++ b/systick.c
@@ -1,8 +1,25 @@
+/* (CC-BY-NC-SA) ROBIN KRENS - ROBIN @ ROBINKRENS.NL
+ * 
+ * $LOG$
+ * 2019/8/14 - ROBIN KRENS     
+ * Initial version 
+ * 
+ * $DESCRIPTION$
+ * SysTick of Cortex M* MCUs. Have a look at the more complex RTC 
+ * in case more accurate timing is needed.
+ *
+ * 
+ * */
+
 #include <stdbool.h>
 #include <stddef.h>
 #include <stdint.h>
-#include <stm32.h>
-#include <mmap.h>
+
+#include <sys/robsys.h>
+#include <sys/mmap.h>
+
+#include <lib/regfunc.h>
+#include <lib/tinyprintf.h>
 
 struct interrupt_frame {
 
@@ -16,35 +33,33 @@ struct interrupt_frame {
         uint32_t psr; // N-4
 };
 
+//__attribute__ ((interrupt))
+void * systick_handler(/* struct interrupt_frame * frame */) {
 
-/* void * systick_handler() {
-
-//     *RANDOM_ADDR = (volatile uint32_t) 0x10101010 ;
-//     uart_puts("TEST");
-} */
-
-__attribute__ ((interrupt))
-void * systick_handler(struct interrupt_frame * frame) {
-
-       uart_puts("TICKING\n");
-//     addrtohex((uint32_t) 0x12345678);
-       //      for(;;);
+       printf("Ticking...\n");
 }
 
 
 void systick_init() {
 
+       /* Every time the counter counts down to zero
+        * a systick exception is invoked. Systick has
+        * exception number 15. in the vector table  */
+       ivt_set_gate(15, systick_handler, 0); 
+
+       /* Get calibration and set this to 1 sec
+        * !Most boards have a 1 ms or 10 ms 
+        * calibration value */
+       int calib = (*STK_CALIB << 0) * 500;
+
+       /* The counter reload registers counts down to zero
+        * and then it is restores the value */
+       rwrite(STK_RELOAD, calib);
+       
        /* Enable the counter and enable the interrupt
         * associated with it */
-       *STK_CTRL = (volatile uint32_t) 0x00000003;
+       rsetbit(STK_CTRL, 0);
+       rsetbit(STK_CTRL, 1);
 
-       /* The counter reload register here holds 
-        * 0x1000 -- that's 4096 clock cycles -- if 
-        * it is down to zero it is restores the value */
-       *STK_RELOAD = (volatile uint32_t) 0x00000400; 
 
-       /* Every time the counter counts down to zero
-        * a systick exception is asserted. Systick has
-        * exception number 15. in the vector table  */
-       ivt_set_gate(15, systick_handler, 0); 
 }