Skip to content

Commit

Permalink
Use dedicated namespace for radio SPI (#985)
Browse files Browse the repository at this point in the history
  • Loading branch information
tekka007 authored and fallberg committed Nov 11, 2017
1 parent a23ab14 commit 4f0a4ee
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 46 deletions.
18 changes: 9 additions & 9 deletions drivers/RF24/RF24.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ LOCAL uint8_t RF24_spiMultiByteTransfer(const uint8_t cmd, uint8_t* buf, uint8_t
uint8_t status;
uint8_t* current = buf;
#if !defined(MY_SOFTSPI) && defined(SPI_HAS_TRANSACTION)
_SPI.beginTransaction(SPISettings(MY_RF24_SPI_SPEED, RF24_SPI_DATA_ORDER,
RF24_SPI_DATA_MODE));
RF24_SPI.beginTransaction(SPISettings(MY_RF24_SPI_SPEED, RF24_SPI_DATA_ORDER,
RF24_SPI_DATA_MODE));
#endif
RF24_csn(LOW);
// timing
Expand All @@ -76,7 +76,7 @@ LOCAL uint8_t RF24_spiMultiByteTransfer(const uint8_t cmd, uint8_t* buf, uint8_t
*ptx++ = *current++;
}
}
_SPI.transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, size);
RF24_SPI.transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, size);
if (readMode) {
if (size == 2) {
status = *++prx; // result is 2nd byte of receive buffer
Expand All @@ -91,21 +91,21 @@ LOCAL uint8_t RF24_spiMultiByteTransfer(const uint8_t cmd, uint8_t* buf, uint8_t
status = *prx; // status is 1st byte of receive buffer
}
#else
status = _SPI.transfer(cmd);
status = RF24_SPI.transfer(cmd);
while ( len-- ) {
if (readMode) {
status = _SPI.transfer(RF24_CMD_NOP);
status = RF24_SPI.transfer(RF24_CMD_NOP);
if (buf != NULL) {
*current++ = status;
}
} else {
status = _SPI.transfer(*current++);
status = RF24_SPI.transfer(*current++);
}
}
#endif
RF24_csn(HIGH);
#if !defined(MY_SOFTSPI) && defined(SPI_HAS_TRANSACTION)
_SPI.endTransaction();
RF24_SPI.endTransaction();
#endif
// timing
delayMicroseconds(10);
Expand Down Expand Up @@ -462,12 +462,12 @@ LOCAL bool RF24_initialize(void)
RF24_csn(HIGH);

// Initialize SPI
_SPI.begin();
RF24_SPI.begin();
#if defined(MY_RX_MESSAGE_BUFFER_FEATURE)
// assure SPI can be used from interrupt context
// Note: ESP8266 & SoftSPI currently do not support interrupt usage for SPI,
// therefore it is unsafe to use MY_RF24_IRQ_PIN with ESP8266/SoftSPI!
_SPI.usingInterrupt(digitalPinToInterrupt(MY_RF24_IRQ_PIN));
RF24_SPI.usingInterrupt(digitalPinToInterrupt(MY_RF24_IRQ_PIN));
// attach interrupt
attachInterrupt(digitalPinToInterrupt(MY_RF24_IRQ_PIN), RF24_irqHandler, FALLING);
#endif
Expand Down
10 changes: 5 additions & 5 deletions drivers/RF24/RF24.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,13 @@

#define RF24_BROADCAST_ADDRESS (255u) //!< RF24_BROADCAST_ADDRESS

#if defined(ARDUINO) && !defined(__arm__) && !defined(_SPI)
#if defined(ARDUINO) && !defined(__arm__) && !defined(RF24_SPI)
#include <SPI.h>
#if defined(MY_SOFTSPI)
SoftSPI<MY_SOFT_SPI_MISO_PIN, MY_SOFT_SPI_MOSI_PIN, MY_SOFT_SPI_SCK_PIN, RF24_SPI_DATA_MODE>
_SPI;
RF24_SPI;
#else
#define _SPI SPI //!< SPI
#define RF24_SPI SPI //!< SPI
#endif
#else
#include <stdint.h>
Expand All @@ -111,8 +111,8 @@ _SPI;
extern HardwareSPI SPI; //!< SPI
#endif

#if !defined(_SPI)
#define _SPI SPI //!< SPI
#if !defined(RF24_SPI)
#define RF24_SPI SPI //!< SPI
#endif
#endif

Expand Down
27 changes: 14 additions & 13 deletions drivers/RFM69/new/RFM69_new.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ LOCAL void RFM69_csn(const bool level)
LOCAL void RFM69_prepareSPITransaction(void)
{
#if !defined(MY_SOFTSPI) && defined(SPI_HAS_TRANSACTION)
_SPI.beginTransaction(SPISettings(MY_RFM69_SPI_SPEED, RFM69_SPI_DATA_ORDER, RFM69_SPI_DATA_MODE));
RFM69_SPI.beginTransaction(SPISettings(MY_RFM69_SPI_SPEED, RFM69_SPI_DATA_ORDER,
RFM69_SPI_DATA_MODE));
#else
#if defined(SREG)
_SREG = SREG;
Expand All @@ -80,9 +81,9 @@ LOCAL void RFM69_prepareSPITransaction(void)

// set RFM69 SPI settings
#if !defined(MY_SOFTSPI)
_SPI.setDataMode(RFM69_SPI_DATA_MODE);
_SPI.setBitOrder(RFM69_SPI_DATA_ORDER);
_SPI.setClockDivider(RFM69_CLOCK_DIV);
RFM69_SPI.setDataMode(RFM69_SPI_DATA_MODE);
RFM69_SPI.setBitOrder(RFM69_SPI_DATA_ORDER);
RFM69_SPI.setClockDivider(RFM69_CLOCK_DIV);
#endif

#endif
Expand All @@ -91,7 +92,7 @@ LOCAL void RFM69_prepareSPITransaction(void)
LOCAL void RFM69_concludeSPITransaction(void)
{
#if !defined(MY_SOFTSPI) && defined(SPI_HAS_TRANSACTION)
_SPI.endTransaction();
RFM69_SPI.endTransaction();
#else
// restore SPI settings to what they were before talking to RFM69
#if defined(SPCR) && defined(SPSR)
Expand Down Expand Up @@ -128,7 +129,7 @@ LOCAL uint8_t RFM69_spiMultiByteTransfer(const uint8_t cmd, uint8_t* buf, uint8_
*ptx++ = *current++;
}
}
_SPI.transfernb((char *)spi_txbuff, (char *)spi_rxbuff, size);
RFM69_SPI.transfernb((char *)spi_txbuff, (char *)spi_rxbuff, size);
if (aReadMode) {
if (size == 2) {
status = *++prx; // result is 2nd byte of receive buffer
Expand All @@ -143,15 +144,15 @@ LOCAL uint8_t RFM69_spiMultiByteTransfer(const uint8_t cmd, uint8_t* buf, uint8_
status = *prx; // status is 1st byte of receive buffer
}
#else
status = _SPI.transfer(cmd);
status = RFM69_SPI.transfer(cmd);
while (len--) {
if (aReadMode) {
status = _SPI.transfer((uint8_t)RFM69_NOP);
status = RFM69_SPI.transfer((uint8_t)RFM69_NOP);
if (buf != NULL) {
*current++ = status;
}
} else {
status = _SPI.transfer(*current++);
status = RFM69_SPI.transfer(*current++);
}
}
#endif
Expand Down Expand Up @@ -223,7 +224,7 @@ LOCAL bool RFM69_initialise(const uint32_t frequencyHz)
// SPI init
hwDigitalWrite(MY_RFM69_CS_PIN, HIGH);
hwPinMode(MY_RFM69_CS_PIN, OUTPUT);
_SPI.begin();
RFM69_SPI.begin();

RFM69_setConfiguration();
RFM69_setFrequency(frequencyHz);
Expand All @@ -236,7 +237,7 @@ LOCAL bool RFM69_initialise(const uint32_t frequencyHz)
// IRQ
hwPinMode(MY_RFM69_IRQ_PIN, INPUT);
#if defined (SPI_HAS_TRANSACTION) && !defined (ESP8266) && !defined (MY_SOFTSPI)
_SPI.usingInterrupt(digitalPinToInterrupt(MY_RFM69_IRQ_PIN));
RFM69_SPI.usingInterrupt(digitalPinToInterrupt(MY_RFM69_IRQ_PIN));
#endif

#ifdef MY_DEBUG_VERBOSE_RFM69_REGISTERS
Expand Down Expand Up @@ -302,14 +303,14 @@ LOCAL void RFM69_interruptHandler(void)
RFM69.dataReceived = !RFM69.ackReceived;
}
#else
_SPI.transfer(RFM69_REG_FIFO & RFM69_READ_REGISTER);
RFM69_SPI.transfer(RFM69_REG_FIFO & RFM69_READ_REGISTER);
// set reading pointer
uint8_t* current = (uint8_t*)&RFM69.currentPacket;
bool headerRead = false;
// first read header
uint8_t readingLength = RFM69_HEADER_LEN;
while (readingLength--) {
*current++ = _SPI.transfer((uint8_t)0x00);
*current++ = RFM69_SPI.transfer((uint8_t)0x00);
if (!readingLength && !headerRead) {
// header read
headerRead = true;
Expand Down
11 changes: 6 additions & 5 deletions drivers/RFM69/new/RFM69_new.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,13 @@
#define RFM69_CLOCK_DIV SPI_CLOCK_DIV256 //!< SPI clock divider 256
#endif

#if defined (ARDUINO) && !defined (__arm__) && !defined (_SPI)
#if defined (ARDUINO) && !defined (__arm__) && !defined (RFM69_SPI)
#include <SPI.h>
#if defined(MY_SOFTSPI)
SoftSPI<MY_SOFT_SPI_MISO_PIN, MY_SOFT_SPI_MOSI_PIN, MY_SOFT_SPI_SCK_PIN, RFM69_SPI_DATA_MODE> _SPI;
SoftSPI<MY_SOFT_SPI_MISO_PIN, MY_SOFT_SPI_MOSI_PIN, MY_SOFT_SPI_SCK_PIN, RFM69_SPI_DATA_MODE>
RFM69_SPI;
#else
#define _SPI SPI
#define RFM69_SPI SPI
#endif
#else
#if defined(__arm__) || defined(__linux__)
Expand All @@ -134,8 +135,8 @@ SoftSPI<MY_SOFT_SPI_MISO_PIN, MY_SOFT_SPI_MOSI_PIN, MY_SOFT_SPI_SCK_PIN, RFM69_S
extern HardwareSPI SPI; //!< SPI
#endif

#if !defined(_SPI)
#define _SPI SPI //!< SPI
#if !defined(RFM69_SPI)
#define RFM69_SPI SPI //!< SPI
#endif
#endif

Expand Down
18 changes: 9 additions & 9 deletions drivers/RFM95/RFM95.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ LOCAL uint8_t RFM95_spiMultiByteTransfer(const uint8_t cmd, uint8_t* buf, uint8_
uint8_t status;
uint8_t* current = buf;
#if !defined(MY_SOFTSPI) && defined(SPI_HAS_TRANSACTION)
_SPI.beginTransaction(SPISettings(MY_RFM95_SPI_SPEED, MY_RFM95_SPI_DATA_ORDER,
MY_RFM95_SPI_DATA_MODE));
RFM95_SPI.beginTransaction(SPISettings(MY_RFM95_SPI_SPEED, MY_RFM95_SPI_DATA_ORDER,
MY_RFM95_SPI_DATA_MODE));
#endif
RFM95_csn(LOW);
#if defined(LINUX_SPI_BCM)
Expand All @@ -76,7 +76,7 @@ LOCAL uint8_t RFM95_spiMultiByteTransfer(const uint8_t cmd, uint8_t* buf, uint8_
*ptx++ = *current++;
}
}
_SPI.transfernb((char *)spi_txbuff, (char *)spi_rxbuff, size);
RFM95_SPI.transfernb((char *)spi_txbuff, (char *)spi_rxbuff, size);
if (aReadMode) {
if (size == 2) {
status = *++prx; // result is 2nd byte of receive buffer
Expand All @@ -91,21 +91,21 @@ LOCAL uint8_t RFM95_spiMultiByteTransfer(const uint8_t cmd, uint8_t* buf, uint8_
status = *prx; // status is 1st byte of receive buffer
}
#else
status = _SPI.transfer(cmd);
status = RFM95_SPI.transfer(cmd);
while (len--) {
if (aReadMode) {
status = _SPI.transfer((uint8_t)RFM95_NOP);
status = RFM95_SPI.transfer((uint8_t)RFM95_NOP);
if (buf != NULL) {
*current++ = status;
}
} else {
status = _SPI.transfer(*current++);
status = RFM95_SPI.transfer(*current++);
}
}
#endif
RFM95_csn(HIGH);
#if !defined(MY_SOFTSPI) && defined(SPI_HAS_TRANSACTION)
_SPI.endTransaction();
RFM95_SPI.endTransaction();
#endif
return status;
}
Expand Down Expand Up @@ -165,7 +165,7 @@ LOCAL bool RFM95_initialise(const uint32_t frequencyHz)
// SPI init
hwDigitalWrite(MY_RFM95_CS_PIN, HIGH);
hwPinMode(MY_RFM95_CS_PIN, OUTPUT);
_SPI.begin();
RFM95_SPI.begin();

// Set LoRa mode (during sleep mode)
RFM95_writeReg(RFM95_REG_01_OP_MODE, RFM95_MODE_SLEEP | RFM95_LONG_RANGE_MODE);
Expand All @@ -188,7 +188,7 @@ LOCAL bool RFM95_initialise(const uint32_t frequencyHz)
// IRQ
hwPinMode(MY_RFM95_IRQ_PIN, INPUT);
#if defined(SPI_HAS_TRANSACTION) && !defined(ESP8266) && !defined(MY_SOFTSPI)
_SPI.usingInterrupt(MY_RFM95_IRQ_NUM);
RFM95_SPI.usingInterrupt(MY_RFM95_IRQ_NUM);
#endif

if (!RFM95_sanityCheck()) {
Expand Down
10 changes: 5 additions & 5 deletions drivers/RFM95/RFM95.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,13 @@
#define MY_RFM95_SPI_DATA_ORDER MSBFIRST //!< SPI data order
#define MY_RFM95_SPI_DATA_MODE SPI_MODE0 //!< SPI mode

#if defined (ARDUINO) && !defined (__arm__) && !defined (_SPI)
#if defined (ARDUINO) && !defined (__arm__) && !defined (RFM95_SPI)
#include <SPI.h>
#if defined(MY_SOFTSPI)
SoftSPI<MY_SOFT_SPI_MISO_PIN, MY_SOFT_SPI_MOSI_PIN, MY_SOFT_SPI_SCK_PIN, MY_RF24_SPI_DATA_MODE>
_SPI;
RFM95_SPI;
#else
#define _SPI SPI //!< SPI
#define RFM95_SPI SPI //!< SPI
#endif
#else
#if defined(__arm__) || defined(__linux__)
Expand All @@ -129,8 +129,8 @@ _SPI;
extern HardwareSPI SPI; //!< SPI
#endif

#if !defined(_SPI)
#define _SPI SPI //!< SPI
#if !defined(RFM95_SPI)
#define RFM95_SPI SPI //!< SPI
#endif
#endif

Expand Down

0 comments on commit 4f0a4ee

Please sign in to comment.