?? drv_uart0.c
字號:
#include "config_60_61.h"
#include "config_GD60.h"
#include "include_GD60.h"
#include "config_GD61.h"
#include "include_GD61.h"
volatile uint8 r_buf0[RX_BUFFER_LEN]; // UART0 receiving buffer
volatile uint8 t_buf0[TX_BUFFER_LEN]; // UART0 sending buffer
extern volatile uint8 r_waittingTime0;
volatile uint8 r_BufferFlag0=0;
volatile uint16 r_inp0=0,r_outp0=0,r_count0=0;
volatile uint16 t_inp0=0,t_outp0=0,t_count0=0;
extern uint8 Comm_Src_Addr;
/////////////////////////////////////////////////////////////////////////
void Uart0Initialize (uint32 baud, UARTMODE set);
void SetDefaultUart0(void);
void __irq IRQ_Uart0Serving (void);
void ReleaseRS485TE0(void);
void Uart0SendOver(void);
void Uart0SendChar(char *send_pt,uint8 send_cnt);
void Uart0SendString(char *send_pt);
void Uart0SendAddCrc(uint8 send_cnt);
void Uart0SendEnter(void);
uint16 MB_CRC16Check(uint8 *puchMsg, uint8 usDataLen);
char _putchar(char ch);
char _getchar(void);
void Uart0SendAdd_Crc(uint8 *send_pt,uint8 send_cnt);
/*
*************************************************************************
** 函數名稱 :Uart0Initialize()
** 函數功能 :初始化串口: 設置工作模式和波特率。
** 入口參數 :baud 波特率
** : set 模式設置(UARTMODE數據結構)
** 出口參數 :返回1表示成功,0表示參數出錯。
status : ok
*************************************************************************
*/
void Uart0Initialize (uint32 baud, UARTMODE set)
{
uint32 bak;
IRQDisable();
PINSEL0 = (PINSEL0&0xfffffff0)|0x00000005;
// 參數過濾
if ((0 == baud) || (baud > 115200)) return ;
if ((set.datab < 5) || (set.datab > 8)) return ;
if ((0 == set.stopb) || (set.stopb > 2)) return ;
if (set.parity > 4) return ;
// 設置串口波特率
U0LCR = 0x80; // DLAB=1
bak = (LPC_FPCLK >> 4) / baud;
U0DLM = bak >> 8;
U0DLL = bak & 0xff;
// 設置串口模式
bak = set.datab - 5;
if (2 == set.stopb) bak |= 0x04;
if (0 != set.parity)
{
set.parity = set.parity - 1;
bak |= 0x08;
}
bak |= set.parity << 4;
U0LCR = bak;
U0FCR = 0x81; // 使能FIFO,并設置觸發點為8字節
U0IER = 0x03; // 允許UART中斷, receive and sending int
/* 使能UART0中斷 */
VICVectCntl0 = 0x20|INT_UART0; // UART0分配到IRQ slot0,即最高優先級
VICVectAddr0 = (uint32)IRQ_Uart0Serving; // 設置UART0向量地址
VICIntEnable = 1<<INT_UART0; // 使能UART0中斷
VICVectAddr = 0x00; // 中斷處理結束
bak=U0RBR; // 讀取FIFO的數據,并清除中斷
IRQEnable(); // 使能IRQ中斷
}
/*****************************************************************************/
void SetDefaultUart0(void)
{
UARTMODE uart0Set;
uart0Set.datab = 8;
uart0Set.stopb = 1;
uart0Set.parity = 0;
Uart0Initialize(UART0_BAND_RATE, uart0Set);
}
/*
********************************************************************************
** 函數名稱 :IRQ_Uart0Serving()
** 函數功能 :串口0接收中斷服務程序
** 入口參數 :無
** 出口參數 :無
** status ok
********************************************************************************
*/
void __irq IRQ_Uart0Serving (void)
{
uint8 buf;
#ifdef UART_FAST_MODE
uint8 i;
#endif
buf=U0IIR; //read interrupt identification register
while((buf&0x01)==0x0){ //bit0=0 indicates that an interrupt occur
buf&=0x0f; //get u0iir[3:0]
switch(buf){
////line error
case 0x06: //RLS interrupt, line error occur
buf=U0LSR;
break;
////receiving data
case 0x04: //RDA interrupt
case 0x0c: //CTI interrupt
buf=U0LSR; //
while((buf&0x01)!=0x0){ //uxlsr_bit0 indicates data in uxrbr available
if(r_count0==0) r_outp0=r_inp0;
r_buf0[r_inp0]=U0RBR; //讀取FIFO的數據,并清除中斷
r_inp0++; if(r_inp0>=RX_BUFFER_LEN) r_inp0=0;
r_count0++;
buf=U0LSR; //more ?
r_BufferFlag0 = _DATA_OK;
r_waittingTime0 = 5;
}
break;
////sending data
case 0x02: //THRE interrupt
buf=U0LSR;
#ifndef UART_FAST_MODE
while(((buf&0x20)==0x20)&&(t_count0!=0)){ //check the THRE empty?
IO1CLR=RS485TE0_P1o; //set it sending mode
U0THR = t_buf0[t_outp0]; // 發送數據 here
t_outp0++; if(t_outp0>=TX_BUFFER_LEN) t_outp0=0;
t_count0--;
buf=U0LSR;
}
#else
i=0x5a;
if(((buf&0x20)==0x20)&&(t_count0!=0)){ //check the THRE empty?
IO1CLR=RS485TE0_P1o; // set it sending mode
for(i=0;((i<12)&&(t_count0!=0));i++){ //fifo depth is 16
U0THR = t_buf0[t_outp0]; // 發送數據 here
t_outp0++; if(t_outp0>=TX_BUFFER_LEN) t_outp0=0;
t_count0--;
// if(t_count0==0) break;
}
}
#endif
if(t_count0==0){
t_outp0=t_inp0; //all has been transmited
// if(i==0x5a){ IO1SET = RS485TE0_P1o; }
}
break;
////default condition
default:
break;
}
buf=U0IIR; //check the other case
}
VICVectAddr = 0x00; // 中斷處理結束
}
////////////////////////////////////////////////////////////////////////////////
//sending anything to host without ending flag(such as CRC or ENTER)
//state :ok
////////////////////////////////////////////////////////////////////////////////
void ReleaseRS485TE0(void)
{
if(((IO1PIN&RS485TE0_P1o)==0x0000)&&(t_count0==0)){
if((U0LSR&0x40)==0x40){ //sending over
DelayUS_(50);
IO1SET=RS485TE0_P1o; //set it receiving mode
}
}
}
////////////////////////////////////////////////////////////////////////////////
//sending anything to host without ending flag(such as CRC or ENTER)
//state :ok
////////////////////////////////////////////////////////////////////////////////
void Uart0SendOver(void)
{
while(((U0LSR&0x20)==0)||(t_count0!=0)); // 等待數據發送完畢
}
////////////////////////////////////////////////////////////////////////////////
//sending anything to host without ending flag(such as CRC or ENTER)
//state :ok
////////////////////////////////////////////////////////////////////////////////
void Uart0SendChar(char *send_pt,uint8 send_cnt)
{
while((t_count0!=0)||((U0LSR&0x20)==0)); //wait until t_buff empty
IRQDisable();
do
{
t_buf0[t_inp0]=*send_pt;
t_inp0++; if(t_inp0>=TX_BUFFER_LEN) t_inp0=0; //elarge the sending buffer
send_pt++;
t_count0++;
send_cnt--;
}while(send_cnt>0); // copy the string to t_buffer
IRQEnable(); // 使能IRQ中斷
IO1CLR=RS485TE0_P1o; //set it sending mode
DelayUS_(1000000/UART0_BAND_RATE);
U0THR = t_buf0[t_outp0]; //initiate sending
t_outp0++; if(t_outp0>=TX_BUFFER_LEN) t_outp0=0;
t_count0--;
}
//////////////////////////////////////////////////////////////////////////////
//sending string ending with ENTER to computer or host
//state :ok
//////////////////////////////////////////////////////////////////////////////
void Uart0SendString(char *send_pt)
{
while((t_count0!=0)||((U0LSR&0x20)==0)); //wait until t_buff empty
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -