S3C2440 learning path - simple configuration of 007UART

Publisher:考古专家Latest update time:2021-10-14 Source: eefocusKeywords:s3c2440  uart Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Basic knowledge

The serial port is one of the most basic functions of various chips. After the serial port is configured, the program can print various information through the serial port, which is convenient for code debugging. The main parameters of the serial port are 4: baud rate, data width, check bit, stop bit

image.png

Common serial port configurations are: baud rate 115200, 8-bit data width, no parity bit, 1-bit stop bit, usually abbreviated as 115200 8N1


2440 has 3 sets of UARTs, each set of UARTs has 2 64Bytes FIFOs


jz2240 uses serial port 0 to communicate with the computer, and PL2303 is used to convert the logic level between the computer and the development board.

insert image description here

insert image description here

insert image description here

2. Main registers

2.1 GPHCON & GPHUP

The serial port needs to use three pins: Tx, RX, GND, so first you need to set the corresponding pins to TX, RD mode, and in addition, you need to turn on the pull-up function because the UART is high level when idle.

insert image description here

insert image description here

2.2 ULCON0

ULCON0 is used to control the basic properties of the serial port. Here, 8N1 in 115200 8N1 is set.

insert image description here

2.3 UCON0

ULCON mainly sets the clock source and various working modes

If FLCK is selected as the clock, there are many restrictions on the configuration, so it is more convenient to select PCLK here.

Select polling mode for both sending and receiving, so you need to check the status register manually.

insert image description hereinsert image description here
insert image description here
insert image description here

2.4 UTRSTAT0

Status register. In polling mode, you can check the value of this register to determine the status of data transmission and reception.

When sending data, you need to first check whether UTRSTAT0[2] is empty. The logic code is:


{

/* Wait for the trasnmitter to be empty, otherwise it means there is still data left to be sent*/

while (!(UTRSTAT0 & 0x4))

send data

}


When receiving data, you need to first check whether UTRSTAT0[0] is not empty. The logic code is:


{

/*You need to wait until the Receive buffer is not empty, which means that data has been obtained, otherwise keep waiting*/

while (!(UTRSTAT0 & 0x1));

Receive data

}

insert image description here

2.5 UTXH0

When sending data, fill the data to be sent into this register

insert image description here

2.6 URXH0

When receiving data, this register is used to obtain the data.

insert image description here

2.7 OFFENSIVE0

Baud rate setting, the formula is: UBRDIVn = (int)( UART clock / ( buad rate x 16) ) –1

UART clock is set to PLCK=50M

Expected buad rate = 115200

Substituting into the formula (50M / (115200 x 16)) - 1 = 27.12 - 1 = 26


Because division may have decimals, the actual baud rate will have errors, but 2440 provides the error calculation formula and the maximum error range.

The error is calculated as:

tUPCLK = (UBRDIVn + 1) x 16 x 1Frame / PCLK

tUEXACT = 1Frame / baud-rate

UART error = (tUPCLK – tUEXACT) / tUEXACT x 100%


UBRDIVn = 26,

1Frame = start bit + data bit + parity bit + stop bit. Because each time 1 Byte is transmitted, there will be 1 start bit.

1Frame = 1 + 8 + 0 + 1 = 10,

tUPCLK = (26 + 1)* 16 * 10 / 50M = 432 / 5M

tUEXACT = 10 / 115200 = 1 / 11520

UART error = (432 / 5M - 1 / 11520) / (1 / 11520) * 100% = -0.446%

insert image description here

![Insert image description here](https://img-blog.csdnimg.cn/2018121823063113.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xpYW40OTQzNjI4MTY=,size_16,color_FFFFFF,t_70

3. Source code

uart.c


int uart0_init(void)

{

    /* 1 set gpio to uart mode

        Tx GPH2 [5:4] b10

        Rx GPH3 [7:6] b10

    */

    GPHCON &= ~((0x3 << 4) | (0x3 << 6));

    GPHCON |= (0x2 << 4) | (0x2 << 6);


    /* 2 set pull up */

    GPHUP |= (0x1 << 2) | (0x1 << 3);


    /* 3 set form 8N1 */

    ULCON0 |= 0x3;


    /* 4 set Soruce clock PCLK and poling mode*/

    UCON0 = 0x5;


    /* 5 set baud rate 115200

    PCLK = 50M

    UBRDIVn   = (int)( UART clock / ( buad rate x 16) ) –1

    UBRDIVn = (50M / (115200 x 16))-1 = 26.1267 -> 26

    */

    OFFENSIVE0 = 26;


return 0;

}


int getchar(void)

{

    int chr = 0;


    while (!(UTRSTAT0 & 0x1))

    {

        //nothing

    }


    chr = URXH0;


    return chr;

}


int putchar(int c)

{

    while (!(UTRSTAT0 & 0x4))

    {

        //nothing

    }


    UTXH0 = (unsigned char )c;

}


The implementation of uart0_init, getchar, putchar is very simple, so I won't introduce it in detail.

The only thing to note is that when sending data, it is best to convert the data into unsigned char to avoid abnormal data transmission when the ascii of the character exceeds 127 and becomes a negative number.

Keywords:s3c2440  uart Reference address:S3C2440 learning path - simple configuration of 007UART

Previous article:s3c2440 learning path-008 uart implements printf function
Next article:s3c2440 learning path-006 clock settings

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号