I have a problem with UART Receiving byte.
First, I have run UART example program on S32K142EVB. It is working with no problem. Both receive and transmit demo is working as it should be.
Then I write code for the real application board. I am not using interrupt or DMA, just simple pooling method. Baud rate is 19200. The PutChar function is working well (MCU to PC data send, captured in PuTTY or RealTerm). But GetChar function is not working (PC to MCU). I have checked first invoke to the function is working properly, but then it stuck at
>> while((pUART->STAT & LPUART_STAT_RDRF_MASK)>>LPUART_STAT_RDRF_SHIFT==0)
Any idea please?
The code is as follow:
void UART_pin_settings(void)
{
PCC->PCCn[PCC_PORTC_INDEX ]|=PCC_PCCn_CGC_MASK; /* Enable clock for PORTC */
/* enable the UART port for the UART of S32K142 */
PORTC->PCR[3]|=PORT_PCR_MUX(4); /* Port C3: MUX = ALT4, UART0 TX */
PORTC->PCR[2]|=PORT_PCR_MUX(4); /* Port C2: MUX = ALT4, UART0 RX */
}
void UART_Init(LPUART_Type *pUART, uint32_t u32SysClk, uint32_t u32Baud)
{
uint16_t u16Sbr;
/* Enable the clock to the selected UART */
if (pUART == LPUART0)
{
PCC->PCCn[PCC_LPUART0_INDEX] |= PCC_PCCn_PCS(0b011) /* Clock Src= 1 (SOSCDIV2_CLK) */
|PCC_PCCn_CGC_MASK; /* Enable clock for LPUART1 regs */
}
else if (pUART == LPUART1)
{
PCC->PCCn[PCC_LPUART1_INDEX] |= PCC_PCCn_PCS(0b011) /* Clock Src= 1 (SOSCDIV2_CLK) */
|PCC_PCCn_CGC_MASK; /* Enable clock for LPUART1 regs */
}
/* Make sure that the transmitter and receiver are disabled while we
* change settings.
*/
pUART->CTRL &= ~(LPUART_CTRL_TE_MASK | LPUART_CTRL_RE_MASK);
/* Configure the UART for 8-bit mode, no parity */
pUART->CTRL &= ~(LPUART_CTRL_M7_MASK | LPUART_CTRL_M_MASK | LPUART_CTRL_R8T9_MASK | LPUART_CTRL_R9T8_MASK);
/* Calculate baud settings */
pUART->BAUD |= LPUART_BAUD_OSR(15);
u16Sbr = (u32SysClk/u32Baud)/16;
pUART->BAUD |= LPUART_BAUD_SBR(u16Sbr);
/* Enable receiver and transmitter */
pUART->CTRL |= LPUART_CTRL_TE_MASK | LPUART_CTRL_RE_MASK;
}
uint8_t UART_GetChar(LPUART_Type *pUART)
{
/* Wait until character has been received */
while((pUART->STAT & LPUART_STAT_RDRF_MASK)>>LPUART_STAT_RDRF_SHIFT==0);
/* Return the 8-bit data from the receiver */
return pUART->DATA;
}
void UART_PutChar(LPUART_Type *pUART, uint8_t u8Char)
{
/* Wait until space is available in the FIFO */
while((pUART->STAT & LPUART_STAT_TDRE_MASK)>>LPUART_STAT_TDRE_SHIFT==0);
/* Send the character */
pUART->DATA = (uint8_t)u8Char;
}