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_ESP32PAR8.cpp (9226B)

      1 #include "Arduino_ESP32PAR8.h"
      2 
      3 #if defined(ESP32) && (CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3)
      4 
      5 Arduino_ESP32PAR8::Arduino_ESP32PAR8(
      6     int8_t dc, int8_t cs, int8_t wr, int8_t rd,
      7     int8_t d0, int8_t d1, int8_t d2, int8_t d3, int8_t d4, int8_t d5, int8_t d6, int8_t d7)
      8     : _dc(dc), _cs(cs), _wr(wr), _rd(rd),
      9       _d0(d0), _d1(d1), _d2(d2), _d3(d3), _d4(d4), _d5(d5), _d6(d6), _d7(d7)
     10 {
     11 }
     12 
     13 bool Arduino_ESP32PAR8::begin(int32_t speed, int8_t dataMode)
     14 {
     15   UNUSED(speed);
     16   UNUSED(dataMode);
     17 
     18   pinMode(_dc, OUTPUT);
     19   digitalWrite(_dc, HIGH); // Data mode
     20   if (_dc >= 32)
     21   {
     22     _dcPinMask = digitalPinToBitMask(_dc);
     23     _dcPortSet = (PORTreg_t)&GPIO.out1_w1ts.val;
     24     _dcPortClr = (PORTreg_t)&GPIO.out1_w1tc.val;
     25   }
     26   else
     27   {
     28     _dcPinMask = digitalPinToBitMask(_dc);
     29     _dcPortSet = (PORTreg_t)&GPIO.out_w1ts;
     30     _dcPortClr = (PORTreg_t)&GPIO.out_w1tc;
     31   }
     32 
     33   if (_cs != GFX_NOT_DEFINED)
     34   {
     35     pinMode(_cs, OUTPUT);
     36     digitalWrite(_cs, HIGH); // disable chip select
     37   }
     38   if (_cs >= 32)
     39   {
     40     _csPinMask = digitalPinToBitMask(_cs);
     41     _csPortSet = (PORTreg_t)&GPIO.out1_w1ts.val;
     42     _csPortClr = (PORTreg_t)&GPIO.out1_w1tc.val;
     43   }
     44   else if (_cs != GFX_NOT_DEFINED)
     45   {
     46     _csPinMask = digitalPinToBitMask(_cs);
     47     _csPortSet = (PORTreg_t)&GPIO.out_w1ts;
     48     _csPortClr = (PORTreg_t)&GPIO.out_w1tc;
     49   }
     50 
     51   pinMode(_wr, OUTPUT);
     52   digitalWrite(_wr, HIGH); // Set write strobe high (inactive)
     53   if (_wr >= 32)
     54   {
     55     _wrPinMask = digitalPinToBitMask(_wr);
     56     _wrPortSet = (PORTreg_t)&GPIO.out1_w1ts.val;
     57     _wrPortClr = (PORTreg_t)&GPIO.out1_w1tc.val;
     58   }
     59   else
     60   {
     61     _wrPinMask = digitalPinToBitMask(_wr);
     62     _wrPortSet = (PORTreg_t)&GPIO.out_w1ts;
     63     _wrPortClr = (PORTreg_t)&GPIO.out_w1tc;
     64   }
     65 
     66   if (_rd != GFX_NOT_DEFINED)
     67   {
     68     pinMode(_rd, OUTPUT);
     69     digitalWrite(_rd, HIGH);
     70   }
     71 
     72   pinMode(_d0, OUTPUT);
     73   pinMode(_d1, OUTPUT);
     74   pinMode(_d2, OUTPUT);
     75   pinMode(_d3, OUTPUT);
     76   pinMode(_d4, OUTPUT);
     77   pinMode(_d5, OUTPUT);
     78   pinMode(_d6, OUTPUT);
     79   pinMode(_d7, OUTPUT);
     80 
     81   // INIT 8-bit mask
     82   _data1ClrMask = 0;
     83   _data2ClrMask = 0;
     84   if (_d0 >= 32)
     85   {
     86     _data2ClrMask |= digitalPinToBitMask(_d0);
     87   }
     88   else
     89   {
     90     _data1ClrMask |= digitalPinToBitMask(_d0);
     91   }
     92   if (_d1 >= 32)
     93   {
     94     _data2ClrMask |= digitalPinToBitMask(_d1);
     95   }
     96   else
     97   {
     98     _data1ClrMask |= digitalPinToBitMask(_d1);
     99   }
    100   if (_d2 >= 32)
    101   {
    102     _data2ClrMask |= digitalPinToBitMask(_d2);
    103   }
    104   else
    105   {
    106     _data1ClrMask |= digitalPinToBitMask(_d2);
    107   }
    108   if (_d3 >= 32)
    109   {
    110     _data2ClrMask |= digitalPinToBitMask(_d3);
    111   }
    112   else
    113   {
    114     _data1ClrMask |= digitalPinToBitMask(_d3);
    115   }
    116   if (_d4 >= 32)
    117   {
    118     _data2ClrMask |= digitalPinToBitMask(_d4);
    119   }
    120   else
    121   {
    122     _data1ClrMask |= digitalPinToBitMask(_d4);
    123   }
    124   if (_d5 >= 32)
    125   {
    126     _data2ClrMask |= digitalPinToBitMask(_d5);
    127   }
    128   else
    129   {
    130     _data1ClrMask |= digitalPinToBitMask(_d5);
    131   }
    132   if (_d6 >= 32)
    133   {
    134     _data2ClrMask |= digitalPinToBitMask(_d6);
    135   }
    136   else
    137   {
    138     _data1ClrMask |= digitalPinToBitMask(_d6);
    139   }
    140   if (_d7 >= 32)
    141   {
    142     _data2ClrMask |= digitalPinToBitMask(_d7);
    143   }
    144   else
    145   {
    146     _data1ClrMask |= digitalPinToBitMask(_d7);
    147   }
    148 
    149   for (int32_t c = 0; c < 256; c++)
    150   {
    151     _xset_mask1[c] = 0;
    152     _xset_mask2[c] = 0;
    153     if (c & 0x01)
    154     {
    155       if (_d0 >= 32)
    156       {
    157         _xset_mask2[c] |= digitalPinToBitMask(_d0);
    158       }
    159       else
    160       {
    161         _xset_mask1[c] |= digitalPinToBitMask(_d0);
    162       }
    163     }
    164     if (c & 0x02)
    165     {
    166       if (_d1 >= 32)
    167       {
    168         _xset_mask2[c] |= digitalPinToBitMask(_d1);
    169       }
    170       else
    171       {
    172         _xset_mask1[c] |= digitalPinToBitMask(_d1);
    173       }
    174     }
    175     if (c & 0x04)
    176     {
    177       if (_d2 >= 32)
    178       {
    179         _xset_mask2[c] |= digitalPinToBitMask(_d2);
    180       }
    181       else
    182       {
    183         _xset_mask1[c] |= digitalPinToBitMask(_d2);
    184       }
    185     }
    186     if (c & 0x08)
    187     {
    188       if (_d3 >= 32)
    189       {
    190         _xset_mask2[c] |= digitalPinToBitMask(_d3);
    191       }
    192       else
    193       {
    194         _xset_mask1[c] |= digitalPinToBitMask(_d3);
    195       }
    196     }
    197     if (c & 0x10)
    198     {
    199       if (_d4 >= 32)
    200       {
    201         _xset_mask2[c] |= digitalPinToBitMask(_d4);
    202       }
    203       else
    204       {
    205         _xset_mask1[c] |= digitalPinToBitMask(_d4);
    206       }
    207     }
    208     if (c & 0x20)
    209     {
    210       if (_d5 >= 32)
    211       {
    212         _xset_mask2[c] |= digitalPinToBitMask(_d5);
    213       }
    214       else
    215       {
    216         _xset_mask1[c] |= digitalPinToBitMask(_d5);
    217       }
    218     }
    219     if (c & 0x40)
    220     {
    221       if (_d6 >= 32)
    222       {
    223         _xset_mask2[c] |= digitalPinToBitMask(_d6);
    224       }
    225       else
    226       {
    227         _xset_mask1[c] |= digitalPinToBitMask(_d6);
    228       }
    229     }
    230     if (c & 0x80)
    231     {
    232       if (_d7 >= 32)
    233       {
    234         _xset_mask2[c] |= digitalPinToBitMask(_d7);
    235       }
    236       else
    237       {
    238         _xset_mask1[c] |= digitalPinToBitMask(_d7);
    239       }
    240     }
    241   }
    242   GPIO.out_w1tc = _data1ClrMask;
    243   GPIO.out1_w1tc.val = _data2ClrMask;
    244 
    245   return true;
    246 }
    247 
    248 void Arduino_ESP32PAR8::beginWrite()
    249 {
    250   DC_HIGH();
    251   CS_LOW();
    252 }
    253 
    254 void Arduino_ESP32PAR8::endWrite()
    255 {
    256   CS_HIGH();
    257 }
    258 
    259 void Arduino_ESP32PAR8::writeCommand(uint8_t c)
    260 {
    261   DC_LOW();
    262 
    263   WRITE(c);
    264 
    265   DC_HIGH();
    266 }
    267 
    268 void Arduino_ESP32PAR8::writeCommand16(uint16_t c)
    269 {
    270   DC_LOW();
    271 
    272   _data16.value = c;
    273   WRITE(_data16.msb);
    274   WRITE(_data16.lsb);
    275 
    276   DC_HIGH();
    277 }
    278 
    279 void Arduino_ESP32PAR8::write(uint8_t d)
    280 {
    281   WRITE(d);
    282 }
    283 
    284 void Arduino_ESP32PAR8::write16(uint16_t d)
    285 {
    286   _data16.value = d;
    287   WRITE(_data16.msb);
    288   WRITE(_data16.lsb);
    289 }
    290 
    291 void Arduino_ESP32PAR8::writeRepeat(uint16_t p, uint32_t len)
    292 {
    293   _data16.value = p;
    294   if (_data16.msb == _data16.lsb)
    295   {
    296     uint32_t setMask1 = _xset_mask1[_data16.msb];
    297     uint32_t setMask2 = _xset_mask2[_data16.msb];
    298     GPIO.out_w1tc = _data1ClrMask;
    299     GPIO.out1_w1tc.val = _data2ClrMask;
    300     GPIO.out_w1ts = setMask1;
    301     GPIO.out1_w1ts.val = setMask2;
    302     while (len--)
    303     {
    304       *_wrPortClr = _wrPinMask;
    305       *_wrPortSet = _wrPinMask;
    306       *_wrPortClr = _wrPinMask;
    307       *_wrPortSet = _wrPinMask;
    308     }
    309   }
    310   else
    311   {
    312     uint32_t hiMask1 = _xset_mask1[_data16.msb];
    313     uint32_t hiMask2 = _xset_mask2[_data16.msb];
    314     uint32_t loMask1 = _xset_mask1[_data16.lsb];
    315     uint32_t loMask2 = _xset_mask2[_data16.lsb];
    316     while (len--)
    317     {
    318       GPIO.out_w1tc = _data1ClrMask;
    319       GPIO.out1_w1tc.val = _data2ClrMask;
    320       GPIO.out_w1ts = hiMask1;
    321       GPIO.out1_w1ts.val = hiMask2;
    322       *_wrPortClr = _wrPinMask;
    323       *_wrPortSet = _wrPinMask;
    324 
    325       GPIO.out_w1tc = _data1ClrMask;
    326       GPIO.out1_w1tc.val = _data2ClrMask;
    327       GPIO.out_w1ts = loMask1;
    328       GPIO.out1_w1ts.val = loMask2;
    329       *_wrPortClr = _wrPinMask;
    330       *_wrPortSet = _wrPinMask;
    331     }
    332   }
    333 }
    334 
    335 void Arduino_ESP32PAR8::writePixels(uint16_t *data, uint32_t len)
    336 {
    337   while (len--)
    338   {
    339     _data16.value = *data++;
    340     WRITE(_data16.msb);
    341     WRITE(_data16.lsb);
    342   }
    343 }
    344 
    345 void Arduino_ESP32PAR8::writeC8D8(uint8_t c, uint8_t d)
    346 {
    347   DC_LOW();
    348 
    349   WRITE(c);
    350 
    351   DC_HIGH();
    352 
    353   WRITE(d);
    354 }
    355 
    356 void Arduino_ESP32PAR8::writeC8D16(uint8_t c, uint16_t d)
    357 {
    358   DC_LOW();
    359 
    360   WRITE(c);
    361 
    362   DC_HIGH();
    363 
    364   _data16.value = d;
    365   WRITE(_data16.msb);
    366   WRITE(_data16.lsb);
    367 }
    368 
    369 void Arduino_ESP32PAR8::writeC8D16D16(uint8_t c, uint16_t d1, uint16_t d2)
    370 {
    371   DC_LOW();
    372 
    373   WRITE(c);
    374 
    375   DC_HIGH();
    376 
    377   _data16.value = d1;
    378   WRITE(_data16.msb);
    379   WRITE(_data16.lsb);
    380 
    381   _data16.value = d2;
    382   WRITE(_data16.msb);
    383   WRITE(_data16.lsb);
    384 }
    385 
    386 void Arduino_ESP32PAR8::writeC8D16D16Split(uint8_t c, uint16_t d1, uint16_t d2)
    387 {
    388   DC_LOW();
    389 
    390   WRITE(c);
    391 
    392   DC_HIGH();
    393 
    394   _data16.value = d1;
    395   WRITE(_data16.msb);
    396   WRITE(_data16.lsb);
    397 
    398   _data16.value = d2;
    399   WRITE(_data16.msb);
    400   WRITE(_data16.lsb);
    401 }
    402 
    403 void Arduino_ESP32PAR8::writeBytes(uint8_t *data, uint32_t len)
    404 {
    405   while (len--)
    406   {
    407     WRITE(*data++);
    408   }
    409 }
    410 
    411 void Arduino_ESP32PAR8::writePattern(uint8_t *data, uint8_t len, uint32_t repeat)
    412 {
    413   while (repeat--)
    414   {
    415     writeBytes(data, len);
    416   }
    417 }
    418 
    419 void Arduino_ESP32PAR8::writeIndexedPixels(uint8_t *data, uint16_t *idx, uint32_t len)
    420 {
    421   while (len--)
    422   {
    423     _data16.value = idx[*data++];
    424     WRITE(_data16.msb);
    425     WRITE(_data16.lsb);
    426   }
    427 }
    428 
    429 void Arduino_ESP32PAR8::writeIndexedPixelsDouble(uint8_t *data, uint16_t *idx, uint32_t len)
    430 {
    431   while (len--)
    432   {
    433     _data16.value = idx[*data++];
    434     WRITE(_data16.msb);
    435     WRITE(_data16.lsb);
    436     WRITE(_data16.msb);
    437     WRITE(_data16.lsb);
    438   }
    439 }
    440 
    441 INLINE void Arduino_ESP32PAR8::WRITE(uint8_t d)
    442 {
    443   uint32_t setMask1 = _xset_mask1[d];
    444   uint32_t setMask2 = _xset_mask2[d];
    445   GPIO.out_w1tc = _data1ClrMask;
    446   GPIO.out1_w1tc.val = _data2ClrMask;
    447   GPIO.out_w1ts = setMask1;
    448   GPIO.out1_w1ts.val = setMask2;
    449   *_wrPortClr = _wrPinMask;
    450   *_wrPortSet = _wrPinMask;
    451 }
    452 
    453 /******** low level bit twiddling **********/
    454 
    455 INLINE void Arduino_ESP32PAR8::DC_HIGH(void)
    456 {
    457   *_dcPortSet = _dcPinMask;
    458 }
    459 
    460 INLINE void Arduino_ESP32PAR8::DC_LOW(void)
    461 {
    462   *_dcPortClr = _dcPinMask;
    463 }
    464 
    465 INLINE void Arduino_ESP32PAR8::CS_HIGH(void)
    466 {
    467   if (_cs != GFX_NOT_DEFINED)
    468   {
    469     *_csPortSet = _csPinMask;
    470   }
    471 }
    472 
    473 INLINE void Arduino_ESP32PAR8::CS_LOW(void)
    474 {
    475   if (_cs != GFX_NOT_DEFINED)
    476   {
    477     *_csPortClr = _csPinMask;
    478   }
    479 }
    480 
    481 #endif // #if defined(ESP32) && (CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3)