JM9 XRCU Board 0.1.3a
Libraries API Reference
Loading...
Searching...
No Matches
Adafruit_SPIDevice.h
1#ifndef Adafruit_SPIDevice_h
2#define Adafruit_SPIDevice_h
3
4#include <Arduino.h>
5
6#if !defined(SPI_INTERFACES_COUNT) || \
7 (defined(SPI_INTERFACES_COUNT) && (SPI_INTERFACES_COUNT > 0))
8// HW SPI available
9#include <SPI.h>
10#define BUSIO_HAS_HW_SPI
11#else
12// SW SPI ONLY
13enum { SPI_MODE0, SPI_MODE1, SPI_MODE2, _SPI_MODE4 };
14typedef uint8_t SPIClass;
15#endif
16
17// some modern SPI definitions don't have BitOrder enum
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) || defined(ARDUINO_SILABS)
27
28typedef enum _BitOrder {
29 SPI_BITORDER_MSBFIRST = MSBFIRST,
30 SPI_BITORDER_LSBFIRST = LSBFIRST,
31} BusIOBitOrder;
32
33#elif defined(ESP32) || defined(__ASR6501__) || defined(__ASR6502__)
34
35// some modern SPI definitions don't have BitOrder enum and have different SPI
36// mode defines
37typedef enum _BitOrder {
38 SPI_BITORDER_MSBFIRST = SPI_MSBFIRST,
39 SPI_BITORDER_LSBFIRST = SPI_LSBFIRST,
40} BusIOBitOrder;
41
42#else
43// Some platforms have a BitOrder enum but its named MSBFIRST/LSBFIRST
44#define SPI_BITORDER_MSBFIRST MSBFIRST
45#define SPI_BITORDER_LSBFIRST LSBFIRST
46typedef BitOrder BusIOBitOrder;
47#endif
48
49#if defined(__IMXRT1062__) // Teensy 4.x
50// *Warning* I disabled the usage of FAST_PINIO as the set/clear operations
51// used in the cpp file are not atomic and can effect multiple IO pins
52// and if an interrupt happens in between the time the code reads the register
53// and writes out the updated value, that changes one or more other IO pins
54// on that same IO port, those change will be clobbered when the updated
55// values are written back. A fast version can be implemented that uses the
56// ports set and clear registers which are atomic.
57// typedef volatile uint32_t BusIO_PortReg;
58// typedef uint32_t BusIO_PortMask;
59//#define BUSIO_USE_FAST_PINIO
60
61#elif defined(ARDUINO_ARCH_XMC)
62#undef BUSIO_USE_FAST_PINIO
63
64#elif defined(__AVR__) || defined(TEENSYDUINO)
65typedef volatile uint8_t BusIO_PortReg;
66typedef uint8_t BusIO_PortMask;
67#define BUSIO_USE_FAST_PINIO
68
69#elif defined(ESP8266) || defined(ESP32) || defined(__SAM3X8E__) || \
70 defined(ARDUINO_ARCH_SAMD)
71typedef volatile uint32_t BusIO_PortReg;
72typedef uint32_t BusIO_PortMask;
73#define BUSIO_USE_FAST_PINIO
74
75#elif (defined(__arm__) || defined(ARDUINO_FEATHER52)) && \
76 !defined(ARDUINO_ARCH_MBED) && !defined(ARDUINO_ARCH_RP2040) && \
77 !defined(ARDUINO_SILABS)
78typedef volatile uint32_t BusIO_PortReg;
79typedef uint32_t BusIO_PortMask;
80#if !defined(__ASR6501__) && !defined(__ASR6502__)
81#define BUSIO_USE_FAST_PINIO
82#endif
83
84#else
85#undef BUSIO_USE_FAST_PINIO
86#endif
87
90public:
91#ifdef BUSIO_HAS_HW_SPI
92 Adafruit_SPIDevice(int8_t cspin, uint32_t freq = 1000000,
93 BusIOBitOrder dataOrder = SPI_BITORDER_MSBFIRST,
94 uint8_t dataMode = SPI_MODE0, SPIClass *theSPI = &SPI);
95#else
96 Adafruit_SPIDevice(int8_t cspin, uint32_t freq = 1000000,
97 BusIOBitOrder dataOrder = SPI_BITORDER_MSBFIRST,
98 uint8_t dataMode = SPI_MODE0, SPIClass *theSPI = nullptr);
99#endif
100 Adafruit_SPIDevice(int8_t cspin, int8_t sck, int8_t miso, int8_t mosi,
101 uint32_t freq = 1000000,
102 BusIOBitOrder dataOrder = SPI_BITORDER_MSBFIRST,
103 uint8_t dataMode = SPI_MODE0);
105
106 bool begin(void);
107 bool read(uint8_t *buffer, size_t len, uint8_t sendvalue = 0xFF);
108 bool write(const uint8_t *buffer, size_t len,
109 const uint8_t *prefix_buffer = nullptr, size_t prefix_len = 0);
110 bool write_then_read(const uint8_t *write_buffer, size_t write_len,
111 uint8_t *read_buffer, size_t read_len,
112 uint8_t sendvalue = 0xFF);
113 bool write_and_read(uint8_t *buffer, size_t len);
114
115 uint8_t transfer(uint8_t send);
116 void transfer(uint8_t *buffer, size_t len);
117 void beginTransaction(void);
118 void endTransaction(void);
121
122private:
123#ifdef BUSIO_HAS_HW_SPI
124 SPIClass *_spi = nullptr;
125 SPISettings *_spiSetting = nullptr;
126#else
127 uint8_t *_spi = nullptr;
128 uint8_t *_spiSetting = nullptr;
129#endif
130 uint32_t _freq;
131 BusIOBitOrder _dataOrder;
132 uint8_t _dataMode;
133 void setChipSelect(int value);
134
135 int8_t _cs, _sck, _mosi, _miso;
136#ifdef BUSIO_USE_FAST_PINIO
137 BusIO_PortReg *mosiPort, *clkPort, *misoPort, *csPort;
138 BusIO_PortMask mosiPinMask, misoPinMask, clkPinMask, csPinMask;
139#endif
140 bool _begun;
141};
142
143#endif // Adafruit_SPIDevice_h
void beginTransactionWithAssertingCS()
Write a buffer or two to the SPI device, with transaction management.
Definition Adafruit_SPIDevice.cpp:318
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
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
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
void endTransaction(void)
Manually end a transaction (calls endTransaction if hardware SPI)
Definition Adafruit_SPIDevice.cpp:294
bool begin(void)
Initializes SPI bus and sets CS pin high.
Definition Adafruit_SPIDevice.cpp:92
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,...
Definition Adafruit_SPIDevice.cpp:502
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
~Adafruit_SPIDevice()
Release memory allocated in constructors.
Definition Adafruit_SPIDevice.cpp:82
uint8_t transfer(uint8_t send)
Transfer (send/receive) one byte over hard/soft SPI, without transaction management.
Definition Adafruit_SPIDevice.cpp:273
void beginTransaction(void)
Manually begin a transaction (calls beginTransaction if hardware SPI)
Definition Adafruit_SPIDevice.cpp:283
void endTransactionWithDeassertingCS()
Manually end a transaction (calls endTransaction if hardware SPI) with deasserting the CS pin.
Definition Adafruit_SPIDevice.cpp:327