Exemplo n.º 1
0
/**
\brief The program starts executing here.
*/
int mote_main(void) {  
   // initialize board
   board_init();
   
   // switch radio LED on
   leds_radio_on();
   
   // prepare bsp_timer
   bsp_timer_set_callback(bsp_timer_cb_compare);
   
   // prepare radiotimer
   radiotimer_setOverflowCb(radiotimer_cb_overflow);
   radiotimer_setCompareCb(radiotimer_cb_compare);
   
   // kick off first bsp_timer compare
   bsp_timer_scheduleIn(BSP_TIMER_PERIOD);
   
   // start periodic radiotimer overflow
   radiotimer_start(RADIOTIMER_OVERFLOW_PERIOD);
   
   // kick off first radiotimer compare
   app_vars.radiotimer_num_compares_left  = RADIOTIMER_NUM_COMPARES-1;
   app_vars.radiotimer_last_compare_val   = RADIOTIMER_COMPARE_PERIOD;
   radiotimer_schedule(app_vars.radiotimer_last_compare_val);
   
   while (1) {
      board_sleep();
   }
}
Exemplo n.º 2
0
/**
\brief The program starts executing here.
*/
int mote_main(void) {
   
   // clear local variable
   memset(&app_vars,0,sizeof(app_vars_t));
   
   // initialize the board
   board_init();
   
   // setup UART
   uart_setCallbacks(cb_uartTxDone,cb_uartRxCb);
   uart_enableInterrupts();
   
   // setup BSP timer
   bsp_timer_set_callback(cb_compare);
   bsp_timer_scheduleIn(BSP_TIMER_PERIOD);
   
   while(1) {
      
      // wait for timer to elapse
      while (app_vars.uartSendNow==0);
      app_vars.uartSendNow = 0;
      
      // send string over UART
      app_vars.uartDone              = 0;
      app_vars.uart_lastTxByteIndex  = 0;
      uart_writeByte(stringToSend[app_vars.uart_lastTxByteIndex]);
      while(app_vars.uartDone==0);
   }
}
Exemplo n.º 3
0
void cb_timer() {
   // set flag
   app_vars.flags |= APP_FLAG_TIMER;
   // update debug stats
   app_dbg.num_timer++;
   // schedule again
   bsp_timer_scheduleIn(TIMER_PERIOD);
}
Exemplo n.º 4
0
void cb_compare(void) {
   
   // have main "task" send over UART
   app_vars.uartSendNow = 1;
   
   // schedule again
   bsp_timer_scheduleIn(BSP_TIMER_PERIOD);
}
Exemplo n.º 5
0
/**
\brief The program starts executing here.
*/
int mote_main(void)
{  
   // initialize board
   board_init();
   
   bsp_timer_set_callback(cb_compare);
   bsp_timer_scheduleIn(BSP_TIMER_PERIOD);
   
   while (1) {
      board_sleep();
   }
}
Exemplo n.º 6
0
void cb_compare() {
   // toggle pin
   debugpins_fsm_toggle();
   
   // toggle error led
   leds_error_toggle();
   
   // increment counter
   app_vars.num_compare++;
   
   // schedule again
   bsp_timer_scheduleIn(BSP_TIMER_PERIOD);
}
Exemplo n.º 7
0
void bsp_timer_cb_compare(void) {
   // toggle pin
   debugpins_frame_toggle();
   
   // toggle error led
   leds_sync_toggle();
   
   // increment counter
   app_dbg.bsp_timer_num_compare++;
   
   // schedule again
   bsp_timer_scheduleIn(BSP_TIMER_PERIOD);
}
Exemplo n.º 8
0
void bsp_timer_cb_compare() {
   uint16_t delay;
   
   // toggle pin
   debugpins_fsm_toggle();
   
   // toggle error led
   leds_error_toggle();
   
   // increment counter
   app_dbg.bsp_timer_num_compare++;
   
   // schedule again
   bsp_timer_scheduleIn(BSP_TIMER_PERIOD);
   
   // wait a bit
   for (delay=0;delay<ISR_DELAY;delay++);
}
Exemplo n.º 9
0
/**
\brief The program starts executing here.
*/
int mote_main(void)
{  
   // initialize board
   board_init();
   
   // prepare bsp_timer
   bsp_timer_set_callback(bsp_timer_cb_compare);
   
   // prepare radiotimer
   radiotimer_setOverflowCb(radiotimer_cb_overflow);
   radiotimer_setCompareCb(radiotimer_cb_compare);
   
   // kick off first bsp_timer compare
   bsp_timer_scheduleIn(BSP_TIMER_PERIOD);
   
   // start periodic radiotimer overflow
   radiotimer_start(RADIOTIMER_OVERFLOW_PERIOD);
   
   while (1) {
      board_sleep();
   }
}
Exemplo n.º 10
0
/**
\brief The program starts executing here.
in order to echo chunks of bytes, each chunk needs to start with character 'H' as
openserial takes different actions according to the initial character of the stream.
*/
int mote_main(void) {
   
   board_init();
   openserial_init();
   
   bsp_timer_set_callback(cb_compare);
   bsp_timer_scheduleIn(BSP_TIMER_PERIOD);
   
   while(1) {
      board_sleep();
      if (app_vars.timerFired==1) {
         app_vars.timerFired = 0;
         if (app_vars.outputting==1) {
            openserial_startInput();
            app_vars.outputting = 0;
         } else {
            openserial_startOutput();
            app_vars.outputting = 1;
         }
      }
   }
}
Exemplo n.º 11
0
/**
\brief The program starts executing here.
*/
int mote_main(void) {
   uint8_t i;
   
   // clear local variables
   memset(&app_vars,0,sizeof(app_vars_t));
   
   // initialize board
   board_init();
   GPIO_Configuration();
   EXTI_Configuration();
   
   GPIOD->BRR = (uint32_t)GPIO_Pin_2;
   
#ifdef RADIO_SLEEP
   PORT_PIN_RADIO_SLP_TR_CNTL_HIGH();
#ifdef RADIO_SLEEP_IN_RUN_MOdE
   while(1);
#endif
   board_sleep();
#endif
   
#ifdef RADIO_TRXOFF
   board_sleep();
#endif
 
   // add callback functions radio
   radio_setOverflowCb(cb_radioTimerOverflows);
   radio_setCompareCb(cb_radioTimerCompare);
   radio_setStartFrameCb(cb_startFrame);
   radio_setEndFrameCb(cb_endFrame);
   
   // prepare packet
   app_vars.packet_len = sizeof(app_vars.packet);
   for (i=0;i<app_vars.packet_len;i++) {
      app_vars.packet[i] = ID;
   }

   // start bsp timer
   bsp_timer_set_callback(cb_timer);
   bsp_timer_scheduleIn(TIMER_PERIOD);
   
   // prepare radio
   radio_rfOn();
   radio_setFrequency(CHANNEL);
   
   // switch in RX by default
   radio_rxEnable();
   app_vars.state = APP_STATE_RX;
   
#ifdef RADIO_RX_ON
   leds_all_off();
   board_sleep();
#endif
   
   // start by a transmit
   app_vars.flags |= APP_FLAG_TIMER;

   while (1) {
      // sleep while waiting for at least one of the flags to be set
      while (app_vars.flags==0x00) {
         board_sleep();
      }
      // handle and clear every flag
      while (app_vars.flags) {

    	  if (app_vars.flags & APP_FLAG_START_FRAME) {
            // start of frame
            
            switch (app_vars.state) {
               case APP_STATE_RX:
                  // started receiving a packet
                  leds_error_on();
                  break;
               case APP_STATE_TX:
                  // started sending a packet
                  leds_sync_on();
                  break;
            }
            // clear flag
            app_vars.flags &= ~APP_FLAG_START_FRAME;
         }
         
         if (app_vars.flags & APP_FLAG_END_FRAME) {
            // end of frame
            
            switch (app_vars.state) {
               case APP_STATE_RX:
                  // done receiving a packet
            	   app_vars.packet_len = sizeof(app_vars.packet);
            	     for (i=0;i<app_vars.packet_len;i++) {
            	        app_vars.packet[i] = 0;
            	     }
                  // get packet from radio
                  radio_getReceivedFrame(app_vars.packet,
                                         &app_vars.packet_len,
                                         sizeof(app_vars.packet),
                                         &app_vars.rxpk_rssi,
                                         &app_vars.rxpk_lqi,
                                         &app_vars.rxpk_crc);
                  
                  leds_error_off();
                  break;
               case APP_STATE_TX:
                  // done sending a packet
                  
                  // switch to RX mode
                  radio_rxEnable();
                  app_vars.state = APP_STATE_RX;
                  
                  leds_sync_off();
                  break;
            }
            // clear flag
            app_vars.flags &= ~APP_FLAG_END_FRAME;
         }
         
         if (app_vars.flags & APP_FLAG_TIMER) {
            // timer fired
            
            if (app_vars.state==APP_STATE_RX) {
               // stop listening
               radio_rfOff();
               
               // prepare packet
               app_vars.packet_len = sizeof(app_vars.packet);
               for (i=0;i<app_vars.packet_len;i++) {
                  app_vars.packet[i] = ID;
               }
               
               // start transmitting packet
               radio_loadPacket(app_vars.packet,app_vars.packet_len);
               radio_txEnable();
               
#ifdef RADIO_PLL_ON
               leds_all_off();
               board_sleep();
#endif
               
               radio_txNow();
#ifdef RADIO_BUSY_TX
               leds_all_off();
               while(1) { //keep sending
                    PORT_PIN_RADIO_SLP_TR_CNTL_HIGH();
                    PORT_PIN_RADIO_SLP_TR_CNTL_LOW();
               }
               board_sleep();
#endif
               
               app_vars.state = APP_STATE_TX;
            }
            
            // clear flag
            app_vars.flags &= ~APP_FLAG_TIMER;
         }
      }
   }
}
Exemplo n.º 12
0
/**
\brief The program starts executing here.
*/
int mote_main(void) {
   uint8_t i;
   
   // clear local variables
   memset(&app_vars,0,sizeof(app_vars_t));
   
   // initialize board
   board_init();
 
   // add callback functions radio
   radio_setOverflowCb(cb_radioTimerOverflows);
   radio_setCompareCb(cb_radioTimerCompare);
   radio_setStartFrameCb(cb_startFrame);
   radio_setEndFrameCb(cb_endFrame);
   
   // prepare packet
   app_vars.packet_len = sizeof(app_vars.packet);
   for (i=0;i<app_vars.packet_len;i++) {
      app_vars.packet[i] = ID;
   }
   
   // start bsp timer
   bsp_timer_set_callback(cb_timer);
   bsp_timer_scheduleIn(TIMER_PERIOD);
   
   // prepare radio
   radio_rfOn();
   radio_setFrequency(CHANNEL);
   
   // switch in RX by default
   radio_rxEnable();
   app_vars.state = APP_STATE_RX;
   
   // start by a transmit
   app_vars.flags |= APP_FLAG_TIMER;
   
   while (1) {
      
      // sleep while waiting for at least one of the flags to be set
      while (app_vars.flags==0x00) {
         board_sleep();
      }
      
      // handle and clear every flag
      while (app_vars.flags) {
         
         
         //==== APP_FLAG_START_FRAME (TX or RX)
         
         if (app_vars.flags & APP_FLAG_START_FRAME) {
            // start of frame
            
            switch (app_vars.state) {
               case APP_STATE_RX:
                  // started receiving a packet
                  
                  // led
                  leds_error_on();
                  break;
               case APP_STATE_TX:
                  // started sending a packet
                  
                  // led
                  leds_sync_on();
                  break;
            }
            
            // clear flag
            app_vars.flags &= ~APP_FLAG_START_FRAME;
         }
         
         
         //==== APP_FLAG_END_FRAME (TX or RX)
         
         if (app_vars.flags & APP_FLAG_END_FRAME) {
            // end of frame
            
            switch (app_vars.state) {
               
               case APP_STATE_RX:
                  
                  // done receiving a packet
                  app_vars.packet_len = sizeof(app_vars.packet);
                  
                  // get packet from radio
                  radio_getReceivedFrame(
                     app_vars.packet,
                     &app_vars.packet_len,
                     sizeof(app_vars.packet),
                     &app_vars.rxpk_rssi,
                     &app_vars.rxpk_lqi,
                     &app_vars.rxpk_crc
                  );
                  
                  // led
                  leds_error_off();
                  break;
               case APP_STATE_TX:
                  // done sending a packet
                  
                  // switch to RX mode
                  radio_rxEnable();
                  app_vars.state = APP_STATE_RX;
                  
                  // led
                  leds_sync_off();
                  break;
            }
            // clear flag
            app_vars.flags &= ~APP_FLAG_END_FRAME;
         }
         
         
         //==== APP_FLAG_TIMER
         
         if (app_vars.flags & APP_FLAG_TIMER) {
            // timer fired
            
            if (app_vars.state==APP_STATE_RX) {
               // stop listening
               radio_rfOff();
               
               // prepare packet
               app_vars.packet_len = sizeof(app_vars.packet);
               for (i=0;i<app_vars.packet_len;i++) {
                  app_vars.packet[i] = ID;
               }
               
               // start transmitting packet
               radio_loadPacket(app_vars.packet,app_vars.packet_len);
               radio_txEnable();
               radio_txNow();
               
               app_vars.state = APP_STATE_TX;
            }
            
            // clear flag
            app_vars.flags &= ~APP_FLAG_TIMER;
         }
      }
   }
}
Exemplo n.º 13
0
/**
\brief Start a timer.

The timer works as follows:
- currentTimeout is the number of ticks before the next timer expires.
- if a new timer is inserted, we check that it is not earlier than the soonest
- if it is earliest, replace it
- if not, insert it in the list

\param duration Number milli-seconds after which the timer will fire.
\param type     Type of timer:
   - #TIMER_PERIODIC for a periodic timer.
   - #TIMER_ONESHOT for a on-shot timer.
\param timetype Units of the <tt>duration</tt>:
   - #TIME_MS when <tt>duration</tt> is in ms.
   - #TIME_TICS when <tt>duration</tt> is in clock ticks.
\param callback The function to call when the timer fires.

\returns The id of the timer (which serves as a handler to stop it) if the
         timer could be started.
\returns TOO_MANY_TIMERS_ERROR if the timer could NOT be started.
 */
opentimer_id_t opentimers_start(uint32_t duration, timer_type_t type, time_type_t timetype, opentimers_cbt callback) {

   uint8_t  id;

   // find an unused timer
   for (id=0; id<MAX_NUM_TIMERS && opentimers_vars.timersBuf[id].isrunning==TRUE; id++);

   if (id<MAX_NUM_TIMERS) {
      // we found an unused timer

      // register the timer
      if (timetype==TIME_MS) {
         opentimers_vars.timersBuf[id].period_ticks      = duration*PORT_TICS_PER_MS;
         opentimers_vars.timersBuf[id].wraps_remaining   = (duration*PORT_TICS_PER_MS/MAX_TICKS_IN_SINGLE_CLOCK);//65535=maxValue of uint16_t
      } else if (timetype==TIME_TICS) {
         opentimers_vars.timersBuf[id].period_ticks      = duration;
         opentimers_vars.timersBuf[id].wraps_remaining   = (duration/MAX_TICKS_IN_SINGLE_CLOCK);//65535=maxValue of uint16_t  
      } else {
         while (1); //error
      }
      //if the number of ticks falls below a 16bit value, use it, otherwise set to max 16bit value
      if(opentimers_vars.timersBuf[id].wraps_remaining==0){
         if (timetype==TIME_MS){ 
            opentimers_vars.timersBuf[id].ticks_remaining   = duration*PORT_TICS_PER_MS;
         } else if (timetype==TIME_TICS) {
            opentimers_vars.timersBuf[id].ticks_remaining   = duration;
         } else {
            // this should never happpen!
   
            // we can not print from within the drivers. Instead:
            // blink the error LED
            leds_error_blink();
            // reset the board
            board_reset();
         }
      }else{
         opentimers_vars.timersBuf[id].ticks_remaining = MAX_TICKS_IN_SINGLE_CLOCK;
      }                                                   
      opentimers_vars.timersBuf[id].type              = type;
      opentimers_vars.timersBuf[id].isrunning         = TRUE;
      opentimers_vars.timersBuf[id].callback          = callback;
      opentimers_vars.timersBuf[id].hasExpired        = FALSE;

      // re-schedule the running timer, if needed
      if (
            (opentimers_vars.running==FALSE)
            ||
            (opentimers_vars.timersBuf[id].ticks_remaining < opentimers_vars.currentTimeout)
      ) {  
         opentimers_vars.currentTimeout            = opentimers_vars.timersBuf[id].ticks_remaining;
         if (opentimers_vars.running==FALSE) {
            bsp_timer_reset();
         }
         bsp_timer_scheduleIn(opentimers_vars.timersBuf[id].ticks_remaining);
      }

      opentimers_vars.running                         = TRUE;

   } else {
      return TOO_MANY_TIMERS_ERROR;
   }

   return id;
}
Exemplo n.º 14
0
void opentimers_sleepTimeCompesation(uint16_t sleepTime)
{
   opentimer_id_t   id;
   PORT_TIMER_WIDTH min_timeout;
   bool             found;
   
   //step 1. reCount the ticks_remain after waking up from sleep
   for(id=0; id<MAX_NUM_TIMERS; id++)
   {
     if (opentimers_vars.timersBuf[id].isrunning==TRUE) 
     {
       if(opentimers_vars.timersBuf[id].ticks_remaining > sleepTime)
       {
         opentimers_vars.timersBuf[id].ticks_remaining -= sleepTime;
       }
       else
       {
         if(opentimers_vars.timersBuf[id].wraps_remaining > 0)
         {
           opentimers_vars.timersBuf[id].wraps_remaining--;
           opentimers_vars.timersBuf[id].ticks_remaining += (MAX_TICKS_IN_SINGLE_CLOCK - sleepTime);
         }
         else
         {
           opentimers_vars.timersBuf[id].hasExpired  = TRUE;
         }
       }
     }
   }
   
   // step 2. call callbacks of expired timers
   for(id=0; id<MAX_NUM_TIMERS; id++) {
      if (opentimers_vars.timersBuf[id].hasExpired==TRUE){

         // call the callback
         opentimers_vars.timersBuf[id].callback(id);
         opentimers_vars.timersBuf[id].hasExpired     = FALSE;

         // reload the timer, if applicable
         if (opentimers_vars.timersBuf[id].type==TIMER_PERIODIC) {
            opentimers_vars.timersBuf[id].wraps_remaining   = (opentimers_vars.timersBuf[id].period_ticks/MAX_TICKS_IN_SINGLE_CLOCK);//65535=maxValue of uint16_t
            //if the number of ticks falls below a 16bit value, use it, otherwise set to max 16bit value
            if(opentimers_vars.timersBuf[id].wraps_remaining==0)                                                
               opentimers_vars.timersBuf[id].ticks_remaining   = opentimers_vars.timersBuf[id].period_ticks;
            else
               opentimers_vars.timersBuf[id].ticks_remaining = MAX_TICKS_IN_SINGLE_CLOCK;

         } else {
            opentimers_vars.timersBuf[id].isrunning   = FALSE;
         }
      }

   }
   
   // step 3. find the minimum remaining timeout among running timers
   found = FALSE;
   for(id=0;id<MAX_NUM_TIMERS;id++) {
      if (
            opentimers_vars.timersBuf[id].isrunning==TRUE &&
            (
                  found==FALSE
                  ||
                  opentimers_vars.timersBuf[id].ticks_remaining < min_timeout
            )
      ) {
         min_timeout    = opentimers_vars.timersBuf[id].ticks_remaining;
         found          = TRUE;
      }
   }

   // step 4. schedule next timeout
   if (found==TRUE) {
      // at least one timer pending
      opentimers_vars.currentTimeout = min_timeout;
      bsp_timer_scheduleIn(opentimers_vars.currentTimeout);
   } else {
      // no more timers pending
      opentimers_vars.running = FALSE;
   }
}
Exemplo n.º 15
0
/**
\brief Function called when the hardware timer expires.

Executed in interrupt mode.

This function maps the expiration event to possibly multiple timers, calls the
corresponding callback(s), and restarts the hardware timer with the next timer
to expire.
 */
void opentimers_timer_callback() {
   
   opentimer_id_t   id;
   PORT_TIMER_WIDTH min_timeout;
   bool             found;
    
   // step 1. Identify expired timers
   for(id=0; id<MAX_NUM_TIMERS; id++) {
      if (opentimers_vars.timersBuf[id].isrunning==TRUE) {

         if(opentimers_vars.currentTimeout >= opentimers_vars.timersBuf[id].ticks_remaining) {
            // this timer has expired
            //check to see if we have completed the whole timer, or we're just wrapping around the max 16bit value
            if(opentimers_vars.timersBuf[id].wraps_remaining == 0){
               // declare as so
               opentimers_vars.timersBuf[id].hasExpired  = TRUE;
            }else{
               opentimers_vars.timersBuf[id].wraps_remaining--;
               if(opentimers_vars.timersBuf[id].wraps_remaining == 0){
                  //if we have fully wrapped around, then set the remainring ticks to the modulus of the total ticks and the max clock value
                  opentimers_vars.timersBuf[id].ticks_remaining = (opentimers_vars.timersBuf[id].period_ticks) % MAX_TICKS_IN_SINGLE_CLOCK;
               }else{
                  opentimers_vars.timersBuf[id].ticks_remaining = MAX_TICKS_IN_SINGLE_CLOCK;
               }
            }
         } else {
            // this timer is not expired

            // update its counter
            opentimers_vars.timersBuf[id].ticks_remaining -= opentimers_vars.currentTimeout;
         }   
      }
   }

   // step 2. call callbacks of expired timers
   for(id=0; id<MAX_NUM_TIMERS; id++) {
      if (opentimers_vars.timersBuf[id].hasExpired==TRUE){

         // call the callback
         opentimers_vars.timersBuf[id].callback(id);
         opentimers_vars.timersBuf[id].hasExpired     = FALSE;

         // reload the timer, if applicable
         if (opentimers_vars.timersBuf[id].type==TIMER_PERIODIC) {
            opentimers_vars.timersBuf[id].wraps_remaining   = (opentimers_vars.timersBuf[id].period_ticks/MAX_TICKS_IN_SINGLE_CLOCK);//65535=maxValue of uint16_t
            //if the number of ticks falls below a 16bit value, use it, otherwise set to max 16bit value
            if(opentimers_vars.timersBuf[id].wraps_remaining==0)                                                
               opentimers_vars.timersBuf[id].ticks_remaining   = opentimers_vars.timersBuf[id].period_ticks;
            else
               opentimers_vars.timersBuf[id].ticks_remaining = MAX_TICKS_IN_SINGLE_CLOCK;

         } else {
            opentimers_vars.timersBuf[id].isrunning   = FALSE;
         }
      }

   }

   // step 3. find the minimum remaining timeout among running timers
   found = FALSE;
   for(id=0;id<MAX_NUM_TIMERS;id++) {
      if (
            opentimers_vars.timersBuf[id].isrunning==TRUE &&
            (
                  found==FALSE
                  ||
                  opentimers_vars.timersBuf[id].ticks_remaining < min_timeout
            )
      ) {
         min_timeout    = opentimers_vars.timersBuf[id].ticks_remaining;
         found          = TRUE;
      }
   }

   // step 4. schedule next timeout
   if (found==TRUE) {
      // at least one timer pending
      opentimers_vars.currentTimeout = min_timeout;
      bsp_timer_scheduleIn(opentimers_vars.currentTimeout);
   } else {
      // no more timers pending
      opentimers_vars.running = FALSE;
   }
}
Exemplo n.º 16
0
void cb_compare(void) {
   app_vars.timerFired = 1;
   bsp_timer_scheduleIn(BSP_TIMER_PERIOD);
}