Skip to content

Commit d65e73a

Browse files
committed
Merge pull request arduino#392 from tekka007/refactoring_RF24
Major RF24 library rework: minimal driver at full specs
2 parents b7400ab + b415c69 commit d65e73a

File tree

7 files changed

+487
-2793
lines changed

7 files changed

+487
-2793
lines changed

libraries/MySensors/MyConfig.h

+33-8
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,6 @@
3535
// final sketch but is helpful to see what is actually is happening during development
3636
//#define MY_DEBUG
3737

38-
// Enable MY_DEBUG_VERBOSE flag for verbose debug prints related to RF24 radio.
39-
// Requires DEBUG to be enabled.
40-
// This will add even more to the size of the final sketch!
41-
//#define MY_DEBUG_VERBOSE
42-
4338
// Enable MY_DEBUG_VERBOSE_SIGNING flag for verbose debug prints related to signing.
4439
// Requires DEBUG to be enabled.
4540
// This will add even more to the size of the final sketch!
@@ -327,12 +322,24 @@
327322
#endif
328323

329324
/**********************************
330-
* NRF24L01 Driver Defaults
325+
* NRF24L01P Driver Defaults
331326
***********************************/
332327

333328
// Enables RF24 encryption (all nodes and gateway must have this enabled, and all must be personalized with the same AES key)
334329
//#define MY_RF24_ENABLE_ENCRYPTION
335330

331+
/**
332+
* @def MY_DEBUG_VERBOSE_RF24
333+
* @brief Enable MY_DEBUG_VERBOSE_RF24 flag for verbose debug prints related to the RF24 driver. Requires DEBUG to be enabled.
334+
*/
335+
//#define MY_DEBUG_VERBOSE_RF24
336+
337+
/**
338+
* @def MY_RF24_SPI_MAX_SPEED
339+
* @brief MY_RF24_SPI_MAX_SPEED to overrule default nRF24L01+ SPI speed.
340+
*/
341+
//#define MY_RF24_SPI_MAX_SPEED 4000000
342+
336343
/**
337344
* @def MY_RF24_CE_PIN
338345
* @brief Default RF24 chip enable pin setting. Override in sketch if needed.
@@ -399,12 +406,30 @@
399406
* @def MY_RF24_BASE_RADIO_ID
400407
* @brief RF24 radio network identifier.
401408
*
402-
* This is also act as base value for sensor nodeId addresses. Change this (or channel) if you have more than one sensor network.
409+
* This acts as base value for sensor nodeId addresses. Change this (or channel) if you have more than one sensor network.
403410
*/
404411
#ifndef MY_RF24_BASE_RADIO_ID
405-
#define MY_RF24_BASE_RADIO_ID ((uint64_t)0xA8A8E1FC00LL)
412+
#define MY_RF24_BASE_RADIO_ID 0x00,0xFC,0xE1,0xA8,0xA8
413+
#endif
414+
415+
/**
416+
* @def MY_RF24_ADDR_WIDTH
417+
* @brief RF24 address width.
418+
*
419+
* This defines the width of the base address.
420+
*/
421+
#ifndef MY_RF24_ADDR_WIDTH
422+
#define MY_RF24_ADDR_WIDTH 5
406423
#endif
407424

425+
/**
426+
* @def MY_RF24_SANITY_CHECK
427+
* @brief RF24 sanity check to verify functional RF module
428+
*
429+
* This reads back and compares configuration registers. Disable if using non-P modules
430+
*/
431+
#define MY_RF24_SANITY_CHECK
432+
408433
// Enable SOFTSPI for NRF24L01, useful for the W5100 Ethernet module
409434
//#define MY_SOFTSPI
410435

libraries/MySensors/core/MyTransportNRF24.cpp

+31-76
Original file line numberDiff line numberDiff line change
@@ -19,118 +19,73 @@
1919

2020
#include "MyConfig.h"
2121
#include "MyTransport.h"
22-
#include <stdint.h>
2322
#include "drivers/RF24/RF24.h"
24-
#include "drivers/RF24/RF24_config.h"
23+
2524
#if defined(MY_RF24_ENABLE_ENCRYPTION)
26-
#include "drivers/AES/AES.h"
25+
#include "drivers/AES/AES.h"
2726
#endif
2827

29-
#define TO_ADDR(x) (MY_RF24_BASE_RADIO_ID + x)
30-
31-
#define WRITE_PIPE ((uint8_t)0)
32-
#define CURRENT_NODE_PIPE ((uint8_t)1)
33-
#define BROADCAST_PIPE ((uint8_t)2)
34-
35-
36-
RF24 _rf24(MY_RF24_CE_PIN, MY_RF24_CS_PIN);
37-
uint8_t _address;
3828
#if defined(MY_RF24_ENABLE_ENCRYPTION)
3929
AES _aes;
4030
uint8_t _dataenc[32] = {0};
4131
uint8_t _psk[16];
4232
#endif
4333

4434
bool transportInit() {
45-
// Start up the radio library
46-
_rf24.begin();
47-
48-
if (!_rf24.isPVariant()) {
49-
return false;
50-
}
51-
_rf24.setAutoAck(1);
52-
_rf24.setAutoAck(BROADCAST_PIPE,false); // Turn off auto ack for broadcast
53-
_rf24.enableAckPayload();
54-
_rf24.setChannel(MY_RF24_CHANNEL);
55-
_rf24.setPALevel(MY_RF24_PA_LEVEL);
56-
if (!_rf24.setDataRate(MY_RF24_DATARATE)) {
57-
return false;
58-
}
59-
_rf24.setRetries(5,15);
60-
_rf24.setCRCLength(RF24_CRC_16);
61-
_rf24.enableDynamicPayloads();
62-
63-
// All nodes listen to broadcast pipe (for FIND_PARENT_RESPONSE messages)
64-
_rf24.openReadingPipe(BROADCAST_PIPE, TO_ADDR(BROADCAST_ADDRESS));
65-
66-
35+
6736
#if defined(MY_RF24_ENABLE_ENCRYPTION)
6837
hwReadConfigBlock((void*)_psk, (void*)EEPROM_RF_ENCRYPTION_AES_KEY_ADDRESS, 16);
69-
_aes.set_key(_psk, 16); //set up AES-key
70-
memset(_psk, 0, 16); // Make sure it is purged from memory when set
38+
//set up AES-key
39+
_aes.set_key(_psk, 16);
40+
// Make sure it is purged from memory when set
41+
memset(_psk, 0, 16);
7142
#endif
72-
73-
//_rf24.printDetails();
74-
75-
return true;
43+
44+
return initializeRF24();
7645
}
7746

7847
void transportSetAddress(uint8_t address) {
79-
_address = address;
80-
_rf24.openReadingPipe(WRITE_PIPE, TO_ADDR(address));
81-
_rf24.openReadingPipe(CURRENT_NODE_PIPE, TO_ADDR(address));
82-
_rf24.startListening();
48+
setNodeAddress(address);
49+
startListening();
8350
}
8451

8552
uint8_t transportGetAddress() {
86-
return _address;
53+
return getNodeID();
8754
}
8855

89-
bool transportSend(uint8_t to, const void* data, uint8_t len) {
56+
bool transportSend(uint8_t recipient, const void* data, uint8_t len) {
9057
#if defined(MY_RF24_ENABLE_ENCRYPTION)
91-
memcpy(_dataenc,data,len); // copy input data because it is read-only
92-
93-
_aes.set_IV(0);//not sure if necessary
58+
// copy input data because it is read-only
59+
memcpy(_dataenc,data,len);
60+
// has to be adjusted, WIP!
61+
_aes.set_IV(0);
9462
len = len > 16 ? 32 : 16;
95-
_aes.cbc_encrypt(_dataenc, _dataenc, len/16); //encrypt
96-
#endif
97-
98-
// Make sure radio has powered up
99-
100-
_rf24.powerUp();
101-
_rf24.stopListening();
102-
_rf24.openWritingPipe(TO_ADDR(to));
103-
#if defined(MY_RF24_ENABLE_ENCRYPTION)
104-
bool ok = _rf24.write(_dataenc, len, to == BROADCAST_ADDRESS);
63+
//encrypt data
64+
_aes.cbc_encrypt(_dataenc, _dataenc, len/16);
65+
bool status = sendMessage( recipient, _dataenc, len );
10566
#else
106-
bool ok = _rf24.write(data, len, to == BROADCAST_ADDRESS);
67+
bool status = sendMessage( recipient, data, len );
10768
#endif
108-
_rf24.startListening();
109-
return ok;
69+
70+
return status;
11071
}
11172

11273
bool transportAvailable(uint8_t *to) {
113-
uint8_t pipe = 255;
114-
boolean avail = _rf24.available(&pipe);
115-
116-
(void)avail; //until somebody makes use of 'avail'
117-
if (pipe == CURRENT_NODE_PIPE)
118-
*to = _address;
119-
else if (pipe == BROADCAST_PIPE)
120-
*to = BROADCAST_ADDRESS;
121-
return (_rf24.available() && pipe < 6);
74+
bool avail = IsDataAvailable(to);
75+
return avail;
12276
}
12377

12478
uint8_t transportReceive(void* data) {
125-
uint8_t len = _rf24.getDynamicPayloadSize();
126-
_rf24.read(data, len);
79+
uint8_t len = readMessage(data);
12780
#if defined(MY_RF24_ENABLE_ENCRYPTION)
128-
_aes.set_IV(0);//not sure if necessary
129-
_aes.cbc_decrypt((byte*)(data), (byte*)(data), len>16?2:1); // decrypt
81+
// has to be adjusted, WIP!
82+
_aes.set_IV(0);
83+
// decrypt data
84+
_aes.cbc_decrypt((byte*)(data), (byte*)(data), len>16?2:1);
13085
#endif
13186
return len;
13287
}
13388

13489
void transportPowerDown() {
135-
_rf24.powerDown();
90+
powerDown();
13691
}

0 commit comments

Comments
 (0)