diff --git a/main/u8g2_esp32_hal.c b/main/u8g2_esp32_hal.c index 3147d05..a580dcd 100644 --- a/main/u8g2_esp32_hal.c +++ b/main/u8g2_esp32_hal.c @@ -110,14 +110,17 @@ uint8_t u8g2_esp32_spi_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void return 0; } // u8g2_esp32_spi_byte_cb + /* * HAL callback function as prescribed by the U8G2 library. This callback is invoked * to handle I2C communications. */ uint8_t u8g2_esp32_i2c_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) { + #define TXBUF_SIZE 32 + static uint8_t txbuf[TXBUF_SIZE]; + static uint8_t *txbuf_ptr; // ESP_LOGV(TAG, "i2c_cb: Received a msg: %d, arg_int: %d, arg_ptr: %p", msg, arg_int, arg_ptr); - switch (msg) { case U8X8_MSG_BYTE_SET_DC: @@ -157,14 +160,16 @@ uint8_t u8g2_esp32_i2c_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void case U8X8_MSG_BYTE_SEND: { uint8_t *data_ptr = (uint8_t *)arg_ptr; - ESP_LOG_BUFFER_HEXDUMP(TAG, data_ptr, arg_int, ESP_LOG_VERBOSE); + size_t data_len = (size_t) arg_int; + // ESP_LOGV(TAG, "U8x8_MSG_BYTE_SEND. txbuf len: %d", txbuf_ptr - txbuf); + // ESP_LOG_BUFFER_HEXDUMP(TAG, data_ptr, data_len, ESP_LOG_VERBOSE); - while (arg_int > 0) - { - ESP_ERROR_CHECK(i2c_master_write_byte(handle_i2c, *data_ptr, ACK_CHECK_EN)); - data_ptr++; - arg_int--; + if (txbuf_ptr + data_len >= txbuf_ptr + TXBUF_SIZE) { + ESP_LOGE(TAG, "txbuf overflow"); + return 0; } + memcpy(txbuf_ptr, data_ptr, data_len); + txbuf_ptr += data_len; break; } @@ -175,12 +180,15 @@ uint8_t u8g2_esp32_i2c_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void // ESP_LOGV(TAG, "Start I2C transfer to %02X.", i2c_address >> 1); ESP_ERROR_CHECK(i2c_master_start(handle_i2c)); ESP_ERROR_CHECK(i2c_master_write_byte(handle_i2c, i2c_address | I2C_MASTER_WRITE, ACK_CHECK_EN)); + txbuf_ptr = txbuf; break; } case U8X8_MSG_BYTE_END_TRANSFER: { - // ESP_LOGV(TAG, "End I2C transfer."); + // ESP_LOGV(TAG, "End I2C transfer. txbuf len: %d", txbuf_ptr - txbuf); + // ESP_LOG_BUFFER_HEXDUMP(TAG, txbuf, txbuf_ptr - txbuf, ESP_LOG_VERBOSE); + ESP_ERROR_CHECK(i2c_master_write(handle_i2c, txbuf, txbuf_ptr - txbuf, ACK_CHECK_EN)); ESP_ERROR_CHECK(i2c_master_stop(handle_i2c)); ESP_ERROR_CHECK(i2c_master_cmd_begin(I2C_MASTER_NUM, handle_i2c, I2C_TIMEOUT_MS / portTICK_RATE_MS)); i2c_cmd_link_delete(handle_i2c); diff --git a/main/u8g2_esp32_hal.h b/main/u8g2_esp32_hal.h index 3763f20..e22a06d 100644 --- a/main/u8g2_esp32_hal.h +++ b/main/u8g2_esp32_hal.h @@ -18,7 +18,7 @@ #define I2C_MASTER_NUM I2C_NUM_1 // I2C port number for master dev #define I2C_MASTER_TX_BUF_DISABLE 0 // I2C master do not need buffer #define I2C_MASTER_RX_BUF_DISABLE 0 // I2C master do not need buffer -#define I2C_MASTER_FREQ_HZ 50000 // I2C master clock frequency +#define I2C_MASTER_FREQ_HZ 400000 // I2C master clock frequency #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