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.
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:
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
Hello @avinashstarkenn,
Have you tried the suggestions provided? I also don't see any code attached to your latest answer.
Best regards,
Cristina
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.
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.
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.
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