Contiki 2.5
clock.c
1 #include <stm32f10x_map.h>
2 #include <nvic.h>
3 #include <sys/clock.h>
4 #include <sys/cc.h>
5 #include <sys/etimer.h>
6 #include <debug-uart.h>
7 
8 static volatile clock_time_t current_clock = 0;
9 static volatile unsigned long current_seconds = 0;
10 static unsigned int second_countdown = CLOCK_SECOND;
11 
12 void
13 SysTick_handler(void) __attribute__ ((interrupt));
14 
15 void
16 SysTick_handler(void)
17 {
18  (void)SysTick->CTRL;
19  SCB->ICSR = SCB_ICSR_PENDSTCLR;
20  current_clock++;
21  if(etimer_pending() && etimer_next_expiration_time() <= current_clock) {
23  /* printf("%d,%d\n", clock_time(),etimer_next_expiration_time ()); */
24 
25  }
26  if (--second_countdown == 0) {
27  current_seconds++;
28  second_countdown = CLOCK_SECOND;
29  }
30 }
31 
32 
33 void
35 {
36  NVIC_SET_SYSTICK_PRI(8);
37  SysTick->LOAD = MCK/8/CLOCK_SECOND;
38  SysTick->CTRL = SysTick_CTRL_ENABLE | SysTick_CTRL_TICKINT;
39 }
40 
41 clock_time_t
43 {
44  return current_clock;
45 }
46 
47 #if 0
48 /* The inner loop takes 4 cycles. The outer 5+SPIN_COUNT*4. */
49 
50 #define SPIN_TIME 2 /* us */
51 #define SPIN_COUNT (((MCK*SPIN_TIME/1000000)-5)/4)
52 
53 #ifndef __MAKING_DEPS__
54 
55 void
56 clock_delay(unsigned int t)
57 {
58 #ifdef __THUMBEL__
59  asm volatile("1: mov r1,%2\n2:\tsub r1,#1\n\tbne 2b\n\tsub %0,#1\n\tbne 1b\n":"=l"(t):"0"(t),"l"(SPIN_COUNT));
60 #else
61 #error Must be compiled in thumb mode
62 #endif
63 }
64 #endif
65 #endif /* __MAKING_DEPS__ */
66 
67 unsigned long
68 clock_seconds(void)
69 {
70  return current_seconds;
71 }