How to I2C read on 344

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to I2C read on 344

1,427 Views
pb632146
Contributor V

I am trying to perform a read on a sensor I have attached to my s32k344 but I do not really understand how to get there from the example provided which has the board be both master and slave. 

pb632146_0-1727992093320.png

pb632146_1-1727992239522.png

 

This is what the datasheet says to perform a read and the default value of 999 I would like to read from a register. I have set the slave address in the peripherals tab but do not know what to put in the TX buffer beyond 0xCA/CB. Are the ACKs and NACKs written automatically by the MasterSendData function? How do I send data so that it is recognized as a read transaction and not a write?

pb632146_2-1727992823143.png

 

0 Kudos
Reply
7 Replies

1,389 Views
pb632146
Contributor V

As an extension, I mainly do not understand whether I need to be filling in the Acks/Nacks/Restart on my own. By my understanding of i2c, to read the data from the register at address 0xCA I would need to give it this buffer correct?

pb632146_0-1728127704336.png

Since the device register of 0x39 is set in the peripherals, the first 0x39 would be excluded? and then the address write is the first 0xCA, followed by the restart command (which appears to be missing), the device address, then the address of the register to be read again.

But I do not see how you write the repeated start command into this buffer after the first 0xCA, what should I do? And do I need to include the device address at the start of the buffer?

I have a very basic project spun off the i2c example for s32k344 attached, any help or direction at all would be greatly appreciated

0 Kudos
Reply

1,358 Views
PetrS
NXP TechSupport
NXP TechSupport

Hi

device/slave address is send automatically as first byte for each Send/Receive function, so it is not needed to include it in buffer. Repeated start will be used if previous transmission does not trigger stop; SendStop parameter in function is FALSE.

So to read device register/memory you need...
- send device register address to be read with SendStop=False. The txBuffer include register address, transfer size is 1 or 2 depending of address width
  Lpi2c_Ip_MasterSendDataBlocking(LPI2C_CHANNEL_0, txBuffer, TRANSFER_SIZE, FALSE, timeout);
- receive number of desired bytes from register address send above, generate STOP at the end
Lpi2c_Ip_MasterReceiveDataBlocking(LPI2C_CHANNEL_0, rxBuffer, TRANSFER_SIZE, TRUE, timeout);

Non-blocking transfer can be used as well, but you need to check end of transfer in that case.

BR, Petr

0 Kudos
Reply

1,345 Views
pb632146
Contributor V

Thank you for the response, I put that into my code and it seems to be pointing me towards a different issue somewhere, when I went to debug I saw that the function was skipping over all the "Master->" lines and not updating the BufferSize or DataBuffer, which I assume means nothing is being sent in the end. It also is stuck on I2CIdle = False so when it gets to the receive function it just skips over it because it see that its idle. I'm not sure what is happening here or what the fix would be but it seems to be the issue. 

pb632146_0-1728336630619.png

 

0 Kudos
Reply

1,319 Views
pb632146
Contributor V

For some reason when I brought the Master -> DataBuffer  and BufferSize lines out of the idle check if statement, it actually did place the values into Master. It also ran  the Direction and SendStop lines when it previously did not.

pb632146_0-1728589089856.png

It seems like all the values being set at the start of the if statement are being optimized out and I do not know why.

0 Kudos
Reply

1,316 Views
pb632146
Contributor V

also I have this error that says the number of instances is undefined and registers as 0 instead of presumably 1 given there is one instance, what issues would that be causing?

pb632146_0-1728590521119.png

 

0 Kudos
Reply

1,290 Views
pb632146
Contributor V
It appears the LPI2C_IP_NUMBER_OF_MASTER_INSTANCES was undefined because in LPI2C_Ip_CFG.h the LPI2C_IP_NUM_MASTER_INSTANCES_USED_BOARD_INITPERIPHERALS was InitPeripherals and not all uppercase.
0 Kudos
Reply

1,287 Views
pb632146
Contributor V

It is now placing the values and setting the buffer correctly but is not actually transferring the value and timing out.

pb632146_1-1728600843336.png

For some reason it is placing the value from the rxBuffer into the Master buffer during the send function? Why would this happen. Also pictured is it hitting the timeout limit. Would this be a hardware issue of the sensor not working?

pb632146_0-1728600744619.png

 

0 Kudos
Reply