acid-drop

- Hacking the planet from a LilyGo T-Deck using custom firmware
git clone git://git.acid.vegas/acid-drop.git
Log | Files | Refs | Archive | README | LICENSE

Arduino_ESP32SPI.h (3712B)

      1 /*
      2  * start rewrite from:
      3  * https://github.com/espressif/arduino-esp32.git
      4  */
      5 #include "Arduino_DataBus.h"
      6 
      7 #if defined(ESP32)
      8 
      9 #ifndef _ARDUINO_ESP32SPI_H_
     10 #define _ARDUINO_ESP32SPI_H_
     11 
     12 #include "soc/spi_struct.h"
     13 #if CONFIG_IDF_TARGET_ESP32S3
     14 #include "driver/periph_ctrl.h"
     15 #elif CONFIG_IDF_TARGET_ESP32C3
     16 #include "driver/periph_ctrl.h"
     17 #include "esp32c3/rom/gpio.h"
     18 #include "soc/periph_defs.h"
     19 #else
     20 #include "soc/dport_reg.h"
     21 #endif
     22 
     23 #define SPI_MAX_PIXELS_AT_ONCE 32
     24 
     25 #if (CONFIG_IDF_TARGET_ESP32)
     26 #define MOSI_BIT_LEN _spi->dev->mosi_dlen.usr_mosi_dbitlen
     27 #define MISO_BIT_LEN _spi->dev->miso_dlen.usr_miso_dbitlen
     28 #elif (CONFIG_IDF_TARGET_ESP32S2)
     29 #define MOSI_BIT_LEN _spi->dev->mosi_dlen.usr_mosi_bit_len
     30 #define MISO_BIT_LEN _spi->dev->miso_dlen.usr_miso_bit_len
     31 #elif (CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C3)
     32 #define MOSI_BIT_LEN _spi->dev->ms_dlen.ms_data_bitlen
     33 #define MISO_BIT_LEN _spi->dev->ms_dlen.ms_data_bitlen
     34 #endif
     35 
     36 class Arduino_ESP32SPI : public Arduino_DataBus
     37 {
     38 public:
     39 #if CONFIG_IDF_TARGET_ESP32
     40   Arduino_ESP32SPI(int8_t dc = GFX_NOT_DEFINED, int8_t cs = GFX_NOT_DEFINED, int8_t sck = GFX_NOT_DEFINED, int8_t mosi = GFX_NOT_DEFINED, int8_t miso = GFX_NOT_DEFINED, uint8_t spi_num = VSPI, bool is_shared_interface = true); // Constructor
     41 #elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
     42   Arduino_ESP32SPI(int8_t dc = GFX_NOT_DEFINED, int8_t cs = GFX_NOT_DEFINED, int8_t sck = GFX_NOT_DEFINED, int8_t mosi = GFX_NOT_DEFINED, int8_t miso = GFX_NOT_DEFINED, uint8_t spi_num = HSPI, bool is_shared_interface = true); // Constructor
     43 #else
     44   Arduino_ESP32SPI(int8_t dc = GFX_NOT_DEFINED, int8_t cs = GFX_NOT_DEFINED, int8_t sck = GFX_NOT_DEFINED, int8_t mosi = GFX_NOT_DEFINED, int8_t miso = GFX_NOT_DEFINED, uint8_t spi_num = FSPI, bool is_shared_interface = true); // Constructor
     45 #endif
     46 
     47   bool begin(int32_t speed = GFX_NOT_DEFINED, int8_t dataMode = SPI_MODE0) override;
     48   void beginWrite() override;
     49   void endWrite() override;
     50   void writeCommand(uint8_t) override;
     51   void writeCommand16(uint16_t) override;
     52   void write(uint8_t) override;
     53   void write16(uint16_t) override;
     54   void writeRepeat(uint16_t p, uint32_t len) override;
     55   void writePixels(uint16_t *data, uint32_t len) override;
     56 
     57   void writeC8D8(uint8_t c, uint8_t d) override;
     58   void writeC8D16(uint8_t c, uint16_t d) override;
     59   void writeC8D16D16(uint8_t c, uint16_t d1, uint16_t d2) override;
     60   void writeBytes(uint8_t *data, uint32_t len) override;
     61   void writePattern(uint8_t *data, uint8_t len, uint32_t repeat) override;
     62 
     63   void writeIndexedPixels(uint8_t *data, uint16_t *idx, uint32_t len) override;
     64   void writeIndexedPixelsDouble(uint8_t *data, uint16_t *idx, uint32_t len) override;
     65 
     66 protected:
     67   void flush_data_buf();
     68   INLINE void WRITE8BIT(uint8_t d);
     69   INLINE void WRITE9BIT(uint32_t d);
     70   INLINE void DC_HIGH(void);
     71   INLINE void DC_LOW(void);
     72   INLINE void CS_HIGH(void);
     73   INLINE void CS_LOW(void);
     74 
     75 private:
     76   int8_t _dc, _cs;
     77   int8_t _sck, _mosi, _miso;
     78   uint8_t _spi_num;
     79   bool _is_shared_interface;
     80   uint32_t _div = 0;
     81 
     82   PORTreg_t _dcPortSet; ///< PORT register for data/command SET
     83   PORTreg_t _dcPortClr; ///< PORT register for data/command CLEAR
     84   PORTreg_t _csPortSet; ///< PORT register for chip select SET
     85   PORTreg_t _csPortClr; ///< PORT register for chip select CLEAR
     86   uint32_t _dcPinMask;  ///< Bitmask for data/command
     87   uint32_t _csPinMask;  ///< Bitmask for chip select
     88 
     89   spi_t *_spi;
     90   uint8_t _bitOrder = SPI_MSBFIRST;
     91   union
     92   {
     93     uint8_t _buffer[SPI_MAX_PIXELS_AT_ONCE * 2] = {0};
     94     uint32_t _buffer32[SPI_MAX_PIXELS_AT_ONCE / 2];
     95   };
     96   uint16_t _data_buf_bit_idx = 0;
     97 };
     98 
     99 #endif // _ARDUINO_ESP32SPI_H_
    100 
    101 #endif // #if defined(ESP32)