Drive your serial port to deal with registers

Source: Internet
Author: User

Drive your serial port to deal with registers
++ ++
After the system completes the initialization for entering the C language environment, the first one that can be written in C is the serial port
Driver. In the startup Code provided on the lumit website, the serial port driver is compiled by sink (sysinit. s ).
Here I think that as long as the work can be completed with C, we should try to use the. H. C code.
One is easy to understand, and the other is easy to transplant, both of which are important.

The implementation of serial port drivers on various processors is similar:
Datong mainly sets parameters such as the baud rate, stop bit, parity, and data bit during initialization.
The parameter settings are the same. Other settings are also important, such as whether to enable the interrupt mode, including data collection interruption and sending.
Data interruption, but at the same time, the interrupt processing function must be implemented to handle serial port interruption.

The difference is that the UART-related registers of different processors are more or less different.
The meanings of bit values are also different. You only need to carefully compare the processor manual, directly to the relevant
Find out the UART section.

In the attachment UART driver file UART. C, some code is quite interesting.
Come out and share it with you.

/* UART primitives */
# Define get_status (p) (* (volatile unsigned *) (p) + ustat) // obtain the current UART status // offset ?? // Because MMU is available, the offset of so to locate pmemory must be added.
# Define rx_data (s) (S) & usrrxdata) // indicates the arrival of the received data
# Define get_char (p) (* (volatile unsigned *) (p) + urxbuf) // obtain (accept) A data from the buffer
# Define tx_ready (s) (S) & usrtxholdempty) // send data readiness flag
# Define put_char (p, c) (* (unsigned *) (p) + utxbuf) = (unsigned) (c) // send a data to the buffer //? Buffer ??

Int uart_putchar (unsigned int uart_base, char ch) // uart_base is defined in UART. C.
{
/* Read TX ready flag, when = 1 break */
While (tx_ready (get_status (uart_base) = 0)
;
 
Put_char (uart_base, CH );

Return ch;
}

Int uart_getchar (unsigned int uart_base, char * Ch)
{
/* Read RX data flag, when = 1 break */
While (rx_data (get_status (uart_base) = 0)
;

* Ch = get_char (uart_base );

Return * Ch;
}

The first part defines a set of UART primitive operations with macros, mainly including the following five: // primitive operations ??

Get_status (P): obtains the current UART status.
Rx_data (s): indicates the arrival of received data.
Tx_ready (s): indicates the ready to send data.
Get_char (P): Get a data from the buffer
Put_char (p, c): sends a data to the buffer.

This set of primitives works with a certain serial data sending and receiving logic, that is, first look at the flag bit, the flag bit represents
You can send and receive data only when you permit the operation. Otherwise, you are waiting. Here two underlying driver interfaces are implemented: // busy waiting = polling ??

Int uart_putchar (unsigned int uart_base, char ch );

Int uart_getchar (unsigned int uart_base, char * Ch );

The attachment also provides uart_test, the simplest UART-driven test function.
Receive a character sent by the user from the serial port and then send it back, that is, getchar () with Echo ().
With the underlying UART Driver Interface, more data sending and receiving interfaces can be implemented on the upper layer.

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.