Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to operate Serial Port UART with Library

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

In this issue, the editor will bring you about how to use the library to operate serial port UART. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.

1. In the previous sections from GPIO to UART we talked about how MCU starts, how to flip IO pins, and how to trigger interrupts with keys. Next we introduce the most commonly used module, serial port (UART). Serial port can be said to be the oldest and most vital communication interface. The RS485 bus is even more time-tested. Although the serial port has long been removed from the standard configuration of most PC, the most common communication between the embedded system and the host PC should be through the serial port to USB. We use Keil to open the following project: the serial port of STM32Cube_FW_F0_V1.11.0ProjectsSTM32F030R8-NucleoExamplesUARTUART_TwoBoards_ComPollingMDK-ARMProject.uvprojx code configuration is 9600jiao 8 N1. After we compile and download the code, we can connect to the USB port of PC through UART to USB converter, and observe the data sent by MCU on the PC side with serial port.

2.UART initialization let's take a look at the code, serial port parameters are set in the main program, and some are completed in stm32f0xx_hal_msp.c. Why go to so much trouble instead of putting all the initialization code in one main program? We should gradually realize the benefits of doing so. When we call a driver, the driver will inevitably deal with the underlying hardware, such as serial port driver, which finally uses a serial port module selected by the user and the transceiver pin connected with this module to send and receive data. HAL (Hardware Abstract Layer) strips out the code related to the specific hardware details separately, and introduces the concept of MSP (MCU Support Package) into the Cube library, where the specific hardware details are configured by the user.

There are two functions for each hardware module in the HAL library, for example:

HAL_UART_Init () functional description: set the transceiver mode, parity bits, stop bits, and so on (independent of the chip).

HAL_UART_MspInit () hardware description: IO initialization, different chips, different pin settings are different.

Back to the program, we want to use the serial port to call the driver layer initialization function HAL_UART_Init (), this initialization function back to call HAL_UART_MspInit () this function to complete the UART clock and transceiver pin clock enable, as well as the configuration of transceiver pins. The initialization function then continues to configure the parameters of the UART port. One advantage of this is that the initialization function of the driver layer is hardware-independent. Generally speaking, after we make a board, the serial ports and pins we use will be fixed, and we can configure them once in HAL_UART_MspInit (). After that, we don't need to change the code frequently.

3. Being familiar with the initialization of Handle is different from the initialization of GPIO. The concept of Handle is introduced in the module UART. Before we look at Handle, let's familiarize ourselves with the use of structures and their pointers that are often used in drivers:

Typedef struct _ _ MY_TypeDef

{

Uint8_t Var1

Uint8_t Var2

Uint16_t Var3

Uint8_t* Var4

} MY_TypeDef

MY_TypeDef* MY_VAR

MY_VAR is a pointer of type MY_TypeDef. Let's see what happens when we point it to a different address. MY_VAR = (MY_TypeDef*) 0x20000018

Note that MY_VAR- > Var4 is a byte pointer variable, which itself takes up 4 bytes, whose value is 0x20000018, and

The value of * MY_VAR- > Var4 is 0x02.

Point the MY_VAR to another address: MY_VAR = (MY_TypeDef*) 0x2000001C

Similarly, for a serial module, the driver defines a structure type UART_HandleTypeDef, which we can use to define multiple structures and establish a connection between the structure and the serial port by paying the starting address of the register area of the serial module to a structure:

The current program we are running operates the serial port in polling mode. The content related to DMA and interrupt mode in the structure can be ignored first. You only need to pay attention to the following members in the structure:

USART_TypeDef * Instance

A pointer of type USART_TypeDef, which needs to point to the starting address of the serial port register area you want to operate. To establish a connection between the Handle and the serial port.

UART_InitTypeDef Init

Before calling the initialization function, initialization parameters such as baud rate, parity and so on need to be written into this structure.

UART_AdvFeatureInitTypeDef AdvancedInit

Serial port extension function initialization parameters. The extension feature is not currently used. The advantage of using Handle is that when we operate a module, we just pass the first address of the Handle corresponding to that module to the driver function. This function can find everything you need through Handle. Such as:

HAL_UART_Transmit (& UartHandle, (uint8_t*) aTxBuffer, TXBUFFERSIZE, 5000)

& UartHandle is the first address of the Handle corresponding to UART1. Handle not only saves the parameter information of its corresponding module, but also saves the buffer data, as well as the current working state. It can ensure that each module does not interfere with each other, is interrupted in the process of code execution, and can continue to execute correctly after recovery. This also facilitates the integration of drivers into the operating system. In the future interrupt mode and DMA operation mode, we can more deeply realize the advantages of this method. After understanding how the serial port module works, it is very easy to understand other modules.

It should be mentioned that within the M0 chip, there are some shared or system-level hardware modules that do not use Handle to handle:

GPIO

SYSTICK

NVIC

PWR

RCC

FLASH.

The above is the editor for you to share how to use the library to operate serial port UART, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report