00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #define HPLC_DEBUG(_x)
00035
00036 #include "platform_params.h"
00037 #include "powermod.h"
00038
00039 static int clockScales[] = {-1, 122, 976, 3906, 7812, 15625, 31250, 125000};
00040
00041 norace static event_t* clockEvents[TOSNODES];
00042 norace static uint8_t intervals[TOSNODES];
00043 norace static uint8_t scales[TOSNODES];
00044 norace static long long setTime[TOSNODES];
00045 norace static uint8_t interruptPending[TOSNODES];
00046
00047 TOS_INTERRUPT_HANDLER(SIG_OUTPUT_COMPARE2, (void));
00048 TOS_INTERRUPT_HANDLER(SIG_OUTPUT_COMPARE1A, (void));
00049 void TOSH_adc_data_ready(uint16_t data);
00050 void TOSH_rfm_bit_event(void);
00051 void TOSH_uart_putdone(void);
00052
00053
00054 void TOSH_clock_set_interval(uint8_t interval) {
00055 long long elapsed_time;
00056 long long ticks;
00057 int elapsedTicks;
00058 int realInterval;
00059
00060 event_t* event = NULL;
00061 interval = interval + 1;
00062
00063 dbg(DBG_CLOCK, "CLOCK: Setting clock interval to %u @ %s\n", (unsigned int)(interval & 0xff), currentTime());
00064 if (clockEvents[NODE_NUM] != NULL) {
00065 event_clocktick_invalidate(clockEvents[NODE_NUM]);
00066 }
00067
00068
00069 elapsed_time = tos_state.tos_time - setTime[NODE_NUM];
00070 elapsedTicks = (elapsed_time / (long long)clockScales[scales[NODE_NUM]]);
00071
00072 realInterval = interval - elapsedTicks;
00073 if (realInterval <= 0) {
00074 realInterval += 256;
00075 }
00076
00077 ticks = clockScales[(int)(scales[NODE_NUM] & 0xff)] * realInterval;
00078 event = (event_t*)malloc(sizeof(event_t));
00079
00080 event_clocktick_create(event, NODE_NUM, tos_state.tos_time, ticks);
00081 TOS_queue_insert_event(event);
00082 intervals[NODE_NUM] = interval;
00083 clockEvents[NODE_NUM] = event;
00084 }
00085
00086 void TOSH_clock_set_rate(char interval, char scale)
00087 {
00088 long long ticks;
00089 event_t* event = NULL;
00090 interval = interval + 1;
00091
00092 dbg(DBG_CLOCK, "CLOCK: Setting clock rate to interval %u, scale %u\n", (unsigned int)(interval & 0xff), (unsigned int)(scale & 0xff));
00093 if (clockEvents[NODE_NUM] != NULL) {
00094 event_clocktick_invalidate(clockEvents[NODE_NUM]);
00095 }
00096
00097 ticks = clockScales[(int)(scale & 0xff)] * (int)(interval & 0xff);
00098
00099 if (ticks > 0) {
00100 ticks = clockScales[(int)(scale & 0xff)] * (int)(interval & 0xff);
00101
00102
00103 event = (event_t*)malloc(sizeof(event_t));
00104
00105 event_clocktick_create(event, NODE_NUM, tos_state.tos_time, ticks);
00106 TOS_queue_insert_event(event);
00107 }
00108 intervals[NODE_NUM] = interval;
00109 scales[NODE_NUM] = scale;
00110 clockEvents[NODE_NUM] = event;
00111 setTime[NODE_NUM] = tos_state.tos_time;
00112 return ;
00113 }
00114
00115 uint8_t TOSH_get_clock_interval() {
00116 return intervals[NODE_NUM] - 1;
00117 }
00118
00119 void TOSH_set_clock0_counter(uint8_t n) {
00120
00121 return;
00122 }
00123
00124 uint8_t TOSH_get_clock0_counter() {
00125
00126 if (scales[NODE_NUM] == 0 ||
00127 intervals[NODE_NUM] == 0) {return 0;}
00128 else {
00129 long long timeDiff = tos_state.tos_time - setTime[NODE_NUM];
00130 timeDiff /= (long long)clockScales[scales[NODE_NUM]];
00131 timeDiff %= 256;
00132 return (uint8_t)timeDiff;
00133 }
00134 }
00135
00136 uint8_t TOSH_clock_int_disable() {
00137 if (clockEvents[NODE_NUM] != NULL) {
00138 clock_tick_data_t* data = (clock_tick_data_t*)clockEvents[NODE_NUM]->data;
00139 data->disabled = 1;
00140 }
00141 }
00142
00143 uint8_t TOSH_clock_int_enable() {
00144 if (clockEvents[NODE_NUM] != NULL) {
00145 clock_tick_data_t* data = (clock_tick_data_t*)clockEvents[NODE_NUM]->data;
00146 data->disabled = 0;
00147 if (interruptPending[NODE_NUM]) {
00148 TOS_ISSUE_INTERRUPT(SIG_OUTPUT_COMPARE2)();
00149 }
00150 }
00151 }
00152
00153
00154 static struct timeval _last_time;
00155
00156 void event_clocktick_handle(event_t* event,
00157 struct TOS_state* state) {
00158
00159 event_queue_t* queue = &(state->queue);
00160 clock_tick_data_t* data = (clock_tick_data_t*)event->data;
00161 atomic TOS_LOCAL_ADDRESS = (short)((event->mote + state->first_node) & 0xffff);
00162
00163
00164
00165
00166
00167
00168 if (data->valid) {
00169 if (dbg_active(DBG_CLOCK)) {
00170 char buf[1024];
00171 printTime(buf, 1024);
00172 dbg(DBG_CLOCK, "CLOCK: event handled for mote %i at %s (%i ticks).\n", event->mote, buf, data->interval);
00173 }
00174
00175 setTime[NODE_NUM] = tos_state.tos_time;
00176 event->time = event->time + data->interval;
00177 queue_insert_event(queue, event);
00178 if (!data->disabled) {
00179 TOS_ISSUE_INTERRUPT(SIG_OUTPUT_COMPARE2)();
00180 }
00181 else {
00182 interruptPending[NODE_NUM] = 1;
00183 }
00184 }
00185 else {
00186
00187
00188 event_cleanup(event);
00189 }
00190 }
00191
00192 void event_clocktick_create(event_t* event, int mote, long long eventTime, int interval) {
00193
00194
00195 clock_tick_data_t* data = malloc(sizeof(clock_tick_data_t));
00196 dbg(DBG_MEM, "malloc data entry for clock event: 0x%x\n", (int)data);
00197 data->interval = interval;
00198 data->mote = mote;
00199 data->valid = 1;
00200 data->disabled = 0;
00201
00202 event->mote = mote;
00203 event->force = 0;
00204 event->pause = 1;
00205 event->data = data;
00206 event->time = eventTime + interval;
00207 event->handle = event_clocktick_handle;
00208 event->cleanup = event_total_cleanup;
00209 }
00210
00211
00212 void event_clocktick_invalidate(event_t* event) {
00213 clock_tick_data_t* data = event->data;
00214 data->valid = 0;
00215 }
00216
00217
00218
00219
00220
00221
00222
00223 enum {
00224 ADC_LATENCY = 200
00225 };
00226
00227 static int adcScales[] = {3750, 7500, 15000, 30000, 60000, 120000, 240000, 480000};
00228
00229 norace static event_t* adcEvents[TOSNODES];
00230 static char adcSamplingRates[TOSNODES];
00231
00232 void TOSH_adc_init(void) {
00233 }
00234
00235
00236 void TOSH_adc_set_sampling_rate(uint8_t rate)
00237 {
00238 adcSamplingRates[tos_state.current_node] = rate;
00239 }
00240
00241
00242 void TOSH_adc_sample_port(uint8_t port)
00243 {
00244 event_t* event = NULL;
00245 dbg(DBG_ADC, "ADC: request for port %i\n", (int)port);
00246 if (NULL == adcEvents[tos_state.current_node]) {
00247 event = (event_t*)malloc(sizeof(event_t));
00248 dbg(DBG_MEM, "malloc adc tick event: 0x%x.\n", (int)event);
00249 event_adc_create(event, NODE_NUM, port, tos_state.tos_time, adcSamplingRates[tos_state.current_node]);
00250 adcEvents[tos_state.current_node] = event;
00251 }
00252 else {
00253 event = adcEvents[tos_state.current_node];
00254 event_adc_update(event, NODE_NUM, port, tos_state.tos_time, adcSamplingRates[tos_state.current_node]);
00255 }
00256
00257 TOS_queue_insert_event(event);
00258
00259 }
00260
00261 void TOSH_adc_sample_again(void)
00262 {
00263 event_t* event = adcEvents[tos_state.current_node];
00264 adc_tick_data_t* data = event->data;
00265 dbg(DBG_ADC, "Sample ADC again\n");
00266 if (NULL == event)
00267 dbg(DBG_ERROR, "TOSH_adc_sample_again called after TOSH_adc_sample_stop without calling TOSH_adc_sample_port again...VERY BAD!!");
00268
00269 event->time += adcScales[(int)adcSamplingRates[tos_state.current_node]];
00270 data->valid = 1;
00271 }
00272
00273 void TOSH_adc_sample_stop(void)
00274 {
00275 ((adc_tick_data_t*)adcEvents[tos_state.current_node]->data)->valid = 0;
00276 }
00277
00278 uint16_t get_adc_data(uint8_t port) {
00279 return tos_state.adc->read(tos_state.current_node, port, tos_state.tos_time);
00280 }
00281
00282 TOS_SIGNAL_HANDLER(SIG_ADC, ()) {
00283 ADCDataReadyEvent ev;
00284 uint16_t data;
00285 uint8_t port = ((adc_tick_data_t*) adcEvents[tos_state.current_node]->data)->port;
00286 data = get_adc_data(port);
00287 TOSH_adc_data_ready(data);
00288 ev.port = port;
00289 ev.data = data;
00290 HPLC_DEBUG(fprintf(stderr, "Sending adc ready event with data = %x\n", data));
00291 sendTossimEvent(tos_state.current_node, AM_ADCDATAREADYEVENT, tos_state.tos_time, &ev);
00292
00293 }
00294
00295 void event_adc_handle(event_t* event, struct TOS_state* state) {
00296 TOS_ISSUE_SIGNAL(SIG_ADC)();
00297 if (((adc_tick_data_t*) event->data)->valid) {
00298 TOS_queue_insert_event(event);
00299 }
00300 else {
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310 }
00311 }
00312
00313 void event_adc_update(event_t* event, int mote, uint8_t port, long long eventTime, int interval) {
00314 adc_tick_data_t* data = event->data;
00315 data->valid = 1;
00316 data->port = port;
00317
00318 event->time = eventTime + interval;
00319 }
00320
00321 void event_adc_create(event_t* event, int mote, uint8_t port, long long eventTime, int interval) {
00322 adc_tick_data_t* data = (adc_tick_data_t*) malloc(sizeof(adc_tick_data_t));
00323 dbg(DBG_MEM, "malloc data entry for adc event: 0x%x\n", (int)data);
00324 data->valid = 1;
00325 data->port = port;
00326
00327 event->data = data;
00328 event->mote = mote;
00329 event->force = 0;
00330 event->pause = 0;
00331 event->time = eventTime + interval;
00332 event->handle = event_adc_handle;
00333 event->cleanup = event_total_cleanup;
00334 }
00335
00336
00337
00338
00339
00340
00341 event_t* radioTickEvents[TOSNODES];
00342 int tickScale[] = {2000, 3000, 4000};
00343 uint8_t radioWaitingState[TOSNODES];
00344 char TOSH_MHSR_start[12] = {0xf0, 0xf0, 0xf0, 0xff, 0x00, 0xff, 0x0f, 0x00, 0xff, 0x0f, 0x0f, 0x0f};
00345
00346 enum {
00347 NOT_WAITING = 0,
00348 WAITING_FOR_ONE_TO_PASS = 1,
00349 WAITING_FOR_ONE_TO_CAPTURE = 2
00350 };
00351
00352
00353
00354 TOS_SIGNAL_HANDLER(SIG_OUTPUT_COMPARE1A, ()) {
00355 tos_state.rfm->stop_transmit(NODE_NUM);
00356 TOSH_rfm_bit_event();
00357 }
00358
00359
00360
00361
00362 void TOSH_rfm_set_bit_rate(uint8_t level) {
00363 event_t* event;
00364 long long ftime;
00365 long long timerSpacing;
00366
00367 if (radioTickEvents[NODE_NUM] != NULL) {
00368 event_radiotick_invalidate(radioTickEvents[NODE_NUM]);
00369 }
00370 dbg(DBG_MEM, "malloc radio bit event.\n");
00371 event = (event_t*)malloc(sizeof(event_t));
00372
00373
00374
00375 timerSpacing = tickScale[(int)level] / tos_state.radio_kb_rate;
00376 ftime = tos_state.tos_time + timerSpacing;
00377
00378 event_radiotick_create(event, NODE_NUM, ftime, timerSpacing);
00379 TOS_queue_insert_event(event);
00380
00381 radioTickEvents[NODE_NUM] = event;
00382 }
00383
00384 void TOSH_rfm_init(void)
00385 {
00386 TOSH_rfm_set_bit_rate(0);
00387 dbg(DBG_BOOT, "RFM initialized\n");
00388 }
00389
00390 uint8_t TOSH_rfm_rx_bit(void)
00391 {
00392 uint8_t data;
00393 data = tos_state.rfm->hears(NODE_NUM);
00394 dbg(DBG_RADIO, "RFM: Mote %i got bit %x\n", TOS_LOCAL_ADDRESS, data);
00395 return data;
00396 }
00397
00398
00399 void TOSH_rfm_tx_bit(uint8_t data)
00400 {
00401 tos_state.rfm->transmit(NODE_NUM, (char)(data & 0x01));
00402 dbg(DBG_RADIO, "RFM: Mote %i sent bit %x\n", TOS_LOCAL_ADDRESS, data & 0x01);
00403 }
00404
00405
00406 void TOSH_rfm_power_off(void) {}
00407
00408 void TOSH_rfm_disable_timer(void) {}
00409
00410 void TOSH_rfm_enable_timer(void) {}
00411
00412 void TOSH_rfm_tx_mode(void) {}
00413
00414 void TOSH_rfm_rx_mode(void) {}
00415
00416
00417 void event_radiotick_handle(event_t* event,
00418 struct TOS_state* state) {
00419 event_queue_t* queue = &(state->queue);
00420 radio_tick_data_t* data = (radio_tick_data_t*)event->data;
00421 if (data->valid) {
00422 if(dbg_active(DBG_RADIO)) {
00423 char ftime[128];
00424 ftime[0] = 0;
00425 printTime(ftime, 128);
00426 dbg(DBG_RADIO, "RADIO: tick event handled for mote %i at %s with interval of %i.\n", event->mote, ftime, data->interval);
00427
00428 }
00429
00430 event->time = event->time + data->interval;
00431 queue_insert_event(queue, event);
00432
00433 TOS_ISSUE_SIGNAL(SIG_OUTPUT_COMPARE1A)();
00434 }
00435 else {
00436 dbg(DBG_RADIO, "RADIO: invalid tick event for mote %i at %lli discarded.\n", data->mote, event->time);
00437
00438 event_cleanup(event);
00439 }
00440 }
00441
00442 void event_radiotick_create(event_t* event, int mote, long long ftime, int interval) {
00443
00444
00445 radio_tick_data_t* data = (radio_tick_data_t*)malloc(sizeof(radio_tick_data_t));
00446 dbg(DBG_MEM, "malloc radio clock bit event data.\n");
00447 data->interval = interval;
00448 data->mote = mote;
00449 data->valid = 1;
00450
00451 event->mote = mote;
00452 event->data = data;
00453 event->time = ftime;
00454 event->handle = event_radiotick_handle;
00455 event->cleanup = event_total_cleanup;
00456 event->force = 0;
00457 event->pause = 0;
00458 }
00459
00460 void event_radiotick_invalidate(event_t* event) {
00461 clock_tick_data_t* data = event->data;
00462 data->valid = 0;
00463 }
00464
00465
00466
00467
00468
00469
00470
00471 void event_spi_byte_create(event_t* fevent, int mote, long long ftime, int interval, int count) __attribute__ ((C, spontaneous));
00472
00473 event_t* spiByteEvents[TOSNODES];
00474
00475 int RADIO_TICKS_PER_EVENT = 100;
00476
00477
00478
00479
00480
00481