Hi XiangJun,
I have done what you said, I have code that is running a continuous print to USART2 pins using the following code:
--------------------------------------------------------------------------------
UART_CFG_Type UART;
UART.Baud_rate = 115200;
UART.Parity = UART_PARITY_NONE;
UART.Databits = UART_DATABIT_8;
UART.Stopbits = UART_STOPBIT_1;
UART.Clock_Speed = 0;
/* Initialize UART */
UART_Init(LPC_USART2,&UART);
while(1){
UART_Send(LPC_USART2,test,5,NONE_BLOCKING);
UART_Init(LPC_USART2,&UART);
UART_Send(LPC_USART2,test2,5,NONE_BLOCKING);
UART_Init(LPC_USART2,&UART);
}
--------------------------------------------------------------------------------
*Function: UART_Init();*
--------------------------------------------------------------------------------
/********************************************************************//**
* @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
* @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_USARTn_Type *UARTx, UART_CFG_Type *UART_ConfigStruct)
{
uint32_t tmp;
// For debug mode
CHECK_PARAM(PARAM_UARTx(UARTx));
CHECK_PARAM(PARAM_UART_DATABIT(UART_ConfigStruct->Databits));
CHECK_PARAM(PARAM_UART_STOPBIT(UART_ConfigStruct->Stopbits));
CHECK_PARAM(PARAM_UART_PARITY(UART_ConfigStruct->Parity));
#ifdef _UART0
if(UARTx == LPC_USART0)
{
/* Set up peripheral clock for UART0 module */
//LPC_CGU->BASE_UART0_CLK = (SRC_PL160M_0<<24) | (1<<11);// Use PLL1 and auto block
CGU_EntityConnect(CGU_CLKSRC_XTAL_OSC, CGU_BASE_UART0);
}
#endif
#ifdef _UART1
if(((LPC_UART1_Type *)UARTx) == LPC_UART1)
{
/* Set up peripheral clock for UART1 module */
//LPC_CGU->BASE_UART1_CLK = (SRC_PL160M_0<<24) | (1<<11);// Use PLL1 and auto block
CGU_EntityConnect(CGU_CLKSRC_PLL1, CGU_BASE_UART1);
}
#endif
#ifdef _UART2
if(UARTx == LPC_USART2)
{
/* Set up peripheral clock for UART2 module */
//LPC_CGU->BASE_UART2_CLK = (SRC_PL160M_0<<24) | (1<<11);// Use PLL1 and auto block
CGU_EntityConnect(CGU_CLKSRC_XTAL_OSC, CGU_BASE_UART2);
}
#endif
#ifdef _UART3
if(UARTx == LPC_USART3)
{
/* Set up peripheral clock for UART3 module */
//LPC_CGU->BASE_UART3_CLK = (SRC_PL160M_0<<24) | (1<<11);// Use PLL1 and auto block
CGU_EntityConnect(CGU_CLKSRC_XTAL_OSC, CGU_BASE_UART3);
}
#endif
if (((LPC_UART1_Type *)UARTx) == LPC_UART1)
{
/* FIFOs are empty */
((LPC_UART1_Type *)UARTx)->/*IIFCR.*/FCR = ( UART_FCR_FIFO_EN \
| UART_FCR_RX_RS | UART_FCR_TX_RS);
// Disable FIFO
((LPC_UART1_Type *)UARTx)->/*IIFCR.*/FCR = 0;
// Dummy reading
while (((LPC_UART1_Type *)UARTx)->LSR & UART_LSR_RDR)
{
tmp = ((LPC_UART1_Type *)UARTx)->/*RBTHDLR.*/RBR;
}
((LPC_UART1_Type *)UARTx)->TER = UART1_TER_TXEN;
// Wait for current transmit complete
while (!(((LPC_UART1_Type *)UARTx)->LSR & UART_LSR_THRE));
// Disable Tx
((LPC_UART1_Type *)UARTx)->TER = 0;
// Disable interrupt
((LPC_UART1_Type *)UARTx)->/*DLIER.*/IER = 0;
// Set LCR to default state
((LPC_UART1_Type *)UARTx)->LCR = 0;
// Set ACR to default state
((LPC_UART1_Type *)UARTx)->ACR = 0;
// Set Modem Control to default state
((LPC_UART1_Type *)UARTx)->MCR = 0;
// Set RS485 control to default state
((LPC_UART1_Type *)UARTx)->RS485CTRL = 0;
// Set RS485 delay timer to default state
((LPC_UART1_Type *)UARTx)->RS485DLY = 0;
// Set RS485 addr match to default state
((LPC_UART1_Type *)UARTx)->RS485ADRMATCH = 0;
//Dummy Reading to Clear Status
tmp = ((LPC_UART1_Type *)UARTx)->MSR;
tmp = ((LPC_UART1_Type *)UARTx)->LSR;
}
else
{
/* FIFOs are empty */
UARTx->/*IIFCR.*/FCR = ( UART_FCR_FIFO_EN | UART_FCR_RX_RS | UART_FCR_TX_RS);
// Disable FIFO
UARTx->/*IIFCR.*/FCR = 0;
// Dummy reading
while (UARTx->LSR & UART_LSR_RDR)
{
tmp = UARTx->/*RBTHDLR.*/RBR;
}
UARTx->TER = UART0_2_3_TER_TXEN;
// Wait for current transmit complete
while (!(UARTx->LSR & UART_LSR_THRE));
// Disable Tx
UARTx->TER = 0;
// Disable interrupt
UARTx->/*DLIER.*/IER = 0;
// Set LCR to default state
UARTx->LCR = 0;
// Set ACR to default state
UARTx->ACR = 0;
// set HDEN to default state
UARTx->HDEN = 0;
// set SCICTRL to default state
UARTx->SCICTRL = 0;
// set SYNCCTRL to default state
UARTx->SYNCCTRL =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->RS485ADRMATCH = 0;
// Dummy reading
tmp = UARTx->LSR;
}
if (UARTx == LPC_USART3)
{
// Set IrDA to default state
UARTx->ICR = 0;
}
// Set Line Control register ----------------------------
uart_set_divisors(UARTx, (UART_ConfigStruct->Baud_rate), UART_ConfigStruct->Clock_Speed);
if (((LPC_UART1_Type *)UARTx) == LPC_UART1)
{
tmp = (((LPC_UART1_Type *)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_Type *)UARTx) == LPC_UART1)
{
((LPC_UART1_Type *)UARTx)->LCR = (uint8_t)(tmp & UART_LCR_BITMASK);
}
else
{
UARTx->LCR = (uint8_t)(tmp & UART_LCR_BITMASK);
}
}
--------------------------------------------------------------------------------
*Function: UART_Send();*
--------------------------------------------------------------------------------
/*********************************************************************//**
* @brief Send a block of data via UART peripheral
* @param[in] UARTx Selected UART peripheral used to send data, should be:
* - LPC_UART0 :UART0 peripheral
* - LPC_UART1 :UART1 peripheral
* - LPC_UART2 :UART2 peripheral
* - LPC_UART3 :UART3 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
* - 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(LPC_USARTn_Type *UARTx, uint8_t *txbuf,
uint32_t buflen, TRANSFER_BLOCK_Type flag)
{
uint32_t bToSend, bSent, timeOut, fifo_cnt;
uint8_t *pChar = txbuf;
bToSend = buflen;
// blocking mode
if (flag == BLOCKING) {
bSent = 0;
while (bToSend){
timeOut = UART_BLOCKING_TIMEOUT;
// Wait for THR empty with timeout
while (!(UARTx->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(UARTx, (*pChar++));
fifo_cnt--;
bToSend--;
bSent++;
}
}
}
// None blocking mode
else {
bSent = 0;
while (bToSend) {
if (!(UARTx->LSR & UART_LSR_THRE)){
break;
}
fifo_cnt = UART_TX_FIFO_SIZE;
while (fifo_cnt && bToSend) {
UART_SendByte(UARTx, (*pChar++));
bToSend--;
fifo_cnt--;
bSent++;
}
}
}
return bSent;
}
--------------------------------------------------------------------------------
*Function: UART_SendByte();*
--------------------------------------------------------------------------------
/*********************************************************************//**
* @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_USARTn_Type* UARTx, uint8_t Data)
{
CHECK_PARAM(PARAM_UARTx(UARTx));
if (((LPC_UART1_Type *)UARTx) == LPC_UART1)
{
((LPC_UART1_Type *)UARTx)->/*RBTHDLR.*/THR = Data & UART_THR_MASKBIT;
}
else
{
UARTx->/*RBTHDLR.*/THR = Data & UART_THR_MASKBIT;
}
}
--------------------------------------------------------------------------------
I have had the Tx and GND pins connected to an oscilloscope while the above code is running, however I have only had a continuous ~3V signal without any data.
Any other suggestions?
I will try CMSIS USART package next.
Regards,
Quinn