nRF905 Radio Library for Arduino
4.0
|
#include <nRF905.h>
Public Member Functions | |
nRF905 () | |
void | begin (SPIClass spi, uint32_t spiClock, int csn, int trx, int tx, int pwr, int cd, int dr, int am, void(*callback_interrupt_dr)(), void(*callback_interrupt_am)()) |
Initialise, must be called after SPI.begin() and before any other nRF905 stuff. More... | |
void | otherSPIinterrupts () |
If your sketch uses libraries that access the SPI bus from inside an interrupt then this method should be called after the .begin() method. More... | |
void | events (void(*onRxComplete)(nRF905 *device), void(*onRxInvalid)(nRF905 *device), void(*onTxComplete)(nRF905 *device), void(*onAddrMatch)(nRF905 *device)) |
Register event functions. More... | |
void | setChannel (uint16_t channel) |
Channel to listen and transmit on. More... | |
void | setBand (nRF905_band_t band) |
Frequency band. More... | |
void | setAutoRetransmit (bool val) |
Set auto-retransmit. More... | |
void | setLowRxPower (bool val) |
Set low power receive mode. More... | |
void | setTransmitPower (nRF905_pwr_t val) |
Set transmit output power. More... | |
void | setCRC (nRF905_crc_t val) |
Set CRC algorithm. More... | |
void | setClockOut (nRF905_outclk_t val) |
Set clock output. More... | |
void | setPayloadSize (uint8_t sizeTX, uint8_t sizeRX) |
Payload sizes. More... | |
void | setAddressSize (uint8_t sizeTX, uint8_t sizeRX) |
Address sizes. More... | |
bool | receiveBusy () |
See if the address match (AM) is asserted. More... | |
bool | airwayBusy () |
See if airway is busy (carrier detect pin asserted). More... | |
void | setListenAddress (uint32_t address) |
Set address to listen to. More... | |
void | write (uint32_t sendTo, void *data, uint8_t len) |
Write payload data and set destination address. More... | |
void | read (void *data, uint8_t len) |
Read received payload. More... | |
bool | TX (nRF905_nextmode_t nextMode, bool collisionAvoid) |
Begin a transmission. More... | |
void | RX () |
Enter receive mode. More... | |
void | powerDown () |
Sleep. More... | |
void | standby () |
Enter standby mode. More... | |
nRF905_mode_t | mode () |
Get current radio mode. More... | |
void | getConfigRegisters (void *regs) |
Read configuration registers into byte array of NRF905_REGISTER_COUNT elements, mainly for debugging. More... | |
void | interrupt_dr () |
When running in interrupt mode this method must be called from the DR interrupt callback function. More... | |
void | interrupt_am () |
When running in interrupt mode this method must be called from the AM interrupt callback function. More... | |
void | poll () |
When running in polled mode this method should be called as often as possible. More... | |
nRF905::nRF905 | ( | ) |
void nRF905::begin | ( | SPIClass | spi, |
uint32_t | spiClock, | ||
int | csn, | ||
int | trx, | ||
int | tx, | ||
int | pwr, | ||
int | cd, | ||
int | dr, | ||
int | am, | ||
void(*)() | callback_interrupt_dr, | ||
void(*)() | callback_interrupt_am | ||
) |
Initialise, must be called after SPI.begin() and before any other nRF905 stuff.
trx
, tx
, pwr
, cd
, dr
and am
pins are optional. Use NRF905_PIN_UNUSED if the pin is not connected, but some functionally will be lost.
Have a look at the examples to see how things work (or not work) when some pins are not used. If tx
is not used and connected to VCC (TX) then either trx
or pwr
must be used otherwise the radio will always be transmitting either an empty carrier wave or if auto-retransmit is enabled then the payload is retransmitted over and over again, though updating the payload on-the-fly does work in auto-retransmit mode if that's what you're after.
If dr
pin is not used or callback_interrupt_dr
is NULL
then the library will run in polling mode. See .poll().
If am
pin is not used then the onAddrMatch event will not work.
[spi] | SPI bus (usually SPI, SPI1, SPI2 etc) |
[spiClock] | SPI clock rate, max 10000000 (10MHz) |
[csn] | SPI Slave select pin |
[trx] | CE/TRX_EN pin (standby control) (optional - must be connected to VCC if not used) |
[tx] | TXE/TX_EN pin (RX/TX mode control) (optional - must be connected to VCC (TX) or GND (RX) if not used depending on what the radio should do) |
[pwr] | PWR pin (power-down control) (optional - must be connected to VCC if not used) |
[cd] | CD pin (carrier detect) (optional) |
[dr] | DR pin (data ready) (optional) |
[am] | AM pin (address match) (optional) |
[callback_interrupt_dr] | Callback for DR interrupt (optional) |
[callback_interrupt_am] | Callback for AM interrupt (optional) |
void nRF905::otherSPIinterrupts | ( | ) |
If your sketch uses libraries that access the SPI bus from inside an interrupt then this method should be called after the .begin() method.
This will disable all interrupts when accessing the nRF905 via SPI instead of only disabling our own DR/AM pin interrupts. This will prevent other libraries from accessing the SPI at the same time as this library.
Example: transceiver.otherSPIinterrupts();
void nRF905::events | ( | void(*)(nRF905 *device) | onRxComplete, |
void(*)(nRF905 *device) | onRxInvalid, | ||
void(*)(nRF905 *device) | onTxComplete, | ||
void(*)(nRF905 *device) | onAddrMatch | ||
) |
Register event functions.
Example: transceiver.events(onRxComplete, onRxInvalid, onTxComplete, NULL);
[onRxComplete] | Function to run on new payload received |
[onRxInvalid] | Function to run on corrupted payload received |
[onTxComplete] | Function to run on transmission completion (only works when calling .TX() with NRF905_NEXTMODE_TX or NRF905_NEXTMODE_STANDBY) |
[onAddrMatch] | Function to run on address match (beginning of payload reception) |
void nRF905::setChannel | ( | uint16_t | channel | ) |
Channel to listen and transmit on.
433MHz band: Channel 0 is 422.4MHz up to 511 which is 473.5MHz (Each channel is 100KHz apart)
868/915MHz band: Channel 0 is 844.8MHz up to 511 which is 947MHz (Each channel is 200KHz apart)
Example: transceiver.setChannel(50);
[channel] | The channel (0 - 511) |
void nRF905::setBand | ( | nRF905_band_t | band | ) |
Frequency band.
Radio modules are usually only tuned for a specific band, either 433MHz or 868/915MHz. Using the wrong frequency band will result in very short range of less than a meter, sometimes they have to be touching each other before anything happens.
Example: transceiver.setBand(NRF905_BAND_433);
[band] | Frequency band, see nRF905_band_t |
void nRF905::setAutoRetransmit | ( | bool | val | ) |
Set auto-retransmit.
If nextMode
is set to NRF905_NEXTMODE_TX when calling .TX() and auto-retransmit is enabled then the radio will continuously retransmit the payload. If auto-retransmit is disabled then a carrier wave with no data will be transmitted instead (kinda useless).
Transmission will continue until the radio is put into standby, power-down or RX mode.
Can be useful in areas with lots of interference, but you'll need to make sure you can differentiate between retransmitted packets and new packets (like an ID number).
Other transmissions will be blocked if collision avoidance is enabled.
Example: transceiver.setAutoRetransmit(true);
[val] | true = enable, false = disable |
void nRF905::setLowRxPower | ( | bool | val | ) |
Set low power receive mode.
Reduces receive current from 12.2mA to 10.5mA, but also reduces sensitivity.
Example: transceiver.setLowRxPower(true);
[val] | true = enable, false = disable |
void nRF905::setTransmitPower | ( | nRF905_pwr_t | val | ) |
Set transmit output power.
Example: transceiver.setTransmitPower(NRF905_PWR_10);
[val] | Output power level, see nRF905_pwr_t |
void nRF905::setCRC | ( | nRF905_crc_t | val | ) |
Set CRC algorithm.
Example: transceiver.setCRC(NRF905_CRC_16);
[val] | CRC Type, see nRF905_crc_t |
void nRF905::setClockOut | ( | nRF905_outclk_t | val | ) |
Set clock output.
Example: transceiver.setClockOut(NRF905_OUTCLK_4MHZ);
[val] | Clock out frequency, see nRF905_outclk_t |
void nRF905::setPayloadSize | ( | uint8_t | sizeTX, |
uint8_t | sizeRX | ||
) |
Payload sizes.
The nRF905 only supports fixed sized payloads. The receiving end must be configured for the same payload size as the transmitting end.
Example: This will send 5 byte payloads in one direction, and 32 byte payloads in the other direction:
transceiver1.setPayloadSize(5, 32); // Will transmit 5 byte payloads, receive 32 byte payloads
transceiver2.setPayloadSize(32, 5); // Will transmit 32 byte payloads, receive 5 byte payloads
[sizeTX] | Transmit payload size (1 - 32) |
[sizeRX] | Recivie payload size (1 - 32) |
void nRF905::setAddressSize | ( | uint8_t | sizeTX, |
uint8_t | sizeRX | ||
) |
Address sizes.
The address is actually the SYNC part of the packet, just after the preamble and before the data. The SYNC word is used to mark the beginning of the payload data.
A 1 byte address is not recommended, a lot of false invalid packets will be received.
Example: transceiver.setAddressSize(4, 4);
[sizeTX] | Transmit address size, either 1 or 4 bytes only |
[sizeRX] | Receive address size, either 1 or 4 bytes only |
bool nRF905::receiveBusy | ( | ) |
See if the address match (AM) is asserted.
Example: if(transceiver.receiveBusy())
true
if currently receiving payload or payload is ready to be read, otherwise false
bool nRF905::airwayBusy | ( | ) |
See if airway is busy (carrier detect pin asserted).
Example: if(transceiver.airwayBusy())
true
if other transmissions detected, otherwise false
void nRF905::setListenAddress | ( | uint32_t | address | ) |
Set address to listen to.
Some good addresses would be 0xA94EC554
and 0xB54CAB34
, bad address would be 0xC201043F
and 0xFF00FF00
Example: transceiver.setListenAddress(0xA94EC554);
[address] | The address, a 32 bit integer (default address is 0xE7E7E7E7) |
void nRF905::write | ( | uint32_t | sendTo, |
void * | data, | ||
uint8_t | len | ||
) |
Write payload data and set destination address.
If data
is NULL
and/or len
is 0
then the payload will not be modified, only the destination address will be updated. Useful to sending the same data to multiple addresses.
If the radio is still transmitting then the payload and address will be updated as it is being sent, this means the payload on the receiving end may contain old and new data.
This also means that a node may receive part of a payload that was meant for a node with a different address.
Use the onTxComplete
event to set a flag or something to ensure the transmission is complete before writing another payload. However this only works if nextMode
is set to NRF905_NEXTMODE_TX or NRF905_NEXTMODE_STANDBY.
Example: transceiver.write(0xB54CAB34, buffer, sizeof(buffer));
[sendTo] | Address to send the payload to |
[data] | The data |
[len] | Data length (max NRF905_MAX_PAYLOAD) |
void nRF905::read | ( | void * | data, |
uint8_t | len | ||
) |
Read received payload.
This method can be called multiple times to read a few bytes at a time.
The payload is cleared when the radio enters power-down, RX or TX mode. Entering standby mode from RX mode does not clear the payload.
The radio will not receive any more data until all of the payload has been read or cleared.
Example: transceiver.read(buffer, 4);
[data] | Buffer for the data |
[len] | How many bytes to read |
bool nRF905::TX | ( | nRF905_nextmode_t | nextMode, |
bool | collisionAvoid | ||
) |
Begin a transmission.
If the radio is in power-down mode then this function will take an additional 3ms to complete.
If 3ms is too long then call .standby() and do whatever you need to do for at least 3ms before calling .TX().
If nextMode
is set to NRF905_NEXTMODE_RX and the radio was in standby mode then this function will take an additional 700us to complete.
If 700us is too long then set nextMode
to NRF905_NEXTMODE_STANDBY and call .RX() in the onTxComplete
event instead.
The onTxComplete event does not work when nextMode
is set to NRF905_NEXTMODE_RX.
For the collision avoidance to work the radio should be in RX mode for around 5ms before attempting to transmit.
Example: transceiver.TX(NRF905_NEXTMODE_STANDBY, true);
[nextMode] | What mode to enter once the transmission is complete, see nRF905_nextmode_t |
[collisionAvoid] | true = check for other transmissions before transmitting (CD pin must be connected), false = skip the check and just transmit |
false
if collision avoidance is enabled and other transmissions are going on, true
if transmission has successfully begunvoid nRF905::RX | ( | ) |
Enter receive mode.
If the radio is currently transmitting then receive mode will be entered once it has finished. This function will also automatically power-up the radio and leave standby mode.
Example: transceiver.RX();
void nRF905::powerDown | ( | ) |
Sleep.
Current consumption will be 2.5uA in this mode. Cancels any ongoing transmissions and clears the RX payload.
Example: transceiver.powerDown();
void nRF905::standby | ( | ) |
Enter standby mode.
Radio will wait for any ongoing transmissions to complete before entering standby mode.
If the radio was in power-down mode then there must be a 3ms delay between entering standby mode and beginning a transmission using .TX() with nextMode
as NRF905_NEXTMODE_STANDBY or NRF905_NEXTMODE_RX otherwise the transmission will not start. Calling .TX() while the radio is in power-down mode will automatically power-up the radio and wait 3ms before starting the transmission.
Example: transceiver.standby();
nRF905_mode_t nRF905::mode | ( | ) |
Get current radio mode.
Example: if(transceiver.mode() == NRF905_MODE_POWERDOWN)
void nRF905::getConfigRegisters | ( | void * | regs | ) |
Read configuration registers into byte array of NRF905_REGISTER_COUNT elements, mainly for debugging.
Example: transceiver.getConfigRegisters(regs);
[regs] | Buffer for register data, size must be at least NRF905_REGISTER_COUNT bytes. |
void nRF905::interrupt_dr | ( | ) |
When running in interrupt mode this method must be called from the DR interrupt callback function.
Example: transceiver.interrupt_dr();
void nRF905::interrupt_am | ( | ) |
When running in interrupt mode this method must be called from the AM interrupt callback function.
Example: transceiver.interrupt_am();
void nRF905::poll | ( | ) |
When running in polled mode this method should be called as often as possible.
Maximum interval depends on how long it takes to receive a payload, which is around 6ms for 32 bytes, down to around 1.1ms for a 1 byte payload. If .poll() is not called often enough then events will be missed, the worse one being a missed onRxComplete
event caused by reading a received payload, then another payload is received again without a .poll() call in between.
Example: transceiver.poll();