fix I2C timeout crash

Signed-off-by: Peter Siegmund <developer@mars3142.org>
This commit is contained in:
2025-12-27 00:10:51 +01:00
parent e7af663bc3
commit 9e9fb15f86

View File

@@ -9,29 +9,45 @@
#include "u8g2_esp32_hal.h" #include "u8g2_esp32_hal.h"
static const char* TAG = "u8g2_hal"; static const char *TAG = "u8g2_hal";
static const unsigned int I2C_TIMEOUT_MS = 1000; static const unsigned int I2C_TIMEOUT_MS = 1000;
static spi_device_handle_t handle_spi; // SPI handle. static spi_device_handle_t handle_spi; // SPI handle.
static i2c_cmd_handle_t handle_i2c; // I2C handle. static i2c_cmd_handle_t handle_i2c; // I2C handle.
static u8g2_esp32_hal_t u8g2_esp32_hal; // HAL state data. static u8g2_esp32_hal_t u8g2_esp32_hal; // HAL state data.
static bool i2c_transfer_failed = false; // Flag to track I2C transfer errors
#define HOST SPI2_HOST #define HOST SPI2_HOST
#undef ESP_ERROR_CHECK #undef ESP_ERROR_CHECK
#define ESP_ERROR_CHECK(x) \ #define ESP_ERROR_CHECK(x) \
do { \ do \
{ \
esp_err_t rc = (x); \ esp_err_t rc = (x); \
if (rc != ESP_OK) { \ if (rc != ESP_OK) \
{ \
ESP_LOGE("err", "esp_err_t = %d", rc); \ ESP_LOGE("err", "esp_err_t = %d", rc); \
assert(0 && #x); \ assert(0 && #x); \
} \ } \
} while (0); } while (0);
// Softer error handling for I2C operations that may fail temporarily
#define I2C_ERROR_CHECK(x) \
do \
{ \
esp_err_t rc = (x); \
if (rc != ESP_OK) \
{ \
ESP_LOGW(TAG, "I2C error: %s = %d", #x, rc); \
i2c_transfer_failed = true; \
} \
} while (0);
/* /*
* Initialze the ESP32 HAL. * Initialze the ESP32 HAL.
*/ */
void u8g2_esp32_hal_init(u8g2_esp32_hal_t u8g2_esp32_hal_param) { void u8g2_esp32_hal_init(u8g2_esp32_hal_t u8g2_esp32_hal_param)
{
u8g2_esp32_hal = u8g2_esp32_hal_param; u8g2_esp32_hal = u8g2_esp32_hal_param;
} // u8g2_esp32_hal_init } // u8g2_esp32_hal_init
@@ -39,15 +55,14 @@ void u8g2_esp32_hal_init(u8g2_esp32_hal_t u8g2_esp32_hal_param) {
* HAL callback function as prescribed by the U8G2 library. This callback is * HAL callback function as prescribed by the U8G2 library. This callback is
* invoked to handle SPI communications. * invoked to handle SPI communications.
*/ */
uint8_t u8g2_esp32_spi_byte_cb(u8x8_t* u8x8, uint8_t u8g2_esp32_spi_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
uint8_t msg, {
uint8_t arg_int, ESP_LOGD(TAG, "spi_byte_cb: Received a msg: %d, arg_int: %d, arg_ptr: %p", msg, arg_int, arg_ptr);
void* arg_ptr) { switch (msg)
ESP_LOGD(TAG, "spi_byte_cb: Received a msg: %d, arg_int: %d, arg_ptr: %p", {
msg, arg_int, arg_ptr);
switch (msg) {
case U8X8_MSG_BYTE_SET_DC: case U8X8_MSG_BYTE_SET_DC:
if (u8g2_esp32_hal.dc != U8G2_ESP32_HAL_UNDEFINED) { if (u8g2_esp32_hal.dc != U8G2_ESP32_HAL_UNDEFINED)
{
gpio_set_level(u8g2_esp32_hal.dc, arg_int); gpio_set_level(u8g2_esp32_hal.dc, arg_int);
} }
break; break;
@@ -55,7 +70,8 @@ uint8_t u8g2_esp32_spi_byte_cb(u8x8_t* u8x8,
case U8X8_MSG_BYTE_INIT: { case U8X8_MSG_BYTE_INIT: {
if (u8g2_esp32_hal.bus.spi.clk == 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.mosi == U8G2_ESP32_HAL_UNDEFINED ||
u8g2_esp32_hal.bus.spi.cs == U8G2_ESP32_HAL_UNDEFINED) { u8g2_esp32_hal.bus.spi.cs == U8G2_ESP32_HAL_UNDEFINED)
{
break; break;
} }
@@ -111,16 +127,15 @@ uint8_t u8g2_esp32_spi_byte_cb(u8x8_t* u8x8,
* HAL callback function as prescribed by the U8G2 library. This callback is * HAL callback function as prescribed by the U8G2 library. This callback is
* invoked to handle I2C communications. * invoked to handle I2C communications.
*/ */
uint8_t u8g2_esp32_i2c_byte_cb(u8x8_t* u8x8, uint8_t u8g2_esp32_i2c_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
uint8_t msg, {
uint8_t arg_int, ESP_LOGD(TAG, "i2c_cb: Received a msg: %d, arg_int: %d, arg_ptr: %p", msg, arg_int, arg_ptr);
void* arg_ptr) {
ESP_LOGD(TAG, "i2c_cb: Received a msg: %d, arg_int: %d, arg_ptr: %p", msg,
arg_int, arg_ptr);
switch (msg) { switch (msg)
{
case U8X8_MSG_BYTE_SET_DC: { case U8X8_MSG_BYTE_SET_DC: {
if (u8g2_esp32_hal.dc != U8G2_ESP32_HAL_UNDEFINED) { if (u8g2_esp32_hal.dc != U8G2_ESP32_HAL_UNDEFINED)
{
gpio_set_level(u8g2_esp32_hal.dc, arg_int); gpio_set_level(u8g2_esp32_hal.dc, arg_int);
} }
break; break;
@@ -128,7 +143,8 @@ uint8_t u8g2_esp32_i2c_byte_cb(u8x8_t* u8x8,
case U8X8_MSG_BYTE_INIT: { case U8X8_MSG_BYTE_INIT: {
if (u8g2_esp32_hal.bus.i2c.sda == 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) { u8g2_esp32_hal.bus.i2c.scl == U8G2_ESP32_HAL_UNDEFINED)
{
break; break;
} }
@@ -145,19 +161,26 @@ uint8_t u8g2_esp32_i2c_byte_cb(u8x8_t* u8x8,
ESP_LOGI(TAG, "i2c_param_config %d", conf.mode); ESP_LOGI(TAG, "i2c_param_config %d", conf.mode);
ESP_ERROR_CHECK(i2c_param_config(I2C_MASTER_NUM, &conf)); ESP_ERROR_CHECK(i2c_param_config(I2C_MASTER_NUM, &conf));
ESP_LOGI(TAG, "i2c_driver_install %d", I2C_MASTER_NUM); ESP_LOGI(TAG, "i2c_driver_install %d", I2C_MASTER_NUM);
ESP_ERROR_CHECK(i2c_driver_install(I2C_MASTER_NUM, conf.mode, ESP_ERROR_CHECK(
I2C_MASTER_RX_BUF_DISABLE, i2c_driver_install(I2C_MASTER_NUM, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0));
I2C_MASTER_TX_BUF_DISABLE, 0));
break; break;
} }
case U8X8_MSG_BYTE_SEND: { case U8X8_MSG_BYTE_SEND: {
uint8_t* data_ptr = (uint8_t*)arg_ptr; if (i2c_transfer_failed)
{
break; // Skip sending if transfer already failed
}
uint8_t *data_ptr = (uint8_t *)arg_ptr;
ESP_LOG_BUFFER_HEXDUMP(TAG, data_ptr, arg_int, ESP_LOG_VERBOSE); ESP_LOG_BUFFER_HEXDUMP(TAG, data_ptr, arg_int, ESP_LOG_VERBOSE);
while (arg_int > 0) { while (arg_int > 0)
ESP_ERROR_CHECK( {
i2c_master_write_byte(handle_i2c, *data_ptr, ACK_CHECK_EN)); I2C_ERROR_CHECK(i2c_master_write_byte(handle_i2c, *data_ptr, ACK_CHECK_EN));
if (i2c_transfer_failed)
{
break;
}
data_ptr++; data_ptr++;
arg_int--; arg_int--;
} }
@@ -167,18 +190,20 @@ uint8_t u8g2_esp32_i2c_byte_cb(u8x8_t* u8x8,
case U8X8_MSG_BYTE_START_TRANSFER: { case U8X8_MSG_BYTE_START_TRANSFER: {
uint8_t i2c_address = u8x8_GetI2CAddress(u8x8); uint8_t i2c_address = u8x8_GetI2CAddress(u8x8);
handle_i2c = i2c_cmd_link_create(); handle_i2c = i2c_cmd_link_create();
i2c_transfer_failed = false; // Reset error flag at start of transfer
ESP_LOGD(TAG, "Start I2C transfer to %02X.", i2c_address >> 1); ESP_LOGD(TAG, "Start I2C transfer to %02X.", i2c_address >> 1);
ESP_ERROR_CHECK(i2c_master_start(handle_i2c)); I2C_ERROR_CHECK(i2c_master_start(handle_i2c));
ESP_ERROR_CHECK(i2c_master_write_byte( I2C_ERROR_CHECK(i2c_master_write_byte(handle_i2c, i2c_address | I2C_MASTER_WRITE, ACK_CHECK_EN));
handle_i2c, i2c_address | I2C_MASTER_WRITE, ACK_CHECK_EN));
break; break;
} }
case U8X8_MSG_BYTE_END_TRANSFER: { case U8X8_MSG_BYTE_END_TRANSFER: {
ESP_LOGD(TAG, "End I2C transfer."); ESP_LOGD(TAG, "End I2C transfer.");
ESP_ERROR_CHECK(i2c_master_stop(handle_i2c)); if (!i2c_transfer_failed)
ESP_ERROR_CHECK(i2c_master_cmd_begin(I2C_MASTER_NUM, handle_i2c, {
pdMS_TO_TICKS(I2C_TIMEOUT_MS))); I2C_ERROR_CHECK(i2c_master_stop(handle_i2c));
I2C_ERROR_CHECK(i2c_master_cmd_begin(I2C_MASTER_NUM, handle_i2c, pdMS_TO_TICKS(I2C_TIMEOUT_MS)));
}
i2c_cmd_link_delete(handle_i2c); i2c_cmd_link_delete(handle_i2c);
break; break;
} }
@@ -190,30 +215,31 @@ uint8_t u8g2_esp32_i2c_byte_cb(u8x8_t* u8x8,
* HAL callback function as prescribed by the U8G2 library. This callback is * HAL callback function as prescribed by the U8G2 library. This callback is
* invoked to handle callbacks for GPIO and delay functions. * invoked to handle callbacks for GPIO and delay functions.
*/ */
uint8_t u8g2_esp32_gpio_and_delay_cb(u8x8_t* u8x8, uint8_t u8g2_esp32_gpio_and_delay_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
uint8_t msg, {
uint8_t arg_int, ESP_LOGD(TAG, "gpio_and_delay_cb: Received a msg: %d, arg_int: %d, arg_ptr: %p", msg, arg_int, arg_ptr);
void* arg_ptr) {
ESP_LOGD(TAG,
"gpio_and_delay_cb: Received a msg: %d, arg_int: %d, arg_ptr: %p",
msg, arg_int, arg_ptr);
switch (msg) { switch (msg)
{
// Initialize the GPIO and DELAY HAL functions. If the pins for DC and // Initialize the GPIO and DELAY HAL functions. If the pins for DC and
// RESET have been specified then we define those pins as GPIO outputs. // RESET have been specified then we define those pins as GPIO outputs.
case U8X8_MSG_GPIO_AND_DELAY_INIT: { case U8X8_MSG_GPIO_AND_DELAY_INIT: {
uint64_t bitmask = 0; uint64_t bitmask = 0;
if (u8g2_esp32_hal.dc != U8G2_ESP32_HAL_UNDEFINED) { if (u8g2_esp32_hal.dc != U8G2_ESP32_HAL_UNDEFINED)
{
bitmask = bitmask | (1ull << u8g2_esp32_hal.dc); bitmask = bitmask | (1ull << u8g2_esp32_hal.dc);
} }
if (u8g2_esp32_hal.reset != U8G2_ESP32_HAL_UNDEFINED) { if (u8g2_esp32_hal.reset != U8G2_ESP32_HAL_UNDEFINED)
{
bitmask = bitmask | (1ull << u8g2_esp32_hal.reset); bitmask = bitmask | (1ull << u8g2_esp32_hal.reset);
} }
if (u8g2_esp32_hal.bus.spi.cs != U8G2_ESP32_HAL_UNDEFINED) { if (u8g2_esp32_hal.bus.spi.cs != U8G2_ESP32_HAL_UNDEFINED)
{
bitmask = bitmask | (1ull << u8g2_esp32_hal.bus.spi.cs); bitmask = bitmask | (1ull << u8g2_esp32_hal.bus.spi.cs);
} }
if (bitmask == 0) { if (bitmask == 0)
{
break; break;
} }
gpio_config_t gpioConfig; gpio_config_t gpioConfig;
@@ -228,26 +254,30 @@ uint8_t u8g2_esp32_gpio_and_delay_cb(u8x8_t* u8x8,
// Set the GPIO reset pin to the value passed in through arg_int. // Set the GPIO reset pin to the value passed in through arg_int.
case U8X8_MSG_GPIO_RESET: case U8X8_MSG_GPIO_RESET:
if (u8g2_esp32_hal.reset != U8G2_ESP32_HAL_UNDEFINED) { if (u8g2_esp32_hal.reset != U8G2_ESP32_HAL_UNDEFINED)
{
gpio_set_level(u8g2_esp32_hal.reset, arg_int); gpio_set_level(u8g2_esp32_hal.reset, arg_int);
} }
break; break;
// Set the GPIO client select pin to the value passed in through arg_int. // Set the GPIO client select pin to the value passed in through arg_int.
case U8X8_MSG_GPIO_CS: case U8X8_MSG_GPIO_CS:
if (u8g2_esp32_hal.bus.spi.cs != U8G2_ESP32_HAL_UNDEFINED) { if (u8g2_esp32_hal.bus.spi.cs != U8G2_ESP32_HAL_UNDEFINED)
{
gpio_set_level(u8g2_esp32_hal.bus.spi.cs, arg_int); gpio_set_level(u8g2_esp32_hal.bus.spi.cs, arg_int);
} }
break; break;
// Set the Software I²C pin to the value passed in through arg_int. // Set the Software I²C pin to the value passed in through arg_int.
case U8X8_MSG_GPIO_I2C_CLOCK: case U8X8_MSG_GPIO_I2C_CLOCK:
if (u8g2_esp32_hal.bus.i2c.scl != U8G2_ESP32_HAL_UNDEFINED) { if (u8g2_esp32_hal.bus.i2c.scl != U8G2_ESP32_HAL_UNDEFINED)
{
gpio_set_level(u8g2_esp32_hal.bus.i2c.scl, arg_int); gpio_set_level(u8g2_esp32_hal.bus.i2c.scl, arg_int);
// printf("%c",(arg_int==1?'C':'c')); // printf("%c",(arg_int==1?'C':'c'));
} }
break; break;
// Set the Software I²C pin to the value passed in through arg_int. // Set the Software I²C pin to the value passed in through arg_int.
case U8X8_MSG_GPIO_I2C_DATA: case U8X8_MSG_GPIO_I2C_DATA:
if (u8g2_esp32_hal.bus.i2c.sda != U8G2_ESP32_HAL_UNDEFINED) { if (u8g2_esp32_hal.bus.i2c.sda != U8G2_ESP32_HAL_UNDEFINED)
{
gpio_set_level(u8g2_esp32_hal.bus.i2c.sda, arg_int); gpio_set_level(u8g2_esp32_hal.bus.i2c.sda, arg_int);
// printf("%c",(arg_int==1?'D':'d')); // printf("%c",(arg_int==1?'D':'d'));
} }