@@ -86,19 +86,70 @@ LOCAL uint8_t RF24_getStatus(void) {
86
86
return RF24_spiByteTransfer ( NOP );
87
87
}
88
88
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
+
89
140
LOCAL void RF24_openWritingPipe (uint8_t recipient) {
90
141
RF24_DEBUG (PSTR (" open writing pipe, recipient=%d\n " ), recipient);
91
142
// 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);
94
145
}
95
146
96
147
LOCAL void RF24_startListening (void ) {
97
148
RF24_DEBUG (PSTR (" start listening\n " ));
98
149
// 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) );
100
151
// 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);
102
153
// start listening
103
154
RF24_ce (HIGH);
104
155
}
@@ -108,14 +159,14 @@ LOCAL void RF24_stopListening(void) {
108
159
RF24_ce (LOW);
109
160
// timing
110
161
delayMicroseconds (130 );
111
- RF24_writeByteRegister (NRF_CONFIG, MY_RF24_CONFIGURATION | _BV (PWR_UP) );
162
+ RF24_setRFConfiguration ( MY_RF24_CONFIGURATION | _BV (PWR_UP) );
112
163
// timing
113
164
delayMicroseconds (100 );
114
165
}
115
166
116
167
LOCAL void RF24_powerDown (void ) {
117
168
RF24_ce (LOW);
118
- RF24_writeByteRegister (NRF_CONFIG, 0x00 );
169
+ RF24_setRFConfiguration (MY_RF24_CONFIGURATION );
119
170
RF24_DEBUG (PSTR (" power down\n " ));
120
171
}
121
172
@@ -140,7 +191,7 @@ LOCAL bool RF24_sendMessage( uint8_t recipient, const void* buf, uint8_t len ) {
140
191
141
192
RF24_ce (LOW);
142
193
// reset interrupts
143
- RF24_writeByteRegister (RF24_STATUS, _BV (TX_DS) | _BV (MAX_RT) );
194
+ RF24_setStatus ( _BV (TX_DS) | _BV (MAX_RT) );
144
195
// Max retries exceeded
145
196
if ( status & _BV (MAX_RT)){
146
197
// flush packet
@@ -154,7 +205,7 @@ LOCAL bool RF24_sendMessage( uint8_t recipient, const void* buf, uint8_t len ) {
154
205
}
155
206
156
207
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 );
158
209
// check if payload size invalid
159
210
if (result > 32 ) {
160
211
RF24_DEBUG (PSTR (" invalid payload length = %d\n " ),result);
@@ -184,24 +235,25 @@ LOCAL uint8_t RF24_readMessage( void* buf) {
184
235
RF24_DEBUG (PSTR (" read message, len=%d\n " ), len);
185
236
RF24_spiMultiByteTransfer ( R_RX_PAYLOAD , (uint8_t *)buf, len, true );
186
237
// clear RX interrupt
187
- RF24_writeByteRegister (RF24_STATUS, _BV (RX_DR) );
238
+ RF24_setStatus ( _BV (RX_DR) );
188
239
return len;
189
240
}
190
241
191
242
LOCAL void RF24_setNodeAddress (uint8_t address) {
192
243
if (address!=AUTO){
193
244
MY_RF24_NODE_ADDRESS = address;
194
245
// 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) );
196
247
// enable autoACK on pipe 0
197
- RF24_writeByteRegister (EN_AA, _BV (ENAA_P0) );
248
+ RF24_setAutoACK ( _BV (ENAA_P0) );
198
249
}
199
250
}
200
251
201
252
LOCAL uint8_t RF24_getNodeID (void ) {
202
253
return MY_RF24_NODE_ADDRESS;
203
254
}
204
255
256
+
205
257
LOCAL bool RF24_initialize (void ) {
206
258
// Initialize pins
207
259
pinMode (MY_RF24_CE_PIN,OUTPUT);
@@ -211,45 +263,46 @@ LOCAL bool RF24_initialize(void) {
211
263
RF24_ce (LOW);
212
264
RF24_csn (HIGH);
213
265
// CRC and power up
214
- RF24_writeByteRegister (NRF_CONFIG, MY_RF24_CONFIGURATION | _BV (PWR_UP) ) ;
266
+ RF24_setRFConfiguration ( MY_RF24_CONFIGURATION | _BV (PWR_UP) ) ;
215
267
// settle >2ms
216
268
delay (5 );
217
269
// set address width
218
- RF24_writeByteRegister (SETUP_AW, MY_RF24_ADDR_WIDTH - 2 );
270
+ RF24_setAddressWidth ( MY_RF24_ADDR_WIDTH);
219
271
// 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);
221
273
// set channel
222
- RF24_writeByteRegister (RF_CH, MY_RF24_CHANNEL);
274
+ RF24_setChannel ( MY_RF24_CHANNEL);
223
275
// set data rate and pa level
224
- RF24_writeByteRegister (RF_SETUP, MY_RF24_RF_SETUP);
276
+ RF24_setRFSetup ( MY_RF24_RF_SETUP);
225
277
// sanity check
226
278
#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);
229
282
return false ;
230
283
}
231
284
#endif
232
285
// toggle features (necessary on some clones)
233
- RF24_writeByteRegister (ACTIVATE, 0x73 );
286
+ RF24_enableFeatures ( );
234
287
// enable ACK payload and dynamic payload
235
- RF24_writeByteRegister (FEATURE, MY_RF24_FEATURE );
288
+ RF24_setFeature ( MY_RF24_FEATURE);
236
289
// enable broadcasting pipe
237
- RF24_writeByteRegister (EN_RXADDR, _BV (ERX_P0 + BROADCAST_PIPE) );
290
+ RF24_setPipe ( _BV (ERX_P0 + BROADCAST_PIPE));
238
291
// disable AA on all pipes, activate when node pipe set
239
- RF24_writeByteRegister (EN_AA, 0x00 );
292
+ RF24_setAutoACK ( 0x00 );
240
293
// 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));
242
295
// listen to broadcast pipe
243
296
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);
245
298
// 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);
248
301
// reset FIFO
249
302
RF24_flushRX ();
250
303
RF24_flushTX ();
251
304
// 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));
253
306
return true ;
254
307
}
255
308
0 commit comments