/******** Transceiver_SendMessage ******************************************* // unpack transceiver packet and send out // Input: // pkt - transceiver packet to be sent // Output: status // ------------------------------------------------------------------------*/ unsigned char Transceiver_SendMessage ( TransceiverPacket pkt ) { unsigned char tx[TRANSCEIVER_MAX_PAYLOAD]; unsigned char index, length; unsigned char status = SUCCESS; // validate packet Debug_NetworkTransceiver_PrintPacket(&pkt); if ( MAX_DATA_SIZE < pkt.dataSize ) { status = ERROR_INVALID_PAKCET; Debug_Printf ("Transceiver_SendMessage: Error 0x%x\n", status); goto exit; } // unpack transceiver packet tx[SOURCE_ID_INDEX] = pkt.srcID; tx[DEST_ID_INDEX] = pkt.destID; tx[MSG_ID_INDEX] = pkt.msgID; tx[DATA_SIZE_INDEX] = pkt.dataSize; length = DATA_INDEX; for ( index = 0; index < pkt.dataSize; index++,length++ ) { tx[length] = pkt.data[index]; } Debug_NetworkTransceiver_PrintPayload(tx); // lock transceiver while ( xSemaphoreTake(Transceiver_Mutex, portMAX_DELAY) != pdTRUE ); GPIOPinIntDisable ( nrf24l01_IRQ_IOREGISTER, nrf24l01_IRQ_PIN ); nrf24l01_set_as_tx(); nrf24l01_write_tx_payload ( tx, TRANSCEIVER_MAX_PAYLOAD, true ); //wait until the packet has been sent or the maximum number of retries has been active while( !( nrf24l01_irq_pin_active() && (nrf24l01_irq_tx_ds_active()||nrf24l01_irq_max_rt_active()) ) ); if ( nrf24l01_irq_max_rt_active() ) { // hit maximum number of retries nrf24l01_flush_tx(); status = ERROR_MAX_RETRIES; Debug_Printf ("Transceiver_SendMessage: Error 0x%x\n", status); } // reset transceiver nrf24l01_irq_clear_all(); nrf24l01_set_as_rx(true); Delay_US(130); //unlock transceiver GPIOPinIntEnable ( nrf24l01_IRQ_IOREGISTER, nrf24l01_IRQ_PIN ); while ( xSemaphoreGive(Transceiver_Mutex) != pdTRUE ); exit: return status; }
//flush both fifos and clear interrupts void nrf24l01_clear_flush() { nrf24l01_flush_rx(); nrf24l01_flush_tx(); nrf24l01_irq_clear_all(); }
//main routine int main() { unsigned char data; //register to hold letter sent and received unsigned int count; //counter for for loop Initialize(); //initialize PLL, IO, UART, SPI, set up nRF24L01 as TX stdout = &my_stream; stdin = &my_stream; printf_P(PSTR(" type character to send \n")); //main program loop while(1) { //check UART status register to see if data has been received. if so, process while(usart0_rx_data_ready()) { // data = usart0_get_rx_data(); //get data from UART data = getchar(); //get data from UART PORTC = data; nrf24l01_write_tx_payload(&data, 1, true); //transmit received char over RF //wait until the packet has been sent or the maximum number of retries has been active while(!(nrf24l01_irq_pin_active() && (nrf24l01_irq_tx_ds_active() || nrf24l01_irq_max_rt_active()))); //check to see if the maximum number of retries has been hit. if not, wait for the RX device // to send the char back. if so, assume the packet is lost and send "*" back to UART if(!nrf24l01_irq_max_rt_active()) { nrf24l01_irq_clear_all(); //clear all interrupts in the 24L01 nrf24l01_set_as_rx(true); //change the device to an RX to get the character back from the other 24L01 //wait a while to see if we get the data back (change the loop maximum and the lower if // argument (should be loop maximum - 1) to lengthen or shorten this time frame for(count = 0; count < 25000; count++) { //check to see if the data has been received. if so, get the data and exit the loop. // if the loop is at its last count, assume the packet has been lost and set the data // to go to the UART to "?". If neither of these is true, keep looping. if((nrf24l01_irq_pin_active() && nrf24l01_irq_rx_dr_active())) { nrf24l01_read_rx_payload(&data, 1); //get the payload into data break; } //if loop is on its last iteration, assume packet has been lost. if(count == 24999) data = '?'; } nrf24l01_irq_clear_all(); //clear interrupts again printf("%c", data); //print the received data (or ? if none) to the screen // DelayUS(130); //wait for receiver to come from standby to RX _delay_us(35); _delay_us(35); _delay_us(35); _delay_us(25); nrf24l01_set_as_tx(); //resume normal operation as a TX } else { nrf24l01_flush_tx(); //get the unsent character out of the TX FIFO nrf24l01_irq_clear_all(); //clear all interrupts printf("*"); //print "*" to the screen to show that the receiver did not receive the packet } // ToggleLED(); //toggle the on-board LED as visual indication that the loop has completed } } }