S3C2440 serial port learning

The serial port is a commonly used communication method in embedded systems. It uses bit-by-bit transmission and bit-by-bit reception. Serial communication. The serial port has many functions, such as printing and debugging, connecting various modules. It is safe and reliable, easy to connect, so it is widely used.

Basic definition of serial communication:
1. Baud rate
2. Format: data bit, stop bit, parity bit, flow control.
3. Connect the transmitter and receiver with TX and RX

The logic level of USART:
Originally TX (that is, the sending control terminal) was high, and ARM gave it time to pull it low and maintain 1Bit . The PC starts timing when it is low, because the transmitter and the receiver have an agreed baud rate , that is , the level of the sending pin is read at intervals according to the agreed baud rate . (Usually read the value of the intermediate time of a Bit), in this way, the reception of one byte is completed after eight cycles. After receiving a byte, end the reception of this byte with an agreed stop bit (the stop bit is generally 1-2 bits of data). In some cases, eight bits of data and stop bits in the middle also has a parity bit. (Generally, odd and even parity bits, that is, the number of high levels in a byte).

Serial port storage:
Because it is sent bit by bit, after each bit of data is received, it will be stored in the FIFIO of the chip and wait for the memory to take it out, and then store it again.

Serial port transmission speed:
for example, the baud rate of 115200, the number of bits sent in one second is 115200/10=11520Bit

Next, we start to analyze and write the USART code:
1. First, we analyze the configuration of the register from the data sheet:
GPH2 and GPH3 can be configured as TX and RX of serial port 0 (ie, send and receive mode). The
required condition is GPFCON [5:4]=10 and GPFCON[7:6]=10 As
Insert picture description here
mentioned above, when the serial port is not working, the pin level defaults to high level.
Insert picture description hereHere we initialize the GPH pin to high level, that is, GPHUP[ 2] is 1; in
this way, our pin initialization is complete.

2. Next, let's set the baud rate.
First, we find the calculation formula of the baud rate in the manual: Insert picture description here
UBRDIVn = (int)( UART clock / (buad rate x 16)) –1
From this we can calculate UBRDOVn = 50000000/ (115200*16)-1=26
so write 26 to UBRDIV0
Insert picture description here, which is checked by the mode setting register, should write 0x00000005 to the UCON0 register

3. After setting these, then we set the data mode of the serial port.
From the list in the data manual, Insert picture description herewe get: ULCON0 = 0x00000003; /* 8n1: 8 data bits, no check bit, 1 stop bit*/

Next, write the serial port output and input functions. I won’t introduce the very simple logic here, but I will list them here.

int putchar(int c)
{
	/* UTRSTAT0 */
	/* UTXH0 */

	while (!(UTRSTAT0 & (1<<2)));
	UTXH0 = (unsigned char)c;
	
}

int getchar(void)
{
	while (!(UTRSTAT0 & (1<<0)));
	return URXH0;
}

Next, let's write a printing function, which is also listed here:


int puts(const char *s)
{
	while (*s)
	{
		putchar(*s);
		s++;
	}
}在这里插入代码片

At this point, the whole experiment is basically completed. You can download it to the development board to try whether the serial port prints Hello World when booting up.

Remarks:
Experiment code:

在这里#include "s3c2440_soc.h"


/* 115200,8n1 */
void uart0_init()
{
	/* 设置引脚用于串口 */
	/* GPH2,3用于TxD0, RxD0 */
	GPHCON &= ~((3<<4) | (3<<6));
	GPHCON |= ((2<<4) | (2<<6));

	GPHUP &= ~((1<<2) | (1<<3));  /* 使能内部上拉 */
	

	/* 设置波特率 */
	/* UBRDIVn = (int)( UART clock / ( buad rate x 16) ) –1
	 *  UART clock = 50M
	 *  UBRDIVn = (int)( 50000000 / ( 115200 x 16) ) –1 = 26
	 */
	UCON0 = 0x00000005; /* PCLK,中断/查询模式 */
	UBRDIV0 = 26;

	/* 设置数据格式 */
	ULCON0 = 0x00000003; /* 8n1: 8个数据位, 无较验位, 1个停止位 */

	/*  */

}

int putchar(int c)
{
	/* UTRSTAT0 */
	/* UTXH0 */

	while (!(UTRSTAT0 & (1<<2)));
	UTXH0 = (unsigned char)c;
	
}

int getchar(void)
{
	while (!(UTRSTAT0 & (1<<0)));
	return URXH0;
}

int puts(const char *s)
{
	while (*s)
	{
		putchar(*s);
		s++;
	}
}
插入代码片

Guess you like

Origin blog.csdn.net/weixin_41407439/article/details/89112594