/* This function is called whenever a broadcast message is received. */ static void broadcast_recv(struct broadcast_conn *c, const rimeaddr_t *from) { struct broadcast_message *m; struct unicast_message sMsg; struct RTC_time t, t_now; rimeaddr_t *add; struct sensor_data tk; m = (struct broadcast_message *)packetbuf_dataptr(); t = m->time; if(id==0&&status==0) { rimeaddr_copy(&nmaddr, from); sMsg.type = INIT_REQ; sMsg.time = t; sMsg.numSampl = 20; strcpy(sMsg.name, "Dust sensor\0"); packetbuf_copyfrom(&sMsg, sizeof(struct unicast_message)); RTC_getTime(&t_now); unicast_send(&unicast, &nmaddr); broadcast_close(&broadcast); status = 1; return; } if(m->type == 1) { t = m->time; } }
static void alarmCallback() { static struct RTC_time t_now; static struct unicast_message *tmp, sMsg; static struct sensor_data sens_data; static uint16_t samplingDelay = 280; static uint16_t delay = 40; // ADC requires minimum 20 us of sampling time. static uint16_t sleepDelay = 9680; static uint16_t avg; static uint8_t count = 0; if(count==0) RTC_getTime(&t_now); int len; uint8_t i, numSampl = 20; static uint16_t v = 0, v1; ioPins_configurePin(8, USEGPIO, OUTPUT, NOPULLUP, HYSTERESIS_OFF); ioPins_configurePin(7, USEGPIO, OUTPUT, NOPULLUP, HYSTERESIS_OFF); ioPins_setValue(8, 1); ioPins_setValue(7, 1); avg = 0; for(i=0;i<numSampl;i++) { ioPins_setValue(8, 0); clock_delay_usec(samplingDelay); v = ioPins_getValue(16); avg+=v; clock_delay_usec(delay); ioPins_setValue(8, 1); clock_delay_usec(sleepDelay); } sens_data.lraw[count] = avg&0xFF; sens_data.hraw[count] = (avg&0xFF00)>>8; if(count==59) { sMsg.id = id; sMsg.type = DATA_REQ; sMsg.time = t_now; strcpy(sMsg.name, "Dust sensor\0"); sMsg.data = sens_data; sMsg.numSampl = numSampl; packetbuf_copyfrom(&sMsg, sizeof(struct unicast_message)); unicast_send(&unicast, &nmaddr); count = 0; } else { count++; } }
static void startReading() { struct unicast_message msg, *tmp; rimeaddr_t *add; struct RTC_time time; while(1) { msg.id = id; msg.type = DATA_REQ; RTC_getTime(&time); msg.time = time; packetbuf_copyfrom(&msg, sizeof(struct unicast_message)); unicast_send(&unicast, &nmaddr); clock_delay_msec(1000); } }
void net_node_run(void){ struct net_packet_broadcast pkg; while(true) { while(Radio_available() == 0) { __WFE(); } Radio_recive((uint8_t *) &pkg, sizeof(struct net_packet_broadcast)); Radio_enable(false); // convet to tx mode and load packet pkg.hello[0] = 'h'; pkg.hello[1] = 'e'; pkg.hello[2] = 'l'; pkg.hello[3] = 'l'; pkg.hello[4] = 'o'; pkg.time = RTC_getTime(); Radio_loadbuf_broadcast(&pkg); Radio_setMode(Radio_Mode_TX, false); } }
PROCESS_THREAD(test_RTC_process, ev, data) { PROCESS_BEGIN(); leds_off(LEDS_ALL); // Set the RTC time to be 12:34:56 on 30/1/15 // struct RTC_time t = { 0, // hundredths 0, // tenths 56, // seconds 34, // minutes 12, // hours 30, // day 01, // month 15 // year }; struct RTC_alarm q = { 10, // seconds 35, // minutes 12, // hours 30, // day 01 // month }; RTC_setTime(&t); // Alarm can be set with a callback // (or this can be NULL) RTC_setAlarm(&q, alarmCallback, RPT_MINUTE); int i = 0; while(1) { printf("GOING TO SLEEP\n"); clock_delay_msec(50); /* ------------ * Go to sleep * ------------ */ *CRM_SLEEP_CNTL = 0x71; // hibernate, keep all RAM pages, retain state, don't power GPIO, approx. 2kHz = 16.1uA while((*CRM_STATUS & 0x1) == 0) // wait for the sleep cycle to complete { continue; } *CRM_STATUS = 1; // write 1 to sleep_sync --- this clears the bit (it's a r1wc bit) and powers down /* ------------ * Wake up again * ------------ */ printf("AWAKE AGAIN\n"); for (i = 0; i < 2; i++) FLASH_LED(LEDS_ALL); PROCESS_YIELD(); RTC_getTime(&t); printf("%02d/%02d/%02d %02d:%02d:%02d.%d%d\n", t.day, t.month, t.year, t.hours, t.minutes, t.seconds, t.tenths, t.hundredths); } PROCESS_END(); }
void net_test_init(uint8_t test){ struct radio_address local; struct radio_address broadcast; //memcpy(&local, (void *) &(DEVINFO->UNIQUEL), 4); local.b0 = *(&(DEVINFO->UNIQUEL)+0); local.b1 = *(&(DEVINFO->UNIQUEL)+1); local.b2 = *(&(DEVINFO->UNIQUEL)+2); local.b3 = *(&(DEVINFO->UNIQUEL)+3); local.b4 = 0x00; broadcast.b0 = 0x55; broadcast.b1 = 0xAA; broadcast.b2 = 0xff; broadcast.b3 = 0xAA; broadcast.b4 = 0x55; // local and broadcast recive adresses and tx on local address Radio_init(&local, &broadcast); if (test == NET_TEST_TX_ONLY) { struct net_packet_broadcast p; Radio_enable(false); Radio_setMode(Radio_Mode_TX, false); p.hello[0] = 'h'; p.hello[1] = 'e'; p.hello[2] = 'l'; p.hello[3] = 'l'; p.hello[4] = 'o'; while(true) { DBG_probe_on(DBG_Probe_1); p.time = RTC_getTime(); p.tick = RTC_getTickCount(); Radio_loadbuf_broadcast(&p); Radio_enable(true); for(volatile uint8_t i=0; i < 9; ++i) { __NOP(); } Radio_enable(false); while(radio_has_packets_to_sent()){ __WFI(); } DBG_probe_off(DBG_Probe_1); delay(250); delay(250); } } else if (test == NET_TEST_RX_ONLY) { Radio_setMode(Radio_Mode_RX, false); Radio_enable(true); //uint8_t i = 0; while(true){ if(Radio_available() > 0) { struct net_packet_broadcast buf; Radio_recive((uint8_t *) &buf, sizeof(struct net_packet_broadcast)); //++i; //if (i > 5) { // RTC_reset_irq(abs((buf.tick % RTC_S) - (RTC_getTickCount() % RTC_S))); // i = 0; //} DBG_probe_toggle(DBG_Probe_1); } } } else if ( test == NET_TEST_RX_TX) { struct net_packet_broadcast sent; struct net_packet_broadcast recv; sent.hello[0] = 'h'; sent.hello[1] = 'e'; sent.hello[2] = 'l'; sent.hello[3] = 'l'; sent.hello[4] = 'o'; Radio_setMode(Radio_Mode_TX, false); Radio_loadbuf_broadcast(&sent); Radio_enable(true); for(volatile int i=0; i < 10; ++i) { __NOP(); } Radio_enable(false); while (radio_has_packets_to_sent()); while(true){ Radio_setMode(Radio_Mode_RX, false); Radio_enable(true); while (Radio_available() == 0); Radio_recive((uint8_t *) &recv, sizeof(struct net_packet_broadcast)); Radio_enable(false); Radio_setMode(Radio_Mode_TX, false); Radio_loadbuf_broadcast(&sent); Radio_enable(true); for(volatile int i=0; i < 150; ++i) { __NOP(); } Radio_enable(false); while (radio_has_packets_to_sent()); } } else { while(true); } }