USB CDC+MSC composite device based on STM32CubeMx

The previous article introduced the USB application of STM32, including virtual serial port (CDC) and mass storage device (MSC). Today, we will introduce the method of USB implementing CDC and MSC composite devices.

Hardware: STM32F407VET6

Software: STM32CubeMx v6.5+F4 library v1.27.1

Compilation environment: MDK 5.30

1. Prepare two projects

First, use STM32CubeMX to generate two projects, CDC and MSC, respectively, and the test is normal. For details, please refer to the previous article.

STM32 implements USB to serial port function (including source code)

STM32&SD card realizes USB virtual U disk (including source code)

2. Copy to a project 

Use one of the projects as a template to copy the files needed in another project. Here, the MSC project is used as a template, and the following files/folders in the CDC project are copied and added to the project.

         5fc47c2d8081ea6218d9fd903433c3aa.png

e6bf338b6514d06cd0b77f4d29d81bc8.png

The project directory after adding is as follows:

b62c9bc5d46f78d103bea99d9c9e3cc8.png

Only usbd_cdc_if.c and usbd_cdc.c and the corresponding .h files are copied. The usbd_msccdc.c file is the code of the USB composite device that needs to be implemented by itself.

3. Modify the program

a) First of all, the original project already has the framework of the USB composite device. If you want to use it, you need to add a macro definition: USE_USBD_COMPOSITE, just add it directly in the compiler:

a1da6477d3d5efa85fc1400cf1e45fd3.png

b ) Then write usbd_msccdc.c and corresponding .h files. This file is mainly the descriptor of the USB composite device, etc., which will not be introduced in detail here. See the link at the end of the article for the contents of the file.         

c ) Secondly, modify the endpoints of USB CDC and MSC, in the usbd_cdc.h and usbd_msc.h files:

CDC uses three endpoints 0x81 0x01 and 0x82

#ifndef CDC_IN_EP
#define CDC_IN_EP                                   0x81U  /* EP1 for data IN */
#endif /* CDC_IN_EP */


#ifndef CDC_OUT_EP
#define CDC_OUT_EP                                  0x01U  /* EP1 for data OUT */
#endif /* CDC_OUT_EP */


#ifndef CDC_CMD_EP
#define CDC_CMD_EP                                  0x82U  /* EP2 for CDC commands */

MSC is using the 0x83 and 0x03 endpoints

#ifndef MSC_EPIN_ADDR
#define MSC_EPIN_ADDR                0x83U


#ifndef MSC_EPOUT_ADDR
#define MSC_EPOUT_ADDR               0x03U

d) Then change the value 1 of USBD_MAX_NUM_INTERFACES in the usbd_conf.h file to 3

e) Modify the usbd_conf.c file as follows:

92ed9fa4a6c7c4ef5ad73326f4208ce0.png

f) Modify the usbd_msc.c and usbd_cdc.c files as follows:

e90c4c4413d38ee0bca2e413f59546b8.png

ee215925a0e2b7e2d456431799bb61a3.png        g)  Finally modify the usb_device.c file:

void MX_USB_DEVICE_Init(void)
{
  /* USER CODE BEGIN USB_DEVICE_Init_PreTreatment */
  /* USER CODE END USB_DEVICE_Init_PreTreatment */


  /* Init Device Library, add supported class and start the library. */
 if (USBD_Init(&hUsbDeviceFS, &usbCmpsitFS_Desc, DEVICE_FS) != USBD_OK)
  {
    Error_Handler();
  }
  /* if (USBD_RegisterClass(&hUsbDeviceFS, &USBD_MSC) != USBD_OK)
  {
    Error_Handler();
  } */


  if(USBD_RegisterClassComposite(&hUsbDeviceFS, &USBD_CDC,CLASS_TYPE_CDC,0) != USBD_OK)
  {
    Error_Handler();
  }


  if(USBD_CDC_RegisterInterface(&hUsbDeviceFS, &USBD_Interface_fops_FS) != USBD_OK)
  {
    Error_Handler();
  }
  if(USBD_RegisterClassComposite(&hUsbDeviceFS, &USBD_MSC,CLASS_TYPE_MSC,0) != USBD_OK)
  {
    Error_Handler();
  }
  if (USBD_MSC_RegisterStorage(&hUsbDeviceFS, &USBD_Storage_Interface_fops_FS) != USBD_OK)
  {
    Error_Handler();
  }
  if (USBD_Start(&hUsbDeviceFS) != USBD_OK)
  {
    Error_Handler();
  }


  /* USER CODE BEGIN USB_DEVICE_Init_PostTreatment */


  /* USER CODE END USB_DEVICE_Init_PostTreatment */
}

Finally, download the program for testing after the compilation is successful. It can be seen that a serial port and a U disk are virtualized in the USB, and the U disk here is a 32G TF card. Test the serial port to send and receive and the U disk to read and write, both are normal.

4e3331e507257f82498b19912caefae0.png

586bed6480369c1f9de385ab861adc77.png

Link: https://pan.baidu.com/s/1ZsH1C65ywMlmuznY7IrxRw?pwd=r4zr

Extract code: r4zr

Recommended reading:

SDIO+FatFS of STM32CubeMX reads and writes SD card

How fast is SDIO reading and writing to SD card?

Advanced usage of setting breakpoints during Keil debugging

How fast is the reading and writing speed of SD card in SPI mode?

[Dry goods] STM32 realizes power-down saving through ADC analog watchdog

   Welcome to pay attention to the public account "Embedded Technology Development". You can leave me a message in the background to communicate. If you think this official account is helpful to you, you are welcome to recommend and share it with others.

Guess you like

Origin blog.csdn.net/zhang062061/article/details/130164809