From 5553dd3c2d9c5b89daf7a49bf45a5a0bd7baad9e Mon Sep 17 00:00:00 2001 From: Markus Frey Date: Sun, 19 Feb 2023 14:26:02 +0100 Subject: [PATCH] Separate I2C and SPI bus settings in config struct --- README.md | 10 ++++---- examples/test_SSD1306.c | 6 ++--- examples/test_SSD1306_i2c.c | 5 ++-- include/u8g2_esp32_hal.h | 49 ++++++++++++++++++++++++++++--------- src/u8g2_esp32_hal.c | 46 +++++++++++++++++----------------- 5 files changed, 72 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index d718760..2bee9df 100644 --- a/README.md +++ b/README.md @@ -29,9 +29,9 @@ this call does is tell the ESP32 what pins we wish to map. Here is an example o ``` u8g2_esp32_hal_t u8g2_esp32_hal = U8G2_ESP32_HAL_DEFAULT; -u8g2_esp32_hal.clk = PIN_CLK; -u8g2_esp32_hal.mosi = PIN_MOSI; -u8g2_esp32_hal.cs = PIN_CS; +u8g2_esp32_hal.bus.spi.clk = PIN_CLK; +u8g2_esp32_hal.bus.spi.mosi = PIN_MOSI; +u8g2_esp32_hal.bus.spi.cs = PIN_CS; u8g2_esp32_hal.dc = PIN_DC; u8g2_esp32_hal.reset = PIN_RESET; u8g2_esp32_hal_init(u8g2_esp32_hal); @@ -41,8 +41,8 @@ and here is an example of I2C use: ``` u8g2_esp32_hal_t u8g2_esp32_hal = U8G2_ESP32_HAL_DEFAULT; -u8g2_esp32_hal.sda = PIN_SDA; -u8g2_esp32_hal.scl = PIN_SCL; +u8g2_esp32_hal.bus.i2c.sda = PIN_SDA; +u8g2_esp32_hal.bus.i2c.scl = PIN_SCL; u8g2_esp32_hal_init(u8g2_esp32_hal); ``` diff --git a/examples/test_SSD1306.c b/examples/test_SSD1306.c index 65510a2..2a46beb 100644 --- a/examples/test_SSD1306.c +++ b/examples/test_SSD1306.c @@ -28,9 +28,9 @@ static char tag[] = "test_SSD1306"; void task_test_SSD1306(void* ignore) { u8g2_esp32_hal_t u8g2_esp32_hal = U8G2_ESP32_HAL_DEFAULT; - u8g2_esp32_hal.clk = PIN_CLK; - u8g2_esp32_hal.mosi = PIN_MOSI; - u8g2_esp32_hal.cs = PIN_CS; + u8g2_esp32_hal.bus.spi.clk = PIN_CLK; + u8g2_esp32_hal.bus.spi.mosi = PIN_MOSI; + u8g2_esp32_hal.bus.spi.cs = PIN_CS; u8g2_esp32_hal.dc = PIN_DC; u8g2_esp32_hal.reset = PIN_RESET; u8g2_esp32_hal_init(u8g2_esp32_hal); diff --git a/examples/test_SSD1306_i2c.c b/examples/test_SSD1306_i2c.c index a3e089b..c6cbf16 100644 --- a/examples/test_SSD1306_i2c.c +++ b/examples/test_SSD1306_i2c.c @@ -20,8 +20,8 @@ static const char* TAG = "ssd1306"; void task_test_SSD1306i2c(void* ignore) { u8g2_esp32_hal_t u8g2_esp32_hal = U8G2_ESP32_HAL_DEFAULT; - u8g2_esp32_hal.sda = PIN_SDA; - u8g2_esp32_hal.scl = PIN_SCL; + u8g2_esp32_hal.bus.i2c.sda = PIN_SDA; + u8g2_esp32_hal.bus.i2c.scl = PIN_SCL; u8g2_esp32_hal_init(u8g2_esp32_hal); u8g2_t u8g2; // a structure which will contain all the data for one display @@ -55,3 +55,4 @@ void task_test_SSD1306i2c(void* ignore) { vTaskDelete(NULL); } +u \ No newline at end of file diff --git a/include/u8g2_esp32_hal.h b/include/u8g2_esp32_hal.h index f1c2c01..e6d0c1a 100644 --- a/include/u8g2_esp32_hal.h +++ b/include/u8g2_esp32_hal.h @@ -24,24 +24,51 @@ #define ACK_CHECK_EN 0x1 // I2C master will check ack from slave #define ACK_CHECK_DIS 0x0 // I2C master will not check ack from slave +/** @public + * HAL configuration structure. + */ typedef struct { - gpio_num_t clk; - gpio_num_t mosi; - gpio_num_t sda; // data for I²C - gpio_num_t scl; // clock for I²C - gpio_num_t cs; + union { + /* SPI settings. */ + struct { + /* GPIO num for clock. */ + gpio_num_t clk; + /* GPIO num for SPI mosi. */ + gpio_num_t mosi; + /* GPIO num for SPI slave/chip select. */ + gpio_num_t cs; + } spi; + /* I2C settings. */ + struct { + /* GPIO num for I2C data. */ + gpio_num_t sda; + /* GPIO num for I2C clock. */ + gpio_num_t scl; + } i2c; + } bus; + /* GPIO num for reset. */ gpio_num_t reset; + /* GPIO num for DC. */ gpio_num_t dc; } u8g2_esp32_hal_t; -#define U8G2_ESP32_HAL_DEFAULT \ - { \ - U8G2_ESP32_HAL_UNDEFINED, U8G2_ESP32_HAL_UNDEFINED, \ - U8G2_ESP32_HAL_UNDEFINED, U8G2_ESP32_HAL_UNDEFINED, \ - U8G2_ESP32_HAL_UNDEFINED, U8G2_ESP32_HAL_UNDEFINED, \ - U8G2_ESP32_HAL_UNDEFINED \ +/** + * Construct a default HAL configuration with all fields undefined. + */ +#define U8G2_ESP32_HAL_DEFAULT \ + { \ + .bus = {.spi = {.clk = U8G2_ESP32_HAL_UNDEFINED, \ + .mosi = U8G2_ESP32_HAL_UNDEFINED, \ + .cs = U8G2_ESP32_HAL_UNDEFINED}}, \ + .reset = U8G2_ESP32_HAL_UNDEFINED, .dc = U8G2_ESP32_HAL_UNDEFINED \ } +/** + * Initialize the HAL with the given configuration. + * + * @see u8g2_esp32_hal_t + * @see U8G2_ESP32_HAL_DEFAULT + */ void u8g2_esp32_hal_init(u8g2_esp32_hal_t u8g2_esp32_hal_param); uint8_t u8g2_esp32_spi_byte_cb(u8x8_t* u8x8, uint8_t msg, diff --git a/src/u8g2_esp32_hal.c b/src/u8g2_esp32_hal.c index 1a8d01f..cf09d21 100644 --- a/src/u8g2_esp32_hal.c +++ b/src/u8g2_esp32_hal.c @@ -51,19 +51,19 @@ uint8_t u8g2_esp32_spi_byte_cb(u8x8_t* u8x8, break; case U8X8_MSG_BYTE_INIT: { - if (u8g2_esp32_hal.clk == U8G2_ESP32_HAL_UNDEFINED || - u8g2_esp32_hal.mosi == U8G2_ESP32_HAL_UNDEFINED || - u8g2_esp32_hal.cs == U8G2_ESP32_HAL_UNDEFINED) { + if (u8g2_esp32_hal.bus.spi.clk == U8G2_ESP32_HAL_UNDEFINED || + u8g2_esp32_hal.bus.spi.mosi == U8G2_ESP32_HAL_UNDEFINED || + u8g2_esp32_hal.bus.spi.cs == U8G2_ESP32_HAL_UNDEFINED) { break; } spi_bus_config_t bus_config; memset(&bus_config, 0, sizeof(spi_bus_config_t)); - bus_config.sclk_io_num = u8g2_esp32_hal.clk; // CLK - bus_config.mosi_io_num = u8g2_esp32_hal.mosi; // MOSI - bus_config.miso_io_num = GPIO_NUM_NC; // MISO - bus_config.quadwp_io_num = GPIO_NUM_NC; // Not used - bus_config.quadhd_io_num = GPIO_NUM_NC; // Not used + bus_config.sclk_io_num = u8g2_esp32_hal.bus.spi.clk; // CLK + bus_config.mosi_io_num = u8g2_esp32_hal.bus.spi.mosi; // MOSI + bus_config.miso_io_num = GPIO_NUM_NC; // MISO + bus_config.quadwp_io_num = GPIO_NUM_NC; // Not used + bus_config.quadhd_io_num = GPIO_NUM_NC; // Not used // ESP_LOGI(TAG, "... Initializing bus."); ESP_ERROR_CHECK(spi_bus_initialize(HSPI_HOST, &bus_config, 1)); @@ -76,7 +76,7 @@ uint8_t u8g2_esp32_spi_byte_cb(u8x8_t* u8x8, dev_config.cs_ena_posttrans = 0; dev_config.cs_ena_pretrans = 0; dev_config.clock_speed_hz = 10000; - dev_config.spics_io_num = u8g2_esp32_hal.cs; + dev_config.spics_io_num = u8g2_esp32_hal.bus.spi.cs; dev_config.flags = 0; dev_config.queue_size = 200; dev_config.pre_cb = NULL; @@ -125,18 +125,18 @@ uint8_t u8g2_esp32_i2c_byte_cb(u8x8_t* u8x8, } case U8X8_MSG_BYTE_INIT: { - if (u8g2_esp32_hal.sda == U8G2_ESP32_HAL_UNDEFINED || - u8g2_esp32_hal.scl == U8G2_ESP32_HAL_UNDEFINED) { + if (u8g2_esp32_hal.bus.i2c.sda == U8G2_ESP32_HAL_UNDEFINED || + u8g2_esp32_hal.bus.i2c.scl == U8G2_ESP32_HAL_UNDEFINED) { break; } i2c_config_t conf = {0}; conf.mode = I2C_MODE_MASTER; - ESP_LOGI(TAG, "sda_io_num %d", u8g2_esp32_hal.sda); - conf.sda_io_num = u8g2_esp32_hal.sda; + ESP_LOGI(TAG, "sda_io_num %d", u8g2_esp32_hal.bus.i2c.sda); + conf.sda_io_num = u8g2_esp32_hal.bus.i2c.sda; conf.sda_pullup_en = GPIO_PULLUP_ENABLE; - ESP_LOGI(TAG, "scl_io_num %d", u8g2_esp32_hal.scl); - conf.scl_io_num = u8g2_esp32_hal.scl; + ESP_LOGI(TAG, "scl_io_num %d", u8g2_esp32_hal.bus.i2c.scl); + conf.scl_io_num = u8g2_esp32_hal.bus.i2c.scl; conf.scl_pullup_en = GPIO_PULLUP_ENABLE; ESP_LOGI(TAG, "clk_speed %d", I2C_MASTER_FREQ_HZ); conf.master.clk_speed = I2C_MASTER_FREQ_HZ; @@ -207,8 +207,8 @@ uint8_t u8g2_esp32_gpio_and_delay_cb(u8x8_t* u8x8, if (u8g2_esp32_hal.reset != U8G2_ESP32_HAL_UNDEFINED) { bitmask = bitmask | (1ull << u8g2_esp32_hal.reset); } - if (u8g2_esp32_hal.cs != U8G2_ESP32_HAL_UNDEFINED) { - bitmask = bitmask | (1ull << u8g2_esp32_hal.cs); + if (u8g2_esp32_hal.bus.spi.cs != U8G2_ESP32_HAL_UNDEFINED) { + bitmask = bitmask | (1ull << u8g2_esp32_hal.bus.spi.cs); } if (bitmask == 0) { @@ -232,21 +232,21 @@ uint8_t u8g2_esp32_gpio_and_delay_cb(u8x8_t* u8x8, break; // Set the GPIO client select pin to the value passed in through arg_int. case U8X8_MSG_GPIO_CS: - if (u8g2_esp32_hal.cs != U8G2_ESP32_HAL_UNDEFINED) { - gpio_set_level(u8g2_esp32_hal.cs, arg_int); + if (u8g2_esp32_hal.bus.spi.cs != U8G2_ESP32_HAL_UNDEFINED) { + gpio_set_level(u8g2_esp32_hal.bus.spi.cs, arg_int); } break; // Set the Software I²C pin to the value passed in through arg_int. case U8X8_MSG_GPIO_I2C_CLOCK: - if (u8g2_esp32_hal.scl != U8G2_ESP32_HAL_UNDEFINED) { - gpio_set_level(u8g2_esp32_hal.scl, arg_int); + if (u8g2_esp32_hal.bus.i2c.scl != U8G2_ESP32_HAL_UNDEFINED) { + gpio_set_level(u8g2_esp32_hal.bus.i2c.scl, arg_int); // printf("%c",(arg_int==1?'C':'c')); } break; // Set the Software I²C pin to the value passed in through arg_int. case U8X8_MSG_GPIO_I2C_DATA: - if (u8g2_esp32_hal.sda != U8G2_ESP32_HAL_UNDEFINED) { - gpio_set_level(u8g2_esp32_hal.sda, arg_int); + if (u8g2_esp32_hal.bus.i2c.sda != U8G2_ESP32_HAL_UNDEFINED) { + gpio_set_level(u8g2_esp32_hal.bus.i2c.sda, arg_int); // printf("%c",(arg_int==1?'D':'d')); } break;