I2C sensor on USART2 Serial communication not working

Hello,
I am using the embedded coder support package in simulink to interface with my onboard MEMS acceleration sensor (LSM303AGR) on my STM32F411VET discovery board. I could successfully connect and run simple LED program through USART2 on simulink block and it returns button state values as well on display, while running in external mode.
I am moving on to more complicated communication, where i am accessing the I2C sensor via USART2 but I am not able to establish a communication. I have I2C read and I2C write blocks inside the embedded coder package which i am using along with correct read and write addresses for the sensor.
However, My I2C sensor will not respond over USART2, is there a step which I am missing?
How does one transmit and receive I2C sensor data over USART2 serial communication?
Your help would be appreciated.

Answers (2)

Vikky Patil
Vikky Patil on 14 Sep 2023
Edited: Vikky Patil on 14 Sep 2023
Hello Sanath,
We assume you want to read the data from the sensor in Simulink using external mode. The sensor is connected via an I2C interface with the controller. The interface used for external mode is serial.
We don’t have an exact example on the same hardware, but you can refer to an example shipped in 23b demonstrating reading sensor data(which is connected via an I2C interface with the controller) and sending the results over CAN to another controller.
Note : The sensor in the example is to read humidity and temperature.
You can refer to the example for understanding how to read the data using the I2C block and instead of sending it over CAN using CAN write a block you can connect it to an output port and run the model in external mode.
if issue persist, please contact technical support: Contact Us - MATLAB & Simulink (mathworks.com)
@Vikky Patil based on two examples, one for I2C communication and another about the specific sensor, LSM303AGR, I made a simulink model, but it does not return any value. what do you think could be the issue?

8 Comments

  1. Peripehral Address should be 0x19
  2. I2C Pins in the STM32 Cube MX should be set correclty PB8 and PB9
@Vikky Patil thank you for your reponse, I followed the addresses in the datasheet where it is 0x33 for read and 0x32 for write but i have also seen 0x19 in some forums and examples, i didnt understand. Could you please explain?
Also then i wrote 0x57 on CTRL_REG1_A (0x20), which is the register address, as per datasheet for initialization in normal mode, but I2C address of write block was 0x33, is that correct?
The block asks for the peripheral address which is 7 bit address. When it's read or write we take care of setting the bit 1 or 0 respectively. So the Slave address SAD[6:0] i.e. 0x19 whether its read or write.
@Vikky Patil I understand, but for example in this I2C read, I write hex2dec('0x19') in Peripheral address and hextodec('20') as the address for CTRL_REG1_A, then where do i enter 0 or 1 for read or write, the simulink blocks dont have any more field?
I2C read will set the bit 1 and write will set it 0 internally. The user does not have to do anything explicitly. 1 and 0 represent if its a read or write operation and we have 2 different blocks to do I2C read and I2C write.
@Vikky Patil Thank you. It makes sense, I will try it today.
One last question related to this, My board has a max baud rate of around 0.2 mbps but i have read that Simulink external mode requires at least 1 mbps, could this be a problem in my case?
It depends upon the rate at which you are logging and number of data bytes getting logged. If you are seeing data drops in external mode use external FTDI and try to increase the baud rate to its max.
@Vikky Patil Thank you so much for all your help. My model is finally working with all values for x,y and z axis. Your suggestion and explainations really helped me move forward.

Sign in to comment.

Asked:

on 13 Sep 2023

Commented:

on 28 Sep 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!