November 12, 2019

 

A, USART Introduction

  Universal Synchronous Asynchronous Receiver Transmitter (The USART) provides a flexible method of using the industry standard NRZ full duplex data exchange between an external device asynchronous serial data format. USART baud fractional baud rate generator provides a wide range of choice.

  STM32 bunch of resources quite rich, function quite strong. STM32F103ZET6 provides up to 5 serial ports, have fractional baud rate generator, one-way communication and half duplex synchronous supported single-wire communication, support LIN (Local Internet), the smart card protocol and IrDA (Infrared Data Association) SIR ENDEC specification, and a modem (CTS / RTS) operation. It also allows for multi-processor communication. DMA buffer using a multi-mode configurations, can achieve high-speed data communication.

Two, USART Features Overview

  Three pins are connected together by an interface with other devices. Any USART bidirectional communication requires at least two legs: receiving data input (RX) and transmit data output (TX).
  RX: receiving a serial data input. To distinguish between data and noise by oversampling techniques to recover data.
  TX: transmission data output. When the transmitter is disabled, the output pin is restored to its I / O port configuration. When the transmitter is activated, and data is not transmitted, TX pin is high. And in-line smart card mode, this I / O port is used for both transmitting and receiving data.

  Serial Peripheral mainly composed of three parts, namely, the baud rate control section, and transmits and receives control data storage section transfer section.

  1, Baud Rate Control

  Baud rate, i.e. the number of bits transmitted per second, with b / s (bps) represented by controlling the clock can change the baud rate. When configuring the baud rate, we USART_BRR baud rate write parameters to register, modify the serial clock divider value USARTDIV. USART_BRR register consists of two parts, namely DIV_Mantissa (USARTDIV the integer part) and DIVFraction (USARTDIV fractional) part, eventually, is calculated as:

      USARTDIV=DIV_Mantissa+(DIVFraction/16)。

  2, produces a fraction of the baud rate

  Receiver and transmitter baud rate value of integer and fractional USARTDIV register should be set to the same.
    Tx / Rx baud rate = fCK / (16 * USARTDIV)

  fCK here is to the peripheral clock (PCLK1 for USART2,3,4,5, PCLK2 for USART1) USARTDIV is an unsigned fixed point numbers. This 12-bit register value set in USART_BRR.

  NOTE: After writing USART_BRR, baud rate the baud rate counter register is replaced by the new value. Thus, the baud rate do not change the value in the register in the communication.

  Is the serial clock source USARTDIV peripherals dividing the frequency of the USART1 for, since it is mounted on APB2 bus, its clock source fPCLK2; and mounted on the APB1 USART2,3, clock source was fPCLK1, USARTDIV serial clock source through the divider outputs a transmitter clock and receiver clock, and controls transmission timing of the reception. 

  3, transmission and reception control

  Around the transmitter and the receiver controlling section, a good number of registers: CR1, CR2, CR3, SR, i.e. three USART control register (Control Register) and a status register (Status Register). Register by writing to various control parameters, controls transmission and reception, such as parity bits, stop bits, etc., further comprising controlling the USART interrupts; serial states are obtained from the status register can be queried at any time. Specific control and status checks, we are using library functions to achieve, this is not a detailed analysis of these register bits.

  4, the transfer data storing section

  According to our transceiver controller configuration register, the shift register storing the data transfer section is controlled.

  When we need to send the data, or the core DMA peripherals (a data transmission mode, in the next chapter) writes data from a memory (variable) to the transmit data register TDR, the transmission controller will automatically timely data from when the TDR is loaded into the transmit shift register, and then via the serial line Tx, the data is sent bit by bit, the data is transferred from the TDR to the shift register, generates a transmit register is empty TDR TXE event, when the data from the shift a register of all sent out, will have to send data completion event TC, these events can check the status register.

  Receives data is a reverse process, the data input bit by bit from the serial shift register to the reception line Rx, and then automatically transferred to the data register receives the RDR, with a final read into memory or DMA core instruction (variable) in .

Third, the serial port settings

  For IO multiplexing function, we first enable GPIO clock and clock enable multiplexing functions while GPIO mode should be set to a mode corresponding to the multiplexing function, set the serial port initialization parameters, including the baud rate, stop bits etc. parameters. After setting the serial port is enabled. At the same time, if you open a serial port interrupt, of course, to initialize the NVIC set the interrupt priority level, and finally to write an interrupt service routine.

  Serial general procedure set can be summarized as the following steps:

    1) serial clock enable, GPIO clock enable

    2) Serial Reset

    3) GPIO port mode

    4) initializing the serial port parameters

    5) enable interrupts and initializes NVIC (if enabled interrupted only need this step)

    6) Enables the serial port

    7) writing interrupt handlers

  Several firmware library functions directly related to the basic configuration of the serial port. These functions and definitions mainly in stm32f10x_usart.h and stm32f10x_usart.c file.

  1, the serial clock enable.

  A serial interface is mounted in the peripheral APB2 below, the enable function:
    RCC_APB2PeriphClockCmd (RCC_APB2Periph_USART1);

  2, the reset port.

  When an exception occurs in the peripheral provided by reset, the resetting of the peripheral, and then reconfigure the peripherals allowed to re-work purposes. Generally when the system is beginning to configure peripheral, will perform the first operation of the peripheral reset. Reset is done in the function USART_DeInit () in:
    void USART_DeInit (USART_TypeDef * USARTx); // reset port

  For example, to reset the port 1, method:
    USART_DeInit (the USART1); // reset port 1

  3, serial port initialization parameters.

  USART_Init serial port initialization function is achieved by (),
    void USART_Init (USART_TypeDef * USARTx, USART_InitTypeDef USART_InitStruct *);

  The first entry parameter to this function is initialized specified serial numbers, Pick USART1. The second parameter is a USART_InitTypeDef inlet structure pointer type, this structure pointer member variables to set the parameters of the serial port. General implementation format:

1 USART_InitStructure.USART_BaudRate = bound; // generally set to 9600; 
2 USART_InitStructure.USART_WordLength = USART_WordLength_8b; // 8-bit word length data format 
3 USART_InitStructure.USART_StopBits = USART_StopBits_1; // a stop bit 
4 USART_InitStructure.USART_Parity = USART_Parity_No; / / no parity bit 
. 5 USART_InitStructure.USART_HardwareFlowControl 
. 6 = USART_HardwareFlowControl_None; // no hardware flow control 
7 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; // transceiver mode 
8 USART_Init (USART1, & USART_InitStructure) ; // initialize the serial port

  From the above it can be seen the format initialization initialization parameters need to be set to: baud rate, word length, stop bits, parity bit, hardware flow control mode (sending and receiving). We can set these parameters.

  4, the data transmission and reception.

  STM32 transmission and reception is achieved through the data register USART_DR, which is a double register, contains the TDR and RDR. When data is written to this register, the serial port will automatically sent, when data is received, is also present within the register.

  Function library functions STM32 transmission data register operation is USART_DR:
    void USART_SendData (USART_TypeDef * USARTx, the Data uint16_t);
  writes data to the serial register through the USART_DR function.

  STM32 library functions USART_DR register read operation to the received serial data is a function of:
    uint16_t USART_ReceiveData (USART_TypeDef * USARTx);
  serial received data can be read out via this method.

  5, port status.

   Serial status can be read through the status register USART_SR. USART_SR everybody description Figure 1:

 

FIG 1 USART_SR description of register bit

  Look at two bits, 5th and 6th place RXNE and TC.

  RxNE (read data register is not empty), when this bit is set to 1, suggesting that has received the data, and can be read out. This time we have to do is as soon as possible to read USART_DR, this bit can be cleared by reading USART_DR, you can also write 0 to this bit, direct removal.

  TC (transmission completed), when the bit is set when the data representing the transmitted has been completed USART_DR. If you set the interrupt bit, it will generate an interrupt. This bit is cleared, there are two ways: 1) read USART_SR, write USART_DR. 2) Direct Writing 0.

  In our firmware library functions inside, read the serial status functions are:
    FlagStatus USART_GetFlagStatus (USART_TypeDef * USARTx, uint16_t USART_FLAG);

  The second entry is critical parameter to this function, it is indicated to see which state the serial interface, such as explained above RxNE (read data register is not empty), and TC (transmission completed). For example, to judge whether the read register is not empty (RXNE), a method of operating a library function:
    USART_GetFlagStatus (the USART1, USART_FLAG_RXNE);

  To determine whether or not to transmit (T End C), method of operating a library function is:

    USART_GetFlagStatus(USART1, USART_FLAG_TC);

  These MDK identification number which is defined by the macro definitions:

 1 #define USART_IT_PE ((uint16_t)0x0028)
 2 #define USART_IT_TXE ((uint16_t)0x0727)
 3 #define USART_IT_TC ((uint16_t)0x0626)
 4 #define USART_IT_RXNE ((uint16_t)0x0525)
 5 #define USART_IT_IDLE ((uint16_t)0x0424)
 6 #define USART_IT_LBD ((uint16_t)0x0846)
 7 #define USART_IT_CTS ((uint16_t)0x096A)
 8 #define USART_IT_ERR ((uint16_t)0x0060)
 9 #define USART_IT_ORE ((uint16_t)0x0360)
10 #define USART_IT_NE ((uint16_t)0x0260)
11 #define USART_IT_FE ((uint16_t)0x0160)

  6, the serial port is enabled.

  Serial port is enabled () function is achieved by USART_Cmd, this is easily appreciated, the method is used:

    USART_Cmd (USART1, ENABLE); // enable serial 

  7, open the serial port interrupt response.

  Sometimes when needed to open the serial port interrupt, you also need to enable serial port interrupt, the interrupt is a function of the serial port:
    void USART_ITConfig (USART_TypeDef * USARTx, uint16_t USART_IT,
                             FunctionalState the NewState)
  The second entry parameters of this function is labeled Enable serial type, which interrupt is enabled, the serial port interrupt because there are many types. For example, when (RxNE read data register is not empty) the received data to generate an interrupt, then the interrupt is turned methods:
    USART_ITConfig (the USART1, USART_IT_RXNE, the ENABLE); // enable interrupts, the interrupt data is received

  At the end of the transmission data (TC, transmission is complete) to generate an interrupt, then the method is:
    USART_ITConfig (the USART1, USART_IT_TC, ENABLE);

  8, to obtain the corresponding interrupt status. When we enabled an interrupt when the interrupt occurs, it will set a flag in the status register. We often interrupt handler, the interrupt is to determine which interrupt function is used:
    ITStatus USART_GetITStatus (USART_TypeDef * USARTx, uint16_t USART_IT);

  For example, to enable the serial port to send complete interrupt, then when an interrupt occurs, you can call this function in the interrupt handler to determine whether in the end is a serial transmission completion interrupt method is:
    USART_GetITStatus (the USART1, USART_IT_TC);

  The return value is SET, instructions are complete serial port interrupt occurs.

Four, uart_init () function

  Introduction uart_init function, which code is as follows:

// initialize GPIO and serial port 1 1 
 2 @ bound: baud 
 . 3 void uart_init (U32 bound) 
 . 4 { 
 . 5 GPIO_InitTypeDef GPIO_InitStructure; 
 . 6 USART_InitTypeDef USART_InitStructure; 
 . 7 NVIC_InitTypeDef NVIC_InitStructure; 
 . 8 ① // serial clock enable, clock enable GPIO multiplexing clock enable 
 . 9 RCC_APB2PeriphClockCmd (RCC_APB2Periph_USART1 | 
10 RCC_APB2Periph_GPIOA, the eNABLE); // enable USART1, GPIOA clock 
11 // ② reset port 
12 USART_DeInit (USART1); // reset port. 1 
13 is ③GPIO port mode // 
14 = GPIO_Pin_9 GPIO_InitStructure.GPIO_Pin; // ISART1_TX PA.9 
15 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
16 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // push-pull output multiplexing
17 GPIO_Init (GPIOA, & GPIO_InitStructure) ; // initialize GPIOA.9 
27 = USART_HardwareFlowControl_None; // no hardware flow control
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 18 is; // USART1_RX PA.10 
. 19 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; // floating input 
20 GPIO_Init (GPIOA, & GPIO_InitStructure) ; // initialize GPIOA.10 
21 is ④ // initialize the serial port parameters 
22 USART_InitStructure.USART_BaudRate = bound; // set the baud rate 
23 USART_InitStructure.USART_WordLength = USART_WordLength_8b; // word length is 8 bits 
24 USART_InitStructure.USART_StopBits = USART_StopBits_1; // a stop bit 
25 USART_InitStructure.USART_Parity = USART_Parity_No; // no parity 
26 USART_InitStructure.USART_HardwareFlowControl 
28 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; // mode transceiver 
29 USART_Init (USART1, & USART_InitStructure) ; // initialize the serial port 
30 #if EN_USART1_RX // if enabled receiver 
31 // ⑤ initialize the NVIC 
32 NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; 
33 is NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; // preemption priority 3 
34 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; // child priority. 3 
35 = NVIC_InitStructure.NVIC_IRQChannelCmd the eNABLE; // enable the IRQ channel 
36 NVIC_Init (& NVIC_InitStructure); // initialize the interrupt priority level 
37 // ⑤ enable interrupts 
38 USART_ITConfig (USART1, USART_IT_RXNE , eNABLE); // enable interrupts 
39 #endif 
40 // enable serial ⑥ 
41 USART_Cmd (USART1, eNABLE); // enable serial port 
42}                    

  As can be seen from the code that initialize the serial port of the process, consistent with our previously described. We use numbers ① ~ ⑥ marked the sequence:

    ① serial clock enable, GPIO clock enable
    ② serial reset
    ③ GPIO port mode
    ④ port initialization parameters
    ⑤ NVIC initialization and enable interrupts
    ⑥ enable serial

Fives,  

  1, configure a full-duplex serial port 1

    TX (PA9) pins to be configured as a push-pull output multiplexing;

    RX (PA10) floating input pins as input or pull.

  Mode Configuration Table 1 refer to the following:

Table 1 Mode GPIO port configuration table

  2, note that if you use a serial port to interrupt reception, which must be set usart.h EN_USART1_RX 1 (the default setting is 1). This function will configure the interrupt enable, and open the serial port 1 NVIC interrupt. Here the serial port 1 interrupt in group 2, the lowest priority group 2 is set to the inside.

  Then you have to write an interrupt service routine. Serial interrupt service routine of USART1_IRQHandler 1.

  3, focus look mian () function in the following two sentences:

    USART_SendData (USART1, USART_RX_BUF [t] ); // send data to the serial port. 1
    the while (! USART_GetFlagStatus (the USART1, USART_FLAG_TC) = the SET);

  First sentence, in fact, is to send a byte to the serial port. The second sentence, that is, we send our data to a serial port after, to detect whether the data has been sent completed. USART_FLAG_TC data transmission is completed macro definition identifier.


Guess you like

Origin www.cnblogs.com/dpc666/p/11844636.html