12.2440 Serial Port Driver design
The function of the serial port is to accept data and send data, in the previous section has known the serial port pin signal. But the data send and receive needs certain conditions, that is, the initialization of the serial port. So today's content is divided into three parts:
1. Initialization of the serial port:
Create a uart.c to process the serial port and add it to the Makefile project file:
Then is the serial port processing program implementation.
The beginning of the program is to initialize the serial port, the process of initialization:
- Configuring PIN Functions
- Setting the Data mode
- Set the operating mode
- Set the baud rate.
1) Configure PIN function:
In 2440 in the serial board schematic diagram:
The above one-TXD0 and RXD0 is the serial port send and receive pins, corresponding to the number of 3, 2, 5 is grounded. Next, find the corresponding common pin in the core board schematic. Search for TXD0 in the core board schematic:
From the core board schematic know, RXD0 corresponds to the core board schematic diagram of the GPH3 pin, TXD0 corresponding to the core board schematic GPH2 PIN. So to achieve the transmission and reception of the serial port, is in the initialization, the configuration of the core board two pin function is: send and receive.
The next step is to find the GPH configuration register in the 2440 chip manual.
The first is to define the address of the Gphcon control register:
We want to configure the two pins for GPH2 and GPH3:
From the above information know, as long as the Gphcon [5:4] configured as 10,GPH2 corresponding PIN is txd[0] Send function, as long as the Gphcon [7:6] configured to 10,gph3 the corresponding PIN is rxd[0] receive function.
Here is the implementation code for [4:7] four-bit write 1010:0xf left four bits, reverse, is to [4:7] bit write all 0, and then the value of Gphcon and [4:7] four bits full 0 phase, that is, the Gphcon [4:7] Four bits are all 0.
Then to the Gphcon write 0b1010=0xa, first move the 0xa left four bits, and the bit to write, and finally, 0xa or up Gphcon, is to write 0xa to Gphcon [4:7] bit, complete the register settings.
2. Set the data format register:
It is known that [1:0] two bits are set word length, [2] bit is the stop bit of setting data, [5:3] bit is the way to set the check bit. This is the serial port we set up is a 8 bit data bit, so [1:0]=11. 1-bit stop bit, so [2]=0. There is no check bit, so [5:3]=0xx, no check bit. [6:7] bits remain the default. So the value to write to the ULCON0 register is: 0B11. The implementation is as follows:
2) Set the operating mode of the serial port refers to the operation mode of serial transceiver
The serial port can work in DMA mode, interrupt mode and polling mode. In general, we'll let the operating system work in DMA and interrupt mode, but if it's just setting up the serial port driver in Uboot, it will make it work in this simple way of polling.
Here's how to set the working mode:
Under the above register, there is a serial port control register:
The [0:3] bit in the UCON0 register is the operating mode of the control serial transceiver:
From the above know, in the acceptance mode, as long as the serial control register [0:1] bit set to 01, is working in the interrupt or polling mode. In send mode, as long as the [2:3] bit of the serial control register is set to 01, it is working in interrupt or polling mode. So the register UCON0 is supposed to be set to 0b0101=0x5. Code implementation:
3) Set the baud rate:
Define the value of this register:
From the front clock to know that the size of the MPLL is 405MHZ, the divide factor is 1:4:8, then the value of PCLK is 50000000HZ. Then the baud rate is 115200. To define and solve the baud rate:
The baud rate above, even if it comes out. This completes the initialization of the serial port.
2. Data transmission:
In the transmission through the serial port data, we do not have to tube the serial port stop bit and check bit, etc., we simply submit the data to the serial port to send, the serial port will automatically add this information.
Serial port data sending status register:
In this register [1] bit, is to determine whether the buffer is empty, if the bit is 0, that is, buffer is not 0, that is, the data has not been sent to complete, need to continue to send. If the bit is 1, which means that buffer is 0, the data has been sent to completion.
Code that continues to be sent:
When the UTRSTAT0 [2] bit is 1 o'clock, the loop will not end. UTRSTAT0 's [2] bit is 1 by the above note know 1 = transmitter (transmit buffer & Shifter register) empty, which is the completion of the data transfer, The inside is already empty. If the [2] bit of the UTRSTAT0 is 0, the bit is 0 for not empty, and the loop continues to send, which does not end.
Above the serial port if the data is sent, we have to send the data to the serial port, let it continue to send:
Implementation code:
The above is the data constantly sent to the serial port, through the UTXH0 register.
3. Data acceptance:
Buffer registers to which the received data is stored:
You can see the value of the [0] bit of the UTRSTAT0 register, or 0, to indicate that no data has been collected and wait. If 1, it means that the data is received. Implementation code:
The serial port will be placed in the URXH0 Register after receiving the data . And then we can read it out.
Here is the test serial data sending and receiving implementation: Add the test code in the main function:
Add the above test code, re-make compile, burn write to the Development Board, set up the Development Board from the Nandflash boot, in the serial port terminal can see a newline, and finally output a large H. Indicates that there is no problem with the operation code of the appeal, the operation of the serial port is successful.
Uart.c
Main.c
In the serial terminal, when we select the option, the serial number of the option will be echoed in the serial port, which is in the acceptance of the serial port to achieve.
The unsigned char getc (void) method in Uart.c is modified to:
The main function is modified to:
The above code is implemented, when the serial terminal input information, will be displayed in the window.
12.2440 Serial Port Driver design