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_RPiPicoSPI.cpp (4341B)

      1 #if defined(TARGET_RP2040)
      2 
      3 #include "Arduino_DataBus.h"
      4 #include "Arduino_RPiPicoSPI.h"
      5 
      6 Arduino_RPiPicoSPI::Arduino_RPiPicoSPI(int8_t dc /* = GFX_NOT_DEFINED */, int8_t cs /* = GFX_NOT_DEFINED */, int8_t sck /* = PIN_SPI0_SCK */, int8_t mosi /* = PIN_SPI0_MOSI */, int8_t miso /* = PIN_SPI0_MISO */, spi_inst_t *spi /* = spi0 */)
      7     : _dc(dc), _cs(cs), _sck(sck), _mosi(mosi), _miso(miso), _spi(spi)
      8 {
      9 }
     10 
     11 bool Arduino_RPiPicoSPI::begin(int32_t speed /* = 0 */, int8_t dataMode /* = GFX_NOT_DEFINED */)
     12 {
     13   // set SPI parameters
     14   _speed = (speed == GFX_NOT_DEFINED) ? SPI_DEFAULT_FREQ : speed;
     15   _dataMode = (dataMode != GFX_NOT_DEFINED) ? dataMode : SPI_MODE0;
     16   _spis = SPISettings(_speed, _bitOrder, _dataMode);
     17 
     18   // set pin mode
     19   pinMode(_dc, OUTPUT);
     20   digitalWrite(_dc, HIGH); // Data mode
     21   if (_cs != GFX_NOT_DEFINED)
     22   {
     23     pinMode(_cs, OUTPUT);
     24     digitalWrite(_cs, HIGH); // disable chip select
     25   }
     26 
     27   gpio_set_function(_miso, GPIO_FUNC_SPI);
     28   gpio_set_function(_sck, GPIO_FUNC_SPI);
     29   gpio_set_function(_mosi, GPIO_FUNC_SPI);
     30 
     31   // set fastIO variables
     32   _dcPinMask = digitalPinToBitMask(_dc);
     33   _dcPortSet = (PORTreg_t)&sio_hw->gpio_set;
     34   _dcPortClr = (PORTreg_t)&sio_hw->gpio_clr;
     35 
     36   if (_cs != GFX_NOT_DEFINED)
     37   {
     38     _csPinMask = digitalPinToBitMask(_cs);
     39     _csPortSet = (PORTreg_t)&sio_hw->gpio_set;
     40     _csPortClr = (PORTreg_t)&sio_hw->gpio_clr;
     41   }
     42 
     43   spi_init(_spi, _spis.getClockFreq());
     44   spi_set_format(_spi, 8, SPI_CPOL_0, SPI_CPHA_0, SPI_MSB_FIRST);
     45 
     46   return true;
     47 }
     48 
     49 void Arduino_RPiPicoSPI::beginWrite()
     50 {
     51   DC_HIGH();
     52   CS_LOW();
     53 }
     54 
     55 void Arduino_RPiPicoSPI::endWrite()
     56 {
     57   CS_HIGH();
     58 }
     59 
     60 void Arduino_RPiPicoSPI::writeCommand(uint8_t c)
     61 {
     62   DC_LOW();
     63 
     64   WRITE(c);
     65 
     66   DC_HIGH();
     67 }
     68 
     69 void Arduino_RPiPicoSPI::writeCommand16(uint16_t c)
     70 {
     71   DC_LOW();
     72 
     73   WRITE16(c);
     74 
     75   DC_HIGH();
     76 }
     77 
     78 void Arduino_RPiPicoSPI::write(uint8_t d)
     79 {
     80   WRITE(d);
     81 }
     82 
     83 void Arduino_RPiPicoSPI::write16(uint16_t d)
     84 {
     85   WRITE16(d);
     86 }
     87 
     88 void Arduino_RPiPicoSPI::writeRepeat(uint16_t p, uint32_t len)
     89 {
     90   MSB_16_SET(p, p);
     91   uint32_t bufLen = (len < SPI_MAX_PIXELS_AT_ONCE) ? len : SPI_MAX_PIXELS_AT_ONCE;
     92   uint32_t xferLen;
     93   for (uint32_t i = 0; i < bufLen; i++)
     94   {
     95     _buffer16[i] = p;
     96   }
     97 
     98   while (len)
     99   {
    100     xferLen = (bufLen < len) ? bufLen : len;
    101     WRITEBUF(_buffer, xferLen * 2);
    102     len -= xferLen;
    103   }
    104 }
    105 
    106 void Arduino_RPiPicoSPI::writePixels(uint16_t *data, uint32_t len)
    107 {
    108   uint32_t xferLen;
    109   uint8_t *p;
    110   union
    111   {
    112     uint16_t val;
    113     struct
    114     {
    115       uint8_t lsb;
    116       uint8_t msb;
    117     };
    118   } t;
    119   while (len)
    120   {
    121     xferLen = (len < SPI_MAX_PIXELS_AT_ONCE) ? len : SPI_MAX_PIXELS_AT_ONCE;
    122     p = _buffer;
    123     for (uint32_t i = 0; i < xferLen; i++)
    124     {
    125       t.val = *data++;
    126       *p++ = t.msb;
    127       *p++ = t.lsb;
    128     }
    129     len -= xferLen;
    130 
    131     xferLen += xferLen; // uint16_t to uint8_t, double length
    132     WRITEBUF(_buffer, xferLen);
    133   }
    134 }
    135 
    136 void Arduino_RPiPicoSPI::writeC8D8(uint8_t c, uint8_t d)
    137 {
    138   DC_LOW();
    139 
    140   WRITE(c);
    141 
    142   DC_HIGH();
    143 
    144   WRITE(d);
    145 }
    146 
    147 void Arduino_RPiPicoSPI::writeC8D16(uint8_t c, uint16_t d)
    148 {
    149   DC_LOW();
    150 
    151   WRITE(c);
    152 
    153   DC_HIGH();
    154 
    155   WRITE16(d);
    156 }
    157 
    158 void Arduino_RPiPicoSPI::writeC8D16D16(uint8_t c, uint16_t d1, uint16_t d2)
    159 {
    160   DC_LOW();
    161 
    162   WRITE(c);
    163 
    164   DC_HIGH();
    165 
    166   WRITE16(d1);
    167   WRITE16(d2);
    168 }
    169 
    170 void Arduino_RPiPicoSPI::writeBytes(uint8_t *data, uint32_t len)
    171 {
    172   WRITEBUF(data, len);
    173 }
    174 
    175 void Arduino_RPiPicoSPI::writePattern(uint8_t *data, uint8_t len, uint32_t repeat)
    176 {
    177   while (repeat--)
    178   {
    179     WRITEBUF(data, len);
    180   }
    181 }
    182 
    183 INLINE void Arduino_RPiPicoSPI::WRITE(uint8_t d)
    184 {
    185   spi_write_blocking(_spi, (const uint8_t *)&d, 1);
    186 }
    187 
    188 INLINE void Arduino_RPiPicoSPI::WRITE16(uint16_t d)
    189 {
    190   MSB_16_SET(d, d);
    191   spi_write_blocking(_spi, (const uint8_t *)&d, 2);
    192 }
    193 
    194 INLINE void Arduino_RPiPicoSPI::WRITEBUF(uint8_t *buf, size_t count)
    195 {
    196   spi_write_blocking(_spi, (const uint8_t *)buf, count);
    197 }
    198 
    199 /******** low level bit twiddling **********/
    200 
    201 INLINE void Arduino_RPiPicoSPI::DC_HIGH(void)
    202 {
    203   *_dcPortSet = _dcPinMask;
    204 }
    205 
    206 INLINE void Arduino_RPiPicoSPI::DC_LOW(void)
    207 {
    208   *_dcPortClr = _dcPinMask;
    209 }
    210 
    211 INLINE void Arduino_RPiPicoSPI::CS_HIGH(void)
    212 {
    213   if (_cs != GFX_NOT_DEFINED)
    214   {
    215     *_csPortSet = _csPinMask;
    216   }
    217 }
    218 
    219 INLINE void Arduino_RPiPicoSPI::CS_LOW(void)
    220 {
    221   if (_cs != GFX_NOT_DEFINED)
    222   {
    223     *_csPortClr = _csPinMask;
    224   }
    225 }
    226 
    227 #endif // #if defined(TARGET_RP2040)