In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to achieve USB to serial port function in STM32, the content is concise and easy to understand, absolutely can make your eyes bright, through the detailed introduction of this article, I hope you can get something.
1. The serial baud rate set on the computer does not affect the transmission rate of the USB interface.
two。 How to get the baud rate set by the serial port assistant on the computer to set the baud rate of USART1 with the serial port
The assistants are consistent.
Baud rate setting
In fact, the relevant interfaces are already provided in the programs generated by STM32CubeMX, which only need to be implemented by the users themselves. Open the usbd_cdc_if.c file and add the following program to the CDC_Control_FS function.
The above comments are generated by the system, and the programs in the corresponding red box are added according to the comments. LineCoding is a structure:
USBD_CDC_LineCodingTypeDef LineCoding = {115200, / * baud rate * / 0x00, / * stop bit-1 percent / 0x00, / * check-none*/ 0x08 / * data bit 8 percent /}
The format of USBD_CDC_LineCodingTypeDef is also defined by the system.
After the addition is completed, when the baud rate is changed on the computer's serial port assistant, the system will call this program to reconfigure the baud rate of USART1 so that the two are consistent.
The remaining function is data forwarding, that is, the data received by USB is sent through USART1, and the data received by USART1 is sent to the USB interface.
USB data is forwarded to USART1
Also in the usbd_cdc_if.c file, modify the USB receiver function CDC_Receive_FS. Shield the original program and add the program sent by the serial port.
Add serial port to send and complete interrupt callback function (provided that serial port is enabled to send interrupt):
Void HAL_UART_TxCpltCallback (UART_HandleTypeDef * huart) {USBD_CDC_ReceivePacket (& hUsbDeviceFS);}
USART1 data is forwarded to USB
This routine receives USART1 data by interrupt mode, and judges the completion of receiving a frame of data by receiving timeout. Of course, forwarding can also be achieved in other ways, and the routine is only a simple implementation of the function.
Add serial port interrupt callback function:
Void HAL_UART_RxCpltCallback (UART_HandleTypeDef * huart) {UserTxBufPtrIn++; if (UserTxBufPtrIn = = APP_RX_DATA_SIZE) / / prevent spillover {UserTxBufPtrIn = 0;} UR_Tick = 0 HAL_UART_Receive_IT / receiving timer zeroing UR_Flag = 1 HAL_UART_Receive_IT (huart, (uint8_t *) (UserTxBufferFS + UserTxBufPtrIn), 1);}
After judging that a frame of data is received, it is forwarded through the USB interface.
If (UR_Flag = = 2) {UR_Flag = 0; CDC_Transmit_FS (UserTxBufferFS, UserTxBufPtrIn); UserTxBufPtrIn = 0;} the above is how to achieve USB to serial port function in STM32. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.