Porting i2c driver to Zephyr

I would like porting i2c driver from RF5_SDK_16 to Zephyr. Does Zephyr provide functions related to the following RF5_SDK_16 functions?

nrf_drv_twi_init()
nrf_drv_twi_enable()
nrf_drv_twi_uninit()
nrf_drv_twi_tx()

Does any sample code for nRF52832 i2c?

Parents
  • Hello,

    You could take a look at the I2C drivers in DevAcademy: https://academy.nordicsemi.com/topic/i2c-driver/

    Best Regards,

    Swathy

  • Thanks for your reply! I have writes the following code for testing i2c_burst_read(). But i2c_burst_read() return -5. What is meaning of -5 error code and how can I fix this issue?

    testing code:

    int i2cInit(void) {
    int ret;

    if (!device_is_ready(_gI2cDev)) {
    printk("I2C: Device is not ready.\n");
    return ERROR_I2C_INITIALIZE_FAIL;
    }
    printk("I2C: Device is ready.\n");

    const uint32_t i2cCfg = I2C_MODE_CONTROLLER | I2C_SPEED_SET(I2C_SPEED_FAST);
    ret = i2c_configure(_gI2cDev, i2cCfg);
    if (ret < 0) {
    printk("I2C: Configure failed err=%d\n", ret);
    }
    printk("I2C: Configure okay.\n");

    #define AFE4900_I2C_ADDRESS 0x5B
    i2cSlaveAddr(AFE4900_I2C_ADDRESS);
    uint8_t buf[3];
    ret = i2c_burst_read(_gI2cDev, AFE4900_I2C_ADDRESS, 0x28, buf, 3);
    if (ret < 0) {
    printk("I2C: i2c_burst_read() failed err=%d\n", ret);
    } else {
    printk("0x%x 0x%x 0x%x\n", buf[2], buf[1], buf[0]);
    }

    return 0;
    }

    log messages:

    00> *** Booting Zephyr OS build v3.2.99-ncs2 ***
    00> Starting button Test example
    00> before calling i2cInit()
    00> I2C: Device is ready.
    00> I2C: Configure okay.
    00> [00:00:00.511,199] <err> i2c_nrfx_twim: Error on I2C line occurred for message 0
    00> I2C: i2c_burst_read() failed err=-5

Reply
  • Thanks for your reply! I have writes the following code for testing i2c_burst_read(). But i2c_burst_read() return -5. What is meaning of -5 error code and how can I fix this issue?

    testing code:

    int i2cInit(void) {
    int ret;

    if (!device_is_ready(_gI2cDev)) {
    printk("I2C: Device is not ready.\n");
    return ERROR_I2C_INITIALIZE_FAIL;
    }
    printk("I2C: Device is ready.\n");

    const uint32_t i2cCfg = I2C_MODE_CONTROLLER | I2C_SPEED_SET(I2C_SPEED_FAST);
    ret = i2c_configure(_gI2cDev, i2cCfg);
    if (ret < 0) {
    printk("I2C: Configure failed err=%d\n", ret);
    }
    printk("I2C: Configure okay.\n");

    #define AFE4900_I2C_ADDRESS 0x5B
    i2cSlaveAddr(AFE4900_I2C_ADDRESS);
    uint8_t buf[3];
    ret = i2c_burst_read(_gI2cDev, AFE4900_I2C_ADDRESS, 0x28, buf, 3);
    if (ret < 0) {
    printk("I2C: i2c_burst_read() failed err=%d\n", ret);
    } else {
    printk("0x%x 0x%x 0x%x\n", buf[2], buf[1], buf[0]);
    }

    return 0;
    }

    log messages:

    00> *** Booting Zephyr OS build v3.2.99-ncs2 ***
    00> Starting button Test example
    00> before calling i2cInit()
    00> I2C: Device is ready.
    00> I2C: Configure okay.
    00> [00:00:00.511,199] <err> i2c_nrfx_twim: Error on I2C line occurred for message 0
    00> I2C: i2c_burst_read() failed err=-5

Children
Related