clock.c

00001 #include <stm32f10x_map.h>
00002 #include <nvic.h>
00003 #include <sys/clock.h>
00004 #include <sys/cc.h>
00005 #include <sys/etimer.h>
00006 #include <debug-uart.h>
00007 
00008 static volatile clock_time_t current_clock = 0;
00009 static volatile unsigned long current_seconds = 0;
00010 static unsigned int second_countdown = CLOCK_SECOND;
00011 
00012 void
00013 SysTick_handler(void) __attribute__ ((interrupt));
00014 
00015 void
00016 SysTick_handler(void)
00017 {
00018   (void)SysTick->CTRL;
00019   SCB->ICSR = SCB_ICSR_PENDSTCLR;
00020   current_clock++;
00021   if(etimer_pending() && etimer_next_expiration_time() <= current_clock) {
00022     etimer_request_poll();
00023     /* printf("%d,%d\n", clock_time(),etimer_next_expiration_time       ()); */
00024 
00025   }
00026   if (--second_countdown == 0) {
00027     current_seconds++;
00028     second_countdown = CLOCK_SECOND;
00029   }
00030 }
00031 
00032 
00033 void
00034 clock_init()
00035 {
00036   NVIC_SET_SYSTICK_PRI(8);
00037   SysTick->LOAD = MCK/8/CLOCK_SECOND;
00038   SysTick->CTRL = SysTick_CTRL_ENABLE | SysTick_CTRL_TICKINT;
00039 }
00040 
00041 clock_time_t
00042 clock_time(void)
00043 {
00044   return current_clock;
00045 }
00046 
00047 #if 0
00048 /* The inner loop takes 4 cycles. The outer 5+SPIN_COUNT*4. */
00049 
00050 #define SPIN_TIME 2 /* us */
00051 #define SPIN_COUNT (((MCK*SPIN_TIME/1000000)-5)/4)
00052 
00053 #ifndef __MAKING_DEPS__
00054 
00055 void
00056 clock_delay(unsigned int t)
00057 {
00058 #ifdef __THUMBEL__ 
00059   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));
00060 #else
00061 #error Must be compiled in thumb mode
00062 #endif
00063 }
00064 #endif
00065 #endif /* __MAKING_DEPS__ */
00066 
00067 unsigned long
00068 clock_seconds(void)
00069 {
00070   return current_seconds;
00071 }

Generated on Mon Apr 11 14:23:33 2011 for Contiki 2.5 by  doxygen 1.6.1