Skip to content
This repository has been archived by the owner on Nov 11, 2024. It is now read-only.

Commit

Permalink
Debug.
Browse files Browse the repository at this point in the history
Test: 10.0 27 portI2c
  • Loading branch information
RobMeades committed Aug 29, 2024
1 parent 38b89ef commit bad73ff
Showing 1 changed file with 36 additions and 24 deletions.
60 changes: 36 additions & 24 deletions port/platform/esp-idf/src/u_port_i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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

/* ----------------------------------------------------------------
Expand All @@ -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;

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

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

0 comments on commit bad73ff

Please sign in to comment.