Skip to content

Commit 244f797

Browse files
authored
Expose RF24 functions (#484)
1 parent 27ecc89 commit 244f797

File tree

2 files changed

+95
-31
lines changed

2 files changed

+95
-31
lines changed

libraries/MySensors/drivers/RF24/RF24.cpp

Lines changed: 80 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,70 @@ LOCAL uint8_t RF24_getStatus(void) {
8686
return RF24_spiByteTransfer( NOP );
8787
}
8888

89+
LOCAL void RF24_setChannel(uint8_t channel) {
90+
RF24_writeByteRegister(RF_CH, channel);
91+
}
92+
93+
LOCAL void RF24_setRetries(uint8_t retransmitDelay, uint8_t retransmitCount) {
94+
RF24_writeByteRegister(SETUP_RETR, retransmitDelay << ARD | retransmitCount << ARC);
95+
}
96+
97+
LOCAL void RF24_setAddressWidth(uint8_t width) {
98+
RF24_writeByteRegister(SETUP_AW, width - 2);
99+
}
100+
101+
LOCAL void RF24_setRFSetup(uint8_t RFsetup) {
102+
RF24_writeByteRegister(RF_SETUP, RFsetup);
103+
}
104+
105+
LOCAL void RF24_setFeature(uint8_t feature) {
106+
RF24_writeByteRegister(FEATURE, feature);
107+
}
108+
109+
LOCAL void RF24_setPipe(uint8_t pipe) {
110+
RF24_writeByteRegister(EN_RXADDR, pipe);
111+
}
112+
113+
LOCAL void RF24_setAutoACK(uint8_t pipe) {
114+
RF24_writeByteRegister(EN_AA, pipe);
115+
}
116+
117+
LOCAL void RF24_setDynamicPayload(uint8_t pipe) {
118+
RF24_writeByteRegister(DYNPD, pipe);
119+
}
120+
121+
LOCAL void RF24_setRFConfiguration(uint8_t configuration) {
122+
RF24_writeByteRegister(NRF_CONFIG, configuration);
123+
}
124+
125+
LOCAL void RF24_setPipeAddress(uint8_t pipe, uint8_t* address, uint8_t width) {
126+
RF24_writeMultiByteRegister(pipe, address, width);
127+
}
128+
129+
LOCAL void RF24_setPipeLSB(uint8_t pipe, uint8_t LSB) {
130+
RF24_writeByteRegister(pipe, LSB);
131+
}
132+
133+
LOCAL void RF24_setStatus(uint8_t status) {
134+
RF24_writeByteRegister(RF24_STATUS, status);
135+
}
136+
LOCAL void RF24_enableFeatures(void) {
137+
RF24_writeByteRegister(ACTIVATE, 0x73);
138+
}
139+
89140
LOCAL void RF24_openWritingPipe(uint8_t recipient) {
90141
RF24_DEBUG(PSTR("open writing pipe, recipient=%d\n"), recipient);
91142
// only write LSB of RX0 and TX pipe
92-
RF24_writeByteRegister(RX_ADDR_P0, recipient);
93-
RF24_writeByteRegister(TX_ADDR, recipient);
143+
RF24_setPipeLSB(RX_ADDR_P0, recipient);
144+
RF24_setPipeLSB(TX_ADDR, recipient);
94145
}
95146

96147
LOCAL void RF24_startListening(void) {
97148
RF24_DEBUG(PSTR("start listening\n"));
98149
// toggle PRX
99-
RF24_writeByteRegister(NRF_CONFIG, MY_RF24_CONFIGURATION | _BV(PWR_UP) | _BV(PRIM_RX) );
150+
RF24_setRFConfiguration(MY_RF24_CONFIGURATION | _BV(PWR_UP) | _BV(PRIM_RX) );
100151
// all RX pipe addresses must be unique, therefore skip if node ID is 0xFF
101-
if(MY_RF24_NODE_ADDRESS!=AUTO) RF24_writeByteRegister(RX_ADDR_P0, MY_RF24_NODE_ADDRESS);
152+
if(MY_RF24_NODE_ADDRESS!=AUTO) RF24_setPipeLSB(RX_ADDR_P0, MY_RF24_NODE_ADDRESS);
102153
// start listening
103154
RF24_ce(HIGH);
104155
}
@@ -108,14 +159,14 @@ LOCAL void RF24_stopListening(void) {
108159
RF24_ce(LOW);
109160
// timing
110161
delayMicroseconds(130);
111-
RF24_writeByteRegister(NRF_CONFIG, MY_RF24_CONFIGURATION | _BV(PWR_UP) );
162+
RF24_setRFConfiguration(MY_RF24_CONFIGURATION | _BV(PWR_UP) );
112163
// timing
113164
delayMicroseconds(100);
114165
}
115166

116167
LOCAL void RF24_powerDown(void) {
117168
RF24_ce(LOW);
118-
RF24_writeByteRegister(NRF_CONFIG, 0x00);
169+
RF24_setRFConfiguration(MY_RF24_CONFIGURATION);
119170
RF24_DEBUG(PSTR("power down\n"));
120171
}
121172

@@ -140,7 +191,7 @@ LOCAL bool RF24_sendMessage( uint8_t recipient, const void* buf, uint8_t len ) {
140191

141192
RF24_ce(LOW);
142193
// reset interrupts
143-
RF24_writeByteRegister(RF24_STATUS, _BV(TX_DS) | _BV(MAX_RT) );
194+
RF24_setStatus(_BV(TX_DS) | _BV(MAX_RT) );
144195
// Max retries exceeded
145196
if( status & _BV(MAX_RT)){
146197
// flush packet
@@ -154,7 +205,7 @@ LOCAL bool RF24_sendMessage( uint8_t recipient, const void* buf, uint8_t len ) {
154205
}
155206

156207
LOCAL uint8_t RF24_getDynamicPayloadSize(void) {
157-
uint8_t result = RF24_spiMultiByteTransfer(R_RX_PL_WID,NULL,1,true);
208+
uint8_t result = RF24_spiMultiByteTransfer(R_RX_PL_WID, NULL, 1, true);
158209
// check if payload size invalid
159210
if(result > 32) {
160211
RF24_DEBUG(PSTR("invalid payload length = %d\n"),result);
@@ -184,24 +235,25 @@ LOCAL uint8_t RF24_readMessage( void* buf) {
184235
RF24_DEBUG(PSTR("read message, len=%d\n"), len);
185236
RF24_spiMultiByteTransfer( R_RX_PAYLOAD , (uint8_t*)buf, len, true );
186237
// clear RX interrupt
187-
RF24_writeByteRegister(RF24_STATUS, _BV(RX_DR) );
238+
RF24_setStatus(_BV(RX_DR) );
188239
return len;
189240
}
190241

191242
LOCAL void RF24_setNodeAddress(uint8_t address) {
192243
if(address!=AUTO){
193244
MY_RF24_NODE_ADDRESS = address;
194245
// enable node pipe
195-
RF24_writeByteRegister(EN_RXADDR, _BV(ERX_P0 + BROADCAST_PIPE) | _BV(ERX_P0) );
246+
RF24_setPipe(_BV(ERX_P0 + BROADCAST_PIPE) | _BV(ERX_P0) );
196247
// enable autoACK on pipe 0
197-
RF24_writeByteRegister(EN_AA, _BV(ENAA_P0) );
248+
RF24_setAutoACK(_BV(ENAA_P0) );
198249
}
199250
}
200251

201252
LOCAL uint8_t RF24_getNodeID(void) {
202253
return MY_RF24_NODE_ADDRESS;
203254
}
204255

256+
205257
LOCAL bool RF24_initialize(void) {
206258
// Initialize pins
207259
pinMode(MY_RF24_CE_PIN,OUTPUT);
@@ -211,45 +263,46 @@ LOCAL bool RF24_initialize(void) {
211263
RF24_ce(LOW);
212264
RF24_csn(HIGH);
213265
// CRC and power up
214-
RF24_writeByteRegister(NRF_CONFIG, MY_RF24_CONFIGURATION | _BV(PWR_UP) ) ;
266+
RF24_setRFConfiguration(MY_RF24_CONFIGURATION | _BV(PWR_UP) ) ;
215267
// settle >2ms
216268
delay(5);
217269
// set address width
218-
RF24_writeByteRegister(SETUP_AW, MY_RF24_ADDR_WIDTH - 2 );
270+
RF24_setAddressWidth(MY_RF24_ADDR_WIDTH);
219271
// auto retransmit delay 1500us, auto retransmit count 15
220-
RF24_writeByteRegister(SETUP_RETR, RF24_ARD << ARD | RF24_ARC << ARC);
272+
RF24_setRetries(RF24_ARD, RF24_ARC);
221273
// set channel
222-
RF24_writeByteRegister(RF_CH, MY_RF24_CHANNEL);
274+
RF24_setChannel(MY_RF24_CHANNEL);
223275
// set data rate and pa level
224-
RF24_writeByteRegister(RF_SETUP, MY_RF24_RF_SETUP);
276+
RF24_setRFSetup(MY_RF24_RF_SETUP);
225277
// sanity check
226278
#if defined(MY_RF24_SANITY_CHECK)
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);
279+
uint8_t _rf_setup = RF24_readByteRegister(RF_SETUP);
280+
if(_rf_setup!=MY_RF24_RF_SETUP) {
281+
RF24_DEBUG(PSTR("Sanity check failed: RF_SETUP register=%d instead of %d, check wiring, replace module or non-P version\n"), _rf_setup, MY_RF24_RF_SETUP);
229282
return false;
230283
}
231284
#endif
232285
// toggle features (necessary on some clones)
233-
RF24_writeByteRegister(ACTIVATE,0x73);
286+
RF24_enableFeatures();
234287
// enable ACK payload and dynamic payload
235-
RF24_writeByteRegister(FEATURE, MY_RF24_FEATURE );
288+
RF24_setFeature(MY_RF24_FEATURE);
236289
// enable broadcasting pipe
237-
RF24_writeByteRegister(EN_RXADDR, _BV(ERX_P0 + BROADCAST_PIPE) );
290+
RF24_setPipe(_BV(ERX_P0 + BROADCAST_PIPE));
238291
// disable AA on all pipes, activate when node pipe set
239-
RF24_writeByteRegister(EN_AA, 0x00 );
292+
RF24_setAutoACK(0x00);
240293
// enable dynamic payloads on used pipes
241-
RF24_writeByteRegister(DYNPD, _BV(DPL_P0 + BROADCAST_PIPE) | _BV(DPL_P0));
294+
RF24_setDynamicPayload(_BV(DPL_P0 + BROADCAST_PIPE) | _BV(DPL_P0));
242295
// listen to broadcast pipe
243296
MY_RF24_BASE_ADDR[0] = BROADCAST_ADDRESS;
244-
RF24_writeMultiByteRegister(RX_ADDR_P0 + BROADCAST_PIPE, (uint8_t*)&MY_RF24_BASE_ADDR, BROADCAST_PIPE > 1 ? 1 : MY_RF24_ADDR_WIDTH);
297+
RF24_setPipeAddress(RX_ADDR_P0 + BROADCAST_PIPE, (uint8_t*)&MY_RF24_BASE_ADDR, BROADCAST_PIPE > 1 ? 1 : MY_RF24_ADDR_WIDTH);
245298
// pipe 0, set full address, later only LSB is updated
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);
299+
RF24_setPipeAddress(RX_ADDR_P0, (uint8_t*)&MY_RF24_BASE_ADDR, MY_RF24_ADDR_WIDTH);
300+
RF24_setPipeAddress(TX_ADDR, (uint8_t*)&MY_RF24_BASE_ADDR, MY_RF24_ADDR_WIDTH);
248301
// reset FIFO
249302
RF24_flushRX();
250303
RF24_flushTX();
251304
// reset interrupts
252-
RF24_writeByteRegister(RF24_STATUS, _BV(TX_DS) | _BV(MAX_RT) | _BV(RX_DR));
305+
RF24_setStatus(_BV(TX_DS) | _BV(MAX_RT) | _BV(RX_DR));
253306
return true;
254307
}
255308

libraries/MySensors/drivers/RF24/RF24.h

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,18 @@ LOCAL uint8_t RF24_readMessage(void* buf);
233233
LOCAL void RF24_setNodeAddress(uint8_t address);
234234
LOCAL uint8_t RF24_getNodeID(void);
235235
LOCAL bool RF24_initialize(void);
236-
237-
#endif // __RF24_H__
238-
239-
236+
LOCAL void RF24_setChannel(uint8_t channel);
237+
LOCAL void RF24_setRetries(uint8_t retransmitDelay, uint8_t retransmitCount);
238+
LOCAL void RF24_setAddressWidth(uint8_t width);
239+
LOCAL void RF24_setRFSetup(uint8_t RFsetup);
240+
LOCAL void RF24_setFeature(uint8_t feature);
241+
LOCAL void RF24_setPipe(uint8_t pipe);
242+
LOCAL void RF24_setAutoACK(uint8_t pipe);
243+
LOCAL void RF24_setDynamicPayload(uint8_t pipe);
244+
LOCAL void RF24_setRFConfiguration(uint8_t configuration);
245+
LOCAL void RF24_setPipeAddress(uint8_t pipe, uint8_t* address, uint8_t width);
246+
LOCAL void RF24_setPipeLSB(uint8_t pipe, uint8_t LSB);
247+
LOCAL void RF24_setStatus(uint8_t status);
248+
LOCAL void RF24_enableFeatures(void);
249+
250+
#endif // __RF24_H__

0 commit comments

Comments
 (0)