This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

new sensor configuration (Melexis MLX90641) - nRF9160

Hi, I am new to nordic and zephyr and I have a sensor (Melexis - MLX90641) and I would like to test it on a custom board using nRF9160.. I add the sensor into the dts files and I have done all the necessary configuration including I2C and I was able to bind it. now I would like to use the I2C functions to read and write .. but the sensor library is written to work with Mbed OS not zephyr. how can I modify the below functions to work with Zephyr?

I mean the equivalent functions for (i2c.stop , i2c.write, i2c.frequency, i2c.read) for the below example ?

int MLX90641_I2CWrite(uint8_t slaveAddr, uint16_t writeAddress, uint16_t data)
{
uint8_t sa;
int ack = 0;
char cmd[4] = {0,0,0,0};
static uint16_t dataCheck;

sa = (slaveAddr << 1);
cmd[0] = writeAddress >> 8;
cmd[1] = writeAddress & 0x00FF;
cmd[2] = data >> 8;
cmd[3] = data & 0x00FF;

i2c.stop();
wait_us(5);
ack = i2c.write(sa, cmd, 4, 0);

if (ack != 0x00)
{
return -1;
}
i2c.stop();

MLX90641_I2CRead(slaveAddr,writeAddress,1, &dataCheck);

if ( dataCheck != data)
{
return -2;
}

return 0;
}

Melexis MLX90641 library: https://github.com/melexis/mlx90641-library 

Parents Reply
  • Hi Carl,

    The zephyr i2c msg.buf accepts only uint_8 input, but in the above function (MLX90641_I2CWrite), the argument (data) - needs to be assigned to msg.buf - is uint16_t

    would you please advise on the below if I understood correctly from the examples:

    struct i2c_msg msg;
    uint8_t buf[4] = {0x0, 0x0, 0x0, 0x0};
    //sa = (slaveAddr << 1);
    buf[0] = writeAddress >> 8;
    buf[1] = writeAddress & 0x00FF;
    buf[2] = data >> 8;
    buf[3] = data & 0x00FF;
    msg.buf = buf;                   // Herę is the issue 
    msg.len = 4;
    msg.flags = I2C_MSG_WRITE | I2C_MSG_STOP;

    k_msleep(500);
    i2c_transfer(i2c_dev, &msg, 1,0x33);
Children
Related