Read Accelerometer and Gyrometer Data from an STMicroelectronics STM32F401RE Board Using USART Blocks
This example shows how to use Embedded Coder® Support Package for STMicroelectronics® STM32 Processors to send data to your computer from a STMicroelectronics® STM32F401RE board and read it using serial (USART) blocks.
This example has three models.
In the first model,
USARTCommunication
, the USART Write block sends serial data to your computer. The USART Read block reads the received data and displays it in display block(Serial Write status).
In the second model,
AccelGyroUSART
, the USART Write block sends serial data to theUSART1_TX
serial transmit port on the STM32F401RE board. This model is deployed on the STM32F401RE board.
In the third model,
AccelGyroUSARTHost
, the Serial Read block from the Instrument Control Toolbox™ reads the data that is transmitted in the second model.
These models are pre-configured for an STMicroelectronics STM32F401RE board. To configure these models for other supported STM32F401RE boards, browse to Simulation > Model Configuration Parameters > Hardware Implementation on the Simulink model toolbar. From the Hardware board list, select any other STM32Fxx based board, and click OK.
Prerequisites
Before you start with this example, we recommend completing the Read Data from I2C-Based Sensors Using STMicroelectronics Nucleo Boards example.
Required Hardware
To run the models, you must have the following hardware:
Supported STMicroelectronics STM32F401RE processor board
USB cable
SparkFun 6 Degrees of Freedom IMU Digital Combo Board - ITG3200/ADXL345
Small breadboard (recommended) and breadboard wires
STMicroelectronics STM32F401RE USART Communication Model
In this model, the USART Write block sends serial data to your computer. The USART Read block reads the received data and displays it in Monitor and Tune.
open_system('USARTCommunication');
Connecting USART1 Pin PA9 to Pin PA10
Connect the USART1 PA9
(serial Tx) pin to the USART1 PA10
(serial Rx) pin using a breadboard wire. The model writes the serial data to the PA9
pin. This data is looped back to the PA10
pin on the STM32F401RE board.
You cannot use the USART2 PA2
and USART2 PA3
serial pins to send and receive data in Monitor and Tune. Monitor and Tune uses these pins when running the code on the model. Avoid changing the configuration of the USART2 module and do not use it for loopback.
Configuring USART1 Communications
In the STM32 CubeMX software, you can set the baud rate, word length, and parity for USART communications.
Configuring USART Write and USART Read Blocks
This task explains how to configure the model to write and read serial data using the USART Write and Read blocks.
1. Open the USARTCommunication
model.
2. In your Simulink model, double-click the USART Write block, and do these steps:
a. Specify the USART module parameter as 2
.
b. Select the Output status option. When you select this option, the Status port becomes available.
This table lists the value at the Status port for different data transfer status.
Output status(in decimal) | Message _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 0 | Successful USART operation 1 | Receiver(Rx) busy 2 | Transmitter(Tx) busy 4 | USART Parity error 8 | USART Frame error 16 | USART Bus error 32 | USART data not available _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
3. Double-click the USART Read block, and perform these steps:
a. Specify the USART module parameter as 2
.
b. Select the Output status option. When you select this option, the Status port becomes available.The value at the Status port for different data transfer status is same as listed in the previous table.
Configuring the Model for USART
1. On the Simulink model toolbar, Open the Modeling tab and press CTRL+E to open Configuration Parameters dialog box.
2. Go to Hardware Implementation pane, select the STMicroelectronics STM32F401RE board that you are using from the Hardware board list. Do not change any other settings.
3. Click OK to save your changes.
Configure the Hardware and Model for Monitoring and Tuning
In this task, you will configure the hardware and tune parameters and monitor the serial data in the model while running the application on the STM32F401RE board.
1. Open the Modeling tab and press Ctrl+E to open Configuration Parameters dialog box.
2. Navigate to Hardware Implementation > Target Hardware Resources > External Mode > COM Port and enter the COM port of the serial interface on Windows. To find the COM port on your Windows system, open the Device Manager window, and expand the Ports (COM & LPT) list. In the expanded list, locate STMicroelectronics STLink Virtual COM Port (COMx), where x is the number of the port.
3. Open the Hardware tab and click Monitor & Tune.
a. The Display block, Serial Write Status, displays the status as 0
, indicating that the serial data write is successful.
b. The Display block, Serial Read Status, displays the status as 0
, indicating that the serial data read is successful.
c. The Display block, Received data displays the data received by the USART Read block. This value matches the data sent using the USART Write block.
4. On the Hardware tab, click the Stop button to end the Monitor and Tune execution.
STMicroelectronics STM32F401RE AccelGyro USART Model
In this model, the USART Write block sends the Sparkfun Digital 6DOF accelerometer and gyrometer sensor data along the X, Y, and Z axes to USBTX, the default serial transmit port on the STM32F401RE board. This model is deployed on the STM32F401RE board.
open_system('AccelGyroUSART');
Configuring and Running the Model to Write Sensor Data
This task explains how to configure the model to write sensor data using the USART Write block.
1. Connect the ITG3200/ADXL345 sensor to the STMicroelectronics STM32F401RE board as described in the Read Data from I2C-Based Sensors Using STMicroelectronics Nucleo Boards example.
2. Open the AccelGyroUSART
model.
3. On the Simulink toolbar, Open the Modeling tab and press CTRL+E to open Configuration Parameters dialog box.
4. In the Hardware Implementation pane, select the STMicroelectronics STM32F401RE board that you are using from the Hardware board list. Do not change any other settings.
5. Click OK
6. In your Simulink model, double-click the USART Write block, and perform these steps.
a. Specify the USART module parameter as 2
.
b. Select the Output status option. When you select this option, the Status port becomes available.
This table lists the value at the Status port for different data transfer status.
Output status(in decimal) | Message _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 0 | Successful USART operation 1 | Receiver(Rx) busy 2 | Transmitter(Tx) busy 4 | USART Parity error 8 | USART Frame error 16 | USART Bus error 32 | USART data not available _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
7. To view the I2C configuration, use the STM32 CubeMX software to open the configuration file readAccelerometerAndGyrometerData.ioc
. The I2C configuration is as shown. For more information, on how to configure I2C blocks in this model, see the Read Data from I2C-Based Sensors Using STMicroelectronics Nucleo Boards example.
8. On the Simulink model toolbar, click the Build, Deploy, & Start button. This action builds, downloads, and runs the model on the STM32F401RE board.
Reading the Values Using Serial Terminal
You can read the values from the board using any serial terminal or by performing the steps specified in the third model. The advantage of using the serial terminal is that you can read the values without any need to install the Instrument Control Toolbox on your computer.
To read values using the serial terminal:
1. Open the serial terminal available in your computer. In this example, PuTTY is used.
2. In PuTTY, select Connection > Serial. Specify the COM port number of the serial interface on your Windows® system, and the baud rate of data transfer.
To find the COM port on your Windows system, open the Device Manager window, and expand the Ports (COM & LPT) list. In the expanded list, locate STMicroelectronics STLink Virtual COM Port (COMx), where x
is the number of the port.
3. Click Open to see the serial data in the terminal.
4. Close the terminal to free the COM port after analyzing the data.
STMicroelectronics STM32F401RE AccelGyro USART Host Model
In this model, the Serial Read block from the Instrument Control Toolbox reads the data that is transmitted in the second model. To use this model, you must have an Instrument Control Toolbox license.
open_system('AccelGyroUSARTHost');
Configuring and Running the Model to Read Serial Data from the Sensor
Before opening the model, ensure that the Instrument Control Toolbox™ is installed in your computer. To check if the toolbox is available in your computer, use the ver
command in the MATLAB® command window.
1. Open the AccelGyroUSARTHost
model.
2. Double-click the Serial Configuration block and set the Communication port parameter to the COM port number of the serial interface on your Windows system.
To find the COM port on your Windows system, open the Device Manager window, and expand the Ports (COM & LPT) list. In the expanded list, locate STMicroelectronics STLink Virtual COM Port (COMx), where x
is the number of the port.
3. Double-click the Serial Receive block and set the Communication port and the Data size parameters.
4. On the Simulink model toolbar, set the Simulation mode to Normal
, and click the Run button.
5. After the model is successfully built, double-click the Scope block Accel and Gyro data. Observe the accelerometer and gyrometer data for each axis separately.
Other Things to Try
Build and deploy the
AccelGyroUSART
model with External mode on different pins using the USART Write block.
Perform the steps in this example to communicate with other UART-based sensors.
Close Models
close_system('USARTCommunication', 0); close_system('AccelGyroUSART', 0); close_system('AccelGyroUSARTHost', 0);