static void start2Babble() { uint8_t msg[1]; /* frequency hopping doesn't support sleeping just yet */ #ifndef FREQUENCY_HOPPING /* wake up radio. */ SMPL_Ioctl( IOCTL_OBJ_RADIO, IOCTL_ACT_RADIO_AWAKE, 0); #endif /* Send the bad news message. To prevent confusion with different "networks" * such as neighboring smoke alarm arrays send a token controlled by a DIP * switch, for example, and filter in this token. */ msg[0] = BAD_NEWS; while (1) { FHSS_ACTIVE( nwk_pllBackgrounder( false ) ); /* manage FHSS */ /*wait "a while" */ NWK_DELAY(100); /* babble... */ SMPL_Send(SMPL_LINKID_USER_UUD, msg, sizeof(msg)); toggleLED(2); } }
static void linkTo() { uint8_t msg[10], delay = 0; memset(msg, 0, 10); while (SMPL_SUCCESS != SMPL_Link(&sLinkID1)) { /* blink LEDs until we link successfully */ toggleLED(1); toggleLED(2); SPIN_ABOUT_A_SECOND; } /* we're linked. turn off red LED. received messages will toggle the green LED. */ if (BSP_LED2_IS_ON()) { toggleLED(2); } /* turn on RX. default is RX off. */ SMPL_Ioctl( IOCTL_OBJ_RADIO, IOCTL_ACT_RADIO_RXON, 0); /* put LED to toggle in the message */ msg[0] = 2; /* toggle red */ while (1) { __bis_SR_register(LPM4_bits + GIE); // Enter LPM4 w/interrupt SPIN_ABOUT_A_SECOND; if (delay > 0x00) { SPIN_ABOUT_A_SECOND; } if (delay > 0x01) { SPIN_ABOUT_A_SECOND; } if (delay > 0x02) { SPIN_ABOUT_A_SECOND; } /* delay longer and longer -- then start over */ delay = (delay+1) & 0x03; /* put the sequence ID in the message */ msg[9] = ++sTxTid; ReadRTCCTimeDate(&Rtcctimedate); /* put data into packet */ msg[1] = Rtcctimedate.Sec; msg[2] = Rtcctimedate.Min; msg[3] = Rtcctimedate.Hour; msg[4] = Rtcctimedate.Day; msg[5] = Rtcctimedate.Date; msg[6] = Rtcctimedate.Month; msg[7] = Rtcctimedate.Year; SMPL_Send(sLinkID1, msg, sizeof(msg)); } }
__interrupt void RTC_ISR(void) { #if defined(ACCESS_POINT) uint8_t sync_msg[6]; uint8_t i=0; #endif switch (RTCIV) { case 0x02: break; case 0x04: break; case 0x06: RTCCTL01 |= RTCHOLD; /* Toggle sleep state if (__get_SR_register_on_exit() & CPUOFF) { LPM3_EXIT; } else { Sleep_command(SLEEP_TIME_AFTER_DATA); } */ LPM3_EXIT; //UCSCTL6_L |= XT1OFF; break; case 0x08: break; case 0x0A: //RT1PSIFG #if defined(ACCESS_POINT) sync_msg[0]='S'; //Command 'SS' : Start Sampling sync_msg[1]='Y'; sync_msg[2]=RTCNT4; sync_msg[3]=RTCNT3; sync_msg[4]=RTCNT2; sync_msg[5]=RTCNT1; for(i=0;i<3;i++) // 3 attempts to send synch command { if ( SMPL_SUCCESS == SMPL_Send(SMPL_LINKID_USER_UUD, sync_msg, sizeof(sync_msg))) //broadcast message { break; } } //UsbSendString(sync_msg,6); #endif break; } RTCCTL01 &= ~RTCAIFG; RTCPS1CTL &= ~RT1PSIFG; }
static void linkFrom() { uint8_t msg[2], tid = 0; /* Turn off one LED so we can tell the device is now listening. * Received messages will toggle the other LED. */ //toggleLED(1); /* listen for link forever... */ while (1) { if (SMPL_SUCCESS == SMPL_LinkListen(&sLinkID2)) { break; } /* Implement fail-to-link policy here. otherwise, listen again. */ } /* we're linked. turn off red LED. Received messages will toggle the green LED. */ if (BSP_LED2_IS_ON()) { toggleLED(2); } if (BSP_LED1_IS_ON()) { toggleLED(1); } /* turn on LED1 on the peer in response to receiving a frame. */ *msg = 0x01; /* toggle red led */ /* turn on RX. default is RX off. */ SMPL_Ioctl( IOCTL_OBJ_RADIO, IOCTL_ACT_RADIO_RXON, 0); while (1) { /* Wait for a frame to be received. The Rx handler, which is running in * ISR thread, will post to this semaphore allowing the application to * send the reply message in the user thread. */ if (sSemaphore) { *(msg+1) = ++tid; SMPL_Send(sLinkID2, msg, 2); /* Reset semaphore. This is not properly protected and there is a race * here. In theory we could miss a message. Good enough for a demo, though. */ sSemaphore = 0; } } }
/** * Check for semaphore set and ping back a message */ static void PingCheckStep(void) { uint8_t msg[2]; if (sSemaphore) { sSemaphore = 0; *msg = 0x01; /* toggle red led */ *(msg+1) = ++sTxTid; SMPL_Send(sLinkID2, msg, 2); } }
static void processMessage(linkID_t lid, uint8_t *msg, uint8_t len) { /* do something useful */ if (len) { toggleLED(*msg); *msg |= NWK_APP_REPLY_BIT; #ifdef FREQUENCY_AGILITY /* send ack frame... */ SMPL_Send(lid, msg, len); #endif } return; }
//***************************************************************************** // // This function is called whenever an alert is received from another device. // It retransmits the alert every 100mS and toggles an LED to indicate that // the alarm has been sounded. // // This function does not return. // //***************************************************************************** void Start2Babble() { uint8_t pucMsg[1]; // // Tell the user what we are doing. // UpdateStatus(false, "Retransmitting alert"); /* wake up radio. */ SMPL_Ioctl( IOCTL_OBJ_RADIO, IOCTL_ACT_RADIO_AWAKE, 0); // // Send the bad news message. In a real application, to prevent confusion // with different "networks" such as neighboring smoke alarm arrays send a // token controlled by a DIP switch, for example, and filter in this token. // pucMsg[0] = BAD_NEWS; // // Keep sending the alert forever. In "real life" you would provide a method // to stop sending the alert when the sensor was reset or the alert condition // cleared. // while (1) { // // Wait 100mS or so. // ApplicationDelay(100); // // Babble... // SMPL_Send(SMPL_LINKID_USER_UUD, pucMsg, sizeof(pucMsg)); ToggleLED(2); } }
//***************************************************************************** // // Main application entry function. // //***************************************************************************** int main(void) { tBoolean bSuccess, bRetcode; unsigned char pucMsg[2]; unsigned char ucTid; unsigned char ucDelay; unsigned long ulLastRxCount, ulLastTxCount; smplStatus_t eRetcode; // // Set the system clock to run at 50MHz from the PLL // ROM_SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); // // NB: We don't call PinoutSet() in this testcase since the EM header // expansion board doesn't currently have an I2C ID EEPROM. If we did // call PinoutSet() this would configure all the EPI pins for SDRAM and // we don't want to do this. // g_eDaughterType = DAUGHTER_NONE; // // Enable peripherals required to drive the LCD. // ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE); ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOH); // // Configure SysTick for a 10Hz interrupt. // ROM_SysTickPeriodSet(ROM_SysCtlClockGet() / TICKS_PER_SECOND); ROM_SysTickEnable(); ROM_SysTickIntEnable(); // // Initialize the display driver. // Kitronix320x240x16_SSD2119Init(); // // Initialize the touch screen driver. // TouchScreenInit(); // // Set the touch screen event handler. // TouchScreenCallbackSet(WidgetPointerMessage); // // Add the compile-time defined widgets to the widget tree. // WidgetAdd(WIDGET_ROOT, (tWidget *)&g_sHeading); // // Initialize the status string. // UpdateStatus(true, "Please choose the operating mode."); // // Paint the widget tree to make sure they all appear on the display. // WidgetPaint(WIDGET_ROOT); // // Initialize the SimpliciTI BSP. // BSP_Init(); // // Set the SimpliciTI device address using the current Ethernet MAC address // to ensure something like uniqueness. // bRetcode = SetSimpliciTIAddress(); if(!bRetcode) { // // The board does not have a MAC address configured so we can't set // the SimpliciTI device address (which we derive from the MAC address). // while(1); } // // Initialize the SimpliciTI stack and supply our receive callback // function pointer. // SMPL_Init(RxCallback); // // Initialize our message ID, initial inter-message delay and packet // counters. // ucTid = 0; ucDelay = 0; ulLastRxCount = 0; ulLastTxCount = 0; // // Fall into the command line processing loop. // while (1) { // // Process any messages from or for the widgets. // WidgetMessageQueueProcess(); // // Check to see if we've been told to do anything. // if(g_ulCommandFlags) { // // Has the mode been set? If so, set up the display to show the // "LEDs" and then start communication. // if(HWREGBITW(&g_ulCommandFlags, COMMAND_MODE_SET)) { // // Clear the bit now that we have seen it. // HWREGBITW(&g_ulCommandFlags, COMMAND_MODE_SET) = 0; // // Remove the buttons and replace them with the LEDs then // repaint the display. // WidgetRemove((tWidget *)&g_sBtnContainer); WidgetAdd((tWidget *)&g_sBackground, (tWidget *)&g_sLEDContainer); WidgetPaint((tWidget *)&g_sBackground); // // Now call the function that initiates communication in // the desired mode. Note that these functions will not return // until communication is established or an error occurs. // if(g_ulMode == MODE_TALKER) { bSuccess = LinkTo(); } else { bSuccess = LinkFrom(); } // // If we were unsuccessfull, go back to the mode selection // display. // if(!bSuccess) { // // Remove the LEDs and show the buttons again. // WidgetRemove((tWidget *)&g_sLEDContainer); WidgetAdd((tWidget *)&g_sBackground, (tWidget *)&g_sBtnContainer); WidgetPaint((tWidget *)&g_sBackground); // // Tell the user what happened. // UpdateStatus(false, "Error establishing communication!"); UpdateStatus(true, "Please choose the operating mode."); // // Remember that we don't have an operating mode chosen. // g_ulMode = MODE_UNDEFINED; } } // // Have we been asked to toggle the first "LED"? // if(HWREGBITW(&g_ulCommandFlags, COMMAND_LED1_TOGGLE)) { // // Clear the bit now that we have seen it. // HWREGBITW(&g_ulCommandFlags, COMMAND_LED1_TOGGLE) = 0; // // Toggle the LED. // ToggleLED(1); } // // Have we been asked to toggle the second "LED"? // if(HWREGBITW(&g_ulCommandFlags, COMMAND_LED2_TOGGLE)) { // // Clear the bit now that we have seen it. // HWREGBITW(&g_ulCommandFlags, COMMAND_LED2_TOGGLE) = 0; // // Toggle the LED. // ToggleLED(2); } // // Have we been asked to send a packet back to our peer? This // command is only ever sent to the main loop when we are running // in listener mode (LinkListen). // if(HWREGBITW(&g_ulCommandFlags, COMMAND_SEND_REPLY)) { // // Clear the bit now that we have seen it. // HWREGBITW(&g_ulCommandFlags, COMMAND_SEND_REPLY) = 0; // // Create the message. The first byte tells the receiver to // toggle LED1 and the second is a sequence counter. // pucMsg[0] = 1; pucMsg[1] = ++ucTid; eRetcode = SMPL_Send(sLinkID, pucMsg, 2); // // Update our transmit counter if we transmitted the packet // successfully. // if(eRetcode == SMPL_SUCCESS) { g_ulTxCount++; } else { UpdateStatus(false, "TX error %s (%d)", MapSMPLStatus(eRetcode), eRetcode); } } } // // If we are the talker (LinkTo mode), check to see if it's time to // send another packet to our peer. // if((g_ulMode == MODE_TALKER) && (g_ulSysTickCount >= g_ulNextPacketTick)) { // // Create the message. The first byte tells the receiver to // toggle LED1 and the second is a sequence counter. // pucMsg[0] = 1; pucMsg[1] = ++ucTid; eRetcode = SMPL_Send(sLinkID, pucMsg, 2); // // Update our transmit counter if we transmitted the packet // correctly. // if(eRetcode == SMPL_SUCCESS) { g_ulTxCount++; } else { UpdateStatus(false, "TX error %s (%d)", MapSMPLStatus(eRetcode), eRetcode); } // // Set the delay before the next message. // #ifndef USE_2_SECOND_DELAY // // Set the delay before the next message. We increase this from 1 // second to 4 seconds then cycle back to 1. // ucDelay = (ucDelay == 4) ? 1 : (ucDelay + 1); #else // // Wait 2 seconds before sending the next message. // ucDelay = 2; #endif // // Calculate the system tick count when our delay has completed. // This algorithm will generate a spurious packet every 13.7 years // since I don't handle the rollover case in the comparison above // but I'm pretty sure you will forgive me for this oversight. // g_ulNextPacketTick = g_ulSysTickCount + (TICKS_PER_SECOND * ucDelay); } // // If either the transmit or receive packet count changed, update // the status on the display. // if((g_ulRxCount != ulLastRxCount) || (g_ulTxCount != ulLastTxCount)) { ulLastTxCount = g_ulTxCount; ulLastRxCount = g_ulRxCount; UpdateStatus(false, "Received %d pkts, sent %d (%d)", ulLastRxCount, ulLastTxCount); } } }
void main(void) { sensor.cadc = 'A'; sensor.iadc = 125; trigger(0x5 + (0x6 << 3) + (0x1 << 6) + (0x7 << 9)); BSP_Init(); // init bsp first, then simpliciti BCSCTL3 = LFXT1S_2; // aclk = vlo // address check and creation Flash_Addr = (char *)0x10F0; // RF Address = 0x10F0 if( Flash_Addr[0] == 0xFF && // Check if device Address is missing Flash_Addr[1] == 0xFF && Flash_Addr[2] == 0xFF && Flash_Addr[3] == 0xFF ) { createRandomAddress(); // Create Random device address at } // initial startup if missing lAddr.addr[0] = Flash_Addr[0]; lAddr.addr[1] = Flash_Addr[1]; lAddr.addr[2] = Flash_Addr[2]; lAddr.addr[3] = Flash_Addr[3]; // load address SMPL_Ioctl(IOCTL_OBJ_ADDR, IOCTL_ACT_SET, &lAddr); SMPL_Init(NULL); // null callback for TX Init_ADC10(); Init_TIMER0A0(); do { // wait for button if (BSP_BUTTON1()) { break; } } while (1); while (SMPL_SUCCESS != SMPL_Link(&linkIDTemp)) // link to Rx { BSP_TOGGLE_LED1(); // toggle red for not linked } BSP_TURN_OFF_LED1(); // red off BSP_Delay(2000); // for 2 seconds BSP_TURN_ON_LED1(); _EINT(); // Enable Global Interupts while (1) { BSP_TOGGLE_LED2(); // adc with dtc in use ADC10CTL0 &= ~ENC; // turn off adc10 while (ADC10CTL1 & BUSY); // wait if adc10 core is active ADC10SA = (unsigned int)ADCdata; // data buffer start ADC10CTL0 |= ENC + ADC10SC; // sampling and conversion start LPM3; // insert sensor calculations here // or // send raw adc data from P1.0 sensor.iadc = ADCdata[0]; // turn on radio and tx sensor struct SMPL_Ioctl( IOCTL_OBJ_RADIO, IOCTL_ACT_RADIO_AWAKE, 0); SMPL_Send(linkIDTemp, (uint8_t *)&sensor, sizeof( my_sensors )); } }
void main1 (void) { /* holds length of current message */ uint8_t len; /* the link token */ linkID_t LinkID = 0; /* the transmit and receive buffers */ uint8_t rx[MAX_APP_PAYLOAD], tx[MAX_APP_PAYLOAD]; /* holds led indicator time out counts */ uint16_t led_tmr; int8_t rssi_value; BSP_Init( ); SMPL_Init( NULL ); uart_intfc_init( ); /* turn on the radio so we are always able to receive data asynchronously */ SMPL_Ioctl( IOCTL_OBJ_RADIO, IOCTL_ACT_RADIO_RXON, NULL ); /* turn on LED. */ BSP_TURN_ON_LED1( ); #ifdef LINK_TO { uint8_t cnt = 0; tx_send_wait( "Linking to...\r\n", 15 ); while (SMPL_SUCCESS != SMPL_Link(&LinkID)) if( cnt++ == 0 ) { /* blink LED until we link successfully */ BSP_TOGGLE_LED1( ); } } #else // ifdef LINK_LISTEN tx_send_wait( "Listening for Link...\r\n", 23 ); while (SMPL_SUCCESS != SMPL_LinkListen(&LinkID)) { /* blink LED until we link successfully */ BSP_TOGGLE_LED1( ); } #endif tx_send_wait( "Link Established!\r\nReady...\r\n", 29 ); /* turn off the led */ BSP_TURN_OFF_LED1( ); main_loop: /* Check to see if we received any characters from the other radio * and if we have send them out the uart and reset indicator timeout. * Prioritize on radio received links as that buffer is the most time * critical with respect to the UART buffers. */ if( SMPL_Receive( LinkID, tx, &len ) == SMPL_SUCCESS ) { /* blocking call but should be ok if both ends have same uart buad rate */ //tx_send_wait( tx, len ); led_tmr = INDICATOR_TIME_OUT; /* update activity time out */ /* int8_t dbm; SMPL_Ioctl(IOCTL_OBJ_RADIO, IOCTL_ACT_RADIO_RSSI, (void *)&dbm); sSample= dbm; tx_send_wait(&char2send, 1 );//input only (unsigned char) tx_send_wait("\n", 1 );*/ unsigned char Test2; //unsigned char Test; rssi_value = 0 ; ioctlRadioSiginfo_t sigInfo1; sigInfo1.lid=LinkID; smplStatus_t SMPL_SUCCESS = SMPL_Ioctl(IOCTL_OBJ_RADIO, IOCTL_ACT_RADIO_SIGINFO, (void *)&sigInfo1); //if( SMPL_SUCCESS == SMPL_Ioctl( IOCTL_OBJ_RADIO, IOCTL_ACT_RADIO_RSSI,(void*)&rssi_value) ) { //transmitData( i, (signed char)sigInfo.sigInfo.rssi, (char*)msg ); //MRFI_Rssi() //Test2=RSSI; //Test=rssi_value; //int8_t i =-127; //int i =-127; unsigned char rssi_tmp[10]; int8_t rssi_int = sigInfo1.sigInfo.rssi; itoa(rssi_int,rssi_tmp); //tx_send_wait("This is Test :", sizeof("This is Test :") ); //tx_send_wait(&Test2, strlen(&Test2)); tx_send_wait(&rssi_tmp, strlen(rssi_tmp)); //tx_send_wait("\r\n", sizeof("\r\n") ); int8_t mk=0x01,bitshift=0; tx_send_wait("=",1); for (bitshift=0;bitshift<8;bitshift++) { if( (mk<<bitshift) & rssi_int ) tx_send_wait("1",1); else tx_send_wait("0",1); //tx_send_wait("x",1); MRFI_DelayMs( 5 ); } tx_send_wait("\r\n", sizeof("\r\n") ); /* tx_send_wait("This is Test2 :", sizeof("This is Test2 :") ); tx_send_wait( &Test2, sizeof(Test2) ); tx_send_wait("\n", sizeof("\n") ); */ } } FHSS_ACTIVE( if( nwk_pllBackgrounder( false ) != false ) ); { /* check to see if the host has sent any characters and if it has * then send them over the radio link and reset indicator timeout. */ len = rx_receive( rx, MAX_APP_PAYLOAD ); if( len != 0 ) { while( SMPL_Send( LinkID, rx, len ) != SMPL_SUCCESS ) ; /* if( SMPL_SUCCESS == SMPL_Ioctl( IOCTL_OBJ_RADIO, IOCTL_ACT_RADIO_RSSI,&rssi_value ) ){ SMPL_Send( LinkID, &rssi_value, sizeof(rssi_value) ); }*/ led_tmr = INDICATOR_TIME_OUT; /* update activity time out */ /* By forcing a minimum delay between transmissions we guarantee * a window for receiving packets. This is necessary as the radio * link is half duplex while the UART is full duplex. Without this * delay mechanism, packets can get lost as both ends may attempt to * transmit at the same time which the CCA algorithm fails to handle. */ MRFI_DelayMs( 5 ); } } /* manage led indicator */ if( led_tmr != 0 ) { led_tmr--; BSP_TURN_ON_LED1( ); } else BSP_TURN_OFF_LED1( ); goto main_loop; /* do it again and again and again and ... */ }
//***************************************************************************** // // This function attempts to link to another SimpliciTI device by sending a // link request. // //***************************************************************************** tBoolean LinkTo(void) { linkID_t linkID1; uint8_t pucMsg[2], ucWrap; unsigned long ulCount; smplStatus_t eRetcode; // // Our message counter hasn't wrapped. // ucWrap = 0; // // Turn on LED 2 // SetLED(2, true); // // Tell the user what we're doing. // UpdateStatus(false, "Attempting to link..."); // // Try to link for about 10 seconds. // for(ulCount = 0; ulCount < LINK_TIMEOUT_SECONDS; ulCount++) { // // Try to link. // eRetcode = SMPL_Link(&linkID1); // // Did we succeed? // if(eRetcode == SMPL_SUCCESS) { // // Yes - drop out of the loop. // break; } // // We didn't link so toggle the LEDs, wait a bit then try again. // ToggleLED(1); ToggleLED(2); SPIN_ABOUT_A_SECOND; } // // Did the link succeed? // if(eRetcode != SMPL_SUCCESS) { // // No - return an error code. // UpdateStatus(false, "Failed to link!"); return(false); } else { UpdateStatus(false, "Link succeeded."); } // // Turn off the second LED now that we have linked. // SetLED(2, false); #ifdef FREQUENCY_AGILITY // // The radio comes up with Rx off. If Frequency Agility is enabled we need // it to be on so we don't miss a channel change broadcast. We need to // hear this since this application has no ack. The broadcast from the AP // is, therefore, the only way to find out about a channel change. // SMPL_Ioctl( IOCTL_OBJ_RADIO, IOCTL_ACT_RADIO_RXON, 0); #endif // // Set up the initial message and message counter. // pucMsg[0] = 2; pucMsg[1] = ++g_ucTid; ulCount = 0; // // We've linked successfully so drop into an infinite loop during which // we send messages to the receiver every 5 seconds or so. // while (1) { // // Send a message every 5 seconds. This could be emulating a sleep. // #ifndef FREQUENCY_AGILITY // // If Frequency Agility is disabled we don't need to listen for the // broadcast channel change command. // SMPL_Ioctl(IOCTL_OBJ_RADIO, IOCTL_ACT_RADIO_SLEEP, 0); #endif // // Kill about 5 seconds. // SPIN_ABOUT_A_SECOND; SPIN_ABOUT_A_SECOND; SPIN_ABOUT_A_SECOND; SPIN_ABOUT_A_SECOND; SPIN_ABOUT_A_SECOND; #ifndef FREQUENCY_AGILITY // // See comment above...If Frequency Agility is not enabled we never // listen so it is OK if we just awaken leaving Rx off. // SMPL_Ioctl(IOCTL_OBJ_RADIO, IOCTL_ACT_RADIO_AWAKE, 0); #endif // // Send a message. // eRetcode = SMPL_Send(linkID1, pucMsg, sizeof(pucMsg)); if (SMPL_SUCCESS == eRetcode) { // // Toggle LED 1 to indicate that we sent something. // ToggleLED(1); // // Set up the next message. Every 8th message toggle LED 1 instead // of LED 2 on the receiver. // pucMsg[0] = (++ucWrap & 0x7) ? 2 : 1; pucMsg[1] = ++g_ucTid; } // // Tell the user what we did. // UpdateStatus(false, "Sent msg %d (%s).", ++ulCount, MapSMPLStatus(eRetcode)); } }
// AP main routine void simpliciti_main(void) { bspIState_t intState; uint8_t j; uint8_t len; uint32_t led_toggle = 0; uint8_t pwr; // Init variables simpliciti_flag = SIMPLICITI_STATUS_LINKING; // Init SimpliciTI SMPL_Init(sCB); // Set output power to +1.1dBm (868MHz) / +1.3dBm (915MHz) pwr = IOCTL_LEVEL_2; SMPL_Ioctl(IOCTL_OBJ_RADIO, IOCTL_ACT_RADIO_SETPWR, &pwr); // LED off BSP_TURN_OFF_LED1(); /* main work loop */ while (1) { // Wait for the Join semaphore to be set by the receipt of a Join frame from a //device that supports an End Device. if (sJoinSem && !sNumCurrentPeers) { /* listen for a new connection */ while (1) { if (SMPL_SUCCESS == SMPL_LinkListen(&linkID0)) { // We have a connection simpliciti_flag = SIMPLICITI_STATUS_LINKED; BSP_TURN_ON_LED1(); break; } /* Implement fail-to-link policy here. otherwise, listen again. */ } sNumCurrentPeers++; BSP_ENTER_CRITICAL_SECTION(intState); sJoinSem--; BSP_EXIT_CRITICAL_SECTION(intState); } /* Have we received a frame on one of the ED connections? * No critical section -- it doesn't really matter much if we miss a poll */ if (sPeerFrameSem) { // Continuously try to receive end device packets if (SMPL_SUCCESS == SMPL_Receive(linkID0, ed_data, &len)) { // Acceleration / ppt data packets are 4 byte long if (len == 4) { BSP_TOGGLE_LED1(); memcpy(simpliciti_data, ed_data, 4); setFlag(simpliciti_flag, SIMPLICITI_TRIGGER_RECEIVED_DATA); #ifdef EMBEDDED_UART uart_analyze_packet(simpliciti_data[0],simpliciti_data[1],simpliciti_data[2],simpliciti_data[3]); #endif } // Sync packets are either R2R (2 byte) or data (19 byte) long else if ((len == 2) || (len == 19)) { // Indicate received packet BSP_TOGGLE_LED1(); // Decode end device packet switch (ed_data[0]) { case SYNC_ED_TYPE_R2R: // Send reply if (getFlag(simpliciti_flag, SIMPLICITI_TRIGGER_SEND_CMD)) { // Clear flag clearFlag(simpliciti_flag, SIMPLICITI_TRIGGER_SEND_CMD); // Command data was set by USB buffer previously len = BM_SYNC_DATA_LENGTH; } else // No command currently available { simpliciti_data[0] = SYNC_AP_CMD_NOP; simpliciti_data[1] = 0x55; len = 2; } // Send reply packet to end device SMPL_Send(linkID0, simpliciti_data, len); break; case SYNC_ED_TYPE_MEMORY: case SYNC_ED_TYPE_STATUS: // If buffer is empty, copy received end device data to intermediate buffer if (!simpliciti_sync_buffer_status) { for (j=0; j<BM_SYNC_DATA_LENGTH; j++) simpliciti_data[j] = ed_data[j]; simpliciti_sync_buffer_status = 1; } // Set buffer status to full break; } } } } // Exit function if SIMPLICITI_TRIGGER_STOP flag bit is set in USB driver if (getFlag(simpliciti_flag, SIMPLICITI_TRIGGER_STOP)) { // Immediately turn off RF interrupt IEN2 &= ~BIT0; RFIM = 0; RFIF = 0; // Clean up after SimpliciTI and enable restarting the stack linkID0 = 0; sNumCurrentPeers = 0; sJoinSem = 0; sPeerFrameSem = 0; sInit_done = 0; // LED off BSP_TURN_OFF_LED1(); return; } // Blink slowly to indicate that access point is on if (!sNumCurrentPeers) { if (led_toggle++>150000) { BSP_TOGGLE_LED1(); led_toggle = 0; } } } }
static void linkTo() { uint8_t msg[2]; uint8_t button, misses, done; /* Keep trying to link... */ while (SMPL_SUCCESS != SMPL_Link(&sLinkID1)) { toggleLED(1); toggleLED(2); SPIN_ABOUT_A_SECOND; } /* Turn off LEDs. */ if (BSP_LED2_IS_ON()) { toggleLED(2); } if (BSP_LED1_IS_ON()) { toggleLED(1); } /* sleep until button press... */ SMPL_Ioctl( IOCTL_OBJ_RADIO, IOCTL_ACT_RADIO_SLEEP, 0); while (1) { button = 0; /* Send a message when either button pressed */ if (BSP_BUTTON1()) { SPIN_ABOUT_A_QUARTER_SECOND; /* debounce... */ /* Message to toggle LED 1. */ button = 1; } else if (BSP_BUTTON2()) { SPIN_ABOUT_A_QUARTER_SECOND; /* debounce... */ /* Message to toggle LED 2. */ button = 2; } if (button) { /* get radio ready...awakens in idle state */ SMPL_Ioctl( IOCTL_OBJ_RADIO, IOCTL_ACT_RADIO_AWAKE, 0); /* Set TID and designate which LED to toggle */ msg[1] = ++sTid; msg[0] = (button == 1) ? 1 : 2; done = 0; while (!done) { for (misses=0; misses < MISSES_IN_A_ROW; ++misses) { if (SMPL_SUCCESS == SMPL_Send(sLinkID1, msg, sizeof(msg))) { #if defined( FREQUENCY_AGILITY ) /* If macro is defined we're supporting Frequency Agility. In this case * the peer will acknowledge the message sent. It is the only way we can * tell if we are on the right channel. Wait for ack. */ { bspIState_t intState; SMPL_Ioctl( IOCTL_OBJ_RADIO, IOCTL_ACT_RADIO_RXON, 0); NWK_REPLY_DELAY(); SMPL_Ioctl( IOCTL_OBJ_RADIO, IOCTL_ACT_RADIO_RXIDLE, 0); if (!sPeerFrameSem) { /* Try again if we havn't received anything. */ continue; } else { uint8_t len; BSP_ENTER_CRITICAL_SECTION(intState); sPeerFrameSem--; BSP_EXIT_CRITICAL_SECTION(intState); /* We got something. Go get it. */ SMPL_Receive(sLinkID1, msg, &len); if (len && (*msg & NWK_APP_REPLY_BIT)) { toggleLED(*msg & ~NWK_APP_REPLY_BIT); break; } } } #else /* Not supporting Frequency agility. Just break out since there * will be no ack. */ break; #endif } } if (misses == MISSES_IN_A_ROW) { /* This can only happen if we are supporting Frequency Agility and we * appear not to have received an acknowledge. Do a scan. */ ioctlScanChan_t scan; freqEntry_t freq[NWK_FREQ_TBL_SIZE]; scan.freq = freq; SMPL_Ioctl(IOCTL_OBJ_FREQ, IOCTL_ACT_SCAN, &scan); /* If we now know the channel (number == 1) change to it. In any case * try it all again. If we changed channels we should get an ack now. */ if (1 == scan.numChan) { SMPL_Ioctl(IOCTL_OBJ_FREQ, IOCTL_ACT_SET, freq); } } else { /* Got the ack. We're done. */ done = 1; } } /* radio back to sleep */ SMPL_Ioctl( IOCTL_OBJ_RADIO, IOCTL_ACT_RADIO_SLEEP, 0); } } }