/* Send data using the nRF radio */ void radioTask(void *pvParameters) { (void) pvParameters; uint8_t rfData[NRF24_PAYLOAD_LEN]; uint8_t rcvData[NRF24_PAYLOAD_LEN]; uint8_t pktCounter = 0; uint8_t blinkCounter = 0; const portTickType blinkDelay = 250 / portTICK_RATE_MS; const portTickType sendDelay = 5000 / portTICK_RATE_MS; rfData[0] = 0xB1; rfData[1] = 0x3A; rfData[2] = 0x23; while(1) { if(nrf24_dataReady()) { nrf24_getData(rcvData); // Format message for debugging char debugRcvString[3*6] = {0}; for (uint8_t i=0; i < NRF24_PAYLOAD_LEN; i++) { sprintf(debugRcvString+(3*i), "%02X ", rcvData[i]); } printf("Received: %s\n", debugRcvString); } rfData[3] = pktCounter++; nrf24_send(rfData); while(nrf24_isSending()); char debugSendString[3*6] = {0}; for (uint8_t i = 0; i < NRF24_PAYLOAD_LEN; i++) { sprintf(debugSendString+(3*i), "%02X ", rfData[i]); } printf("Sent: %s\n", debugSendString); if(nrf24_lastMessageStatus()) { //there was a send problem }else{ //transmission was ok } for(blinkCounter = 1 + nrf24_retransmissionCount(); blinkCounter > 0; --blinkCounter) { PORTC |= _BV(PORTC3); vTaskDelay(blinkDelay); PORTC &= ~_BV(PORTC3); vTaskDelay(blinkDelay); } nrf24_powerUpRx(); vTaskDelay(sendDelay); } }
/* ------------------------------------------------------------------------- */ int main() { /* init the software uart */ uart_init(); /* init the xprintf library */ xdev_out(uart_put_char); /* simple greeting message */ xprintf("\r\n> TX device ready\r\n"); /* init hardware pins */ nrf24_init(); /* Channel #2 , payload length: 4 */ nrf24_config(2,4); /* Set the device addresses */ nrf24_tx_address(tx_address); nrf24_rx_address(rx_address); while(1) { /* Fill the data buffer */ data_array[0] = 0x00; data_array[1] = 0xAA; data_array[2] = 0x55; data_array[3] = q++; /* Automatically goes to TX mode */ nrf24_send(data_array); /* Wait for transmission to end */ while(nrf24_isSending()); /* Make analysis on last tranmission attempt */ temp = nrf24_lastMessageStatus(); if(temp == NRF24_TRANSMISSON_OK) { xprintf("> Tranmission went OK\r\n"); } else if(temp == NRF24_MESSAGE_LOST) { xprintf("> Message is lost ...\r\n"); } /* Retranmission count indicates the tranmission quality */ temp = nrf24_retransmissionCount(); xprintf("> Retranmission count: %d\r\n",temp); /* Optionally, go back to RX mode ... */ nrf24_powerUpRx(); /* Or you might want to power down after TX */ // nrf24_powerDown(); /* Wait a little ... */ _delay_ms(10); } }
/** * @brief Main program. * @param None * @retval None */ void main(void) { static u8 status; bool flag_display_retrans_cnt; disableInterrupts(); Config(); Errors_Init(); enableInterrupts(); Goto_HALT(); while (1) { if((btn_pressed != BUTTON1) && (btn_pressed != BUTTON2)) { DELAY_STOP; goto sleep; } PWR_PVDCmd(ENABLE); /* Power voltage detector and brownout Reset unit supply current 2,6uA */ PWR_PVDLevelConfig(PWR_PVDLevel_2V26); /* Set Programmable voltage detector threshold to 2,26V */ if(DELAY_EXPIRED) flag_display_retrans_cnt = TRUE; else flag_display_retrans_cnt = FALSE; while(!DELAY_EXPIRED); /* wait for power-up delay to expire (199.68ms) - needed for nRF24L01+ power up */ DELAY_STOP; CLK_PeripheralClockConfig(CLK_Peripheral_SPI1, ENABLE); nrf24_init(); /* init hardware pins */ nrf24_config(0, 6); /* Channel #0 , payload length: 6 */ nrf24_tx_address(tx_address); /* Set the device addresses */ nrf24_rx_address(rx_address); /* Check NRF24 Init error */ if(Errors_CheckError(ERROR_NRF24_INIT)) { LED_Red_Blink(2); goto sleep; } TXmsg.M.status = 0; /* Reset status byte */ /* Check Programmable voltage detector (PVD) output flag, if battery low send this information */ /* Internal reference voltage should be stable at this point */ if(PWR->CSR1 & PWR_FLAG_PVDOF) { /* VDD/VDDA is below the VPVD threshold (2,26V) */ TXmsg.M.status |= STATUS_LOWBATT; } PWR_PVDCmd(DISABLE); if(btn_pressed == BUTTON1) { TXmsg.M.ownID = (u32)ownID_btn1; TXmsg.M.txcnt = tx_cnt++; nrf24_send((u8*)&TXmsg.B.b[0]); } else if(btn_pressed = BUTTON2) { TXmsg.M.ownID = (u32)ownID_btn2; TXmsg.M.txcnt = tx_cnt++; nrf24_send((u8*)&TXmsg.B.b[0]); } btn_pressed = 0; do { status = nrf24_getStatus(); } while(!(status & ((1 << TX_DS) | (1 << MAX_RT)))); if(status & (1 << TX_DS)) /* send successful */ { if(flag_display_retrans_cnt) { u8 i; u8 retrans_cnt = nrf24_retransmissionCount(); LED_Green_Blink(retrans_cnt); } else { LED_Green_Blink(1); } } else if(status & (1 << MAX_RT)) /* send failed, max retries exceeded */ { LED_Red_Blink(1); } sleep: Goto_HALT(); } }