Commit f285fde2 authored by TMRh20's avatar TMRh20

Minor chgs to mirror Arduino SPI transaction API

- Bring beginTransaction inline with Arduino API
- #define SPI_HAS_TRANSACTION in SPI.h
- #define the spi speed on both Arduino and RPi/Linux using
RF24_SPI_SPEED instead of different vars
- Chg beginTransaction to use bitOrder & mode instead of CSN
- Add delay directly to chipSelect function (helps ensure pin is active
before beginning SPI transactions)

#173
parent b615951a
...@@ -43,6 +43,9 @@ void RF24::csn(bool mode) ...@@ -43,6 +43,9 @@ void RF24::csn(bool mode)
_SPI.setDataMode(SPI_MODE0); _SPI.setDataMode(SPI_MODE0);
_SPI.setClockDivider(SPI_CLOCK_DIV2); _SPI.setClockDivider(SPI_CLOCK_DIV2);
#endif #endif
#elif defined (RF24_RPi)
if(!mode)
_SPI.chipSelect(csn_pin);
#endif #endif
#if !defined (RF24_LINUX) #if !defined (RF24_LINUX)
...@@ -66,9 +69,6 @@ void RF24::ce(bool level) ...@@ -66,9 +69,6 @@ void RF24::ce(bool level)
#if defined (RF24_SPI_TRANSACTIONS) #if defined (RF24_SPI_TRANSACTIONS)
_SPI.beginTransaction(SPISettings(RF_SPI_SPEED, MSBFIRST, SPI_MODE0)); _SPI.beginTransaction(SPISettings(RF_SPI_SPEED, MSBFIRST, SPI_MODE0));
#endif #endif
#if defined (RF24_LINUX)
_SPI.beginTransaction(spi_speed ? spi_speed : RF24_CLOCK_DIVIDER, csn_pin);
#endif
csn(LOW); csn(LOW);
} }
...@@ -79,9 +79,6 @@ void RF24::ce(bool level) ...@@ -79,9 +79,6 @@ void RF24::ce(bool level)
#if defined (RF24_SPI_TRANSACTIONS) #if defined (RF24_SPI_TRANSACTIONS)
_SPI.endTransaction(); _SPI.endTransaction();
#endif #endif
#if defined (RF24_LINUX)
_SPI.endTransaction();
#endif
} }
/****************************************************************************/ /****************************************************************************/
......
...@@ -68,8 +68,8 @@ private: ...@@ -68,8 +68,8 @@ private:
uint8_t ce_pin; /**< "Chip Enable" pin, activates the RX or TX role */ uint8_t ce_pin; /**< "Chip Enable" pin, activates the RX or TX role */
uint8_t csn_pin; /**< SPI Chip select */ uint8_t csn_pin; /**< SPI Chip select */
#if defined (RF24_LINUX)
uint16_t spi_speed; /**< SPI Bus Speed */ uint16_t spi_speed; /**< SPI Bus Speed */
#if defined (RF24_LINUX)
uint8_t spi_rxbuff[32+1] ; //SPI receive buffer (payload max 32 bytes) uint8_t spi_rxbuff[32+1] ; //SPI receive buffer (payload max 32 bytes)
uint8_t spi_txbuff[32+1] ; //SPI transmit buffer (payload max 32 bytes + 1 byte for the command) uint8_t spi_txbuff[32+1] ; //SPI transmit buffer (payload max 32 bytes + 1 byte for the command)
#endif #endif
......
...@@ -58,7 +58,7 @@ ...@@ -58,7 +58,7 @@
#include <Arduino.h> #include <Arduino.h>
// RF modules support 10 Mhz SPI bus speed // RF modules support 10 Mhz SPI bus speed
const uint32_t RF_SPI_SPEED = 10000000; const uint32_t RF24_SPI_SPEED = 10000000;
#if defined (ARDUINO) && !defined (__arm__) && !defined (__ARDUINO_X86__) #if defined (ARDUINO) && !defined (__arm__) && !defined (__ARDUINO_X86__)
#if defined SPI_UART #if defined SPI_UART
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
#define _SPI spi #define _SPI spi
#define RF24_BIT_ORDER BCM2835_SPI_BIT_ORDER_MSBFIRST #define RF24_BIT_ORDER BCM2835_SPI_BIT_ORDER_MSBFIRST
#define RF24_DATA_MODE BCM2835_SPI_MODE0 #define RF24_DATA_MODE BCM2835_SPI_MODE0
#define RF24_CLOCK_DIVIDER BCM2835_SPI_SPEED_8MHZ #define RF24_SPI_SPEED BCM2835_SPI_SPEED_8MHZ
// GCC a Arduino Missing // GCC a Arduino Missing
#define _BV(x) (1<<(x)) #define _BV(x) (1<<(x))
......
...@@ -16,13 +16,12 @@ void SPI::begin( int busNo ) { ...@@ -16,13 +16,12 @@ void SPI::begin( int busNo ) {
bcm2835_spi_begin(); bcm2835_spi_begin();
} }
void SPI::beginTransaction(int clock_divider, uint8_t csn_pin) { void SPI::beginTransaction(int clock_divider, uint8_t bitOrder, uint8_t mode) {
pthread_mutex_lock (&spiMutex); pthread_mutex_lock (&spiMutex);
setBitOrder(RF24_BIT_ORDER); setBitOrder(bitOrder);
setDataMode(RF24_DATA_MODE); setDataMode(mode);
setClockDivider(clock_divider); setClockDivider(clock_divider);
chipSelect(csn_pin);
delayMicroseconds(5);
} }
void SPI::endTransaction() { void SPI::endTransaction() {
...@@ -43,6 +42,7 @@ void SPI::setClockDivider(uint16_t spi_speed) { ...@@ -43,6 +42,7 @@ void SPI::setClockDivider(uint16_t spi_speed) {
void SPI::chipSelect(int csn_pin){ void SPI::chipSelect(int csn_pin){
bcm2835_spi_chipSelect(csn_pin); bcm2835_spi_chipSelect(csn_pin);
delayMicroseconds(5);
} }
SPI::~SPI() { SPI::~SPI() {
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#include "bcm2835.h" #include "bcm2835.h"
#include "interrupt.h" #include "interrupt.h"
#define SPI_HAS_TRANSACTION
class SPI { class SPI {
public: public:
...@@ -33,7 +34,7 @@ public: ...@@ -33,7 +34,7 @@ public:
static void setClockDivider(uint16_t spi_speed); static void setClockDivider(uint16_t spi_speed);
static void chipSelect(int csn_pin); static void chipSelect(int csn_pin);
static void beginTransaction(int clock_divider, uint8_t csn_pin); static void beginTransaction(int clock_divider, uint8_t bitOrder, uint8_t mode);
static void endTransaction(); static void endTransaction();
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment