preparation for switch to C6 MCU (but also compatible with S3)
Some checks failed
ESP-IDF Build / build (esp32c6, release-v5.4) (push) Failing after 2m23s
ESP-IDF Build / build (esp32c6, release-v5.5) (push) Failing after 6m1s
ESP-IDF Build / build (esp32s3, release-v5.4) (push) Failing after 2m17s
ESP-IDF Build / build (esp32s3, release-v5.5) (push) Failing after 5m57s
Some checks failed
ESP-IDF Build / build (esp32c6, release-v5.4) (push) Failing after 2m23s
ESP-IDF Build / build (esp32c6, release-v5.5) (push) Failing after 6m1s
ESP-IDF Build / build (esp32s3, release-v5.4) (push) Failing after 2m17s
ESP-IDF Build / build (esp32s3, release-v5.5) (push) Failing after 5m57s
Signed-off-by: Peter Siegmund <developer@mars3142.org>
This commit is contained in:
@@ -13,7 +13,11 @@ static const char *TAG = "u8g2_hal";
|
||||
static const unsigned int I2C_TIMEOUT_MS = 1000;
|
||||
|
||||
static spi_device_handle_t handle_spi; // SPI handle.
|
||||
static i2c_cmd_handle_t handle_i2c; // I2C handle.
|
||||
static i2c_master_bus_handle_t i2c_bus; // I2C bus handle (new driver).
|
||||
static i2c_master_dev_handle_t i2c_dev; // I2C device handle (new driver).
|
||||
static uint8_t i2c_tx_buf[256]; // Buffer for one I2C transaction.
|
||||
static size_t i2c_tx_len; // Current length in buffer.
|
||||
static uint8_t current_i2c_addr7; // Current 7-bit device address.
|
||||
static u8g2_esp32_hal_t u8g2_esp32_hal; // HAL state data.
|
||||
static bool i2c_transfer_failed = false; // Flag to track I2C transfer errors
|
||||
|
||||
@@ -148,21 +152,23 @@ uint8_t u8g2_esp32_i2c_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void
|
||||
break;
|
||||
}
|
||||
|
||||
i2c_config_t conf = {0};
|
||||
conf.mode = I2C_MODE_MASTER;
|
||||
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.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;
|
||||
ESP_LOGI(TAG, "i2c_param_config %d", conf.mode);
|
||||
ESP_ERROR_CHECK(i2c_param_config(I2C_MASTER_NUM, &conf));
|
||||
ESP_LOGI(TAG, "i2c_driver_install %d", I2C_MASTER_NUM);
|
||||
ESP_ERROR_CHECK(
|
||||
i2c_driver_install(I2C_MASTER_NUM, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0));
|
||||
// Neue I2C-Master-API: Bus einmalig anlegen
|
||||
if (i2c_bus == NULL)
|
||||
{
|
||||
i2c_master_bus_config_t bus_cfg = {
|
||||
.i2c_port = I2C_MASTER_NUM,
|
||||
.scl_io_num = u8g2_esp32_hal.bus.i2c.scl,
|
||||
.sda_io_num = u8g2_esp32_hal.bus.i2c.sda,
|
||||
.clk_source = I2C_CLK_SRC_DEFAULT,
|
||||
.flags = {.enable_internal_pullup = true},
|
||||
};
|
||||
|
||||
ESP_LOGI(TAG, "sda_io_num %d", u8g2_esp32_hal.bus.i2c.sda);
|
||||
ESP_LOGI(TAG, "scl_io_num %d", u8g2_esp32_hal.bus.i2c.scl);
|
||||
ESP_LOGI(TAG, "clk_speed %d", I2C_MASTER_FREQ_HZ);
|
||||
ESP_LOGI(TAG, "i2c_new_master_bus %d", I2C_MASTER_NUM);
|
||||
ESP_ERROR_CHECK(i2c_new_master_bus(&bus_cfg, &i2c_bus));
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -174,37 +180,55 @@ uint8_t u8g2_esp32_i2c_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void
|
||||
uint8_t *data_ptr = (uint8_t *)arg_ptr;
|
||||
ESP_LOG_BUFFER_HEXDUMP(TAG, data_ptr, arg_int, ESP_LOG_VERBOSE);
|
||||
|
||||
while (arg_int > 0)
|
||||
// Bytes in lokalen Puffer sammeln, tatsächliche Übertragung bei END_TRANSFER
|
||||
if (i2c_tx_len + (size_t)arg_int > sizeof(i2c_tx_buf))
|
||||
{
|
||||
I2C_ERROR_CHECK(i2c_master_write_byte(handle_i2c, *data_ptr, ACK_CHECK_EN));
|
||||
if (i2c_transfer_failed)
|
||||
{
|
||||
break;
|
||||
}
|
||||
data_ptr++;
|
||||
arg_int--;
|
||||
ESP_LOGW(TAG, "I2C tx buffer overflow (%zu + %d)", i2c_tx_len, arg_int);
|
||||
i2c_transfer_failed = true;
|
||||
break;
|
||||
}
|
||||
memcpy(&i2c_tx_buf[i2c_tx_len], data_ptr, arg_int);
|
||||
i2c_tx_len += (size_t)arg_int;
|
||||
break;
|
||||
}
|
||||
|
||||
case U8X8_MSG_BYTE_START_TRANSFER: {
|
||||
uint8_t i2c_address = u8x8_GetI2CAddress(u8x8);
|
||||
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);
|
||||
I2C_ERROR_CHECK(i2c_master_start(handle_i2c));
|
||||
I2C_ERROR_CHECK(i2c_master_write_byte(handle_i2c, i2c_address | I2C_MASTER_WRITE, ACK_CHECK_EN));
|
||||
i2c_transfer_failed = false; // Reset error flag at start of transfer
|
||||
|
||||
// Für neuen Treiber: Device-Handle für diese 7-Bit-Adresse anlegen (oder wiederverwenden)
|
||||
uint8_t addr7 = i2c_address >> 1;
|
||||
if (i2c_dev == NULL || addr7 != current_i2c_addr7)
|
||||
{
|
||||
if (i2c_dev)
|
||||
{
|
||||
i2c_master_bus_rm_device(i2c_dev);
|
||||
i2c_dev = NULL;
|
||||
}
|
||||
|
||||
i2c_device_config_t dev_cfg = {
|
||||
.device_address = addr7,
|
||||
.scl_speed_hz = I2C_MASTER_FREQ_HZ,
|
||||
};
|
||||
ESP_ERROR_CHECK(i2c_master_bus_add_device(i2c_bus, &dev_cfg, &i2c_dev));
|
||||
current_i2c_addr7 = addr7;
|
||||
}
|
||||
i2c_tx_len = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
case U8X8_MSG_BYTE_END_TRANSFER: {
|
||||
ESP_LOGD(TAG, "End I2C transfer.");
|
||||
if (!i2c_transfer_failed)
|
||||
if (!i2c_transfer_failed && i2c_dev != NULL && i2c_tx_len > 0)
|
||||
{
|
||||
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)));
|
||||
esp_err_t rc = i2c_master_transmit(i2c_dev, i2c_tx_buf, i2c_tx_len, I2C_TIMEOUT_MS);
|
||||
if (rc != ESP_OK)
|
||||
{
|
||||
ESP_LOGW(TAG, "I2C error: i2c_master_transmit = %d", rc);
|
||||
i2c_transfer_failed = true;
|
||||
}
|
||||
}
|
||||
i2c_cmd_link_delete(handle_i2c);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,56 +1,41 @@
|
||||
#include "i2c_checker.h"
|
||||
|
||||
#include "driver/i2c.h"
|
||||
#include "driver/i2c_master.h"
|
||||
#include "esp_insights.h"
|
||||
#include "esp_log.h"
|
||||
#include "hal/u8g2_esp32_hal.h"
|
||||
|
||||
static const char *TAG = "i2c_checker";
|
||||
|
||||
esp_err_t i2c_device_check(i2c_port_t i2c_port, uint8_t device_address)
|
||||
static esp_err_t i2c_device_check(i2c_master_bus_handle_t i2c_bus, uint8_t device_address)
|
||||
{
|
||||
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
|
||||
i2c_master_start(cmd);
|
||||
// Send the device address with the write bit (LSB = 0)
|
||||
i2c_master_write_byte(cmd, (device_address << 1) | I2C_MASTER_WRITE, true);
|
||||
i2c_master_stop(cmd);
|
||||
|
||||
esp_err_t ret = i2c_master_cmd_begin(i2c_port, cmd, pdMS_TO_TICKS(100));
|
||||
|
||||
i2c_cmd_link_delete(cmd);
|
||||
|
||||
return ret;
|
||||
// Use the new I2C master driver to probe for the device.
|
||||
return i2c_master_probe(i2c_bus, device_address, 100);
|
||||
}
|
||||
|
||||
esp_err_t i2c_bus_scan_and_check(void)
|
||||
{
|
||||
// 1. Configure and install I2C bus
|
||||
i2c_config_t conf = {
|
||||
.mode = I2C_MODE_MASTER,
|
||||
.sda_io_num = I2C_MASTER_SDA_PIN,
|
||||
// 1. Configure and create I2C master bus using the new driver API
|
||||
i2c_master_bus_handle_t i2c_bus = NULL;
|
||||
i2c_master_bus_config_t bus_cfg = {
|
||||
.i2c_port = I2C_MASTER_NUM,
|
||||
.scl_io_num = I2C_MASTER_SCL_PIN,
|
||||
.sda_pullup_en = GPIO_PULLUP_ENABLE,
|
||||
.scl_pullup_en = GPIO_PULLUP_ENABLE,
|
||||
.master.clk_speed = I2C_MASTER_FREQ_HZ,
|
||||
.sda_io_num = I2C_MASTER_SDA_PIN,
|
||||
.clk_source = I2C_CLK_SRC_DEFAULT,
|
||||
.flags = {.enable_internal_pullup = true},
|
||||
};
|
||||
esp_err_t err = i2c_param_config(I2C_MASTER_NUM, &conf);
|
||||
|
||||
esp_err_t err = i2c_new_master_bus(&bus_cfg, &i2c_bus);
|
||||
if (err != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "I2C parameter configuration failed: %s", esp_err_to_name(err));
|
||||
ESP_LOGE(TAG, "I2C bus creation failed: %s", esp_err_to_name(err));
|
||||
return err;
|
||||
}
|
||||
|
||||
err = i2c_driver_install(I2C_MASTER_NUM, conf.mode, 0, 0, 0);
|
||||
if (err != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "I2C driver installation failed: %s", esp_err_to_name(err));
|
||||
return err;
|
||||
}
|
||||
ESP_LOGI(TAG, "I2C master bus initialized. Searching for device...");
|
||||
|
||||
ESP_LOGI(TAG, "I2C driver initialized. Searching for device...");
|
||||
|
||||
// 2. Check if the device is present
|
||||
err = i2c_device_check(I2C_MASTER_NUM, DISPLAY_I2C_ADDRESS);
|
||||
// 2. Check if the device is present using the new API
|
||||
err = i2c_device_check(i2c_bus, DISPLAY_I2C_ADDRESS);
|
||||
|
||||
if (err == ESP_OK)
|
||||
{
|
||||
@@ -66,9 +51,13 @@ esp_err_t i2c_bus_scan_and_check(void)
|
||||
ESP_LOGE(TAG, "Error communicating with address 0x%02X: %s", DISPLAY_I2C_ADDRESS, esp_err_to_name(err));
|
||||
}
|
||||
|
||||
// 3. Uninstall I2C driver if it is no longer needed
|
||||
i2c_driver_delete(I2C_MASTER_NUM);
|
||||
ESP_DIAG_EVENT(TAG, "I2C driver uninstalled.");
|
||||
// 3. Delete I2C master bus if it is no longer needed
|
||||
esp_err_t del_err = i2c_del_master_bus(i2c_bus);
|
||||
if (del_err != ESP_OK)
|
||||
{
|
||||
ESP_LOGW(TAG, "Failed to delete I2C master bus: %s", esp_err_to_name(del_err));
|
||||
}
|
||||
ESP_DIAG_EVENT(TAG, "I2C master bus deleted.");
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user