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
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
00026
00027
00028
00029
00030 #if RADIOCALIBRATE
00031 extern volatile uint8_t rf230_calibrate;
00032 static uint8_t calibrate_interval;
00033 #endif
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
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
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
00084 if(etimer_pending()) {
00085 etimer_request_poll();
00086 }
00087 #else
00088
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
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
00129
00130 void
00131 clock_delay(unsigned int i)
00132 {
00133 for (; i > 0; i--) {
00134 unsigned j;
00135 for (j = 50; j > 0; j--)
00136 asm volatile("nop");
00137 }
00138 }
00139
00140
00141
00142
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
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
00172
00173
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
00197
00198 ISR( _VECTOR(22)) {while (1) x++;}
00199 ISR( _VECTOR(23)) {while (1) x++;}
00200 ISR( _VECTOR(24)) {while (1) x++;}
00201
00202 ISR( _VECTOR(26)) {while (1) x++;}
00203
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
00209 ISR( _VECTOR(33)) {while (1) x++;}
00210 ISR( _VECTOR(34)) {while (1) x++;}
00211 ISR( _VECTOR(35)) {while (1) x++;}
00212
00213 ISR( _VECTOR(37)) {while (1) x++;}
00214
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
00234
00235
00236
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