USB to serial communication in stm32f4USART

Publisher:平凡梦想Latest update time:2017-02-05 Source: eefocusKeywords:stm32f4  USART  USB Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Beginner learning STM32, first contact with USART serial communication.

A personal summary of the USART communication hardware principle. The TTL level from the MCU (Micro Controller Unit, also known as the ST chip) is converted to the USB level through a level conversion chip, thereby achieving communication with the PC.

USART (Universal Synchronous/Asynchronous Receiver/Transmitter) Universal synchronous/asynchronous serial receiver/transmitter.

Here we only talk about asynchronous communication. The main difference between asynchronous communication and synchronous communication is the clock, so SLCK is turned off here. USART has a total of 5 external pins: TX, RX, nRTS, nCTS, and SCLK (SW_RX in the block diagram is internal, and n means low level is valid). When learning to communicate with the PC host computer, the hardware flow control mode is turned off, so asynchronous communication only involves the two pins TX (Transmit) and RX (Receive).

The general idea is: data is transferred to the transmit data register USART_DR (TRD) by software, and TC (Transmit complete) is judged to be set to 1. If it is set to 1, it means that the transmit shift register has completed the transmission, and the data in the transmit data register is sent to the transmit shift register to start transmission. TXE (Transmit data register empty) is judged, and the next data is read to TDR; if TC is 0, wait. TC is set to 0 by the software sequence (reading the USART_SR register and then writing to the USART_DR register), or it can be achieved by writing "0" to this bit. When the hardware is reset, TC is set to 1 by hardware.

    When receiving data, the data is transferred to the receive shift register and then to the read data register. At this time, RXNE (Read data register not empty) is set to 1, and the interrupt flag RXNEIE can be generated.

Data format: 1 start bit, 8 data bits (USART_CR1, bits 12-M in control register 1 are set to 0), no check bit (USART_SR, bits 1-PE in status register are set to 0 - no parity error), 1 stop bit (USART_CR2, bits 13:12-STOP in control register 2 are assigned to 00). Because the communication is between the development board and the PC, the distance is short and the communication environment is good, so no parity check is set.

USART1 is used, PA9 is selected for TX and PA10 is selected for RX.

Code flow: 1. Initialization a. Turn on the peripheral clock (if you want to use any peripheral, you must enable the clock first), turn on the clock of GPIOA mounted on AHB1 and USART1 mounted on APB2, and call the firmware library functions RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE) and RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE)

b. Initialize the GPIO port related to the serial port, assign values ​​to the GPIO_InitTypeDef GPIO_InitStruct structure, and then map it to the register through GPIO_Init(GPIOA,&GPIO_InitStruct)

c. Configure and select the multiplexing function, call the firmware library function GPIO_PinAFConfig (form parameter, form parameter, form parameter) to determine the multiplexing function of the pin

d. Initialize the serial port, initialize the USART structure, set the baud rate, data bit length, stop bit, check bit selection, hardware flow control mode selection, USART mode control (enable receiving/transmitting), and finally complete the configuration through USART_Init(...)

e. If interrupt execution service is required, initialize NVIC (Nested Vectored Interrupt Controller). If not, skip

f. Enable the serial port and call USART_Cmd (.....) to enable USART1

    2. Write a data transmission function. Since USART_DR has only 8 bits valid, it is divided into byte transmission, double-byte transmission, string transmission, and array transmission. These four transmissions are based on the firmware library function USART_SendData (...). When sending a byte, determine whether TXE is 1. If it is 1, prepare to send the next data to the transmission data register; if it is 0, wait. The firmware library function for obtaining the interrupt status bit is ITStatus USART_GetITStatus (...). Finally, it can be determined whether TC is 1, that is, whether the transmission is completed.

    3. Write the data receiving function and call the firmware library function USART_ReceiveData (...) to obtain the latest USARTx data. Judge RXNE, 1 means the reception is complete, 0 means no data is received, so the content of the data receiving function should be written under the condition that RXNE is judged to be 1.

   4. Just call the main() function.

According to the Wildfire tutorial, reference manual, and novice understanding, I just reviewed it for my own reference. If there is anything wrong, please point it out. If it is not well written, please forgive me.

As a novice, there are still many questions I don't understand. 1. If parity check is set, should the receiver handle it? How to handle it? If this is the case for MCU to PC communication, what about MCU to MCU?

2. To send 8-bit data, you need to write a function, and to send 16-bit data, you need to write another function. When sending an array, you can only send an array of 8 bytes. Maybe I don’t know enough and I don’t understand it.


Keywords:stm32f4  USART  USB Reference address:USB to serial communication in stm32f4USART

Previous article:STM32F429 library function I2C read EEPROM
Next article:Necessary configuration when using the firmware library to operate STM32F4

Recommended ReadingLatest update time:2024-11-23 11:33

Overview of Qinheng USB MCU CH554
Overview CH554 is an enhanced E8051 core microcontroller compatible with MCS51. 79% of the instructions are single-byte single-cycle instructions. It has a built-in 24MHz clock source, built-in 1K+256B RAM, and 16KB iFlash-ROM. CH554 provides rich interface resources, including ADC analog-to-digital conversion, 3 sets
[Microcontroller]
Overview of Qinheng USB MCU CH554
How to Simplify USB Power Charging
Introduction Consumers want fewer power cords and cables for their devices and want the flexibility of charging from a computer or AC outlet, requiring that nearly all current and future handheld devices be able to charge from both a USB power source and an AC wall adapter. USB requirements present many chall
[Power Management]
How to Simplify USB Power Charging
192K SRAM issue in STM32F407
In STM32, 192K is divided into 128k and 64k, and these two RAM physical addresses are not continuous. Therefore, when the file size exceeds 128K, you need to define the storage address directly and check RAM2 in OPTION. The value is automatically defined according to the selected chip. By default, KEIL uses 128K RA
[Microcontroller]
【stm32f407】Independent watchdog iwdog
Independent watchdog The independent watchdog of STM32F4 is driven by a dedicated internal 32Khz low-speed clock (LSI), which is still effective even if the main clock fails. It should be noted that the clock of the independent watchdog is an internal RC clock, so it is not an accurate 32Khz, but a variable clock be
[Microcontroller]
【stm32f407】Independent watchdog iwdog
Toshiba Launches 2:1 Multiplexer/1:2 Demultiplexer Switches Supporting High-Speed ​​Differential Signals Such as PCIe® 5.0 and USB4®
Shanghai, China, July 11, 2024 - Toshiba Electronic Devices & Storage Corporation ("Toshiba") today announced the launch of its latest multiplexer/demultiplexer (Mux/De-Mux) switches, the "TDS4A212MX" and "TDS4B212MX" multiplexer/demultiplexer (Mux/De-Mux) switches . The new products can be used as 2-input 1
[Analog Electronics]
Toshiba Launches 2:1 Multiplexer/1:2 Demultiplexer Switches Supporting High-Speed ​​Differential Signals Such as PCIe® 5.0 and USB4®
USB HID bidirectional communication developed using stm32F10XX chip
Implement USB and PC communication on the STM32 ARM platform (IC is STM32F10XX series). This article provides a routine (tested), without understanding any USB protocol (of course, it is necessary to understand the meaning of USB-related protocols or description tables). With a little modification on this routine, you
[Microcontroller]
Necessary configuration when using the firmware library to operate STM32F4
When using the STM32F4 firmware library, the default crystal oscillator is a 25Mhz crystal oscillator, so some modifications need to be made. Because I had not paid attention to this problem before, I fiddled with it for a long time and found that the working clock was always wrong. After checking the information, I r
[Microcontroller]
Design of USB interface multi-channel high-speed data acquisition system on LABVIEW platform
introduction In daily test and measurement, data acquisition cards are often used to collect data. However, many data acquisition cards often complete data transmission through the PCI bus, which has many disadvantages, such as inconvenient operation, limited by the number of computer slots and interrupt re
[Test Measurement]
Design of USB interface multi-channel high-speed data acquisition system on LABVIEW platform
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号