User manual

LPCXpresso Experiment Kit - User’s Guide
Page 105
Copyright 2013 © Embedded Artists AB
** Function name: UARTSendChar
**
** Descriptions: Send a byte/char of data to the UART 0 port
**
** parameters: byte to send
** Returned value: None
**
*****************************************************************************/
void UARTSendChar(uint8_t toSend)
{
/* THRE status, contain valid data */
while ( !(LPC_UART->LSR & LSR_THRE) )
;
LPC_UART->THR = toSend;
}
/*****************************************************************************
** Function name: UARTReceive
**
** Descriptions: Receive a block of data from the UART 0 port based
** on the data length
**
** parameters: buffer pointer, data length
** Returned value: Number of received bytes
**
*****************************************************************************/
uint32_t UARTReceive(uint8_t *buffer, uint32_t length, uint32_t blocking)
{
uint32_t recvd = 0;
uint32_t toRecv = length;
if (blocking) {
while (toRecv) {
/* wait for data */
while (!(LPC_UART->LSR & LSR_RDR));
*buffer++ = LPC_UART->RBR;
recvd++;
toRecv--;
}
}
else {
while (toRecv) {
/* break if no data */
if (!(LPC_UART->LSR & LSR_RDR)) {
break;
}
*buffer++ = LPC_UART->RBR;
recvd++;
toRecv--;
}
}
return recvd;
}
Function UARTSendChar() transmits one byte/char of data. UARTReceive() is a function
that can receive data either blocking or non-blocking. Blocking means that the processor spends all
time idle waiting (in the function call) for the wanted number of characters to be received. Non-blocking
means that the function returns with the number of available characters that was/were received. It will
be somewhere between zero (0) and length number of characters. Another name for non-blocking
is asynchronous function call. Blocking calls can also be called synchronous function calls.
Place the UART related code in file uart.c, and place the function prototype declarations in file
uart.h.