Dynamic I2C configuration

Dear colleagues,

I'd be very grateful for your help in the following challenge: I would need to change pins and/or i2c bus speed without recompilation of my code.

I can perfectly communicate to my i2c slaves from the 52833 (custom board) when my i2c0 is defined in DTS. This works great allowing me to use various devices just by calling i2c_transfer() with different flags and device addresses.

This is not enough. I need to be able to reconfigure SCL/SDA pins "on the fly" per external request. It seems the relevant API is present here, and here. Is there any example of how to use i2c bus without DTS please? 

As an option I am also considering taking the initial i2c configuration from the dts (like it works now) just by letting it be initialized on runlevel stage. Next I would need to reconfigure that one. Unfortunately I see in the code that lefts .get_config field of device's API not filled and configuration structures are left static - for the driver's internal use as I can imagine. As such i2c_get_config() is not working in that context due to above mentioned reason. So in this case so there must be some other way to program i2c that would allow e.g. using i2c_get_config(), i2c_configure(), etc.

Any other approach / example on how to change i2c pins and speed without recompilation of the code would be very much appreciated.

Thanks!

Parents
  • Hi Einar,

    Thanks a lot for your suggestion! I will try to figure out if pin control could be used. I am little bit uncertain because of the following statement in the doc:

    Note

    Dynamic pin control should only be used on devices that have not been initialized. Changing pin configurations while a device is operating may lead to unexpected behavior. Since Zephyr does not support device de-initialization yet, this functionality should only be used during early boot stages.

    As we would need to reset pins during on the fly, I would need to find a proper de-initialization function that is not static Slight smile

    And yes, i2c_configure() does change bitrate for me without re-initialization of the device. I was lucky that this function is only changing interface speed (and allowing 10 bits operations). Note, that the proper way to use it would be to a) get current configuration [that is not possible :\] b) modify it accordingly, and c) set it back using i2c_configure(). Its parameter dev_config is also "encrypted" as the doc only says that it is some bit-packed structure while in fact there are perfectly defined relevant macros in the codebase like for example 

    I2C_SPEED_SET(I2C_SPEED_FAST)
    So I managed to change bitrate on the fly although I had to explore the SDK source for that Slight smile
Reply
  • Hi Einar,

    Thanks a lot for your suggestion! I will try to figure out if pin control could be used. I am little bit uncertain because of the following statement in the doc:

    Note

    Dynamic pin control should only be used on devices that have not been initialized. Changing pin configurations while a device is operating may lead to unexpected behavior. Since Zephyr does not support device de-initialization yet, this functionality should only be used during early boot stages.

    As we would need to reset pins during on the fly, I would need to find a proper de-initialization function that is not static Slight smile

    And yes, i2c_configure() does change bitrate for me without re-initialization of the device. I was lucky that this function is only changing interface speed (and allowing 10 bits operations). Note, that the proper way to use it would be to a) get current configuration [that is not possible :\] b) modify it accordingly, and c) set it back using i2c_configure(). Its parameter dev_config is also "encrypted" as the doc only says that it is some bit-packed structure while in fact there are perfectly defined relevant macros in the codebase like for example 

    I2C_SPEED_SET(I2C_SPEED_FAST)
    So I managed to change bitrate on the fly although I had to explore the SDK source for that Slight smile
Children
Related