USB communication

This tutorial demonstrates: using the USB Full Speed Device interface to simulate the serial port to send and receive data (OLED shows USB connection status, LED represents different connection status).

1、software-hardware

2、Brief principle

2.1、Hardware schematic diagram

The LED interface is not shown here, you can refer to the schematic diagram by yourself

image-20231106192951002

2.2、Physical connection diagram

image-20231106192438380

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).

source filefunction
usb_device.cUSB device initialization function
usbd_conf.cUSB protocol parameters, GPIO initialization, and other functions
usbd_cdc_if.cVirtual serial port receiving and sending 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-20231107151148486

image-20231107114437253

image-20231107114544460

image-20231107115033161

image-20231107115141369

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

function:USB_Init

Function prototypesvoid USB_Init(void)
Functional DescriptionUSB pin initialization
Input parametersNone
Return valueNone
TipsPrevent the need to plug and unplug the USB port again after downloading the program

function:VCP_Status

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

function:usb_printf

Function prototypesvoid usb_printf(const char *format, ...)
Functional Descriptionprintf redefined to usb serial port (send function)
Input parametersSpecifies the content and format to output
Return valueNone

function:Deal_Recv

Function prototypesvoid Deal_Recv(void)
Functional DescriptionProcess the received data
Input parametersNone
Return valueNone
TipsThis routine prints the received data and displays the length of the data

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:LED1 off, OLED display "USB disconnect";

Connect the STM32 USB port:LED1 bright, OLED display "USB connect";

Use the serial debugging assistant to connect the virtual serial port of the USB interface. Sending data will return the same data.

If the virtual serial port shows an exception in the Device Manager:Install "STM32 USB virtual serial port Driver" in the "Project Source" folder

image-20231107150741250

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

image-20231107193120520

image-20231107193125813