/**
\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;
         }
      }
   }
}
Exemple #2
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;
         }
      }
   }
}
Exemple #3
0
/**
\brief The program starts executing here.
*/
int mote_main(void) {
   
   // 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);
   
   // setup UART
   uart_setCallbacks(cb_uartTxDone,cb_uartRxCb);
   
   // prepare radio
   radio_rfOn();
   radio_setFrequency(CHANNEL);
   
   // switch in RX
   radio_rxEnable();
   
   while (1) {
      
      // sleep while waiting for at least one of the rxpk_done to be set
      app_vars.rxpk_done = 0;
      while (app_vars.rxpk_done==0) {
         board_sleep();
      }
      
      // if I get here, I just received a packet
      
      //===== get packet from radio
      
      // led
      leds_sync_on();
      
      // get packet from radio
      radio_getReceivedFrame(
         app_vars.rxpk_buf,
         &app_vars.rxpk_len,
         sizeof(app_vars.rxpk_buf),
         &app_vars.rxpk_rssi,
         &app_vars.rxpk_lqi,
         &app_vars.rxpk_crc
      );
      
      // read the packet number
      app_vars.rxpk_num = app_vars.rxpk_buf[0];
      
      // led
      leds_sync_off();
      
      //===== send notification over serial port
      
      // led
      leds_error_on();
      
      // format frame to send over serial port
      app_vars.uart_txFrame[0] = app_vars.rxpk_len;  // packet length
      app_vars.uart_txFrame[1] = app_vars.rxpk_num;  // packet number
      app_vars.uart_txFrame[2] = app_vars.rxpk_rssi; // RSSI
      app_vars.uart_txFrame[3] = app_vars.rxpk_lqi;  // LQI
      app_vars.uart_txFrame[4] = app_vars.rxpk_crc;  // CRC
      app_vars.uart_txFrame[5] = 0xff;               // closing flag
      app_vars.uart_txFrame[6] = 0xff;               // closing flag
      app_vars.uart_txFrame[7] = 0xff;               // closing flag
      
      app_vars.uart_done          = 0;
      app_vars.uart_lastTxByte    = 0;
      
      // send app_vars.uart_txFrame over UART
      uart_clearTxInterrupts();
      uart_clearRxInterrupts();
      uart_enableInterrupts();
      uart_writeByte(app_vars.uart_txFrame[app_vars.uart_lastTxByte]);
      while (app_vars.uart_done==0); // busy wait to finish
      uart_disableInterrupts();
      
      // led
      leds_error_off();
   }
}
Exemple #4
0
/**
\brief The program starts executing here.
*/
int mote_main() {
   
   // needed since we are disabling/enabling interrupts below
   INTERRUPT_DECLARATION();
   
   // 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);
   
   // setup UART
   uart_setCallbacks(cb_uartTxDone,cb_uartRxCb);
   
   app_vars.uart_end=FALSE;
   
   // prepare radio
   radio_rfOn();
   radio_setFrequency(CHANNEL);
   
   // switch in RX by default
   radio_rxEnable();
   
   app_vars.flags=0x00; //wait for rx
   
   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) {
         DISABLE_INTERRUPTS();
         leds_sync_on();
         // done receiving a 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);
         
         app_vars.packet_num=app_vars.packet[0];//packet number
         leds_error_off();
         stringToSend[0]=app_vars.packet_num;
         stringToSend[1]=app_vars.rxpk_rssi;
         stringToSend[2]=app_vars.rxpk_lqi;
         stringToSend[3]=app_vars.rxpk_crc;
         stringToSend[4]= 0xFF;      
         
         //clear this interrupt.
         app_vars.flags = 0x00;
         app_vars.uart_end=FALSE;
         app_vars.uart_lastTxByte = 0;
         ENABLE_INTERRUPTS();  
         // send stringToSend over UART
         
         
         uart_clearTxInterrupts();
         uart_clearRxInterrupts();
         
         uart_enableInterrupts();
         uart_writeByte(stringToSend[app_vars.uart_lastTxByte]);
         
         while (app_vars.uart_end==FALSE);//wait to finish              
         uart_disableInterrupts();
         
         // clear flag
         
         leds_sync_off(); 
      }
   }
}