I'm using MPLAB Harmony to program a PIC32. We have a accelerometer connected via I2C. Both lines are pulled up and the open drain is enabled. The Pins are set correct.
Now, I'm trying to use Harmony to read out the "who am i" register from the accelerometer. But... I can't seem to understand how this would work. Normally a I2C operation goes as follows:
- Start
- Slave address with write bit
- Sack
- Register address to read on slave
- Sack
- Repeated start
- Slave address with read bit
- Sack
- Slave data
- No ack master
- Stop
But the writeread function from Harmony does not seem te be able to push a repeated start on the correct position as their own comment above the function says the following:
Summary: Write and Read data from Slave. Description: This function writes data from the wdata to the bus and then reads data from the slave and stores the received in the rdata. The function generates a Start condition on the bus and will then send wlength number of bytes contained in wdata. The function will then insert a Repeated start condition and proceeed to read rlength number of bytes from the slave. The received bytes are stored in rdata buffer. A Stop condition is generated after the last byte has been received.
So long story short. They immidiately start reading after the repeated start condition. But that's the problem, the Slave adress with the read bit should be done in between. So... I quess my question is, how do you guys work with the I2C library from Harmony to read adresses from the chip? What am I not understanding? Using only write and then read is not possible either as they assert a stop condition after both. All I get from the chip now is 0xFF and that's not good.
Thanks.
Oscar
Solved
Solved, I read out the complete state machine code and deduced that they do in fact follow standard protocol, alltough their description says otherwise. I also retreived data after finding that it is need to provide a 7 bit address without the r/w bit. The r/w bit is set by the statemachine with a shift and or.