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

📄 lpc177x_8x_uart.c

📁 NXPl788上lwip的无操作系统移植,基于Embest开发板
💻 C
📖 第 1 页 / 共 4 页
字号:
		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 (UartID == UART_1)
	{
		LPC_UART1->LCR = (uint8_t)(tmp & UART_LCR_BITMASK);
	}
	else if (UartID == UART_4)
	{
		LPC_UART4->LCR = (uint8_t)(tmp & UART_LCR_BITMASK);
	}	
	else
	{
		LPC_UART_TypeDef *UARTx = uart_get_pointer(UartID);
		UARTx->LCR = (uint8_t)(tmp & UART_LCR_BITMASK);
	}
}

/*********************************************************************//**
 * @brief		De-initializes the UARTx peripheral registers to their
 *                  default reset values.
 * @param[in]	UartID	UART peripheral selected, should be:
 *				- UART_0: UART0 peripheral
 *				- UART_1: UART1 peripheral
 *				- UART_2: UART2 peripheral
 *				- UART_3: UART3 peripheral
 *				- UART_4: UART4 peripheral
 * @return 		None
 **********************************************************************/
void UART_DeInit(UART_ID_Type UartID)
{
	UART_TxCmd(UartID, DISABLE);
	if (UartID == UART_0)
	{
		/* Set up clock and power for UART module */
		CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCUART0, DISABLE);
	}

	else if (UartID == UART_1)
	{
		/* Set up clock and power for UART module */
		CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCUART1, DISABLE);
	}

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

	else if (UartID == UART_3)
	{
		/* Set up clock and power for UART module */
		CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCUART3, DISABLE);
	}
	else if (UartID == UART_4)
	{
		/* Set up clock and power for UART module */
		CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCUART4, DISABLE);
	}
}

/*****************************************************************************//**
* @brief		Fills each UART_InitStruct member with its default value:
* 				- 115200 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 = 115200;

	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:
 *				- UART_0: UART0 peripheral
 *				- UART_1: UART1 peripheral
 *				- UART_2: UART2 peripheral
 *				- UART_3: UART3 peripheral
 *				- UART_4: UART4 peripheral
 * @param[in]	Data	Data to transmit (must be 8-bit long)
 * @return 		None
 **********************************************************************/
void UART_SendByte(UART_ID_Type UartID, uint8_t Data)
{
	switch (UartID)
	{
		case UART_0:
			LPC_UART0->THR = Data & UART_THR_MASKBIT;
			break;
		case UART_1:
			LPC_UART1->THR = Data & UART_THR_MASKBIT;
			break;	
		case UART_2:
			LPC_UART2->THR = Data & UART_THR_MASKBIT;
			break;
		case UART_3:
			LPC_UART3->THR = Data & UART_THR_MASKBIT;
			break;
		case UART_4:
			LPC_UART4->THR = Data & UART_THR_MASKBIT;
			break;
	}

}


/*********************************************************************//**
 * @brief		Receive a single data from UART peripheral
 * @param[in]	UARTx	UART peripheral selected, should be:
 *				- UART_0: UART0 peripheral
 *				- UART_1: UART1 peripheral
 *				- UART_2: UART2 peripheral
 *				- UART_3: UART3 peripheral
 *				- UART_4: UART4 peripheral
 * @return 		Data received
 **********************************************************************/
uint8_t UART_ReceiveByte(UART_ID_Type UartID)
{
	switch (UartID)
	{
		case UART_0:
			return (LPC_UART0->RBR & UART_RBR_MASKBIT);
		case UART_1:
			return (LPC_UART1->RBR & UART_RBR_MASKBIT);	
		case UART_2:
			return (LPC_UART2->RBR & UART_RBR_MASKBIT);
		case UART_3:
			return (LPC_UART3->RBR & UART_RBR_MASKBIT);
		case UART_4:
			return (LPC_UART4->RBR & UART_RBR_MASKBIT);
	}
    return 0x00;
}

/*********************************************************************//**
 * @brief		Send a block of data via UART peripheral
 * @param[in]	UARTx	Selected UART peripheral used to send data, should be:
 *				- UART_0: UART0 peripheral
 *				- UART_1: UART1 peripheral
 *				- UART_2: UART2 peripheral
 *				- UART_3: UART3 peripheral
 *				- UART_4: UART4 peripheral
 * @param[in]	txbuf 	Pointer to Transmit buffer
 * @param[in]	buflen 	Length of Transmit buffer
 * @param[in] 	flag 	Flag used in  UART transfer, should be
 * 						NONE_BLOCKING or BLOCKING
 * @return 		Number of bytes sent.
 *
 * Note: when using UART in BLOCKING mode, a time-out condition is used
 * via defined symbol UART_BLOCKING_TIMEOUT.
 **********************************************************************/
uint32_t UART_Send(UART_ID_Type UartID, uint8_t *txbuf,
							uint32_t buflen, TRANSFER_BLOCK_Type flag)
{
	uint32_t bToSend, bSent, timeOut, fifo_cnt;
	uint8_t *pChar = txbuf;
	__IO uint32_t *LSR = NULL;

	switch (UartID)
	{
		case UART_0:
			LSR = (__IO uint32_t *)&LPC_UART0->LSR;
			break;
		case UART_1:
			LSR = (__IO uint32_t *)&LPC_UART1->LSR;
			break;
		case UART_2:
			LSR = (__IO uint32_t *)&LPC_UART2->LSR;
			break;
		case UART_3:
			LSR = (__IO uint32_t *)&LPC_UART3->LSR;
			break;
		case UART_4:
			LSR = (__IO uint32_t *)&LPC_UART4->LSR;
			break;
	}

	bToSend = buflen;

	// blocking mode
	if (flag == BLOCKING)
	{
		bSent = 0;
		while (bToSend)
		{
			timeOut = UART_BLOCKING_TIMEOUT;

			// Wait for THR empty with timeout
			while (!(*LSR & UART_LSR_THRE))
			{
				if (timeOut == 0)
					break;

				timeOut--;
			}

			// Time out!
			if(timeOut == 0)
				break;

			fifo_cnt = UART_TX_FIFO_SIZE;

			while (fifo_cnt && bToSend)
			{
				UART_SendByte(UartID, (*pChar++));

				fifo_cnt--;

				bToSend--;

				bSent++;
			}
		}
	}

	// None blocking mode
	else
	{
		bSent = 0;
		while (bToSend)
		{
			if (bToSend == 0)
				break;

			if (!(*LSR & UART_LSR_THRE))
			{
				break;
			}

			fifo_cnt = UART_TX_FIFO_SIZE;

			while (fifo_cnt && bToSend)
			{
				UART_SendByte(UartID, (*pChar++));

				bToSend--;

				fifo_cnt--;

				bSent++;
			}
		}
	}

	return bSent;
}

/*********************************************************************//**
 * @brief		Receive a block of data via UART peripheral
 * @param[in]	UARTx	Selected UART peripheral used to send data,
 * 				should be:
 *				- UART_0: UART0 peripheral
 *				- UART_1: UART1 peripheral
 *				- UART_2: UART2 peripheral
 *				- UART_3: UART3 peripheral
 *				- UART_4: UART4 peripheral
 * @param[out]	rxbuf 	Pointer to Received buffer
 * @param[in]	buflen 	Length of Received buffer
 * @param[in] 	flag 	Flag mode, should be NONE_BLOCKING or BLOCKING

 * @return 		Number of bytes received
 *
 * Note: when using UART in BLOCKING mode, a time-out condition is used
 * via defined symbol UART_BLOCKING_TIMEOUT.
 **********************************************************************/
uint32_t UART_Receive(UART_ID_Type UartID, uint8_t *rxbuf,
								uint32_t buflen, TRANSFER_BLOCK_Type flag)
{
	uint32_t bToRecv, bRecv, timeOut;
	uint8_t *pChar = rxbuf;
	__IO uint32_t *LSR = NULL;

	switch (UartID)
	{
		case UART_0:
			LSR = (__IO uint32_t *)&LPC_UART0->LSR;
			break;
		case UART_1:
			LSR = (__IO uint32_t *)&LPC_UART1->LSR;
			break;
		case UART_2:
			LSR = (__IO uint32_t *)&LPC_UART2->LSR;
			break;
		case UART_3:
			LSR = (__IO uint32_t *)&LPC_UART3->LSR;
			break;
		case UART_4:
			LSR = (__IO uint32_t *)&LPC_UART4->LSR;
			break;
	}
	
	bToRecv = buflen;

	// Blocking mode
	if (flag == BLOCKING)
	{
		bRecv = 0;
		while (bToRecv)
		{
			timeOut = UART_BLOCKING_TIMEOUT;
			while (!(*LSR & UART_LSR_RDR))
			{
				if (timeOut == 0)
					break;

				timeOut--;
			}

			// Time out!
			if(timeOut == 0)
				break;

			// Get data from the buffer
			(*pChar++) = UART_ReceiveByte(UartID);

			bToRecv--;

			bRecv++;
		}
	}
	// None blocking mode
	else
	{
		bRecv = 0;
		while (bToRecv)
		{
			if (!(*LSR & UART_LSR_RDR))
			{
				break;
			}
			else
			{
				(*pChar++) = UART_ReceiveByte(UartID);

				bRecv++;

				bToRecv--;
			}
		}
	}

	return bRecv;
}

/*********************************************************************//**
 * @brief		Force BREAK character on UART line, output pin UARTx TXD is
				forced to logic 0.
 * @param[in]	UARTx	UART peripheral selected, should be:
 *				- UART_0: UART0 peripheral
 *				- UART_1: UART1 peripheral
 *				- UART_2: UART2 peripheral
 *				- UART_3: UART3 peripheral
 *				- UART_4: UART4 peripheral
 * @return 		None
 **********************************************************************/
void UART_ForceBreak(UART_ID_Type UartID)
{
	switch (UartID)
	{
		case UART_0:
			LPC_UART0->LCR |= UART_LCR_BREAK_EN;
			break;
		case UART_1:
			LPC_UART1->LCR |= UART_LCR_BREAK_EN;
			break;
		case UART_2:
			LPC_UART2->LCR |= UART_LCR_BREAK_EN;
			break;
		case UART_3:
			LPC_UART3->LCR |= UART_LCR_BREAK_EN;
			break;
		case UART_4:
			LPC_UART4->LCR |= UART_LCR_BREAK_EN;
			break;
	}
}


/********************************************************************//**
 * @brief 		Enable or disable specified UART interrupt.
 * @param[in]	UARTx	UART peripheral selected, should be
 *				- UART_0: UART0 peripheral
 *				- UART_1: UART1 peripheral

⌨️ 快捷键说明

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