uart1.h File Reference

Go to the source code of this file.

Data Structures

struct  UART1_BUFFER_CONTROL
struct  UART1_CONTROL

Defines

#define COM1_BASE   0x3f8
#define COM3_BASE   0x3e8
#define COM4_BASE   0x2e8
#define RXREG   0
#define TXREG   0
#define IER   1
#define IIR   2
#define FCR   2
#define LCR   3
#define MCR   4
#define LSR   5
#define MSR   6
#define SR   7
#define DLLR   0
#define DLHR   1
#define COM1_INT_EDA   0x01
#define COM1_INT_THRE   0x02
#define COM1_INT_RLS   0x04
#define COM1_INT_MS   0x08
#define COM1_INTERRUPTS   (COM1_INT_EDA|COM1_INT_THRE|COM1_INT_RLS)
#define COM1_IID_MS   0x00
#define COM1_IID_TXEMPTY   0x01
#define COM1_IID_RXRDY   0x02
#define COM1_IID_RLS   0x03
#define COM1_IID_TIMEOUT   0x06
#define COM1_LSR_DR   0x01
#define COM1_LSR_OE   0x02
#define COM1_LSR_PE   0x04
#define COM1_LSR_FE   0x08
#define COM1_LSR_BI   0x10
#define COM1_LSR_THRE   0x20
#define COM1_LSR_TEMT   0x40
#define COM1_LSR_ER   0x80
#define BAUDRATE_115200   0x01
#define BAUDRATE_57600   0x02
#define BAUDRATE_38400   0x03
#define BAUDRATE_19200   0x06
#define BAUDRATE_9600   0x0c
#define BAUDRATE_4800   0x18
#define BAUDRATE_2400   0x30
#define UART1_MAX_BUFFER_SIZE   500

Functions

void Uart1Init (void)
void Uart1Handler (void)
void Uart1Svc (void)
void Uart1BufferReset (void)
U8 Uart1RxBufferCheck (void)
U8 Uart1RxBufferCheckIsr (void)
void Uart1RxBufferPutIsr (U8 Data)
U8 Uart1RxBufferGet (void)
U8 Uart1TxBufferCheck (void)
void Uart1TxBufferPut (U8 Data)
U8 Uart1TxBufferGet (void)
U8 Uart1TxBufferCheckIsr (void)
U8 Uart1TxBufferGetIsr (void)

Variables

UART1_CONTROL Uart1Ctrl


Define Documentation

#define BAUDRATE_115200   0x01

Definition at line 46 of file uart1.h.

#define BAUDRATE_19200   0x06

Definition at line 49 of file uart1.h.

#define BAUDRATE_2400   0x30

Definition at line 52 of file uart1.h.

#define BAUDRATE_38400   0x03

Definition at line 48 of file uart1.h.

#define BAUDRATE_4800   0x18

Definition at line 51 of file uart1.h.

#define BAUDRATE_57600   0x02

Definition at line 47 of file uart1.h.

#define BAUDRATE_9600   0x0c

Definition at line 50 of file uart1.h.

#define COM1_BASE   0x3f8

Definition at line 4 of file uart1.h.

#define COM1_IID_MS   0x00

Definition at line 29 of file uart1.h.

#define COM1_IID_RLS   0x03

Definition at line 32 of file uart1.h.

#define COM1_IID_RXRDY   0x02

Definition at line 31 of file uart1.h.

#define COM1_IID_TIMEOUT   0x06

Definition at line 33 of file uart1.h.

#define COM1_IID_TXEMPTY   0x01

Definition at line 30 of file uart1.h.

#define COM1_INT_EDA   0x01

Definition at line 22 of file uart1.h.

#define COM1_INT_MS   0x08

Definition at line 25 of file uart1.h.

#define COM1_INT_RLS   0x04

Definition at line 24 of file uart1.h.

#define COM1_INT_THRE   0x02

Definition at line 23 of file uart1.h.

#define COM1_INTERRUPTS   (COM1_INT_EDA|COM1_INT_THRE|COM1_INT_RLS)

Definition at line 27 of file uart1.h.

#define COM1_LSR_BI   0x10

Definition at line 39 of file uart1.h.

#define COM1_LSR_DR   0x01

Definition at line 35 of file uart1.h.

#define COM1_LSR_ER   0x80

Definition at line 42 of file uart1.h.

#define COM1_LSR_FE   0x08

Definition at line 38 of file uart1.h.

#define COM1_LSR_OE   0x02

Definition at line 36 of file uart1.h.

#define COM1_LSR_PE   0x04

Definition at line 37 of file uart1.h.

#define COM1_LSR_TEMT   0x40

Definition at line 41 of file uart1.h.

#define COM1_LSR_THRE   0x20

Definition at line 40 of file uart1.h.

#define COM3_BASE   0x3e8

Definition at line 6 of file uart1.h.

#define COM4_BASE   0x2e8

Definition at line 7 of file uart1.h.

#define DLHR   1

Definition at line 20 of file uart1.h.

#define DLLR   0

Definition at line 19 of file uart1.h.

#define FCR   2

Definition at line 13 of file uart1.h.

#define IER   1

Definition at line 11 of file uart1.h.

#define IIR   2

Definition at line 12 of file uart1.h.

#define LCR   3

Definition at line 14 of file uart1.h.

#define LSR   5

Definition at line 16 of file uart1.h.

#define MCR   4

Definition at line 15 of file uart1.h.

#define MSR   6

Definition at line 17 of file uart1.h.

#define RXREG   0

Definition at line 9 of file uart1.h.

#define SR   7

Definition at line 18 of file uart1.h.

#define TXREG   0

Definition at line 10 of file uart1.h.

#define UART1_MAX_BUFFER_SIZE   500

Definition at line 54 of file uart1.h.


Function Documentation

void Uart1BufferReset ( void   ) 

Definition at line 86 of file uart1.c.

00086                            {
00087     Uart1Ctrl.TxBuffer.Get=0;
00088     Uart1Ctrl.TxBuffer.Put=0;
00089     Uart1Ctrl.RxBuffer.Get=0;
00090     Uart1Ctrl.RxBuffer.Put=0;
00091 }

void Uart1Handler ( void   ) 

Definition at line 38 of file uart1.c.

00038                        {
00039     U8 i;
00040     
00041     i=InByte(COM1_BASE+IIR); //get interrupt identification register
00042     i=i>>1;                  //get interrupt id number
00043     if(i==COM1_IID_RXRDY){
00044         i=InByte(COM1_BASE+LSR);
00045         if(i&COM1_LSR_DR){
00046             i=InByte(COM1_BASE+RXREG); //read data from RXREG to terminate interrupt
00047             Uart1RxBufferPutIsr(i);
00048         }
00049     }
00050     else if(i==COM1_IID_TXEMPTY){
00051         i=InByte(COM1_BASE+IER); //read IER to terminate this interrupt
00052         if(Uart1TxBufferCheckIsr()==BUFFER_EMPTY){
00053             Uart1Ctrl.UartTxTransmit=false;
00054             return;
00055         }
00056         else{
00057             i=Uart1TxBufferGetIsr();
00058             OutByte(COM1_BASE+TXREG,i);
00059         }
00060     }
00061     else if(i==COM1_IID_RLS){
00062         i=InByte(COM1_BASE+LSR);     // read LSR to terminate interrupt
00063         if(i&COM1_LSR_OE);           // code for overflow error
00064         if(i&COM1_LSR_PE);           // code for parity error
00065         if(i&COM1_LSR_FE);           // code for frame error
00066         if(i&COM1_LSR_BI);           // code for bit interval error
00067     }
00068     else if(i==COM1_IID_TIMEOUT){
00069         i=InByte(COM1_BASE+RXREG);   // read rxreg to terminate interrupt
00070     }
00071     else if(i==COM1_IID_MS){
00072         i=InByte(COM1_BASE+MSR);     //read modem status register
00073     }
00074 }

void Uart1Init ( void   ) 

Definition at line 15 of file uart1.c.

00015                     {
00016     PicIrqDisable(IRQ4_COM1);
00017     Uart1Ctrl.HasData=false;
00018     Uart1Ctrl.Transfered=false;
00019     Uart1Ctrl.RxData=0;
00020     Uart1Ctrl.TxBuffer.Get=0;
00021     Uart1Ctrl.TxBuffer.Put=0;
00022     Uart1Ctrl.RxBuffer.Get=0;
00023     Uart1Ctrl.RxBuffer.Put=0;
00024     Uart1Ctrl.UartTxTransmit=false;
00025     OutByte((U32)(COM1_BASE+IER),0x00);  //disable all interrupt
00026     OutByte((U32)(COM1_BASE+LCR),0x80);  //set DLAB=1
00027     OutByte((U32)(COM1_BASE+DLLR),(BAUDRATE_38400%256)); // set baudrate - divisor latch low byte
00028     OutByte((U32)(COM1_BASE+DLHR),(BAUDRATE_38400/256));  // set baudrate - divisor latch high byte
00029     OutByte((U32)(COM1_BASE+LCR),0x03);  //DLAB=0,n.8.1
00030     OutByte((U32)(COM1_BASE+FCR),0x00);  //no use fifo
00031     OutByte((U32)(COM1_BASE+MCR),0x0b);  //turn on OUT2=1,OUT1=0,RTS=1,DTR=1,OUT2 is for interrupt use
00032     OutByte((U32)(COM1_BASE+IER),COM1_INTERRUPTS); //enable eda,thre,rls interrupt,no enable ms interrupt 
00033     OsEventAdd(&Uart1Ctrl.Uart1Event,&Uart1Name,OS_EVENT_UART1);
00034     InterruptIrqHandlerSet(IRQ4_COM1,Uart1Handler);
00035     PicIrqEnable(IRQ4_COM1);
00036 }

U8 Uart1RxBufferCheck ( void   ) 

Definition at line 93 of file uart1.c.

00093                            {
00094     if(Uart1Ctrl.RxBuffer.Put!=Uart1Ctrl.RxBuffer.Get){
00095         if(Uart1Ctrl.RxBuffer.Put==(UART1_MAX_BUFFER_SIZE-1)){
00096             if(Uart1Ctrl.RxBuffer.Get==0) return BUFFER_FULL;
00097             else return BUFFER_NOT_EMPTY;
00098         }
00099         else{
00100             if((Uart1Ctrl.RxBuffer.Put+1)==Uart1Ctrl.RxBuffer.Get) return BUFFER_FULL;
00101             else return BUFFER_NOT_EMPTY;
00102         }
00103     }
00104     else return BUFFER_EMPTY;
00105 }

U8 Uart1RxBufferCheckIsr ( void   ) 

Definition at line 107 of file uart1.c.

00107                               {
00108     if(Uart1Ctrl.RxBuffer.Put!=Uart1Ctrl.RxBuffer.Get){
00109         if(Uart1Ctrl.RxBuffer.Put==(UART1_MAX_BUFFER_SIZE-1)){
00110             if(Uart1Ctrl.RxBuffer.Get==0) return BUFFER_FULL;
00111             else return BUFFER_NOT_EMPTY;
00112         }
00113         else{
00114             if((Uart1Ctrl.RxBuffer.Put+1)==Uart1Ctrl.RxBuffer.Get) return BUFFER_FULL;
00115             else return BUFFER_NOT_EMPTY;
00116         }
00117     }
00118     else return BUFFER_EMPTY;
00119 }

U8 Uart1RxBufferGet ( void   ) 

Definition at line 133 of file uart1.c.

00133                          {
00134     U8 Data;
00135 
00136     if(Uart1RxBufferCheck()==BUFFER_EMPTY) return 0;
00137     if(Uart1Ctrl.RxBuffer.Get==(UART1_MAX_BUFFER_SIZE-1)){
00138         Data=Uart1Ctrl.RxBuffer.Data[Uart1Ctrl.RxBuffer.Get];
00139         Uart1Ctrl.RxBuffer.Get=0;
00140     }
00141     else{
00142         Data=Uart1Ctrl.RxBuffer.Data[Uart1Ctrl.RxBuffer.Get];
00143         Uart1Ctrl.RxBuffer.Get++;
00144     }
00145     return Data;
00146 }

void Uart1RxBufferPutIsr ( U8  Data  ) 

Definition at line 121 of file uart1.c.

00121                                  {
00122     if(Uart1RxBufferCheckIsr()==BUFFER_FULL) return;
00123     if(Uart1Ctrl.RxBuffer.Put==(UART1_MAX_BUFFER_SIZE-1)){
00124         Uart1Ctrl.RxBuffer.Data[Uart1Ctrl.RxBuffer.Put]=Data;
00125         Uart1Ctrl.RxBuffer.Put=0;
00126     }
00127     else{
00128         Uart1Ctrl.RxBuffer.Data[Uart1Ctrl.RxBuffer.Put]=Data;
00129         Uart1Ctrl.RxBuffer.Put++;
00130     }
00131 }

void Uart1Svc ( void   ) 

Definition at line 76 of file uart1.c.

00076                    {
00077     U8 Data;
00078 
00079     if(Uart1Ctrl.UartTxTransmit==true) return;
00080     if(Uart1TxBufferCheck()==BUFFER_EMPTY) return;
00081     Data=Uart1TxBufferGet();
00082     Uart1Ctrl.UartTxTransmit=true;
00083     OutByte(COM1_BASE+TXREG,Data);
00084 }

U8 Uart1TxBufferCheck ( void   ) 

Definition at line 148 of file uart1.c.

00148                            {
00149     if(Uart1Ctrl.TxBuffer.Put!=Uart1Ctrl.TxBuffer.Get){
00150         if(Uart1Ctrl.TxBuffer.Put==(UART1_MAX_BUFFER_SIZE-1)){
00151             if(Uart1Ctrl.TxBuffer.Get==0) return BUFFER_FULL;
00152             else return BUFFER_NOT_EMPTY;
00153         }
00154         else{
00155             if((Uart1Ctrl.TxBuffer.Put+1)==Uart1Ctrl.TxBuffer.Get) return BUFFER_FULL;
00156             else return BUFFER_NOT_EMPTY;
00157         }
00158     }
00159     else return BUFFER_EMPTY;
00160 }

U8 Uart1TxBufferCheckIsr ( void   ) 

Definition at line 162 of file uart1.c.

00162                               {
00163     if(Uart1Ctrl.TxBuffer.Put!=Uart1Ctrl.TxBuffer.Get){
00164         if(Uart1Ctrl.TxBuffer.Put==(UART1_MAX_BUFFER_SIZE-1)){
00165             if(Uart1Ctrl.TxBuffer.Get==0) return BUFFER_FULL;
00166             else return BUFFER_NOT_EMPTY;
00167         }
00168         else{
00169             if((Uart1Ctrl.TxBuffer.Put+1)==Uart1Ctrl.TxBuffer.Get) return BUFFER_FULL;
00170             else return BUFFER_NOT_EMPTY;
00171         }
00172     }
00173     else return BUFFER_EMPTY;
00174 }

U8 Uart1TxBufferGet ( void   ) 

Definition at line 188 of file uart1.c.

00188                          {
00189     U8 Data;
00190 
00191     if(Uart1TxBufferCheck()==BUFFER_EMPTY) return 0;
00192     if(Uart1Ctrl.TxBuffer.Get==(UART1_MAX_BUFFER_SIZE-1)){
00193         Data=Uart1Ctrl.TxBuffer.Data[Uart1Ctrl.TxBuffer.Get];
00194         Uart1Ctrl.TxBuffer.Get=0;
00195     }
00196     else{
00197         Data=Uart1Ctrl.TxBuffer.Data[Uart1Ctrl.TxBuffer.Get];
00198         Uart1Ctrl.TxBuffer.Get++;
00199     }
00200     return Data;
00201 }

U8 Uart1TxBufferGetIsr ( void   ) 

Definition at line 203 of file uart1.c.

00203                             {
00204     U8 Data;
00205 
00206     //if(Uart1TxBufferCheck()==BUFFER_EMPTY) return 0;
00207     if(Uart1Ctrl.TxBuffer.Get==(UART1_MAX_BUFFER_SIZE-1)){
00208         Data=Uart1Ctrl.TxBuffer.Data[Uart1Ctrl.TxBuffer.Get];
00209         Uart1Ctrl.TxBuffer.Get=0;
00210     }
00211     else{
00212         Data=Uart1Ctrl.TxBuffer.Data[Uart1Ctrl.TxBuffer.Get];
00213         Uart1Ctrl.TxBuffer.Get++;
00214     }
00215     return Data;
00216 }

void Uart1TxBufferPut ( U8  Data  ) 

Definition at line 176 of file uart1.c.

00176                               {
00177     if(Uart1TxBufferCheck()==BUFFER_FULL) return;
00178     if(Uart1Ctrl.TxBuffer.Put==(UART1_MAX_BUFFER_SIZE-1)){
00179         Uart1Ctrl.TxBuffer.Data[Uart1Ctrl.TxBuffer.Put]=Data;
00180         Uart1Ctrl.TxBuffer.Put=0;
00181     }
00182     else{
00183         Uart1Ctrl.TxBuffer.Data[Uart1Ctrl.TxBuffer.Put]=Data;
00184         Uart1Ctrl.TxBuffer.Put++;
00185     }
00186 }


Variable Documentation

Definition at line 13 of file uart1.c.


Generated on Sat Apr 10 23:28:08 2010 for 80X86 MULTI-TASKING OPERATION SYSTEM by  doxygen 1.5.9