lpc812两个串口同时使用时,收发数据错误

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

lpc812两个串口同时使用时,收发数据错误

275 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by 正原 on Thu Dec 19 21:19:52 MST 2013
lpc812 设置UART0波特率57600,n,8,1;uart1波特率115200,n,8,1;两个串口同时使用时,收发数据不对。
LPC812的uart0和uart1使用的buffer是分开独立的。当他们都工作在115200,n, 8,1时收发正常。

下面是初始化代码。
void UARTInit(LPC_USART_TypeDef *UARTx, uint32_t baudrate)
{
uint32_t UARTSysClk;

if( UARTx== LPC_USART0 ) /* UART0 */
{
UART0_TxEmpty = 1;
}
else if( UARTx== LPC_USART1 ) /* UART1 */
{
UART1_TxEmpty = 1;
}

UARTClock_Init( UARTx );

UARTSysClk = SystemCoreClock/LPC_SYSCON->UARTCLKDIV;

UARTx->CFG = DATA_LENG_8|PARITY_NONE|STOP_BIT_1; /* 8 bits, no Parity, 1 Stop bit */

#if ADDR_DETECT_EN
/* Without two boards connected, the simpliest test is, on the PC side, set to 8 bits,
even parity. on the board side, set to 9 bit, ADDR_DET is set. When data is received
and 9th bit is one, if ADDR_DET is set in CTRL, grab the data as address detected. */
/* Overwrite above UART CFG register. */
UARTx->CFG = DATA_LENG_9|PARITY_NONE|STOP_BIT_1; /* 9 bits, Parity doesn't apply, 1 Stop bit */
UARTx->CTRL = ADDR_DET;
#endif

if ( !LPC_SYSCON->UARTCLKDIV )
{
/* UART clock divider for FDR is disabled. need to know why? */
while ( 1 );
}
else
{
UARTx->BRG = UARTSysClk/16/baudrate-1; /* baud rate */

LPC_SYSCON->UARTFRGDIV = 0xFF;
LPC_SYSCON->UARTFRGMULT = (((UARTSysClk / 16) * (LPC_SYSCON->UARTFRGDIV + 1)) / (baudrate * (UARTx->BRG + 1))) - (LPC_SYSCON->UARTFRGDIV + 1);
}

#if FLOWCTRL_ENABLE
FlowControlInit(UARTx);
#endif

UARTx->STAT = CTS_DELTA | DELTA_RXBRK; /* Clear all status bits. */
/* Enable the UART Interrupt. */
if (UARTx == LPC_USART0) {
#if NMI_ENABLED
NVIC_DisableIRQ( UART0_IRQn );
NMI_Init( UART0_IRQn );
#else
NVIC_EnableIRQ(UART0_IRQn);
#endif
}
else if (UARTx == LPC_USART1) {
#if NMI_ENABLED
NVIC_DisableIRQ( UART1_IRQn );
NMI_Init( UART1_IRQn );
#else
NVIC_EnableIRQ(UART1_IRQn);
#endif
}
else if (UARTx == LPC_USART2) {
#if NMI_ENABLED
NVIC_DisableIRQ( UART2_IRQn );
NMI_Init( UART2_IRQn );
#else
NVIC_EnableIRQ(UART2_IRQn);
#endif
}

#if TX_INTERRUPT
UARTx->INTENSET = RXRDY | TXRDY | DELTA_RXBRK; /* Enable UART interrupt */
#else
UARTx->INTENSET = RXRDY | DELTA_RXBRK;
UARTx->INTENCLR = TXRDY;
#endif

#if ERROR_INTERRUPT
UARTx->INTENSET = (FRM_ERR|OVRN_ERR|PAR_ERR|RXNOISE);
#endif

#if TX_DISABLE
UARTx->CTRL |= TXDIS;
UARTx->INTENSET = TXINT_DIS;
#endif

UARTx->CFG |= UART_EN;
return;
}

void UARTClock_Init( LPC_USART_TypeDef *UARTx )
{
LPC_SYSCON->UARTCLKDIV = 1; /* divided by 1 */

if (UARTx == LPC_USART0)
{
NVIC_DisableIRQ(UART0_IRQn);
/* Enable UART clock */
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<14);
/* Peripheral reset control to UART, a "1" bring it out of reset. */
LPC_SYSCON->PRESETCTRL &= ~(0x1<<3);
LPC_SYSCON->PRESETCTRL |= (0x1<<3);
}
if (UARTx == LPC_USART1)
{
NVIC_DisableIRQ(UART1_IRQn);
/* Enable UART clock */
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<15);
/* Peripheral reset control to UART, a "1" bring it out of reset. */
LPC_SYSCON->PRESETCTRL &= ~(0x1<<4);
LPC_SYSCON->PRESETCTRL |= (0x1<<4);
}

return;
}

波特率初始化:
UARTInit(LPC_USART0, 57600);
UARTInit(LPC_USART1, 115200);
数据收发流程:
PC UART1 <-> LPC_USART0 然后把LPC_USART0收到的数从LPC_USART1发出去,在PC UART2上接收,结果收到的数据不对。

请问是什么问题?
Labels (1)
0 Kudos
1 Reply

213 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by NXPARM on Mon Jun 09 19:08:07 MST 2014

Quote: 正原
lpc812 设置UART0波特率57600,n,8,1;uart1波特率115200,n,8,1;两个串口同时使用时,收发数据不对。
LPC812的uart0和uart1使用的buffer是分开独立的。当他们都工作在115200,n, 8,1时收发正常。

下面是初始化代码。
void UARTInit(LPC_USART_TypeDef *UARTx, uint32_t baudrate)
{
uint32_t UARTSysClk;

if( UARTx== LPC_USART0 ) /* UART0 */
{
UART0_TxEmpty = 1;
}
else if( UARTx== LPC_USART1 ) /* UART1 */
{
UART1_TxEmpty = 1;
}

UARTClock_Init( UARTx );

UARTSysClk = SystemCoreClock/LPC_SYSCON->UARTCLKDIV;

UARTx->CFG = DATA_LENG_8|PARITY_NONE|STOP_BIT_1; /* 8 bits, no Parity, 1 Stop bit */

#if ADDR_DETECT_EN
/* Without two boards connected, the simpliest test is, on the PC side, set to 8 bits,
even parity. on the board side, set to 9 bit, ADDR_DET is set. When data is received
and 9th bit is one, if ADDR_DET is set in CTRL, grab the data as address detected. */
/* Overwrite above UART CFG register. */
UARTx->CFG = DATA_LENG_9|PARITY_NONE|STOP_BIT_1; /* 9 bits, Parity doesn't apply, 1 Stop bit */
UARTx->CTRL = ADDR_DET;
#endif

if ( !LPC_SYSCON->UARTCLKDIV )
{
/* UART clock divider for FDR is disabled. need to know why? */
while ( 1 );
}
else
{
UARTx->BRG = UARTSysClk/16/baudrate-1; /* baud rate */

LPC_SYSCON->UARTFRGDIV = 0xFF;
LPC_SYSCON->UARTFRGMULT = (((UARTSysClk / 16) * (LPC_SYSCON->UARTFRGDIV + 1)) / (baudrate * (UARTx->BRG + 1))) - (LPC_SYSCON->UARTFRGDIV + 1);
}

#if FLOWCTRL_ENABLE
FlowControlInit(UARTx);
#endif

UARTx->STAT = CTS_DELTA | DELTA_RXBRK; /* Clear all status bits. */
/* Enable the UART Interrupt. */
if (UARTx == LPC_USART0) {
#if NMI_ENABLED
NVIC_DisableIRQ( UART0_IRQn );
NMI_Init( UART0_IRQn );
#else
NVIC_EnableIRQ(UART0_IRQn);
#endif
}
else if (UARTx == LPC_USART1) {
#if NMI_ENABLED
NVIC_DisableIRQ( UART1_IRQn );
NMI_Init( UART1_IRQn );
#else
NVIC_EnableIRQ(UART1_IRQn);
#endif
}
else if (UARTx == LPC_USART2) {
#if NMI_ENABLED
NVIC_DisableIRQ( UART2_IRQn );
NMI_Init( UART2_IRQn );
#else
NVIC_EnableIRQ(UART2_IRQn);
#endif
}

#if TX_INTERRUPT
UARTx->INTENSET = RXRDY | TXRDY | DELTA_RXBRK; /* Enable UART interrupt */
#else
UARTx->INTENSET = RXRDY | DELTA_RXBRK;
UARTx->INTENCLR = TXRDY;
#endif

#if ERROR_INTERRUPT
UARTx->INTENSET = (FRM_ERR|OVRN_ERR|PAR_ERR|RXNOISE);
#endif

#if TX_DISABLE
UARTx->CTRL |= TXDIS;
UARTx->INTENSET = TXINT_DIS;
#endif

UARTx->CFG |= UART_EN;
return;
}

void UARTClock_Init( LPC_USART_TypeDef *UARTx )
{
LPC_SYSCON->UARTCLKDIV = 1; /* divided by 1 */

if (UARTx == LPC_USART0)
{
NVIC_DisableIRQ(UART0_IRQn);
/* Enable UART clock */
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<14);
/* Peripheral reset control to UART, a "1" bring it out of reset. */
LPC_SYSCON->PRESETCTRL &= ~(0x1<<3);
LPC_SYSCON->PRESETCTRL |= (0x1<<3);
}
if (UARTx == LPC_USART1)
{
NVIC_DisableIRQ(UART1_IRQn);
/* Enable UART clock */
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<15);
/* Peripheral reset control to UART, a "1" bring it out of reset. */
LPC_SYSCON->PRESETCTRL &= ~(0x1<<4);
LPC_SYSCON->PRESETCTRL |= (0x1<<4);
}

return;
}

波特率初始化:
UARTInit(LPC_USART0, 57600);
UARTInit(LPC_USART1, 115200);
数据收发流程:
PC UART1 <-> LPC_USART0 然后把LPC_USART0收到的数从LPC_USART1发出去,在PC UART2上接收,结果收到的数据不对。

请问是什么问题?



你的测试方法有问题,用两块芯片测试试试,而且LPC800的UART 没有FIFO, 这是很要命的,如果两个串口同时有长数据过来,很容易丢失数据的。
0 Kudos