Source: Huaqing vision embedded School, source: http://www.embedu.org/Column/Column698.htm
Author: Instructor Cheng,Hua Qing vision embedded college lecturer.
I. Data Communication Methods
The basic data communication methods can be divided into parallel communication and serial communication.
(1) Parallel Communication: multiple data lines are used to send data simultaneously. It features high transmission speed and is suitable for short-distance communication.
(2) Serial Communication: one piece of data is used to transmit data one by one in sequence. It is characterized by simple communication lines and low costs. It is suitable for long-distance communication.
II. Introduction to UART
The UART English name is universal Asynchronous Receiver er and transmitter, and the Chinese name is universal asynchronous transceiver.
Many beginners always don't understand What synchronization is and what Asynchronization is. To put it simply, a clock line synchronization is required during data transmission. For example, An IIC or SPI bus must have a clock line for Data Synchronization during data transmission. Asynchronous mode means that data can be directly sent without a clock line during data transmission. For example, we often say that serial ports adopt asynchronous methods. There is no clock signal line between the sender and receiver. They use their own clock.
After understanding synchronization and Asynchronization, continue to look at UART. We can think of it as a thing integrated into the microprocessor, which consists of two parts: a parallel data receiver (RX) that can be used to replace serial bit transfer with a microprocessor) and a transmitter (TX) that converts parallel data from a microprocessor into a serial form ). UART provides some status information, such as whether the receiver is full or whether the transmitter is empty. Nowadays, many microprocessor chips have built-in on-chip UART.
Iii. Serial Port
A serial interface, also known as a serial communication interface (usually a COM interface), is an extended interface that uses a serial communication method. Serial communication refers to the transmission of data by bit during transmission, that is, one-bit and one-bit transmission.
We often say that the serial port refers to the RS-232C interface, which was in 1970 by the American Electronics Industry Association (EIA) the serial communication standard jointly developed by Bell systems, modem manufacturers, and computer terminal manufacturers. Its full name is "Technical Standard for Serial Binary data exchange interfaces between data terminal devices (DTE) and data communication devices (DCE ". The traditional RS-232-C interface standard has 22 lines, using a standard 25-core d-plug-in seat (db25), and later simplified to 9-core d-socket (DB9 ), currently, 25-core plug-in blocks are rarely used in applications. The serial port on s5pc100 adopts DB9.
Although it provides me with nine lines, we often only use two lines, RX and TX.
In combination with the UART mentioned above, the two devices communicate through the serial port and the wiring is as follows:
Serial Communication is an asynchronous serial communication method. To ensure correct output transmission, there must be a protocol. Here we call it the serial communication protocol. For example:
Start position: sends a logic "0" signal, indicating the start of the transmission character.
Data bit: It can be 5-8 bits. Commonly Used 8 bits represent a single character.
Parity bit: add the first digit behind the data bit so that the number of "1" is an even number (even verification) or an odd number (odd verification) to verify the correctness of data transmission.
Stop bit: it indicates the end of a character. It can be 1 or 2 digits.
Idle bit: In the logical "1" status, indicating that no data is transmitted on the current line.
Another important parameter is the baud rate, which measures the data transmission rate. The number of binary digits transmitted per second. For example, if the baud rate is 115200, the number of characters transferred per second is 115200/10 = 11520. Someone may ask, isn't it divided by 8? The actual character is 8 bits, but the start and stop bits are required for serial communication.
Generally, each digit is represented by a single digit, which is 10 digits. Generally, each digit is represented by a single digit, which is 10 digits.
Iv. UART of s5pc100
The UART of s5pc100 has four independent asynchronous serial input and output ports. Each port can be operated based on the interrupt or DMA. In other words, the UART Controller can generate an interrupt or DMA request between the CPU and UART to transmit data. In the system clock, the maximum baud rate supported by the UART port 0th and the second port is 115.2 k bps, and the maximum baud rate supported by ports 1st and third is 3 Mbps. If the uclk is provided by the external clock, the UART speed can be higher. Each UART contains two 64-byte receiving and sending FIFO queues.
We can see that each UART Controller is roughly divided into two parts: the transmitter and the receiver. In the transmitter, if the UART is in FIFO mode, there are 64-byte buffer registers, generally, when sending data, the CPU saves the data to these 64 bytes first. If the UART is in non-FIFO mode, this 64-byte buffer register is useless, we only use the buffer register of the lowest of the 64 bytes to store data, also known as transmit holding register, the key point is that the data in the sending buffer in the transmitter is not directly sent to the output pin, but must also be sent to the sending shift register, that is, transmit.
Shifter, and then the transmit shifter sends it to the output pin. The acceptor side is probably the same. Both the sending and receiving shift registers control the transmission and receiving of data at the baud rate generated by the baud rate generator. The generation of the baud rate also needs to be controlled by the clock source.
For UART configuration, you must configure its working mode (The UART of s5pc100 can be set to normal mode and infrared mode), parity bit, stop bit and baud rate.
Here, we configure UART to normal mode (rotation mode), with no parity bits and 1-bit Stop bits, without FIFO, without interruption, and with a baud rate of 115200bps. Below is a brief description of the involved registers:
Ulcon:
According to our requirements, this register should be configured as follows:
[6]: 0 [5:3]: 00 [2]: 0 [1:0]: 11
Ulcon = 0x3;
Ucon:
Here we use the system clock pclk to directly set the value of this register to 0.
Ucon = 0x0;
Ufcon: FIFO control register
If you do not use FIFO, set the 0th bits to 0.
Ufcon = 0;
Finally, let's talk about the baud rate settings:
Previously we have set the UART clock to be provided by pclk (66 MHz. The UART baud rate of s5pc100 is determined by the ubrdivn and udivslotn registers. The calculation rules are as follows:
To generate the baud rate of 115200bps, we should set it as follows:
Set the baud rate of uart0 to 115200
Div_val = (pclk/(BPS × 16)-1
Div_val = ubrdivn (integer part) + (Num of 1's in udivslotn)/16 (small part)
Div_val = (pclk/(BPS × 16)-1
= (66 MHz/(115200*16)-1
= 34.8
The integer part is stored in the ubrdivn register. The fractional part is 0.8, which is composed
(Num of 1's in udivslotn)/16. That is:
(Num of 1's in udivslotn)/16 = 0.8
Num of 1's in udivslotn = 0.8*16 = 12.8 = 12
You can see from the table that 12 corresponds to 0 xdddd. That is, the value of udivslotn is 0 xdddd.
V. Core code
# Define uart_0_rcv_ OK (x) (X = uart0.utrstat0 & (1 <0 ))
# Define uart_0_sed_ OK (x) (X = uart0.utrstat0 & (1 <1 ))
Void uart_0_init ()
{
// Set the working mode of gpio to uart_0_rxd and uart_0_txd.
Gpa0.gpa0con = gpa0.gpa0con &~ (0xff <0) | (0x22 <0 );
/* Do not enable the pclk clock to be UART */
Clk_gate_d1.clk_gate_d1_4 & = (1 <0 );
/* Uart0 Data Transmission
* Mode: Normal Mode [6] 0
* Parity: No parity [5:3] 000
* Stop bit: One Stop bit [2] 0
* Length: 8bit [1:0] 11
*/
Uart0.ulcon0 = 0x3;
/* Uart0 control register
* Clock select: pclk [11: 10] 00
* Transmit mode: polling [3: 2] 01
* Receive mode: polling [1:0] 01
*/
Uart0.ucon0 = (0x1 <2) | (0x1 <0 );
/* Do not enable FIFO */
Uart0.ufcon0 = 0x0;
/* Disable AFC */
Uart0.umcon0 = 0x0;
/* Set the baud rate of uart0 to 115200.
* Div_val = (pclk/(BPS x 16)-1
* Div_val = ubrdivn (integer part) + (Num of 1's in udivslotn)/16 (decimal part)
*
* Div_val = (pclk/(BPS x 16)-1
* = (66 MHz/(115200*16)-1
* = 34.8
*
*/
Uart0.ubrdiv0 = 34;
Uart0.udivslot0 = 0 xdddd;
/* Shield all uart0 interruptions */
Uart0.uintm0 = 0xf;
/* Enable the pclk to be a uart clock, and the UART starts to work */
Clk_gate_d1.clk_gate_d1_4 & = (1 <0 );
Return ;}
/* Send a character */
Void send_char (unsigned char ch)
{
Int flag = 0;
Uart0.utxh0 = CH;
While (! Uart_0_sed_ OK (FLAG ));
Return;
}
/* Accept one character */
Unsigned char recv_char (void)
{
Int flag;
Unsigned char ch;
While (! Uart_0_rcv_ OK (FLAG ))
; CH = uart0.urxh0;
Return ch;
}