Custom SPI support

This commit is contained in:
Auri 2020-06-02 13:43:09 +02:00 committed by GitHub
parent 07c3e9ed16
commit 78447f61ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 51 additions and 47 deletions

View File

@ -41,7 +41,7 @@ uint8_t RFM69::ACK_RECEIVED; // should be polled immediately after sending a pac
int16_t RFM69::RSSI; // most accurate RSSI during reception (closest to the reception) int16_t RFM69::RSSI; // most accurate RSSI during reception (closest to the reception)
volatile bool RFM69::_haveData; volatile bool RFM69::_haveData;
RFM69::RFM69(uint8_t slaveSelectPin, uint8_t interruptPin, bool isRFM69HW) RFM69::RFM69(uint8_t slaveSelectPin, uint8_t interruptPin, bool isRFM69HW, SPIClass *spi)
{ {
_slaveSelectPin = slaveSelectPin; _slaveSelectPin = slaveSelectPin;
_interruptPin = interruptPin; _interruptPin = interruptPin;
@ -49,6 +49,7 @@ RFM69::RFM69(uint8_t slaveSelectPin, uint8_t interruptPin, bool isRFM69HW)
_spyMode = false; _spyMode = false;
_powerLevel = 31; _powerLevel = 31;
_isRFM69HW = isRFM69HW; _isRFM69HW = isRFM69HW;
_spi = spi;
#if defined(RF69_LISTENMODE_ENABLE) #if defined(RF69_LISTENMODE_ENABLE)
_isHighSpeed = true; _isHighSpeed = true;
_haveEncryptKey = false; _haveEncryptKey = false;
@ -111,13 +112,16 @@ bool RFM69::initialize(uint8_t freqBand, uint16_t nodeID, uint8_t networkID)
digitalWrite(_slaveSelectPin, HIGH); digitalWrite(_slaveSelectPin, HIGH);
pinMode(_slaveSelectPin, OUTPUT); pinMode(_slaveSelectPin, OUTPUT);
if(_spi == nullptr){
_spi = &SPI;
}
#if defined(ESP32) #if defined(ESP32)
SPI.begin(18,19,23,5); //SPI3 (SCK,MISO,MOSI,CS) _spi->begin(18,19,23,5); //SPI3 (SCK,MISO,MOSI,CS)
//SPI.begin(14,12,13,15); //SPI2 (SCK,MISO,MOSI,CS) //_spi->begin(14,12,13,15); //SPI2 (SCK,MISO,MOSI,CS)
#else #else
SPI.begin(); _spi->begin();
#endif #endif
#ifdef SPI_HAS_TRANSACTION #ifdef SPI_HAS_TRANSACTION
_settings = SPISettings(8000000, MSBFIRST, SPI_MODE0); _settings = SPISettings(8000000, MSBFIRST, SPI_MODE0);
#endif #endif
@ -325,14 +329,14 @@ void RFM69::sendFrame(uint16_t toAddress, const void* buffer, uint8_t bufferSize
// write to FIFO // write to FIFO
select(); select();
SPI.transfer(REG_FIFO | 0x80); _spi->transfer(REG_FIFO | 0x80);
SPI.transfer(bufferSize + 3); _spi->transfer(bufferSize + 3);
SPI.transfer((uint8_t)toAddress); _spi->transfer((uint8_t)toAddress);
SPI.transfer((uint8_t)_address); _spi->transfer((uint8_t)_address);
SPI.transfer(CTLbyte); _spi->transfer(CTLbyte);
for (uint8_t i = 0; i < bufferSize; i++) for (uint8_t i = 0; i < bufferSize; i++)
SPI.transfer(((uint8_t*) buffer)[i]); _spi->transfer(((uint8_t*) buffer)[i]);
unselect(); unselect();
// no need to wait for transmit mode to be ready since its handled by the radio // no need to wait for transmit mode to be ready since its handled by the radio
@ -349,12 +353,12 @@ void RFM69::interruptHandler() {
{ {
setMode(RF69_MODE_STANDBY); setMode(RF69_MODE_STANDBY);
select(); select();
SPI.transfer(REG_FIFO & 0x7F); _spi->transfer(REG_FIFO & 0x7F);
PAYLOADLEN = SPI.transfer(0); PAYLOADLEN = _spi->transfer(0);
PAYLOADLEN = PAYLOADLEN > 66 ? 66 : PAYLOADLEN; // precaution PAYLOADLEN = PAYLOADLEN > 66 ? 66 : PAYLOADLEN; // precaution
TARGETID = SPI.transfer(0); TARGETID = _spi->transfer(0);
SENDERID = SPI.transfer(0); SENDERID = _spi->transfer(0);
uint8_t CTLbyte = SPI.transfer(0); uint8_t CTLbyte = _spi->transfer(0);
TARGETID |= (uint16_t(CTLbyte) & 0x0C) << 6; //10 bit address (most significant 2 bits stored in bits(2,3) of CTL byte TARGETID |= (uint16_t(CTLbyte) & 0x0C) << 6; //10 bit address (most significant 2 bits stored in bits(2,3) of CTL byte
SENDERID |= (uint16_t(CTLbyte) & 0x03) << 8; //10 bit address (most sifnigicant 2 bits stored in bits(0,1) of CTL byte SENDERID |= (uint16_t(CTLbyte) & 0x03) << 8; //10 bit address (most sifnigicant 2 bits stored in bits(0,1) of CTL byte
@ -372,7 +376,7 @@ void RFM69::interruptHandler() {
ACK_REQUESTED = CTLbyte & RFM69_CTL_REQACK; // extract ACK-requested flag ACK_REQUESTED = CTLbyte & RFM69_CTL_REQACK; // extract ACK-requested flag
interruptHook(CTLbyte); // TWS: hook to derived class interrupt function interruptHook(CTLbyte); // TWS: hook to derived class interrupt function
for (uint8_t i = 0; i < DATALEN; i++) DATA[i] = SPI.transfer(0); for (uint8_t i = 0; i < DATALEN; i++) DATA[i] = _spi->transfer(0);
DATA[DATALEN] = 0; // add null at end of string // add null at end of string DATA[DATALEN] = 0; // add null at end of string // add null at end of string
unselect(); unselect();
@ -435,9 +439,9 @@ void RFM69::encrypt(const char* key) {
memcpy(_encryptKey, key, 16); memcpy(_encryptKey, key, 16);
#endif #endif
select(); select();
SPI.transfer(REG_AESKEY1 | 0x80); _spi->transfer(REG_AESKEY1 | 0x80);
for (uint8_t i = 0; i < 16; i++) for (uint8_t i = 0; i < 16; i++)
SPI.transfer(key[i]); _spi->transfer(key[i]);
unselect(); unselect();
} }
writeReg(REG_PACKETCONFIG2, (readReg(REG_PACKETCONFIG2) & 0xFE) | (key ? 1 : 0)); writeReg(REG_PACKETCONFIG2, (readReg(REG_PACKETCONFIG2) & 0xFE) | (key ? 1 : 0));
@ -460,8 +464,8 @@ int16_t RFM69::readRSSI(bool forceTrigger) {
uint8_t RFM69::readReg(uint8_t addr) uint8_t RFM69::readReg(uint8_t addr)
{ {
select(); select();
SPI.transfer(addr & 0x7F); _spi->transfer(addr & 0x7F);
uint8_t regval = SPI.transfer(0); uint8_t regval = _spi->transfer(0);
unselect(); unselect();
return regval; return regval;
} }
@ -469,8 +473,8 @@ uint8_t RFM69::readReg(uint8_t addr)
void RFM69::writeReg(uint8_t addr, uint8_t value) void RFM69::writeReg(uint8_t addr, uint8_t value)
{ {
select(); select();
SPI.transfer(addr | 0x80); _spi->transfer(addr | 0x80);
SPI.transfer(value); _spi->transfer(value);
unselect(); unselect();
} }
@ -483,15 +487,15 @@ void RFM69::select() {
#endif #endif
#ifdef SPI_HAS_TRANSACTION #ifdef SPI_HAS_TRANSACTION
SPI.beginTransaction(_settings); _spi->beginTransaction(_settings);
#else #else
// set RFM69 SPI settings explicitly // set RFM69 SPI settings explicitly
SPI.setDataMode(SPI_MODE0); _spi->setDataMode(SPI_MODE0);
SPI.setBitOrder(MSBFIRST); _spi->setBitOrder(MSBFIRST);
#ifdef defined(__SAMD51__) #ifdef defined(__SAMD51__)
SPI.setClockDivider(SPI_CLOCK_DIV16); _spi->setClockDivider(SPI_CLOCK_DIV16);
#else #else
SPI.setClockDivider(SPI_CLOCK_DIV2); _spi->setClockDivider(SPI_CLOCK_DIV2);
#endif #endif
#endif #endif
digitalWrite(_slaveSelectPin, LOW); digitalWrite(_slaveSelectPin, LOW);
@ -501,7 +505,7 @@ void RFM69::select() {
void RFM69::unselect() { void RFM69::unselect() {
digitalWrite(_slaveSelectPin, HIGH); digitalWrite(_slaveSelectPin, HIGH);
#ifdef SPI_HAS_TRANSACTION #ifdef SPI_HAS_TRANSACTION
SPI.endTransaction(); _spi->endTransaction();
#endif #endif
// restore SPI settings to what they were before talking to RFM69 // restore SPI settings to what they were before talking to RFM69
#if defined (SPCR) && defined (SPSR) #if defined (SPCR) && defined (SPSR)
@ -573,8 +577,8 @@ void RFM69::readAllRegs()
for (uint8_t regAddr = 1; regAddr <= 0x4F; regAddr++) for (uint8_t regAddr = 1; regAddr <= 0x4F; regAddr++)
{ {
select(); select();
SPI.transfer(regAddr & 0x7F); // send address + r/w bit _spi->transfer(regAddr & 0x7F); // send address + r/w bit
regVal = SPI.transfer(0); regVal = _spi->transfer(0);
unselect(); unselect();
Serial.print(regAddr, HEX); Serial.print(regAddr, HEX);
@ -1053,10 +1057,10 @@ void RFM69::listenModeInterruptHandler(void)
burstRemaining.l = 0; burstRemaining.l = 0;
SPI.transfer(REG_FIFO & 0x7F); _spi->transfer(REG_FIFO & 0x7F);
PAYLOADLEN = SPI.transfer(0); PAYLOADLEN = _spi->transfer(0);
PAYLOADLEN = PAYLOADLEN > 64 ? 64 : PAYLOADLEN; // precaution PAYLOADLEN = PAYLOADLEN > 64 ? 64 : PAYLOADLEN; // precaution
TARGETID = SPI.transfer(0); TARGETID = _spi->transfer(0);
if(!(_spyMode || TARGETID == _address || TARGETID == RF69_BROADCAST_ADDR) // match this node's address, or broadcast address or anything in spy mode if(!(_spyMode || TARGETID == _address || TARGETID == RF69_BROADCAST_ADDR) // match this node's address, or broadcast address or anything in spy mode
|| PAYLOADLEN < 3) // address situation could receive packets that are malformed and don't fit this library's extra fields || PAYLOADLEN < 3) // address situation could receive packets that are malformed and don't fit this library's extra fields
{ {
@ -1066,13 +1070,13 @@ void RFM69::listenModeInterruptHandler(void)
// We've read the target, and will read the sender id and two time offset bytes for a total of 4 bytes // We've read the target, and will read the sender id and two time offset bytes for a total of 4 bytes
DATALEN = PAYLOADLEN - 4; DATALEN = PAYLOADLEN - 4;
SENDERID = SPI.transfer(0); SENDERID = spi->transfer(0);
burstRemaining.b[0] = SPI.transfer(0); // and get the time remaining burstRemaining.b[0] = _spi->transfer(0); // and get the time remaining
burstRemaining.b[1] = SPI.transfer(0); burstRemaining.b[1] = _spi->transfer(0);
RF69_LISTEN_BURST_REMAINING_MS = burstRemaining.l; RF69_LISTEN_BURST_REMAINING_MS = burstRemaining.l;
for (uint8_t i = 0; i < DATALEN; i++) for (uint8_t i = 0; i < DATALEN; i++)
DATA[i] = SPI.transfer(0); DATA[i] = _spi->transfer(0);
if (DATALEN < RF69_MAX_DATA_LEN) if (DATALEN < RF69_MAX_DATA_LEN)
DATA[DATALEN] = 0; // add null at end of string DATA[DATALEN] = 0; // add null at end of string
@ -1179,17 +1183,17 @@ void RFM69::listenModeSendBurst( uint8_t targetNode, const void* buffer, uint8_t
noInterrupts(); noInterrupts();
// write to FIFO // write to FIFO
select(); select();
SPI.transfer(REG_FIFO | 0x80); _spi->transfer(REG_FIFO | 0x80);
SPI.transfer(size + 4); // two bytes for target and sender node, two bytes for the burst time remaining _spi->transfer(size + 4); // two bytes for target and sender node, two bytes for the burst time remaining
SPI.transfer(targetNode); _spi->transfer(targetNode);
SPI.transfer(_address); _spi->transfer(_address);
// We send the burst time remaining with the packet so the receiver knows how long to wait before trying to reply // We send the burst time remaining with the packet so the receiver knows how long to wait before trying to reply
SPI.transfer(timeRemaining.b[0]); _spi->transfer(timeRemaining.b[0]);
SPI.transfer(timeRemaining.b[1]); _spi->transfer(timeRemaining.b[1]);
for (uint8_t i = 0; i < size; i++) { for (uint8_t i = 0; i < size; i++) {
SPI.transfer(((uint8_t*) buffer)[i]); _spi->transfer(((uint8_t*) buffer)[i]);
} }
unselect(); unselect();
@ -1202,4 +1206,4 @@ void RFM69::listenModeSendBurst( uint8_t targetNode, const void* buffer, uint8_t
setMode(RF69_MODE_STANDBY); setMode(RF69_MODE_STANDBY);
reinitRadio(); reinitRadio();
} }
#endif #endif