1 #ifndef Adafruit_SPIDevice_h 2 #define Adafruit_SPIDevice_h 6 #if !defined(SPI_INTERFACES_COUNT) || \ 7 (defined(SPI_INTERFACES_COUNT) && (SPI_INTERFACES_COUNT > 0)) 10 #define BUSIO_HAS_HW_SPI 13 enum { SPI_MODE0, SPI_MODE1, SPI_MODE2, _SPI_MODE4 };
14 typedef uint8_t SPIClass;
18 #if (defined(__AVR__) && !defined(ARDUINO_ARCH_MEGAAVR)) || \ 19 defined(ESP8266) || defined(TEENSYDUINO) || defined(SPARK) || \ 20 defined(ARDUINO_ARCH_SPRESENSE) || defined(MEGATINYCORE) || \ 21 defined(DXCORE) || defined(ARDUINO_AVR_ATmega4809) || \ 22 defined(ARDUINO_AVR_ATmega4808) || defined(ARDUINO_AVR_ATmega3209) || \ 23 defined(ARDUINO_AVR_ATmega3208) || defined(ARDUINO_AVR_ATmega1609) || \ 24 defined(ARDUINO_AVR_ATmega1608) || defined(ARDUINO_AVR_ATmega809) || \ 25 defined(ARDUINO_AVR_ATmega808) || defined(ARDUINO_ARCH_ARC32) || \ 26 defined(ARDUINO_ARCH_XMC) 28 typedef enum _BitOrder {
29 SPI_BITORDER_MSBFIRST = MSBFIRST,
30 SPI_BITORDER_LSBFIRST = LSBFIRST,
33 #elif defined(ESP32) || defined(__ASR6501__) || defined(__ASR6502__) 37 typedef enum _BitOrder {
38 SPI_BITORDER_MSBFIRST = SPI_MSBFIRST,
39 SPI_BITORDER_LSBFIRST = SPI_LSBFIRST,
44 #define SPI_BITORDER_MSBFIRST MSBFIRST 45 #define SPI_BITORDER_LSBFIRST LSBFIRST 46 typedef BitOrder BusIOBitOrder;
49 #if defined(__IMXRT1062__) // Teensy 4.x 61 #elif defined(__MBED__) || defined(__ZEPHYR__) 64 #undef BUSIO_USE_FAST_PINIO 66 #elif defined(ARDUINO_ARCH_XMC) 67 #undef BUSIO_USE_FAST_PINIO 69 #elif defined(__AVR__) || defined(TEENSYDUINO) 70 typedef volatile uint8_t BusIO_PortReg;
71 typedef uint8_t BusIO_PortMask;
72 #define BUSIO_USE_FAST_PINIO 74 #elif defined(ESP8266) || defined(ESP32) || defined(__SAM3X8E__) || \ 75 defined(ARDUINO_ARCH_SAMD) 76 typedef volatile uint32_t BusIO_PortReg;
77 typedef uint32_t BusIO_PortMask;
78 #define BUSIO_USE_FAST_PINIO 80 #elif (defined(__arm__) || defined(ARDUINO_FEATHER52)) && \ 81 !defined(ARDUINO_ARCH_RP2040) && !defined(ARDUINO_SILABS) && \ 82 !defined(ARDUINO_UNOR4_MINIMA) && !defined(ARDUINO_UNOR4_WIFI) 83 typedef volatile uint32_t BusIO_PortReg;
84 typedef uint32_t BusIO_PortMask;
85 #if !defined(__ASR6501__) && !defined(__ASR6502__) 86 #define BUSIO_USE_FAST_PINIO 90 #undef BUSIO_USE_FAST_PINIO 96 #ifdef BUSIO_HAS_HW_SPI 98 BusIOBitOrder dataOrder = SPI_BITORDER_MSBFIRST,
99 uint8_t dataMode = SPI_MODE0, SPIClass *theSPI = &SPI);
102 BusIOBitOrder dataOrder = SPI_BITORDER_MSBFIRST,
103 uint8_t dataMode = SPI_MODE0, SPIClass *theSPI =
nullptr);
106 uint32_t freq = 1000000,
107 BusIOBitOrder dataOrder = SPI_BITORDER_MSBFIRST,
108 uint8_t dataMode = SPI_MODE0);
112 bool read(uint8_t *buffer,
size_t len, uint8_t sendvalue = 0xFF);
113 bool write(
const uint8_t *buffer,
size_t len,
114 const uint8_t *prefix_buffer =
nullptr,
size_t prefix_len = 0);
116 uint8_t *read_buffer,
size_t read_len,
117 uint8_t sendvalue = 0xFF);
121 void transfer(uint8_t *buffer,
size_t len);
128 #ifdef BUSIO_HAS_HW_SPI 129 SPIClass *_spi =
nullptr;
130 SPISettings *_spiSetting =
nullptr;
132 uint8_t *_spi =
nullptr;
133 uint8_t *_spiSetting =
nullptr;
136 BusIOBitOrder _dataOrder;
138 void setChipSelect(
int value);
140 int8_t _cs, _sck, _mosi, _miso;
141 #ifdef BUSIO_USE_FAST_PINIO 142 BusIO_PortReg *mosiPort, *clkPort, *misoPort, *csPort;
143 BusIO_PortMask mosiPinMask, misoPinMask, clkPinMask, csPinMask;
148 #endif // Adafruit_SPIDevice_h bool write(const uint8_t *buffer, size_t len, const uint8_t *prefix_buffer=nullptr, size_t prefix_len=0)
Write a buffer or two to the SPI device, with transaction management.
Definition: Adafruit_SPIDevice.cpp:343
void beginTransactionWithAssertingCS()
Write a buffer or two to the SPI device, with transaction management.
Definition: Adafruit_SPIDevice.cpp:318
void endTransactionWithDeassertingCS()
Manually end a transaction (calls endTransaction if hardware SPI) with deasserting the CS pin...
Definition: Adafruit_SPIDevice.cpp:327
bool write_and_read(uint8_t *buffer, size_t len)
Write some data and read some data at the same time from SPI into the same buffer, with transaction management. This is basicaly a wrapper for transfer() with CS-pin and transaction management. This /does/ transmit-receive at the same time!
Definition: Adafruit_SPIDevice.cpp:502
uint8_t transfer(uint8_t send)
Transfer (send/receive) one byte over hard/soft SPI, without transaction management.
Definition: Adafruit_SPIDevice.cpp:273
~Adafruit_SPIDevice()
Release memory allocated in constructors.
Definition: Adafruit_SPIDevice.cpp:82
bool begin(void)
Initializes SPI bus and sets CS pin high.
Definition: Adafruit_SPIDevice.cpp:92
void endTransaction(void)
Manually end a transaction (calls endTransaction if hardware SPI)
Definition: Adafruit_SPIDevice.cpp:294
bool read(uint8_t *buffer, size_t len, uint8_t sendvalue=0xFF)
Read from SPI into a buffer from the SPI device, with transaction management.
Definition: Adafruit_SPIDevice.cpp:402
Adafruit_SPIDevice(int8_t cspin, uint32_t freq=1000000, BusIOBitOrder dataOrder=SPI_BITORDER_MSBFIRST, uint8_t dataMode=SPI_MODE0, SPIClass *theSPI=&SPI)
Create an SPI device with the given CS pin and settings.
Definition: Adafruit_SPIDevice.cpp:14
Definition: Adafruit_SPIDevice.h:94
void beginTransaction(void)
Manually begin a transaction (calls beginTransaction if hardware SPI)
Definition: Adafruit_SPIDevice.cpp:283
bool write_then_read(const uint8_t *write_buffer, size_t write_len, uint8_t *read_buffer, size_t read_len, uint8_t sendvalue=0xFF)
Write some data, then read some data from SPI into another buffer, with transaction management...
Definition: Adafruit_SPIDevice.cpp:438