USB emulated U disk

This tutorial demonstrates: using USB Full Speed Device interface to simulate USB flash drive to store files (OLED display W25Q64, STM32 USB connection information).

1、software to hardware

2、Brief principle

2.1、Hardware schematic diagram

image-20231107165228499

2.2、Physical connection diagram

image-20231106192438380

image-20231107165305745

OLEDSTM32 board
VCCVCC
SCLSCL
SDASDA
GNDGND
STM32 USBPC
Type-C interfaceUSB interface

2.3、Principle of control

USBCharacteristics
Class of speedLow speed(USB1.0:1.5Mbps)
Full speed(USB1.1:12Mbps)
High speed(USB2.0:480Mbps)
Super high speed(USB3.0:5Gbps、USB3.1:10Gbps)
The development board is in full speed mode
Communication interfaceDevelopment board uses 16-pin Type-C interface (see schematic)
Data transferControl transmission
Interrupt transmission
Bulk transfer
Isochronous transmission
Advantages1. Support hotplugging and PNP (Plug-and-Play);
2. Provide USB peripherals with various speed levels to adapt to different requirements;
3. The port has very flexible expansibility;
4. Provides a single, easy-to-use standard connection type;
5. To meet the requirements of various types of peripheral devices, USB provides four different data transfer types.

The USB bus uses differential signal transmission, where D+ and D- represent the positive and negative data lines of the differential signal respectively.

USB1.1 and USB2.0 use a differential signal level of 3.3V and USB3.0 uses a differential signal level of 5V.

High speed/full speed equipment:D+ connected with 1.5k pull-up resistor;

Low speed equipment: D-connected 1.5k pull-up resistor;

Level typeScope
Difference 1D+>2.8V,D-<0.3V
Difference 0D+<0.3V,D-<0.3V
Single ended 0(SE0)D+<0.3V,D-<0.3V
Single ended 1(SE1)D+>2.8V,D->2.8V
J state:
Low speed
Full speed
High speed

Difference 0
Difference 1
Difference 1
K state
Low speed
Full speed
High speed

Difference 1
Difference 0
Difference 0
Restore statusK state
Packet start(SOF)The USB data bus switches from the idle state to the K state
Packet end(EOP)The SE0 lasts for two basic time units, and the J state lasts for one time unit

The data in USB is encoded by NRZI (none-return to zero inverted), and the different states of D+ and D- lines are defined as J and K.

Encoding/decoding of data (reverse non-return-to-zero code)

 

img

Packet is the basic unit of information transmission in USB system. All data is transmitted on the bus after being packaged.

Each packet consists of six parts: synchronization field (SYNC), packet identifier (PID), address field (ADDR), DATA field (including frame number), validation field (CRC), End of Packet (EOP).

USB包

The STM32F103ZET6 has an embedded device controller compatible with full speed USB, which complies with the standard for full speed USB devices (12 MBPS).

The USB-specific 48MHz clock is generated directly from the internal main PLL (the clock source must be a HSE crystal oscillator).

surce filefunction
usb_device.cUSB device initialization function
usbd_conf.cUSB protocol parameters, GPIO initialization, and other functions
usbd_storage_if.cUSB storage device related functions
usbd_desc.cUSB descriptor and USB enumeration processing

3、Engineering configuration

Project Configuration: Prompts for configuration options in the STM32CubeIDE project configuration process

3.1、Notes

Omitted project configuration: New project, chip selection, project configuration, SYS for pin configuration, RCC configuration, clock configuration, and project configuration content

The project configuration part, which is not omitted, is the key point to configure in this tutorial.

3.2、Pin configuration

You can directly select the corresponding pin number in the pin view, and the corresponding option will appear when the mouse is left clicked

image-20231107171204649

image-20231107171307656

image-20231107171537115

image-20231107171348137

image-20231107115033161

image-20231107171603197

image-20231107115208498

4、Main Function

This section mainly introduces the functional code written by users. Detailed code can be opened by yourself in the project file we provide, and enter the Bsp folder to view the source code. .

4.1、User function

Functions that were covered in the previous tutorial will not be covered

function:VCP_Status

Function prototypesvoid VCP_Status(void)
Functional DescriptionCheck the USB connection status
Input parametersNone
Return valueNone

4.2、HAL library function parsing

The HAL library functions that were covered in the previous tutorial will not be covered

function:MX_USB_DEVICE_Init

Function prototypesvoid MX_USB_DEVICE_Init(void)
Functional DescriptionInitialize the USB device
Input parametersNone
Return valueNone

function:MX_USB_DEVICE_Init

Function prototypesUSBD_StatusTypeDef USBD_Init
(USBD_HandleTypeDef *pdev, USBD_DescriptorsTypeDef *pdesc, uint8_t id)
Functional DescriptionInitialize the configuration and parameters of the USB device
Input parameters1pdev:Handle to a USB device
Input parameters2pdesc:USB device descriptor information
Input parameters3id:The identifier of the USB device
Return valueUSB status:USBD_OK、USBD_BUSY、USBD_FAIL

function:USB_LP_CAN1_RX0_IRQHandler

Function prototypesvoid USB_LP_CAN1_RX0_IRQHandler(void)
Functional DescriptionHandle USB_LP_CAN1_RX0_IRQn interrupt (USB low priority and CAN1 receive 0 interrupt)
Input parametersNone
Return valueNone

5、Experimental phenomenon

After downloading the program successfully, press the RESET button of the development board to observe the OLED display and LED display

phenomenon:

The STM32 USB interface is not connected

The first line of OLED shows: "W25Q64 find! "

OLED second line: "USB disconnect"

Connect the STM32 USB port

The first line of OLED shows: "W25Q64 find! "

OLED second line: "USB connect"

The USB flash drive recognized by the system for the first time, needs to be formatted to open it

Format U disk must carefully check the label, do not format other disks, otherwise the data in the disk will be lost

image-20231107173647027

Note the Type-C data line interface location for the following experimental phenomena

image-20231107193606875

image-20231107193529196