nRF905 Radio Library for Arduino  4.0
nRF905.h
Go to the documentation of this file.
1 /*
2  * Project: nRF905 Radio Library for Arduino
3  * Author: Zak Kemble, contact@zakkemble.net
4  * Copyright: (C) 2020 by Zak Kemble
5  * License: GNU GPL v3 (see License.txt)
6  * Web: https://blog.zakkemble.net/nrf905-avrarduino-librarydriver/
7  */
8 
9 #ifndef NRF905_H_
10 #define NRF905_H_
11 
12 #include <Arduino.h>
13 #include <SPI.h>
14 #include <stdint.h>
15 #include "nRF905_config.h"
16 
20 typedef enum
21 {
22 // TODO use nRF905_mode_t instead of nRF905_nextmode_t?
23 
28 
32 typedef enum
33 {
40 
44 typedef enum
45 {
46 // NOTE:
47 // When using NRF905_BAND_868 and NRF905_BAND_915 for calculating channel (NRF905_CALC_CHANNEL(f, b)) they should be value 0x01,
48 // but when using them for setting registers their value should be 0x02.
49 // They're defined as 0x02 here so when used for calculating channel they're right shifted by 1
50 
51  NRF905_BAND_433 = 0x00,
52  NRF905_BAND_868 = 0x02,
55 
59 typedef enum
60 {
61  NRF905_PWR_n10 = 0x00,
62  NRF905_PWR_n2 = 0x04,
63  NRF905_PWR_6 = 0x08,
64  NRF905_PWR_10 = 0x0C
65 } nRF905_pwr_t;
66 
70 typedef enum
71 {
78 
84 typedef enum
85 {
87  NRF905_CRC_8 = 0x40,
88  NRF905_CRC_16 = 0xC0,
89 } nRF905_crc_t;
90 
91 #define NRF905_MAX_PAYLOAD 32
92 #define NRF905_REGISTER_COUNT 10
93 #define NRF905_DEFAULT_RXADDR 0xE7E7E7E7
94 #define NRF905_DEFAULT_TXADDR 0xE7E7E7E7
95 #define NRF905_PIN_UNUSED 255
96 
97 #define NRF905_CALC_CHANNEL(f, b) ((((f) / (1 + (b>>1))) - 422400000UL) / 100000UL)
98 
99 class nRF905 //: public Stream // TODO see Wire library
100 {
101 private:
102  SPIClass spi;
103  SPISettings spiSettings;
104 
105  // Pins
106  uint8_t csn; // SPI SS
107  uint8_t trx; // Standby (CE/TRX_EN)
108  uint8_t tx; // TX or RX mode (TXE/TX_EN)
109  uint8_t pwr; // Power-down control (PWR)
110  uint8_t cd; // Carrier detect (CD)
111  uint8_t dr; // Data ready (DR)
112  uint8_t am; // Address match (AM)
113 
114  // Events
115  void (*onRxComplete)(nRF905* device);
116  void (*onRxInvalid)(nRF905* device);
117  void (*onTxComplete)(nRF905* device);
118  void (*onAddrMatch)(nRF905* device);
119 
120  volatile uint8_t validPacket;
121  bool polledMode;
122 
123  inline uint8_t cselect();
124  inline uint8_t cdeselect();
125  uint8_t readConfigRegister(uint8_t reg);
126  void writeConfigRegister(uint8_t reg, uint8_t val);
127  void setConfigReg1(uint8_t val, uint8_t mask, uint8_t reg);
128  void setConfigReg2(uint8_t val, uint8_t mask, uint8_t reg);
129  void defaultConfig();
130  inline void powerOn(bool val);
131  inline void standbyMode(bool val);
132  inline void txMode(bool val);
133  void setAddress(uint32_t address, uint8_t cmd);
134  uint8_t readStatus();
135  //bool dataReady();
136  bool addressMatched();
137 
138 public:
139  /*virtual size_t write(uint8_t);
140  virtual size_t write(const uint8_t *, size_t);
141  virtual int available();
142  virtual int read();
143  virtual int peek();
144  virtual void flush();
145  using Print::write;*/
146 
147  nRF905();
148 
174  void begin(
175  SPIClass spi,
176  uint32_t spiClock,
177  int csn,
178  int trx,
179  int tx,
180  int pwr,
181  int cd,
182  int dr,
183  int am,
184  void (*callback_interrupt_dr)(),
185  void (*callback_interrupt_am)()
186  );
187 
197  void otherSPIinterrupts();
198 
211  void events(
212  void (*onRxComplete)(nRF905* device),
213  void (*onRxInvalid)(nRF905* device),
214  void (*onTxComplete)(nRF905* device),
215  void (*onAddrMatch)(nRF905* device)
216  );
217 
232  void setChannel(uint16_t channel);
233 
246  void setBand(nRF905_band_t band);
247 
263  void setAutoRetransmit(bool val);
264 
275  void setLowRxPower(bool val);
276 
287  void setTransmitPower(nRF905_pwr_t val);
288 
299  void setCRC(nRF905_crc_t val);
300 
311  void setClockOut(nRF905_outclk_t val);
312 
328  void setPayloadSize(uint8_t sizeTX, uint8_t sizeRX);
329 
343  void setAddressSize(uint8_t sizeTX, uint8_t sizeRX);
344 
352  bool receiveBusy();
353 
361  bool airwayBusy();
362 
374  void setListenAddress(uint32_t address);
375 
392  void write(uint32_t sendTo, void* data, uint8_t len);
393 
407  void read(void* data, uint8_t len);
408 
409  //uint32_t readUInt32(); // TODO
410  //uint8_t readUInt8(); // TODO
411  //char readChar(); // TODO
412  //uint8_t readString(char str, uint8_t size); // TODO
413 
435  bool TX(nRF905_nextmode_t nextMode, bool collisionAvoid);
436 
447  void RX();
448 
459  void powerDown();
460 
473  void standby();
474 
485 
494  void getConfigRegisters(void* regs);
495 
503  void interrupt_dr();
504 
512  void interrupt_am();
513 
523  void poll();
524 };
525 
526 #endif /* NRF905_H_ */
bool airwayBusy()
See if airway is busy (carrier detect pin asserted).
void setListenAddress(uint32_t address)
Set address to listen to.
10dBm = 10mW
Definition: nRF905.h:64
void RX()
Enter receive mode.
void poll()
When running in polled mode this method should be called as often as possible.
868/915MHz band
Definition: nRF905.h:53
4MHz clock
Definition: nRF905.h:73
nRF905_band_t
Frequency bands.
Definition: nRF905.h:44
void setAutoRetransmit(bool val)
Set auto-retransmit.
Transmit mode.
Definition: nRF905.h:37
Transmit mode (will auto-retransmit if enabled, otherwise will transmit a carrier wave with no data) ...
Definition: nRF905.h:26
2MHz clock
Definition: nRF905.h:74
nRF905_mode_t
Current radio mode.
Definition: nRF905.h:32
nRF905_outclk_t
Output a clock signal on pin 3 of IC.
Definition: nRF905.h:70
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.
Power-down mode.
Definition: nRF905.h:34
6dBm = 4mW
Definition: nRF905.h:63
Receive or transmit mode (when unable to tell due to the tx pin being unused, hardwired to VCC (TX) o...
Definition: nRF905.h:38
void read(void *data, uint8_t len)
Read received payload.
Standby mode.
Definition: nRF905.h:35
Disable CRC.
Definition: nRF905.h:86
1MHz clock
Definition: nRF905.h:75
nRF905_pwr_t
Output power (n means negative, n10 = -10).
Definition: nRF905.h:59
void standby()
Enter standby mode.
void interrupt_am()
When running in interrupt mode this method must be called from the AM interrupt callback function...
void setLowRxPower(bool val)
Set low power receive mode.
void events(void(*onRxComplete)(nRF905 *device), void(*onRxInvalid)(nRF905 *device), void(*onTxComplete)(nRF905 *device), void(*onAddrMatch)(nRF905 *device))
Register event functions.
void setBand(nRF905_band_t band)
Frequency band.
433MHz band
Definition: nRF905.h:51
Standby mode.
Definition: nRF905.h:24
void interrupt_dr()
When running in interrupt mode this method must be called from the DR interrupt callback function...
8bit CRC (Don&#39;t know what algorithm is used for this one)
Definition: nRF905.h:87
void otherSPIinterrupts()
If your sketch uses libraries that access the SPI bus from inside an interrupt then this method shoul...
void setChannel(uint16_t channel)
Channel to listen and transmit on.
void setAddressSize(uint8_t sizeTX, uint8_t sizeRX)
Address sizes.
void setPayloadSize(uint8_t sizeTX, uint8_t sizeRX)
Payload sizes.
void write(uint32_t sendTo, void *data, uint8_t len)
Write payload data and set destination address.
bool receiveBusy()
See if the address match (AM) is asserted.
Receive mode.
Definition: nRF905.h:25
nRF905_mode_t mode()
Get current radio mode.
void getConfigRegisters(void *regs)
Read configuration registers into byte array of NRF905_REGISTER_COUNT elements, mainly for debugging...
nRF905_nextmode_t
Available modes after transmission complete.
Definition: nRF905.h:20
Disable output clock.
Definition: nRF905.h:72
16bit CRC (CRC16-CCITT-FALSE (0xFFFF))
Definition: nRF905.h:88
868/915MHz band
Definition: nRF905.h:52
void setTransmitPower(nRF905_pwr_t val)
Set transmit output power.
-2dBm = 631uW
Definition: nRF905.h:62
Receive mode.
Definition: nRF905.h:36
-10dBm = 100uW
Definition: nRF905.h:61
nRF905_crc_t
CRC Checksum.
Definition: nRF905.h:84
500KHz clock (default)
Definition: nRF905.h:76
void setClockOut(nRF905_outclk_t val)
Set clock output.
bool TX(nRF905_nextmode_t nextMode, bool collisionAvoid)
Begin a transmission.
Definition: nRF905.h:99
void setCRC(nRF905_crc_t val)
Set CRC algorithm.
void powerDown()
Sleep.