|
| 1 | +/* |
| 2 | + * 25-JUL-2024 |
| 3 | + * STM32 HAL NRF24 LIBRARY |
| 4 | + * |
| 5 | + */ |
| 6 | + |
| 7 | + |
1 | 8 | #ifndef NRF_24_H
|
2 | 9 | #define NRF_24_H
|
3 | 10 |
|
@@ -34,66 +41,306 @@ enum {
|
34 | 41 | disable = 0
|
35 | 42 | };
|
36 | 43 |
|
| 44 | +//These functions are for controll CE and CSN pins which are selected in NRF24_conf.h |
37 | 45 | void csn_high(void);
|
38 | 46 | void csn_low(void);
|
39 | 47 | void ce_high(void);
|
40 | 48 | void ce_low(void);
|
41 | 49 |
|
| 50 | + |
| 51 | +//For read or write nrf24 registers via spi bus |
42 | 52 | void nrf24_w_reg(uint8_t reg, uint8_t *data, uint8_t size);
|
43 | 53 | uint8_t nrf24_r_reg(uint8_t reg, uint8_t size);
|
44 | 54 |
|
| 55 | + |
| 56 | +//For write some special commands such as W_TX_PAYLOAD, FLUSH_RX, ETC |
45 | 57 | 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 | + */ |
46 | 64 | void nrf24_w_spec_reg(uint8_t *data, uint8_t size);
|
47 | 65 | void nrf24_r_spec_reg(uint8_t *data, uint8_t size);
|
48 | 66 |
|
| 67 | + |
| 68 | +//Power up nrf24 module |
49 | 69 | void nrf24_pwr_up(void);
|
| 70 | + |
| 71 | + |
| 72 | +//Power down nrf24 module |
50 | 73 | void nrf24_pwr_dwn(void);
|
51 | 74 |
|
| 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 | + */ |
52 | 87 | 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 | + */ |
53 | 99 | 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 | + */ |
54 | 111 | void nrf24_set_channel(uint8_t ch);
|
55 | 112 |
|
| 113 | + |
| 114 | +/*Write here pipe address, for example: |
| 115 | + * uint8_t addr[5] = { 0x53, 0x13, 0x01, 0x75, 0x82 }; |
| 116 | + */ |
56 | 117 | void nrf24_open_tx_pipe(uint8_t *addr);
|
| 118 | + |
| 119 | + |
| 120 | +/* |
| 121 | + * Control payload size for each pipe individually (from 0 to 32) |
| 122 | + */ |
57 | 123 | 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 | + */ |
58 | 132 | void nrf24_open_rx_pipe(uint8_t pipe, uint8_t *addr);
|
| 133 | + |
| 134 | + |
| 135 | +/* |
| 136 | + * Close opened RX pipe |
| 137 | + */ |
59 | 138 | void nrf24_cls_rx_pipe(uint8_t pipe);
|
60 | 139 |
|
| 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 | + */ |
61 | 152 | 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 | + */ |
62 | 161 | void nrf24_set_addr_width(uint8_t bytes);
|
63 | 162 |
|
| 163 | + |
| 164 | +//This function is for clear TX FIFO fully |
64 | 165 | void nrf24_flush_tx(void);
|
| 166 | + |
| 167 | + |
| 168 | +//This function is for clear RX FIFO fully |
65 | 169 | void nrf24_flush_rx(void);
|
66 | 170 |
|
| 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 | + */ |
67 | 177 | 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 | + */ |
68 | 185 | 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 | + */ |
69 | 193 | void nrf24_clear_max_rt(void);
|
70 | 194 |
|
| 195 | + |
| 196 | +/* |
| 197 | + * Read individual bit in preferred register |
| 198 | + */ |
71 | 199 | uint8_t nrf24_read_bit(uint8_t reg, uint8_t bit);
|
| 200 | + |
| 201 | + |
| 202 | +/* |
| 203 | + * Set individual bit in preferred register |
| 204 | + */ |
72 | 205 | 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 | + */ |
73 | 212 | uint8_t nrf24_r_pld_wid(void);
|
74 | 213 |
|
| 214 | + |
| 215 | +/* |
| 216 | + * This function turns nrf24 to RX mode also it sets CE pin HIGH |
| 217 | + */ |
75 | 218 | void nrf24_listen(void);
|
| 219 | + |
| 220 | + |
| 221 | +/* |
| 222 | + * This function is for turn off RX mode and go in TX mode |
| 223 | + */ |
76 | 224 | void nrf24_stop_listen(void);
|
77 | 225 |
|
| 226 | + |
| 227 | +/* |
| 228 | + * Enable or disable Dynamic PayLoad by writing 'enable' or 'disable' as an argument |
| 229 | + */ |
78 | 230 | 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 | + */ |
79 | 237 | void nrf24_set_rx_dpl(uint8_t pipe, uint8_t en);
|
80 | 238 |
|
| 239 | + |
| 240 | +/* |
| 241 | + * Enable or disable auto acknowledgement for each pipe |
| 242 | + */ |
81 | 243 | void nrf24_auto_ack(uint8_t pipe, uint8_t ack);
|
| 244 | + |
| 245 | + |
| 246 | +/* |
| 247 | + * Enable or disable auto acknowledgement for all pipe together |
| 248 | + */ |
82 | 249 | void nrf24_auto_ack_all(uint8_t ack);
|
| 250 | + |
| 251 | + |
| 252 | +/* |
| 253 | + * Enable or disable acknowledgement with user defined payload |
| 254 | + */ |
83 | 255 | void nrf24_en_ack_pld(uint8_t en);
|
| 256 | + |
| 257 | + |
| 258 | +/* |
| 259 | + * Enable or disable "nrf24_transmit_no_ack" |
| 260 | + */ |
84 | 261 | void nrf24_en_dyn_ack(uint8_t en);
|
| 262 | + |
| 263 | + |
| 264 | +/* |
| 265 | + * Set delay between auto-retransmissions |
| 266 | + */ |
85 | 267 | void nrf24_auto_retr_delay(uint8_t delay);
|
| 268 | + |
| 269 | + |
| 270 | +/* |
| 271 | + * Set auto-retransmissions limit |
| 272 | + */ |
86 | 273 | void nrf24_auto_retr_limit(uint8_t limit);
|
87 | 274 |
|
| 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 | + */ |
88 | 296 | 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 | + */ |
89 | 302 | 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 | + */ |
90 | 308 | void nrf24_transmit_rx_ack_pld(uint8_t pipe, uint8_t *data, uint8_t size);
|
91 | 309 |
|
| 310 | + |
| 311 | +/* |
| 312 | + * Detect signal on selected channel above -64dbm |
| 313 | + */ |
92 | 314 | uint8_t nrf24_carrier_detect(void);
|
| 315 | + |
| 316 | + |
| 317 | +/* |
| 318 | + * check if data is in RX FIFO |
| 319 | + */ |
93 | 320 | uint8_t nrf24_data_available(void);
|
| 321 | + |
| 322 | + |
| 323 | +/* |
| 324 | + * Receive data |
| 325 | + */ |
94 | 326 | void nrf24_receive(uint8_t *data, uint8_t size);
|
95 | 327 |
|
| 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 | + */ |
96 | 335 | 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 | + */ |
97 | 344 | void nrf24_init(void);
|
98 | 345 |
|
99 | 346 | #endif
|
|
0 commit comments