Skip to content

Commit 30d125e

Browse files
committed
some improvements
1 parent aa1bada commit 30d125e

File tree

2 files changed

+109
-67
lines changed

2 files changed

+109
-67
lines changed

NRF24.c

Lines changed: 87 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ void nrf24_set_channel(uint8_t ch){
113113
nrf24_w_reg(RF_CH, &ch, 1);
114114
}
115115

116-
void nrf24_open_tx_pipe(const uint8_t *addr){
116+
void nrf24_open_tx_pipe(uint8_t *addr){
117117
nrf24_w_reg(TX_ADDR, addr, 5);
118118
}
119119

@@ -150,7 +150,7 @@ void nrf24_pipe_pld_size(uint8_t pipe, uint8_t size){
150150
}
151151
}
152152

153-
void nrf24_open_rx_pipe(uint8_t pipe, const uint8_t *addr){
153+
void nrf24_open_rx_pipe(uint8_t pipe, uint8_t *addr){
154154

155155
uint8_t data = 0;
156156

@@ -264,7 +264,7 @@ uint8_t nrf24_max_rt_flag(void){
264264

265265
data &= (1 << MAX_RT);
266266

267-
if(data > 0){
267+
if(data){
268268
return 1;
269269
}
270270

@@ -315,80 +315,70 @@ void nrf24_dpl(uint8_t en){
315315
}
316316

317317
void nrf24_set_rx_dpl(uint8_t pipe, uint8_t en){
318-
uint8_t enaa = nrf24_r_reg(EN_AA, 1);
318+
319319
uint8_t dynpd = nrf24_r_reg(DYNPD, 1);
320320

321321
if(pipe > 5){
322322
pipe = 5;
323323
}
324324

325-
if(en == enable){
326-
switch(pipe){
327-
case 0:
328-
enaa |= (1 << ENAA_P0);
329-
dynpd |= (1 << DPL_P0);
330-
break;
331-
case 1:
332-
enaa |= (1 << ENAA_P1);
333-
dynpd |= (1 << DPL_P1);
334-
break;
335-
case 2:
336-
enaa |= (1 << ENAA_P2);
337-
dynpd |= (1 << DPL_P2);
338-
break;
339-
case 3:
340-
enaa |= (1 << ENAA_P3);
341-
dynpd |= (1 << DPL_P3);
342-
break;
343-
case 4:
344-
enaa |= (1 << ENAA_P4);
345-
dynpd |= (1 << DPL_P4);
346-
break;
347-
case 5:
348-
enaa |= (1 << ENAA_P5);
349-
dynpd |= (1 << DPL_P5);
350-
break;
351-
}
325+
if(en){
326+
dynpd |= (1 << pipe);
327+
}else{
328+
dynpd &= ~(1 << pipe);
329+
}
330+
331+
nrf24_w_reg(DYNPD, &dynpd, 1);
332+
}
333+
334+
void nrf24_auto_ack(uint8_t pipe, uint8_t ack){
335+
336+
if(pipe > 5){
337+
pipe = 5;
338+
}
339+
340+
uint8_t enaa = nrf24_r_reg(EN_AA, 1);
352341

342+
if(ack){
343+
enaa |= (1 << pipe);
353344
}else{
354-
switch(pipe){
355-
case 0:
356-
enaa &= ~(1 << ENAA_P0);
357-
dynpd &= ~(1 << DPL_P0);
358-
break;
359-
case 1:
360-
enaa &= ~(1 << ENAA_P1);
361-
dynpd &= ~(1 << DPL_P1);
362-
break;
363-
case 2:
364-
enaa &= ~(1 << ENAA_P2);
365-
dynpd &= ~(1 << DPL_P2);
366-
break;
367-
case 3:
368-
enaa &= ~(1 << ENAA_P3);
369-
dynpd &= ~(1 << DPL_P3);
370-
break;
371-
case 4:
372-
enaa &= ~(1 << ENAA_P4);
373-
dynpd &= ~(1 << DPL_P4);
374-
break;
375-
case 5:
376-
enaa &= ~(1 << ENAA_P5);
377-
dynpd &= ~(1 << DPL_P5);
378-
break;
379-
}
345+
enaa &= ~(1 << pipe);
380346
}
347+
381348
nrf24_w_reg(EN_AA, &enaa, 1);
382-
nrf24_w_reg(DYNPD, &dynpd, 1);
383349
}
384350

385-
void nrf24_auto_ack(uint8_t ack){
351+
void nrf24_auto_ack_all(uint8_t ack){
352+
uint8_t enaa = nrf24_r_reg(EN_AA, 1);
353+
354+
if(ack){
355+
enaa = 63;
356+
}else{
357+
enaa = 0;
358+
}
359+
360+
nrf24_w_reg(EN_AA, &enaa, 1);
361+
}
362+
363+
void nrf24_en_ack_pld(uint8_t en){
364+
uint8_t feature = nrf24_r_reg(FEATURE, 1);
365+
366+
if(en){
367+
feature |= (1 << EN_ACK_PAY);
368+
}else{
369+
feature &= ~(1 << EN_ACK_PAY);
370+
}
371+
372+
nrf24_w_reg(FEATURE, &feature, 1);
373+
}
374+
375+
void nrf24_en_dyn_ack(uint8_t en){
386376
uint8_t feature = nrf24_r_reg(FEATURE, 1);
387377

388-
if(ack == auto_ack){
389-
feature |= (1 << EN_ACK_PAY) | (1 << EN_DYN_ACK);
390-
}else if(ack == no_auto_ack){
391-
feature &= ~(1 << EN_ACK_PAY) & ~(1 << EN_DYN_ACK);
378+
if(en){
379+
feature |= (1 << EN_DYN_ACK);
380+
}else{
381+
feature &= ~(1 << EN_DYN_ACK);
392382
}
393383

394384
nrf24_w_reg(FEATURE, &feature, 1);
@@ -509,9 +499,43 @@ void nrf24_receive(uint8_t *data, uint8_t size){
509499
nrf24_r_spec_reg(data, size);
510500
csn_high();
511501
}
502+
503+
HAL_Delay(1);
504+
512505
nrf24_clear_rx_dr();
513506
}
514507
}
515508

509+
void nrf24_defaults(void){
510+
ce_low();
511+
512+
nrf24_pwr_dwn();
513+
nrf24_tx_pwr(3);
514+
nrf24_data_rate(_1mbps);
515+
nrf24_set_channel(2);
516+
nrf24_set_crc(no_crc, _1byte);
517+
nrf24_set_addr_width(5);
518+
nrf24_flush_tx();
519+
nrf24_flush_rx();
520+
nrf24_clear_rx_dr();
521+
nrf24_clear_tx_ds();
522+
nrf24_clear_max_rt();
523+
nrf24_stop_listen();
524+
nrf24_dpl(disable);
525+
nrf24_en_ack_pld(disable);
526+
nrf24_en_dyn_ack(disable);
527+
nrf24_auto_retr_delay(0);
528+
nrf24_auto_retr_limit(3);
529+
530+
531+
for(uint8_t i = 0; i <= 5; i++){
532+
nrf24_pipe_pld_size(i, 0);
533+
nrf24_cls_rx_pipe(i);
534+
nrf24_set_rx_dpl(i, disable);
535+
nrf24_auto_ack(i, enable);
536+
}
537+
538+
ce_high();
539+
}
516540

517541

NRF24.h

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ enum tx_power {
1616

1717
enum acknowledgement {
1818
auto_ack = 1,
19-
no_auto_ack = 0
19+
no_auto_ack = 0
2020
};
2121

2222
enum crc_en {
@@ -38,42 +38,60 @@ void csn_high(void);
3838
void csn_low(void);
3939
void ce_high(void);
4040
void ce_low(void);
41+
4142
void nrf24_w_reg(uint8_t reg, uint8_t *data, uint8_t size);
4243
uint8_t nrf24_r_reg(uint8_t reg, uint8_t size);
44+
4345
void nrf24_w_spec_cmd(uint8_t cmd);
4446
void nrf24_w_spec_reg(uint8_t *data, uint8_t size);
4547
void nrf24_r_spec_reg(uint8_t *data, uint8_t size);
48+
4649
void nrf24_pwr_up(void);
4750
void nrf24_pwr_dwn(void);
51+
4852
void nrf24_tx_pwr(uint8_t pwr);
4953
void nrf24_data_rate(uint8_t bps);
5054
void nrf24_set_channel(uint8_t ch);
51-
void nrf24_open_tx_pipe(const uint8_t *addr);
55+
56+
void nrf24_open_tx_pipe(uint8_t *addr);
5257
void nrf24_pipe_pld_size(uint8_t pipe, uint8_t size);
53-
void nrf24_open_rx_pipe(uint8_t pipe, const uint8_t *addr);
58+
void nrf24_open_rx_pipe(uint8_t pipe, uint8_t *addr);
5459
void nrf24_cls_rx_pipe(uint8_t pipe);
60+
5561
void nrf24_set_crc(uint8_t en_crc, uint8_t crc0);
5662
void nrf24_set_addr_width(uint8_t bytes);
63+
5764
void nrf24_flush_tx(void);
5865
void nrf24_flush_rx(void);
66+
5967
void nrf24_clear_rx_dr(void);
6068
void nrf24_clear_tx_ds(void);
6169
void nrf24_clear_max_rt(void);
70+
6271
uint8_t nrf24_max_rt_flag(void);
6372
uint8_t nrf24_r_pld_wid(void);
73+
6474
void nrf24_listen(void);
6575
void nrf24_stop_listen(void);
76+
6677
void nrf24_dpl(uint8_t en);
6778
void nrf24_set_rx_dpl(uint8_t pipe, uint8_t en);
68-
void nrf24_auto_ack(uint8_t ack);
79+
void nrf24_auto_ack(uint8_t pipe, uint8_t ack);
80+
void nrf24_auto_ack_all(uint8_t ack);
81+
void nrf24_en_ack_pld(uint8_t en);
82+
void nrf24_en_dyn_ack(uint8_t en);
6983
void nrf24_auto_retr_delay(uint8_t delay);
7084
void nrf24_auto_retr_limit(uint8_t limit);
85+
7186
void nrf24_transmit(uint8_t *data, uint8_t size);
7287
void nrf24_transmit_no_ack(uint8_t *data, uint8_t size);
7388
void nrf24_transmit_rx_ack_pld(uint8_t pipe, uint8_t *data, uint8_t size);
89+
7490
uint8_t nrf24_data_available(void);
7591
void nrf24_flush_on_full_rx(void);
7692
void nrf24_receive(uint8_t *data, uint8_t size);
7793

94+
void nrf24_defaults(void);
95+
7896
#endif
7997

0 commit comments

Comments
 (0)