olikraus / ucglib

Arduino True Color Library for TFTs and OLEDs
https://github.com/olikraus/ucglib/wiki
Other
261 stars 76 forks source link

ESP32 2nd HW SPI busses #148

Open meckiemac opened 3 years ago

meckiemac commented 3 years ago

Hi, tweaked a bit the C++/Header files to allow alternatives HW SPI busses, like the ESP32 has. It also enables HW CS.

Ucglib.h changes

`61c61 < #include "src/clib/ucg.h"

include "clib/ucg.h"

63d62 < static SPIClass extSPI; 235,259d233 < class Ucglib4Wire2ndHWSPI : public Ucglib < { < public: < Ucglib4Wire2ndHWSPI(ucg_dev_fnptr dev, ucg_dev_fnptr ext, SPIClass spi, uint8_t cd, uint8_t cs = UCG_PIN_VAL_NONE, uint8_t reset = UCG_PIN_VAL_NONE, uint8_t scl = UCG_PIN_VAL_NONE, uint8_t sda = UCG_PIN_VAL_NONE) < { init(); dev_cb = dev; ext_cb = ext; < < ucg.pin_list[UCG_PIN_RST] = reset; < ucg.pin_list[UCG_PIN_CD] = cd; < ucg.pin_list[UCG_PIN_CS] = cs; < ucg.pin_list[UCG_PIN_SCL] = scl; < ucg.pin_list[UCG_PIN_SDA] = sda; < extSPI = spi; < < #ifdef AVR < ucg.data_port[UCG_PIN_RST] = portOutputRegister(digitalPinToPort(reset)); < ucg.data_mask[UCG_PIN_RST] = digitalPinToBitMask(reset); < ucg.data_port[UCG_PIN_CD] = portOutputRegister(digitalPinToPort(cd)); < ucg.data_mask[UCG_PIN_CD] = digitalPinToBitMask(cd); < ucg.data_port[UCG_PIN_CS] = portOutputRegister(digitalPinToPort(cs)); < ucg.data_mask[UCG_PIN_CS] = digitalPinToBitMask(cs); < #endif < } < void begin(uint8_t is_transparent); < }; < 415,423d388 < class Ucglib_SSD1351_18x128x128_2nd_HWSPI : public Ucglib4Wire2ndHWSPI < { < public: < / new default constructor, if standard we just have an optional reset / < Ucglib_SSD1351_18x128x128_2nd_HWSPI( SPIClass spi, uint8_t cd, uint8_t reset = UCG_PIN_VAL_NONE, uint8_t cs = UCG_PIN_VAL_NONE, uint8_t scl = UCG_PIN_VAL_NONE, uint8_t sda = UCG_PIN_VAL_NONE) : < Ucglib4Wire2ndHWSPI(ucg_dev_ssd1351_18x128x128_ilsoft, ucg_ext_ssd1351_18, spi, /cd=/ cd , /cs=/ cs, /reset=/ reset, /scl=/ scl, /sda=/ sda) < { } < }; <`

Ucglib.cpp changes `1303,1443d1302 < /=========================================================================/ < / External/2nd SPI Controller e.g. ESP32 / < < static int16_t ucg_com_arduino_4wire_2nd_HW_SPI(ucg_t ucg, int16_t msg, uint16_t arg, uint8_t data) < { < switch(msg) < { < case UCG_COM_MSG_POWER_UP: < / "data" is a pointer to ucg_com_info_t structure with the following information: / < / ((ucg_com_info_t )data)->serial_clk_speed value in nanoseconds / < / ((ucg_com_info_t )data)->parallel_clk_speed value in nanoseconds / < < / setup pins / < < if ( ucg->pin_list[UCG_PIN_RST] != UCG_PIN_VAL_NONE ) < pinMode(ucg->pin_list[UCG_PIN_RST], OUTPUT); < pinMode(ucg->pin_list[UCG_PIN_CD], OUTPUT); < < < / on ESP32 we have HW CS / < #if !defined(ARDUINO_ARCH_ESP32) < if ( ucg->pin_list[UCG_PIN_CS] != UCG_PIN_VAL_NONE ) < pinMode(ucg->pin_list[UCG_PIN_CS], OUTPUT); < #endif // defined(ARDUINO_ARCH_ESP32) < < / setup Arduino SPI / < < #if ARDUINO >= 10600 < #if defined(ARDUINO_ARCH_ESP32) < //Serial.print("ESP32 SPI Begin "); < extSPI.begin( < (ucg->pin_list[UCG_PIN_SCL] == UCG_PIN_VAL_NONE ? -1 : ucg->pin_list[UCG_PIN_SCL]), < -1, < (ucg->pin_list[UCG_PIN_SDA] == UCG_PIN_VAL_NONE ? -1 : ucg->pin_list[UCG_PIN_SDA]), < (ucg->pin_list[UCG_PIN_CS] == UCG_PIN_VAL_NONE ? -1 : ucg->pin_list[UCG_PIN_CS]) < ); < extSPI.setHwCs(true); / maybe a feature in fuure / < #else // defined(ARDUINO_ARCH_ESP32) < extSPI.begin(); < #endif // defined(ARDUINO_ARCH_ESP32) < extSPI.beginTransaction(SPISettings(1000000000UL/((ucg_com_info_t )data)->serial_clk_speed, MSBFIRST, SPI_MODE0)); < #else // ARDUINO >= 10600 < extSPI.begin(); < < if ( ((ucg_com_info_t )data)->serial_clk_speed/2 < 70 ) < extSPI.setClockDivider( SPI_CLOCK_DIV2 ); < else if ( ((ucg_com_info_t )data)->serial_clk_speed/2 < 140 ) < extSPI.setClockDivider( SPI_CLOCK_DIV4 ); < else < extSPI.setClockDivider( SPI_CLOCK_DIV8 ); < extSPI.setDataMode(SPI_MODE0); < extSPI.setBitOrder(MSBFIRST); < #endif // ARDUINO >= 10600 < < < break; < case UCG_COM_MSG_POWER_DOWN: < #if ARDUINO >= 10600 < extSPI.endTransaction(); < extSPI.end(); < #else < extSPI.end(); < #endif < break; < case UCG_COM_MSG_DELAY: < delayMicroseconds(arg); < break; < case UCG_COM_MSG_CHANGE_RESET_LINE: < if ( ucg->pin_list[UCG_PIN_RST] != UCG_PIN_VAL_NONE ) < digitalWrite(ucg->pin_list[UCG_PIN_RST], arg); < break; < case UCG_COM_MSG_CHANGE_CS_LINE: < / on ESP32 we have HW CS / < #if !defined(ARDUINO_ARCH_ESP32) < if ( ucg->pin_list[UCG_PIN_CS] != UCG_PIN_VAL_NONE ) < digitalWrite(ucg->pin_list[UCG_PIN_CS], arg); < #endif // !defined(ARDUINO_ARCH_ESP32) < break; < case UCG_COM_MSG_CHANGE_CD_LINE: < digitalWrite(ucg->pin_list[UCG_PIN_CD], arg); < break; < case UCG_COM_MSG_SEND_BYTE: < extSPI.transfer(arg); < break; < case UCG_COM_MSG_REPEAT_1_BYTE: < while( arg > 0 ) { < extSPI.transfer(data[0]); < arg--; < } < break; < case UCG_COM_MSG_REPEAT_2_BYTES: < while( arg > 0 ) { < extSPI.transfer(data[0]); < extSPI.transfer(data[1]); < arg--; < } < break; < case UCG_COM_MSG_REPEAT_3_BYTES: < while( arg > 0 ) { < extSPI.transfer(data[0]); < extSPI.transfer(data[1]); < extSPI.transfer(data[2]); < arg--; < } < break; < case UCG_COM_MSG_SEND_STR: < while( arg > 0 ) { < extSPI.transfer(data++); < arg--; < } < break; < case UCG_COM_MSG_SEND_CD_DATA_SEQUENCE: < while(arg > 0) < { < if ( data != 0 ) < { < / set the data line directly, ignore the setting from UCG_CFG_CD / < if ( data == 1 ) < { < digitalWrite(ucg->pin_list[UCG_PIN_CD], 0); < } < else < { < digitalWrite(ucg->pin_list[UCG_PIN_CD], 1); < } < } < data++; < extSPI.transfer(*data); < data++; < arg--; < } < break; < } < return 1; < } < < void Ucglib4Wire2ndHWSPI::begin(uint8_t is_transparent) < { < ucg_Init(&ucg, dev_cb, ext_cb, ucg_com_arduino_4wire_2nd_HW_SPI); < ucg_SetFontMode(&ucg, is_transparent); < } 1473a1333

`

The C++ code can be probably combined with the original function ucg_com_arduino_4wire_HW_SPI .