📄 lpc_uart.c
字号:
/***********************************************************************
* BU MMS China, Philips Semiconductor Software Support
* Embest info&Tech Co. Software Support
*---------------------------------------------------------------------------
* The software is delivered "AS IS" without warranty or condition of any
* kind, either express, implied or statutory. Everybody can use it as
* it is opened and without copyright. We will not take any law responsibility
* for any problem produced by using this software.
*---------------------------------------------------------------------------
* File name: LPC_Uart.c
* Description: Define API for UART
*
* History:
* 1. Date: Jul 13, 2004
* Author: Shawn Zhang
* Description: Create
*
* 2. Date: Aug 09, 2004
* Author: Shawn Zhang
* Description: Integrate two method :- by polling and by interrupt, user
* could select it.
*
* 3. Date: Aug 17, 2004
* Author: Shawn Zhang
* Description: 1) Repair the bug about Tx ring buffer.
* 2) Repair the bug about ReadTxBuf function and ISR.
*
* 4. Date: Oct 29, 2004
* Author: Shawn Zhang
* Description: 1) Use LPC_UartChannel_t struct
* 2) Modify the interrupt receive characters mechanism
* 3) Repair some bugs and optimize the code
*
* $Revision: 1.0 $
**********************************************************************/
#include "LPC_Uart.h"
// Globe variable
LPC_Uart_Config_t Uart0Config, Uart1Config;
/*************************************************************************
* Function Name: UART_InitUartBuf
* Parameters: LPC_Uart_Buffer_t *pUartBuf
* Return: void
*
* Description: Initialize Rx & Tx buffer, which is used when adopting interrupt method
*
*************************************************************************/
static void UART_InitUartBuf (LPC_Uart_Buffer_t *pUartBuf)
{
int i;
for(i=0; i<UART_RXBUFSIZE; i++)
pUartBuf->RxBuf[i] = 0;
for(i=0; i<UART_TXBUFSIZE; i++)
pUartBuf->TxBuf[i] = 0;
pUartBuf->RxHeadPoint=0;
pUartBuf->RxTailPoint=0;
pUartBuf->TxHeadPoint=0;
pUartBuf->TxTailPoint=0;
pUartBuf->RxCount=0;
pUartBuf->TxCount=0;
pUartBuf->RxFlag=0;
}
/*************************************************************************
* Function Name: UART_Init
* Parameters: LPC_Uart_Channel_t DevNum
* unsigned long BaudRate
* LPC_Uart_WordLenth_t WordLenth
* bool TwoStopBitsSelect
* bool ParityEnable;
* LPC_Uart_ParitySelect_t ParitySelect
* bool BreakEnable
* bool FIFOEnable
* int FIFORxTriggerLevel
* bool IntEnable
* unsigned long IntType
*
* Return: int
* 0: sucess
* 1: fail
* Description: Initialize Uart, configure baut rate, frame format and FIFO
*
*************************************************************************/
int UART_Init (LPC_Uart_Channel_t DevNum, unsigned long BaudRate,
LPC_Uart_WordLenth_t WordLenth, bool TwoStopBitsSelect,
bool ParityEnable, LPC_Uart_ParitySelect_t ParitySelect,
bool BreakEnable, bool FIFOEnable, LPC_Uart_FIFORxTriggerLevel_t FIFORxTriggerLevel,
bool IntEnable, unsigned long IntType)
{
unsigned long Divisor, Frame, FIFO;
LPC_Uart_Config_t *pConfig;
if ( DevNum == UART0 )
pConfig = &Uart0Config;
else if (DevNum == UART1)
pConfig = &Uart1Config;
else
return 1;
// Valid Check
if ((BaudRate == 0) || (BaudRate > BD115200))
return 0;
// Copy parameter to global variable
pConfig->BaudRate = BaudRate;
pConfig->WordLenth = WordLenth;
pConfig->TwoStopBitsSelect = TwoStopBitsSelect;
pConfig->ParityEnable = ParityEnable;
pConfig->BreakEnable = BreakEnable;
pConfig->FIFOEnable = FIFOEnable;
pConfig->IntEnable = IntEnable;
// baut rate
Divisor = (SYS_GetFpclk() >>4) / BaudRate; // Divisor = pclk / (16*bautrate)
// frame format
Frame = WordLenth;
if ( TwoStopBitsSelect )
Frame |= ( 1<<LCR_STOPBITSEL_BIT );
if ( ParityEnable )
{
pConfig->ParitySelect = ParitySelect;
Frame |= ( 1<<LCR_PARITYENBALE_BIT );
Frame |= ( ParitySelect<<LCR_PARITYSEL_BIT );
}
if ( BreakEnable )
Frame |= ( 1<<LCR_BREAKCONTROL_BIT );
// FIFO
if ( FIFOEnable )
{
pConfig->FIFORxTriggerLevel = FIFORxTriggerLevel;
FIFO = ((FIFORxTriggerLevel & 0x3)<<6) | 0x1;
}
// Method Mode
if ( IntEnable )
{
pConfig->IntType = IntType;
UART_InitUartBuf (&pConfig->UartBuffer);
}
if ( DevNum == UART0 )
{
/* Set baut rate */
UART0_LCR |= (1<<LCR_DLAB_BIT); // DLAB = 1
UART0_DLM = Divisor>>8;
UART0_DLL = Divisor & 0xff;
/* Set frame format */
UART0_LCR = Frame; // DLAB =0
/* Set FIFO */
UART0_FCR = FIFO;
/* Set Interrupt Enable Register */
UART0_IER = IntType & 0x7;
/* Enable TxD0 and RxD0, bit 0~3=0101 */
PINSEL0 &= ~0xF;
PINSEL0 |= 0x5;
}
else // UART1
{
/* Set baut rate */
UART1_LCR |= (1<<LCR_DLAB_BIT); // DLAB = 1
UART1_DLM = Divisor>>8;
UART1_DLL = Divisor & 0xff;
/* Set frame format */
UART1_LCR = Frame; // DLAB =0
/* Set FIFO */
UART1_FCR = FIFO;
/* Set Interrupt Enable Register */
UART1_IER = IntType & 0x7;
/* Enable TxD0 and RxD0, bit 16~19=0101 */
PINSEL0 &= ~0xF0000;
PINSEL0 |= 0x50000;
}
return 0;
}
/*************************************************************************
* Function Name: UART_PutCharByPolling
* Parameters: LPC_UartChannel_t DevNum
* char ch
* Return: void
*
* Description: Send character by polling LSR register
*
*************************************************************************/
static void UART_PutCharByPolling (LPC_Uart_Channel_t DevNum, char ch)
{
if ( DevNum == UART0 )
{
while(!(UART0_LSR & (1<<LSR_THRE_BIT)));
UART0_THR = ch;
}
else // UART1
{
while(!(UART1_LSR & (1<<LSR_THRE_BIT)));
UART1_THR = ch;
}
}
/*************************************************************************
* Function Name: UART_PutStringByPolling
* Parameters: LPC_UartChannel_t DevNum
* char *Buf
* Return: int : send character count
*
* Description: Send a string by using polling method
*
*************************************************************************/
int UART_PutStringByPolling(LPC_Uart_Channel_t DevNum, char *Buf)
{
char *pBuf = Buf ;
int SendCount = 0;
while (*pBuf)
{
UART_PutCharByPolling(DevNum, *pBuf++);
++SendCount;
}
return SendCount;
}
/*************************************************************************
* Function Name: UART_GetCharByPolling
* Parameters: LPC_UartChannel_t DevNum
* Return: char
*
* Description: Receive a character from Uart by polling LSR register
*
*************************************************************************/
static char UART_GetCharByPolling (LPC_Uart_Channel_t DevNum)
{
if ( DevNum == UART0 )
{
while (!(UART0_LSR & (1<<LSR_RDR_BIT)));
return (UART0_RBR);
}
else //UART1
{
while (!(UART1_LSR & (1<<LSR_RDR_BIT)));
return (UART1_RBR);
}
}
/*************************************************************************
* Function Name: UART_PutStringByInterrupt
* Parameters: LPC_UartChannel_t DevNum
* char *Buf
* Return: int : send character count
*
* Description: Send a string into Uart Buffer.
* Then Uart will send buffer data in its IRQ subroutine.
*
*************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -