My aim for today’s post is to show an example of Virtual COM Port USB mode. Some time ago we worked with the USB Mass Storage mode - please don't miss this article 🙂
In previous posts devoted to the STM32CubeMx I've used the STM32F4Discovery board, so today I'll use it again 🙂 It's very suitable for our aims, because it can be connected to the PC via micro-USB connector mounted on the board. Thus, let's launch the STM32CubeMx and create a new project. This process is quite clear, because we've done it a lot of times in previous posts.
In this project we'll realize the Virtual COM Port driver for our microcontroller and try to send a small amount of data via USB. And, first of all, we should enable the USB peripheral. This can be done by setting the mode of the USB_OTG_FS module at the “Pinout” tab.
Furthermore, let’s enable the external 8 MHz crystal oscillator:
Note that STM32CubeMx marked all “busy” pins:
Finally, we should set the VCP mode to our USB device:
So, the first configuration step is now finished, let’s proceed to the “Clock Configuration” tab. We can setup all of the MCU frequencies there. The final clock settings for this project are shown in the picture:
The next is “Configuration” tab, where we can access the various USB peripheral settings. And, firstly, we should enable the VBus pin (PA9). In order to do this we should click on the USB_FS button:
Secondly, click on the USB_DEVICE button. A new configuration window will be shown, where different USB properties, such as VID and PID, can be set. In this project we have no need to change any of them, so it’s time to generate the new project! In contrast to previous posts let’s compile the project without any changes. After that let’s program the MCU and connect the board to the PC. As a result we’ll see a new device in the Device Manager:
So, the generated project works fine 🙂
As we decided at the beginning of this post, we should send an amount of data from the development board to the PC. All necessary functions for data exchange via USB Virtual COM Port are located in the file usbd_cdc_if.c:
- CDC_Receive_FS() – for receiving data
- CDC_Transmit_FS – for transmitting data
In this project we’ll send eight bytes with the timeout (1 second). For the delay implementation HAL_Delay() function can be used. Delay value in milliseconds should be passed into this function. Thus, let’s add some code in the function main():
int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MCU Configuration----------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* Configure the system clock */ SystemClock_Config(); /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_USB_DEVICE_Init(); /* USER CODE BEGIN 2 */ /* USER CODE END 2 */ /* USER CODE BEGIN 3 */ uint8_t testDataToSend[8]; for (uint8_t i = 0; i < 8; i++) { testDataToSend[i] = i; } /* Infinite loop */ while (1) { HAL_Delay(1000); CDC_Transmit_FS(testDataToSend, 8); } /* USER CODE END 3 */ }
Let’s compile the edited project and try to connect the board again. In order to receive the data we can use any serial port monitor utility:
As you see the received data equals the data in our project, it means that the data exchange was successful! So, this article has come to the end, we thank you for your attention and hope you’ll visit our site again! 🙂
Full project - STM32Cube_USB_VCP_Project.