/******** 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; }
//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 } } }
int main (void) { setvbuf(stdin, NULL, _IONBF, 0); setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stderr, NULL, _IONBF, 0); //Driver initialization ds_systick_init(); ds_delay_init(); ds_uart_init(); ds_rtc_init(); ds_i2c1_init(); ds_nordic_init(); ds_therm_init(); if (SysTick_Config(SystemCoreClock / 1000)) while (1); //Initialize the nordic library nrf24l01_initialize_debug(false, 32, true); //Clear queues and interrupts //Need to fill txdata[] with our data nrf24l01_clear_flush(); char rxdata[32]; char txdata[32]; // txdata[0] = 'h'; // txdata[1] = ' '; // txdata[2] = 't'; // txdata[3] = 'i'; // txdata[4] = 'm'; float temp = 0; int i; // for (i = 5; i < 30; i++) { // txdata[i] = '!'; // } // txdata[30] = '\n'; // txdata[31] = '\0'; while (1) { for (i = 0; i < 5000; i++) { ds_delay_uS(1000); } temp = ds_therm_read_degC(ds_read_therm()); sprintf(txdata, "%f\n", temp); nrf24l01_write_tx_float(&temp, 32, true); while(!(nrf24l01_irq_pin_active() && (nrf24l01_irq_tx_ds_active() || nrf24l01_irq_max_rt_active()))); //Confirms acknowledge //if (!nrf24l01_irq_max_rt_active()) { // nrf24l01_irq_clear_all(); // nrf24l01_set_as_rx(true); //} //else { // nrf24l01_flush_tx(); //get the unsent character out of the TX FIFO // nrf24l01_irq_clear_all(); //clear all interrupts // printf("Node: Failed to send %c\n",txdata); //} //Waits for data and reads it //while(!(nrf24l01_irq_pin_active() && nrf24l01_irq_rx_dr_active())); //nrf24l01_read_rx_payload(rxdata, 32); nrf24l01_irq_clear_all(); //printf(rxdata); } //puts into transmit mode - not needed for base station ds_delay_uS(130); nrf24l01_set_as_tx(); }