nrf24l01+ Library
nrf24l01.h
Go to the documentation of this file.
1 
10  #define _SUPPRESS_PLIB_WARNING 1
11  #include <plib.h>
12  #include "inttypes.h"
13 
14 #define _csn LATBbits.LATB9
15 #define TRIS_csn TRISBbits.TRISB9
16 
17 #define _ce LATBbits.LATB8
18 #define TRIS_ce TRISBbits.TRISB8
19 
20 // Credit to S. Brennen Ball for the register definitions
21 // https://github.com/fffaraz/Introduction-to-Microprocessors/blob/master/material/atmel/sample_code/nordic1/nrf24l01.h
22 
23 // SPI Commands
24 #define nrf24l01_R_REGISTER 0x00
25 #define nrf24l01_W_REGISTER 0x20
26 #define nrf24l01_R_REGISTER_WID 0x61
27 #define nrf24l01_R_RX_PL_WID 0x60
28 #define nrf24l01_R_RX_PAYLOAD 0x61
29 #define nrf24l01_W_TX_PAYLOAD 0xA0
30 #define nrf24l01_FLUSH_TX 0xE1
31 #define nrf24l01_FLUSH_RX 0xE2
32 #define nrf24l01_REUSE_TX_PL 0xE3
33 #define nrf24l01_NOP 0xFF
34 
35 
36 // Register definitions
37 #define nrf24l01_CONFIG 0x00
38 #define nrf24l01_EN_AA 0x01
39 #define nrf24l01_EN_RXADDR 0x02
40 #define nrf24l01_SETUP_AW 0x03
41 #define nrf24l01_SETUP_RETR 0x04
42 #define nrf24l01_RF_CH 0x05
43 #define nrf24l01_RF_SETUP 0x06
44 #define nrf24l01_STATUS 0x07
45 #define nrf24l01_OBSERVE_TX 0x08
46 #define nrf24l01_RPD 0x09
47 #define nrf24l01_RX_ADDR_P0 0x0A
48 #define nrf24l01_RX_ADDR_P1 0x0B
49 #define nrf24l01_RX_ADDR_P2 0x0C
50 #define nrf24l01_RX_ADDR_P3 0x0D
51 #define nrf24l01_RX_ADDR_P4 0x0E
52 #define nrf24l01_RX_ADDR_P5 0x0F
53 #define nrf24l01_TX_ADDR 0x10
54 #define nrf24l01_RX_PW_P0 0x11
55 #define nrf24l01_RX_PW_P1 0x12
56 #define nrf24l01_RX_PW_P2 0x13
57 #define nrf24l01_RX_PW_P3 0x14
58 #define nrf24l01_RX_PW_P4 0x15
59 #define nrf24l01_RX_PW_P5 0x16
60 #define nrf24l01_FIFO_STATUS 0x17
61 #define nrf24l01_DYNPD 0x1C
62 #define nrf24l01_FEATURE 0x1D
63 
64 //CONFIG register bitwise definitions
65 #define nrf24l01_CONFIG_RESERVED 0x80
66 #define nrf24l01_CONFIG_MASK_RX_DR 0x40
67 #define nrf24l01_CONFIG_MASK_TX_DS 0x20
68 #define nrf24l01_CONFIG_MASK_MAX_RT 0x10
69 #define nrf24l01_CONFIG_EN_CRC 0x08
70 #define nrf24l01_CONFIG_CRCO 0x04
71 #define nrf24l01_CONFIG_PWR_UP 0x02
72 #define nrf24l01_CONFIG_PRIM_RX 0x01
73 
74 //EN_AA register bitwise definitions
75 #define nrf24l01_EN_AA_RESERVED 0xC0
76 #define nrf24l01_EN_AA_ENAA_ALL 0x3F
77 #define nrf24l01_EN_AA_ENAA_P5 0x20
78 #define nrf24l01_EN_AA_ENAA_P4 0x10
79 #define nrf24l01_EN_AA_ENAA_P3 0x08
80 #define nrf24l01_EN_AA_ENAA_P2 0x04
81 #define nrf24l01_EN_AA_ENAA_P1 0x02
82 #define nrf24l01_EN_AA_ENAA_P0 0x01
83 #define nrf24l01_EN_AA_ENAA_NONE 0x00
84 
85 //EN_RXADDR register bitwise definitions
86 #define nrf24l01_EN_RXADDR_RESERVED 0xC0
87 #define nrf24l01_EN_RXADDR_ERX_ALL 0x3F
88 #define nrf24l01_EN_RXADDR_ERX_P5 0x20
89 #define nrf24l01_EN_RXADDR_ERX_P4 0x10
90 #define nrf24l01_EN_RXADDR_ERX_P3 0x08
91 #define nrf24l01_EN_RXADDR_ERX_P2 0x04
92 #define nrf24l01_EN_RXADDR_ERX_P1 0x02
93 #define nrf24l01_EN_RXADDR_ERX_P0 0x01
94 #define nrf24l01_EN_RXADDR_ERX_NONE 0x00
95 
96 //SETUP_AW register bitwise definitions
97 #define nrf24l01_SETUP_AW_RESERVED 0xFC
98 #define nrf24l01_SETUP_AW 0x03
99 #define nrf24l01_SETUP_AW_5BYTES 0x03
100 #define nrf24l01_SETUP_AW_4BYTES 0x02
101 #define nrf24l01_SETUP_AW_3BYTES 0x01
102 #define nrf24l01_SETUP_AW_ILLEGAL 0x00
103 
104 //SETUP_RETR register bitwise definitions
105 #define nrf24l01_SETUP_RETR_ARD 0xF0
106 #define nrf24l01_SETUP_RETR_ARD_4000 0xF0
107 #define nrf24l01_SETUP_RETR_ARD_3750 0xE0
108 #define nrf24l01_SETUP_RETR_ARD_3500 0xD0
109 #define nrf24l01_SETUP_RETR_ARD_3250 0xC0
110 #define nrf24l01_SETUP_RETR_ARD_3000 0xB0
111 #define nrf24l01_SETUP_RETR_ARD_2750 0xA0
112 #define nrf24l01_SETUP_RETR_ARD_2500 0x90
113 #define nrf24l01_SETUP_RETR_ARD_2250 0x80
114 #define nrf24l01_SETUP_RETR_ARD_2000 0x70
115 #define nrf24l01_SETUP_RETR_ARD_1750 0x60
116 #define nrf24l01_SETUP_RETR_ARD_1500 0x50
117 #define nrf24l01_SETUP_RETR_ARD_1250 0x40
118 #define nrf24l01_SETUP_RETR_ARD_1000 0x30
119 #define nrf24l01_SETUP_RETR_ARD_750 0x20
120 #define nrf24l01_SETUP_RETR_ARD_500 0x10
121 #define nrf24l01_SETUP_RETR_ARD_250 0x00
122 #define nrf24l01_SETUP_RETR_ARC 0x0F
123 #define nrf24l01_SETUP_RETR_ARC_15 0x0F
124 #define nrf24l01_SETUP_RETR_ARC_14 0x0E
125 #define nrf24l01_SETUP_RETR_ARC_13 0x0D
126 #define nrf24l01_SETUP_RETR_ARC_12 0x0C
127 #define nrf24l01_SETUP_RETR_ARC_11 0x0B
128 #define nrf24l01_SETUP_RETR_ARC_10 0x0A
129 #define nrf24l01_SETUP_RETR_ARC_9 0x09
130 #define nrf24l01_SETUP_RETR_ARC_8 0x08
131 #define nrf24l01_SETUP_RETR_ARC_7 0x07
132 #define nrf24l01_SETUP_RETR_ARC_6 0x06
133 #define nrf24l01_SETUP_RETR_ARC_5 0x05
134 #define nrf24l01_SETUP_RETR_ARC_4 0x04
135 #define nrf24l01_SETUP_RETR_ARC_3 0x03
136 #define nrf24l01_SETUP_RETR_ARC_2 0x02
137 #define nrf24l01_SETUP_RETR_ARC_1 0x01
138 #define nrf24l01_SETUP_RETR_ARC_0 0x00
139 
140 //RF_CH register bitwise definitions
141 #define nrf24l01_RF_CH_RESERVED 0x80
142 
143 //RF_SETUP register bitwise definitions
144 #define nrf24l01_RD_SETUP_CONT_WAVE 0x80
145 #define nrf24l01_RF_SETUP_RESERVED 0xE0
146 #define nrf24l01_RF_SETUP_PLL_LOCK 0x10
147 #define nrf24l01_RF_SETUP_RF_DR 0x08
148 #define nrf24l01_RF_SETUP_RF_PWR 0x06
149 #define nrf24l01_RF_SETUP_RF_PWR_0 0x06
150 #define nrf24l01_RF_SETUP_RF_PWR_6 0x04
151 #define nrf24l01_RF_SETUP_RF_PWR_12 0x02
152 #define nrf24l01_RF_SETUP_RF_PWR_18 0x00
153 #define nrf24l01_RF_SETUP_LNA_HCURR 0x01
154 
155 //STATUS register bitwise definitions
156 #define nrf24l01_STATUS_RESERVED 0x80
157 #define nrf24l01_STATUS_RX_DR 0x40
158 #define nrf24l01_STATUS_TX_DS 0x20
159 #define nrf24l01_STATUS_MAX_RT 0x10
160 #define nrf24l01_STATUS_RX_P_NO 0x0E
161 #define nrf24l01_STATUS_RX_P_NO_RX_FIFO_NOT_EMPTY 0x0E
162 #define nrf24l01_STATUS_RX_P_NO_UNUSED 0x0C
163 #define nrf24l01_STATUS_RX_P_NO_5 0x0A
164 #define nrf24l01_STATUS_RX_P_NO_4 0x08
165 #define nrf24l01_STATUS_RX_P_NO_3 0x06
166 #define nrf24l01_STATUS_RX_P_NO_2 0x04
167 #define nrf24l01_STATUS_RX_P_NO_1 0x02
168 #define nrf24l01_STATUS_RX_P_NO_0 0x00
169 #define nrf24l01_STATUS_TX_FULL 0x01
170 
171 //OBSERVE_TX register bitwise definitions
172 #define nrf24l01_OBSERVE_TX_PLOS_CNT 0xF0
173 #define nrf24l01_OBSERVE_TX_ARC_CNT 0x0F
174 
175 //CD register bitwise definitions
176 #define nrf24l01_CD_RESERVED 0xFE
177 #define nrf24l01_CD_CD 0x01
178 
179 //RX_PW_P0 register bitwise definitions
180 #define nrf24l01_RX_PW_P0_RESERVED 0xC0
181 
182 //RX_PW_P0 register bitwise definitions
183 #define nrf24l01_RX_PW_P0_RESERVED 0xC0
184 
185 //RX_PW_P1 register bitwise definitions
186 #define nrf24l01_RX_PW_P1_RESERVED 0xC0
187 
188 //RX_PW_P2 register bitwise definitions
189 #define nrf24l01_RX_PW_P2_RESERVED 0xC0
190 
191 //RX_PW_P3 register bitwise definitions
192 #define nrf24l01_RX_PW_P3_RESERVED 0xC0
193 
194 //RX_PW_P4 register bitwise definitions
195 #define nrf24l01_RX_PW_P4_RESERVED 0xC0
196 
197 //RX_PW_P5 register bitwise definitions
198 #define nrf24l01_RX_PW_P5_RESERVED 0xC0
199 
200 //FIFO_STATUS register bitwise definitions
201 #define nrf24l01_FIFO_STATUS_RESERVED 0x8C
202 #define nrf24l01_FIFO_STATUS_TX_REUSE 0x40
203 #define nrf24l01_FIFO_STATUS_TX_FULL 0x20
204 #define nrf24l01_FIFO_STATUS_TX_EMPTY 0x10
205 #define nrf24l01_FIFO_STATUS_RX_FULL 0x02
206 #define nrf24l01_FIFO_STATUS_RX_EMPTY 0x01
207 
208 // RF power definitions
209 #define nrf24l01_DR_LOW 0x20
210 #define nrf24l01_DR_MED 0x00
211 #define nrf24l01_DR_HIGH 0x08
212 
213 // Send no data for when you read data from the radio
214 #define nrf24l01_SEND_CLOCK 0x00
215 
216 // State Definitions
217 #define PWR_DOWN 0
218 #define STANDBY_1 1
219 #define RX_MODE 2
220 #define TX_MODE 3
221 
222 #define PBCLK 40000000 // peripheral bus clock
223 
224 #define dTime_ms PBCLK/2000
225 #define dTime_us PBCLK/2000000
226 
227 
228 static char status;
229 static volatile char RX_payload[32];
230 static char payload_size;
231 static int pipe_no;
232 static int width;
234 static int state;
236 static volatile int received;
237 static volatile int sent;
238 static volatile int error;
257 char rf_spiwrite(unsigned char c);
263 void init_SPI();
264 
271 void nrf_setup();
272 
281 void nrf_read_reg(char reg, char * buff, int len);
282 
290 void nrf_write_reg(char reg, char * data, char len);
291 
295 void nrf_flush_tx();
296 
300 void nrf_flush_rx();
301 
312 
319 void nrf_write_payload(char * data, char len);
320 
326 void nrf_read_payload(char * buff);
327 
343 int nrf_get_payload(char * buff, char len);
356 
364 int nrf_get_pipe();
365 
373 int nrf_get_width();
374 
375 
380 void nrf_pwrup();
381 
386 void nrf_pwrdown();
387 
393 void nrf_state_pwr_down();
394 
400 void nrf_state_standby_1();
401 
407 void nrf_state_rx_mode();
408 
412 void nrf_set_prim_rx();
413 
417 void nrf_clear_prim_rx();
418 
432 void nrf_set_transmit_pwr(char power);
433 
446 void nrf_set_transmit_rate(char rate);
447 
458 void nrf_set_ard(char ard);
459 
470 void nrf_set_arc(char arc);
471 
486 void nrf_set_rf_ch(char ch);
487 
497 char nrf_received_pipe_num();
498 
509 void nrf_set_address_width(char width);
510 
525 void nrf_start_cont_wave(char pwr);
526 
535 void nrf_stop_cont_wave();
536 
549 char nrf_received_pwr();
550 
561 void nrf_en_aa(int pipe);
562 
570 void nrf_dis_aa(int pipe);
571 
582 void nrf_en_rxaddr(int pipe);
583 
591 void nrf_dis_rxaddr(int pipe);
592 
609 void nrf_set_pw(char width, int pipe);
610 
624 void nrf_en_dpl(int pipe);
625 
636 void nrf_dis_dpl(int pipe);
637 
645 void nrf_en_dyn_ack();
646 
653 void nrf_dis_dyn_ack();
654 
676 int nrf_set_rx_addr(int pipe, uint64_t address, int len);
677 
690 void nrf_set_tx_addr(uint64_t address);
691 
692 //\cond
693 // Helper function for parsing addresses into a buffer
694 char * parse_addr(uint64_t address);
695 
696 
700 void nrf_en_ack_pay();
701 
705 void nrf_dis_ack_pay();
706 //\endcond
707 
714 void nrf_reset();
715 
716 //\cond
727 int nrf_send_payload_nonblock(char * data, char len);
728 //\endcond
729 
746 int nrf_send_payload(char * data, char len);
747 
748 //\cond
749 //From tft_master.h, by Syed Tahmid Mahbub
750 void nrf_delay_ms(unsigned long);
751 
752 //From tft_master.h, by Syed Tahmid Mahbub
753 void nrf_delay_us(unsigned long);
754 //\endcond
void nrf_dis_dpl(int pipe)
Disable dynamic payload length for a pipe.
Definition: nrf24l01.c:491
void nrf_set_address_width(char width)
Set the address width of RX and TX pipes.
Definition: nrf24l01.c:364
void nrf_set_prim_rx()
Set the PRIM_RX bit in the CONFIG register.
Definition: nrf24l01.c:300
void nrf_en_dpl(int pipe)
Enable dynamic payload length for a pipe.
Definition: nrf24l01.c:475
void nrf_set_transmit_pwr(char power)
Set power of transmitter.
Definition: nrf24l01.c:319
void nrf_set_rf_ch(char ch)
Set the RF frequency the radio will operate at.
Definition: nrf24l01.c:355
int nrf_get_width()
Get the width of the most recently received payload.
Definition: nrf24l01.c:219
void nrf_clear_prim_rx()
Clear the PRIM_RX bit in the CONFIG register.
Definition: nrf24l01.c:307
void nrf_start_cont_wave(char pwr)
Send a constant carrier wave out at specified power.
Definition: nrf24l01.c:404
void nrf_read_reg(char reg, char *buff, int len)
Read a register and store the data in an array. Can be multiple bytes of data.
Definition: nrf24l01.c:79
void nrf_set_transmit_rate(char rate)
Set data rate.
Definition: nrf24l01.c:331
void nrf_state_standby_1()
Put the radio in the standby 1 state.
Definition: nrf24l01.c:259
void nrf_setup()
Sets up the radio, SPI, and interrupts. Also resets all radio registers to their default values...
Definition: nrf24l01.c:59
void nrf_state_rx_mode()
Put the radio in the rx mode state.
Definition: nrf24l01.c:276
void nrf_flush_rx()
Flush the RX FIFO.
Definition: nrf24l01.c:112
void nrf_pwrup()
Sets the power up bit in the status register in order to leave the power down state.
Definition: nrf24l01.c:224
void nrf_flush_tx()
Flush the TX FIFO.
Definition: nrf24l01.c:103
void nrf_set_arc(char arc)
Set the auto retransmit count.
Definition: nrf24l01.c:347
char nrf_received_pipe_num()
Returns the pipe data is available in.
Definition: nrf24l01.c:359
char nrf_received_pwr()
Check the power of the signal the nrf42l01 is receiving.
Definition: nrf24l01.c:423
int nrf_get_payload(char *buff, char len)
Read a received payload.
Definition: nrf24l01.c:196
int nrf_get_pipe()
Get the number of the pipe the most recent payload was received on.
Definition: nrf24l01.c:214
void nrf_en_dyn_ack()
Enable dynamic auto-acknowledgements.
Definition: nrf24l01.c:508
void nrf_set_ard(char ard)
Set the auto retransmit delay.
Definition: nrf24l01.c:339
int nrf_send_payload(char *data, char len)
Send a payload over the radio.
Definition: nrf24l01.c:556
void nrf_en_aa(int pipe)
Enable auto-acknowledge for a pipe.
Definition: nrf24l01.c:430
int nrf_get_payload_width()
Get the width of the top payload in the RX FIFO.
Definition: nrf24l01.c:142
void nrf_pwrdown()
Clears the power up bit in the status register in order to enter the power down state.
Definition: nrf24l01.c:233
int nrf_payload_available()
Check if a payload is available to be read.
Definition: nrf24l01.c:210
int nrf_set_rx_addr(int pipe, uint64_t address, int len)
Set the address of a pipe.
Definition: nrf24l01.c:378
void nrf_dis_dyn_ack()
Disable dynamic auto-acknowledgements.
Definition: nrf24l01.c:515
void nrf_reset()
Resets all registers to their default values as listed on the datasheet.
Definition: nrf24l01.c:573
void init_SPI()
Set up SPI for the radio.
Definition: nrf24l01.c:50
void nrf_read_payload(char *buff)
Read a payload from the RX FIFO.
Definition: nrf24l01.c:167
void nrf_stop_cont_wave()
Stop sending the carrier wave.
Definition: nrf24l01.c:414
void nrf_en_rxaddr(int pipe)
Enable a pipe to receive packets.
Definition: nrf24l01.c:451
void nrf_set_pw(char width, int pipe)
Set the width received static payloads should be.
Definition: nrf24l01.c:470
void nrf_set_tx_addr(uint64_t address)
Set the address for transmitting.
Definition: nrf24l01.c:399
void nrf_state_pwr_down()
Put the radio in the power down state.
Definition: nrf24l01.c:241
void nrf_write_reg(char reg, char *data, char len)
Write to a register from an array. Can be multiple bytes of data.
Definition: nrf24l01.c:92
void nrf_dis_rxaddr(int pipe)
Disable a pipe from receiving packets.
Definition: nrf24l01.c:460
void nrf_dis_aa(int pipe)
Disable auto-acknowledge for a pipe.
Definition: nrf24l01.c:441
void nrf_write_payload(char *data, char len)
Write a payload to the TX FIFO.
Definition: nrf24l01.c:154