Skip to content

Commit 460ae09

Browse files
committed
Merge pull request arduino#429 from tekka007/RF24_updates
RF24 driver update
2 parents 0c8acb0 + 8515368 commit 460ae09

File tree

4 files changed

+153
-150
lines changed

4 files changed

+153
-150
lines changed

libraries/MySensors/MySensor.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,7 @@
240240
#if defined(ARDUINO_ARCH_ESP8266)
241241
#error Soft SPI is not available on ESP8266
242242
#endif
243-
#include "drivers/AVR/DigitalIO/SoftI2cMaster.cpp"
244-
#include "drivers/AVR/DigitalIO/PinIO.cpp"
243+
#include "drivers/AVR/DigitalIO/DigitalIO.h"
245244
#endif
246245

247246
// FLASH

libraries/MySensors/core/MyTransportNRF24.cpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,16 @@ bool transportInit() {
4141
memset(_psk, 0, 16);
4242
#endif
4343

44-
return initializeRF24();
44+
return RF24_initialize();
4545
}
4646

4747
void transportSetAddress(uint8_t address) {
48-
setNodeAddress(address);
49-
startListening();
48+
RF24_setNodeAddress(address);
49+
RF24_startListening();
5050
}
5151

5252
uint8_t transportGetAddress() {
53-
return getNodeID();
53+
return RF24_getNodeID();
5454
}
5555

5656
bool transportSend(uint8_t recipient, const void* data, uint8_t len) {
@@ -62,21 +62,21 @@ bool transportSend(uint8_t recipient, const void* data, uint8_t len) {
6262
len = len > 16 ? 32 : 16;
6363
//encrypt data
6464
_aes.cbc_encrypt(_dataenc, _dataenc, len/16);
65-
bool status = sendMessage( recipient, _dataenc, len );
65+
bool status = RF24_sendMessage( recipient, _dataenc, len );
6666
#else
67-
bool status = sendMessage( recipient, data, len );
67+
bool status = RF24_sendMessage( recipient, data, len );
6868
#endif
6969

7070
return status;
7171
}
7272

7373
bool transportAvailable(uint8_t *to) {
74-
bool avail = IsDataAvailable(to);
74+
bool avail = RF24_isDataAvailable(to);
7575
return avail;
7676
}
7777

7878
uint8_t transportReceive(void* data) {
79-
uint8_t len = readMessage(data);
79+
uint8_t len = RF24_readMessage(data);
8080
#if defined(MY_RF24_ENABLE_ENCRYPTION)
8181
// has to be adjusted, WIP!
8282
_aes.set_IV(0);
@@ -87,5 +87,5 @@ uint8_t transportReceive(void* data) {
8787
}
8888

8989
void transportPowerDown() {
90-
powerDown();
90+
RF24_powerDown();
9191
}

libraries/MySensors/drivers/RF24/RF24.cpp

+85-87
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,23 @@
2222

2323
#include "RF24.h"
2424

25-
uint8_t MY_RF24_BASE_ADDR[MY_RF24_ADDR_WIDTH] = { MY_RF24_BASE_RADIO_ID };
26-
uint8_t MY_RF24_NODE_ADDRESS = AUTO;
25+
LOCAL uint8_t MY_RF24_BASE_ADDR[MY_RF24_ADDR_WIDTH] = { MY_RF24_BASE_RADIO_ID };
26+
LOCAL uint8_t MY_RF24_NODE_ADDRESS = AUTO;
2727

28-
void csn(bool level) {
28+
LOCAL void RF24_csn(bool level) {
2929
digitalWrite(MY_RF24_CS_PIN, level);
3030
}
3131

32-
void ce(bool level) {
32+
LOCAL void RF24_ce(bool level) {
3333
digitalWrite(MY_RF24_CE_PIN, level);
3434
}
3535

36-
uint8_t spiMultiByteTransfer(uint8_t cmd, uint8_t* buf, uint8_t len, bool aReadMode) {
36+
LOCAL uint8_t RF24_spiMultiByteTransfer(uint8_t cmd, uint8_t* buf, uint8_t len, bool aReadMode) {
3737
uint8_t* current = buf;
3838
#if !defined(MY_SOFTSPI)
3939
_SPI.beginTransaction(SPISettings(MY_RF24_SPI_MAX_SPEED, MY_RF24_SPI_DATA_ORDER, MY_RF24_SPI_DATA_MODE));
4040
#endif
41-
csn(LOW);
41+
RF24_csn(LOW);
4242
// timing
4343
delayMicroseconds(10);
4444
uint8_t status = _SPI.transfer( cmd );
@@ -48,7 +48,7 @@ uint8_t spiMultiByteTransfer(uint8_t cmd, uint8_t* buf, uint8_t len, bool aReadM
4848
if(buf != NULL) *current++ = status;
4949
} else status = _SPI.transfer(*current++);
5050
}
51-
csn(HIGH);
51+
RF24_csn(HIGH);
5252
#if !defined(MY_SOFTSPI)
5353
_SPI.endTransaction();
5454
#endif
@@ -57,120 +57,116 @@ uint8_t spiMultiByteTransfer(uint8_t cmd, uint8_t* buf, uint8_t len, bool aReadM
5757
return status;
5858
}
5959

60-
uint8_t spiByteTransfer(uint8_t cmd) {
61-
return spiMultiByteTransfer( cmd, NULL, 0, false);
60+
LOCAL uint8_t RF24_spiByteTransfer(uint8_t cmd) {
61+
return RF24_spiMultiByteTransfer( cmd, NULL, 0, false);
6262
}
6363

64-
uint8_t _readByteRegister(uint8_t cmd) {
65-
uint8_t value = spiMultiByteTransfer( cmd, NULL, 1, true);
64+
LOCAL uint8_t RF24_RAW_readByteRegister(uint8_t cmd) {
65+
uint8_t value = RF24_spiMultiByteTransfer( cmd, NULL, 1, true);
6666
RF24_DEBUG(PSTR("read register, reg=%d, value=%d\n"), cmd & (R_REGISTER ^ 0xFF), value);
6767
return value;
6868
}
6969

70-
uint8_t _writeByteRegister(uint8_t cmd, uint8_t value) {
70+
LOCAL uint8_t RF24_RAW_writeByteRegister(uint8_t cmd, uint8_t value) {
7171
RF24_DEBUG(PSTR("write register, reg=%d, value=%d\n"), cmd & (W_REGISTER ^ 0xFF), value);
72-
return spiMultiByteTransfer( cmd , &value, 1, false);
72+
return RF24_spiMultiByteTransfer( cmd , &value, 1, false);
7373
}
7474

75-
#define readByteRegister(reg) _readByteRegister( R_REGISTER | ( REGISTER_MASK & (reg) ) )
76-
#define writeByteRegister(reg, value) _writeByteRegister( W_REGISTER | ( REGISTER_MASK & (reg) ), value )
77-
#define writeMultiByteRegister(reg, buf, len) spiMultiByteTransfer( W_REGISTER | ( REGISTER_MASK & (reg) ), (uint8_t*)buf, len, false )
78-
79-
void flushRX(void) {
80-
RF24_DEBUG(PSTR("flushRX\n"));
81-
spiByteTransfer( FLUSH_RX );
75+
LOCAL void RF24_flushRX(void) {
76+
RF24_DEBUG(PSTR("RF24_flushRX\n"));
77+
RF24_spiByteTransfer( FLUSH_RX );
8278
}
8379

84-
void flushTX(void) {
85-
RF24_DEBUG(PSTR("flushTX\n"));
86-
spiByteTransfer( FLUSH_TX );
80+
LOCAL void RF24_flushTX(void) {
81+
RF24_DEBUG(PSTR("RF24_flushTX\n"));
82+
RF24_spiByteTransfer( FLUSH_TX );
8783
}
8884

89-
uint8_t getStatus(void) {
90-
return spiByteTransfer( NOP );
85+
LOCAL uint8_t RF24_getStatus(void) {
86+
return RF24_spiByteTransfer( NOP );
9187
}
9288

93-
void openWritingPipe(uint8_t recipient) {
89+
LOCAL void RF24_openWritingPipe(uint8_t recipient) {
9490
RF24_DEBUG(PSTR("open writing pipe, recipient=%d\n"), recipient);
9591
// only write LSB of RX0 and TX pipe
96-
writeByteRegister(RX_ADDR_P0, recipient);
97-
writeByteRegister(TX_ADDR, recipient);
92+
RF24_writeByteRegister(RX_ADDR_P0, recipient);
93+
RF24_writeByteRegister(TX_ADDR, recipient);
9894
}
9995

100-
void startListening(void) {
96+
LOCAL void RF24_startListening(void) {
10197
RF24_DEBUG(PSTR("start listening\n"));
10298
// toggle PRX
103-
writeByteRegister(NRF_CONFIG, MY_RF24_CONFIGURATION | _BV(PWR_UP) | _BV(PRIM_RX) );
99+
RF24_writeByteRegister(NRF_CONFIG, MY_RF24_CONFIGURATION | _BV(PWR_UP) | _BV(PRIM_RX) );
104100
// all RX pipe addresses must be unique, therefore skip if node ID is 0xFF
105-
if(MY_RF24_NODE_ADDRESS!=AUTO) writeByteRegister(RX_ADDR_P0, MY_RF24_NODE_ADDRESS);
101+
if(MY_RF24_NODE_ADDRESS!=AUTO) RF24_writeByteRegister(RX_ADDR_P0, MY_RF24_NODE_ADDRESS);
106102
// start listening
107-
ce(HIGH);
103+
RF24_ce(HIGH);
108104
}
109105

110-
void stopListening(void) {
106+
LOCAL void RF24_stopListening(void) {
111107
RF24_DEBUG(PSTR("stop listening\n"));
112-
ce(LOW);
108+
RF24_ce(LOW);
113109
// timing
114110
delayMicroseconds(130);
115-
writeByteRegister(NRF_CONFIG, MY_RF24_CONFIGURATION | _BV(PWR_UP) );
111+
RF24_writeByteRegister(NRF_CONFIG, MY_RF24_CONFIGURATION | _BV(PWR_UP) );
116112
// timing
117113
delayMicroseconds(100);
118114
}
119115

120-
void powerDown(void) {
121-
ce(LOW);
122-
writeByteRegister(NRF_CONFIG, 0x00);
116+
LOCAL void RF24_powerDown(void) {
117+
RF24_ce(LOW);
118+
RF24_writeByteRegister(NRF_CONFIG, 0x00);
123119
RF24_DEBUG(PSTR("power down\n"));
124120
}
125121

126-
bool sendMessage( uint8_t recipient, const void* buf, uint8_t len ) {
122+
LOCAL bool RF24_sendMessage( uint8_t recipient, const void* buf, uint8_t len ) {
127123
uint8_t status;
128124

129-
stopListening();
130-
openWritingPipe( recipient );
125+
RF24_stopListening();
126+
RF24_openWritingPipe( recipient );
131127
RF24_DEBUG(PSTR("send message to %d, len=%d\n"),recipient,len);
132128
// flush TX FIFO
133-
flushTX();
129+
RF24_flushTX();
134130
// this command is affected in clones (e.g. Si24R1): flipped NoACK bit when using W_TX_PAYLOAD_NO_ACK / W_TX_PAYLOAD
135-
// spiMultiByteTransfer( recipient==BROADCAST_ADDRESS ? W_TX_PAYLOAD_NO_ACK : W_TX_PAYLOAD, (uint8_t*)buf, len, false );
131+
// RF24_spiMultiByteTransfer( recipient==BROADCAST_ADDRESS ? W_TX_PAYLOAD_NO_ACK : W_TX_PAYLOAD, (uint8_t*)buf, len, false );
136132
// we are safe by disabling AutoACK on the broadcasting pipe
137-
spiMultiByteTransfer( W_TX_PAYLOAD, (uint8_t*)buf, len, false );
133+
RF24_spiMultiByteTransfer( W_TX_PAYLOAD, (uint8_t*)buf, len, false );
138134
// go
139-
ce(HIGH);
135+
RF24_ce(HIGH);
140136
// TX starts after ~10us
141137
do {
142-
status = getStatus();
138+
status = RF24_getStatus();
143139
} while (!(status & ( _BV(MAX_RT) | _BV(TX_DS) )));
144140

145-
ce(LOW);
141+
RF24_ce(LOW);
146142
// reset interrupts
147-
writeByteRegister(RF24_STATUS, _BV(TX_DS) | _BV(MAX_RT) );
143+
RF24_writeByteRegister(RF24_STATUS, _BV(TX_DS) | _BV(MAX_RT) );
148144
// Max retries exceeded
149145
if( status & _BV(MAX_RT)){
150146
// flush packet
151147
RF24_DEBUG(PSTR("MAX_RT\n"));
152-
flushTX();
148+
RF24_flushTX();
153149
}
154150

155-
startListening();
151+
RF24_startListening();
156152

157153
return (status & _BV(TX_DS));
158154
}
159155

160-
uint8_t getDynamicPayloadSize(void) {
161-
uint8_t result = spiMultiByteTransfer(R_RX_PL_WID,NULL,1,true);
156+
LOCAL uint8_t RF24_getDynamicPayloadSize(void) {
157+
uint8_t result = RF24_spiMultiByteTransfer(R_RX_PL_WID,NULL,1,true);
162158
// check if payload size invalid
163159
if(result > 32) {
164160
RF24_DEBUG(PSTR("invalid payload length = %d\n"),result);
165-
flushRX();
161+
RF24_flushRX();
166162
result = 0;
167163
}
168164
return result;
169165
}
170166

171167

172-
bool IsDataAvailable(uint8_t* to) {
173-
uint8_t pipe_num = ( getStatus() >> RX_P_NO ) & 0b0111;
168+
LOCAL bool RF24_isDataAvailable(uint8_t* to) {
169+
uint8_t pipe_num = ( RF24_getStatus() >> RX_P_NO ) & 0b0111;
174170
#if defined(MY_DEBUG_VERBOSE_RF24)
175171
if(pipe_num <= 5)
176172
RF24_DEBUG(PSTR("Data available on pipe %d\n"),pipe_num);
@@ -183,75 +179,77 @@ bool IsDataAvailable(uint8_t* to) {
183179
}
184180

185181

186-
uint8_t readMessage( void* buf) {
187-
uint8_t len = getDynamicPayloadSize();
182+
LOCAL uint8_t RF24_readMessage( void* buf) {
183+
uint8_t len = RF24_getDynamicPayloadSize();
188184
RF24_DEBUG(PSTR("read message, len=%d\n"), len);
189-
spiMultiByteTransfer( R_RX_PAYLOAD , (uint8_t*)buf, len, true );
185+
RF24_spiMultiByteTransfer( R_RX_PAYLOAD , (uint8_t*)buf, len, true );
190186
// clear RX interrupt
191-
writeByteRegister(RF24_STATUS, _BV(RX_DR) );
187+
RF24_writeByteRegister(RF24_STATUS, _BV(RX_DR) );
192188
return len;
193189
}
194190

195-
void setNodeAddress(uint8_t address) {
196-
MY_RF24_NODE_ADDRESS = address;
197-
// enable node pipe
198-
writeByteRegister(EN_RXADDR, _BV(ERX_P0 + BROADCAST_PIPE) | _BV(ERX_P0) );
199-
// enable autoACK on pipe 0
200-
writeByteRegister(EN_AA, _BV(ENAA_P0) );
191+
LOCAL void RF24_setNodeAddress(uint8_t address) {
192+
if(address!=AUTO){
193+
MY_RF24_NODE_ADDRESS = address;
194+
// enable node pipe
195+
RF24_writeByteRegister(EN_RXADDR, _BV(ERX_P0 + BROADCAST_PIPE) | _BV(ERX_P0) );
196+
// enable autoACK on pipe 0
197+
RF24_writeByteRegister(EN_AA, _BV(ENAA_P0) );
198+
}
201199
}
202200

203-
uint8_t getNodeID(void) {
201+
LOCAL uint8_t RF24_getNodeID(void) {
204202
return MY_RF24_NODE_ADDRESS;
205203
}
206204

207-
bool initializeRF24(void) {
205+
LOCAL bool RF24_initialize(void) {
208206
// Initialize pins
209207
pinMode(MY_RF24_CE_PIN,OUTPUT);
210208
pinMode(MY_RF24_CS_PIN,OUTPUT);
211209
// Initialize SPI
212210
_SPI.begin();
213-
ce(LOW);
214-
csn(HIGH);
211+
RF24_ce(LOW);
212+
RF24_csn(HIGH);
215213
// CRC and power up
216-
writeByteRegister(NRF_CONFIG, MY_RF24_CONFIGURATION | _BV(PWR_UP) ) ;
214+
RF24_writeByteRegister(NRF_CONFIG, MY_RF24_CONFIGURATION | _BV(PWR_UP) ) ;
217215
// settle >2ms
218216
delay(5);
219217
// set address width
220-
writeByteRegister(SETUP_AW, MY_RF24_ADDR_WIDTH - 2 );
218+
RF24_writeByteRegister(SETUP_AW, MY_RF24_ADDR_WIDTH - 2 );
221219
// auto retransmit delay 1500us, auto retransmit count 15
222-
writeByteRegister(SETUP_RETR, RF24_ARD << ARD | RF24_ARC << ARC);
220+
RF24_writeByteRegister(SETUP_RETR, RF24_ARD << ARD | RF24_ARC << ARC);
223221
// set channel
224-
writeByteRegister(RF_CH, MY_RF24_CHANNEL);
222+
RF24_writeByteRegister(RF_CH, MY_RF24_CHANNEL);
225223
// set data rate and pa level
226-
writeByteRegister(RF_SETUP, MY_RF24_RF_SETUP);
224+
RF24_writeByteRegister(RF_SETUP, MY_RF24_RF_SETUP);
227225
// sanity check
228226
#if defined(MY_RF24_SANITY_CHECK)
229-
if(readByteRegister(RF_SETUP)!=MY_RF24_RF_SETUP) {
230-
RF24_DEBUG(PSTR("Sanity check failed: RF_SETUP register=%d instead of %d, check wiring, replace module or non-P version\n"),readByteRegister(RF_SETUP), MY_RF24_RF_SETUP);
227+
if(RF24_readByteRegister(RF_SETUP)!=MY_RF24_RF_SETUP) {
228+
RF24_DEBUG(PSTR("Sanity check failed: RF_SETUP register=%d instead of %d, check wiring, replace module or non-P version\n"),RF24_readByteRegister(RF_SETUP), MY_RF24_RF_SETUP);
231229
return false;
232230
}
233231
#endif
234232
// toggle features (necessary on some clones)
235-
writeByteRegister(ACTIVATE,0x73);
233+
RF24_writeByteRegister(ACTIVATE,0x73);
236234
// enable ACK payload and dynamic payload
237-
writeByteRegister(FEATURE, MY_RF24_FEATURE );
235+
RF24_writeByteRegister(FEATURE, MY_RF24_FEATURE );
238236
// enable broadcasting pipe
239-
writeByteRegister(EN_RXADDR, _BV(ERX_P0 + BROADCAST_PIPE) );
237+
RF24_writeByteRegister(EN_RXADDR, _BV(ERX_P0 + BROADCAST_PIPE) );
240238
// disable AA on all pipes, activate when node pipe set
241-
writeByteRegister(EN_AA, 0x00 );
239+
RF24_writeByteRegister(EN_AA, 0x00 );
242240
// enable dynamic payloads on used pipes
243-
writeByteRegister(DYNPD, _BV(DPL_P0 + BROADCAST_PIPE) | _BV(DPL_P0));
241+
RF24_writeByteRegister(DYNPD, _BV(DPL_P0 + BROADCAST_PIPE) | _BV(DPL_P0));
244242
// listen to broadcast pipe
245243
MY_RF24_BASE_ADDR[0] = BROADCAST_ADDRESS;
246-
writeMultiByteRegister(RX_ADDR_P0 + BROADCAST_PIPE, (uint8_t*)&MY_RF24_BASE_ADDR, BROADCAST_PIPE > 1 ? 1 : MY_RF24_ADDR_WIDTH);
244+
RF24_writeMultiByteRegister(RX_ADDR_P0 + BROADCAST_PIPE, (uint8_t*)&MY_RF24_BASE_ADDR, BROADCAST_PIPE > 1 ? 1 : MY_RF24_ADDR_WIDTH);
247245
// pipe 0, set full address, later only LSB is updated
248-
writeMultiByteRegister(RX_ADDR_P0, (uint8_t*)&MY_RF24_BASE_ADDR, MY_RF24_ADDR_WIDTH);
249-
writeMultiByteRegister(TX_ADDR, (uint8_t*)&MY_RF24_BASE_ADDR, MY_RF24_ADDR_WIDTH);
246+
RF24_writeMultiByteRegister(RX_ADDR_P0, (uint8_t*)&MY_RF24_BASE_ADDR, MY_RF24_ADDR_WIDTH);
247+
RF24_writeMultiByteRegister(TX_ADDR, (uint8_t*)&MY_RF24_BASE_ADDR, MY_RF24_ADDR_WIDTH);
250248
// reset FIFO
251-
flushRX();
252-
flushTX();
249+
RF24_flushRX();
250+
RF24_flushTX();
253251
// reset interrupts
254-
writeByteRegister(RF24_STATUS, _BV(TX_DS) | _BV(MAX_RT) | _BV(RX_DR));
252+
RF24_writeByteRegister(RF24_STATUS, _BV(TX_DS) | _BV(MAX_RT) | _BV(RX_DR));
255253
return true;
256254
}
257255

0 commit comments

Comments
 (0)