STM32CubeMX Configuration for STM32F103C8Tx UART Data Manager

Introduction

Since both the Arduino controlling the lead screw and the board controlling the servo only have one UART, they need to communicate with each other, and also with the Raspberry Pi. Therefore, the number of UARTs is insufficient. Thus, I created a UART Data Manager using the STM32F103 chip, which has three UARTs: USART1~3. I assigned UART1 for communication with the Raspberry Pi, UART2 for communication with the servo driver board, and UART3 for communication with the lead screw driver board.

Previous posts on controlling the servo and lead screw are as follows:

“Arduino Controls Stepper Motor to Move Lead Screw for Horizontal Movement of Servo Frame”

“Arduino Drives ST90S Micro Servo with Servo to Complete Cargo Picking Task”

Task Analysis

STM32CubeMX Configuration for STM32F103C8Tx UART Data Manager

The allocation of UART data flow is roughly as shown in the image above. The black arrows indicate the UART data flow, which is quite understandable. The only part that is a bit difficult to understand is the blue arrows.

The blue arrows indicate that the servo driver board and the lead screw driver board communicate indirectly through the STM32F103 chip.

The STM32F103 plays a crucial role in parsing commands issued by the Raspberry Pi, accurately assigning tasks to the servo driver board and the lead screw driver board, and also parsing commands sent from the lead screw driver board and servo driver board to ensure communication between the two driver boards. This is essential for ensuring the stability of the entire UART data flow.

STM32CubeMX Configuration Project

First, select our chip (STM32F103C8Tx) in STM32CubeMX.
STM32CubeMX Configuration for STM32F103C8Tx UART Data Manager

First, we configure SYS to Serial Wire and set the reference clock to SysTick.

STM32CubeMX Configuration for STM32F103C8Tx UART Data Manager

Next, configure RCC.

STM32CubeMX Configuration for STM32F103C8Tx UART Data Manager

After completing the basic chip configuration, we start configuring the UART. Since the baud rate for both the servo driver board and the lead screw driver board is 9600, I configured the baud rates for USART2 and USART3 to 9600 as well. To maintain consistency, I also set the baud rate for communication between the STM32F103 and the Raspberry Pi to 9600, as shown in the configuration below.

STM32CubeMX Configuration for STM32F103C8Tx UART Data Manager
STM32CubeMX Configuration for STM32F103C8Tx UART Data Manager
STM32CubeMX Configuration for STM32F103C8Tx UART Data Manager

After completing the UART configuration, we can see the pin usage diagram of the chip as shown below.

STM32CubeMX Configuration for STM32F103C8Tx UART Data Manager

At this point, we have completed the chip configuration. Next, we need to generate the project. I am using the development environment Keil5, so I selected the IDE MDK-ARM V5.

STM32CubeMX Configuration for STM32F103C8Tx UART Data Manager

The two options in the image below are not checked by default, but to reduce the size of the generated Keil5 project files, I recommend checking both. The first is to include only necessary libraries, and the second is to categorize peripheral files by .c and .h.

STM32CubeMX Configuration for STM32F103C8Tx UART Data Manager

After completing the project configuration, click the file shown in the image below to generate the project.

STM32CubeMX Configuration for STM32F103C8Tx UART Data Manager

Then you can compile the generated project files. If you encounter the error shown in the image below, please refer to the article “CubeMX Configuration for STM32F103C8T6 Chip Debugging Photoelectric Sensors” (*https://www.guyuehome.com/29307)

STM32CubeMX Configuration for STM32F103C8Tx UART Data Manager

Logic Writing

First, we will rename each UART to reduce program coupling.
#define PI_SERIAL huart1#define SCREW_SERIAL huart2#define SERVO_SERIAL huart3

Next, we will perform operations in the main loop of the program.

char pi_serial[7] = {'\0'};char servo_serial[4] = {'\0'};char screw_serial[3] = {'\0'};while (1){    if(!HAL_UART_Receive(&PI_SERIAL,(uint8_t *)pi_serial,6,0XFFFF)){//Received message from Raspberry Pi        //Parse Raspberry Pi's command     }}

The general framework of the program is as shown above. The main idea is to parse and distribute commands received from the Raspberry Pi and the driver boards. Since command transmission and management are quite complex, I will share more in the future.

“Guide to Robot Path Planning Algorithms Based on Grid Maps”This course will familiarize you with several common path planning algorithms and their respective advantages and disadvantages; it will also provide you with Matlab source code to enhance your Matlab programming skills.

STM32CubeMX Configuration for STM32F103C8Tx UART Data Manager

(Scan the QR code to view course details)

Leave a Comment