AnsweredAssumed Answered

FRDM-K22F I2C Failure with sensor

Question asked by tonythelion on May 21, 2017

I have a kinetis FRDM-F22 board connected to an LIS331H ( sensor. 


I communicate to it using the I2C protocol (, and when I write to it I always have success, but when reading from it, after writing, from a different function, the code errors after the repeated start saying it got a nack flag.  However it never seems to send the SUB address, which has its own failure code, and never fails there.  But it fails much later in the code after the repeated start.


I am using the Kinetis API, however I have ripped the guts from some of the functions and put them directly in my code.


The write_register function succeeds, each and every time.

In the read_sensor function (where there is a problem):


The first 8 bits are the hex value 0x32 (b 00110010) which is the write address.  After the last 0 bit you get another zero bit (the ack).  You can see this on the Read Zoom.png logic analyzer image attached.


After this should be the register address from which to read, in this case (0x20), but it somehow fails attempting to read from this.


Looking at the logic analyzer images, you can see the only thing that succeeded is sending the slave address, (0x32), and not the sub address.  Though running the code gives you the error that the repeated start failed (nack), even though it never got that.


It is unclear to me as to why this fails?


When I do the entire write and then read sequence in one function, it succeeds.  Seperating the read register from write to register out into two functions makes the read operation fail. 


Attached are the logic analyzer outputs 

Write Read.png (write first block - read second block)

Read Zoom.png (read zoomed in - so failure can be seen)


and the code.

sensor.c  - contains read_sensor and write_register

main() function - calls init board functions and calling both functions


The first function returns 0 (success)

The second function prints "Got nak flag on repeated start"

Original Attachment has been moved to: