Made RF24::available() report whether there are bytes available to be read #46
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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, 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: