maniacbug/RF24 — pull request #46 by FesterBesterTester (2014-05-25 23:38:27+00:00)
Title: Made RF24::available() report whether there are bytes available to be read

RF24::available() is intended to test whether there are bytes available to be read. The current implementation instead tests whether a packet has been received since the last time RF24::available() was called.

The RX FIFO can hold three packets. If there are multiple packets in the RX FIFO at the start of the loop below but no packets arrive during the loop, then only the first packet will be retrieved.

while (radio.available())
radio.read(buf, len);

RF24::available() checks and then clears the RX_DR IRQ, returning true if and only if RX_DR was set. According to the [nrf24l01+ spec](http://www.nordicsemi.com/eng/content/download/2726/34069/file/nRF24L01P_Product_Specification_1_0.pdf), the RX_DR IRQ is asserted when a new packet arrives. Clearing RX_DR in RF24::available() will cause subsequent calls to RF24::available() to return false until another packet arrives, even though there may still be unread data available in the RX FIFO.

This patch makes RF24::available() correctly report whether there are bytes available to be read by checking the RX_EMPTY bit of the FIFO_STATUS register instead of relying on the state of RX_DR. It clears the RX_DR bit after the payload has been read as suggested by the nrf24l01+ spec:

> The RX_DR IRQ is asserted by a new packet arrival event. The procedure for handling this interrupt should be: 1) read payload through SPI, 2) clear RX_DR IRQ, 3) read FIFO_STATUS to check if there are more payloads available in RX FIFO, 4) if there are more data in RX FIFO, repeat from step 1).