UART.C
资源名称:gca-150A.rar [点击查看]
上传用户:jndfzc
上传日期:2014-06-02
资源大小:325k
文件大小:6k
源码类别:
单片机开发
开发平台:
Others
- #include "define.h"
- #include "uart.h"
- #include "maindef.h"
- //232
- #define SENDD(PB,c) *(volatile unsigned char*)(PB+USTHR)=(char)c
- #define RECVD(PB) *(volatile unsigned char*)(PB+USRHR)&0xff
- #define GET_USCSR(PB) *(volatile unsigned char*)(PB+USCSR)
- #define UART0_BASE 0xFFFD0000 //A port
- #define UART1_BASE 0XFFFCC000 //B port
- #define UART422_BASE 0x700008
- #define UART485_BASE 0x700000
- #define baud_2400 0x30
- #define baud_4800 0x18
- #define baud_9600 0x0c
- #define baud_192 0x06
- #define baud_384 0x03
- #define baud_1152 0x01
- //16c552
- #define aComPortBaseAddr (*(volatile unsigned char *)0x00700008)
- #define aRecBuffer (*(volatile unsigned char *)0x00700008)
- #define aTransBuffer (*(volatile unsigned char *)0x00700008)
- #define aInterruptEnable (*(volatile unsigned char *)0x00700009)
- #define aIntIdentify (*(volatile unsigned char *)0x0070000a)
- #define aFIFO_Control (*(volatile unsigned char *)0x0070000a)
- #define aLineControl (*(volatile unsigned char *)0x0070000b)
- #define aModemControl (*(volatile unsigned char *)0x0070000c)
- #define aLineStatus (*(volatile unsigned char *)0x0070000d)
- #define aModemStatus (*(volatile unsigned char *)0x0070000e)
- #define aLSB_DivisorLatch (*(volatile unsigned char *)0x00700008)
- #define aMSB_DivisorLatch (*(volatile unsigned char *)0x00700009)
- //com0
- #define COM0_USBRGR (*(volatile unsigned int *)0xFFFD0020)
- #define COM0_USMR (*(volatile unsigned int *)0xFFFD0004)
- #define COM0_USCR (*(volatile unsigned int *)0xFFFD0000)
- #define COM0_USCSR (*(volatile unsigned int *)0xFFFD0014)
- #define COM0_RHR (*(volatile unsigned char *)0xFFFD0018)
- #define COM0_THR (*(volatile unsigned char *)0xFFFD001c)
- struct comm_buf_fm PortA;
- struct comm_buf_fm Comm0;
- void UART_init(unsigned int port,unsigned int baud)
- {
- unsigned int UART_BASE=port?UART1_BASE:UART0_BASE;
- *(volatile unsigned *)(PIO_PDR)=0xE000; //pio disable
- *(volatile unsigned *)(UART_BASE+USBRGR)=baud;
- *(volatile unsigned *)(UART_BASE+USCR) = 0x15c;
- *(volatile unsigned *)(UART_BASE+USMR) = 0x8c0; //8bit,1 stop bit,no parity
- // *(volatile unsigned *)(UART_BASE+UCON) = 0x09; //Enable Interrupt
- *(volatile unsigned *)(UART_BASE+USCR) = 0x050;
- }
- /*
- void UART_trans(unsigned port,char d)
- {
- unsigned int UART_BASE=port?UART1_BASE:UART0_BASE;
- while((GET_USCSR(UART_BASE) & 0x2)!=0X2);
- SENDD(UART_BASE,d);
- }
- char UART_recev(unsigned port)
- {
- int i;
- unsigned int UART_BASE=port?UART1_BASE:UART0_BASE;
- for(i=0;i<1000;i++)
- {
- if((GET_USCSR(UART_BASE) & 0x1)==0x1)
- {
- break;
- }
- }
- return RECVD(UART_BASE);
- }
- void UART_send(unsigned port,char *buf,int BufLen)
- {
- unsigned int UART_BASE=port?UART1_BASE:UART0_BASE;
- int i=0;
- while(i<BufLen)
- {
- while((GET_USCSR(UART_BASE) & 0x2)!=0X2);
- SENDD(UART_BASE,buf[i++]);
- }
- }
- int UART_get(unsigned port,char *buf,int BufLen)
- {
- int count=0;
- unsigned int UART_BASE=port?UART1_BASE:UART0_BASE;
- int status=1;
- for(count=0;count<BufLen;count++)
- {
- while(!(status=(GET_USCSR(UART_BASE) & 0x1)));
- if(status)
- buf[count]=RECVD(UART_BASE);
- else
- buf[count]=0;
- }
- return count;
- }
- void UART_pdcsend(unsigned port,char *buf,int BufLen)
- {
- unsigned int UART_BASE=port?UART1_BASE:UART0_BASE;
- while((GET_USCSR(UART_BASE) & 0x12)!=0X12);
- *(volatile unsigned *)(UART_BASE+USTPR)=buf;
- *(volatile unsigned *)(UART_BASE+USTCR)=BufLen;
- }*/
- //422
- void UART422_init(unsigned char baud)
- {
- aLineControl = 0x80; /*Enable Set Baudrate*/
- aLSB_DivisorLatch = baud; /*Set Baudrate*/
- aMSB_DivisorLatch = 0;
- aLineControl = 0x03;
- aFIFO_Control = 0xc9; /* enable FIFO */
- aInterruptEnable = 0x00;
- }
- void UART422_server(void)
- {
- unsigned char temp;
- if( aLineStatus & 0x1E )
- {
- temp = aRecBuffer;
- }
- if( aLineStatus & 0x01 )
- {
- PortA.RecvBuf[PortA.RecvSet] = aRecBuffer;
- PortA.RecvSet++;
- if((PortA.RecvSet) >= RECVNUM)
- PortA.RecvSet = 0;
- //PortA.RecvFlag = 1;
- }
- if( aLineStatus & 0x20 )
- {
- if( PortA.SendSet != PortA.SendGet )
- {
- aTransBuffer = PortA.SendBuf[PortA.SendGet];
- PortA.SendBuf[PortA.SendGet] = 0;
- PortA.SendGet++;
- if( (PortA.SendGet) >= SENDNUM )
- PortA.SendGet = 0;
- //PortA.SendFlag = 1;
- }
- }
- }
- void Comm0Server(void)
- {
- // unsigned char temp;
- /* if( COM0_USCSR & 0x1E0 )
- {
- temp = COM0_RHR;
- }
- */
- if( (COM0_USCSR & 0x1) )
- {
- Comm0.RecvBuf[Comm0.RecvSet] = COM0_RHR;
- Comm0.RecvSet++;
- if((Comm0.RecvSet) >= RECVNUM)
- Comm0.RecvSet = 0;
- //Comm0.RecvFlag = 1;
- }
- if( (COM0_USCSR & 0x2) )
- {
- if( Comm0.SendSet != Comm0.SendGet )
- {
- COM0_THR = Comm0.SendBuf[Comm0.SendGet];
- Comm0.SendBuf[Comm0.SendGet]=0;
- Comm0.SendGet++;
- if( (Comm0.SendGet) >= SENDNUM )
- Comm0.SendGet = 0;
- //Comm0.SendFlag = 1;
- }
- }
- }
- void InitCommBuf(void)
- {
- PortA.RecvGet = PortA.RecvSet = 0;
- Comm0.RecvGet = Comm0.RecvSet = 0;
- UART422_init(baud_9600);
- UART_init(0,208);
- }
- void CommRun(void)
- {
- UART422_server();
- Comm0Server();
- if((PortA.RecvSet) >= RECVNUM)
- PortA.RecvSet = 0;
- if((PortA.RecvGet) >= RECVNUM)
- PortA.RecvGet = 0;
- if((Comm0.RecvSet) >= RECVNUM)
- Comm0.RecvSet = 0;
- if((Comm0.RecvGet) >= RECVNUM)
- Comm0.RecvGet = 0;
- }