/*! * \brief Reset the given mask of status bits * \param flags Flags, one or more of RF24_STATUS_RX_DR, RF24_STATUS_TX_DS, RF24_STATUS_MAX_RT */ void RF_ResetStatusIRQ(uint8_t flags) { RF_WAIT_US(10); RF_CSN_LOW(); RF_WAIT_US(10); RF_WriteRegister(RF24_STATUS, flags); /* reset all IRQ in status register */ RF_WAIT_US(10); RF_CSN_HIGH(); RF_WAIT_US(10); }
/*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!!! ***/