From bad73fff48920000c51af5e3494fdbfaae1830f8 Mon Sep 17 00:00:00 2001 From: RobMeades Date: Wed, 28 Aug 2024 18:24:18 +0100 Subject: [PATCH] Debug. Test: 10.0 27 portI2c --- port/platform/esp-idf/src/u_port_i2c.c | 60 +++++++++++++++----------- 1 file changed, 36 insertions(+), 24 deletions(-) diff --git a/port/platform/esp-idf/src/u_port_i2c.c b/port/platform/esp-idf/src/u_port_i2c.c index 9d541cc5..d23f94ad 100644 --- a/port/platform/esp-idf/src/u_port_i2c.c +++ b/port/platform/esp-idf/src/u_port_i2c.c @@ -53,7 +53,7 @@ #endif #if !defined(ESP_IDF_I2C_NEW_API_DISABLE) && (ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 4, 0)) -/** If we are using ESP-IDF older than version 5.2.0, use the old I2C API. +/** If we are using ESP-IDF older than version 5.4.0, use the old I2C API. */ # define ESP_IDF_I2C_NEW_API_DISABLE #endif @@ -79,28 +79,37 @@ */ #define U_PORT_10BIT_ADDRESS(__ADDRESS__) ((__ADDRESS__) & 0xFF) -#ifndef U_PORT_I2C_ESP32X3_CLOCK_SOURCE -/** For ESP32 the I2C clock source is the APB clock (80 MHz) - * and this code doesn't care, however for ESP32x3 the clock - * source can be selected between the crystal/XTAL (40 MHz) and - * the RC network which drives the RTC (17.5 MHz); the I2C - * timeout value is calculated differently depending on which - * source is employed. The crystal is the default: switch to - * the RC network by setting this #define to - * I2C_SCLK_SRC_FLAG_LIGHT_SLEEP. - */ -# define U_PORT_I2C_ESP32X3_CLOCK_SOURCE I2C_CLK_SRC_DEFAULT -#endif - #ifndef U_PORT_I2C_CLOCK_SOURCE /** The new ESP-IDF I2C API allows a clock source setting in * all cases, not just the ESP32X3 case. Here we just pick - * up the default. + * up what a user has defined for X3 (for backwards compatibility) + * or otherwise the default. */ # ifdef U_PORT_I2C_ESP32X3_CLOCK_SOURCE # define U_PORT_I2C_CLOCK_SOURCE U_PORT_I2C_ESP32X3_CLOCK_SOURCE # else -# define U_PORT_I2C_CLOCK_SOURCE I2C_CLK_SRC_DEFAULT +# ifdef ESP_IDF_I2C_NEW_API_DISABLE +# define U_PORT_I2C_CLOCK_SOURCE 0 +# else +# define U_PORT_I2C_CLOCK_SOURCE I2C_CLK_SRC_DEFAULT +# endif +# endif +#endif + +#ifndef U_PORT_I2C_ESP32X3_CLOCK_SOURCE +/** For ESP32 and the old I2C API the I2C clock source is the + * APB clock (80 MHz) and this code doesn't care, however for + * ESP32x3 the clock source can be selected between the + * crystal/XTAL (40 MHz) and the RC network which drives the + * RTC (17.5 MHz); the I2C timeout value is calculated differently + * depending on which source is employed. The crystal is the + * default: switch to the RC network by setting this #define to + * I2C_SCLK_SRC_FLAG_LIGHT_SLEEP. + */ +# ifdef ESP_IDF_I2C_NEW_API_DISABLE +# define U_PORT_I2C_ESP32X3_CLOCK_SOURCE 0 +# else +# define U_PORT_I2C_ESP32X3_CLOCK_SOURCE I2C_CLK_SRC_DEFAULT # endif #endif @@ -132,16 +141,17 @@ #ifndef U_PORT_CLOCK_WAIT_TIME_MICROSECONDS /** The clock wait time in microseconds for the new ESP-IDF - * I2C API; 0 means use the default. + * I2C API; this needs to take into account the amount of time + * that an attached [GNSS] device could stretch the clock for. */ -# define U_PORT_CLOCK_WAIT_TIME_MICROSECONDS 0 +# define U_PORT_CLOCK_WAIT_TIME_MICROSECONDS ((U_PORT_I2C_TIMEOUT_MILLISECONDS) * 1000) #endif #ifndef U_PORT_I2C_TRANSACTION_TIMEOUT_MS -/** The new ESP-IDF I2C API requires a transaction timeout, - * as opposed to a per-byte timeout. +/** The new ESP-IDF I2C API has a transaction timeout but + * -1 can be used to ignore it. */ -# define U_PORT_I2C_TRANSACTION_TIMEOUT_MS 100 +# define U_PORT_I2C_TRANSACTION_TIMEOUT_MS -1 #endif /* ---------------------------------------------------------------- @@ -159,8 +169,10 @@ typedef struct { // busHandle; keeps the code simple int32_t clockHertz; bool adopted; +#ifndef ESP_IDF_I2C_NEW_API_DISABLE i2c_master_bus_handle_t busHandle; // Only used by the new ESP-IDF I2C API i2c_master_dev_handle_t devHandle; // Only used by the new ESP-IDF I2C API +#endif uint16_t address; // Only used by the new ESP-IDF I2C API } uPortI2cData_t; @@ -289,7 +301,6 @@ static int32_t receive(int32_t handle, uint16_t address, char *pData, size_t siz } } if (errorCodeOrLength == (int32_t) U_ERROR_COMMON_SUCCESS) { - // Now read the data, the last byte with a nack, and execute it // Now read the data, the last byte with a nack, and execute it if (size > 1) { if ((i2c_master_read(cmd, (uint8_t *) pData, size - 1, I2C_MASTER_ACK) != ESP_OK) || @@ -365,6 +376,8 @@ static int32_t ensureDevice(int32_t handle, uint16_t address, if (i2c_master_bus_add_device(gI2cData[handle].busHandle, &devCfg, &(gI2cData[handle].devHandle)) == ESP_OK) { + uPortLog("### Adding device address 0x%02x, %d Hz, SCL wait %d us.\n", + devCfg.device_address, devCfg.scl_speed_hz, devCfg.scl_wait_us); gI2cData[handle].address = devCfg.device_address; errorCode = (int32_t) U_ERROR_COMMON_SUCCESS; } @@ -553,7 +566,6 @@ int32_t uPortI2cCloseRecoverBus(int32_t handle) closeI2c(handle); // Nothing to do - bus recovery is done as required // on ESP-IDF; return "not supported" to indicate this - errorCode = (int32_t) U_ERROR_COMMON_NOT_SUPPORTED; } } @@ -633,7 +645,7 @@ int32_t uPortI2cSetClock(int32_t handle, int32_t clockHertz) // Set the clock frequency in the main configuration structure gI2cData[handle].clockHertz = clockHertz; } - // Now remove it again; the send/receive + // Now remove the device again; the send/receive // functions will set it up with the correct // device address when they need it removeDevice(handle);