One: tiny4412 serial driver writing
1. Introduction of serial communication
Serial communication refers to the serial port bitwise (BIT) to send and receive bytes, the concept of serial communication is very simple, serial port bitwise (BIT) Send and receive bytes. Although it is slower than parallel communication by Byte (byte), the serial port can receive data with another line while sending data using one line. It is simple and enables long-distance communication. For example, when the IEEE488 defines the parallel state of passage, the total length of the equipment line shall not exceed 20 meters, and no two devices shall be longer than 2 meters, and for the serial port, the length can be up to 1200 meters.
The communication protocol used for serial communication is rs-232,rs-232 communication mode allowing simple connection of three lines: Tx, Rx and ground. However, for data transmission, both parties must use the same baud rate for the timing of the information. The RS-232 (ansi/eia-232 standard) is the serial connection standard on IBM-PC and its compatible machines. Can be used for many purposes, such as connecting the mouse, printer or modem, but also can be connected to industrial instruments and meters. For the improvement of drive and wiring, the transmission length or speed of RS-232 in practical applications often exceeds the standard value. RS-232 is limited to the PC serial port and the point-to-point communication between devices.
2. The basic communication model of the serial port is as follows:
TXD: Sending data
RXD: Receiving data
GND: Ground
The serial drive is similar to the other peripheral driver configuration process, which is roughly divided into the following steps:
(1) View circuit diagram, configure the corresponding GPIO function pin
(2) Configure the corresponding registers of the serial controller
(3) Test serial port receiving, sending
Today is the first serial port COM0, the following is the circuit diagram:
Find the appropriate GPIO function pin
The following is the entire mode of the serial port operation diagram:
The following is the corresponding register of the serial controller:
Some of the major registers we used today are:
ULCON0: Data Format control register (configuration data bits, stop bit, check bit, etc.);
UCON0: Serial Control switch
UTXH0: Sending data
URXH0: Receiving data
UTRSTAT0: Data transmit and receive status register
UBRDIV0,UFRACVAL0: Configuring the baud rate
Here is the clock frequency required for the UART, and the calculation formula for the bit rate:
The sclk_uart used to calculate the UART is 100M
ubrdiv0= (100000000)/(115200 x16)-1 = 53.3=53=0x35;
Ufracval0= 4;
The following specific test code:
1 #ifndef __regs_h2 #define__regs_h3 4 #defineGpa0base 0x114000005 #defineGpa0con (* (volatile unsigned long *) (gpa0base + 0x0000))6 7 #defineUart0base 0x138000008 #defineULCON0 (* (volatile unsigned long *) (uart0base + 0x0000))9 #defineUCON0 (* (volatile unsigned long *) (uart0base + 0x0004))Ten #defineUFCON0 (* (volatile unsigned long *) (uart0base + 0x0008)) One #defineUMCON0 (* (volatile unsigned long *) (uart0base + 0x000c)) A #defineUTRSTAT0 (* (volatile unsigned long *) (uart0base + 0x0010)) - #defineUERSTAT0 (* (volatile unsigned long *) (uart0base + 0x0014)) - #defineUFSTAT0 (* (volatile unsigned long *) (uart0base + 0x0018)) the #defineUMSTAT0 (* (volatile unsigned long *) (uart0base + 0x001c)) - #defineUTXH0 (* (volatile unsigned char *) (uart0base + 0x0020)) - #defineURXH0 (* (volatile unsigned char *) (uart0base + 0x0024)) - #defineUBRDIV0 (* (volatile unsigned long *) (uart0base + 0x0028)) + #defineUFRACVAL0 (* (volatile unsigned long *) (uart0base + 0x002c)) - #defineUINTP0 (* (volatile unsigned long *) (uart0base + 0x0030)) + #defineUINTSP0 (* (volatile unsigned long *) (uart0base + 0x0034)) A #defineUINTM0 (* (volatile unsigned long *) (uart0base + 0x0038)) at - #defineUart3base 0x13830000 - #defineULCON3 (* (volatile unsigned long *) (uart3base + 0x0000)) - #defineUCON3 (* (volatile unsigned long *) (uart3base + 0x0004)) - #defineUFCON3 (* (volatile unsigned long *) (uart3base + 0x0008)) - #defineUMCON3 (* (volatile unsigned long *) (uart3base + 0x000c)) in #defineUTRSTAT3 (* (volatile unsigned long *) (uart3base + 0x0010)) - #defineUERSTAT3 (* (volatile unsigned long *) (uart3base + 0x0014)) to #defineUFSTAT3 (* (volatile unsigned long *) (uart3base + 0x0018)) + #defineUMSTAT3 (* (volatile unsigned long *) (uart3base + 0x001c)) - #defineUTXH3 (* (volatile unsigned char *) (uart3base + 0x0020)) the #defineURXH3 (* (volatile unsigned char *) (uart3base + 0x0024)) * #defineUBRDIV3 (* (volatile unsigned long *) (uart3base + 0x0028)) $ #defineUFRACVAL3 (* (volatile unsigned long *) (uart3base + 0x002c))Panax Notoginseng #defineUINTP3 (* (volatile unsigned long *) (uart3base + 0x0030)) - #defineUINTSP3 (* (volatile unsigned long *) (uart3base + 0x0034)) the #defineUINTM3 (* (volatile unsigned long *) (uart3base + 0x0038)) + A #endif //__regs_h
1 #ifndef __uart_h2 #define__uart_h3 4 voiduart_init ();5 voidSet_gpio ();6 voidshow_serial ();7 voidMYPUTC (unsignedCharc);8UnsignedCharMYGETC (void);9 voidMyputs (unsignedChar*str);Ten voidMygets (unsignedChar*str); One A #endif //__uart_h
1#include"Regs.h"2#include"uart.h"3 4 intMain ()5 {6 Set_gpio ();7 uart_init ();8 show_serial ();9 return 0;Ten } One A voidshow_serial () - { - /*loop to send and receive data*/ theUnsignedCharcmd[ +]; - while(1) { -Myputs ("[[Email protected]]#"); - mygets (cmd); + myputs (cmd); -Myputs ("\ n"); + } A at } - - voidMYPUTC (unsignedCharc) - { - while(! (UTRSTAT0 & (1<<1)));//wait for buffer to be empty before sending the character -UTXH0 =C; in } -UnsignedCharMYGETC (void) to { +UnsignedCharch; - while(! (UTRSTAT0 &1));//wait for buffer not to be empty theCH =URXH0; * $ returnch;Panax Notoginseng } - the voidMyputs (unsignedChar*str) + { A while(*str) { theMYPUTC (*str); + if(*str = ='\ n') -MYPUTC ('\ r'); $str++; $ } - } - the voidMygets (unsignedChar*str) - {WuyiUnsignedCharch; the while(1) { -ch = mygetc ();//Get character Wu MYPUTC (CH); - if(ch = ='\ r') { AboutMYPUTC ('\ n'); $ Break; - } -*str =ch; -str++; A } +*str =0; the } - $ voidSet_gpio () the { the /*0x2 = Uart_0_rxd the *0x2 = Uart_0_txd*/ theGpa0con &= ~0xFF; -Gpa0con |=0x22; in } the the voidUart_init () About { the /*Step 1: Data format Control*/ theULCON0 =3;/*8bit Data 1bit stop no parity*/ the /*Step 2:uart Switch*/ +UCON0 =1| (1<<2); - /*Step 3:set baud-rate*/ theUBRDIV0 =0x35;BayiUFRACVAL0 =0x4; the /*Step 4: Data Transceiver Cache*/ the //UTXH0 - //URXH0 - /*Step 5: Data transmit and receive status registers*/ the //UTRSTAT0 the}
Tiny4412 serial port (Uart) driver authoring