📄 lib_lpc_uart.c
字号:
/*
***********************************************************************
Embest Info&Tech Co., Ltd. All rights reserved.
www.embedinfo.com
***********************************************************************
---------------- file information -------------------------------------
file name: lib_lpc_uart.c
version : v0
author : peter pan / panqan@hotmail.com
panqian@embedinfo.com
zhangxiuquan
begin : 2006-02-10
finish : 2006-02-10
define : uart init and operation lib
notes :
---------------- modify information -----------------------------------
version :
modify :
begin :
finish :
define :
-----------------------------------------------------------------------
*/
/*-------------------------------------------------------------------*/
/* include files */
/*---------------------------------------------------------------- --*/
#include "lib_lpc_uart.h"
/*-------------------------------------------------------------------*/
/* local function declare */
/*-------------------------------------------------------------------*/
/*-------------------------------------------------------------------*/
/* extern function/variable declare */
/*-------------------------------------------------------------------*/
/*-------------------------------------------------------------------*/
/* global variable define */
/*-------------------------------------------------------------------*/
/*-------------------------------------------------------------------*/
/* function code */
/*-------------------------------------------------------------------*/
/**********************************************************************
* name : uart_io_en(uart_channel uart_channel_01)
* func : enable the port to uart output function
* para : xxxx
* ret : xxxx
* glob : xxxx
* inc :
* author:
* date :
* modify:
* comment:
**********************************************************************/
void uart_io_en(uart_channel uart_channel_01)
{
INT32U i;
if (uart_c0 == uart_channel_01)
{ // uart0
i=PINSEL0; //select the pin to uart0
i&=(~UART0_EN_PINSEL0_M);
i|=UART0_EN_PINSEL0_V;
PINSEL0=i;
}
else
{ // uart1
i=PINSEL0; //select the pin to uart1
i&=(~UART1_EN_PINSEL0_M);
i|=UART1_EN_PINSEL0_V;
PINSEL0=i;
}
}
/**********************************************************************
* name : uart_init()
* func : init the uart part
* para : xxxx
* ret : xxxx
* glob : xxxx
* inc :
* author:
* date :
* modify:
* comment:
**********************************************************************/
INT32U uart_init(uart_channel uart_channel_01, INT32U uart_baudrate,
word_length_v uart_word_length, stop_bit_v uart_spb,
parity_generation_en_v uart_pg, parity_select_v uart_ps,
break_transmission_v uart_btr, fifo_en_v uart_fifo,
INT32U int_set_v
)
{
INT32U i,j,k;
INT32U baudrate_div;
INT32U lcr_value;
// set port to uart connect mode
uart_io_en(uart_channel_01);
//check the baudrate
if ( (0==uart_baudrate) || (uart_baudrate > 115200) )
return 0;
//calculate the value of LCR
lcr_value = uart_word_length;
lcr_value &= (~STOP_BIT_SEL_U0LCR_M);
lcr_value |= (uart_spb<<2);
lcr_value &= (~PARITY_ENBALE_U0LCR_M);
lcr_value |= (uart_pg<<3);
lcr_value &= (~PARITY_SEL_U0LCR_M);
lcr_value |= (uart_ps<<4);
lcr_value &= (~BREAK_CONTROL_U0LCR_M);
lcr_value |= (uart_btr<<6);
// DLAB=0
lcr_value &= (~DLAB_U0LCR_M);
//calculate the baudrate_div
i = Fpclk;
i = PCLKF; //why and what!!!!!!!!!!
baudrate_div = (i>>4) / uart_baudrate;
if (uart_channel_01==uart_c0)
{
//set the baudrate
UART0_LCR |= DLAB_U0LCR_M; //EN DLAB
UART0_DLM = baudrate_div >> 8;
UART0_DLL = baudrate_div & 0xff;
//set LCR
UART0_LCR = lcr_value;
//set IER
UART0_IER = int_set_v;
//set fifo
UART0_FCR = uart_fifo;
i=UART0_RBR; //clear the Recive buffer
}
else
{
// calculate the baudrate_div
// i = Fpclk;
// baudrate_div = (i>>4) / uart_baudrate;
UART1_LCR |= DLAB_U1LCR_M; //EN DLAB
UART1_DLM = baudrate_div >> 8;
UART1_DLL = baudrate_div & 0xff;
//set LCR
UART1_LCR = lcr_value;
//set IER
UART1_IER = int_set_v;
//set fifo
UART1_FCR = uart_fifo;
i=UART1_RBR; //clear the Recive buffer
}
// open 232 transform output enable
if (uart_c0==uart_channel_01)
{
spi_extend_set(ext_device_on, module_c_U0_EN);
}
else
{
spi_extend_set(ext_device_on, module_c_U1_EN);
}
}
/**********************************************************************
* name : uart_send_char()
* func : send the 8bit data
* para : uart channel
data
* ret : xxxx
* glob : xxxx
* inc :
* author:
* date :
* modify:
* comment:
**********************************************************************/
void uart_send_char(uart_channel uart_channel_01, INT8U data)
{
if (uart_c0==uart_channel_01)
{
if(data=='\n') //if data is '\n',change line and to begin of the line
uart_send_char(uart_c0, '\r');
UART0_THR=data;
while ((UART0_LSR&0x20)==0); //wait the THR for empty
}
else
{
if(data=='\n') //if data is '\n',change line and to begin of the line
uart_send_char(uart_c1, '\r');
UART1_THR=data;
while ((UART1_LSR&0x20)==0); //wait the THR for empty
}
}
/**********************************************************************
* name : uart_send_string(uart_channel uart_channel_01, INT8U *str)
* func : send the string data
* para :
point str
* ret : xxxx
* glob : xxxx
* inc :
* author:
* date :
* modify:
* comment:
**********************************************************************/
void uart_send_string(uart_channel uart_channel_01, INT8U *str)
{
INT8U *sstr=str;
if (uart_c0==uart_channel_01)
{
while(0!=*sstr)
uart_send_char(uart_c0,*sstr++);
}
else
{
while(0!=*sstr)
uart_send_char(uart_c1,*sstr++);
}
}
/**********************************************************************
* name : uart_get_char()
* func : get the 8bit data
* para : uart channel
* ret : get INT8U data
* glob : xxxx
* inc :
* author:
* date :
* modify:
* comment:
**********************************************************************/
INT8U uart_get_char(uart_channel uart_channel_01)
{
if (uart_c0==uart_channel_01)
{
while((UART0_LSR&0x01)==0); // wait recive data ready
return UART0_RBR;
}
else
{
while((UART1_LSR&0x01)==0); // wait recive data ready
return UART1_RBR;
}
}
/**********************************************************************
* name : uart_get_key(uart_channel uart_channel_01)
* func : get the input key
* para : uart channel
* ret : INT8U data
* glob : xxxx
* inc :
* author:
* date :
* modify:
* comment:
**********************************************************************/
INT8U uart_get_key(uart_channel uart_channel_01)
{
if (uart_c0==uart_channel_01)
{
if((UART0_LSR&0x01)!=0)
return UART0_RBR;
else
return 0;
}
else
{
if((UART1_LSR&0x01)!=0)
return UART1_RBR;
else
return 0;
}
}
/**********************************************************************
* name : uart_get_string(uart_channel uart_channel_01, INT8U *ss)
* func : get the input string
* para : uart channel
* ret : *ss
* glob : input_string[128](char)
* inc :
* author:
* date :
* modify:
* comment:
**********************************************************************/
void uart_get_string(uart_channel uart_channel_01, INT8U *char_top)
{
INT8U char_temp_v;
INT8U *char_input=char_top;
INT32U over_check=0;
if (uart_c0==uart_channel_01)
{
for(;;)
{
//get char
char_temp_v = uart_get_char(uart_c0);
//check control edit
if ((char_temp_v == '\b')&&((INT32U)char_input>(INT32U)char_top))
{
uart_send_char(uart_c0, '\b');
uart_send_char(uart_c0, ' ');
*char_input--;
over_check--;
}
else
{
*char_input = char_temp_v;
*char_input++;
over_check++;
if (over_check>254) //over
break;
}
//check control end
if (char_temp_v == '\r')
break;
//send char by uart
uart_send_char(uart_c0, char_temp_v);
}
*char_input='\0';
uart_send_char(uart_c0, '\n');
//for(;;);
}
else
{
}
}
/**********************************************************************
* name : uart_printf(uart_channel uart_channel_01, INT8U *ss,...)
* func : get the input string
* para : uart channel
*ss
...
* ret :
* glob :
* inc :
* author:
* date :
* modify:
* comment:
**********************************************************************/
void uart_printf(uart_channel uart_channel_01, INT8U *fmt,...)
{
if (uart_c0==uart_channel_01)
{
va_list ap;
INT8U string[128];
va_start(ap,fmt); //correspond to va_end func
vsprintf(string,fmt,ap); //variable function
uart_send_string(uart_c0, string);
va_end(ap);
}
else
{
va_list ap;
INT8U string[128];
va_start(ap,fmt); //correspond to va_end func
vsprintf(string,fmt,ap); //variable function
uart_send_string(uart_c1, string);
va_end(ap);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -