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

📄 uart.c

📁 称植到lpc2124上的UCOS2.85版 是本人初学移植和ARM的成果 可能已有人上传类似的了
💻 C
字号:
/*****************************************************************************
 *   uart.c:  UART API file for Philips LPC214x Family Microprocessors
 *
 *   Copyright(C) 2006, Philips Semiconductor
 *   All rights reserved.
 *
 *   History
 *   2005.10.01  ver 1.00    Prelimnary version, first Release
 *
******************************************************************************/

#include "target.h"


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

/*****************************************************************************
** Function name:		UART0Handler
**
** Descriptions:		UART0 interrupt handler
**
** parameters:			None
** Returned value:		None
** 
*****************************************************************************/
void UART0_Exception  (void)  
{
#if OS_CRITICAL_METHOD == 3      
    OS_CPU_SR  cpu_sr;
#endif

    BYTE IIRValue, LSRValue;
    BYTE Dummy;

    OS_ENTER_CRITICAL();  
	
    IIRValue = U0IIR;			/* 读取中断标示寄存器,清除中断 */
    IIRValue >>= 1;			    /* 跳过中断挂起位 */
    IIRValue &= 0x07;			/* 检测1-3位,即中断标示 */

    if ( IIRValue == IIR_RLS )  /* 接收线状态中断 */
    {	
	    /* 读LSR清除中断 */
	    LSRValue = U0LSR;		/* 读取线状态寄存器 */
	    if ( LSRValue & (LSR_OE|LSR_PE|LSR_FE|LSR_RXFE|LSR_BI) ) /* 错误发生 */
	   {
			OS_EXIT_CRITICAL() ;
	        VICVectAddr = 0; /* 允许下次中断 */
	        return;
	   }
	   if ( LSRValue & LSR_RDR ) /* 接收数据就绪 */			
	   {
	       /* 读取RBR清除中断 */
	       UART0Buffer[UART0Count] = U0RBR; /* 读取接收到的数据 */
	       UART0Count++;
	       if ( UART0Count == BUFSIZE ) /* 缓冲器满 */
	       {
		       UART0Count = 0;	
	       }	
	   }
    }
    else if ( IIRValue == IIR_RDA )	/* 接收数据可用 */
   {	 
	         UART0Buffer[UART0Count] = U0RBR; /* 读取接收到的数据 */
    	     UART0Count++;
    	     if ( UART0Count == BUFSIZE ) /* 缓冲器满 */
	        {
	              UART0Count = 0;	
	        }

    }
    else if ( IIRValue == IIR_CTI )	/* 字符超时指示 */
    {
			 /* 读取RBR清除中断 */
			 Dummy = U0RBR;	    
			 /* 记录超时 */
	         UART0Status |= 0x100; /* 第九位作为 CTI 错误 */
    }
    else if ( IIRValue == IIR_THRE ) /* THRE中断 */
    {
	          LSRValue = U0LSR;		 /* 检查LSR查看U0THR是否包含有效数据 */
			  	              
              if ( LSRValue & LSR_THRE ) /* U0THR空 */
	         {
	              UART0TxEmpty = 1;
	         }    
	          else
       	     {
	              UART0TxEmpty = 0;
	         }
    }
    
	OS_EXIT_CRITICAL(); 
    VICVectAddr = 0;	/* 允许下次中断 */
}

/*****************************************************************************
** 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;

	extern void UART0Handler(void);

    PINSEL0 = 0x00050005;       /* 使能RxD1和TxD1,RxD0和TxD0引脚 */

    U0LCR = 0x83;               /* 8位数据,无奇偶校检位,1个停止位,禁止间隔发送,允许访问除数锁存寄存器 */
    Fdiv = ( Fpclk / 16 ) / baudrate; /* 设置波特率 */
    U0DLM = Fdiv / 256;							
    U0DLL = Fdiv % 256;	
    U0LCR &= ~0x80;             /* 不允许访问除数锁存寄存器 */
    U0FCR = 0x07;	           	/* 使能并复位TX和RX FIFO */

    if ( install_irq(2, UART0_INT, (void *)UART0Handler ) == FALSE ) /* 安装向量中断 */
    {
	     return (FALSE);
    }  
   
    U0IER = IER_RBR | IER_THRE | IER_RLS; /* 允许UART0中断 */
    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 )
    {
	   while (!(UART0TxEmpty & 0x01) ); /* THRE状态,防止覆盖未发送数据 */	
 	   UART0TxEmpty = 0;	            /* U0THR不空 */
	   U0THR = *BufferPtr;	
	   BufferPtr++;
	   Length--;
    }
    return;
}

void UARTPutChar(BYTE c )
{
	while (!(UART0TxEmpty & 0x01) ); /* THRE状态,防止覆盖未发送数据 */	
 	UART0TxEmpty = 0;	             /* U0THR不空 */
	U0THR = c;	
}

void UARTPutStr(BYTE *BufferPtr)
{
	while ( *BufferPtr != '\0' )
    {
	   while (!(UART0TxEmpty & 0x01) ); /* THRE状态,防止覆盖未发送数据 */	
 	   UART0TxEmpty = 0;	            /* U0THR不空 */
	   U0THR = *BufferPtr;	
	   BufferPtr++;
    }
    return;
}
/******************************************************************************
**                            End Of File
******************************************************************************/

⌨️ 快捷键说明

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