14. Serial Console Setup
Serial console This section is built with three main elements:
1. Console Frame Setup
1.1 The classification of the console is introduced:
1.1.1 Menu-based console: the console that performs the appropriate function when the set number or Letter option is selected:
For example, after entering the Uboot interface, is the menu-based console:
Wait for us to enter a command to perform the appropriate action. For example above, if we enter 1 at this point, we are doing the operation of format the NAND flash:
1.1.2 Analytic Console: In the menu-based console above, select 5:exit to command line: The following will appear:
Into the Analytic console:
After we enter help, the console will parse whether the command is supported by the console, and if so, it will call the corresponding help function to run. Lists the commands supported by the analytic console where the Uboot is located:
Because the analytic console can be very complex to implement, today choose a relatively simple menu-based console, to achieve a relatively simple uboot console.
Menu-based console:
You can see a menu-based console, the first thing is to print out the menu, and the cycle of printing, the first thought is to use printf to print the menu information. Code:
Above is the uboot information, according to the user's options to execute the corresponding function function, through switch to select the implementation. Above is a menu-shaped frame.
In the implementation of the menu-based console above, the main two functions are scanf and printf.
2. Implementation of scanf and printf
First of all to implement the printf function, in the implementation of a function, the first consideration is its function, printf function is to print information, printf in the PC computer printing information is printed to the display. However, in the Development Board, printf information is printed to the serial terminal. The next thing is to understand the parameters of the function. To understand that its parameters need to be executed at the command line: Man 3 printf:
You can see that the first one is our printf function, except that its arguments are in the form of a variable parameter, and the parameter is represented by three points, that is, the function's parameters are not necessarily. Can be zero, can be one, can be a lot of. The form of the latter argument is determined by the preceding const char *format. is if the const char *FORMAT specifies an shaping parameter, then ... It has to be the shape parameter, if the const char *format is a character parameter, then ... It has to be a character parameter.
Realization idea: Convert incoming parameters into a string buffer, then the string type buffer through a while loop, using the void putc (unsigned char ch) function implemented in the previous section to output to the serial terminal.
The difficulty of realizing the idea is how to convert the incoming parameters into the buffer of the string type.
1) Create a PRINTF.C in the Uboot project in the previous section:
2) then add the PRINTF.C to the makefile:
3) The implementation of PRINTF.C: (1) converts a parameter into a string. (2) Print the string to the serial port.
for (2) print the string to the serial port. The start-up is convenient because the PUTC () function has been implemented previously:
The difficulty is (1) converting the argument to a string. You need to use some of the macros that have been implemented in the C language. Some macros (functions) about the argument processing. To help us finish. (1) Convert the argument to a string. Implementation steps:
The above implements the conversion of a parameter to a string. But the problem comes again. The above three macros (functions) have to be implemented, and the implementation is very complex, and is very standard. Here you can use the porting method.
We're going to implement this embedded software, not that all the code needs to be written by itself. For example, the implementation of TFTP,TFTP also need IP protocol stack, for the IP stack, we can not achieve. So it is in the implementation of embedded software, a great time to do is to transplant.
There are two places we have ported, one in the Linux kernel, or the C library. When it comes to porting, it's too difficult for us at this stage. So now we just need to know what the functions of these three functions are, and how to use the files extracted from the Linux kernel to integrate into your own Uboot project.
Here are the two extracted file directories:
Copy the two folders to the original project directory:
Next, make the following modifications to the makefile under the project folder:
Before modification:
Modify:
- Put the printf.c into the Lib folder , in the Lib folder compiled together LIB.O, you need to include LIB/LIB.O. So replace the PRINTF.O with LIB/LIB.O.
- Then, Gboot.bin was used by gboot.elf to get it.
- Gboot.elf is obtained by linking the *.o file above.
- To get LIB/LIB.O, you need to go to the Lib folder to compile, and the result of make is to get all:
All is the file that you get after make in the Lib folder:
- Also go to the Lib folder for clean:
The last modified file is:
Makefile under Lib:
The makefile on the top floor are:
After you have modified it, compile make and report the following error:
The above ctype.c file is in the Lib folder, where the error indicates that the Lib folder files have been used in the Gboot.bin project, just an error. The error message is that there are a lot of undefined symbols in ctype.c. Next open ctype.c and see what's going on:
The above see ctyp.c used these symbols, make why do you quote these symbols are not defined? You can see that it contains the ctype.h header file, which opens:
You can see that the symbols have been defined in the include/ctype.h. You can see that ctype.h has defined these symbols. The problem is that at compile time, arm-linux-gcc this compiler by default looks for the path of the header file, and does not find the Include/ctype.h header file in our project now. This requires us to compile at the time, specify the compiler ARM-LINUX-GCC at compile time except to go to the default path to your change file, but also to the path we specify to find the header file. the header file path is specified by the-I parameter to find the path to the header file.
Open the makefile in the Lib directory:
Here, we need to specify the path to the your change file by adding-I to the ARM-LINUX-GCC name. But as you can see, there are already cflags variables, which are to save some of the compiled parameters. Cflags is defined in the makefile under the project folder:
Open the top-level makefile to add the definition of cflags:
The value of the cflags variable is a path that obtains the current relative path through the shell command pwd, including the Include folder under that path. For the underlying directory to be able to use the file, you need to export the variable:
After the modification, make, the previous problem solved:
You can see that the previous problem is resolved, but there is a new problem: the va_list of line 3rd in PRINTF.C is undefined. Open PRINTF.C:
The above type va_list is defined in the Lib/vsprintf.h file:
So to resolve this error, simply include the lib/vsprintf.h into the PRINTF.C to resolve:
Compile make after joining, there will be the following error:
The first warning, uart.c:29:warning:conflicting types for built-in function ' PUTC ' then turns on the 29 line of Uart.c and discovers there is no restrained function at all. Starting this is because there is one missing parameter in makefile:-fno-builtin
Add the variable cflags to the makefile where the C file is compiled:
Recompile:
Will find the warning gone. There is a mistake again. is to find the undefined vaprintf when compiling lib/lib.o. Oh, this is our function is wrong, it should be vsprintf. After modification:
Modify and then make: or the original error:
After the reason has been modified, there should be no more such a mistake. Besides, there is no vaprintf shadow in the project now. It's still a mistake. No matter how you make the Clea, or the error. Got helpless, help Baidu, Baidu no solution. Finally make clean after entering the Lib folder, found that the Lib folder generated things are not cleared. In the Lib folder to make a clean back to the original project directory, made Clean, the miracle appeared:
However, there is still a mistake, which is caused by the scanf function that we used to define and implement:
This is the first comment we take out:
After the modification, the recompilation succeeded:
The Gboot.bin is generated:
Write to the Development Board, found that the contents of the serial terminal output:
The printf function is implemented, and the next step is to implement the SCANF function. In Redhat 6.4 Execute command: Man 3 scanf:
You can see that the return value is int, the first parameter is a const char *format, the second argument is a variable. This function is implemented in the opposite way as printf. The scanf function is: 1) Gets the input string first. 2) obtain a string for format conversion, passed to the system. Implementation code:
Compilation succeeded:
Here, printf and scanf are all implemented, compiled and burned to the Development Board:
You can see that the input effect we're looking for appears. We input 1, no response, input 4, will output Error:wrong
Selection. In the subsequent operation, the corresponding processing method of the switch selection is implemented. The basic building of the console has been completed here.
3. Optimization of program Structure
Can see, now the directory of the file is very chaotic, with the depth of the project, the file will be more and more, for the convenience of management, here, we put the device-driven C files in the Dev folder:
In this way, the layout of our engineering documents is much more reasonable. Of course, the Dev folder also needs to makefile files, copy the past from the Lib folder and then modify:
The last dev makefile content is:
This modifies the makefile in the Dev directory, and note that the makefile on the top floor also needs to be changed:
After making the corresponding changes above, make:
Here, our basic engineering framework has been implemented and the structure is more reasonable. For the future with the multi-function to build a platform.
14. Serial Console Setup