Bare metal UART Driver (redefinition of printf to serial port)

Source: Internet
Author: User

Make sure that the initialize of the object has been initialized by using uboot.

 

// Serial register

// Uart0

# Defineulcon0 * (vu32 *) 0x7f005000) // UART channel 0-row control register

# Defineucon0 * (vu32 *) 0x7f005004) // UART channel 0 control register

# Defineufcon0 * (vu32 *) 0x7f005008) // UART channel 0 FIFO control register

# Defineumcon0 * (vu32 *) 0x7f00500c) // UART channel 0 Modem Control Register

# Defineutrstat0 * (vu32 *) 0x7f005010) // UART channel 0 sending/receiving status register

# Defineuerstat0 * (vu32 *) 0x7f005014) // UART channel 0 receives error Status Register

# Defineufstat0 * (vu32 *) 0x7f005018) // UART channel 0 FIFO Status Register

# Defineumstat0 * (vu32 *) 0x7f00501c) // UART channel 0 modem Status Register

# Defineutxh0 * (vu32 *) 0x7f005020) // UART channel 0 sending buffer register

# Defineurxh0 * (vu32 *) 0x7f005024) // UART channel 0 receiving buffer register

# Defineubrdiv0 * (vu32 *) 0x7f005028) // channel 0 baud rate division register

# Defineudivslot0 * (vu32 *) 0x7f00502c) // UART channel 0 division slot register

# Defineuintp0 * (vu32 *) 0x7f005030) // channel 0 Interrupt Processing Register

# Defineuintsp0 * (vu32 *) 0x7f005034) // UART channel 0 interrupt source Processing Register

# Defineuintm0 * (vu32 *) 0x7f005038) // UART channel 0 Interrupt Mask register

 

 

 

 

 

// Author: Chen

// Time created: 20120220

// Last modification time: 20120220

// Description

 

# Include "system. H"

# Include "UART. H"

# Include "maid"

 

 

 

 

//////////////////////////////////////// //////////////////////////

// Add the following code to support the printf function,

# If 1

 

# Include "stdio. H"

// Supported functions required by the standard library

Struct _ file

{

Inthandle;

};

/* File is typedef 'd in stdio. H .*/

File _ stdout;

// DEFINE _ sys_exit () to avoid half-Host Mode

_ Sys_exit (int x)

{

X = X;

}

// Redefine the fputc Function

Int fputc (int ch, file * F)

{

Uart0_sendbyte (u8) CH );

Returnch;

}

# Endif

 

 

 

Static const u16uart_speed [13] [2] ={{ 17,0x4924 },{ 858,0x4924 },{ 428,0x4924 },{ 285,0x4924 }, {4924x4924}, {4924x4924}, {4924x4924}, {x }, {4924x4924}, {4924 x}, {x }};

 

 

 

 

/*************************************** **************************************** **************************************** **

* Function: voiduart0_init (void)

* Function: Serial Port 0 Initialization

* Parameter: None

* Return value: None

* Dependency: underlying macro definition

* Author: Chen

* Duration: 20120220

* Last modification time: 20120220

* Description: Initialize the serial port 0.

**************************************** **************************************** **************************************** */

Void uart0_init (u8uart_speed)

{

Pclk_gate | = 1 <1; // uart0 selects the pclk clock, 66 MHz

Gpioa-> Con & = ~ (0xff); // clear the previous settings, gpioa0, gpioa1

Gpioa-> con | = (host | (host <4); // set gpioa0 and gpioa1 to UART

Ulcon0 = 0x3; // 8 bits per frame // clear settings // normal mode, no verification, 1 Stop bits

Ucon0 = (0x1 <2) | 0x1 | (2 <10); // clear the control register // set (pclk = 66 MHz) to a baud rate clock

Ufcon0 = 0; // disable FIFO

Umcon0 = 0; // disable AFC. The high level of nrts is invalid.

Ubrdiv0 = uart_speed [uart_speed] [0]; // set the baud rate integer

Udivslot0 = uart_speed [uart_speed] [1]; // you can specify the fractional part of the baud rate.

Uintp0 = 0; // disable uart0 interrupt

Uintsp0 = 0; // set the interrupt source

Uintm0 = 0; // disable interrupt shielding \

 

}

 

/*************************************** **************************************** **************************************** **

* Function: voiduart0_sendbyte (u8 data)

* Function: one byte is sent from the serial port 0.

* Parameter: sent data

* Return value: None

* Dependency: underlying macro definition

* Author: Chen

* Duration: 20120220

* Last modification time: 20120220

* Note: Use Serial Port 0 to send one byte

**************************************** **************************************** **************************************** */

Voiduart0_sendbyte (u8 data)

{

While (! (Utrstat0 & (1 <2); // wait for the sending buffer to be empty

Utxh0 = data; // write data to the sending Buffer

}

Now you can directly use printf to send data to the serial port.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.