static void RADIO_HandleStateMachine(void) { uint8_t status; for(;;) { /* will break/return */ switch (RADIO_AppStatus) { case RADIO_INITIAL_STATE: RF1_StopRxTx(); /* will send/receive data later */ RADIO_AppStatus = RADIO_RECEIVER_ALWAYS_ON; /* turn receive on */ break; /* process switch again */ case RADIO_RECEIVER_ALWAYS_ON: /* turn receive on */ RX_POWERUP(); RF1_StartRxTx(); /* Listening for packets */ RADIO_AppStatus = RADIO_READY_FOR_TX_RX_DATA; break; /* process switch again */ case RADIO_READY_FOR_TX_RX_DATA: /* we are ready to receive/send data data */ if (RADIO_isrFlag) { /* Rx interrupt? */ RADIO_isrFlag = FALSE; /* reset interrupt flag */ (void)CheckRx(); /* get message */ RADIO_AppStatus = RADIO_RECEIVER_ALWAYS_ON; /* continue listening */ break; /* process switch again */ } if (CheckTx()==ERR_OK) { /* there was data and it has been sent */ RADIO_AppStatus = RADIO_WAITING_DATA_SENT; break; /* process switch again */ } return; case RADIO_WAITING_DATA_SENT: if (RADIO_isrFlag) { /* check if we have received an interrupt: this is either timeout or low level ack */ RADIO_isrFlag = FALSE; /* reset interrupt flag */ status = RF1_GetStatus(); if (status&RF1_STATUS_RX_DR) { /* data received interrupt */ RF1_ResetStatusIRQ(RF1_STATUS_RX_DR); /* clear bit */ } if (status&RF1_STATUS_TX_DS) { /* data sent interrupt */ RF1_ResetStatusIRQ(RF1_STATUS_TX_DS); /* clear bit */ } if (status&RF1_STATUS_MAX_RT) { /* retry timeout interrupt */ RF1_ResetStatusIRQ(RF1_STATUS_MAX_RT); /* clear bit */ RF1_Write(RF1_FLUSH_TX); /* flush old data */ RADIO_AppStatus = RADIO_TIMEOUT; /* timeout */ } else { RADIO_AppStatus = RADIO_RECEIVER_ALWAYS_ON; /* turn receive on */ } break; /* process switch again */ } return; case RADIO_TIMEOUT: Err((unsigned char*)"ERR: Timeout\r\n"); RADIO_AppStatus = RADIO_RECEIVER_ALWAYS_ON; /* turn receive on */ break; /* process switch again */ default: /* should not happen! */ return; } /* switch */ } /* for */ }
/*! * \brief Radio power-on initialization. * \return Error code, ERR_OK if everything is ok. */ uint8_t RADIO_PowerUp(void) { WAIT1_WaitOSms(100); /* the transceiver needs 100 ms power up time */ RF1_Init(); /* set CE and CSN to initialization value */ RF1_WriteRegister(RF1_RF_SETUP, RF1_RF_SETUP_RF_PWR_0|RF1_RF_SETUP_RF_DR_250); #if NRF24_DYNAMIC_PAYLOAD /* enable dynamic payload */ RF1_WriteFeature(RF1_FEATURE_EN_DPL|RF1_FEATURE_EN_ACK_PAY|RF1_FEATURE_EN_DYN_PAY); /* set EN_DPL for dynamic payload */ RF1_EnableDynanicPayloadLength(RF1_DYNPD_DPL_P0); /* set DYNPD register for dynamic payload for pipe0 */ #else RF1_SetStaticPipePayload(0, RPHY_PAYLOAD_SIZE); /* static number of payload bytes we want to send and receive */ #endif (void)RADIO_SetChannel(RADIO_CHANNEL_DEFAULT); /* Set RADDR and TADDR as the transmit address since we also enable auto acknowledgment */ RF1_WriteRegisterData(RF1_RX_ADDR_P0, (uint8_t*)TADDR, sizeof(TADDR)); RF1_WriteRegisterData(RF1_TX_ADDR, (uint8_t*)TADDR, sizeof(TADDR)); /* Enable RX_ADDR_P0 address matching */ RF1_WriteRegister(RF1_EN_RXADDR, RF1_EN_RXADDR_ERX_P0); /* enable data pipe 0 */ /* clear interrupt flags */ RF1_ResetStatusIRQ(RF1_STATUS_RX_DR|RF1_STATUS_TX_DS|RF1_STATUS_MAX_RT); /* rx/tx mode */ RF1_EnableAutoAck(RF1_EN_AA_ENAA_P0); /* enable auto acknowledge on pipe 0. RX_ADDR_P0 needs to be equal to TX_ADDR! */ RF1_WriteRegister(RF1_SETUP_RETR, RF1_SETUP_RETR_ARD_750|RF1_SETUP_RETR_ARC_15); /* Important: need 750 us delay between every retry */ RX_POWERUP(); /* Power up in receiving mode */ RF1_StartRxTx(); /* Listening for packets */ RADIO_AppStatus = RADIO_INITIAL_STATE; /* init Rx descriptor */ radioRx.phyData = &radioRxBuf[0]; radioRx.phySize = sizeof(radioRxBuf); radioRx.rxtx = &RPHY_BUF_SIZE(radioRx.phyData); /* we transmit the size too */ return ERR_OK; }
/*lint -save -e970 Disable MISRA rule (6.3) checking. */ int main(void) /*lint -restore Enable MISRA rule (6.3) checking. */ { /* Write your local variable definition here */ /*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/ PE_low_level_init(); /*** End of Processor Expert internal initialization. ***/ for (;;) { #if IS_SENDER int i; #endif int cntr; uint8_t status; WAIT1_Waitms(100); /* give device time to power up */ RF_Init(); /* set CE and CSN to initialization value */ RF_WriteRegister(RF24_RF_SETUP, RF24_RF_SETUP_RF_PWR_0 | RF24_RF_SETUP_RF_DR_250); RF_WriteRegister(RF24_RX_PW_P0, PAYLOAD_SIZE); /* number of payload bytes we want to send and receive */ RF_WriteRegister(RF24_RF_CH, CHANNEL_NO); /* set channel */ /* Set RADDR and TADDR as the transmit address since we also enable auto acknowledgment */ RF_WriteRegisterData(RF24_RX_ADDR_P0, (uint8_t*) TADDR, sizeof(TADDR)); RF_WriteRegisterData(RF24_TX_ADDR, (uint8_t*) TADDR, sizeof(TADDR)); /* Enable RX_ADDR_P0 address matching */ RF_WriteRegister(RF24_EN_RXADDR, RF24_EN_RXADDR_ERX_P0); /* enable data pipe 0 */ /* clear interrupt flags */ RF_ResetStatusIRQ( RF24_STATUS_RX_DR | RF24_STATUS_TX_DS | RF24_STATUS_MAX_RT); #if IS_SENDER RF_WriteRegister(RF24_EN_AA, RF24_EN_AA_ENAA_P0); /* enable auto acknowledge. RX_ADDR_P0 needs to be equal to TX_ADDR! */ RF_WriteRegister(RF24_SETUP_RETR, RF24_SETUP_RETR_ARD_750|RF24_SETUP_RETR_ARC_15); /* Important: need 750 us delay between every retry */ TX_POWERUP(); /* Power up in transmitting mode */ CE_ClrVal(); /* Will pulse this later to send data */ #else RX_POWERUP(); /* Power up in receiving mode */ CE_SetVal(); /* Listening for packets */ #endif cntr = 0; for (;;) { #if IS_SENDER cntr++; if (cntr==200) { /* send data every 200 ms */ cntr = 0; for(i=0;i<PAYLOAD_SIZE;i++) { payload[i] = i+1; /* just fill payload with some data */ } LEDR_ClrVal(); RF_TxPayload(payload, sizeof(payload)); /* send data */ } if (cntr>50) { /* not able to send data for 50 ms? */ LEDR_SetVal(); } if (isrFlag) { /* check if we have received an interrupt */ isrFlag = FALSE; /* reset interrupt flag */ status = RF_GetStatus(); if (status&RF24_STATUS_RX_DR) { /* data received interrupt */ RF_ResetStatusIRQ(RF24_STATUS_RX_DR); /* clear bit */ } if (status&RF24_STATUS_TX_DS) { /* data sent interrupt */ cntr = 0; /* reset timeout counter */ LEDR_SetVal(); /* indicate data has been sent */ RF_ResetStatusIRQ(RF24_STATUS_TX_DS); /* clear bit */ } if (status&RF24_STATUS_MAX_RT) { /* retry timeout interrupt */ RF_ResetStatusIRQ(RF24_STATUS_MAX_RT); /* clear bit */ } } WAIT1_Waitms(1); #else if (isrFlag) { /* interrupt? */ isrFlag = FALSE; /* reset interrupt flag */ cntr = 0; /* reset counter */ LEDB_SetVal(); LEDG_NegVal(); /* blink green LED to indicate good communication */ status = RF_GetStatus(); if (status & RF24_STATUS_RX_DR) { /* data received interrupt */ RF_RxPayload(payload, sizeof(payload)); /* will reset RX_DR bit */ RF_ResetStatusIRQ( RF24_STATUS_RX_DR | RF24_STATUS_TX_DS | RF24_STATUS_MAX_RT); /* make sure we reset all flags. Need to have the pipe number too */ } if (status & RF24_STATUS_TX_DS) { /* data sent interrupt */ RF_ResetStatusIRQ(RF24_STATUS_TX_DS); /* clear bit */ } if (status & RF24_STATUS_MAX_RT) { /* retry timeout interrupt */ RF_ResetStatusIRQ(RF24_STATUS_MAX_RT); /* clear bit */ } } else { cntr++; if (cntr > 500) { /* blink every 500 ms if not receiving data */ cntr = 0; /* reset counter */ LEDG_SetVal(); LEDB_NegVal(); /* blink blue to indicate no communication */ } WAIT1_Waitms(1); /* burning some cycles here */ } #endif } } /*** Don't write any code pass this line, or it will be deleted during code generation. ***/ /*** RTOS startup code. Macro PEX_RTOS_START is defined by the RTOS component. DON'T MODIFY THIS CODE!!! ***/ #ifdef PEX_RTOS_START PEX_RTOS_START(); /* Startup of the selected RTOS. Macro is defined by the RTOS component. */ #endif /*** End of RTOS startup code. ***/ /*** Processor Expert end of main routine. DON'T MODIFY THIS CODE!!! ***/ for(;;){} /*** Processor Expert end of main routine. DON'T WRITE CODE BELOW!!! ***/ } /*** End of main routine. DO NOT MODIFY THIS TEXT!!! ***/