TechnologySTM32: Interfacing STM32 with External Devices using USB CDC Protocol (P1) Posted on June 14, 2023June 14, 2023 by Thanh Pham 14 Jun 1. Introduction 2. Creating a New Project with STM32CubeIDE for USB CDC Communication 3. Interfacing STM32 with external devices 3.1. Modifying the code for the project 3.2. Loading the code and observing the achieved results 4. Conclusion 1. Introduction In this article, we delve into the process of interfacing an STM32 microcontroller with external devices using the USB CDC (Communication Device Class) protocol. Our focus is on configuring the STM32 as a CDC device to establish reliable communication. The objective of this comprehensive guide is to provide step-by-step instructions and insights for establishing seamless data transmission and reception between the STM32 and external devices. Whether you are a beginner or an experienced embedded systems developer, this article aims to equip you with the necessary knowledge and guidance for your USB CDC interfacing projects. Throughout the article, we highlight key concepts, present code examples, and share practical insights to help you navigate the complexities of USB CDC communication. By following the instructions and leveraging the provided resources, you can harness the full potential of your STM32 microcontroller for seamless communication with external devices. Join us on this exploration of the USB CDC world, as we unlock the power of connectivity between your STM32 microcontroller and the world of external devices. 2. Creating a New Project with STM32CubeIDE for USB CDC Communication To create a new project using STM32CubeIDE software, you can refer to our previous article. Follow the steps below to create a project that is suitable for the initial topic of STM32 communication using the USB CDC protocol. Note: In this case, we use the NUCLEO – STM32F429ZI board. Depending on the MCU or board you are using, please adapt the process accordingly. Step 1: Go to File >>> New >>> STM32 Project. Create New STM32 Project Step 2: Select Board Selector >>> Search for “NUCLEO – F429ZI” >>> Next Select Board Step 3: Name your project >>> Finish Naming the Project and Completion Step 4: Configure pin settings as shown in the following images. System Core: Config RCC Connectivity: USB_OTG_FS Middleware: USB_DEVICE Step 5: Configure the clock configuration Clock Configuration Step 6: Finally, click on “Generate Code” Code interface after code generation 3. Interfacing STM32 with external devices 3.1. Modifying the code for the project In this example, we will perform the following tasks: predefine 2 commands and compare the received values with these commands. Declare the necessary functions in the main.h file /* USER CODE BEGIN EFP */ // line 55 int USBRxHandler(uint8_t *buf, uint16_t len); void Led1_ON(); void Led1_OFF(); void Led2_ON(); void Led2_OFF(); /* USER CODE END EFP */ Declare the variables used char *data = "please send command\r\n"; uint8_t buffer[64]; Implement the function to check the received command from the device int USBRxHandler(uint8_t *buf, uint16_t len){ if(strncmp(buf, "led1on", len) == 0){ Led1_ON(); } else if(strncmp(buf, "led2on", len) == 0){ //HAL_Delay(1000); Led2_ON(); } else if(strncmp(buf, "led1off", len) == 0){ //HAL_Delay(1000); Led1_OFF(); } else if(strncmp(buf, "led2off", len) == 0){ //HAL_Delay(1000); Led2_OFF(); } return 0; } Implement the functions to turn on and off the LED void Led1_ON(){ HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET); CDC_Transmit_FS("LED1_ON, please send command\n", strlen("LED1_ON, please send command\n")); return 0; } void Led1_OFF(){ HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET); CDC_Transmit_FS("LED1_OFF, please send command\n", strlen("LED1_OFF, please send command\n")); return 0; } void Led2_ON(){ HAL_GPIO_WritePin(GPIOB, GPIO_PIN_7, GPIO_PIN_SET); CDC_Transmit_FS("LED2_ON, please send command\n", strlen("LED2_ON, please send command\n")); return 0; } void Led2_OFF(){ HAL_GPIO_WritePin(GPIOB, GPIO_PIN_7, GPIO_PIN_RESET); CDC_Transmit_FS("LED2_OFF, please send command\n", strlen("LED2_OFF, please send command\n")); return 0; } Execute the code inside the while(1) loop in the main.c file while (1) { CDC_Transmit_FS((uint8_t *)data, strlen(data)); //data transmit HAL_Delay(1000); USBRxHandler(buffer, 64); /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */ } Finally, proceed to build the code Code build finished 3.2. Loading the code and observing the achieved results Firstly, to monitor the data transmission and reception between the STM32 and the device, another tool needs to be used. In this case, I utilized the Hercules SETUP utility tool, which can be downloaded from at here Interface of the Hercules SETUP utility download webpage After completing the installation, when running the tool, we will have an interface like Interface of the Hercules tool Next, we establish a connection between the STM32 and the device (laptop). Connect laptop with STM32 Then, we open the Device Manager on the device, check the Port channels, and select the corresponding settings on the Hercules tool as shown in the image below. USB Serial Device(COM5) in Device Manager and config Serial in Hercules Next, run USB_CDC Debug Run USB_CDC Debug After running the debug, open Hercules and click on “Open” to observe the operation process: Initially, the data sent beforehand is the string “please send command”. Next, when we start sending commands from the computer through the “Send” section, the LEDs on the NUCLEO – STM32F429ZI board will function according to the predefined instructions. Hercules Interface during Data Transmission from Computer to STM32F429ZI Below are some images depicting the process of toggling LEDs LED2_ON, LED1_OFF LED1_ON, LED2_ON LED1_OFF, LED2_OFF 4. Conclusion In conclusion, this comprehensive guide provides step-by-step instructions on interfacing an STM32 microcontroller with external devices using the USB CDC protocol. By following the outlined procedures in STM32CubeIDE, configuring USB CDC communication, and implementing LED control based on received commands, developers can establish reliable and seamless data transmission. This article serves as a valuable resource for integrating USB CDC communication into STM32 projects, enabling efficient data exchange and offering endless possibilities for various applications. Explore the potential of STM32 microcontrollers and unlock seamless communication with external devices today. We wish you every success in your future endeavors and If you encounter any difficulties or have any questions, please feel free to contact us. Thanh Pham Enabling SEGGER RTT Logging by Converting ST-Link to J-Link for Nucleo-STM32F429ZI Renesas EK-RA6M4: Getting Started – Part 2