/*---------------------------------------------------------------------------*/ static void init(void) { radio_is_on = 0; PT_INIT(&pt); #if NURTIMER rtimer_setup(&rt, RTIMER_HARD, (void (*)(struct rtimer *, void *, int status))powercycle, NULL); rtimer_schedule(&rt, CYCLE_TIME, 1); #else rtimer_set(&rt, RTIMER_NOW() + CYCLE_TIME, 1, (void (*)(struct rtimer *, void *))powercycle, NULL); #endif contikimac_is_on = 1; #if WITH_PHASE_OPTIMIZATION phase_init(&phase_list); #endif /* WITH_PHASE_OPTIMIZATION */ #if CONTIKIMAC_CONF_ANNOUNCEMENTS announcement_register_listen_callback(listen_callback); ctimer_set(&announcement_cycle_ctimer, ANNOUNCEMENT_TIME, cycle_announcement, NULL); #endif /* CONTIKIMAC_CONF_ANNOUNCEMENTS */ }
/*---------------------------------------------------------------------------*/ static int turn_on(void) { xmac_is_on = 1; rtimer_set(&rt, RTIMER_NOW() + xmac_config.off_time, 1, (void (*)(struct rtimer *, void *))powercycle, NULL); return 1; }
static char rtimer_task(struct rtimer *t, void *ptr) { PT_BEGIN(&pt); while (1) { leds_on(LEDS_ALL); rtimer_set(t, RTIMER_NOW() + LEDS_ON_PERIOD, 0, (rtimer_callback_t)rtimer_task, NULL); PT_YIELD(&pt); leds_off(LEDS_ALL); rtimer_set(t, timesynch_time_to_rtimer(0), 0, (rtimer_callback_t)rtimer_task, NULL); PT_YIELD(&pt); } PT_END(&pt); }
/** * \brief rtimer process only set the callback ok * \param * */ PROCESS_THREAD(rtimer_ex_process, ev, data) { PROCESS_BEGIN(); led_init(); /* start the led, period 200ms */ rtimer_set(&ex_timer, 1000, 0, led_on, NULL); PROCESS_END(); }
//Rtimer callback function void do_timeout3(struct rtimer *timer, void *ptr) { counter_rtimer++; printf("\n\rProcess 3: RTimer callback called"); /* Re-arm rtimer */ rtimer_set(&timer_rtimer, RTIMER_NOW() + timeout_rtimer, 0, do_timeout3, NULL); }
//Rtimer thread PROCESS_THREAD(process3, ev, data) { PROCESS_BEGIN(); while(1) { rtimer_set(&timer_rtimer, RTIMER_NOW() + timeout_rtimer, 0, do_timeout3, NULL); PROCESS_YIELD(); } PROCESS_END(); }
/*---------------------------------------------------------------------------*/ static int turn_on(void) { if(contikimac_is_on == 0) { contikimac_is_on = 1; contikimac_keep_radio_on = 0; rtimer_set(&rt, RTIMER_NOW() + CYCLE_TIME, 1, powercycle_wrapper, NULL); } return 1; }
static void test_rtimer(void) { while(1) { printf("Now = %u\r\n",RTIMER_NOW()); rtimer_set(&task, RTIMER_NOW() + RTIMER_SECOND*2, RTIMER_SECOND, &callback, NULL); PROCESS_WAIT_UNTIL(sem); sem=0; } }
static void zero_cross_callback(uint8_t port, uint8_t pin) { //INTERRUPTS_DISABLE(); //if (pin == BUTTON_LEFT_PIN) //{ // GPIO_DISABLE_INTERRUPT(BUTTON_LEFT_PORT, BUTTON_LEFT_PIN); //do{ //} while ((GPIO_READ_PIN(GPIO_B_BASE, 0x08)&0x08)==0x08); // clock_delay_usec(8000); // GPIO_SET_PIN(GPIO_C_BASE, 0x04); // clock_delay_usec(50); //disable simistor // GPIO_CLR_PIN(GPIO_C_BASE, 0x04); // GPIO_ENABLE_INTERRUPT(BUTTON_LEFT_PORT, BUTTON_LEFT_PIN); //INTERRUPTS_ENABLE(); // count50++; // if (count50==100) { // GPIO_SET_PIN(GPIO_C_BASE, 0x80); // count50=0; // ctimer_set(&my_ct, CLOCK_SECOND/128, my_rt_callback, NULL); // GPIO_DISABLE_INTERRUPT(BUTTON_LEFT_PORT_BASE, BUTTON_LEFT_PIN_MASK); // nvic_interrupt_disable(BUTTON_LEFT_VECTOR); // clock_delay_usec(4000); // GPIO_ENABLE_INTERRUPT(BUTTON_LEFT_PORT, BUTTON_LEFT_PIN); // nvic_interrupt_enable(BUTTON_LEFT_VECTOR); //timer if RISING detect // rtimer_set(&zero_detector_rtimer, RTIMER_NOW()+(RTIMER_SECOND/1000)*9, 1, zero_detector_enable_callback, NULL); // if (GPIO_READ_PIN(GPIO_B_BASE, 0x08)&0x08) // rtimer_set(&my_rt, RTIMER_NOW()+(RTIMER_SECOND/10000)*abs(95-dimming_time), 1, my_rt_callback, NULL); // rtimer_set(&simistor_strob_rtimer, RTIMER_NOW()+(RTIMER_SECOND/10000)*abs(95-dimming_time), 1, simistor_strob_callback, NULL); if (dim_chan0.thyristor_open_time) // rtimer_set(&dim_chan0.thyristor_rtimer, RTIMER_NOW()+(RTIMER_SECOND/10000)*(255 - dim_chan0.thyristor_open_time)/2.68, 1, simistor_strob_callback, NULL); rtimer_set(&dim_chan0.thyristor_rtimer, RTIMER_NOW()+(RTIMER_SECOND/10000)*((100 - dim_chan0.thyristor_open_time)/1.02), 1, simistor_strob_callback, NULL); // else // nvic_interrupt_enable(BUTTON_LEFT_VECTOR); // } //} }
/*---------------------------------------------------------------------------*/ static int turn_on(void) { if(contikimac_is_on == 0) { contikimac_is_on = 1; contikimac_keep_radio_on = 0; rtimer_set(&rt, RTIMER_NOW() + CYCLE_TIME, 1, (void (*)(struct rtimer *, void *))powercycle, NULL); } return 1; }
static void timeout(struct rtimer *t, void *ptr) { // it is assumed that the timeout rtimer always expires within // the off phase of powercycle immediately after the first received strobe waiting_for_packet = 0; leds_off(LEDS_GREEN); if (we_are_sending == 0) { off(); } rtimer_set(t, last_on + xmac_config.on_time + xmac_config.off_time, 1, (void (*)(struct rtimer *, void *))powercycle, ptr); }
static void schedule_powercycle(struct rtimer *t, rtimer_clock_t time) { int r; if(xmac_is_on) { r = rtimer_set(t, RTIMER_TIME(t) + time, 1, (void (*)(struct rtimer *, void *))powercycle, NULL); if(r) { PRINTF("schedule_powercycle: could not set rtimer\n"); } } }
/*---------------------------------------------------------------------------*/ #if GLOSSY static volatile int in_on_phase = 0; char powercycle(void) { PT_BEGIN(&pt); while(1) { if((xmac_config.off_time > 0) && (!was_timeout)) { if(we_are_sending == 0) { in_on_phase = 1; off(); } if(xmac_is_on) { TBCCR3 += xmac_config.off_time; } PT_YIELD(&pt); } last_on = TBCCR3; was_timeout = 0; if(we_are_sending == 0) { in_on_phase = 0; on(); } if(xmac_is_on) { TBCCR3 += xmac_config.on_time; in_on_phase = 0; } PT_YIELD(&pt); } PT_END(&pt); } #else /* GLOSSY */ static char powercycle(struct rtimer *t, void *ptr) { int r; PT_BEGIN(&pt); while(1) { if((xmac_config.off_time > 0) && (!was_timeout)) { if(we_are_sending == 0) { off(); } if(xmac_is_on) { r = rtimer_set(t, RTIMER_TIME(t) + xmac_config.off_time, 1, (void (*)(struct rtimer *, void *))powercycle, ptr); } PT_YIELD(&pt); } last_on = RTIMER_TIME(t); was_timeout = 0; if(we_are_sending == 0) { on(); } if(xmac_is_on) { r = rtimer_set(t, RTIMER_TIME(t) + xmac_config.on_time, 1, (void (*)(struct rtimer *, void *))powercycle, ptr); } PT_YIELD(&pt); } PT_END(&pt); }
// turn on RDC layer static int on(void) { PRINTF("turn on RDC layer\n"); #ifdef SF_MOTE_TYPE_AP rtimer_set(&BSTimer,RTIMER_NOW()+segment_period,0,TDMA_BS_send,NULL); return NETSTACK_RADIO.on(); #else // SF_MOTE_TYPE_SENSOR TDMA_SN_listen(); return 1; #endif }
/*---------------------------------------------------------------------------*/ static char plb_powercycle(void) { PRINT_P("plb_powercycle [sr:%d cw:%d]\n",send_req,c_wait); PT_BEGIN(&pt); while(1) { // check on/send state if(send_req == 1 && c_wait==1){ PRINTF("plb_powercycle send DATA <start>\n"); send_req = 0; //avoid repeat sending plb_send_data(sent_callback, sent_ptr); PRINTF("plb_powercycle send DATA <end>\n"); radio_off(); NETSTACK_MAC.input(); } /* on */ radio_on(); rtimer_set(&rt, RTIMER_NOW() + PC_ON_TIME, 1, (void (*)(struct rtimer *, void *))plb_powercycle, NULL); PT_YIELD(&pt); /* off */ if(wait_packet == 0){ radio_off(); } else if (wait_packet > 0) { wait_packet = 0; } rtimer_set(&rt, RTIMER_NOW() + PC_OFF_TIME, 1, (void (*)(struct rtimer *, void *))plb_powercycle, NULL); PT_YIELD(&pt); } PT_END(&pt); }
/** * @brief start_discovery_process */ static void start_discovery_process(){ uint16_t Tup_bound = 0, random_slot = 1; random_slot = 1 + random_rand()%period_length; Tup_bound = (period_length*period_length)/4; //random_slot = random_slot + random_rand()%Tup_bound ; /*if (rimeaddr_node_addr.u8[0] < 130){ random_slot = random_slot + random_rand()%period_length; }else{ random_slot = random_slot + Tup_bound + random_rand()%period_length; }*/ /*if(rimeaddr_node_addr.u8[0] == 10 || rimeaddr_node_addr.u8[0] == 11){ //uint8_t channel_group = 1+rimeaddr_node_addr.u8[0]%GROUP_MERGE_SIZE; uint8_t channel_group = 1; random_slot = 0; cc2420_set_channel(i3e154_channels[channel_group]); }*/ //enable random offset wait flag.. rand_offset_wait_flag = 1; node_slots_offset = random_slot; slot_upperBound = 10000 - node_slots_offset; //-------- HERE WE DIVIDE NODES INTO CHANNELS......... if(1 /*rimeaddr_node_addr.u8[0] == 10*/){ //uint8_t channel_group = 1+rimeaddr_node_addr.u8[0]%GROUP_MERGE_SIZE; uint8_t channel_group = 0; cc2420_set_channel(i3e154_channels[channel_group]); } COOJA_DEBUG_PRINTF("======>>>>> ID:%2u, channel:%2u\n", rimeaddr_node_addr.u8[0], cc2420_get_channel()); //--------END GROUP CHANNEL SETTING... rtimer_clock_t startTime = RTIMER_NOW() + 2; int ret = rtimer_set(&generic_timer, startTime, 1, (void (*)(struct rtimer *, void *))power_cycle, NULL); if(ret){ PRINTF("synchronization failed\n"); } }
/** * @brief schedule_fixed * @param rt * @param next_time * @return */ static char schedule_fixed(struct rtimer *rt, rtimer_clock_t next_time){ if(RTIMER_CLOCK_LT(next_time, RTIMER_NOW() + 1)) { next_time = RTIMER_NOW() + 3; } int ret = rtimer_set(&generic_timer, next_time, 1, (void (*)(struct rtimer *, void *))power_cycle, NULL); if(ret){ PRINTF("synchronization failed\n"); } return 0; }
/*---------------------------------------------------------------------------*/ static int turn_on(void) { if(contikimac_is_on == 0) { contikimac_is_on = 1; #if NURTIMER rtimer_schedule(&rt, CYCLE_TIME, 1); #else rtimer_set(&rt, RTIMER_NOW() + CYCLE_TIME, 1, (void (*)(struct rtimer *, void *))powercycle, NULL); #endif } return 1; }
/*---------------------------------------------------------------------------*/ static void init(void) { radio_is_on = 0; PT_INIT(&pt); rtimer_set(&rt, RTIMER_NOW() + CYCLE_TIME, 1, (void (*)(struct rtimer *, void *))powercycle, NULL); contikimac_is_on = 1; #if WITH_PHASE_OPTIMIZATION phase_init(&phase_list); #endif /* WITH_PHASE_OPTIMIZATION */ }
/*---------------------------------------------------------------------------*/ static void init(void) { radio_is_on = 0; PT_INIT(&pt); rtimer_set(&rt, RTIMER_NOW() + CYCLE_TIME, 1, powercycle_wrapper, NULL); contikimac_is_on = 1; #if WITH_PHASE_OPTIMIZATION phase_init(); #endif /* WITH_PHASE_OPTIMIZATION */ }
PROCESS_THREAD(blink_process, ev, data) { PROCESS_BEGIN(); while(1) { leds_on(LEDS_RED); /* start an rtimer; the 1 signifies how long time in rtimer ticks the task * will take (ie the callback) but is currently not implemented on a deeper * level. So, use just any value for now. */ rtimer_set(&rt, RTIMER_NOW() + RTIMER_SECOND/4, 1, rtcb, NULL); leds_toggle(LEDS_GREEN); etimer_set(&et, CLOCK_SECOND/2); PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et)); } PROCESS_END(); }
PROCESS_THREAD(glossy_test, ev, data) { PROCESS_BEGIN(); leds_on(LEDS_RED); // Initialize Glossy data. glossy_data.seq_no = 0; // Start print stats processes. process_start(&glossy_print_stats_process, NULL); // Start Glossy busy-waiting process. process_start(&glossy_process, NULL); // Start Glossy experiment in one second. rtimer_set(&rt, RTIMER_NOW() + RTIMER_SECOND, 1, (rtimer_callback_t)glossy_scheduler, NULL); PROCESS_END(); }
int main(void) { initialize(); while(1) { process_run(); #if TESTRTIMER /* Timeout can be increased up to 8 seconds maximum. * A one second cycle is convenient for triggering the various debug printouts. * The triggers are staggered to avoid printing everything at once. */ if (rtimerflag) { rtimer_set(&rt, RTIMER_NOW()+ RTIMER_ARCH_SECOND*1UL, 1,(void *) rtimercycle, NULL); rtimerflag=0; #if STAMPS if ((rtime%STAMPS)==0) { PRINTA("%us ",rtime); } #endif rtime+=1; #if STACKMONITOR if ((rtime%STACKMONITOR)==3) { extern uint16_t __bss_end; uint16_t p=(uint16_t)&__bss_end; do { if (*(uint16_t *)p != 0x4242) { PRINTA("Never-used stack > %d bytes\n",p-(uint16_t)&__bss_end); break; } p+=4; } while (p<RAMEND-4); } #endif } #endif /* TESTRTIMER */ } return 0; }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(timesync_master_process, ev, data) { static struct etimer et; PROCESS_BEGIN(); timesynch_set_authority_level(0); PT_INIT(&pt); // wait until network is synchronized etimer_set(&et, SYNCH_PERIOD); PROCESS_WAIT_UNTIL(etimer_expired(&et)); rtimer_set(&rt, timesynch_time_to_rtimer(0), 0, (rtimer_callback_t)rtimer_task, NULL); PROCESS_END(); }
/*---------------------------------------------------------------------------*/ static void schedule_powercycle_fixed(struct rtimer *t, rtimer_clock_t fixed_time) { int r; if(contikimac_is_on) { if(RTIMER_CLOCK_LT(fixed_time, RTIMER_NOW() + 1)) { fixed_time = RTIMER_NOW() + 1; } r = rtimer_set(t, fixed_time, 1, (void (*)(struct rtimer *, void *))powercycle, NULL); if(r != RTIMER_OK) { PRINTF("schedule_powercycle: could not set rtimer\n"); } } }
static void schedule_powercycle(struct rtimer *t, rtimer_clock_t time) { int r; if(contikimac_is_on) { if(RTIMER_CLOCK_LT(RTIMER_TIME(t) + time, RTIMER_NOW() + 2)) { time = RTIMER_NOW() - RTIMER_TIME(t) + 2; } r = rtimer_set(t, RTIMER_TIME(t) + time, 1, (void (*)(struct rtimer *, void *))powercycle, NULL); if(r != RTIMER_OK) { PRINTF("schedule_powercycle: could not set rtimer\n"); } } }
/*---------------------------------------------------------------------------*/ static int send(void) { int r; id_counter++; /* Clean up already sent packets */ while(lastqueued != nextsend) { PRINTF("BUFFER Cleaning up packet #%i\n", id[lastqueued]); queuebuf_free(data[lastqueued]); data[lastqueued] = NULL; lastqueued = (lastqueued + 1) % NUM_PACKETS; } if((freeslot + 1) % NUM_PACKETS == lastqueued) { PRINTF("BUFFER Buffer full, dropping packet #%i\n", (id_counter+1)); return UIP_FW_DROPPED; } /* Allocate queue buf for packet */ data[freeslot] = queuebuf_new_from_packetbuf(); id[freeslot] = id_counter; if(data[freeslot] == NULL) { PRINTF("BUFFER Queuebuffer full, dropping packet #%i\n", id[freeslot]); return UIP_FW_DROPPED; } PRINTF("BUFFER Wrote packet #%i to buffer \n", id[freeslot]); freeslot = (freeslot + 1) % NUM_PACKETS; if(!timer_on) { PRINTF("TIMER Starting timer\n"); r = rtimer_set(&rtimer, RTIMER_NOW() + RTIMER_SECOND, 1, (void (*)(struct rtimer *, void *))transmitter, NULL); if(r) { PRINTF("TIMER Error #3: %d\n", r); } else { timer_on = 1; } } return UIP_FW_OK; /* TODO Return what? */ }
/*---------------------------------------------------------------------------*/ static void init(void) { current_slot = TOTAL_SLOTS + 1; int t; if(node_id < 10) { t = REGULAR_SLOT + 300; } else { t = REGULAR_SLOT + 240; } if(!(rtimer_set(&taskSlot, RTIMER_NOW() + t, 1, (void (*)(struct rtimer *, void *))advanceSlot, NULL) == RTIMER_OK)) { printf("%s\n", "WPI-MAC: Could not schedule initial task!!!!!"); } int i = 0; for(; i < TOTAL_SLOTS; i++) { QPQueue[i] = NULL; } }
/*---------------------------------------------------------------------------*/ static void schedule_powercycle_fixed(struct rtimer *t, rtimer_clock_t fixed_time) { int r; rtimer_clock_t now; if(contikimac_is_on) { now = RTIMER_NOW(); if(RTIMER_CLOCK_LT(fixed_time, now + RTIMER_GUARD_TIME)) { fixed_time = now + RTIMER_GUARD_TIME; } r = rtimer_set(t, fixed_time, 1, powercycle_wrapper, NULL); if(r != RTIMER_OK) { PRINTF("schedule_powercycle: could not set rtimer\n"); } } }
/*---------------------------------------------------------------------------*/ static void init(void) { radio_is_on = 0; waiting_for_packet = 0; PT_INIT(&pt); rtimer_set(&rt, RTIMER_NOW() + xmac_config.off_time, 1, (void (*)(struct rtimer *, void *))powercycle, NULL); xmac_is_on = 1; #if WITH_ENCOUNTER_OPTIMIZATION list_init(encounter_list); memb_init(&encounter_memb); #endif /* WITH_ENCOUNTER_OPTIMIZATION */ #if XMAC_CONF_ANNOUNCEMENTS announcement_register_listen_callback(listen_callback); ctimer_set(&announcement_cycle_ctimer, ANNOUNCEMENT_TIME, cycle_announcement, NULL); #endif /* XMAC_CONF_ANNOUNCEMENTS */ }