Skip to content

Commit f8180ac

Browse files
committed
README and comment
1 parent 625176a commit f8180ac

File tree

8 files changed

+1467
-2
lines changed

8 files changed

+1467
-2
lines changed

NRF24.c

+21-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* 25-JUL-2024
3+
* STM32 HAL NRF24 LIBRARY
4+
*/
5+
16
#include <stdio.h>
27
#include "stm32f1xx_hal.h"
38
#include "NRF24_conf.h"
@@ -415,6 +420,22 @@ void nrf24_auto_retr_limit(uint8_t limit){
415420
nrf24_w_reg(SETUP_RETR, &data, 1);
416421
}
417422

423+
void nrf24_type_to_uint8_t(size_t in, uint8_t* out, uint16_t size){
424+
for(uint16_t i = 0; i < size; i++){
425+
out[i] = (((in & (255 << (i*8)))) >> (i*8));
426+
}
427+
}
428+
429+
size_t nrf24_uint8_t_to_type(uint8_t* in, uint16_t size){
430+
size_t out = 0;
431+
432+
for(uint16_t i = 0; i < size; i++){
433+
out |= (in[i] << (8*i));
434+
}
435+
436+
return out;
437+
}
438+
418439
uint8_t nrf24_transmit(uint8_t *data, uint8_t size){
419440

420441
ce_low();
@@ -435,7 +456,6 @@ uint8_t nrf24_transmit(uint8_t *data, uint8_t size){
435456
HAL_Delay(1);
436457
ce_low();
437458

438-
439459
if(nrf24_read_bit(STATUS, MAX_RT) == 1){
440460
return 1;
441461
}
@@ -548,4 +568,3 @@ void nrf24_init(void){
548568
nrf24_clear_max_rt();
549569
}
550570

551-

NRF24.h

+247
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
/*
2+
* 25-JUL-2024
3+
* STM32 HAL NRF24 LIBRARY
4+
*
5+
*/
6+
7+
18
#ifndef NRF_24_H
29
#define NRF_24_H
310

@@ -34,66 +41,306 @@ enum {
3441
disable = 0
3542
};
3643

44+
//These functions are for controll CE and CSN pins which are selected in NRF24_conf.h
3745
void csn_high(void);
3846
void csn_low(void);
3947
void ce_high(void);
4048
void ce_low(void);
4149

50+
51+
//For read or write nrf24 registers via spi bus
4252
void nrf24_w_reg(uint8_t reg, uint8_t *data, uint8_t size);
4353
uint8_t nrf24_r_reg(uint8_t reg, uint8_t size);
4454

55+
56+
//For write some special commands such as W_TX_PAYLOAD, FLUSH_RX, ETC
4557
void nrf24_w_spec_cmd(uint8_t cmd);
58+
59+
60+
/* One of these functions are used after "nrf24_w_spec_cmd" for execute
61+
* command which means reading or writing relevant Buffer. this functions
62+
* are not necessary after all special commands such as FLUSH_TX or FLUSH_RX
63+
*/
4664
void nrf24_w_spec_reg(uint8_t *data, uint8_t size);
4765
void nrf24_r_spec_reg(uint8_t *data, uint8_t size);
4866

67+
68+
//Power up nrf24 module
4969
void nrf24_pwr_up(void);
70+
71+
72+
//Power down nrf24 module
5073
void nrf24_pwr_dwn(void);
5174

75+
76+
/* Control nrf24 TX power:
77+
*
78+
* n18dbm = 0 -> Minimum power
79+
*
80+
* n12dbm = 1
81+
*
82+
* n6dbm = 2
83+
*
84+
* _0dbm = 3 -> Maximum power
85+
*
86+
*/
5287
void nrf24_tx_pwr(uint8_t pwr);
88+
89+
90+
/* Set NRF24 data rate:
91+
*
92+
* _1mbps = 0
93+
*
94+
* _2mbps = 1
95+
*
96+
* _250kbps = 2
97+
*
98+
*/
5399
void nrf24_data_rate(uint8_t bps);
100+
101+
102+
/*Set channel from 0 to 125 (In summary it has 126 channel from 1400Mhz to 1525Mhz)
103+
*
104+
* Frequency -> channel
105+
*
106+
* 1400Mhz -> 0
107+
* 1401Mhz -> 1
108+
* ....
109+
* 1525Mhz -> 125
110+
*/
54111
void nrf24_set_channel(uint8_t ch);
55112

113+
114+
/*Write here pipe address, for example:
115+
* uint8_t addr[5] = { 0x53, 0x13, 0x01, 0x75, 0x82 };
116+
*/
56117
void nrf24_open_tx_pipe(uint8_t *addr);
118+
119+
120+
/*
121+
* Control payload size for each pipe individually (from 0 to 32)
122+
*/
57123
void nrf24_pipe_pld_size(uint8_t pipe, uint8_t size);
124+
125+
126+
/*
127+
* Open and set rx pipe address (Rx pipes are from 0 to 5)
128+
*
129+
* NOTICE: if you are using acknowledgement feature it is obligatory
130+
* to set same address to TX and RX 0 pipe.
131+
*/
58132
void nrf24_open_rx_pipe(uint8_t pipe, uint8_t *addr);
133+
134+
135+
/*
136+
* Close opened RX pipe
137+
*/
59138
void nrf24_cls_rx_pipe(uint8_t pipe);
60139

140+
141+
/*
142+
* Set CRC:
143+
*
144+
* First argument:
145+
* no_crc = 0 -> Disable CRC
146+
* en_crc = 1 -> Enable CRC
147+
*
148+
* Second argument:
149+
* 1byte = 0
150+
*_2byte = 1
151+
*/
61152
void nrf24_set_crc(uint8_t en_crc, uint8_t crc0);
153+
154+
155+
/*
156+
* Set address width which as default is 5 bytes and it mostly doesn't
157+
* requires change but if you need to set it for example 3 bytes you
158+
* won't be able to use this: uint8_t addr[5] = { 0x53, 0x13, 0x01, 0x75, 0x82 };
159+
* you need to shorten it for instance: uint8_t addr[3] = { 0x53, 0x13, 0x01 };
160+
*/
62161
void nrf24_set_addr_width(uint8_t bytes);
63162

163+
164+
//This function is for clear TX FIFO fully
64165
void nrf24_flush_tx(void);
166+
167+
168+
//This function is for clear RX FIFO fully
65169
void nrf24_flush_rx(void);
66170

171+
172+
/*
173+
* This function is for clear RX_DR bit in NRF24 STATUS register which sets
174+
* after receiving data in RX FIFO and it must be cleared by writing 1 which
175+
* sets it to 0
176+
*/
67177
void nrf24_clear_rx_dr(void);
178+
179+
180+
/*
181+
* This function is for clear TX_DS bit in NRF24 STATUS register which sets
182+
* after Transmitting data or receiving acknowledgement and it must be cleared
183+
* by writing 1 which sets it to 0
184+
*/
68185
void nrf24_clear_tx_ds(void);
186+
187+
188+
/*
189+
* This function is for clear MAX_RT bit in NRF24 STATUS register which sets
190+
* after auto retransmissions will exceed limit and it must be cleared by writing 1 which
191+
* sets it to 0
192+
*/
69193
void nrf24_clear_max_rt(void);
70194

195+
196+
/*
197+
* Read individual bit in preferred register
198+
*/
71199
uint8_t nrf24_read_bit(uint8_t reg, uint8_t bit);
200+
201+
202+
/*
203+
* Set individual bit in preferred register
204+
*/
72205
void nrf24_set_bit(uint8_t reg, uint8_t bit, uint8_t val);
206+
207+
208+
/*
209+
* Read received payload size which is also useful for prevent data which size
210+
* is more than 32 bytes by flushing it "nrf24_flush_rx()"
211+
*/
73212
uint8_t nrf24_r_pld_wid(void);
74213

214+
215+
/*
216+
* This function turns nrf24 to RX mode also it sets CE pin HIGH
217+
*/
75218
void nrf24_listen(void);
219+
220+
221+
/*
222+
* This function is for turn off RX mode and go in TX mode
223+
*/
76224
void nrf24_stop_listen(void);
77225

226+
227+
/*
228+
* Enable or disable Dynamic PayLoad by writing 'enable' or 'disable' as an argument
229+
*/
78230
void nrf24_dpl(uint8_t en);
231+
232+
233+
/*
234+
* Set individual pipe for dynamic payload.
235+
* This function must be used after "nrf24_dpl(uint8_t en)" function
236+
*/
79237
void nrf24_set_rx_dpl(uint8_t pipe, uint8_t en);
80238

239+
240+
/*
241+
* Enable or disable auto acknowledgement for each pipe
242+
*/
81243
void nrf24_auto_ack(uint8_t pipe, uint8_t ack);
244+
245+
246+
/*
247+
* Enable or disable auto acknowledgement for all pipe together
248+
*/
82249
void nrf24_auto_ack_all(uint8_t ack);
250+
251+
252+
/*
253+
* Enable or disable acknowledgement with user defined payload
254+
*/
83255
void nrf24_en_ack_pld(uint8_t en);
256+
257+
258+
/*
259+
* Enable or disable "nrf24_transmit_no_ack"
260+
*/
84261
void nrf24_en_dyn_ack(uint8_t en);
262+
263+
264+
/*
265+
* Set delay between auto-retransmissions
266+
*/
85267
void nrf24_auto_retr_delay(uint8_t delay);
268+
269+
270+
/*
271+
* Set auto-retransmissions limit
272+
*/
86273
void nrf24_auto_retr_limit(uint8_t limit);
87274

275+
276+
/*
277+
* This function is used to convert the not uint8_t varible into uint8_t array which is
278+
* necessary for transmit correctly and on timing
279+
*/
280+
void nrf24_type_to_uint8_t(size_t in, uint8_t* out, uint16_t size);
281+
282+
283+
/*
284+
* This function converts received uint8_t array to other one data type for example
285+
* uint16_t, uint32_t
286+
*/
287+
size_t nrf24_uint8_t_to_type(uint8_t* in, uint16_t size);
288+
289+
290+
/*
291+
* Transmit data.
292+
* This function returns 0 if everything is OK but if MAX_RT has been set 1
293+
* which means target RX device does not responding, it returns 1, this is useful
294+
* with acknowledgements
295+
*/
88296
uint8_t nrf24_transmit(uint8_t *data, uint8_t size);
297+
298+
299+
/*
300+
* Transmit in auto_ack mode without request ack packet from RX device
301+
*/
89302
void nrf24_transmit_no_ack(uint8_t *data, uint8_t size);
303+
304+
305+
/*
306+
* Send ACk with payload packet as a receiver(RX) device
307+
*/
90308
void nrf24_transmit_rx_ack_pld(uint8_t pipe, uint8_t *data, uint8_t size);
91309

310+
311+
/*
312+
* Detect signal on selected channel above -64dbm
313+
*/
92314
uint8_t nrf24_carrier_detect(void);
315+
316+
317+
/*
318+
* check if data is in RX FIFO
319+
*/
93320
uint8_t nrf24_data_available(void);
321+
322+
323+
/*
324+
* Receive data
325+
*/
94326
void nrf24_receive(uint8_t *data, uint8_t size);
95327

328+
329+
/*
330+
* Set defaults
331+
*
332+
* NOTICE: Don't use it as a init at start of program because it may broke down process
333+
* after reseting the MCU. This function is for only special situations or testing.
334+
*/
96335
void nrf24_defaults(void);
336+
337+
338+
/*
339+
* This function sets everything which is vital for correct start of program
340+
*
341+
* NOTICE: Without this function in start of program may cause problems after reseting MCU.
342+
* MCU reset does not resets NRF24 module
343+
*/
97344
void nrf24_init(void);
98345

99346
#endif

NRF24_conf.h

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* 25-JUL-2024
3+
* STM32 HAL NRF24 LIBRARY
4+
*/
5+
16
#ifndef _NRF_24_CONF_H_
27
#define _NRF_24_CONF_H_
38

NRF24_reg_addresses.h

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* 25-JUL-2024
3+
* STM32 HAL NRF24 LIBRARY
4+
*/
5+
16
#ifndef _NRF_24_REG_ADDRESSES_H
27
#define _NRF_24_REG_ADDRESSES_H
38

0 commit comments

Comments
 (0)