clock.c

00001 
00002 #include "sys/clock.h"
00003 #include "dev/clock-avr.h"
00004 #include "sys/etimer.h"
00005 
00006 #include <avr/io.h>
00007 #include <avr/interrupt.h>
00008 
00009 static volatile clock_time_t count;
00010 static volatile uint8_t scount;
00011 volatile unsigned long seconds;
00012 long sleepseconds;
00013 
00014 /* Set RADIOSTATS to monitor radio on time (must also be set in the radio driver) */
00015 #if RF230BB && WEBSERVER
00016 #define RADIOSTATS 1
00017 #endif
00018 
00019 #if RADIOSTATS
00020 static volatile uint8_t rcount;
00021 volatile unsigned long radioontime;
00022 extern uint8_t RF230_receive_on;
00023 #endif
00024 
00025 /* Set RADIOCALIBRATE for periodic calibration of the PLL during extended radio on time.
00026  * The data sheet suggests every 5 minutes if the temperature is fluctuating.
00027  * Using an eight bit counter gives 256 second calibrations.
00028  * Actual calibration is done by the driver on the next transmit request.
00029  */
00030 #if RADIOCALIBRATE
00031 extern volatile uint8_t rf230_calibrate;
00032 static uint8_t calibrate_interval;
00033 #endif
00034 
00035 /*
00036   CLOCK_SECOND is the number of ticks per second.
00037   It is defined through CONF_CLOCK_SECOND in the contiki-conf.h for each platform.
00038   The usual AVR default is ~125 ticks per second, counting a prescaler the CPU clock
00039   using the 8 bit timer0.
00040   
00041   As clock_time_t is an unsigned 16 bit data type, intervals up to 524 seconds
00042   can be measured with 8 millisecond precision. 
00043   For longer intervals a 32 bit global is incremented every second. 
00044  
00045   clock-avr.h contains the specific setup code for each mcu.
00046 
00047 */
00048 /*---------------------------------------------------------------------------*/
00049 /* This routine can be called to add seconds to the clock after a sleep
00050  * of an integral number of seconds.
00051  */
00052 void clock_adjust_seconds(uint8_t howmany) {
00053    seconds += howmany;
00054    sleepseconds +=howmany;
00055 #if RADIOSTATS
00056   if (RF230_receive_on) radioontime += howmany;
00057 #endif
00058 }
00059 /*---------------------------------------------------------------------------*/
00060 //SIGNAL(SIG_OUTPUT_COMPARE0)
00061 ISR(AVR_OUTPUT_COMPARE_INT)
00062 {
00063   count++;
00064   if(++scount == CLOCK_SECOND) {
00065     scount = 0;
00066     seconds++;
00067   }
00068 #if RADIOCALIBRATE
00069    if (++calibrate_interval==0) {
00070     rf230_calibrate=1;
00071   }
00072 #endif
00073 #if RADIOSTATS
00074   if (RF230_receive_on) {
00075     if (++rcount == CLOCK_SECOND) {
00076       rcount=0;
00077       radioontime++;
00078     }
00079   }
00080 #endif
00081 
00082 #if 1
00083 /*  gcc will save all registers on the stack if an external routine is called */
00084   if(etimer_pending()) {
00085     etimer_request_poll();
00086   }
00087 #else
00088 /* doing this locally saves 9 pushes and 9 pops, but these etimer.c and process.c variables have to lose the static qualifier */
00089   extern struct etimer *timerlist;
00090   extern volatile unsigned char poll_requested;
00091 
00092 #define PROCESS_STATE_NONE        0
00093 #define PROCESS_STATE_RUNNING     1
00094 #define PROCESS_STATE_CALLED      2
00095 
00096   if (timerlist) {
00097     if(etimer_process.state == PROCESS_STATE_RUNNING ||
00098        etimer_process.state == PROCESS_STATE_CALLED) {
00099       etimer_process.needspoll = 1;
00100       poll_requested = 1;
00101     }
00102   }
00103 #endif
00104 }
00105 
00106 /*---------------------------------------------------------------------------*/
00107 void
00108 clock_init(void)
00109 {
00110   cli ();
00111   OCRSetup();
00112 //scount = count = 0;
00113   sei ();
00114 }
00115 
00116 /*---------------------------------------------------------------------------*/
00117 clock_time_t
00118 clock_time(void)
00119 {
00120   clock_time_t tmp;
00121   do {
00122     tmp = count;
00123   } while(tmp != count);
00124   return tmp;
00125 }
00126 /*---------------------------------------------------------------------------*/
00127 /**
00128  * Delay the CPU for a multiple of TODO
00129  */
00130 void
00131 clock_delay(unsigned int i)
00132 {
00133   for (; i > 0; i--) {          /* Needs fixing XXX */
00134     unsigned j;
00135     for (j = 50; j > 0; j--)
00136       asm volatile("nop");
00137   }
00138 }
00139 
00140 /*---------------------------------------------------------------------------*/
00141 /**
00142  * Wait for a multiple of 1 / 125 sec = 0.008 ms.
00143  *
00144  */
00145 void
00146 clock_wait(int i)
00147 {
00148   clock_time_t start;
00149 
00150   start = clock_time();
00151   while(clock_time() - start < (clock_time_t)i);
00152 }
00153 /*---------------------------------------------------------------------------*/
00154 void
00155 clock_set_seconds(unsigned long sec)
00156 {
00157     // TODO
00158 }
00159 
00160 unsigned long
00161 clock_seconds(void)
00162 {
00163   unsigned long tmp;
00164   do {
00165     tmp = seconds;
00166   } while(tmp != seconds);
00167   return tmp;
00168 }
00169 
00170 #ifdef HANG_ON_UNKNOWN_INTERRUPT
00171 /* Useful for diagnosing unknown interrupts that reset the mcu.
00172  * Currently set up for 12mega128rfa1.
00173  * For other mcus, enable all and then disable the conflicts.
00174  */
00175 static volatile uint8_t x;
00176 ISR( _VECTOR(0)) {while (1) x++;}
00177 ISR( _VECTOR(1)) {while (1) x++;}
00178 ISR( _VECTOR(2)) {while (1) x++;}
00179 ISR( _VECTOR(3)) {while (1) x++;}
00180 ISR( _VECTOR(4)) {while (1) x++;}
00181 ISR( _VECTOR(5)) {while (1) x++;}
00182 ISR( _VECTOR(6)) {while (1) x++;}
00183 ISR( _VECTOR(7)) {while (1) x++;}
00184 ISR( _VECTOR(8)) {while (1) x++;}
00185 ISR( _VECTOR(9)) {while (1) x++;}
00186 ISR( _VECTOR(10)) {while (1) x++;}
00187 ISR( _VECTOR(11)) {while (1) x++;}
00188 ISR( _VECTOR(12)) {while (1) x++;}
00189 ISR( _VECTOR(13)) {while (1) x++;}
00190 ISR( _VECTOR(14)) {while (1) x++;}
00191 ISR( _VECTOR(15)) {while (1) x++;}
00192 ISR( _VECTOR(16)) {while (1) x++;}
00193 ISR( _VECTOR(17)) {while (1) x++;}
00194 ISR( _VECTOR(18)) {while (1) x++;}
00195 ISR( _VECTOR(19)) {while (1) x++;}
00196 //ISR( _VECTOR(20)) {while (1) x++;}
00197 //ISR( _VECTOR(21)) {while (1) x++;}
00198 ISR( _VECTOR(22)) {while (1) x++;}
00199 ISR( _VECTOR(23)) {while (1) x++;}
00200 ISR( _VECTOR(24)) {while (1) x++;}
00201 //ISR( _VECTOR(25)) {while (1) x++;}
00202 ISR( _VECTOR(26)) {while (1) x++;}
00203 //ISR( _VECTOR(27)) {while (1) x++;}
00204 ISR( _VECTOR(28)) {while (1) x++;}
00205 ISR( _VECTOR(29)) {while (1) x++;}
00206 ISR( _VECTOR(30)) {while (1) x++;}
00207 ISR( _VECTOR(31)) {while (1) x++;}
00208 //ISR( _VECTOR(32)) {while (1) x++;}
00209 ISR( _VECTOR(33)) {while (1) x++;}
00210 ISR( _VECTOR(34)) {while (1) x++;}
00211 ISR( _VECTOR(35)) {while (1) x++;}
00212 //ISR( _VECTOR(36)) {while (1) x++;}
00213 ISR( _VECTOR(37)) {while (1) x++;}
00214 //ISR( _VECTOR(38)) {while (1) x++;}
00215 ISR( _VECTOR(39)) {while (1) x++;}
00216 ISR( _VECTOR(40)) {while (1) x++;}
00217 ISR( _VECTOR(41)) {while (1) x++;}
00218 ISR( _VECTOR(42)) {while (1) x++;}
00219 ISR( _VECTOR(43)) {while (1) x++;}
00220 ISR( _VECTOR(44)) {while (1) x++;}
00221 ISR( _VECTOR(45)) {while (1) x++;}
00222 ISR( _VECTOR(46)) {while (1) x++;}
00223 ISR( _VECTOR(47)) {while (1) x++;}
00224 ISR( _VECTOR(48)) {while (1) x++;}
00225 ISR( _VECTOR(49)) {while (1) x++;}
00226 ISR( _VECTOR(50)) {while (1) x++;}
00227 ISR( _VECTOR(51)) {while (1) x++;}
00228 ISR( _VECTOR(52)) {while (1) x++;}
00229 ISR( _VECTOR(53)) {while (1) x++;}
00230 ISR( _VECTOR(54)) {while (1) x++;}
00231 ISR( _VECTOR(55)) {while (1) x++;}
00232 ISR( _VECTOR(56)) {while (1) x++;}
00233 //ISR( _VECTOR(57)) {while (1) x++;}
00234 //ISR( _VECTOR(58)) {while (1) x++;}
00235 //ISR( _VECTOR(59)) {while (1) x++;}
00236 //ISR( _VECTOR(60)) {while (1) x++;}
00237 ISR( _VECTOR(61)) {while (1) x++;}
00238 ISR( _VECTOR(62)) {while (1) x++;}
00239 ISR( _VECTOR(63)) {while (1) x++;}
00240 ISR( _VECTOR(64)) {while (1) x++;}
00241 ISR( _VECTOR(65)) {while (1) x++;}
00242 ISR( _VECTOR(66)) {while (1) x++;}
00243 ISR( _VECTOR(67)) {while (1) x++;}
00244 ISR( _VECTOR(68)) {while (1) x++;}
00245 ISR( _VECTOR(69)) {while (1) x++;}
00246 ISR( _VECTOR(70)) {while (1) x++;}
00247 ISR( _VECTOR(71)) {while (1) x++;}
00248 ISR( _VECTOR(72)) {while (1) x++;}
00249 ISR( _VECTOR(73)) {while (1) x++;}
00250 ISR( _VECTOR(74)) {while (1) x++;}
00251 ISR( _VECTOR(75)) {while (1) x++;}
00252 ISR( _VECTOR(76)) {while (1) x++;}
00253 ISR( _VECTOR(77)) {while (1) x++;}
00254 ISR( _VECTOR(78)) {while (1) x++;}
00255 ISR( _VECTOR(79)) {while (1) x++;}
00256 #endif

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