embedded-software
reusable software modules for embedded systems
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
nrf24.h
Go to the documentation of this file.
1 /* Copyright 2015 Michael Muhlbaier and Anthony Merlino
2 
3  Licensed under the Apache License, Version 2.0 (the "License");
4  you may not use this file except in compliance with the License.
5  You may obtain a copy of the License at
6 
7  http://www.apache.org/licenses/LICENSE-2.0
8 
9  Unless required by applicable law or agreed to in writing, software
10  distributed under the License is distributed on an "AS IS" BASIS,
11  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  See the License for the specific language governing permissions and
13  limitations under the License.
14 
15 
16  Sections of this module were heavily influenced by the Arduino library for Nordic Semiconductor NRF24L01+
17  found at https://github.com/akupila/NRF24
18 */
19 
32 #ifndef _NRF24_H_
33 #define _NRF24_H_
34 
35 #include <stdint.h>
36 
37 typedef struct nrf24_radio_t {
38  void (*ce)(uint8_t);
39  void (*csn)(uint8_t);
40  void (*AckReceived)(void); // should be called when in PTX mode and TX_DS flag is set
41  void (*AckPayloadReceived)(uint8_t * data, uint8_t length);
42  void (*MaxRetriesHit)(void);
43  void (*ReceivedPayload)(uint8_t * data, uint8_t length);
44  void (*AckPayloadSent)(void);
45  uint8_t state;
46  uint8_t spi_channel;
47  struct settings {
48  uint8_t prim_rx : 1;
49  uint8_t aw : 2;
50  uint8_t unused : 5;
51  } settings;
52  uint64_t rx_address_p0;
53  uint64_t last_tx_address;
54  uint8_t last_rx_pipe;
55 } nrf24_t;
56 
57 
58 typedef enum {
59  NRF24_PA_LOW = 0, // -18dBm / 7.0mA transmit
60  NRF24_PA_MID, // -12dBm / 7.5mA transmit
61  NRF24_PA_HIGH, // -6dBm / 9mA transmit
62  NRF24_PA_MAX // 0dBm / 11.3mA transmit
64 
65 typedef enum {
70 
71 typedef enum {
76 
110 void nRF24_Init(nrf24_t * nrf_object);
111 
120 void nRF24_OpenRxPipe(nrf24_t * nrf_object, uint8_t pipenum, uint64_t address);
121 
127 void nRF24_OpenTxPipe(nrf24_t * nrf_object, uint64_t address);
128 
132 void nRF24_EventHandler(nrf24_t * nrf_object);
133 #define nRF24_ISR(nrf_object) Task_Queue( (task_t)nRF24_EventHandler, nrf_object)
134 
136 void nRF24_StartListening(nrf24_t * nrf_object);
137 
139 void nRF24_Standby(nrf24_t * nrf_object);
140 
141 void nRF24_WriteReg(nrf24_t * nrf_object, uint8_t reg_address, uint8_t value);
142 void nRF24_WriteMultReg(nrf24_t * nrf_object, uint8_t reg_address, uint8_t *data_ptr, uint8_t length);
143 uint8_t nRF24_ReadReg(nrf24_t * nrf_object, uint8_t reg_address);
145 uint8_t nRF24_GetPayloadLength(nrf24_t * nrf_object);
146 
148 void nRF24_Write(nrf24_t * nrf_object, uint8_t * buf, uint8_t length);
150 void nRF24_Read(nrf24_t * nrf_object, uint8_t * buf, uint8_t length);
152 void nRF24_WriteAck(nrf24_t * nrf_object, uint8_t * buf, uint8_t length, uint8_t pipe);
153 
155 void nRF24_SetDataRate(nrf24_t * nrf_object, nrf24_datarate_e rate);
156 void nRF24_SetCRCMode(nrf24_t * nrf_object, nrf24_crc_mode_e mode);
157 void nRF24_SetChannel(nrf24_t * nrf_object, uint8_t channel);
158 void nRF24_SetActive(nrf24_t * nrf_object, uint8_t active);
159 
164 void nRF24_SetRetries(nrf24_t * nrf_object, uint8_t delay, uint8_t count);
165 void nRF24_FlushRx(nrf24_t * nrf_object);
166 void nRF24_FlushTx(nrf24_t * nrf_object);
167 
170 /* Memory Map */
171 #define CONFIG 0x00
172 #define EN_AA 0x01
173 #define EN_RXADDR 0x02
174 #define SETUP_AW 0x03
175 #define SETUP_RETR 0x04
176 #define RF_CH 0x05
177 #define RF_SETUP 0x06
178 #define STATUS 0x07
179 #define OBSERVE_TX 0x08
180 #define CD 0x09
181 #define RX_ADDR_P0 0x0A
182 #define RX_ADDR_P1 0x0B
183 #define RX_ADDR_P2 0x0C
184 #define RX_ADDR_P3 0x0D
185 #define RX_ADDR_P4 0x0E
186 #define RX_ADDR_P5 0x0F
187 #define TX_ADDR 0x10
188 #define RX_PW_P0 0x11
189 #define RX_PW_P1 0x12
190 #define RX_PW_P2 0x13
191 #define RX_PW_P3 0x14
192 #define RX_PW_P4 0x15
193 #define RX_PW_P5 0x16
194 #define FIFO_STATUS 0x17
195 #define DYNPD 0x1C
196 #define FEATURE 0x1D
197 
198 /* Helpers - Memory Macros */
199 #define RX_ADDR_BASE RX_ADDR_P0
200 
201 #define AW_5B 0x03 // 5 byte address
202 #define AW_4B 0x02 // 4 byte address
203 #define AW_3B 0x01 // 3 byte address
204 
205 // Bit Mnemonics ----------------------
206 // CONFIG
207 #define MASK_RX_DR 0x40
208 #define MASK_TX_DS 0x20
209 #define MASK_MAX_RT 0x10
210 #define EN_CRC 0x08
211 #define CRCO 0x04
212 #define PWR_UP 0x02
213 #define PRIM_RX 0x01
214 // EN_AA
215 #define ENAA_P5 0x20
216 #define ENAA_P4 0x10
217 #define ENAA_P3 0x08
218 #define ENAA_P2 0x04
219 #define ENAA_P1 0x02
220 #define ENAA_P0 0x01
221 // EN_RXADDR
222 #define ERX_P5 0x20
223 #define ERX_P4 0x10
224 #define ERX_P3 0x08
225 #define ERX_P2 0x04
226 #define ERX_P1 0x02
227 #define ERX_P0 0x01
228 // SETUP_AW
229 #define AW 0x01
230 // SETUP_RETR
231 #define ARD 0x10
232 #define ARC 0x01
233 // RF_SETUP
234 #define CONT_WAVE 0x80
235 #define RF_DR_LOW 0x20
236 #define PLL_LOCK 0x10
237 #define RF_DR_HIGH 0x08
238 #define RF_PA_HIGH 0x04
239 #define RF_PA_LOW 0x02
240 // STATUS
241 #define RX_DR 0x40
242 #define TX_DS 0x20
243 #define MAX_RT 0x10
244 #define RX_P_NO 0x02
245 #define TX_FULL 0x01
246 // OBSERVE_TX
247 #define PLOS_CNT 0x10
248 #define ARC_CNT 0x01
249 // FIFO_STATUS
250 #define TX_REUSE 0x40
251 #define TX_FULL_FIFO 0x20 // annoyingly this has the same mnemonic as in STATUS
252 #define TX_EMPTY 0x10
253 #define RX_FULL 0x02
254 #define RX_EMPTY 0x01
255 // DYNPD
256 #define DPL_P5 0x20
257 #define DPL_P4 0x10
258 #define DPL_P3 0x08
259 #define DPL_P2 0x04
260 #define DPL_P1 0x02
261 #define DPL_P0 0x01
262 // FEATURE
263 #define EN_DPL 0x04
264 #define EN_ACK_PAY 0x02
265 #define EN_DYN_ACK 0x01
266 
267 // SPI Commands ----------------------
268 #define REGISTER_MASK 0x1F
269 #define R_REGISTER 0x00
270 #define W_REGISTER 0x20
271 #define R_RX_PAYLOAD 0x61
272 #define W_TX_PAYLOAD 0xA0
273 #define FLUSH_TX 0xE1
274 #define FLUSH_RX 0xE2
275 #define REUSE_TX_PL 0xE3
276 #define ACTIVATE 0x50
277 #define R_RX_PL_WID 0x60
278 #define W_ACK_PAYLOAD 0xA8
279 #define W_TX_PAYLOAD_NO_ACK 0xB0
280 #define NOP 0xFF
281 
282 #endif //_NRF24_H_
void(* AckPayloadReceived)(uint8_t *data, uint8_t length)
Definition: nrf24.h:41
uint8_t spi_channel
Definition: nrf24.h:46
Definition: nrf24.h:66
Definition: nrf24.h:47
uint8_t prim_rx
Definition: nrf24.h:48
void(* ce)(uint8_t)
Set Chip Enable Function Pointer.
Definition: nrf24.h:38
uint8_t nRF24_ReadReg(nrf24_t *nrf_object, uint8_t reg_address)
Definition: nrf24.c:362
uint8_t state
state of radio
Definition: nrf24.h:45
void(* AckPayloadSent)(void)
Definition: nrf24.h:44
void nRF24_WriteAck(nrf24_t *nrf_object, uint8_t *buf, uint8_t length, uint8_t pipe)
Definition: nrf24.c:105
void nRF24_OpenTxPipe(nrf24_t *nrf_object, uint64_t address)
Definition: nrf24.c:197
Definition: nrf24.h:72
void nRF24_Read(nrf24_t *nrf_object, uint8_t *buf, uint8_t length)
uint8_t unused
Definition: nrf24.h:50
Definition: nrf24.h:74
Definition: nrf24.h:61
void nRF24_SetCRCMode(nrf24_t *nrf_object, nrf24_crc_mode_e mode)
Definition: nrf24.c:161
nrf24_datarate_e
Definition: nrf24.h:65
Definition: nrf24.h:59
uint8_t last_rx_pipe
Definition: nrf24.h:54
void nRF24_SetActive(nrf24_t *nrf_object, uint8_t active)
Definition: nrf24.c:290
void nRF24_FlushRx(nrf24_t *nrf_object)
Definition: nrf24.c:303
void nRF24_FlushTx(nrf24_t *nrf_object)
Definition: nrf24.c:317
void nRF24_Standby(nrf24_t *nrf_object)
Definition: nrf24.c:122
void(* AckReceived)(void)
Definition: nrf24.h:40
void(* csn)(uint8_t)
Set SPI Ship Select Function Pointer.
Definition: nrf24.h:39
void nRF24_SetDataRate(nrf24_t *nrf_object, nrf24_datarate_e rate)
Definition: nrf24.c:143
uint64_t last_tx_address
Definition: nrf24.h:53
void nRF24_SetRetries(nrf24_t *nrf_object, uint8_t delay, uint8_t count)
Definition: nrf24.c:139
Definition: nrf24.h:37
const int delay
Definition: hal_seven_seg.c:25
uint8_t aw
Definition: nrf24.h:49
Definition: nrf24.h:67
void nRF24_WriteMultReg(nrf24_t *nrf_object, uint8_t reg_address, uint8_t *data_ptr, uint8_t length)
Definition: nrf24.c:345
void(* MaxRetriesHit)(void)
Definition: nrf24.h:42
void(* ReceivedPayload)(uint8_t *data, uint8_t length)
Definition: nrf24.h:43
void nRF24_EventHandler(nrf24_t *nrf_object)
Definition: nrf24.c:211
void nRF24_WriteReg(nrf24_t *nrf_object, uint8_t reg_address, uint8_t value)
Definition: nrf24.c:331
struct nrf24_radio_t::settings settings
uint8_t nRF24_GetPayloadLength(nrf24_t *nrf_object)
Definition: nrf24.c:377
nrf24_crc_mode_e
Definition: nrf24.h:71
Definition: nrf24.h:62
nrf24_pa_level_e
Definition: nrf24.h:58
void nRF24_SetPowerAmplificationLevel(nrf24_t *nrf_object, nrf24_pa_level_e level)
Definition: nrf24.c:129
Definition: nrf24.h:60
void nRF24_Write(nrf24_t *nrf_object, uint8_t *buf, uint8_t length)
Definition: nrf24.c:87
void nRF24_OpenRxPipe(nrf24_t *nrf_object, uint8_t pipenum, uint64_t address)
Definition: nrf24.c:175
void nRF24_Init(nrf24_t *nrf_object)
Definition: nrf24.c:8
void nRF24_SetChannel(nrf24_t *nrf_object, uint8_t channel)
Definition: nrf24.c:171
Definition: nrf24.h:68
Definition: nrf24.h:73
struct nrf24_radio_t nrf24_t
void nRF24_StartListening(nrf24_t *nrf_object)
Definition: nrf24.c:69
uint64_t rx_address_p0
Definition: nrf24.h:52