/****************************************************************************** * @fn nwk_radioControl * * @brief Handle radio control functions. * * input parameters * @param action - radio operation to perform. currently suppoerted: * sleep/unsleep * output parameters * * @return Status of operation. */ smplStatus_t nwk_radioControl(ioctlAction_t action, void *val) { smplStatus_t rc = SMPL_SUCCESS; if (IOCTL_ACT_RADIO_SLEEP == action) { /* go to sleep mode. */ MRFI_RxIdle(); MRFI_Sleep(); } else if (IOCTL_ACT_RADIO_AWAKE == action) { MRFI_WakeUp(); #if !defined( END_DEVICE ) MRFI_RxOn(); #endif } else if (IOCTL_ACT_RADIO_SIGINFO == action) { ioctlRadioSiginfo_t *pSigInfo = (ioctlRadioSiginfo_t *)val; connInfo_t *pCInfo = nwk_getConnInfo(pSigInfo->lid); if (!pCInfo) { return SMPL_BAD_PARAM; } memcpy(&pSigInfo->sigInfo, &pCInfo->sigInfo, sizeof(pCInfo->sigInfo)); } else if (IOCTL_ACT_RADIO_RSSI == action) { *((rssi_t *)val) = MRFI_Rssi(); } else if (IOCTL_ACT_RADIO_RXON == action) { MRFI_RxOn(); } else if (IOCTL_ACT_RADIO_RXIDLE == action) { MRFI_RxIdle(); } else { rc = SMPL_BAD_PARAM; } return rc; }
interrupt(PORT1_VECTOR) Buttopn(void) { P1IFG &= ~0x04; P1OUT ^= 0x03; //after press the button , we can send and recieve message BSP_Init(); MRFI_Init(); MRFI_SetLogicalChannel(1); MRFI_SetRFPwr(0); Uart_Init(); MRFI_WakeUp(); MRFI_RxOn(); Scan_Init(&etat); //open timer B for scan Start_Timer_Surveille(); //open timer for surveille print("\n\r"); print("command: \n\r"); print("o : who is on line \n\r"); print("v : voisin \n\r"); print("r : router table \n\r"); print("i : sysinfo \n\r"); print("ESC: help \n\r"); }
int main(void) { BSP_Init(); P2DIR |= 0x04; MRFI_Init(); //mrfiSpiWriteReg(PATABLE,0x50);// -30dBm Tx power //mrfiSpiWriteReg(PATABLE,0x84);// -24dBm Tx power //mrfiSpiWriteReg(PATABLE,0x46);// -20dBm Tx power //mrfiSpiWriteReg(PATABLE,0x55);// -16dBm Tx power //mrfiSpiWriteReg(PATABLE,0x8D);// -14dBm Tx power //mrfiSpiWriteReg(PATABLE,0xC6);// -12dBm Tx power //mrfiSpiWriteReg(PATABLE,0x97);// -10dBm Tx power //mrfiSpiWriteReg(PATABLE,0x6E);// -8 dBm Tx power //mrfiSpiWriteReg(PATABLE,0x7F);// -6 dBm Tx power //mrfiSpiWriteReg(PATABLE,0xA9);// -4 dBm Tx power //mrfiSpiWriteReg(PATABLE,0xBB);// -2 dBm Tx power mrfiSpiWriteReg(PATABLE,0xFE);// 0dBm Tx power mrfiSpiWriteReg(MDMCFG1,0x23); mrfiSpiWriteReg(MDMCFG0,0xF8);// 400kHz channel spacing mrfiSpiWriteReg(FREQ2,0x5C); mrfiSpiWriteReg(FREQ1,0x80); mrfiSpiWriteReg(FREQ0,0x00); // 2.405GHz base frequency mrfiSpiWriteReg(CHANNR,0xBC); // channel 26 MRFI_WakeUp(); MRFI_RxOn(); __bis_SR_register(GIE+LPM3_bits); }
__interrupt void interrupt_slow_timeout (void) { MRFI_WakeUp(); MRFI_RxOn(); start_fast_timeout(); __bic_SR_register_on_exit(SCG1+SCG0); }
/*********************************************************************************** * @fn SMPL_Init * * @brief Initialize the SimpliciTI stack. * * input parameters * @param f - Pointer to call back function. Function called by NWK when * user application frame received. The callback is done in the * ISR thread. Argument is Link ID associated with frame. Function * returns 0 if frame is to be kept by NWK, otherwise 1. Frame * should be kept if application will do a SMPL_Receive() in the * user thread (recommended). Pointer may be NULL. * * output parameters * * @return Status of operation: * SMPL_SUCCESS * SMPL_NO_JOIN No Join reply. AP possibly not yet up. * SMPL_NO_CHANNEL Only if Frequency Agility enabled. Channel scan * failed. AP possibly not yet up. */ smplStatus_t SMPL_Init(uint8_t (*f)(linkID_t)) { smplStatus_t rc; if (!sInit_done) { /* set up radio. */ MRFI_Init(); /* initialize network */ if ((rc=nwk_nwkInit(f)) != SMPL_SUCCESS) { return rc; } MRFI_WakeUp(); #if defined( FREQUENCY_AGILITY ) { freqEntry_t chan; chan.logicalChan = 0; /* ok to set default channel explicitly now that MRFI initialized. */ nwk_setChannel(&chan); } #endif /* don't turn Rx on if we're an end device that isn't always on. */ /* but do turn the radio on for PLL operations */ #if !defined( END_DEVICE ) || defined( NWK_PLL ) MRFI_RxOn(); #endif #if defined( END_DEVICE ) /* All except End Devices are in promiscuous mode */ MRFI_SetRxAddrFilter((uint8_t *)nwk_getMyAddress()); MRFI_EnableRxAddrFilter(); #endif } sInit_done = 1; #ifdef NWK_PLL /* If the PLL is enabled then it must get running before the join * request or the system may lock up in the join request becuase * PLL is not locked in. */ // turn on the PLL SMPL_Ioctl(IOCTL_OBJ_PLL, IOCTL_ACT_ON, NULL); // reference clocks are by definition always locked. #ifndef NWK_PLL_REFERENCE_CLOCK // wait for a 5ms failure rate to be achieved while( nwk_pllIsLocked( 0 ) == false ) nwk_pllBackgrounder( false ); #endif #endif /* Join. if no AP or Join fails that status is returned. */ rc = nwk_join(); return rc; }
/** * swStart * * Start (or re-start) SWAP comms */ void swStart(void) { MRFI_WakeUp(); // Enable reception MRFI_RxOn(); // New SWAP state = running setChronosState(SYSTATE_RUNNING); }
int main(void) { WDTCTL = WDTPW + WDTHOLD; // stop watchdog timer BSP_Init(); MRFI_Init(); MCU_Init(30000); //30000 is dummy large number MRFI_WakeUp(); //start radio in idle state MRFI_RxOn(); //put radio in Rx __bis_SR_register(LPM3_bits + GIE); }
void ReadChannelsAndSendRSSI() { uint8_t channel; int8_t rssi; for (channel=0;channel<255;channel++) { MRFI_RxIdle(); mrfiSpiWriteReg(CHANNR,channel); MRFI_RxOn(); rssi=MRFI_Rssi(); print_rssi(rssi); } }
void MRFI_RxCompleteISR() { mrfiPacket_t packet; stop_fast_timeout(); stop_slow_timeout(); MRFI_Receive(&packet); if (packet.frame[9]<4) { print_counter(packet.frame[9]); start_slow_timeout(); } else { MRFI_WakeUp(); MRFI_RxOn(); } }
/*********************************************************************************** * @fn SMPL_Init * * @brief Initialize the SimpliciTI stack. * * input parameters * @param f - Pointer to call back function. Function called by NWK when * user application frame received. The callback is done in the * ISR thread. Argument is Link ID associated with frame. Function * returns 0 if frame is to be kept by NWK, otherwise 1. Frame * should be kept if application will do a SMPL_Receive() in the * user thread (recommended). Pointer may be NULL. * * output parameters * * @return Status of operation: * SMPL_SUCCESS * SMPL_NO_JOIN No Join reply. AP possibly not yet up. * SMPL_NO_CHANNEL Only if Frequency Agility enabled. Channel scan * failed. AP possibly not yet up. */ smplStatus_t SMPL_Init(uint8_t (*f)(linkID_t)) { smplStatus_t rc; if (!sInit_done) { /* set up radio. */ MRFI_Init(); /* initialize network */ if ((rc=nwk_nwkInit(f)) != SMPL_SUCCESS) { return rc; } MRFI_WakeUp(); #if defined( FREQUENCY_AGILITY ) { freqEntry_t chan; chan.logicalChan = 0; /* ok to set default channel explicitly now that MRFI initialized. */ nwk_setChannel(&chan); } #endif /* don't turn Rx on if we're an end device that isn't always on. */ #if !defined( END_DEVICE ) MRFI_RxOn(); #endif #if defined( END_DEVICE ) /* All except End Devices are in promiscuous mode */ MRFI_SetRxAddrFilter((uint8_t *)nwk_getMyAddress()); MRFI_EnableRxAddrFilter(); #endif } sInit_done = 1; /* Join. if no AP or Join fails that status is returned. */ rc = nwk_join(); return rc; }
__interrupt void Timer_A (void) { P2OUT |= 0x04; uint16_t i; /* stop timer */ TACTL=MC_0; TACCTL0=0; /* send probe packet */ for (i=0;i<1000;i++) { P1OUT |= 0x01; packet.frame[0]=8+20; MRFI_Transmit(&packet, MRFI_TX_TYPE_FORCED); P1OUT &= ~0x01; } /* return to Rx mode on channel 26 */ MRFI_RxIdle(); mrfiSpiWriteReg(CHANNR,0xBC); // channel 26 MRFI_RxOn(); P2OUT &= ~0x04; }
/****************************************************************************** * @fn mrfiLinkInit * * @brief Initialise the MRFI layer. Selects RF channel and addresses. * * @param src - source address (16 bit) * * @param dst - destination address (16 bit) * * @return void * */ void mrfiLinkInit(uint16 src, uint16 dst, uint8 mrfiChannel) { #ifdef USB_SUSPEND_HOOKS // Register USB hooks if necessary pFnSuspendEnterHook= MRFI_Sleep; pFnSuspendExitHook= linkRestore; #endif // Make sure the timer is initialised BSP_INIT_BOARD(); // Initialise the addresses src_addr[0]= LO_UINT16(MRFI_LINK_PAN_ID); src_addr[1]= HI_UINT16(MRFI_LINK_PAN_ID); src_addr[2]= LO_UINT16(src); src_addr[3]= HI_UINT16(src); dest_addr[0]= LO_UINT16(MRFI_LINK_PAN_ID); dest_addr[1]= HI_UINT16(MRFI_LINK_PAN_ID); dest_addr[2]= LO_UINT16(dst); dest_addr[3]= HI_UINT16(dst); // Initialise MRFI link housekeeping data mrfiPktRdy= FALSE; fAckRdy= FALSE; seqSend= 0; seqRecv= 0; // Initialise MRFI MRFI_Init(); MRFI_WakeUp(); MRFI_SetLogicalChannel(mrfiChannel); MRFI_RxOn(); MRFI_SetRxAddrFilter(src_addr); MRFI_EnableRxAddrFilter(); }
smplStatus_t nwk_radioControl(ioctlAction_t action, void *val) { smplStatus_t rc = SMPL_SUCCESS; if (IOCTL_ACT_RADIO_SLEEP == action) { /* go to sleep mode. */ MRFI_RxIdle(); MRFI_Sleep(); } else if (IOCTL_ACT_RADIO_AWAKE == action) { MRFI_WakeUp(); #if !defined(END_DEVICE) MRFI_RxOn(); #endif } else if (IOCTL_ACT_RADIO_SIGINFO == action) { ioctlRadioSiginfo_t *pSigInfo = (ioctlRadioSiginfo_t *)val; connInfo_t *pCInfo = nwk_getConnInfo(pSigInfo->lid); if (!pCInfo) { return SMPL_BAD_PARAM; } memcpy(&pSigInfo->sigInfo, &pCInfo->sigInfo, sizeof(pCInfo->sigInfo)); } else if (IOCTL_ACT_RADIO_RSSI == action) { *((rssi_t *)val) = MRFI_Rssi(); } else if (IOCTL_ACT_RADIO_RXON == action) { MRFI_RxOn(); } else if (IOCTL_ACT_RADIO_RXIDLE == action) { MRFI_RxIdle(); } #ifdef EXTENDED_API else if (IOCTL_ACT_RADIO_SETPWR == action) { uint8_t idx; switch (*(ioctlLevel_t *)val) { case IOCTL_LEVEL_2: idx = 2; break; case IOCTL_LEVEL_1: idx = 1; break; case IOCTL_LEVEL_0: idx = 0; break; default: return SMPL_BAD_PARAM; } MRFI_SetRFPwr(idx); return SMPL_SUCCESS; } #endif /* EXTENDED_API */ else { rc = SMPL_BAD_PARAM; } return rc; }
/*------------------------------------------------------------------------------ * Main *----------------------------------------------------------------------------*/ void main ( void ) { uint8_t tx_cmd; uint8_t tx_data; /* Initialize board devices */ BSP_Init(); MRFI_Init(); /* Setup I/O */ P1DIR |= (LED_RED+LED_GREEN); // Enable LEDs P1DIR &= ~PUSH_BUTTON; // Enable push button P1REN |= PUSH_BUTTON; // Enable pull-up/down resistor P1IE |= PUSH_BUTTON; // Enable interrupt P2DIR &= ~(TRIGGER_L2H+TRIGGER_H2L+MODE_SELECT); // Enable inputs P2IE |= (TRIGGER_L2H+TRIGGER_H2L); // Enable interrupts P2IES &= ~TRIGGER_L2H; // Set rising edge select P2IES |= TRIGGER_H2L; // Set falling edge select /* Setup Timer A */ BCSCTL3 |= LFXT1S_2; // Source VLO @ 12kHz TACCTL0 = CCIE; // Enable TimerA interrupt TACCR0 = 12000; // ~1Hz TACTL = MC_1+TASSEL_1; // Count up + ACLK /* Initialize device settings */ NODE1 |= LINK_MODE; /* Signal boot complete */ P1OUT |= (LED_RED+LED_GREEN); /* Enter main loop */ while(1) { __bis_SR_register(GIE+LPM3_bits); if (NODE1&MEASURE_VCC) { volatile long temp; P1OUT |= LED_GREEN; ADC10CTL1 = INCH_11; ADC10CTL0 = SREF_1 + ADC10SHT_2 + REFON + ADC10ON + ADC10IE + REF2_5V; __delay_cycles(240); ADC10CTL0 |= ENC + ADC10SC; __bis_SR_register(CPUOFF+GIE); temp = ADC10MEM; tx_cmd = NODE_ALIVE; tx_data = (temp*25)/512; ADC10CTL0 &= ~ENC; ADC10CTL0 &= ~(REFON + ADC10ON); NODE1 &= ~MEASURE_VCC; NODE1 |= (WAKE_RADIO+BROADCAST); TACCTL0 |= CCIE; P1OUT &= ~LED_GREEN; } if (NODE1&LINK_MODE) { P1OUT ^= (LED_RED+LED_GREEN); tx_cmd = NEW_NODE; NODE1 |= (WAKE_RADIO+BROADCAST); } else { if (NODE1&STATE_CHANGED) { if (NODE1&ALARMED) { P1OUT |= LED_RED; tx_cmd = ALARMED_NODE; } else { P1OUT &= ~LED_RED; tx_cmd = RESET_NODE; } NODE1 |= (WAKE_RADIO+BROADCAST); } else { if (NODE1&ALARMED) { P1OUT ^= LED_RED; } else { P1OUT &= ~LED_RED; } } } if (NODE1&WAKE_RADIO) { MRFI_WakeUp(); MRFI_RxOn(); } if (NODE1&BROADCAST) { mrfiPacket_t tx_packet; tx_packet.frame[0] = 8+20; tx_packet.frame[SRC_ADDR] = my_addr; tx_packet.frame[DST_ADDR] = 0x00; tx_packet.frame[CMD] = tx_cmd; tx_packet.frame[DATA] = tx_data; MRFI_Transmit(&tx_packet, MRFI_TX_TYPE_FORCED); NODE1 &= ~BROADCAST; } if (!(NODE1&WAKE_RADIO)) { MRFI_Sleep(); } } }
/****************************************************************************** * @fn linkRestore * * @brief Restore the link after exiting from LPM. Typically used as a * USB resume hook. * * @param none * * @return none */ static void linkRestore(void) { MRFI_WakeUp(); MRFI_RxOn(); }
//============================================= void gradient_wakeup_timer() { uint8_t next_hop_addr; mrfiPacket_t packetToSend; switch (gradient_state) { //--------------------------------------- transmitter case (GRADIENT_IDLE): //wake_up timer /* stop preamble sampling */ wor_stop(IS_SINK_NODE); /* stop possible timers */ stop_timer(); /* check that there is data to send */ dataToSend = QgetInUseSlot(OUTQ); if (!dataToSend) { gradient_end_txrx(); print_debug("\r\nno data to send",17); return; } /* TODO. check that the medium is free */ set_timer(UF_PERIOD); //wake_up timer /* send first uF */ gradient_set_state(GRADIENT_TXUF); uf_counter = NUMBER_UF-1; gradient_build_UF(&packetToSend, uf_counter); MRFI_justTransmit(&packetToSend); break; case (GRADIENT_TXUF): //wake_up timer if (uf_counter > 0) { uf_counter--; /* send uF */ gradient_build_UF(&packetToSend, uf_counter); MRFI_justTransmit(&packetToSend); } else { stop_timer(); /* send CW */ gradient_set_state(GRADIENT_TXCW); gradient_build_CW(&packetToSend, CW_LENGTH); MRFI_justTransmit(&packetToSend); /* wait for ACK */ gradient_set_state(GRADIENT_RXACK); gradient_init_neighbor_table(); MRFI_RxOn(); set_timer(CW_LENGTH); } break; case (GRADIENT_RXACK): //wake_up timer stop_timer(); print_neighbor_table(neighbors); gradient_update_height(); print_height(myAddr,myHeight); next_hop_addr = gradient_next_hop(); if (next_hop_addr==0) { gradient_end_txrx(); print_fail("\r\nTX failed: no neighbor",24); } else { /* send DATA */ gradient_set_state(GRADIENT_TXDATA); //if mine, complete with current neighbor list and empty hop count if ( (&(dataToSend->mrfiPkt))->frame[F_SRC]==myAddr) { gradient_build_DATA_headers(dataToSend); } (&(dataToSend->mrfiPkt))->frame[F_DATA_NEXT_HOP]=next_hop_addr; MRFI_justTransmit(&(dataToSend->mrfiPkt)); /* wait for FIN */ gradient_set_state(GRADIENT_RXFIN); MRFI_RxOn(); set_timer(TIMEOUT_FIN); } break; case (GRADIENT_RXFIN): //wake_up timer gradient_end_txrx(); print_fail("\r\nTX failed: no FIN",19); break; //--------------------------------------- receiver case (GRADIENT_RXUF): //wake_up timer stop_timer(); /* wait for CW */ gradient_set_state(GRADIENT_RXCW); MRFI_RxOn(); set_timer(TIMEOUT_CW); break; case (GRADIENT_RXCW): //wake_up timer gradient_end_txrx(); print_fail("\r\nRX failed: no CW",18); break; case (GRADIENT_BACKOFFACK): //wake_up timer stop_second_random_timer(); /* send ACK */ gradient_set_state(GRADIENT_TXACK); gradient_build_ACK(&packetToSend, addr_initiator); MRFI_justTransmit(&packetToSend); gradient_set_state(GRADIENT_SENTACK); MRFI_RxIdle(); break; case (GRADIENT_SENTACK): //wake_up timer stop_timer(); /* wait for DATA */ gradient_set_state(GRADIENT_RXDATA); MRFI_RxOn(); set_timer(TIMEOUT_DATA); break; case (GRADIENT_RXDATA): //wake_up timer gradient_end_txrx(); print_fail("\r\nRX failed: no DATA",20); break; case (GRADIENT_TXFIN): //wake_up timer stop_timer(); /* send FIN */ gradient_build_FIN(&packetToSend, addr_initiator); MRFI_justTransmit(&packetToSend); print_success("\r\nRX successful",15); print_DATA(&lastData, myAddr); gradient_end_txrx(); break; } }