HC32F460 series virtual serial port problem
[Copy link]
The HGI HC32F460 series supports the USB FS protocol stack, and the CDC VCP virtual serial port is provided with a virtual serial port example in the previous HGI SDK version (hc32f46x_ddl_Rev1.2.0).
However, in the latest official SDK (version: hc32f46x_ddl_Rev1.3.1), the VCP virtual serial port example has been deleted.
So I only used the hc32f46x_ddl_Rev1.2.0 version example to run the USB virtual serial port.
Open the example program. In fact, what this example program does is to virtualize USB into a COM serial port, and then connect this COM serial port to the MCU physical serial port UART4. In the example program, the VCP_fops structure stores which physical serial port the user wants to connect the USB virtual serial port to or which receiving and transmitting interface the user wants to connect to. After power-on, the USB initialization function USBD_Init initializes this VCP_fops and runs under the USB protocol stack architecture.
Among them, the VCP_Init function is the initialization function of the physical serial port UART4 to which the virtual serial port is to be connected in this example. But I have commented out all the contents in VCP_fops except VCP_DataTx and VCP_DataRx as empty. Note that I commented out and made these functions empty functions, and directly returned, instead of commenting out the functions. For example, USBD_Init, because I want to modify this example, not to let the virtual serial port connect to UART4, but to let the MCU communicate with the host computer directly through this virtual serial port. All data that the host computer and MCU communicate through the USB virtual serial port are processed by the MCU and are not mapped to a specific UART, so they are no longer transmitted from the MCU through the physical UART. My VCP_Init is as follows: I see that this function returns directly after entering, because I do not need to initialize the UART. Then we are concerned about the two functions VCP_DataTx and VCP_DataRx. VCP_DataRx: Data received from the USB virtual serial port. After the data is received, it can notify the application through this function interface or copy it to other memory. I don't do any processing here. I just print out the data sent from the host computer through the Dump function.
VCP_DataTx: We want to send the data on the MCU to the host computer through the USB virtual serial port. In the code, APP_Rx_Buffer is the buffer where the MCU wants to send data. In the example, the address of this buffer is finally assigned to the source address of the USB DMA. The index counter APP_Rx_ptr_in is actually the end position of the data sent in this example, and this position is placed in APP_Rx_Buffer. As you can see, the function I implemented is named VCP_DataTx2USB, not VCP_DataTx, which is OK. This is just used as a virtual serial port sending interface, and it can be called by different names.
When we want to send data through the USB virtual serial port, we can directly call this function, no matter what the function name is. In fact, this function is the operation of APP_Rx_Buffer and APP_Rx_ptr_in. The USB protocol stack determines whether there is data to be sent and how much data to send by judging APP_Rx_ptr_in. The following is the usage of the VCP_DataTx2USB function to send data to the host computer through the USB virtual serial port: Note a BUG in the Huada virtual serial port protocol stack. I searched the Internet and found that the STM32 USB protocol stack also has a similar BUG. This bug is that if you use VCP_DataTx2USB or VCP_DataTx to send data, if the data length is a multiple of 64B bytes, then the USB protocol stack will only send it out after the USB RAM reaches a total of 4KB bytes, instead of sending it in real time. After an experiment, if it is not an integer multiple of 64 bytes, the data can be sent to the host computer in real time through VCP_DataTx2USB or VCP_DataTx.
|