I2C drivers seems to be corrupting memory

rwel59
Posts: 27
Joined: Thu Oct 12, 2017 3:32 pm

I2C drivers seems to be corrupting memory

Postby rwel59 » Thu Nov 16, 2017 2:08 am

Kind of a strange one that I have been fighting all day. When I perform an operation on bytes read from i2c before shutting i2c down, the bytes get corrupted.

Code: Select all

    cmd = i2c_cmd_link_create();
    i2c_master_start(cmd);
    i2c_master_write_byte(cmd, i2c_addr << 1 | READ_BIT, ACK_CHECK_EN);
    i2c_master_read_byte(cmd, &bytes[0], ACK_VAL);
    i2c_master_read_byte(cmd, &bytes[1], NACK_VAL);

    i2c_master_stop(cmd);
    ret = i2c_master_cmd_begin(i2c_num, cmd, 1000 / portTICK_RATE_MS);
    i2c_cmd_link_delete(cmd);

    bytes[0] = bytes[0] & mask;
   if(low_first) data[0] = bytes[1] << 8 | bytes[0];
   else data[0] = bytes[0] << 8 | bytes[1];


And here is a snippet of code that does NOT work - only difference is the order of operations

Code: Select all

    cmd = i2c_cmd_link_create();
    i2c_master_start(cmd);
    i2c_master_write_byte(cmd, i2c_addr << 1 | READ_BIT, ACK_CHECK_EN);
    i2c_master_read_byte(cmd, &bytes[0], ACK_VAL);
    i2c_master_read_byte(cmd, &bytes[1], NACK_VAL);

    bytes[0] = bytes[0] & mask;
   if(low_first) data[0] = bytes[1] << 8 | bytes[0];
   else data[0] = bytes[0] << 8 | bytes[1];
   
    i2c_master_stop(cmd);
    ret = i2c_master_cmd_begin(i2c_num, cmd, 1000 / portTICK_RATE_MS);
    i2c_cmd_link_delete(cmd);

WiFive
Posts: 1206
Joined: Tue Dec 01, 2015 7:35 am

Re: I2C drivers seems to be corrupting memory

Postby WiFive » Thu Nov 16, 2017 2:23 am

i2c_master_read_byte does not actually perform a read until i2c_master_cmd_begin is called

rwel59
Posts: 27
Joined: Thu Oct 12, 2017 3:32 pm

Re: I2C drivers seems to be corrupting memory

Postby rwel59 » Thu Nov 16, 2017 12:51 pm

thanks. I see now. Pulling my hair out over this one.

Who is online

Users browsing this forum: martinB and 11 guests