How to perform standard IO through UART?

Interaction is still necessary, and the interaction efficiency of using the keyboard and display is still very high. Of course, you can directly use the UART for character input and output. But why waste the C standard input and output format control functions?

This time the content is to use scanf() and printf() functions to interact with the PC and the development board.

How to perform standard IO through UART?

One, C standard function library

The hardware-related functions ultimately require direct access to the hardware. At this point, the realization of the C standard function library faces many hardware devices, and it is already powerless.

The implementation of the C standard library used by Atmel Studio is suspected to be Newlib.

In the project’s ASF\sam\utils\syscalls\gcc\syscalls.c file, ASF has implemented a number of functions that need to be implemented by itself (although this file is called syscall, it may only be due to compliance with unix habits. Because of this Without an operating system, there is no such thing as a "system call"). However, there is no default implementation for highly customized implementations like input and output.

Most of Newlib's file reading and writing functions are implemented through the _read() and _write() functions. Therefore, standard input and output can be realized by implementing these two functions. The signature of the function and the meaning of the parameters can be googled.

Second, realize

Note that the configuration of the UART needs to be completed first.

The specific implementation is very simple. In the implementation, it is not necessary to judge the target file, and all input and output are completed through UART. If you need to determine whether the target file is standard input and output, and report the error when an error is detected, you need to include the following header files:

12#include #include

_write:

123456789101112131415int_write(intfile,constchar*ptr,intlen){// Only process standard output if(file == STDOUT_FILENO){for(inti = 0; iUART_SR & UART_SR_TXRDY));UART0->UART_THR = ptr[i];)returnlen; }else{errno= EBADF;return-1;}}

_read:

12345678910111213141516171819int_read (intfile, char*ptr, intlen){// Only process standard input if(file == STDIN_FILENO){inTI;for(i = 0; i UART_SR & UART_SR_RXRDY));ptr[i] = UART0->UART_RHR; /* When a newline character is read, return if ('' == ptr[i])return i;}return i; /* Slow o flush? Area? Has °? Slow y */}else{errno= EBADF;return -1;}}

Test, the following code gets the input of UART, and re-output through UART:

123456789#include printf("-I- Test for stdio through UART0");charreadbuf[64];while(1){printf("-I- Input something...");scanf("%s", readbuf) ;printf("Output: %s",readbuf);}

Note that the PC side needs to add a newline character when sending data.

Three, use in ASF

Because this is a very commonly used function, it is also implemented in ASF. Not only can some configurations be made in ASF, but there are only a few lines of code that really need to be written when using it, and even these lines of code can be completely referenced (copy) the code in the ASF example.

Add the module Standard serial I/O.

Corresponding macros have been declared by default in conf_board.h:

12/* Configure UART pins */#define CONF_BOARD_UART_CONSOLE

In conf_uart_serial.h, there is already a reference setting code for using UART. Just delete the comment symbol in front of the reference code:

1234567/* A reference setTIng for UART *//** UART Interface */#define CONF_UART CONSOLE_UART/** Baudrate setTIng */#define CONF_UART_BAUDRATE 115200/** Parity setTIng */#define CONF_UART_PARITY UART_MR_PAR_NO

Call stdio_serial_init to initialize serial standard I/O:

1234567constusart_serial_options_t uart_serial_options = {.baudrate = CONF_UART_BAUDRATE,.paritytype = CONF_UART_PARITY};/* Configure console UART. */sysclk_enable_peripheral_clock(CONSOLE_UART_ID);stdio_serial_init(CONF_UART, &stdio_serial_options(CONF_UART);

Wired Barcode Scanner

Wired Barcode Scanner,Wired Scanner,Wired 2D Barcode Scanner,Barcode Scanner Usb Cable

ShengXiaoBang(GZ) Material Union Technology Co.Ltd , http://www.sxbgz.com

Posted on