Skip to content

Commit

Permalink
Separate I2C and SPI bus settings in config struct
Browse files Browse the repository at this point in the history
  • Loading branch information
mkfrey committed Feb 19, 2023
1 parent 9db5709 commit 5553dd3
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 44 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
```

Expand Down
6 changes: 3 additions & 3 deletions examples/test_SSD1306.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions examples/test_SSD1306_i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -55,3 +55,4 @@ void task_test_SSD1306i2c(void* ignore) {

vTaskDelete(NULL);
}
u
49 changes: 38 additions & 11 deletions include/u8g2_esp32_hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
46 changes: 23 additions & 23 deletions src/u8g2_esp32_hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand Down

0 comments on commit 5553dd3

Please sign in to comment.