summaryrefslogtreecommitdiff
path: root/include/driver/nrf24l01.h
blob: c10332d20c84dca1e2c42658034c7c0b02063e6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#ifndef NRF24L01_H
#define NRF24L01_H

#include <stdint.h>
#include "driver/gpio.h"
#include "arch.h"

#define rf24_max(a, b) ((a) > (b) ? (a) : (b))
#define rf24_min(a, b) ((a) < (b) ? (a) : (b))

class Nrf24l01
{
private:
	Nrf24l01(const Nrf24l01 &copy);
	unsigned char txbuf[2];
	unsigned char rxbuf[2];

	bool p_variant;					  /* False for RF24L01 and true for RF24L01P */
	uint8_t payload_size;			  /**< Fixed size of payloads */
	bool dynamic_payloads_enabled;	/**< Whether dynamic payloads are enabled. */
	uint8_t pipe0_reading_address[5]; /**< Last address set on pipe 0 for reading. */
	uint8_t addr_width;				  /**< The address width to use - 3,4 or 5 bytes. */
	uint32_t txRxDelay;				  /**< Var for adjusting delays depending on datarate */

	uint8_t writeRegister(uint8_t reg, uint8_t value);
	uint8_t readRegister(uint8_t reg);
	uint8_t writePayload(const void *buf, uint8_t data_len, const uint8_t writeType);

	inline void csnHigh()
	{
		gpio.write(NRF24L01_CS_PIN, 1);
		arch.delay_us(5);
	}
	inline void csnLow()
	{
		gpio.write(NRF24L01_CS_PIN, 0);
		arch.delay_us(5);
	}
	inline void beginTransaction()
	{
		csnLow();
	}
	inline void endTransaction()
	{
		csnHigh();
	}

public:
	Nrf24l01() : payload_size(32), dynamic_payloads_enabled(false), addr_width(5) {}

	/**
		 * Power Amplifier level.
		 *
		 * For use with setPALevel()
		 */
	enum rf24_pa_dbm_e
	{
		RF24_PA_MIN = 0,
		RF24_PA_LOW,
		RF24_PA_HIGH,
		RF24_PA_MAX,
		RF24_PA_ERROR
	};

	/**
		 * Data rate.  How fast data moves through the air.
		 *
		 * For use with setDataRate()
		 */
	enum rf24_datarate_e
	{
		RF24_1MBPS = 0,
		RF24_2MBPS,
		RF24_250KBPS
	};

	/**
		 * CRC Length.  How big (if any) of a CRC is included.
		 *
		 * For use with setCRCLength()
		 */
	enum rf24_crclength_e
	{
		RF24_CRC_DISABLED = 0,
		RF24_CRC_8,
		RF24_CRC_16
	};

	/**
   * Enter low-power mode
   *
   * To return to normal power mode, call powerUp().
   *
   * @note After calling startListening(), a basic radio will consume about 13.5mA
   * at max PA level.
   * During active transmission, the radio will consume about 11.5mA, but this will
   * be reduced to 26uA (.026mA) between sending.
   * In full powerDown mode, the radio will consume approximately 900nA (.0009mA)
   *
   * @code
   * radio.powerDown();
   * avr_enter_sleep_mode(); // Custom function to sleep the device
   * radio.powerUp();
   * @endcode
   */
	void powerDown(void);

	/**
   * Leave low-power mode - required for normal radio operation after calling powerDown()
   *
   * To return to low power mode, call powerDown().
   * @note This will take up to 5ms for maximum compatibility
   */
	void powerUp(void);

	/**
   * Empty the transmit buffer. This is generally not required in standard operation.
   * May be required in specific cases after stopListening() , if operating at 250KBPS data rate.
   *
   * @return Current value of status register
   */
	uint8_t flushTx(void);

	/**
   * Empty the receive buffer
   *
   * @return Current value of status register
   */
	uint8_t flushRx(void);

	void setup();
	/**
   * Set Power Amplifier (PA) level to one of four levels:
   * RF24_PA_MIN, RF24_PA_LOW, RF24_PA_HIGH and RF24_PA_MAX
   *
   * The power levels correspond to the following output levels respectively:
   * NRF24L01: -18dBm, -12dBm,-6dBM, and 0dBm
   *
   * SI24R1: -6dBm, 0dBm, 3dBM, and 7dBm.
   *
   * @param level Desired PA level.
   */
	void setPALevel(uint8_t level); // 0 (-18B), 1 (-12dB), 2 (-6dB), 3 (0dB)

	/**
  * Set the address width from 3 to 5 bytes (24, 32 or 40 bit)
  *
  * @param a_width The address width to use: 3,4 or 5
  */

	void setAddressWidth(uint8_t a_width);

	/**
   * Set the number and delay of retries upon failed submit
   *
   * @param delay How long to wait between each retry, in multiples of 250us,
   * max is 15.  0 means 250us, 15 means 4000us.
   * @param count How many retries before giving up, max 15
   */
	void setRetries(uint8_t delay, uint8_t count);

	/**
   * Set RF communication channel
   *
   * @param channel Which RF channel to communicate on, 0-125
   */
	void setChannel(uint8_t channel);

	/**
   * Get RF communication channel
   *
   * @return The currently configured RF Channel
   */
	uint8_t getChannel(void);

	/**
   * Set Static Payload Size
   *
   * This implementation uses a pre-stablished fixed payload size for all
   * transmissions.  If this method is never called, the driver will always
   * transmit the maximum payload size (32 bytes), no matter how much
   * was sent to write().
   *
   * @todo Implement variable-sized payloads feature
   *
   * @param size The number of bytes in the payload
   */
	void setPayloadSize(uint8_t size);

	/**
   * Get Static Payload Size
   *
   * @see setPayloadSize()
   *
   * @return The number of bytes in the payload
   */
	uint8_t getPayloadSize(void);

	/**
   * Set the transmission data rate
   *
   * @warning setting RF24_250KBPS will fail for non-plus units
   *
   * @param speed RF24_250KBPS for 250kbs, RF24_1MBPS for 1Mbps, or RF24_2MBPS for 2Mbps
   * @return true if the change was successful
   */
	bool setDataRate(rf24_datarate_e speed);

	/**
  * The radio will generate interrupt signals when a transmission is complete,
  * a transmission fails, or a payload is received. This allows users to mask
  * those interrupts to prevent them from generating a signal on the interrupt
  * pin. Interrupts are enabled on the radio chip by default.
  *
  * @code
  * 	Mask all interrupts except the receive interrupt:
  *
  *		radio.maskIRQ(1,1,0);
  * @endcode
  *
  * @param tx_ok  Mask transmission complete interrupts
  * @param tx_fail  Mask transmit failure interrupts
  * @param rx_ready Mask payload received interrupts
  */
	void maskIRQ(bool tx_ok, bool tx_fail, bool rx_ready);

	/**
   * Turn on or off the special features of the chip
   *
   * The chip has certain 'features' which are only available when the 'features'
   * are enabled.  See the datasheet for details.
   */
	void toggleFeatures(void);

	uint8_t write(const void *buf, uint8_t len, bool blocking);

	uint8_t getStatus();
};

extern Nrf24l01 nrf24l01;

#endif