Contiki 3.x
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
69 {
70  return current_seconds;
71 }
#define SysTick
Definition: core_cm0.h:495
#define SCB
Definition: core_cm0.h:494
void __attribute__((interrupt))
This ISR handles most of the business interacting with the 1-wire bus.
Definition: onewire.c:174
Default definitions of C compiler quirk work-arounds.
void clock_init(void)
Initialize the clock library.
Definition: clock.c:76
CCIF clock_time_t clock_time(void)
Get the current clock time.
Definition: clock.c:41
clock_time_t etimer_next_expiration_time(void)
Get next event timer expiration time.
Definition: etimer.c:229
void etimer_request_poll(void)
Make the event timer aware that the clock has changed.
Definition: etimer.c:145
CCIF unsigned long clock_seconds(void)
Get the current value of the platform seconds.
Definition: clock.c:57
int etimer_pending(void)
Check if there are any non-expired event timers.
Definition: etimer.c:223
void clock_delay(unsigned int delay)
Obsolete delay function but we implement it here since some code still uses it.
Definition: clock.c:60
Event timer header file.
#define CLOCK_SECOND
A second, measured in system clock time.
Definition: clock.h:82