How can I set a non-standard speed for the I2C bus using the Zephyr?

Hi,

Using the Zephyr how can I set a TWI module clock frequency different from the standard speeds?

With nRF SDK I was able to do this by setting "frequency = 0x00290000UL".

void ext_twi_init(void)
{
    ret_code_t err_code;

    const nrf_drv_twi_config_t twi_config = {
       .scl                = EXT_SCL_PIN,
       .sda                = EXT_SDA_PIN,
       .frequency          = 0x00290000UL,      // = 10Kbit/s. <------------
       .interrupt_priority = APP_IRQ_PRIORITY_HIGH,
       .clear_bus_init     = false
    };

    err_code = nrf_drv_twi_init(&m_ext_twi, &twi_config, NULL, NULL);
    APP_ERROR_CHECK(err_code);

    nrf_drv_twi_enable(&m_ext_twi);
}

Using the Zephyr instead I only have the following speeds defined in the i2c.h file:

#define I2C_BITRATE_STANDARD	100000	/* 100 Kbit/s */
#define I2C_BITRATE_FAST	400000	/* 400 Kbit/s */
#define I2C_BITRATE_FAST_PLUS	1000000 /* 1 Mbit/s */
#define I2C_BITRATE_HIGH	3400000	/* 3.4 Mbit/s */
#define I2C_BITRATE_ULTRA	5000000 /* 5 Mbit/s */

If I define a new speed in the i2c.h file, for example:

#define I2C_BITRATE_10KHZ 10000

&i2c0 {
	status = "okay";
	pinctrl-0 = <&i2c0_default>;
	pinctrl-names = "default";
	clock-frequency = <I2C_BITRATE_10KHZ>;
};

It gives me the following error:

static assertion failed: "Wrong I2C 0 frequency setting in dts"

Can anyone help me define a non-standard speed?

Thank you.

Parents
  • For future reference and to anyone who might be interested, I found quite an easy hack to change the i2c bitrate:

    this post explains a very easy hack to set the bitrate lower than 100kbps (Thank you !)

    Under `toolchainVersion > modules > hal > nordic > nrfx > mdk > nrf9160_bitfields.h` you can find the line

    #define TWIM_FREQUENCY_FREQUENCY_K100 (0x01980000UL) /*!< 100 kbps */

    Simply changing this value according this instruction was the solution to our problem. So I set the value to 10 kbps

    #define TWIM_FREQUENCY_FREQUENCY_K100 (0x0028F000UL) /*!< 100 kbps */

    and here you can see that the SCL ticks are 100 µs long (= 10kbps) and the sensor responds now.

    We are reading a sensor hanging on an 8 meter long cable, which is impossible without lowering the bitrate to a much lower value.

Reply
  • For future reference and to anyone who might be interested, I found quite an easy hack to change the i2c bitrate:

    this post explains a very easy hack to set the bitrate lower than 100kbps (Thank you !)

    Under `toolchainVersion > modules > hal > nordic > nrfx > mdk > nrf9160_bitfields.h` you can find the line

    #define TWIM_FREQUENCY_FREQUENCY_K100 (0x01980000UL) /*!< 100 kbps */

    Simply changing this value according this instruction was the solution to our problem. So I set the value to 10 kbps

    #define TWIM_FREQUENCY_FREQUENCY_K100 (0x0028F000UL) /*!< 100 kbps */

    and here you can see that the SCL ticks are 100 µs long (= 10kbps) and the sensor responds now.

    We are reading a sensor hanging on an 8 meter long cable, which is impossible without lowering the bitrate to a much lower value.

Children
No Data
Related