embeddedlibrary
reusable software modules for embedded systems
nrf24.h
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 
24 #ifndef _NRF24_H_
25 #define _NRF24_H_
26 
27 #include <stdint.h>
28 
29 typedef struct nrf24_radio_t {
30  void (*ce)(uint8_t);
31  void (*csn)(uint8_t);
32  void (*AckReceived)(void); // should be called when in PTX mode and TX_DS flag is set
33  void (*AckPayloadReceived)(uint8_t * data, uint8_t length);
34  void (*MaxRetriesHit)(void);
35  void (*ReceivedPayload)(uint8_t * data, uint8_t length);
36  void (*AckPayloadSent)(void);
37  uint8_t state;
38  uint8_t spi_channel;
39  struct settings {
40  uint8_t prim_rx : 1;
41  uint8_t aw : 2;
42  uint8_t unused : 5;
43  } settings;
44  uint64_t rx_address_p0;
45  uint64_t last_tx_address;
46 } nrf24_t;
47 
48 
49 typedef enum {
50  NRF24_PA_LOW = 0, // -18dBm / 7.0mA transmit
51  NRF24_PA_MID, // -12dBm / 7.5mA transmit
52  NRF24_PA_HIGH, // -6dBm / 9mA transmit
53  NRF24_PA_MAX // 0dBm / 11.3mA transmit
54 } nrf24_pa_level_e;
55 
56 typedef enum {
57  NRF24_250KBPS = 0,
58  NRF24_1MBPS,
59  NRF24_2MBPS
60 } nrf24_datarate_e;
61 
62 typedef enum {
63  NRF24_NO_CRC = 0,
64  NRF24_CRC_8BIT,
65  NRF24_CRC_16BIT
66 } nrf24_crc_mode_e;
67 
68 //METHODS ADDED BY MM ON 4/18/15
69 void nRF24_Init(nrf24_t * nrf_object);
70 void nRF24_OpenRxPipe(nrf24_t * nrf_object, uint8_t pipeNum, uint64_t address);
71 void nRF24_OpenTxPipe(nrf24_t * nrf_object, uint64_t address);
72 //void nRF24_EnableAckPayload(void);
73 //void nRF24_EnableDynamicPayloads(void);
74 void nRF24_EventHandler(nrf24_t * nrf_object);
75 #define nRF24_ISR(nrf_object) Task_Queue( (task_fn_t)nRF24_EventHandler, nrf_object)
76 
77 void nRF24_StartListening(nrf24_t * nrf_object);
78 void nRF24_Standby(nrf24_t * nrf_object);
79 
80 void nRF24_WriteReg(nrf24_t * nrf_object, uint8_t reg_address, uint8_t value);
81 void nRF24_WriteMultReg(nrf24_t * nrf_object, uint8_t reg_address, uint8_t *data_ptr, uint8_t length);
82 
83 uint8_t nRF24_ReadReg(nrf24_t * nrf_object, uint8_t reg_address);
84 uint8_t nRF24_GetPayloadLength(nrf24_t * nrf_object);
85 
86 void nRF24_Write(nrf24_t * nrf_object, uint8_t * buf, uint8_t length);
87 void nRF24_Read(nrf24_t * nrf_object, uint8_t * buf, uint8_t length);
88 void nRF24_WriteAck(nrf24_t * nrf_object, uint8_t * buf, uint8_t length, uint8_t pipe);
89 
90 void nRF24_SetPowerAmplificationLevel(nrf24_t * nrf_object, nrf24_pa_level_e level);
91 void nRF24_SetDataRate(nrf24_t * nrf_object, nrf24_datarate_e rate);
92 void nRF24_SetCRCMode(nrf24_t * nrf_object, nrf24_crc_mode_e mode);
93 void nRF24_SetChannel(nrf24_t * nrf_object, uint8_t channel);
94 void nRF24_SetActive(nrf24_t * nrf_object, uint8_t active);
95 void nRF24_SetRetries(nrf24_t * nrf_object, uint8_t delay, uint8_t count);
96 void nRF24_FlushRx(nrf24_t * nrf_object);
97 void nRF24_FlushTx(nrf24_t * nrf_object);
98 
99 /* Memory Map */
100 #define CONFIG 0x00
101 #define EN_AA 0x01
102 #define EN_RXADDR 0x02
103 #define SETUP_AW 0x03
104 #define SETUP_RETR 0x04
105 #define RF_CH 0x05
106 #define RF_SETUP 0x06
107 #define STATUS 0x07
108 #define OBSERVE_TX 0x08
109 #define CD 0x09
110 #define RX_ADDR_P0 0x0A
111 #define RX_ADDR_P1 0x0B
112 #define RX_ADDR_P2 0x0C
113 #define RX_ADDR_P3 0x0D
114 #define RX_ADDR_P4 0x0E
115 #define RX_ADDR_P5 0x0F
116 #define TX_ADDR 0x10
117 #define RX_PW_P0 0x11
118 #define RX_PW_P1 0x12
119 #define RX_PW_P2 0x13
120 #define RX_PW_P3 0x14
121 #define RX_PW_P4 0x15
122 #define RX_PW_P5 0x16
123 #define FIFO_STATUS 0x17
124 #define DYNPD 0x1C
125 #define FEATURE 0x1D
126 
127 /* Helpers - Memory Macros */
128 #define RX_ADDR_BASE RX_ADDR_P0
129 
130 #define AW_5B 0x03 // 5 byte address
131 #define AW_4B 0x02 // 4 byte address
132 #define AW_3B 0x01 // 3 byte address
133 
134 // Bit Mnemonics ----------------------
135 // CONFIG
136 #define MASK_RX_DR 0x40
137 #define MASK_TX_DS 0x20
138 #define MASK_MAX_RT 0x10
139 #define EN_CRC 0x08
140 #define CRCO 0x04
141 #define PWR_UP 0x02
142 #define PRIM_RX 0x01
143 // EN_AA
144 #define ENAA_P5 0x20
145 #define ENAA_P4 0x10
146 #define ENAA_P3 0x08
147 #define ENAA_P2 0x04
148 #define ENAA_P1 0x02
149 #define ENAA_P0 0x01
150 // EN_RXADDR
151 #define ERX_P5 0x20
152 #define ERX_P4 0x10
153 #define ERX_P3 0x08
154 #define ERX_P2 0x04
155 #define ERX_P1 0x02
156 #define ERX_P0 0x01
157 // SETUP_AW
158 #define AW 0x01
159 // SETUP_RETR
160 #define ARD 0x10
161 #define ARC 0x01
162 // RF_SETUP
163 #define CONT_WAVE 0x80
164 #define RF_DR_LOW 0x20
165 #define PLL_LOCK 0x10
166 #define RF_DR_HIGH 0x08
167 #define RF_PA_HIGH 0x04
168 #define RF_PA_LOW 0x02
169 // STATUS
170 #define RX_DR 0x40
171 #define TX_DS 0x20
172 #define MAX_RT 0x10
173 #define RX_P_NO 0x02
174 #define TX_FULL 0x01
175 // OBSERVE_TX
176 #define PLOS_CNT 0x10
177 #define ARC_CNT 0x01
178 // FIFO_STATUS
179 #define TX_REUSE 0x40
180 #define TX_FULL_FIFO 0x20 // annoyingly this has the same mnemonic as in STATUS
181 #define TX_EMPTY 0x10
182 #define RX_FULL 0x02
183 #define RX_EMPTY 0x01
184 // DYNPD
185 #define DPL_P5 0x20
186 #define DPL_P4 0x10
187 #define DPL_P3 0x08
188 #define DPL_P2 0x04
189 #define DPL_P1 0x02
190 #define DPL_P0 0x01
191 // FEATURE
192 #define EN_DPL 0x04
193 #define EN_ACK_PAY 0x02
194 #define EN_DYN_ACK 0x01
195 
196 // SPI Commands ----------------------
197 #define REGISTER_MASK 0x1F
198 #define R_REGISTER 0x00
199 #define W_REGISTER 0x20
200 #define R_RX_PAYLOAD 0x61
201 #define W_TX_PAYLOAD 0xA0
202 #define FLUSH_TX 0xE1
203 #define FLUSH_RX 0xE2
204 #define REUSE_TX_PL 0xE3
205 #define ACTIVATE 0x50
206 #define R_RX_PL_WID 0x60
207 #define W_ACK_PAYLOAD 0xA8
208 #define W_TX_PAYLOAD_NO_ACK 0xB0
209 #define NOP 0xFF
210 
211 #endif //_NRF24_H_
void(* csn)(uint8_t)
Set SPI Ship Select Function Pointer.
Definition: nrf24.h:31
Definition: nrf24.h:39
void(* ce)(uint8_t)
Set Chip Enable Function Pointer.
Definition: nrf24.h:30
uint8_t state
state of radio
Definition: nrf24.h:37
Definition: nrf24.h:29