@@ -100,7 +100,7 @@ void nrf24_data_rate(uint8_t bps){
100
100
101
101
data &= ~(1 << RF_DR_LOW ) & ~(1 << RF_DR_HIGH );
102
102
103
- if (bps == _2Mbps ){
103
+ if (bps == _2mbps ){
104
104
data |= (1 << RF_DR_HIGH );
105
105
}else if (bps == _250kbps ){
106
106
data |= (1 << RF_DR_LOW );
@@ -258,17 +258,26 @@ void nrf24_clear_max_rt(void){
258
258
}
259
259
260
260
uint8_t nrf24_read_bit (uint8_t reg , uint8_t bit ){
261
+
262
+ if (nrf24_r_reg (reg , 1 ) & (1 << bit )){
263
+ return 1 ;
264
+ }
265
+
266
+ return 0 ;
267
+ }
268
+
269
+ void nrf24_set_bit (uint8_t reg , uint8_t bit , uint8_t val ){
261
270
uint8_t data = 0 ;
262
271
263
272
data = nrf24_r_reg (reg , 1 );
264
273
265
- data &= ( 1 << bit );
266
-
267
- if ( data ) {
268
- return 1 ;
274
+ if ( val ){
275
+ data |= ( 1 << bit );
276
+ } else {
277
+ data &= ~( 1 << bit ) ;
269
278
}
270
279
271
- return 0 ;
280
+ nrf24_w_reg ( reg , & data , 1 ) ;
272
281
}
273
282
274
283
uint8_t nrf24_r_pld_wid (void ){
@@ -290,6 +299,8 @@ void nrf24_listen(void){
290
299
data |= (1 << PRIM_RX );
291
300
292
301
nrf24_w_reg (CONFIG , & data , 1 );
302
+
303
+ ce_high ();
293
304
}
294
305
295
306
void nrf24_stop_listen (void ){
@@ -414,6 +425,7 @@ uint8_t nrf24_transmit(uint8_t *data, uint8_t size){
414
425
415
426
nrf24_clear_tx_ds ();
416
427
428
+
417
429
csn_low ();
418
430
nrf24_w_spec_cmd (W_TX_PAYLOAD );
419
431
nrf24_w_spec_reg (data , size );
@@ -423,19 +435,9 @@ uint8_t nrf24_transmit(uint8_t *data, uint8_t size){
423
435
HAL_Delay (1 );
424
436
ce_low ();
425
437
426
- uint32_t ms = HAL_GetTick ();
427
438
428
- while ((HAL_GetTick ()- ms ) < 150 ){
429
- uint8_t stat = nrf24_r_reg (STATUS , 1 );
430
- if (stat & (1 << TX_DS )){
431
- nrf24_clear_tx_ds ();
432
- return 1 ;
433
- }
434
-
435
- if (stat & (1 << MAX_RT )){
436
- nrf24_clear_max_rt ();
437
- return 2 ;
438
- }
439
+ if (nrf24_read_bit (STATUS , MAX_RT ) == 1 ){
440
+ return 1 ;
439
441
}
440
442
441
443
return 0 ;
@@ -475,6 +477,10 @@ void nrf24_transmit_rx_ack_pld(uint8_t pipe, uint8_t *data, uint8_t size){
475
477
476
478
}
477
479
480
+ uint8_t nrf24_carrier_detect (void ){
481
+ return nrf24_r_reg (RPD , 1 );
482
+ }
483
+
478
484
uint8_t nrf24_data_available (void ){
479
485
480
486
uint8_t reg_dt = nrf24_r_reg (FIFO_STATUS , 1 );
@@ -486,22 +492,15 @@ uint8_t nrf24_data_available(void){
486
492
return 0 ;
487
493
}
488
494
489
- void nrf24_flush_on_full_rx (void ){
490
- uint8_t reg_dt = nrf24_r_reg (FIFO_STATUS , 1 );
491
-
492
- if ((reg_dt & (1 << RX_FULL ))){
493
- nrf24_flush_rx ();
494
- }
495
- }
496
-
497
495
void nrf24_receive (uint8_t * data , uint8_t size ){
498
- ce_high ();
499
496
500
497
csn_low ();
501
498
nrf24_w_spec_cmd (R_RX_PAYLOAD );
502
499
nrf24_r_spec_reg (data , size );
503
500
csn_high ();
504
501
502
+ nrf24_clear_rx_dr ();
503
+
505
504
nrf24_flush_rx ();
506
505
}
507
506
@@ -527,7 +526,7 @@ void nrf24_defaults(void){
527
526
nrf24_auto_retr_limit (3 );
528
527
529
528
530
- for (uint8_t i = 0 ; i <= 5 ; i ++ ){
529
+ for (uint8_t i = 0 ; i < 5 ; i ++ ){
531
530
nrf24_pipe_pld_size (i , 0 );
532
531
nrf24_cls_rx_pipe (i );
533
532
nrf24_set_rx_dpl (i , disable );
@@ -537,4 +536,16 @@ void nrf24_defaults(void){
537
536
ce_high ();
538
537
}
539
538
539
+ void nrf24_init (void ){
540
+
541
+ nrf24_pwr_up ();
542
+
543
+ nrf24_flush_tx ();
544
+ nrf24_flush_rx ();
545
+
546
+ nrf24_clear_rx_dr ();
547
+ nrf24_clear_tx_ds ();
548
+ nrf24_clear_max_rt ();
549
+ }
550
+
540
551
0 commit comments