KSZ8863 driver sharing I2C with other chips
KSZ8863 driver sharing I2C with other chips
I have a circuit using KSZ8863 designed many years ago, before the Espressif KSZ8863 driver was available, and put together my own driver, but would now like to switch to the supported driver. However, my circuit shares the I2C port with the KSZ8863 and another unrelated chip. I need to read the status of this chip often. Is there a way I can obtain the KSZ driver I2C bus-lock semaphore so that I can have other code use the same I2C port? Or any other way to do this? Any examples? thanks!
-
- Posts: 112
- Joined: Thu Oct 03, 2019 10:52 pm
- Contact:
Re: KSZ8863 driver sharing I2C with other chips
esp-idf does not have the ability to acquire an I2C bus lock semaphore.
It is necessary to construct a mechanism for exclusive control of the I2C bus by yourself.
All devices sharing an I2C port must be read and written within one task.
If you divide it into two tasks: reading the KSZ8863 and reading another unrelated chip, you will need exclusive control of the i2c bus.
It is necessary to construct a mechanism for exclusive control of the I2C bus by yourself.
All devices sharing an I2C port must be read and written within one task.
If you divide it into two tasks: reading the KSZ8863 and reading another unrelated chip, you will need exclusive control of the i2c bus.
-
- Posts: 1734
- Joined: Mon Oct 17, 2022 7:38 pm
- Location: Europe, Germany
Re: KSZ8863 driver sharing I2C with other chips
The IDF I2C driver takes care of that. You can talk to multiple devices from multiple tasks via the same I2C port without doing anything special.
-
- Posts: 112
- Joined: Thu Oct 03, 2019 10:52 pm
- Contact:
Re: KSZ8863 driver sharing I2C with other chips
> You can talk to multiple devices from multiple tasks via the same I2C port without doing anything special.
This is incorrect.
Thread Safety
The factory function i2c_new_master_bus() and i2c_new_slave_device() are guaranteed to be thread safe by the driver, which means that the functions can be called from different RTOS tasks without protection by extra locks. Other public I2C APIs are not thread safe, which means the user should avoid calling them from multiple tasks, if it is necessary to call them in multiple tasks, please add extra locks.
It's written here.
https://docs.espressif.com/projects/esp ... ead-safety
This is incorrect.
Thread Safety
The factory function i2c_new_master_bus() and i2c_new_slave_device() are guaranteed to be thread safe by the driver, which means that the functions can be called from different RTOS tasks without protection by extra locks. Other public I2C APIs are not thread safe, which means the user should avoid calling them from multiple tasks, if it is necessary to call them in multiple tasks, please add extra locks.
It's written here.
https://docs.espressif.com/projects/esp ... ead-safety
Re: KSZ8863 driver sharing I2C with other chips
The KSZ8863 driver has the following code snipits for internal access of the I2C control port:
And when it wants to use I2C it does:
But I don't see a way to be able to access these bus-lock functions externally, or access the semaphore itself from outside the driver.
Code: Select all
static inline bool bus_lock(uint32_t lock_timeout_ms)
{
return xSemaphoreTake(s_ksz8863_ctrl_intf->bus_lock, pdMS_TO_TICKS(lock_timeout_ms)) == pdTRUE;
}
static inline bool bus_unlock(void)
{
return xSemaphoreGive(s_ksz8863_ctrl_intf->bus_lock) == pdTRUE;
}
Code: Select all
ESP_GOTO_ON_FALSE(bus_lock(KSZ8863_I2C_LOCK_TIMEOUT_MS), ESP_ERR_TIMEOUT, err, TAG, "I2C bus lock timeout");
ESP_GOTO_ON_ERROR(i2c_master_cmd_begin(i2c_port, cmd, pdMS_TO_TICKS(KSZ8863_I2C_TIMEOUT_MS)), err_release, TAG,
"I2C master command begin error");
bus_unlock();
-
- Posts: 1734
- Joined: Mon Oct 17, 2022 7:38 pm
- Location: Europe, Germany
Re: KSZ8863 driver sharing I2C with other chips
It seems the KSZ8863 driver uses the older I2C routines that don't have the built-in locking. So maybe my solution is to update the KSZ drivers to the latest I2C functions.
-
- Posts: 112
- Joined: Thu Oct 03, 2019 10:52 pm
- Contact:
Re: KSZ8863 driver sharing I2C with other chips
https://github.com/espressif/esp-idf/bl ... ter.c#L897
you are right.
ESP-IDF i2c documentation is incorrect.
you are right.
ESP-IDF i2c documentation is incorrect.
-
- Posts: 1734
- Joined: Mon Oct 17, 2022 7:38 pm
- Location: Europe, Germany
Re: KSZ8863 driver sharing I2C with other chips
The "legacy" I2C driver also handles mutual exclusion for each I2C port: https://github.com/espressif/esp-idf/bl ... 2c.c#L1551
Who is online
Users browsing this forum: No registered users and 171 guests