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

📄 lpc177x_8x_uart.c

📁 LPC1788的USBHOST的FATFS移植
💻 C
📖 第 1 页 / 共 3 页
字号:
/**********************************************************************
* $Id$		lpc177x_8x_uart.c			2011-06-02
*//**
* @file		lpc177x_8x_uart.c
* @brief	Contains all functions support for UART firmware library
*			on LPC177x_8x
* @version	1.0
* @date		02. June. 2011
* @author	NXP MCU SW Application Team
* 
* Copyright(C) 2011, NXP Semiconductor
* All rights reserved.
*
***********************************************************************
* Software that is described herein is for illustrative purposes only
* which provides customers with programming information regarding the
* products. This software is supplied "AS IS" without any warranties.
* NXP Semiconductors assumes no responsibility or liability for the
* use of the software, conveys no license or title under any patent,
* copyright, or mask work right to the product. NXP Semiconductors
* reserves the right to make changes in the software without
* notification. NXP Semiconductors also make no representation or
* warranty that such application will be suitable for the specified
* use without further testing or modification.
**********************************************************************/

/* Peripheral group ----------------------------------------------------------- */
/** @addtogroup UART
 * @{
 */

/* Includes ------------------------------------------------------------------- */
#include "lpc177x_8x_uart.h"
#include "lpc177x_8x_clkpwr.h"

/* Private Functions ---------------------------------------------------------- */

static Status uart_set_divisors(LPC_UART_TypeDef *UARTx, uint32_t baudrate);


/*********************************************************************//**
 * @brief		Determines best dividers to get a target clock rate
 * @param[in]	UARTx	Pointer to selected UART peripheral, should be:
 * 				- LPC_UART0: UART0 peripheral
 * 				- LPC_UART1: UART1 peripheral
 * 				- LPC_UART2: UART2 peripheral
 * 				- LPC_UART3: UART3 peripheral
 * @param[in]	baudrate Desired UART baud rate.
 * @return 		Error status, could be:
 * 				- SUCCESS
 * 				- ERROR
 **********************************************************************/
static Status uart_set_divisors(LPC_UART_TypeDef *UARTx, uint32_t baudrate)
{
	Status errorStatus = ERROR;

	uint32_t uClk;
	uint32_t d, m, bestd, bestm, tmp;
	uint64_t best_divisor, divisor;
	uint32_t current_error, best_error;
	uint32_t recalcbaud;

	/* get UART block clock */
	uClk = CLKPWR_GetCLK(CLKPWR_CLKTYPE_PER);

	/* In the Uart IP block, baud rate is calculated using FDR and DLL-DLM registers
	* The formula is :
	* BaudRate= uClk * (mulFracDiv/(mulFracDiv+dividerAddFracDiv) / (16 * (DLL)
	* It involves floating point calculations. That's the reason the formulae are adjusted with
	* Multiply and divide method.*/
	
	/* The value of mulFracDiv and dividerAddFracDiv should comply to the following expressions:
	* 0 < mulFracDiv <= 15, 0 <= dividerAddFracDiv <= 15 */
	best_error = 0xFFFFFFFF; /* Worst case */
	bestd = 0;
	bestm = 0;
	best_divisor = 0;
	
	for (m = 1 ; m <= 15 ;m++)
	{
		for (d = 0 ; d < m ; d++)
		{
			divisor = ((uint64_t)uClk << 28)*m / (baudrate*(m+d));
			current_error = divisor & 0xFFFFFFFF;

			tmp = divisor>>32;

			/* Adjust error */
			if(current_error > ((uint32_t)1<<31))
			{
				current_error = -current_error;
				tmp++;
			}

			/* Out of range */
			if(tmp < 1 || tmp > 65536)
				continue;

			if( current_error < best_error)
			{
				best_error = current_error;
				best_divisor = tmp;
				bestd = d;
				bestm = m;
				
				if(best_error == 0) 
					break;
			}
		} /* end of inner for loop */

		if (best_error == 0)
			break;
	} /* end of outer for loop  */

	/* can not find best match */
	if(best_divisor == 0) 
		return ERROR;

	recalcbaud = (uClk >> 4) * bestm / (best_divisor * (bestm + bestd));

	/* reuse best_error to evaluate baud error*/
	if(baudrate > recalcbaud) 
		best_error = baudrate - recalcbaud;
	else 
		best_error = recalcbaud -baudrate;

	best_error = best_error * 100 / baudrate;

	if (best_error < UART_ACCEPTED_BAUDRATE_ERROR)
	{
		if (((LPC_UART1_TypeDef *)UARTx) == LPC_UART1)
		{
			((LPC_UART1_TypeDef *)UARTx)->LCR |= UART_LCR_DLAB_EN;
			
			((LPC_UART1_TypeDef *)UARTx)->DLM = UART_LOAD_DLM(best_divisor);
			
			((LPC_UART1_TypeDef *)UARTx)->DLL = UART_LOAD_DLL(best_divisor);
			
			/* Then reset DLAB bit */
			((LPC_UART1_TypeDef *)UARTx)->LCR &= (~UART_LCR_DLAB_EN) & UART_LCR_BITMASK;
			
			((LPC_UART1_TypeDef *)UARTx)->FDR = (UART_FDR_MULVAL(bestm)
													| UART_FDR_DIVADDVAL(bestd)) & UART_FDR_BITMASK;
		}
		else
		{
			UARTx->LCR |= UART_LCR_DLAB_EN;
			
			UARTx->DLM = UART_LOAD_DLM(best_divisor);
			
			UARTx->DLL = UART_LOAD_DLL(best_divisor);
			
			/* Then reset DLAB bit */
			UARTx->LCR &= (~UART_LCR_DLAB_EN) & UART_LCR_BITMASK;
			
			UARTx->FDR = (UART_FDR_MULVAL(bestm) \
							| UART_FDR_DIVADDVAL(bestd)) & UART_FDR_BITMASK;
		}
		errorStatus = SUCCESS;
	}

	return errorStatus;
}

/* End of Private Functions ---------------------------------------------------- */


/* Public Functions ----------------------------------------------------------- */
/** @addtogroup UART_Public_Functions
 * @{
 */
/* UART Init/DeInit functions -------------------------------------------------*/
/********************************************************************//**
 * @brief		Initializes the UARTx peripheral according to the specified
 *               parameters in the UART_ConfigStruct.
 * @param[in]	UARTx	UART peripheral selected, should be:
 *   			- LPC_UART0: UART0 peripheral
 * 				- LPC_UART1: UART1 peripheral
 * 				- LPC_UART2: UART2 peripheral
 * 				- LPC_UART3: UART3 peripheral
 *				- LPC_UART4: UART4 peripheral
 * @param[in]	UART_ConfigStruct Pointer to a UART_CFG_Type structure
*                    that contains the configuration information for the
*                    specified UART peripheral.
 * @return 		None
 *********************************************************************/
void UART_Init(LPC_UART_TypeDef *UARTx, UART_CFG_Type *UART_ConfigStruct)
{
	uint32_t tmp;

	if(UARTx == LPC_UART0)
	{
		/* Set up clock and power for UART module */
		CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCUART0, ENABLE);
	}
	if(((LPC_UART1_TypeDef *)UARTx) == LPC_UART1)
	{
		/* Set up clock and power for UART module */
		CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCUART1, ENABLE);
	}
	if(UARTx == LPC_UART2)
	{
		/* Set up clock and power for UART module */
		CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCUART2, ENABLE);
	}
	if(UARTx == LPC_UART3)
	{
		/* Set up clock and power for UART module */
		CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCUART3, ENABLE);
	}

	/* FIFOs are empty */
	UARTx->FCR = ( UART_FCR_FIFO_EN | UART_FCR_RX_RS | UART_FCR_TX_RS);

	// Disable FIFO
	UARTx->FCR = 0;

	// Dummy reading
	while (UARTx->LSR & UART_LSR_RDR)
	{
		tmp = UARTx->RBR;
	}

	UARTx->TER = UART_TER_TXEN;

	// Wait for current transmit complete
	while (!(UARTx->LSR & UART_LSR_THRE));

	// Disable Tx
	UARTx->TER = 0;

	// Disable interrupt
	UARTx->IER = 0;

	// Set LCR to default state
	UARTx->LCR = 0;

	// Set ACR to default state
	UARTx->ACR = 0;

	// Set RS485 control to default state
	UARTx->RS485CTRL = 0;

	// Set RS485 delay timer to default state
	UARTx->RS485DLY = 0;

	// Set RS485 addr match to default state
	UARTx->ADRMATCH = 0;

	// Dummy reading
	tmp = UARTx->LSR;

	if(((LPC_UART1_TypeDef *)UARTx) == LPC_UART1)
	{
		// Set Modem Control to default state
		((LPC_UART1_TypeDef *)UARTx)->MCR = 0;

		//Dummy Reading to Clear Status
		tmp = ((LPC_UART1_TypeDef *)UARTx)->MSR;
	}
	else
	{
		// Set IrDA to default state for all UART other than UART1
		UARTx->ICR = 0;
	}

	// Set Line Control register ----------------------------

	uart_set_divisors(UARTx, (UART_ConfigStruct->Baud_rate));

	if (((LPC_UART1_TypeDef *)UARTx) == LPC_UART1)
	{
		tmp = (((LPC_UART1_TypeDef *)UARTx)->LCR & (UART_LCR_DLAB_EN | UART_LCR_BREAK_EN)) \
													& UART_LCR_BITMASK;
	}
	else
	{
		tmp = (UARTx->LCR & (UART_LCR_DLAB_EN | UART_LCR_BREAK_EN)) & UART_LCR_BITMASK;
	}

	switch (UART_ConfigStruct->Databits)
	{
		case UART_DATABIT_5:
			tmp |= UART_LCR_WLEN5;
			break;

		case UART_DATABIT_6:
			tmp |= UART_LCR_WLEN6;
			break;

		case UART_DATABIT_7:
			tmp |= UART_LCR_WLEN7;
			break;

		case UART_DATABIT_8:

		default:
			tmp |= UART_LCR_WLEN8;
			break;
	}

	if (UART_ConfigStruct->Parity == UART_PARITY_NONE)
	{
		// Do nothing...
	}
	else
	{
		tmp |= UART_LCR_PARITY_EN;
		switch (UART_ConfigStruct->Parity)
		{
			case UART_PARITY_ODD:
				tmp |= UART_LCR_PARITY_ODD;
				break;

			case UART_PARITY_EVEN:
				tmp |= UART_LCR_PARITY_EVEN;
				break;

			case UART_PARITY_SP_1:
				tmp |= UART_LCR_PARITY_F_1;
				break;

			case UART_PARITY_SP_0:
				tmp |= UART_LCR_PARITY_F_0;
				break;

			default:
				break;
		}
	}

	switch (UART_ConfigStruct->Stopbits)
	{
		case UART_STOPBIT_2:
			tmp |= UART_LCR_STOPBIT_SEL;
			break;

		case UART_STOPBIT_1:

		default:
			// Do no thing
			break;
	}


	// Write back to LCR, configure FIFO and Disable Tx
	if (((LPC_UART1_TypeDef *)UARTx) ==  LPC_UART1)
	{
		((LPC_UART1_TypeDef *)UARTx)->LCR = (uint8_t)(tmp & UART_LCR_BITMASK);
	}
	else
	{
		UARTx->LCR = (uint8_t)(tmp & UART_LCR_BITMASK);
	}
}

/*********************************************************************//**
 * @brief		De-initializes the UARTx peripheral registers to their
 *                  default reset values.
 * @param[in]	UARTx	UART peripheral selected, should be:
 *   			- LPC_UART0: UART0 peripheral
 * 				- LPC_UART1: UART1 peripheral
 * 				- LPC_UART2: UART2 peripheral
 * 				- LPC_UART3: UART3 peripheral
 * @return 		None
 **********************************************************************/
void UART_DeInit(LPC_UART_TypeDef* UARTx)
{
	UART_TxCmd(UARTx, DISABLE);

	if (UARTx == LPC_UART0)
	{
		/* Set up clock and power for UART module */
		CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCUART0, DISABLE);
	}

	if (((LPC_UART1_TypeDef *)UARTx) == LPC_UART1)
	{
		/* Set up clock and power for UART module */
		CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCUART1, DISABLE);
	}

	if (UARTx == LPC_UART2)
	{
		/* Set up clock and power for UART module */
		CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCUART2, DISABLE);
	}

	if (UARTx == LPC_UART3)
	{
		/* Set up clock and power for UART module */
		CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCUART3, DISABLE);
	}
}

/*****************************************************************************//**
* @brief		Fills each UART_InitStruct member with its default value:
* 				- 9600 bps
* 				- 8-bit data
* 				- 1 Stopbit
* 				- None Parity
* @param[in]	UART_InitStruct Pointer to a UART_CFG_Type structure
*                    which will be initialized.
* @return		None
*******************************************************************************/
void UART_ConfigStructInit(UART_CFG_Type *UART_InitStruct)
{
	UART_InitStruct->Baud_rate = 9600;

	UART_InitStruct->Databits = UART_DATABIT_8;

	UART_InitStruct->Parity = UART_PARITY_NONE;

	UART_InitStruct->Stopbits = UART_STOPBIT_1;
}

/* UART Send/Recieve functions -------------------------------------------------*/
/*********************************************************************//**
 * @brief		Transmit a single data through UART peripheral
 * @param[in]	UARTx	UART peripheral selected, should be:
 *   			- LPC_UART0: UART0 peripheral
 * 				- LPC_UART1: UART1 peripheral
 * 				- LPC_UART2: UART2 peripheral
 * 				- LPC_UART3: UART3 peripheral
 * @param[in]	Data	Data to transmit (must be 8-bit long)
 * @return 		None
 **********************************************************************/
void UART_SendByte(LPC_UART_TypeDef* UARTx, uint8_t Data)
{
	if (((LPC_UART1_TypeDef *)UARTx) == LPC_UART1)
	{
		((LPC_UART1_TypeDef *)UARTx)->THR = Data & UART_THR_MASKBIT;
	}
	else
	{
		UARTx->THR = Data & UART_THR_MASKBIT;
	}

}


/*********************************************************************//**
 * @brief		Receive a single data from UART peripheral
 * @param[in]	UARTx	UART peripheral selected, should be:
 *  			- LPC_UART0: UART0 peripheral
 * 				- LPC_UART1: UART1 peripheral
 * 				- LPC_UART2: UART2 peripheral
 * 				- LPC_UART3: UART3 peripheral
 * @return 		Data received
 **********************************************************************/
uint8_t UART_ReceiveByte(LPC_UART_TypeDef* UARTx)
{
	if (((LPC_UART1_TypeDef *)UARTx) == LPC_UART1)
	{
		return (((LPC_UART1_TypeDef *)UARTx)->RBR & UART_RBR_MASKBIT);
	}
	else

⌨️ 快捷键说明

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