Skip to content

Commit 8dce522

Browse files
Merge pull request #663 from mathertel/master
ESP32LCD8 for Arduino ESP32 3.x
2 parents 6ea1f22 + ab97167 commit 8dce522

File tree

2 files changed

+411
-5
lines changed

2 files changed

+411
-5
lines changed

src/databus/Arduino_ESP32LCD8.cpp

Lines changed: 320 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
/*
2-
* start rewrite from:
3-
* https://github.com/lovyan03/LovyanGFX/blob/master/src/lgfx/v0/platforms/LGFX_PARALLEL_ESP32.hpp
4-
*/
51
#include "Arduino_ESP32LCD8.h"
62

3+
#include "esp_lcd_panel_interface.h"
4+
#include "esp_lcd_panel_io.h"
5+
#include <esp_private/gdma.h>
6+
#include <hal/dma_types.h>
7+
8+
#ifndef LCD_MAX_PIXELS_AT_ONCE
9+
#define LCD_MAX_PIXELS_AT_ONCE (2048)
10+
#endif
11+
712
#if defined(ESP32) && (CONFIG_IDF_TARGET_ESP32S3)
813
#if (!defined(ESP_ARDUINO_VERSION_MAJOR)) || (ESP_ARDUINO_VERSION_MAJOR < 3)
914

@@ -744,5 +749,316 @@ GFX_INLINE void Arduino_ESP32LCD8::CS_LOW(void)
744749
}
745750
}
746751

752+
#else // (ESP_ARDUINO_VERSION_MAJOR >= 3)
753+
754+
/**
755+
* @brief Construct a new Arduino_ESP32LCD8::Arduino_ESP32LCD8 object
756+
*/
757+
Arduino_ESP32LCD8::Arduino_ESP32LCD8(
758+
int8_t dc, int8_t cs, int8_t wr, int8_t rd,
759+
int8_t d0, int8_t d1, int8_t d2, int8_t d3, int8_t d4, int8_t d5, int8_t d6, int8_t d7)
760+
: _dc(dc), _cs(cs), _wr(wr), _rd(rd),
761+
_d0(d0), _d1(d1), _d2(d2), _d3(d3), _d4(d4), _d5(d5), _d6(d6), _d7(d7)
762+
{
763+
}
764+
765+
/**
766+
* @brief begin
767+
*/
768+
bool Arduino_ESP32LCD8::begin(int32_t speed, int8_t dataMode)
769+
{
770+
if (speed == GFX_NOT_DEFINED)
771+
{
772+
_speed = 40000000UL;
773+
}
774+
else
775+
{
776+
_speed = speed;
777+
}
778+
779+
if (_cs != GFX_NOT_DEFINED)
780+
{
781+
pinMode(_cs, OUTPUT);
782+
digitalWrite(_cs, HIGH); // disable chip select
783+
}
784+
785+
pinMode(_wr, OUTPUT);
786+
digitalWrite(_wr, HIGH); // Set write strobe high (inactive)
787+
788+
if (_rd != GFX_NOT_DEFINED)
789+
{
790+
pinMode(_rd, OUTPUT);
791+
digitalWrite(_rd, HIGH);
792+
}
793+
794+
esp_lcd_i80_bus_config_t bus_config = {
795+
.dc_gpio_num = _dc,
796+
.wr_gpio_num = _wr,
797+
.clk_src = LCD_CLK_SRC_PLL160M, // ??? LCD_CLK_SRC_DEFAULT,
798+
.data_gpio_nums = {_d0, _d1, _d2, _d3, _d4, _d5, _d6, _d7},
799+
.bus_width = 8,
800+
.max_transfer_bytes = LCD_MAX_PIXELS_AT_ONCE * 2,
801+
.dma_burst_size = 64,
802+
// .sram_trans_align = 4
803+
};
804+
esp_lcd_new_i80_bus(&bus_config, &_i80_bus);
805+
806+
// Serial.printf("max_transfer_bytes=%zu\n", bus_config.max_transfer_bytes);
807+
808+
esp_lcd_panel_io_i80_config_t io_config = {
809+
.cs_gpio_num = _cs,
810+
.pclk_hz = _speed,
811+
.trans_queue_depth = 10,
812+
// on_color_trans_done = nullptr,
813+
// user_ctx = nullptr,
814+
.lcd_cmd_bits = 8,
815+
.lcd_param_bits = 8,
816+
.dc_levels = {
817+
.dc_idle_level = 0,
818+
.dc_cmd_level = 0,
819+
.dc_dummy_level = 0,
820+
.dc_data_level = 1,
821+
},
822+
.flags = {
823+
.swap_color_bytes = 0,
824+
.pclk_idle_low = 0,
825+
},
826+
};
827+
esp_lcd_new_panel_io_i80(_i80_bus, &io_config, &_io_handle);
828+
829+
// allocate some DMA buffer
830+
_cmd = -1;
831+
_isColor = false;
832+
_bufferLen = 0;
833+
_buffer = (uint8_t *)esp_lcd_i80_alloc_draw_buffer(_io_handle, LCD_MAX_PIXELS_AT_ONCE * 2 + 16, MALLOC_CAP_DMA);
834+
835+
return (_buffer != nullptr);
836+
}
837+
838+
/**
839+
* @brief start any command or data sequence
840+
*/
841+
void Arduino_ESP32LCD8::beginWrite()
842+
{
843+
_cmd == -1;
844+
_bufferLen = 0;
845+
}
846+
847+
/**
848+
* @brief end any command or data sequence
849+
*/
850+
void Arduino_ESP32LCD8::endWrite()
851+
{
852+
flushBuffer();
853+
}
854+
855+
/**
856+
* @brief write a byte command
857+
* @param c the command byte
858+
*/
859+
void Arduino_ESP32LCD8::writeCommand(uint8_t c)
860+
{
861+
flushBuffer();
862+
// Serial.printf("writeCommand(%02x)\n", c);
863+
_cmd = c;
864+
_isColor = false;
865+
}
866+
867+
/**
868+
* @brief write a word command
869+
* @param c the command word
870+
*/
871+
void Arduino_ESP32LCD8::writeCommand16(uint16_t c)
872+
{
873+
// Serial.printf("ESP32LCD8::writeCommand16 not implemented.\n");
874+
}
875+
876+
/** write a set of data as command + params */
877+
void Arduino_ESP32LCD8::writeCommandBytes(uint8_t *data, uint32_t len)
878+
{
879+
flushBuffer();
880+
// Serial.printf("**writeCommandBytes()\n");
881+
882+
if (len)
883+
{
884+
_cmd = *data++;
885+
_isColor = false;
886+
len--;
887+
}
888+
889+
while (len)
890+
{
891+
write(*data++);
892+
len--;
893+
}
894+
}
895+
896+
/**
897+
* @brief write a byte to the param buffer.
898+
* @param d data byte
899+
*/
900+
void Arduino_ESP32LCD8::write(uint8_t d)
901+
{
902+
_buffer[_bufferLen++] = d;
903+
if (_bufferLen >= LCD_MAX_PIXELS_AT_ONCE * 2)
904+
{
905+
flushBuffer();
906+
}
907+
}
908+
909+
/**
910+
* @brief write16 - write 16-bit to the param buffer.
911+
* @param d data word
912+
*/
913+
void Arduino_ESP32LCD8::write16(uint16_t d)
914+
{
915+
_data16.value = d;
916+
_buffer[_bufferLen++] = _data16.msb;
917+
_buffer[_bufferLen++] = _data16.lsb;
918+
919+
if (_bufferLen >= LCD_MAX_PIXELS_AT_ONCE * 2 - 4)
920+
{
921+
flushBuffer();
922+
}
923+
}
924+
925+
/**
926+
* @brief writeRepeat
927+
*
928+
* @param p
929+
* @param len
930+
*/
931+
void Arduino_ESP32LCD8::writeRepeat(uint16_t p, uint32_t len)
932+
{
933+
// Serial.printf(" writeRepeat(#%04x, %d)\n", p, len);
934+
_isColor = true;
935+
936+
while (len--)
937+
{
938+
write16(p);
939+
}
940+
941+
// // fill buffer
942+
// uint32_t buf16Len = (len > LCD_MAX_PIXELS_AT_ONCE) ? LCD_MAX_PIXELS_AT_ONCE : len;
943+
// _buffer[0] = p >> 8;
944+
// _buffer[1] = p & 0x00ff;
945+
// uint16_t c16 = _buffer16[0];
946+
// for (uint32_t i = 0; i < buf16Len; i++)
947+
// {
948+
// _buffer16[i] = c16;
949+
// }
950+
951+
// while (len > 0)
952+
// { // while there are still pixels to write
953+
// uint32_t txlen16 = (len > LCD_MAX_PIXELS_AT_ONCE) ? LCD_MAX_PIXELS_AT_ONCE : len;
954+
// _bufferLen = txlen16 * 2;
955+
// flushBuffer();
956+
// len -= txlen16;
957+
// }
958+
}
959+
960+
/**
961+
* @brief write Pixel data words to the buffer.
962+
* @param data array of Pixel data words
963+
* @param len length of data array
964+
*/
965+
void Arduino_ESP32LCD8::writePixels(uint16_t *data, uint32_t len)
966+
{
967+
// Serial.printf(" writePixels( [...], %ld)\n", len);
968+
_isColor = true;
969+
970+
// transfer in chunks
971+
while (len--)
972+
{
973+
write16(*data++);
974+
}
975+
}
976+
977+
/**
978+
* @brief write data bytes to the buffer
979+
* @param data array of data bytes
980+
* @param len length of data array
981+
*/
982+
void Arduino_ESP32LCD8::writeBytes(uint8_t *data, uint32_t len)
983+
{
984+
// Serial.printf(" writeBytes( [...], %ld)\n", len);
985+
// transfer in chunks
986+
while (len--)
987+
{
988+
write(*data++);
989+
}
990+
}
991+
992+
/**
993+
* @brief writeIndexedPixels
994+
*
995+
* @param data
996+
* @param idx
997+
* @param len
998+
*/
999+
void Arduino_ESP32LCD8::writeIndexedPixels(uint8_t *data, uint16_t *idx, uint32_t len)
1000+
{
1001+
// Serial.printf("**writeIndexedPixels(...)\n");
1002+
}
1003+
1004+
/**
1005+
* @brief writeIndexedPixelsDouble
1006+
*
1007+
* @param data
1008+
* @param idx
1009+
* @param len
1010+
*/
1011+
void Arduino_ESP32LCD8::writeIndexedPixelsDouble(uint8_t *data, uint16_t *idx, uint32_t len)
1012+
{
1013+
// Serial.printf("**writeIndexedPixelsDouble(...)\n");
1014+
}
1015+
1016+
// ===== sending the buffer =====
1017+
1018+
// for details, see
1019+
// <https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/peripherals/lcd/index.html>
1020+
1021+
void Arduino_ESP32LCD8::flushBuffer()
1022+
{
1023+
1024+
if (_cmd == 0x2c)
1025+
{
1026+
if (!_isColor)
1027+
_isColor = true;
1028+
}
1029+
1030+
if ((_cmd >= 0) || (_bufferLen > 0))
1031+
{
1032+
// Serial.printf(" flush(%02x: (%d)", _cmd, _bufferLen);
1033+
1034+
// for (int n = 0; (n < _bufferLen) && (n < 32); n++)
1035+
// {
1036+
// Serial.printf(" %02x", _buffer[n]);
1037+
// }
1038+
1039+
// wait for color completion (when color sending is on the way)
1040+
// send cmd and buffer and wait for completion
1041+
1042+
if (_isColor)
1043+
// if (_bufferLen > 32)
1044+
// if (_cmd == 0x2c9)
1045+
{
1046+
// async DMA transfer
1047+
esp_lcd_panel_io_tx_color(_io_handle, _cmd, _buffer, _bufferLen);
1048+
}
1049+
else
1050+
{
1051+
esp_lcd_panel_io_tx_param(_io_handle, _cmd, _buffer, _bufferLen);
1052+
}
1053+
// if (!_bufferLen)
1054+
// {
1055+
// _cmd = -1; // next time, we start a data send out without command.
1056+
// }
1057+
_cmd = -1; // next time, we start a data send out without command.
1058+
_bufferLen = 0;
1059+
// Serial.println(")");
1060+
}
1061+
}
1062+
7471063
#endif // #if (!defined(ESP_ARDUINO_VERSION_MAJOR)) || (ESP_ARDUINO_VERSION_MAJOR < 3)
7481064
#endif // #if defined(ESP32) && (CONFIG_IDF_TARGET_ESP32S3)

0 commit comments

Comments
 (0)