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_ESP32S2PAR8.cpp (5819B)

      1 #include "Arduino_ESP32S2PAR8.h"
      2 
      3 #if defined(ESP32) && (CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3)
      4 
      5 Arduino_ESP32S2PAR8::Arduino_ESP32S2PAR8(int8_t dc, int8_t cs, int8_t wr, int8_t rd)
      6     : _dc(dc), _cs(cs), _wr(wr), _rd(rd)
      7 {
      8 }
      9 
     10 bool Arduino_ESP32S2PAR8::begin(int32_t speed, int8_t dataMode)
     11 {
     12   pinMode(_dc, OUTPUT);
     13   digitalWrite(_dc, HIGH); // Data mode
     14   if (_dc >= 32)
     15   {
     16     _dcPinMask = digitalPinToBitMask(_dc);
     17     _dcPortSet = (PORTreg_t)&GPIO.out1_w1ts.val;
     18     _dcPortClr = (PORTreg_t)&GPIO.out1_w1tc.val;
     19   }
     20   else if (_dc != GFX_NOT_DEFINED)
     21   {
     22     _dcPinMask = digitalPinToBitMask(_dc);
     23     _dcPortSet = (PORTreg_t)&GPIO.out_w1ts;
     24     _dcPortClr = (PORTreg_t)&GPIO.out_w1tc;
     25   }
     26 
     27   if (_cs != GFX_NOT_DEFINED)
     28   {
     29     pinMode(_cs, OUTPUT);
     30     digitalWrite(_cs, HIGH); // disable chip select
     31   }
     32   if (_cs >= 32)
     33   {
     34     _csPinMask = digitalPinToBitMask(_cs);
     35     _csPortSet = (PORTreg_t)&GPIO.out1_w1ts.val;
     36     _csPortClr = (PORTreg_t)&GPIO.out1_w1tc.val;
     37   }
     38   else if (_cs != GFX_NOT_DEFINED)
     39   {
     40     _csPinMask = digitalPinToBitMask(_cs);
     41     _csPortSet = (PORTreg_t)&GPIO.out_w1ts;
     42     _csPortClr = (PORTreg_t)&GPIO.out_w1tc;
     43   }
     44 
     45   pinMode(_wr, OUTPUT);
     46   digitalWrite(_wr, HIGH); // Set write strobe high (inactive)
     47   if (_wr >= 32)
     48   {
     49     _wrPinMask = digitalPinToBitMask(_wr);
     50     _wrPortSet = (PORTreg_t)&GPIO.out1_w1ts.val;
     51     _wrPortClr = (PORTreg_t)&GPIO.out1_w1tc.val;
     52   }
     53   else if (_wr != GFX_NOT_DEFINED)
     54   {
     55     _wrPinMask = digitalPinToBitMask(_wr);
     56     _wrPortSet = (PORTreg_t)&GPIO.out_w1ts;
     57     _wrPortClr = (PORTreg_t)&GPIO.out_w1tc;
     58   }
     59 
     60   if (_rd != GFX_NOT_DEFINED)
     61   {
     62     pinMode(_rd, OUTPUT);
     63     digitalWrite(_rd, HIGH);
     64   }
     65   if (_rd >= 32)
     66   {
     67     _rdPinMask = digitalPinToBitMask(_rd);
     68     _rdPortSet = (PORTreg_t)&GPIO.out1_w1ts.val;
     69     _rdPortClr = (PORTreg_t)&GPIO.out1_w1tc.val;
     70   }
     71   else if (_rd != GFX_NOT_DEFINED)
     72   {
     73     _rdPinMask = digitalPinToBitMask(_rd);
     74     _rdPortSet = (PORTreg_t)&GPIO.out_w1ts;
     75     _rdPortClr = (PORTreg_t)&GPIO.out_w1tc;
     76   }
     77   else
     78   {
     79     _rdPinMask = 0;
     80     _rdPortSet = _dcPortSet;
     81     _rdPortClr = _dcPortClr;
     82   }
     83 
     84   pinMode(0, OUTPUT);
     85   pinMode(1, OUTPUT);
     86   pinMode(2, OUTPUT);
     87   pinMode(3, OUTPUT);
     88   pinMode(4, OUTPUT);
     89   pinMode(5, OUTPUT);
     90   pinMode(6, OUTPUT);
     91   pinMode(7, OUTPUT);
     92 
     93   // INIT 8-bit mask
     94   _dataClrMask = 0xFF;
     95   _dataPortSet = (PORTreg_t)&GPIO.out_w1ts;
     96   _dataPortClr = (PORTreg_t)&GPIO.out_w1tc;
     97   *_dataPortClr = _dataClrMask;
     98 
     99   return true;
    100 }
    101 
    102 void Arduino_ESP32S2PAR8::beginWrite()
    103 {
    104   DC_HIGH();
    105   CS_LOW();
    106 }
    107 
    108 void Arduino_ESP32S2PAR8::endWrite()
    109 {
    110   CS_HIGH();
    111 }
    112 
    113 void Arduino_ESP32S2PAR8::writeCommand(uint8_t c)
    114 {
    115   DC_LOW();
    116 
    117   WRITE(c);
    118 
    119   DC_HIGH();
    120 }
    121 
    122 void Arduino_ESP32S2PAR8::writeCommand16(uint16_t c)
    123 {
    124   DC_LOW();
    125 
    126   _data16.value = c;
    127   WRITE(_data16.msb);
    128   WRITE(_data16.lsb);
    129 
    130   DC_HIGH();
    131 }
    132 
    133 void Arduino_ESP32S2PAR8::write(uint8_t d)
    134 {
    135   WRITE(d);
    136 }
    137 
    138 void Arduino_ESP32S2PAR8::write16(uint16_t d)
    139 {
    140   _data16.value = d;
    141   WRITE(_data16.msb);
    142   WRITE(_data16.lsb);
    143 }
    144 
    145 void Arduino_ESP32S2PAR8::writeRepeat(uint16_t p, uint32_t len)
    146 {
    147   _data16.value = p;
    148   if (_data16.msb == _data16.lsb)
    149   {
    150     *_dataPortClr = _dataClrMask;
    151     *_dataPortSet = p;
    152     while (len--)
    153     {
    154       *_wrPortClr = _wrPinMask;
    155       *_wrPortSet = _wrPinMask;
    156       *_wrPortClr = _wrPinMask;
    157       *_wrPortSet = _wrPinMask;
    158     }
    159   }
    160   else
    161   {
    162     while (len--)
    163     {
    164       *_dataPortClr = _dataClrMask;
    165       *_dataPortSet = _data16.msb;
    166       *_wrPortClr = _wrPinMask;
    167       *_wrPortSet = _wrPinMask;
    168 
    169       *_dataPortClr = _dataClrMask;
    170       *_dataPortSet = _data16.lsb;
    171       *_wrPortClr = _wrPinMask;
    172       *_wrPortSet = _wrPinMask;
    173     }
    174   }
    175 }
    176 
    177 void Arduino_ESP32S2PAR8::writePixels(uint16_t *data, uint32_t len)
    178 {
    179   while (len--)
    180   {
    181     _data16.value = *data++;
    182     WRITE(_data16.msb);
    183     WRITE(_data16.lsb);
    184   }
    185 }
    186 
    187 void Arduino_ESP32S2PAR8::writeC8D8(uint8_t c, uint8_t d)
    188 {
    189   DC_LOW();
    190 
    191   WRITE(c);
    192 
    193   DC_HIGH();
    194 
    195   WRITE(d);
    196 }
    197 
    198 void Arduino_ESP32S2PAR8::writeC8D16(uint8_t c, uint16_t d)
    199 {
    200   DC_LOW();
    201 
    202   WRITE(c);
    203 
    204   DC_HIGH();
    205 
    206   _data16.value = d;
    207   WRITE(_data16.msb);
    208   WRITE(_data16.lsb);
    209 }
    210 
    211 void Arduino_ESP32S2PAR8::writeC8D16D16(uint8_t c, uint16_t d1, uint16_t d2)
    212 {
    213   DC_LOW();
    214 
    215   WRITE(c);
    216 
    217   DC_HIGH();
    218 
    219   _data16.value = d1;
    220   WRITE(_data16.msb);
    221   WRITE(_data16.lsb);
    222 
    223   _data16.value = d2;
    224   WRITE(_data16.msb);
    225   WRITE(_data16.lsb);
    226 }
    227 
    228 void Arduino_ESP32S2PAR8::writeBytes(uint8_t *data, uint32_t len)
    229 {
    230   while (len--)
    231   {
    232     WRITE(*data++);
    233   }
    234 }
    235 
    236 void Arduino_ESP32S2PAR8::writePattern(uint8_t *data, uint8_t len, uint32_t repeat)
    237 {
    238   while (repeat--)
    239   {
    240     writeBytes(data, len);
    241   }
    242 }
    243 
    244 void Arduino_ESP32S2PAR8::writeIndexedPixels(uint8_t *data, uint16_t *idx, uint32_t len)
    245 {
    246   while (len--)
    247   {
    248     _data16.value = idx[*data++];
    249     WRITE(_data16.msb);
    250     WRITE(_data16.lsb);
    251   }
    252 }
    253 
    254 void Arduino_ESP32S2PAR8::writeIndexedPixelsDouble(uint8_t *data, uint16_t *idx, uint32_t len)
    255 {
    256   while (len--)
    257   {
    258     _data16.value = idx[*data++];
    259     WRITE(_data16.msb);
    260     WRITE(_data16.lsb);
    261     WRITE(_data16.msb);
    262     WRITE(_data16.lsb);
    263   }
    264 }
    265 
    266 INLINE void Arduino_ESP32S2PAR8::WRITE(uint8_t d)
    267 {
    268   *_dataPortClr = _dataClrMask;
    269   *_dataPortSet = d;
    270   *_wrPortClr = _wrPinMask;
    271   *_wrPortSet = _wrPinMask;
    272 }
    273 
    274 /******** low level bit twiddling **********/
    275 
    276 INLINE void Arduino_ESP32S2PAR8::DC_HIGH(void)
    277 {
    278   *_dcPortSet = _dcPinMask;
    279 }
    280 
    281 INLINE void Arduino_ESP32S2PAR8::DC_LOW(void)
    282 {
    283   *_dcPortClr = _dcPinMask;
    284 }
    285 
    286 INLINE void Arduino_ESP32S2PAR8::CS_HIGH(void)
    287 {
    288   if (_cs != GFX_NOT_DEFINED)
    289   {
    290     *_csPortSet = _csPinMask;
    291   }
    292 }
    293 
    294 INLINE void Arduino_ESP32S2PAR8::CS_LOW(void)
    295 {
    296   if (_cs != GFX_NOT_DEFINED)
    297   {
    298     *_csPortClr = _csPinMask;
    299   }
    300 }
    301 
    302 #endif // #if defined(ESP32) && (CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3)