⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uart.c

📁 nxp的LPC2888处理器的示例代码.
💻 C
字号:
/*****************************************************************************
 *   uart.c:  UART API file for NXP LPC288x Family Microprocessors
 *
 *   Copyright(C) 2006, NXP Semiconductor
 *   All rights reserved.
 *
 *   History
 *   2006.07.12  ver 1.00    Prelimnary version, first Release
 *
******************************************************************************/
#include "LPC288x.h"                        /* LPC288x definitions */
#include "type.h"
#include "target.h"
#include "irq.h"
#include "uart.h"

volatile DWORD UART0Status;
volatile BYTE UART0TxEmpty = 1;
volatile BYTE UART0Buffer[BUFSIZE];
volatile DWORD UART0Count = 0;

/*****************************************************************************
** Function name:		UART_ISR
**
** Descriptions:		UART interrupt handler
**
** parameters:			None
** Returned value:		None
** 
*****************************************************************************/
void UART_ISR(void) 
{
  BYTE IIRValue, LSRValue;
  BYTE Dummy;
	
  IIRValue = UART_IIR;
    
  IIRValue >>= 1;			/* skip pending bit in IIR */
  IIRValue &= 0x07;			/* check bit 1~3, interrupt identification */
  if ( IIRValue == IIR_RLS )		/* Receive Line Status */
  {
	LSRValue = UART_LSR;
	/* Receive Line Status */
	if ( LSRValue & (LSR_OE|LSR_PE|LSR_FE|LSR_RXFE|LSR_BI) )
	{
	  /* There are errors or break interrupt */
	  /* Read LSR will clear the interrupt */
	  UART0Status = LSRValue;
	  Dummy = UART_RBR;		/* Dummy read on RX to clear 
							interrupt, then bail out */
	  return;
	}
	if ( LSRValue & LSR_RDR )	/* Receive Data Ready */			
	{
	  /* If no error on RLS, normal ready, save into the data buffer. */
	  /* Note: read RBR will clear the interrupt */
	  UART0Buffer[UART0Count] = UART_RBR;
	  UART0Count++;
	  if ( UART0Count == BUFSIZE )
	  {
		UART0Count = 0;		/* buffer overflow */
	  }	
	}
  }
  else if ( IIRValue == IIR_RDA )	/* Receive Data Available */
  {
	/* Receive Data Available */
	UART0Buffer[UART0Count] = UART_RBR;
	UART0Count++;
	if ( UART0Count == BUFSIZE )
	{
	  UART0Count = 0;		/* buffer overflow */
	}
  }
  else if ( IIRValue == IIR_CTI )	/* Character timeout indicator */
  {
	/* Character Time-out indicator */
	UART0Status |= 0x100;		/* Bit 9 as the CTI error */
  }
  else if ( IIRValue == IIR_THRE )	/* THRE, transmit holding register empty */
  {
	/* THRE interrupt */
	LSRValue = UART_LSR;		/* Check status in the LSR to see if
								valid data in U0THR or not */
	if ( LSRValue & LSR_THRE )
	{
	  UART0TxEmpty = 1;
	}
	else
	{
	  UART0TxEmpty = 0;
	}
  }
  return;
}

/*****************************************************************************
** Function name:		UARTInit
**
** Descriptions:		Initialize UART0 port, setup pin select,
**						clock, parity, stop bits, FIFO, etc.
**
** parameters:			UART baudrate
** Returned value:		true or false, return false only if the 
**						interrupt handler can't be installed to the 
**						VIC table
** 
*****************************************************************************/
DWORD UARTInit( DWORD baudrate )
{
  DWORD Fdiv;

  UART_FCR = 0x07;
  UART_LCR = 0x83;

#if 0
  /* Please note, by default, the Fractional Divider doesn't
  impact the baudrate, so, the PCLK going to the UART is the same
  as CCLK which is 60Mhz, the PCLK through Fractional Divider to 
  the UART is 30Mhz, see comments in target.c file, so, Fpclk defined 
  in the target.h should be adjusted accordingly. */  
  /* If UART_FDR is set to 0x11, the Fpclk is 30Mhz, if UART_FCR is
  set to 0x10(power on reset value), the Fpclk is 60Mhz. */
  UART_FDR = 0x11;
#endif

  Fdiv = ( Fpclk / 16 ) / baudrate ;	/*baud rate */
  UART_DLM = Fdiv / 256;							
  UART_DLL = Fdiv % 256;
  UART_LCR = 0x03;

  /* If DMA is used, UART interrupt will not be used. */
#if !UART_DMA_ENABLE
  if ( install_IRQ(12, 1, UART_ISR ) == FALSE )
  {
	return( FALSE );
  }
  INT_REQ12=(1<<28)|(1<<27)|(1<<26)|(1<<16)|0x1;
  INT_VECTOR0=IRQ_TABLE_BASE & MASK_INDEX;
  UART_IER = IER_RBR | IER_THRE | IER_RLS;	/* Enable UART0 interrupt */
#endif
  return (TRUE);
}

/*****************************************************************************
** Function name:		UARTSend
**
** Descriptions:		Send a block of data to the UART 0 port based
**				on the data length
**
** parameters:			buffer pointer, and data length
** Returned value:		None
** 
*****************************************************************************/
void UARTSend(BYTE *BufferPtr, DWORD Length )
{
  while ( Length != 0 )
  {
	/* THRE status, contain valid data */
	while ( !(UART0TxEmpty & 0x01) );
	UART_THR = *BufferPtr;
	UART0TxEmpty = 0;	/* not empty in the THR until it shifts out */
	BufferPtr++;
	Length--;
  }
  return;
}

/******************************************************************************
**                            End Of File
******************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -