yikes everyone has a differnet way of doing SPI bit order
This commit is contained in:
parent
dbe2441417
commit
e61d6bac17
|
|
@ -7,7 +7,7 @@
|
||||||
* @brief Create an SPI device with the given CS pin and settins
|
* @brief Create an SPI device with the given CS pin and settins
|
||||||
* @param cspin The arduino pin number to use for chip select
|
* @param cspin The arduino pin number to use for chip select
|
||||||
* @param freq The SPI clock frequency to use, defaults to 1MHz
|
* @param freq The SPI clock frequency to use, defaults to 1MHz
|
||||||
* @param dataOrder The SPI data order to use for bits within each byte, defaults to SPI_MSBFIRST
|
* @param dataOrder The SPI data order to use for bits within each byte, defaults to SPI_BITORDER_MSBFIRST
|
||||||
* @param dataMode The SPI mode to use, defaults to SPI_MODE0
|
* @param dataMode The SPI mode to use, defaults to SPI_MODE0
|
||||||
* @param theSPI The SPI bus to use, defaults to &theSPI
|
* @param theSPI The SPI bus to use, defaults to &theSPI
|
||||||
*/
|
*/
|
||||||
|
|
@ -29,7 +29,7 @@ Adafruit_SPIDevice::Adafruit_SPIDevice(int8_t cspin, uint32_t freq, BitOrder dat
|
||||||
* @param misopin The arduino pin number to use for MISO, set to -1 if not used
|
* @param misopin The arduino pin number to use for MISO, set to -1 if not used
|
||||||
* @param mosipin The arduino pin number to use for MOSI, set to -1 if not used
|
* @param mosipin The arduino pin number to use for MOSI, set to -1 if not used
|
||||||
* @param freq The SPI clock frequency to use, defaults to 1MHz
|
* @param freq The SPI clock frequency to use, defaults to 1MHz
|
||||||
* @param dataOrder The SPI data order to use for bits within each byte, defaults to SPI_MSBFIRST
|
* @param dataOrder The SPI data order to use for bits within each byte, defaults to SPI_BITORDER_MSBFIRST
|
||||||
* @param dataMode The SPI mode to use, defaults to SPI_MODE0
|
* @param dataMode The SPI mode to use, defaults to SPI_MODE0
|
||||||
*/
|
*/
|
||||||
Adafruit_SPIDevice::Adafruit_SPIDevice(int8_t cspin, int8_t sckpin, int8_t misopin, int8_t mosipin,
|
Adafruit_SPIDevice::Adafruit_SPIDevice(int8_t cspin, int8_t sckpin, int8_t misopin, int8_t mosipin,
|
||||||
|
|
@ -97,7 +97,7 @@ void Adafruit_SPIDevice::transfer(uint8_t *buffer, size_t len) {
|
||||||
uint8_t reply = 0;
|
uint8_t reply = 0;
|
||||||
uint8_t send = buffer[i];
|
uint8_t send = buffer[i];
|
||||||
|
|
||||||
if (_dataOrder == LSBFIRST) {
|
if (_dataOrder == SPI_BITORDER_LSBFIRST) {
|
||||||
// LSB is rare, if it happens we'll just flip the bits around for them
|
// LSB is rare, if it happens we'll just flip the bits around for them
|
||||||
uint8_t temp = 0;
|
uint8_t temp = 0;
|
||||||
for (uint8_t b=0; b<8; b++) {
|
for (uint8_t b=0; b<8; b++) {
|
||||||
|
|
@ -125,7 +125,7 @@ void Adafruit_SPIDevice::transfer(uint8_t *buffer, size_t len) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_dataOrder == LSBFIRST) {
|
if (_dataOrder == SPI_BITORDER_LSBFIRST) {
|
||||||
// LSB is rare, if it happens we'll just flip the bits around for them
|
// LSB is rare, if it happens we'll just flip the bits around for them
|
||||||
uint8_t temp = 0;
|
uint8_t temp = 0;
|
||||||
for (uint8_t b=0; b<8; b++) {
|
for (uint8_t b=0; b<8; b++) {
|
||||||
|
|
|
||||||
|
|
@ -3,14 +3,24 @@
|
||||||
#ifndef Adafruit_SPIDevice_h
|
#ifndef Adafruit_SPIDevice_h
|
||||||
#define Adafruit_SPIDevice_h
|
#define Adafruit_SPIDevice_h
|
||||||
|
|
||||||
#if defined(__AVR__) || defined(ESP32) || defined(ESP8266) // modern SPI definitions have a BitOrder enum
|
#if defined(__AVR__) || defined(ESP8266) // some modern SPI definitions don't have BitOrder enum
|
||||||
typedef enum _BitOrder {
|
typedef enum _BitOrder {
|
||||||
SPI_MSBFIRST = MSBFIRST,
|
SPI_BITORDER_MSBFIRST = MSBFIRST,
|
||||||
SPI_LSBFIRST = LSBFIRST,
|
SPI_BITORDER_LSBFIRST = LSBFIRST,
|
||||||
} BitOrder;
|
} BitOrder;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(ESP32)
|
#if defined(ESP32)
|
||||||
#define MSBFIRST SPI_MSBFIRST
|
typedef enum _BitOrder {
|
||||||
|
SPI_BITORDER_MSBFIRST = SPI_MSBFIRST,
|
||||||
|
SPI_BITORDER_LSBFIRST = SPI_LSBFIRST,
|
||||||
|
} BitOrder;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Some platforms have a BitOrder enum but its named MSBFIRST/LSBFIRST
|
||||||
|
#if defined(ARDUINO_ARCH_SAMD)
|
||||||
|
#define SPI_BITORDER_MSBFIRST MSBFIRST
|
||||||
|
#define SPI_BITORDER_LSBFIRST LSBFIRST
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
///< The class which defines how we will talk to this device over SPI
|
///< The class which defines how we will talk to this device over SPI
|
||||||
|
|
@ -18,13 +28,13 @@ class Adafruit_SPIDevice {
|
||||||
public:
|
public:
|
||||||
Adafruit_SPIDevice(int8_t cspin,
|
Adafruit_SPIDevice(int8_t cspin,
|
||||||
uint32_t freq=1000000,
|
uint32_t freq=1000000,
|
||||||
BitOrder dataOrder=SPI_MSBFIRST,
|
BitOrder dataOrder=SPI_BITORDER_MSBFIRST,
|
||||||
uint8_t dataMode=SPI_MODE0,
|
uint8_t dataMode=SPI_MODE0,
|
||||||
SPIClass *theSPI=&SPI);
|
SPIClass *theSPI=&SPI);
|
||||||
|
|
||||||
Adafruit_SPIDevice(int8_t cspin, int8_t sck, int8_t miso, int8_t mosi,
|
Adafruit_SPIDevice(int8_t cspin, int8_t sck, int8_t miso, int8_t mosi,
|
||||||
uint32_t freq=1000000,
|
uint32_t freq=1000000,
|
||||||
BitOrder dataOrder=SPI_MSBFIRST,
|
BitOrder dataOrder=SPI_BITORDER_MSBFIRST,
|
||||||
uint8_t dataMode=SPI_MODE0);
|
uint8_t dataMode=SPI_MODE0);
|
||||||
|
|
||||||
bool begin(void);
|
bool begin(void);
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,29 @@
|
||||||
|
#include <Adafruit_SPIDevice.h>
|
||||||
|
|
||||||
|
#define SPIDEVICE_CS 10
|
||||||
|
Adafruit_SPIDevice spi_dev = Adafruit_SPIDevice(SPIDEVICE_CS, 100000, SPI_BITORDER_MSBFIRST, SPI_MODE1);
|
||||||
|
//Adafruit_SPIDevice spi_dev = Adafruit_SPIDevice(SPIDEVICE_CS, 13, 12, 11, 100000, SPI_BITORDER_MSBFIRST, SPI_MODE1);
|
||||||
|
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
while (!Serial) { delay(10); }
|
||||||
|
Serial.begin(115200);
|
||||||
|
Serial.println("SPI device mode test");
|
||||||
|
|
||||||
|
if (!spi_dev.begin()) {
|
||||||
|
Serial.println("Could not initialize SPI device");
|
||||||
|
while (1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
Serial.println("\n\nTransfer test");
|
||||||
|
for (uint16_t x=0; x<=0xFF; x++) {
|
||||||
|
uint8_t i = x;
|
||||||
|
Serial.print("0x"); Serial.print(i, HEX);
|
||||||
|
spi_dev.read(&i, 1, i);
|
||||||
|
Serial.print("/"); Serial.print(i, HEX);
|
||||||
|
Serial.print(", ");
|
||||||
|
delay(25);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue