Contiki 2.5
clock.c
1 #include <sys/clock.h>
2 #include <sys/cc.h>
3 #include <sys/etimer.h>
4 #include <debug-uart.h>
5 
6 #include <AT91SAM7S64.h>
7 #include <sys-interrupt.h>
8 
9 #define PIV ((MCK/CLOCK_SECOND/16)-1)
10 
11 static volatile clock_time_t current_clock = 0;
12 static volatile unsigned long current_seconds = 0;
13 static unsigned int second_countdown = CLOCK_SECOND;
14 
15 
16 static int pit_handler_func()
17 {
18  if (!(*AT91C_PITC_PISR & AT91C_PITC_PITS)) return 0; /* Check PIT
19  Interrupt */
20  current_clock++;
21  if(etimer_pending() && etimer_next_expiration_time() <= current_clock) {
23  /* dbg_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  (void)*AT91C_PITC_PIVR;
31  return 1;
32 }
33 
34 static SystemInterruptHandler pit_handler = {NULL, pit_handler_func};
35 
36 void
38 {
39  sys_interrupt_append_handler(&pit_handler);
40  *AT91C_PITC_PIMR = (AT91C_PITC_PITIEN | /* PIT Interrupt Enable */
41  AT91C_PITC_PITEN | /* PIT Enable */
42  PIV);
43  sys_interrupt_enable();
44 }
45 
46 clock_time_t
48 {
49  return current_clock;
50 }
51 
52 /* The inner loop takes 4 cycles. The outer 5+SPIN_COUNT*4. */
53 
54 #define SPIN_TIME 2 /* us */
55 #define SPIN_COUNT (((MCK*SPIN_TIME/1000000)-5)/4)
56 
57 #ifndef __MAKING_DEPS__
58 
59 void
60 clock_delay(unsigned int t)
61 {
62 #ifdef __THUMBEL__
63  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));
64 #else
65 #error Must be compiled in thumb mode
66 #endif
67 }
68 
69 unsigned long
70 clock_seconds(void)
71 {
72  return current_seconds;
73 }
74 #endif /* __MAKING_DEPS__ */