Hi Guys! I’m designing software to have a stable and lossless rs232 connection over an rf24l01 (one on each side). My code seems to be working, but sometimes the nrf’s seem to stop listening to each other… And now I’ve written a handshaking code, to find an empty channel and here too it sometimes works perfectly and sometimes it just keeps searching indefinitely…
I have the feeling I’m doing something wrong with timing the sends and receives. I do incorporate the 130us delay and have even added some extra time on the tx side, to be sure the rx is ready.
Any ideas on timing the switch from tx to rx on both sides better?
Here is a snippet of my send and recieve functions for the handshaking:
bool sendAck (bool ok, unsigned int maxSendingTime, bool setAsTX)
{
unsigned char okData[28] = { '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1' };
unsigned char falseData[28] = { '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2' };
nrf24l01_clear_flush(); //clear interrupts and flush
if (setAsTX)
{
nrf24l01_set_as_tx(); //change the device to an TX
DelayUS(130); //wait for other receiver to come from standby to RX
}
if (!ok)
{
nrf24l01_write_tx_payload(falseData, 28, true);
}
else
{
nrf24l01_write_tx_payload(okData, 28, true);
}
WriteTimer0(0); // reset timer
while( !(nrf24l01_irq_pin_active() && (nrf24l01_irq_tx_ds_active() || nrf24l01_irq_max_rt_active()) ) && ReadTimer0() < maxSendingTime );
ToggleLED1();
if (nrf24l01_irq_pin_active() && nrf24l01_irq_tx_ds_active())
{
DelayUS(300); // allow time to recieve ACK and settle as TX again
ToggleLED4();
nrf24l01_clear_flush();
return true; // transmit succeeded and was recieved
}
else
{
ToggleLED2();
nrf24l01_clear_flush();
return false; // unsuccessful transmit
}
}
unsigned int recieveAck (unsigned int maxListeningTime, bool setAsRX)
{
unsigned char okData[28] = { '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1' };
unsigned char falseData[28] = { '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2' };
unsigned char recievedData[28] = { 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x' };
nrf24l01_clear_flush(); //clear interrupts and flush
if (setAsRX)
{
nrf24l01_set_as_rx(true); //change the device to an RX
DelayUS(200); //wait for receiver to come from standby to RX
}
WriteTimer0(0); // reset timer
while ( !(nrf24l01_irq_pin_active() && nrf24l01_irq_rx_dr_active()) && ReadTimer0() < maxListeningTime) // wait for packet
ToggleLED1();
if (nrf24l01_irq_pin_active() && nrf24l01_irq_rx_dr_active()) // if recieved
{
DelayUS(300); // allow time to send ACK and settle as RX again
nrf24l01_read_rx_payload(&recievedData, 28); // read into char
if (okData[0] == recievedData[0] && okData[1] == recievedData[1]) // compare data
{
ToggleLED4();
nrf24l01_clear_flush();
return 1; // revieced an OK
}
if (falseData[0] == recievedData[0] && falseData[1] == recievedData[1])
{
//ToggleLED3();
nrf24l01_clear_flush();
return 0; // recieved a Not OK
}
}
ToggleLED2();
nrf24l01_clear_flush();
return 2; // Did not recieve anything
}