1. System Function Description
The hardware platform is a S3C2440 platform based on the arm9-kernel. The software platform is a GCC-based cross-compilation tool in the Linux operating system, such as arm-Linux-GCC and arm-Linux-lD. Use the superterminal of the upper computer to interact with the user. The main function of the system is to display a function main menu on the Super Terminal after the system is turned on. Then, you can use the computer keyboard to enter numbers for function selection. When the key 1 is pressed, the system enters the first subsystem. This subsystem uses four independent buttons to control the light-off of the four LEDs. After pressing key 2, the system enters the second sub-system, which is mainly used to implement the flow lamp function. When the LED light is on, the binary number "1" is displayed. If the light is off, the binary number "0" is displayed ". In this way, four LED lights are used to indicate that the four-digit binary number accumulates cyclically from 0 to 15. After pressing key 3, the system enters the third sub-system. This sub-system mainly enables text input and display. Enter some characters on the keyboard and display them on the terminal.
2. hardware design and function description
1) The LED circuit is as follows:
The four LEDs share the anode, And the cathode is connected to the pins 5, 6, 7, and 8 of port B of S3C2440 respectively. In this way, when the pins of port B are set to the IO output mode, we can write the data registers of port B to control the light-off of the four LEDs. When the fifth digit of port B is 0, led1 is on, and vice versa. And so on.
2) independent Key circuit:
The four independent buttons share the cathode, and the other end is connected to the pins 1, 4, 2, and 0 of port F of S3C2440 respectively. In addition, a 10 k pull-up resistor is added. In this way, when we set the pins 1, 4, 2, and 0 of port F to the IO input mode, we can judge whether a key is pressed by reading the data register of port F. When the first data register of port F is 0, it means press Key 1, and vice versa, it means press Key 1 is released. And so on.
3) general asynchronous transceiver
The S3C2440 universal Asynchronous Receiver and transmitter (UART) provide three independent asynchronous serial I/O (SiO2) ports, each of which can be operated in either the interrupt mode or the DMA mode. In other words, UART can generate an interrupt or DMA request for data transmission between the CPU and UART. UART supports a baud rate of up to 115.2 kbps using the system clock. If an external device provides uextclk to UART, UART can work at a higher speed. Each UART channel includes two 64-bit FIFO queues for the receiver and transmitter. Each UART contains a baud rate transmitter, transmitter, counter, and a control unit. The unique rate generator can be locked by pclk, fclk/N, or uextclk (external input clock. The transmitter and receiver contain a 64-bit FIFO and a data transmitter. The data is written to the FIFO and then copied to the transmitter before being transferred. Data is sent through the txdn. At the same time, the received data is moved in through the receiving data pin (rxdn), and then copied from the er to the FIFO.
Its features are as follows:
-Rxd0, txd0, rxd1, txd1, rxd2, and txd2 Based on DMA or interrupted operations
-UART channels with infrared and 64-bit FIFO channels: 0 and 1
-UART channels 0 and 1 with nrts0, ncts0, nrts1, and ncts1
-Supports sending and receiving handshakes.
The module diagram is as follows:
Data transmission: The sent data frame is programmable. It includes a starting bit, 5 ~ 8 data bits, an optional parity bits and 1 ~ Two Stop bits can be set by the linear control register ulconn. A sender can also generate an termination condition, which can force the serial output of a frame's sending time to logic 0. After the current message is completely transmitted, this module sends an end signal. After the termination signal is sent, it sends data to txfifo in sequence.
Data Receiving: Data Transmission supports programmable data frames. It includes a starting bit, 5 ~ 8 data bits, an optional parity bits and 1 ~ Two Stop bits can be set by the linear control register ulconn. The receiver can detect overflow errors, parity errors, frame errors, and termination conditions. An error flag can be set for each error.
-Overflow error indicates that the new data overwrites the old data before the old data is read.
-A parity error occurs when the receiver detects an undesired parity condition.
-Frame errors mean that the received data does not have a valid stop bit.
-The termination condition indicates the transmission time when the rxdn input keeps the logic 0 state longer than one frame.
When the receiver does not receive any data within the three-character period (the interval is based on the Character length) and the rx fifo mode is not empty, the receiving timeout condition appears.
3) storage space allocation
When started in NAND mode, the address is allocated as follows:
0x00000000-0x00000fff the first 4 K of NAND will be automatically mapped here.
0x30000000-0x33ffffff 64 msdram
0x48000000-0x5fffffff registers
3. Software Design and Function Description
GNU development in Linux mainly uses the cross-compilation tool arm-Linux-GCC to compile and arm-Linux-LD links.
1) Start the code head. s and write it in assembly language. Mainly implements initialization. Some initialization functions (init. c) written in C language are called during initialization ). As follows:
@*************************************** ***************************************
@ File: head. s
@ Function: Set the SDRAM, copy the program to the SDRAM, and then jump to the SDRAM to continue execution.
@*************************************** ***************************************
. Extern main
. Text
. Global _ start
_ Start:
Reset:
LDR sp, = 4096 @ set the stack pointer. The following are all C functions. You need to set the stack before calling.
BL disable_watch_dog @ shut down watchdog, otherwise the CPU will continue to restart
BL clock_init @ set mpll and change fclk, hclk, and pclk.
BL memsetup @ set the storage controller to use SDRAM
BL copy_steppingstone_to_sdram @ copy the code to the SDRAM
Ldr pc, = on_sdram @ jump to SDRAM to continue execution
On_sdram:
LDR sp, = 0x34000000 @ set the stack pointer,
Ldr lr, = halt_loop @ sets the return address
Ldr pc, = Main @ call the main function
Halt_loop:
B halt_loop
2) Function Code main. C and related function calls (Serial. c stdio. c key_led.c)
# Include "Serial. H" // contains the characters to send putc, and the characters to receive the GETC function declaration header file
# Include "stdio. H" // contains the string input scanf, and the string output printf function declaration header file
# Include "key_led.h" // function declaration header file containing buttons and LEDs
Char STR [1024]; // array used to receive strings
Int main () // Main Function
{
Unsigned char C;
Uart0_init ();
While (1)
{
/*
* Main menu display of the main function:
* 1-use independent buttons to light up corresponding LED lights
* 2-count with 4 LEDs cyclically lit (hexadecimal)
* 3-enter a string on the keyboard and display it on the Super Terminal.
*/
Printf ("\ n \ rplease choose: \ n \ r ");
Printf ("1 key_led_on \ n \ r ");
Printf ("2 led_recycle \ n \ r ");
Printf ("3 text \ n \ r ");
C = GETC ();
If (C = '1') {// enter character 1, then enter the button to light up the LED Function
Printf ("1 \ n \ rplease press key1-key4 to light on LED1-LED4 .");
Init_key_led (); // key and led Initialization
Key_led_on (); // press the key to light up the corresponding led
} Else if (C = '2') {// enter character 2, then the LED is cyclically lit to implement hexadecimal counting.
Printf ("2 \ n \ r recycle LED1-LED4 for 16 .");
Init_key_led (); // key and led Initialization
Led_recycle (); // led cycle light, indicating hexadecimal count
} Else if (C = '3') {// enter character 3, enter the string input and display function
While (1 ){
Printf ("3 \ n \ rplease input some words, then Ctrl + D to display the words: \ n \ r ");
Scanf (STR); // input string
Printf ("\ n \ r"); // line feed and jump to the beginning of the line
Printf (STR); // print the string
}
}
}
Return 0;
}
The Code declared after the sub-function definition is called in the program. For details, see the appendix.
3) makefile is mainly used to organize files and compile link files. During the link process, it also calls the link file UART. LDS. The details are as follows:
Objs: = head. O init. O serial. O stdio. O key_led.o main. O # target file variable
UART. Bin: $ (objs) # establish dependency between executable files
Arm-Linux-LD-tuart. LDS-O uart_elf $ ^ # link to call the UART. LDS File
Arm-Linux-objcopy-O Binary-s uart_elf $ @ # format conversion, ELF format conversion to binary files
Arm-Linux-objdump-D-M arm uart_elf> UART. Dis # Disassembly
%. O: %. C # establish dependency
Arm-Linux-gcc-fno-builtin-printf-fno-builtin-scanf-wall-O2-c-o $ <# compile C Programs
%. O: %. s # establish dependency
Arm-Linux-gcc-wall-O2-c-o $ <# compile Assembler
Clean: # Clear files
Rm-f uart. Bin uart_elf UART. Dis *. o
The connection file UART. LDS is as follows:
Sections {
. = 0x30000000; # define the runtime address as 0x30000000
. Text: {* (. Text)} # Place the code segment in the Code Area
. Rodata align (4): {* (. rodata)} # store read-only data in the read-only data zone. The storage address is 4 bytes aligned.
. Data align (4): {* (. Data)} # store the data in the data zone. The storage address is 4 bytes aligned.
. BSS align (4): {* (. BSS) * (common)} # public zone, 4-byte storage address alignment
4. Compilation and simulation debugging
In Linux, after the cross-compilation tool is installed and configured, run the make command to generate the UART. Bin binary file. Enable serialcrt in windows and use the uboot in nor to download the program to NAND for running. If not, modify the program to recompile the link, and then download and run the program. Until satisfactory results are achieved. In order to debug the program well, it is usually necessary to perform software simulation debugging first. After the software simulation debugging is complete, the software is downloaded to the hardware environment for running and hardware debugging.
Here I use the armul simulator in the axd simulation software provided by ads for simulation (in fact, it can also be used in combination with arm-Linux-GDB and DDD ). Open ads, create a new project, add the above files to work, and modify the header file and an embedded assembly in init. C. The modified header file is as follows:
; **************************************** **************************************
; File: head. s
Function: Set the SDRAM, copy the program to the SDRAM, and then jump to the SDRAM to continue execution.
; **************************************** **************************************
Import main; C function declaration to be called in the Assembly
Import disable_watch_dog
Import clock_init
Import memsetup
Import copy_steppingstone_to_sdram
Area init, code, readonly; defines a code segment init, read-only, 32-bit alignment
Entry
Code32
Start
LDR sp, = 4096; set the stack pointer. The following are all C functions. You need to set the stack before calling.
BL disable_watch_dog; Disable watchdog; otherwise, the CPU will be restarted continuously.
BL clock_init; Set mpll and change fclk, hclk, and pclk.
BL memsetup; set the storage controller to use SDRAM
BL copy_steppingstone_to_sdram; copy the code to the SDRAM.
Ldr pc, = on_sdram; jump to SDRAM to continue execution
On_sdram
LDR sp, = 0x34000000; set the stack pointer,
Ldr lr, = halt_loop; set the return address
Ldr pc, = Main; call the main function
Halt_loop
B halt_loop
End
The embedded assembly part of init. C is modified as follows:
_ ASM
{
MRC P15, 0, R1, C1, C0, 0 // read control register
ORR R1, R1, #0xc0000000 // set it to asynchronous bus mode"
MCR P15, 0, R1, C1, C0, 0 // write control register
}
Set the RO and start execution addresses of the project to 0x30000000, and set the start file head. O, in fact, the Field bit init. The compiler uses ARM920T.
Then compile the file.
Then, armul, a simulator in axd, is used for simulation and configured as ARM920T. Then install it in the generated axd file. The simulation is as follows:
If the simulation finds that the program does not meet the requirements, modify the program and debug it in the simulation.
1. Final reality
Finally, download the binfile generated in ads or the binfile generated by the cross-compilation tool in Linux to S3C2440 and run it. If the results do not match, repeat the above simulation debugging and modification process to know the final running effect as expected. The actual results are as follows:
Figure 1 compile Link
Figure 2 download a program
Figure 3 download a program 2
Figure 3 main interface displayed by the application system on the Super Terminal
Press the number key 1 on the keyboard to enter function 1. Press the independent key 1, then the led1 light is on, and the character key1 down, led1 on is sent to the serial port.
Press number 2 on the keyboard to go to function 2,
Count 4: count 11: count 15:
Press the number key 3 on the keyboard to go to function 3. You can enter the key on the keyboard and display it on the Super Terminal.
The detailed resources have been uploaded to my resources. You are welcome to download and share them, but please attach the original source.