arduinoprojects

git clone https://git.tarina.org/arduinoprojects
Log | Files | Refs

Adafruit_SPIDevice.h (3375B)


      1 #include <SPI.h>
      2 
      3 #ifndef Adafruit_SPIDevice_h
      4 #define Adafruit_SPIDevice_h
      5 
      6 // some modern SPI definitions don't have BitOrder enum
      7 #if (defined(__AVR__) && !defined(ARDUINO_ARCH_MEGAAVR)) ||                    \
      8     defined(ESP8266) || defined(TEENSYDUINO) || defined(SPARK) ||              \
      9     defined(ARDUINO_ARCH_SPRESENSE) || defined(MEGATINYCORE) ||                \
     10     defined(DXCORE) || defined(ARDUINO_AVR_ATmega4809) ||                      \
     11     defined(ARDUINO_AVR_ATmega4808) || defined(ARDUINO_AVR_ATmega3209) ||      \
     12     defined(ARDUINO_AVR_ATmega3208) || defined(ARDUINO_AVR_ATmega1609) ||      \
     13     defined(ARDUINO_AVR_ATmega1608) || defined(ARDUINO_AVR_ATmega809) ||       \
     14     defined(ARDUINO_AVR_ATmega808)
     15 typedef enum _BitOrder {
     16   SPI_BITORDER_MSBFIRST = MSBFIRST,
     17   SPI_BITORDER_LSBFIRST = LSBFIRST,
     18 } BitOrder;
     19 
     20 #elif defined(ESP32) || defined(__ASR6501__)
     21 
     22 // some modern SPI definitions don't have BitOrder enum and have different SPI
     23 // mode defines
     24 typedef enum _BitOrder {
     25   SPI_BITORDER_MSBFIRST = SPI_MSBFIRST,
     26   SPI_BITORDER_LSBFIRST = SPI_LSBFIRST,
     27 } BitOrder;
     28 
     29 #else
     30 // Some platforms have a BitOrder enum but its named MSBFIRST/LSBFIRST
     31 #define SPI_BITORDER_MSBFIRST MSBFIRST
     32 #define SPI_BITORDER_LSBFIRST LSBFIRST
     33 #endif
     34 
     35 #if defined(__AVR__) || defined(TEENSYDUINO)
     36 typedef volatile uint8_t BusIO_PortReg;
     37 typedef uint8_t BusIO_PortMask;
     38 #define BUSIO_USE_FAST_PINIO
     39 
     40 #elif defined(ESP8266) || defined(ESP32) || defined(__SAM3X8E__) ||            \
     41     defined(ARDUINO_ARCH_SAMD)
     42 typedef volatile uint32_t BusIO_PortReg;
     43 typedef uint32_t BusIO_PortMask;
     44 #define BUSIO_USE_FAST_PINIO
     45 
     46 #elif (defined(__arm__) || defined(ARDUINO_FEATHER52)) &&                      \
     47     !defined(ARDUINO_ARCH_MBED)
     48 typedef volatile uint32_t BusIO_PortReg;
     49 typedef uint32_t BusIO_PortMask;
     50 #if not defined(__ASR6501__)
     51 #define BUSIO_USE_FAST_PINIO
     52 #endif
     53 
     54 #else
     55 #undef BUSIO_USE_FAST_PINIO
     56 #endif
     57 
     58 /**! The class which defines how we will talk to this device over SPI **/
     59 class Adafruit_SPIDevice {
     60 public:
     61   Adafruit_SPIDevice(int8_t cspin, uint32_t freq = 1000000,
     62                      BitOrder dataOrder = SPI_BITORDER_MSBFIRST,
     63                      uint8_t dataMode = SPI_MODE0, SPIClass *theSPI = &SPI);
     64 
     65   Adafruit_SPIDevice(int8_t cspin, int8_t sck, int8_t miso, int8_t mosi,
     66                      uint32_t freq = 1000000,
     67                      BitOrder dataOrder = SPI_BITORDER_MSBFIRST,
     68                      uint8_t dataMode = SPI_MODE0);
     69   ~Adafruit_SPIDevice();
     70 
     71   bool begin(void);
     72   bool read(uint8_t *buffer, size_t len, uint8_t sendvalue = 0xFF);
     73   bool write(uint8_t *buffer, size_t len, uint8_t *prefix_buffer = NULL,
     74              size_t prefix_len = 0);
     75   bool write_then_read(uint8_t *write_buffer, size_t write_len,
     76                        uint8_t *read_buffer, size_t read_len,
     77                        uint8_t sendvalue = 0xFF);
     78 
     79   uint8_t transfer(uint8_t send);
     80   void transfer(uint8_t *buffer, size_t len);
     81   void beginTransaction(void);
     82   void endTransaction(void);
     83 
     84 private:
     85   SPIClass *_spi;
     86   SPISettings *_spiSetting;
     87   uint32_t _freq;
     88   BitOrder _dataOrder;
     89   uint8_t _dataMode;
     90 
     91   int8_t _cs, _sck, _mosi, _miso;
     92 #ifdef BUSIO_USE_FAST_PINIO
     93   BusIO_PortReg *mosiPort, *clkPort, *misoPort, *csPort;
     94   BusIO_PortMask mosiPinMask, misoPinMask, clkPinMask, csPinMask;
     95 #endif
     96   bool _begun;
     97 };
     98 
     99 #endif // Adafruit_SPIDevice_h