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