JZ2440 bare board development practice # 4 serial UART

Preliminaries

hardware

RS232 level: negative logic voltage (negative level represents a logic 1, a positive voltage represents a logical 0), 12v, mostly using computer

TTL / CMOS level: positive logic voltage (positive and negative voltages represent a logical 0 and 1) more, 5v, and other embedded devices using microcontrollers

(Mostly RS232 9-pin socket, TTL / CMOS level conversion chip devices need to communicate with RS232 level apparatus, which is generally integrated into the chip embedded device, 9 holes 232 in the slot as the hardware ports, but now less variable, gradually replaced by USB, requiring chip becomes TTL / CMOS chip to USB)

TXD, RXD, GND: TXD pin is transmitted, RXD to receive a pin, the transmitting end and the receiving end should be connected to the TX and RX to the TX-RX connection. Apparatus for common ground GND, to ensure consistent logic level (electric potential with a reference voltage in order to discuss).

software

Data format: start bit, data bits, parity, stop bits

Asynchronous serial protocol data transfer protocol, each transmission needs to initiate a transfer by an additional synchronization information. This format is typically synchronized to the start and stop bits to reflect. Meanwhile, in order to ensure the accuracy of the data, may also be present serial protocol parity bit to determine whether the current data transmission is correct, commonly used as a parity check, the check can also have other ways. The remaining few bits of data.

Idle where TXD and RXD serial protocol resistors is pulled pulled, embodied as a high level when the high level to a low level, is regarded as a start bit (0), opposite to the stop bit was from low to high (1). In general, using a 8N1, i.e., 8 data bits, 1 start bit and 1 stop bit, no parity bit, i.e., the transmission data to be transmitted 1Byte 10bits transmission.

Baud rate: good reception side and a transmission rate of transmission data end negotiation, meaning the number of data bits transmitted 1s.

analysis

In S3C2440 serial block diagram of an example, the data needs to be sent, the data from memory via peripheral bus passing TransmitBufferRegister i.e. transmit buffer register, and then transmits the data bit by bit shift register will be transmitted in the transmission buffer. Receiving is stored by receiving the received data bit by bit shift register into the buffer, waiting for extraction. The rate of the shift register by the impact of the baud rate generator, this is what we need to send or receive in advance to set a good rate mentioned above requires the sender and receiver have agreed to ensure the accuracy of the information.

 

program

As usual, we need to use the same function, you need to look at the schematic to find the appropriate pin, then find the register, the configuration register to enable the feature, coupled with the business logic to complete the desired function. Seen in the schematic circuit board uart USB-connected and are TXD0 the RXD0, and are connected to GPH2 GPH3 pins, it is necessary that both pins of UART function is enabled, then the configuration registers associated uart .

 

 The need to use uart s3c2440.h added to the register, to facilitate future reuse.

s3c2440.h
--------------------------------------------------------------
#ifndef __S3C2440_H
#define __S3C2440_H

#include <stdint.h>

/* CLOCK --------------------------------------------------*/
#define MPLLCON	 (*((volatile uint32_t*)0x4C000004))
#define UPLLCON	 (*((volatile uint32_t*)0x4C000008))

#define CLKDIVN	(*((volatile uint32_t*)0x4C000014))


/* WATCH DOG --------------------------------------------------*/
#define WTCON	(*((volatile uint32_t*)0x53000000))

/* GPIO --------------------------------------------------*/
#define GPGCON	(*((volatile uint32_t*)0x56000060))
#define GPGDAT	(*((volatile uint32_t*)0x56000064))
#define GPGUP	(*((volatile uint32_t*)0x56000068))

#define GPFCON	(*((volatile uint32_t*)0x56000050))
#define GPFDAT	(*((volatile uint32_t*)0x56000054))
#define GPFUP	(*((volatile uint32_t*)0x56000058))

#define GPHCON	(*((volatile uint32_t*)0x56000070))
#define GPHDAT	(*((volatile uint32_t*)0x56000074))
#define GPHUP	(*((volatile uint32_t*)0x56000078))

/* UART --------------------------------------------------*/
//uart0
#define ULCON0	(*((volatile uint32_t*)0x50000000))
#define UCON0	(*((volatile uint32_t*)0x50000004))
#define UFCON0	(*((volatile uint32_t*)0x50000008))
#define UMCON0	(*((volatile uint32_t*)0x5000000C))
#define UTRSTAT0	(*((volatile uint32_t*)0x50000010))
#define UERSTAT0	(*((volatile uint32_t*)0x50000014))
#define UFSTAT0	(*((volatile uint32_t*)0x50000018))
#define UMSTAT0	(*((volatile uint32_t*)0x5000001C))
#define UBRDIV0	(*((volatile uint32_t*)0x50000028))

#define UTXH0 (*((volatile uint8_t*)0x50000020))
#define URXH0 (*((volatile uint8_t*)0x50000024))

//uart1
#define ULCON1	(*((volatile uint32_t*)0x50004000))
#define UCON1	(*((volatile uint32_t*)0x50004004))
#define UFCON1	(*((volatile uint32_t*)0x50004008))
#define UMCON1	(*((volatile uint32_t*)0x5000400C))
#define UTRSTAT1	(*((volatile uint32_t*)0x50004010))
#define UERSTAT1	(*((volatile uint32_t*)0x50004014))
#define UFSTAT1	(*((volatile uint32_t*)0x50004018))
#define UMSTAT1	(*((volatile uint32_t*)0x5000401C))
#define UBRDIV1	(*((volatile uint32_t*)0x50004028))

#define UTXH1 (*((volatile uint8_t*)0x50004020))
#define URXH1 (*((volatile uint8_t*)0x50004024))

//uart2
#define ULCON2	(*((volatile uint32_t*)0x50008000))
#define UCON2	(*((volatile uint32_t*)0x50008004))
#define UTRSTAT2	(*((volatile uint32_t*)0x50008010))
#define UERSTAT2	(*((volatile uint32_t*)0x50008014))
#define UFSTAT2	(*((volatile uint32_t*)0x50008018))
#define UBRDIV2	(*((volatile uint32_t*)0x50008028))

#define UTXH2 (*((volatile uint8_t*)0x50004020))
#define URXH2 (*((volatile uint8_t*)0x50008024))


void HardwareInitAll(void);
void Delay(uint32_t time);

#endif

GPIO configured to RXD and TXD, and configure the UART related registers uart communication is set to 8 data bits, no parity, 1 stop bit, using PCLK (50MHz) clock as uart by the baud rate is 115200 is substituted into the formula to provide a reference manual, to give uart clock divider. as follows:

uart.h
----------------------------------------------------
#ifndef __UART_H
#define __UART_H

#include <stdint.h>

void UartInit(void);

void putc(uint8_t character);
uint8_t getc(void);

#endif
uart.c
------------------------------------------

#include "uart.h"


#include "s3c2440.h"


void UartInit(void)
{
	//GPIO init
	GPHCON &= ~((3<<6) | (3<<4));
	GPHCON |= (2<<6) | (2<<4);

	//UART init
	ULCON0 = 3;  //8n1
	UCON0 = (1<<2) | (1<<0);
	UBRDIV0 = (int32_t)( 50000000/ (115200 * 16) ) -1;
}


static uint8_t IsReadyToSend(void)
{
	return UTRSTAT0 & (1<<1);
}


static uint8_t IsReadyToReceive(void)
{
	return UTRSTAT0 & (1<<0);
}

void putc(uint8_t character)
{
	while(!IsReadyToSend());
	UTXH0 = character;
	if(character==(uint8_t)('\r'))
	{
		while(!IsReadyToSend());
		UTXH0 = (uint8_t)'\n';
	}
}

uint8_t getc(void)
{
	while(!IsReadyToReceive());
	return URXH0;
}

While the status register associated status bits as a function of the specified functions package, increasing the logic required to obtain a final business code: putc and getc, effect, use the serial port communication tool, to echo the input character.

main.c
--------------------------------------------

#include <stdint.h>

#include "s3c2440.h"
#include "led.h"
#include "uart.h"

int main()
{
	HardwareInitAll();
	UartInit();
	
	uint8_t tmp;

	while(1)
	{
		tmp = getc();
		putc(tmp);
	}
}

Thus, a simple rough serial echo procedure is completed.

Published 19 original articles · won praise 7 · views 6925

Guess you like

Origin blog.csdn.net/G_METHOD/article/details/104288739