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)
_SPI.setDataMode(SPI_MODE0);
_SPI.setClockDivider(SPI_CLOCK_DIV2);
#endif
#elif defined (RF24_RPi)
if(!mode)
_SPI.chipSelect(csn_pin);
#endif
#if !defined (RF24_LINUX)
......@@ -66,9 +69,6 @@ void RF24::ce(bool level)
#if defined (RF24_SPI_TRANSACTIONS)
_SPI.beginTransaction(SPISettings(RF_SPI_SPEED, MSBFIRST, SPI_MODE0));
#endif
#if defined (RF24_LINUX)
_SPI.beginTransaction(spi_speed ? spi_speed : RF24_CLOCK_DIVIDER, csn_pin);
#endif
csn(LOW);
}
......@@ -79,9 +79,6 @@ void RF24::ce(bool level)
#if defined (RF24_SPI_TRANSACTIONS)
_SPI.endTransaction();
#endif
#if defined (RF24_LINUX)
_SPI.endTransaction();
#endif
}
/****************************************************************************/
......
......@@ -68,8 +68,8 @@ private:
uint8_t ce_pin; /**< "Chip Enable" pin, activates the RX or TX role */
uint8_t csn_pin; /**< SPI Chip select */
#if defined (RF24_LINUX)
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_txbuff[32+1] ; //SPI transmit buffer (payload max 32 bytes + 1 byte for the command)
#endif
......
......@@ -58,7 +58,7 @@
#include <Arduino.h>
// 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 SPI_UART
......
......@@ -16,7 +16,7 @@
#define _SPI spi
#define RF24_BIT_ORDER BCM2835_SPI_BIT_ORDER_MSBFIRST
#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
#define _BV(x) (1<<(x))
......
......@@ -16,13 +16,12 @@ void SPI::begin( int busNo ) {
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);
setBitOrder(RF24_BIT_ORDER);
setDataMode(RF24_DATA_MODE);
setBitOrder(bitOrder);
setDataMode(mode);
setClockDivider(clock_divider);
chipSelect(csn_pin);
delayMicroseconds(5);
}
void SPI::endTransaction() {
......@@ -43,6 +42,7 @@ void SPI::setClockDivider(uint16_t spi_speed) {
void SPI::chipSelect(int csn_pin){
bcm2835_spi_chipSelect(csn_pin);
delayMicroseconds(5);
}
SPI::~SPI() {
......
......@@ -14,6 +14,7 @@
#include "bcm2835.h"
#include "interrupt.h"
#define SPI_HAS_TRANSACTION
class SPI {
public:
......@@ -33,7 +34,7 @@ public:
static void setClockDivider(uint16_t spi_speed);
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();
};
......
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