The introduction of serial communication protocol and RS-232 and the working principle of USB/TTL to 232 module

1, Serial communication protocol

1. Introduction

  • Serial communication is a very common serial communication mode between devices. Because it is simple and convenient, most electronic devices support this communication mode. Electronic engineers often use this communication mode to output debugging information when debugging equipment.
  • In computer science, most complex problems can be simplified by layering. For example, the chip is divided into core layer and on-chip peripherals; STM32
  • The standard library is the software layer between registers and user code. We also understand the communication protocol in a hierarchical way. The most basic thing is to divide it into physical layer and protocol layer.
nameComposition function
physical layerIt has the characteristics of mechanical and electronic functional parts to ensure the transmission of original data in physical media
protocol layerSpecify the communication logic and unify the data packaging and unpacking standards of both sending and receiving parties.
  • There are many standards and variants in the physical layer of serial communication. The following mainly explains the RS-232 standard

2.RS-232 standard

  • RS-232 standard mainly specifies the purpose of signal, communication interface and signal level standard

Common communication structures between RS-232 standard serial port devices:

It can be seen from the figure that the two communication devices are connected through DB9 interface and use RS-232 standard for signal transmission.

  • Then why do you need a level conversion chip?

The reason is very simple, because the controller cannot directly recognize the signal of RS-232 level standard, so the signal can only be converted into the level signal of "TTL standard" recognized by the controller through "level conversion chip". Next, what is the difference between the level of "RS232 standard" and "TTL standard"

(1) Difference between RS232 level and TTL level

  • According to different level standards used in communication, serial communication can be divided into TTL standard and RS-232 standard
Standard nameLogic 1Logic 0
TLL2.4V~5V0~0.5V
RS-232-15V~3V+3V~+15V

It is not difficult to see from the table that the logic voltages divided by the two standards are different. TTL level standard is often used in electronic circuits. Ideally, 5V is used to represent binary logic 1 and 0V is used to represent logic 0; In order to increase the long-distance transmission and anti-interference ability of serial communication, it uses - 15V to represent logic 1 and + 15V to represent logic 0

  • The following figure shows the comparison when RS232 and TTL level calibration are used to represent the same signal

(2) RS-232 signal line

  • RS-232 serial port standard is commonly used for communication between computer, routing and modulation (MODEN, commonly known as "cat"). In this communication system, the equipment is divided into Data Terminal Equipment DTE (computer, routing) and Data Communication Equipment DCE (modem)
  • Desktop computers generally have RS-232 standard COM port (also known as DB9 interface), in which the pin type outgoing signal line is called the male head, and the hole type outgoing signal line is called the female head. The male connector is generally led out in the computer, while the female connector is generally led out in the modem equipment. It can be connected with the computer by using the serial port cable in the figure above. During communication, the signal transmitted in the serial port line is modulated using the RS-232 standard explained above.
  • The following describes the names and basic functions of each interface
nameSymbolData directionexplain
Carrier detectionDCDDTE→DCEIt is used for DTE to inform the other party whether the machine has received the carrier signal of the other party
receive data RXDDTE←DCEData receiving signal, i.e. input
send dataTXDDTE→DCEData transmission signal, i.e. output
Data terminalDTRDTE→DCEUsed by DTE to inform the other party whether the machine is ready
SignallyGNDThe ground potential between the two communication devices may be different, which will affect the level signal of the transceiver. Therefore, the ground connection must be used between the two serial port devices, that is, common ground
Data equipmentDSRDTE←DCEIt is used by DCE to inform the other party whether the machine is in standby state
Request sendRTSDTE→DCEDTE requests DCE to send data to DCE
Allow sendingCTSDTE←DCEDCE responds to the RTS sending request of the other party and tells the other party whether it can send data
Ring indicate RIDTE←DCEIndicates that the DCE terminal and the line have been connected

Note: the TXD and RXD between the two devices should be cross connected

2, USB to serial port

1. Basic working principle

USB to serial port is to realize the conversion from computer USB interface to physical serial port. You can add serial ports for computers or other USB hosts without serial ports. Using USB to serial port devices is equivalent to turning the traditional serial port devices into plug and play USB devices.

After the USB host detects that the USB to serial port device is inserted, it will first reset the device, and then start the USB enumeration process. During USB enumeration, device descriptors, configuration descriptors, interface descriptors, etc. will be obtained. The descriptor will contain the manufacturer ID, device ID and Class class of the USB device. The operating system will match the corresponding USB device driver for the device according to this information.

The realization of USB virtual serial port depends on the USB to serial port driver in the system, which is generally directly provided by the manufacturer. The CDC serial port driver of the operating system can also be used. The driver is mainly divided into two functions. One is to register the USB device driver to complete the control and data communication of the USB device. The other is to register the serial port driver to provide the corresponding implementation method for the serial port application layer.

List of drive data flows corresponding to serial port transceiver:

Occurrence or receptionData flow direction
Serial port transmissionSerial port application sends data → USB serial port driver obtains data → driver sends data to USB serial port device through USB channel → USB serial port device receives data and sends it through serial port
Serial port receivingThe USB serial port device receives the serial port data → packages the serial port data through USB and uploads it to the USB host → the USB serial port driver obtains the serial port data uploaded through USB → the driver saves the data in the serial port buffer and provides it to the serial port application for reading

2.CH340 module introduction

  • Physical map
  • Internal structure diagram

    Note: RXD receiving terminal is connected to external TX transmitting terminal, and TXD transmitting terminal is connected to external RX receiving terminal

3, Instance

1. Example description

Complete a USART serial port communication program of STM32 (query mode is OK, interrupt mode is not required temporarily). Requirements:

  • Set the baud rate to 115200, 1 stop bit and no check bit;

  • STM32 system continuously sends "hello windows!" to the upper computer (win10). Win10 uses the "serial port assistant" tool to receive.

2. Example process

(1) Create project

  • Open KEIL5 to create a new project, right-click source and add a new file
  • Because it is an assembly language, you choose to create Asm files
  • Creation complete

(2) Code part

code:

;RCC Register address mapping             
RCC_BASE            EQU    0x40021000 
RCC_CR              EQU    (RCC_BASE + 0x00) 
RCC_CFGR            EQU    (RCC_BASE + 0x04) 
RCC_CIR             EQU    (RCC_BASE + 0x08) 
RCC_APB2RSTR        EQU    (RCC_BASE + 0x0C) 
RCC_APB1RSTR        EQU    (RCC_BASE + 0x10) 
RCC_AHBENR          EQU    (RCC_BASE + 0x14) 
RCC_APB2ENR         EQU    (RCC_BASE + 0x18) 
RCC_APB1ENR         EQU    (RCC_BASE + 0x1C) 
RCC_BDCR            EQU    (RCC_BASE + 0x20) 
RCC_CSR             EQU    (RCC_BASE + 0x24) 
                              
;AFIO Register address mapping            
AFIO_BASE           EQU    0x40010000 
AFIO_EVCR           EQU    (AFIO_BASE + 0x00) 
AFIO_MAPR           EQU    (AFIO_BASE + 0x04) 
AFIO_EXTICR1        EQU    (AFIO_BASE + 0x08) 
AFIO_EXTICR2        EQU    (AFIO_BASE + 0x0C) 
AFIO_EXTICR3        EQU    (AFIO_BASE + 0x10) 
AFIO_EXTICR4        EQU    (AFIO_BASE + 0x14) 
                                                           
;GPIOA Register address mapping              
GPIOA_BASE          EQU    0x40010800 
GPIOA_CRL           EQU    (GPIOA_BASE + 0x00) 
GPIOA_CRH           EQU    (GPIOA_BASE + 0x04) 
GPIOA_IDR           EQU    (GPIOA_BASE + 0x08) 
GPIOA_ODR           EQU    (GPIOA_BASE + 0x0C) 
GPIOA_BSRR          EQU    (GPIOA_BASE + 0x10) 
GPIOA_BRR           EQU    (GPIOA_BASE + 0x14) 
GPIOA_LCKR          EQU    (GPIOA_BASE + 0x18) 
                                                       
;GPIO C Mouth control                   
GPIOC_BASE          EQU    0x40011000 
GPIOC_CRL           EQU    (GPIOC_BASE + 0x00) 
GPIOC_CRH           EQU    (GPIOC_BASE + 0x04) 
GPIOC_IDR           EQU    (GPIOC_BASE + 0x08) 
GPIOC_ODR           EQU    (GPIOC_BASE + 0x0C) 
GPIOC_BSRR          EQU    (GPIOC_BASE + 0x10) 
GPIOC_BRR           EQU    (GPIOC_BASE + 0x14) 
GPIOC_LCKR          EQU    (GPIOC_BASE + 0x18) 
                                                           
;Serial port 1 control                       
USART1_BASE         EQU    0x40013800 
USART1_SR           EQU    (USART1_BASE + 0x00) 
USART1_DR           EQU    (USART1_BASE + 0x04) 
USART1_BRR          EQU    (USART1_BASE + 0x08) 
USART1_CR1          EQU    (USART1_BASE + 0x0c) 
USART1_CR2          EQU    (USART1_BASE + 0x10) 
USART1_CR3          EQU    (USART1_BASE + 0x14) 
USART1_GTPR         EQU    (USART1_BASE + 0x18) 
                            
;NVIC Register address                
NVIC_BASE           EQU    0xE000E000 
NVIC_SETEN          EQU    (NVIC_BASE + 0x0010)     
;SETENA Starting address of register array 
NVIC_IRQPRI         EQU    (NVIC_BASE + 0x0400)     
;Start address of interrupt priority register array 
NVIC_VECTTBL        EQU    (NVIC_BASE + 0x0D08)     
;Address of vector table offset register     
NVIC_AIRCR          EQU    (NVIC_BASE + 0x0D0C)     
;Address of application interrupt and reset control register                                                
SETENA0             EQU    0xE000E100 
SETENA1             EQU    0xE000E104 
                            
                              
;SysTick Register address            
SysTick_BASE        EQU    0xE000E010 
SYSTICKCSR          EQU    (SysTick_BASE + 0x00) 
SYSTICKRVR          EQU    (SysTick_BASE + 0x04) 
                              
;FLASH Buffer register address image     
FLASH_ACR           EQU    0x40022000 
                             
;SCB_BASE           EQU    (SCS_BASE + 0x0D00) 
                             
MSP_TOP             EQU    0x20005000               
;Starting value of main stack                
PSP_TOP             EQU    0x20004E00               
;Process stack start value             
                            
BitAlias_BASE       EQU    0x22000000               
;Bit alias area start address         
Flag1               EQU    0x20000200 
b_flas              EQU    (BitAlias_BASE + (0x200*32) + (0*4))               
;Bit address 
b_05s               EQU    (BitAlias_BASE + (0x200*32) + (1*4))               
;Bit address 
DlyI                EQU    0x20000204 
DlyJ                EQU    0x20000208 
DlyK                EQU    0x2000020C 
SysTim              EQU    0x20000210 
 
 
;Constant definition 
Bit0                EQU    0x00000001 
Bit1                EQU    0x00000002 
Bit2                EQU    0x00000004 
Bit3                EQU    0x00000008 
Bit4                EQU    0x00000010 
Bit5                EQU    0x00000020 
Bit6                EQU    0x00000040 
Bit7                EQU    0x00000080 
Bit8                EQU    0x00000100 
Bit9                EQU    0x00000200 
Bit10               EQU    0x00000400 
Bit11               EQU    0x00000800 
Bit12               EQU    0x00001000 
Bit13               EQU    0x00002000 
Bit14               EQU    0x00004000 
Bit15               EQU    0x00008000 
Bit16               EQU    0x00010000 
Bit17               EQU    0x00020000 
Bit18               EQU    0x00040000 
Bit19               EQU    0x00080000 
Bit20               EQU    0x00100000 
Bit21               EQU    0x00200000 
Bit22               EQU    0x00400000 
Bit23               EQU    0x00800000 
Bit24               EQU    0x01000000 
Bit25               EQU    0x02000000 
Bit26               EQU    0x04000000 
Bit27               EQU    0x08000000 
Bit28               EQU    0x10000000 
Bit29               EQU    0x20000000 
Bit30               EQU    0x40000000 
Bit31               EQU    0x80000000 
 
 
;Vector table 
    AREA RESET, DATA, READONLY 
    DCD    MSP_TOP            ;Initialize main stack 
    DCD    Start              ;Reset vector 
    DCD    NMI_Handler        ;NMI Handler 
    DCD    HardFault_Handler  ;Hard Fault Handler 
    DCD    0                   
    DCD    0 
    DCD    0 
    DCD    0 
    DCD    0 
    DCD    0 
    DCD    0 
    DCD    0 
    DCD    0 
    DCD    0 
    DCD    0 
    DCD    SysTick_Handler    ;SysTick Handler 
    SPACE  20                 ;Reserved space 20 bytes 
 
 
 
 
 
 
 
 
                 
;Code snippet 
    AREA |.text|, CODE, READONLY 
    ;Main program start 
    ENTRY                            
    ;Instructs the program to execute from here 
Start 
    ;Clock system settings 
    ldr    r0, =RCC_CR 
    ldr    r1, [r0] 
    orr    r1, #Bit16 
    str    r1, [r0] 
    ;Enable external crystal oscillator  
    ;Start external 8 M Crystal oscillator 
                                            
ClkOk           
    ldr    r1, [r0] 
    ands   r1, #Bit17 
    beq    ClkOk 
    ;Wait for the external crystal oscillator to be ready 
    ldr    r1,[r0] 
    orr    r1,#Bit17 
    str    r1,[r0] 
    ;FLASH Buffer 
    ldr    r0, =FLASH_ACR 
    mov    r1, #0x00000032 
    str    r1, [r0] 
            
    ;set up PLL The PLL magnification is 7,HSE Input no frequency division 
    ldr    r0, =RCC_CFGR 
    ldr    r1, [r0] 
    orr    r1, #(Bit18 :OR: Bit19 :OR: Bit20 :OR: Bit16 :OR: Bit14) 
    orr    r1, #Bit10 
    str    r1, [r0] 
    ;start-up PLL Phase locked loop 
    ldr    r0, =RCC_CR 
    ldr    r1, [r0] 
    orr    r1, #Bit24 
    str    r1, [r0] 
PllOk 
    ldr    r1, [r0] 
    ands   r1, #Bit25 
    beq    PllOk 
    ;choice PLL Clock as system clock 
    ldr    r0, =RCC_CFGR 
    ldr    r1, [r0] 
    orr    r1, #(Bit18 :OR: Bit19 :OR: Bit20 :OR: Bit16 :OR: Bit14) 
    orr    r1, #Bit10 
    orr    r1, #Bit1 
    str    r1, [r0] 
    ;other RCC Related settings 
    ldr    r0, =RCC_APB2ENR 
    mov    r1, #(Bit14 :OR: Bit4 :OR: Bit2) 
    str    r1, [r0]      
 
 
    ;IO port settings 
    ldr    r0, =GPIOC_CRL 
    ldr    r1, [r0] 
    orr    r1, #(Bit28 :OR: Bit29)          
    ;PC.7 Output mode,Maximum speed 50 MHz  
    and    r1, #(~Bit30 & ~Bit31)   
    ;PC.7 Universal push-pull output mode 
    str    r1, [r0] 
            
    ;PA9 Serial port 0 transmitting pin 
    ldr    r0, =GPIOA_CRH 
    ldr    r1, [r0] 
    orr    r1, #(Bit4 :OR: Bit5)          
    ;PA.9 Output mode,Maximum speed 50 MHz  
    orr    r1, #Bit7 
    and    r1, #~Bit6 
    ;10: Multiplexing function push-pull output mode 
    str    r1, [r0]    
 
 
    ldr    r0, =USART1_BRR   
    mov    r1, #0x271 
    str    r1, [r0] 
    ;Configure baud rate-> 115200 
                   
    ldr    r0, =USART1_CR1   
    mov    r1, #0x200c 
    str    r1, [r0] 
    ;USART Module total enable send and receive enable 
    ;71 02 00 00   2c 20 00 00 
             
    ;AFIO Parameter setting             
    ;Systick Parameter setting 
    ldr    r0, =SYSTICKRVR           
    ;Systick Initial installation value 
    mov    r1, #9000 
    str    r1, [r0] 
    ldr    r0, =SYSTICKCSR           
    ;set up,start-up Systick 
    mov    r1, #0x03 
    str    r1, [r0] 
            
    ;NVIC                     
    ;ldr   r0, =SETENA0 
    ;mov   r1, 0x00800000 
    ;str   r1, [r0] 
    ;ldr   r0, =SETENA1 
    ;mov   r1, #0x00000100 
    ;str   r1, [r0] 
              
    ;Switch to user level line program mode 
    ldr    r0, =PSP_TOP                   
    ;Initialize thread stack 
    msr    psp, r0 
    mov    r0, #3 
    msr    control, r0 
              
    ;initialization SRAM register 
    mov    r1, #0 
    ldr    r0, =Flag1 
    str    r1, [r0] 
    ldr    r0, =DlyI 
    str    r1, [r0] 
    ldr    r0, =DlyJ 
    str    r1, [r0] 
    ldr    r0, =DlyK 
    str    r1, [r0] 
    ldr    r0, =SysTim 
    str    r1, [r0] 
               
;Main cycle            
main            
    ldr    r0, =Flag1 
    ldr    r1, [r0] 
    tst    r1, #Bit1                 
    ;SysTick Generate 0.5s,Set bit 1 
    beq    main                  ;0.5s The flag is not set yet       
     
    ;0.5s The flag has been set 
    ldr    r0, =b_05s                
    ;Bit band operation reset 0.5s sign 
    mov    r1, #0 
    str    r1, [r0] 
    bl     LedFlas 
 
 
    mov    r0, #'H' 
    bl     send_a_char
	
	mov    r0, #'e' 
    bl     send_a_char
	
	mov    r0, #'l' 
    bl     send_a_char
	
	mov    r0, #'l' 
    bl     send_a_char
	
	mov    r0, #'o' 
    bl     send_a_char
	
	mov    r0, #' ' 
    bl     send_a_char
	
	mov    r0, #'w' 
    bl     send_a_char
	
	mov    r0, #'o' 
    bl     send_a_char
	
	mov    r0, #'r' 
    bl     send_a_char
	
	mov    r0, #'l' 
    bl     send_a_char
	
	mov    r0, #'d' 
    bl     send_a_char
	
	mov    r0, #'\n' 
    bl     send_a_char
	
	b      main
            
              
            
;Subroutine serial port 1 sends a character 
send_a_char 
    push   {r0 - r3} 
    ldr    r2, =USART1_DR   
    str    r0, [r2] 
b1 
    ldr    r2, =USART1_SR  
    ldr    r2, [r2] 
    tst    r2, #0x40 
    beq    b1 
    ;Send complete(Transmission complete)wait for 
    pop    {r0 - r3} 
    bx     lr 
 
 
                 
;subroutine led twinkle 
LedFlas      
    push   {r0 - r3} 
    ldr    r0, =Flag1 
    ldr    r1, [r0] 
    tst    r1, #Bit0 
    ;bit0 Flashing flag bit 
    beq    ONLED        ;Open for 0 led lamp 
    ;Off for 1 led lamp 
    ldr    r0, =b_flas 
    mov    r1, #0 
    str    r1, [r0] 
    ;The flashing flag position is 0,The next status is on 
    ;PC.7 Output 0 
    ldr    r0, =GPIOC_BRR 
    ldr    r1, [r0] 
    orr    r1, #Bit7 
    str    r1, [r0] 
    b      LedEx 
ONLED       
    ;Open for 0 led lamp 
    ldr    r0, =b_flas 
    mov    r1, #1 
    str    r1, [r0] 
    ;The flashing flag position is 1,The next status is off 
    ;PC.7 Output 1 
    ldr    r0, =GPIOC_BSRR 
    ldr    r1, [r0] 
    orr    r1, #Bit7 
    str    r1, [r0] 
LedEx        
    pop    {r0 - r3} 
    bx     lr 
                                
;Abnormal program 
NMI_Handler 
    bx     lr 
 
 
HardFault_Handler 
    bx     lr 
              
SysTick_Handler 
    ldr    r0, =SysTim 
    ldr    r1, [r0] 
    add    r1, #1 
    str    r1, [r0] 
    cmp    r1, #500 
    bcc    TickExit 
    mov    r1, #0 
    str    r1, [r0] 
    ldr    r0, =b_05s  
    ;The clock tick counter is set to 0 when it is greater than or equal to 500 times of clearing.5s Flag bit 
    ;Bit band operation set 1 
    mov    r1, #1 
    str    r1, [r0] 
TickExit    
    bx     lr 
                                                                           
    ALIGN            
    ;By using zero or null instructions NOP fill,Aligns the current position with a specified boundary 
    END
  • There was no error compiling

(3) Burning program

  • Download succeeded

(4) Serial port debugging

  • Next, open the upper computer and serial communication assistant. I use SSCOM. Click to open the communication port in the upper left corner and select CH340
  • Click to open the serial port

3. Result display

Display received Hello world, successful!

4, Summary

This blog introduces the serial port protocol of STM32F103 single chip microcomputer, introduces the RS-232 standard, explains the reason why RSS-232 is converted to TLL and its principle, and compares the two levels. This paper expounds the basic principle of USB transfer serial port, takes CH340 module as an example, inputs Hello World to the host computer through the serial port, and applies the theoretical learning to practice, hoping to help readers. At the same time, it also hopes that readers can try to experiment by themselves. Only through the experiment can they really know and master knowledge. If there are deficiencies or omissions in the blog, I hope you can point out

5, References

USB to serial port CH340 wiring method
Principle and application of USB to serial port
Creating STM32 assembler based on MDK: serial port output Hello world

Tags: Single-Chip Microcomputer stm32

Posted on Tue, 26 Oct 2021 11:30:52 -0400 by MishieMoo