S32k3:I2c with multiple bytes

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

S32k3:I2c with multiple bytes

1,096 Views
avinashstarkenn
Contributor III

I am using nxp s32k312-100 pins mcu.

I am trying to use i2c using Model based design toolbox.

Is there a way in simulink where i can send and receive multiple bytes between start and stop condition.

I am trying to interface TMP75 temperature sensor.

 

0 Kudos
Reply
7 Replies

863 Views
avinashstarkenn
Contributor III

Hello ,

I have developed the model and the I2c block are in initialize function subsystem of matlab,so it will run once.

To test it I am debugging the generated code in S32 Design Studio.

 

Tags (2)
0 Kudos
Reply

954 Views
CristinaB
NXP Employee
NXP Employee

 

Hello @avinashstarkenn,

 

First I would like to give you a few details about the MBDT implementation of the I2C component.

The request structure required in the I2c_SyncTransmit and I2c_AsyncTransmit functions has the following parameters:

  • SlaveAddress: Slave Device Address.
  • BitsSlaveAddressSize: This is true when the slave address is 10 bits, when false the address is on 7 bits.
  • HighSpeedMode: If this is true the data will be sent with high speed enabled (if hardware support exists).
  • ExpectNack: When this is true, NACK will be ignored during the address cycle.
  • RepeatedStart: When this is true, a repeated start (Sr) will be issued on the bus instead of a STOP at the end of the transfer.
  • BufferSize: The number of bytes for reading or writing.
  • DataDirection: Direction of the data, can be either Send or Receive.
  • DataBuffer: Buffer to store or to transmit Serial data.


The Data Direction parameter can be chosen from the drop-down menu inside the block mask. The 10 Bits Slave Address Size and High Speed Mode parameters can be selected from the checkboxes inside the block mask.

The Slave Address, Repeated Start, Expect Nack, Data Tx (for the Send direction), Buffer Size (for the Receive direction) parameters are expected as inputs to the block, while Data Rx (for the Receive direction) will be an output to the block.

If you want to send multiple bytes from the master, you can simply create a Data Store Memory block containing the bytes and give it as an input (Data Tx) for the block, using the Data Store Read block.

If you want to request multiple bytes from the slave, you must specify the number of bytes in the Buffer Size block input, create a Data Store Memory block with the same size as the number of bytes, and provide it as an output for the block using the Data Store Write block.

For more details about LPI2C and FLEXIO peripherals, you can check chapters 71 and 72 of the Reference Manual located in mbdtbx_s32k3\help\docs. You can also check the RTD I2C User Manual located in S32K3_RTD\SW32K3_S32M27x_RTD_R21-11_5.0.0\eclipse\plugins\I2c_TS_T40D34M50I0R0\doc.

 

Specifically for your sensor, in order to access a particular register, you first need to write the appropriate value to the Pointer register. For example, if you want to access the Temperature register, you have to send the slave address byte with the R/W bit low (for writing), followed by the byte 00000000. Then you need to send the device address with the R/W bit high (for reading) and store the next 2 bytes received, since the Temperature Register is a 12-bit register stored in 2 bytes.

For this you will need 2 request structures, one for sending data from the MCU to the sensor and one for receiving data from the sensor to the MCU. First you will need to use the I2c_SyncTransmit/I2c_AsyncTransmit function (with Data Direction set to Send) to send the byte 00000000 to the sensor, then use the same function (with Data Direction set to Receive) to receive the 2 bytes representing the Temperature register.

If you want the write and read operations to occur between the start and stop conditions, you can try setting the Repeated Start to true for the first request structure, but changes to the configuration project may be required for the scenario to work as expected.

 

Finally, have you tried requesting data from the sensor without first writing to the Pointer register? The datasheet states that the Pointer register is set to the Temperature register by default, so you could try reading directly from the sensor without the write operation. Note that the TMP75 remembers the Pointer register value that was sent with the last write operation. If you only want repeated reads from the Temperature register, the Pointer register bytes do not need to be sent continuously.

 

If you encounter further issues after trying these suggestions, you can send us your application or the generated code so we can better understand the implementation you are trying to achieve.

 

Best regards,

Cristina

0 Kudos
Reply

947 Views
avinashstarkenn
Contributor III

Following is the working code,

The functionality that you have mentioned in initialize state of model.

I am trying this code by debugging is S32 Design Studio.

If there is any issue ,you can make changes in that and send me back the code.

Tags (2)
0 Kudos
Reply

929 Views
CristinaB
NXP Employee
NXP Employee

 

Hello @avinashstarkenn,

 

Have you tried the suggestions provided? I also don't see any code attached to your latest answer.

 

Best regards,

Cristina

0 Kudos
Reply

835 Views
avinashstarkenn
Contributor III

Hello,

I have developed the model where I2c blocks are in initialize function of Model.

So i am debugging code in S32 design studio .

Please check and let me know

Tags (2)
0 Kudos
Reply

784 Views
CristinaB
NXP Employee
NXP Employee

 

Hello @avinashstarkenn,

 

A few observations about the S32CT configuration, I saw that you have configured I2cChannel_1 (LPI2C_0) as Master and used it in your model. Since this channel was previously configured as Slave in the default configuration project, switching it to master mode enables some additional settings that need to be configured for the channel to work as expected. As you can see in the image below, you need to add a clock source for the Lpi2c Module Clock Frequency.

CristinaB_0-1742559871309.png

 

You can configure the I2C_CLK in the Mcu Clock Reference Point tab and set it to the AIPS_SLOW_CLK source. The I2C Master Prescaler, I2c Clock High Period and I2c Clock Low Period parameters also need to be modified to get the right I2c Baud Rate.

CristinaB_1-1742560079656.png

 

I also noticed that you have a few errors in your S32CT project that need to be resolved for the model to work as expected.

Regarding the application you sent, I saw that you are trying to send and receive in the initialize function. To perform multiple transmissions, you need to make sure that the last transmission is finished before starting a new one. You can check if the master has finished sending data using the I2c_GetStatus function, which gets the status of an I2C channel and checks for errors. In this image you can see the different types of status that the function can return.

CristinaB_2-1742560302175.png

You could try sending data from the master in the initialization function, and in the model step function you can check the status of the master channel. If the last transmission is finished, you can request data from the slave.

Also, as I mentioned in the previous answer, you can try requesting data from the sensor without writing to the Pointer register first. The datasheet states that the Pointer register is set to the Temperature register by default, so you can try reading directly from the sensor without the write operation.

Another observation is that the Temperature register is a 12-bit register stored in 2 bytes, so the Buffer Size input for the I2c_AsyncTransmit Receive block should be 2 and the master_i2c_recv variable should also be initialized with a size of 2.


Hope this helps,
Cristina

0 Kudos
Reply

1,056 Views
avinashstarkenn
Contributor III

Hello,

while interfacing i2c with tmp75,following waveform is expected 

0763.pic4.png

But I am getting following waveform

 
 

pastedimage1740393001273v1.png

pastedimage1740393025906v2.png

As you can see following I am not getting data bytes ,as master(S32k3)is not sending clock or it stop working 

 

 

Tags (2)
0 Kudos
Reply