Use dedicated namespace for radio SPI (#985)

This commit is contained in:
tekka
2017-11-11 15:59:54 +01:00
committed by Patrick Fallberg
parent a23ab14494
commit 4f0a4ee021
6 changed files with 48 additions and 46 deletions
+9 -9
View File
@@ -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
@@ -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
@@ -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);
@@ -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
+5 -5
View File
@@ -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>
@@ -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
+14 -13
View File
@@ -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;
@@ -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
@@ -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)
@@ -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
@@ -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
@@ -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);
@@ -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
@@ -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;
+6 -5
View File
@@ -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__)
@@ -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
+9 -9
View File
@@ -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)
@@ -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
@@ -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;
}
@@ -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);
@@ -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()) {
+5 -5
View File
@@ -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__)
@@ -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