uart.c
上传用户:yyhongfa
上传日期:2013-01-18
资源大小:267k
文件大小:8k
- /****************************************************************************
- uart.c
- ****************************************************************************/
- #ifndef UART_C
- #define UART_C
- #include "config.h"
- //#include "IAP.h"
- //#include "ffs.h"
- #include "uart.h"
- //#include "main.h"
- u8_t uart0_Buf[MAX_UART0_BUFFER];
- u8_t uart1_Buf[MAX_UART1_BUFFER];
- u16_t p_Uart0Wr, p_Uart1Wr; /*point to the address will be written ,every UART interrupt*/
- u16_t uart0_Useful_Data, uart1_Useful_Data; /*point to the address read , getchar()*/
- BOOL uart0_Interrupt, uart1_Interrupt ;
- /********************************
- UART0 采用中断方式处理
- *******************************/
- void __irq IRQ_UART0(void)
- {
- if(0x04 EQ (U0IIR & 0x0f))
- {
- p_Uart0Wr %= MAX_UART0_BUFFER;
-
- uart0_Buf[p_Uart0Wr++] = U0RBR;
-
- uart0_Useful_Data++;
- if(uart0_Useful_Data > MAX_UART0_BUFFER)
- {
- uart0_Useful_Data = MAX_UART0_BUFFER;/*Because the max userful data is MAX_UART0_BUFFER*/
- }
- uart0_Interrupt = TRUE;
- }
- VICVectAddr = 0x00;
- }
- /****************************************************************************
- * 名称:UART0_Ini()
- * 功能:初始化串口0。设置为8位数据位,1位停止位,
- 无奇偶校验
- * 入口参数:无
- * 出口参数:无
- ****************************************************************************/
- void UART0_Ini(void)
- {
- u16_t i;
- PINSEL0 |= 0x00000005; // 设置I/O连接到UART0
- PINSEL1 &= 0xFFFFFFF5;
-
- U0LCR = 0x83; // DLAB = 1,可设置波特率
- U0DLL = U0LCR_VALUE;
- U0DLM = 0x00;
- U0LCR = 0x03;
-
- U0FCR = 0x01; /*enable FIFO, 1byte*/
- U0IER = 0x01; /*enable RBR interrupt*/
- VICIntSelect = 0x00000000;
- VICVectCntl0 = 0x26;
- VICVectAddr0 = (u32_t)IRQ_UART0;
- VICIntEnable |= 0x00000040;
- for(i = 0; i < MAX_UART0_BUFFER; i++)
- {
- uart0_Buf[i] = 0;
- }
- p_Uart0Wr = 0;
- uart0_Useful_Data = 0;
- }
-
- /********************************
- UART1 采用中断方式处理
- *******************************/
- void __irq IRQ_UART1(void)
- {
- if(0x04 EQ (U1IIR & 0x0f))
- {
- p_Uart1Wr %= MAX_UART1_BUFFER;
- uart1_Buf[p_Uart1Wr++] = U1RBR;
-
- uart1_Useful_Data++;
- if(uart1_Useful_Data > MAX_UART1_BUFFER)
- {
- uart1_Useful_Data = MAX_UART1_BUFFER ;/*Because the max userful data is MAX_UART0_BUFFER*/
- }
- uart1_Interrupt = TRUE;
- }
- VICVectAddr = 0x00;
- }
- /************************************
- uart1 init
- **********************************/
- void UART1_Ini(void)
- {
- u16_t i;
- U1LCR = 0x83; // DLAB = 1,可设置波特率
- U1DLL = U1LCR_VALUE;
- U1DLM = 0x00;
- U1LCR = 0x03;
-
- U1FCR = 0x01;
- U1IER = 0x01;
- VICIntSelect = 0x00000000; /* ALL IRQ Interrupt*/
- VICVectCntl1 = 0x27; /*UART1 to IRQ Slot 1, the second highest priority*/
- VICVectAddr1 = (int)IRQ_UART1;
- VICIntEnable |= 0x00000080;
- PINSEL0 |= 0x00050000;
- PINSEL0 &= 0xfff5ffff;
- for(i = 0; i < MAX_UART1_BUFFER; i++)
- {
- uart1_Buf[i] = 0;
- }
- p_Uart1Wr = 0;
- uart1_Useful_Data = 0;
- }
- /****************************************************************************
- * 名称:sendByte_2_user()
- * 功能:向串口发送字节数据,并等待发送完毕。
- * 入口参数:data 要发送的数据
- * 出口参数:无
- ****************************************************************************/
- void sendByte_2_user(u8_t data)
- { U0THR = data; // 发送数据
- while( (U0LSR&0x40)==0 ); // 等待数据发送完毕
- }
- //UART SEND BYTE
- void sendByte_2_modem(u8_t data)
- {
- #ifdef DEBUG_MODEM_DATA
- const CHAR ascTable[17] = {"0123456789ABCDEF"};
- sendByte_2_user(ascTable[data>>4]);
- sendByte_2_user(ascTable[data&0x0f]);
- sendByte_2_user(' ');
- #endif
- U1THR = data;
- while((U1LSR & 0x40) == 0);
- }
- /****************************************************************************
- * 名称:sendString_2_user()
- * 功能:向串口发送一字符串
- * 入口参数:srt 要发送的字符串的指针
- * 出口参数:无
- ****************************************************************************/
- void sendString_2_user(char *str)
- { while(1)
- { if( *str == ' ' ) break;
- sendByte_2_user(*str++); // 发送数据
- }
- }
- /****************************************************************************
- * 名称:sendString_2_modem()
- * 功能:向串口发送一字符串
- * 入口参数:srt 要发送的字符串的指针
- * 出口参数:无
- ****************************************************************************/
- void sendString_2_modem(char *str)
- { while(1)
- { if( *str == ' ' ) break;
- sendByte_2_modem(*str++); // 发送数据
- }
- }
- /*****************************
- INT16 Uart0_getByte(u8_t *ch)
- *ch : output byte address
- return: -1 no data
- 0 ok
- ******************************/
- INT16 Uart0_getByte(u8_t *ch)
- {
- //DEBUG_FUNCTION("rnUart0_getByte");
-
- uart0_Interrupt = FALSE;
-
- if(uart0_Useful_Data EQ 0)
- {
- return UART_NO_DATA;
- }
- else
- {
- if(uart0_Useful_Data <= p_Uart0Wr)
- {
- *ch = uart0_Buf[p_Uart0Wr - uart0_Useful_Data];
- }
- else
- {
- *ch = uart0_Buf[MAX_UART0_BUFFER - uart0_Useful_Data + p_Uart0Wr];
- }
- if(uart0_Interrupt EQ TRUE)/*interrupt by UART0, so the data just read may be not right*/
- {
- return UART_NO_DATA;
- }
- else
- {
- uart0_Useful_Data--;
- return DATA_READ_OK;
- }
- }
- }
- /*****************************
- INT16 Uart1_getByte(u8_t *ch)
- *ch : output byte address
- return: -1 no data
- 0 ok
- ******************************/
- INT16 Uart1_getByte(u8_t *ch)
- {
- //DEBUG_FUNCTION("rnUart0_getByte");
- uart1_Interrupt = FALSE;
-
- if(uart1_Useful_Data EQ 0)
- {
- return UART_NO_DATA;
- }
- else
- {
- if(uart1_Useful_Data <= p_Uart1Wr)
- {
- *ch = uart1_Buf[p_Uart1Wr - uart1_Useful_Data];
- }
- else
- {
- *ch = uart1_Buf[MAX_UART1_BUFFER - uart1_Useful_Data + p_Uart1Wr];
- }
- if(uart1_Interrupt EQ TRUE)/*interrupt by UART1, so the data just read may be not right*/
- {
- return UART_NO_DATA;
- }
- else
- {
- uart1_Useful_Data--;
- return DATA_READ_OK;
- }
- }
- }
- /**********************************
- return now UART1 useful data length
- *********************************/
- u16_t get_modem_datalen(void)
- {
- return uart1_Useful_Data;
- }
- /****************************
- return UART0 useful data lenght
- ******************************/
- u16_t get_user_datalen(void)
- {
- return uart0_Useful_Data;
- }
- u32_t get_modem_data(u8_t *buf)
- {
- u8_t ch;
- int len = 0;
- #ifdef DEBUG_MODEM_DATA
- const CHAR ascTable[17] = {"0123456789ABCDEF"};
- #endif
-
- while((uart1_Useful_Data > 0) AND (len <= MAX_READ_LEN))
- {
- if(Uart1_getByte(&ch) EQ DATA_READ_OK)
- {
- buf[len++] = ch;
-
- #ifdef DEBUG_MODEM_DATA
- sendByte_2_user(ascTable[ch>>4]);
- sendByte_2_user(ascTable[ch&0x0f]);
- sendByte_2_user(' ');
- #endif
- }
- }
- return len;
- }
- #define MAX_DELAY_TIME 200
- u32_t get_user_data(u8_t *buf)
- {
- u8_t ch;
- u16_t len = 0;
- u32_t time = sys_jiffies();
-
- while((uart0_Useful_Data > 0) OR((sys_jiffies() - time) < MAX_DELAY_TIME))
- {
- if(Uart0_getByte(&ch) EQ DATA_READ_OK)
- {
- buf[len++] = ch;
- time = sys_jiffies();
- }
- if(len > MAX_READ_LEN)
- {
- break;
- }
- }
- return len;
- }
- u32_t writeDate_2_modem(void *device, u8_t *data, u32_t len)
- {
- u32_t temp = len;
-
- while(len > 0)
- {
- sendByte_2_modem(*data++);
- len--;
- }
- return temp;
- }
- u32_t writeDate_2_user( u8_t *data, u32_t len)
- {
- u32_t temp = len;
-
- while(len > 0)
- {
- sendByte_2_user(*data++);
- len--;
- }
- return temp;
- }
- void sio_read_abort(void *pd)
- {
- return;
- }
- #endif