1. Features of Exynos4412 UART
The UART in Exynos4412 has 4 independent channels, each of which can work in interrupt mode or DMA mode, that is, the UART can issue an interrupt or DMA request to transfer data between the UART and the CPU. The UART consists of a baud rate generator, a transmitter, a receiver, and control logic.
When using the system clock, the UART baud rate of Exynos4412 can reach 4Mbps. The baud rate can be programmed.
The Exynos4412 UART channel 0 has a 256-byte transmit FIFO and a 256-byte receive FIFO; channels 1 and 4 have a 64-byte transmit FIFO and a 64-byte receive FIFO; channels 2 and 3 have a 16-byte transmit FIFO and a 16-byte receive FIFO. When sending data, the CPU first writes the data into the transmit FIFO, and then the UART automatically copies the data in the FIFO to the "transmit shifter", which sends the data one bit at a time to the TxDn data line (inserting the start bit, check bit, and stop bit according to the set format). When receiving data, the "shifter" receives the data on the RxDn data line one bit at a time, and then copies it to the FIFO, from which the CPU can read the data.
Each channel of Exynos4412 UART supports 1 or 2 stop bits, 5, 6, 7 or 8 data bits, supports parity function, and also has infrared sending/receiving function.
Exynos4412 UART block diagram:
2. uart initialization steps:
1. Set the UART channel pins involved to UART function.
For example, in UART channel 0, GPA0_0 and GPA0_1 are used as RXD0 and TXD0 respectively. To use UART channel 0, first set the GPA0CON register to set the functions of GPA0_0 and GPA0_1 pins to RXD0 and TXD0.
2. Select the UART clock source
There are eight clock source options for Exynos4412 UART: XXTI, XusbXTI, SCLK_HDMI24M, SCLK_USBPHY0, SCLK_HDMIPHY, SCLKMPLL_USER_T, SCLKEPLL, SCLKVPLL, controlled by the CLK_SRC_PERIL0 register.
After selecting the clock source, you can also set the division coefficient through DIVUART0 ~ 4, which is controlled by the CLK_DIV_PERIL0 register. The clock obtained from the divider is called SCLK UART.
SCLK UART passes through the "UCLK Generator" in the figure above to get UCLK, whose frequency is the baud rate of the UART. The "Generator UCLK Generator" is set through these two registers: UBRDEVn, UFRACVALn (described below).
3. Set the baud rate: UBRDIVn register (UART BAUD RATE DIVISOR), UFRACVALn register
According to the given baud rate and the selected clock source frequency, the value of the UBRDIVn register (n is 0 to 4, corresponding to 5 UART channels) can be calculated by the following formula.
UBRDIVn = (int)( UART clock / ( buad rate x 16) ) – 1
The UBRDIVn register value calculated by the above formula is not necessarily an integer. The UBRDIVn register takes the integer part, and the smaller part is set by the UFRACVALn register. The introduction of the UFRACVALn register makes the baud rate more accurate.
For example, when the UART clock is 100MHz, the baud rate is required to be 115200 bps, then:
100000000/(115200 x 16) – 1 = 54.25 – 1 = 53.25
UBRDIVn = integer part = 53
UFRACVALn/16 = fractional part = 0.25
UFRACVALn = 4
4. Set the transmission format: ULCONn register (UART LINE CONTROL)
The format of the ULCONn register (n is 0 to 4) is shown in the figure below:
5. Set UART working mode: UCONn register (UART CONTROL)
6. UFCONn register (UART FIFO CONTROL), UFSTATn register (UART FIFO STATUS)
The UFCON n register is used to set whether to use FIFO and set the trigger threshold of each FIFO, that is, how much data in the send FIFO generates an interrupt, how much data in the receive FIFO generates an interrupt. And each FIFO can be reset by setting the UFCON n register.
By reading the UFSTAT n register, you can know whether each FIFO is full and how much data is in it.
When FIFO is not used, the FIFO depth can be considered to be 1. When FIFO is used, the FIFO depth of Exynos4412 can reach up to 256.
7. UMCONn register (UART MODEM CONTROL), UMSTATn register (UART MODEM STATUS)
These two types of registers are used for flow control and are not introduced here.
8. UTRSTATn Register (UART TX/RX STATUS)
The UTRSTAT n register is used to indicate whether the data has been sent or received. The format is shown in the following table. The "buffer" mentioned below is actually the FIFO in the figure below. When the FIFO function is not used, its depth can be considered to be 1.
9. UERSTATn Register (UART ERROR STATUS)
Used to indicate whether various errors have occurred. When bits [0] to [3] are 1, they indicate overflow error, check error, frame error, and detection of a "break" signal. When this register is read, it is automatically cleared to 0.
It should be noted that if FIFO is used when receiving data, an "error FIFO" will be used inside the UART to indicate which data in the receiving FIFO has an error during the receiving process. The CPU will only be aware of the error when reading the erroneous data. To clear the "FIFO", the erroneous data must be read out and the UERSTATn register must be read out.
10. UTXHn register (UART TRANSMIT BUFFER REGISTER)
The CPU writes data to this register, and the UART saves it in the buffer and sends it out automatically.
11. URXHn register (UART RECEIVE BUFFER REGISTER)
When UART receives data, it reads this register to get the data.
3. Sample Program Writing
The following is a small demo that implements the echo function on the terminal and turns the buzzer on and off by entering "beep_on" and "beep_off" on the terminal:
Header file definition:
/***************************************** UART * *************************************/
/* UART0*/
typedef struct {
unsigned int ULCON0;
unsigned int UCON0;
unsigned int UFCON0;
unsigned int UMCON0;
unsigned int UTRSTAT0;
unsigned int UERSTAT0;
unsigned int UFSTAT0;
unsigned int UMSTAT0;
unsigned int UTXH0;
unsigned int URXH0;
unsigned int UBRDIV0;
unsigned int UFRACVAL0;
unsigned int UINTP0;
unsigned int UINTSP0;
unsigned int UINTM0;
}uart0;
#define UART0 ( * (volatile uart0 *)0x13800000 )
UART.c
#include "exynos_4412.h"
#include "pwm.h"
void mydelay_ms(int time)
{
int i, j;
while(time--)
{
for (i = 0; i < 5; i++)
for (j = 0; j < 514; j++);
}
}
int strcmp(const char *src, const char *des)
{
while(*src || *des)
{
if(*src > *des)
return 1;
else if(*src < *des)
return -1;
else
{
src++;
des++;
}
}
return 0;
}
void uart0_init()
{
/*UART0 initialize*/
GPA0.CON = (GPA0.CON & ~0xFF ) | (0x22); //GPA1_0:RX;GPA1_1:TX
UART0.ULCON0 = 0x3; //Normal mode, No parity,One stop bit,8 data bits
UART0.UCON0 = 0x5; //Interrupt request or polling mode
//Baud-rate : src_clock:100Mhz
UART0.UBRDIV0 = 53;
UART0.UFRACVAL0 = 0x4;
}
void putc0(const char data)
{
while(!(UART0.UTRSTAT0 & 0X2));
UART0.UTXH0 = data;
if (data == 'n')
putc0('r');
}
char getc0(void)
{
char data;
while(!(UART0.UTRSTAT0 & 0x1));
data = UART0.URXH0;
if ((data == 'n') || (data == 'r'))
{
putc0('n');
putc0('r');
}
else
putc0(data);
return data;
}
void puts0(const char *pstr)
{
while(*pstr != '')
putc0(*pstr++);
}
void gets0(char *p)
{
char data;
while((data = getc0())!= 'r')
*p++ = data;
if(data == 'r')
*p++ = 'r';
*p = '';
}
/*
* Bare metal code, different from the LINUX application layer, must add loop control
*/
int main (void)
{
char ch[20];
pwm_init();
uart0_init();
char *q = "hello UART!";
puts0(q);
while(1)
{
gets0(ch);
puts0(ch);
if(!strcmp(ch, "beep_onn"))
beep_on();
if(!strcmp(ch, "beep_offn"))
beep_off();
// putc0(getc0());
}
return 0;
}
Previous article:Exynos4412 bare metal development - watchdog timer
Next article:Exynos4412 bare metal development - A/D converter
Recommended ReadingLatest update time:2024-11-16 19:45
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- [SC8905 EVM Evaluation] Power Supply Ripple Test
- Mbed OS changes the way it is released
- STM32MP157A-DK1 Evaluation + SPI Device
- How to define a const structure variable
- [Silicon Labs BG22-EK4108A Bluetooth Development Review] Received the fastest, unboxing
- Introduction to three interrupt debugging methods of ARM
- Ask a superconductivity question
- CC2640R2F Single Carrier Code
- Essential PCB wiring rules for electronic hardware engineers: basic theories of right-angle routing, differential routing, and serpentine routing
- 【EETALK】What is the difference between traditional Bluetooth and low-power Bluetooth? Will traditional Bluetooth be replaced by low-power Bluetooth in the future?