I have a strange problem with UART.
First I configure it :
void UART_Init (UART_MemMapPtr uartch, int sysclk, int baud)
{
register uint16_t sbr, brfa;
uint8_t temp;
if (uartch == UART2_BASE_PTR)
{
PORTE_PCR16 = PORT_PCR_MUX(0x3);
PORTE_PCR17 = PORT_PCR_MUX(0x3);
SIM_SCGC4 |= SIM_SCGC4_UART2_MASK;
}
/* Make sure that the transmitter and receiver are disabled while we change settings. */
UART_C2_REG(uartch) &= ~(UART_C2_TE_MASK| UART_C2_RE_MASK );
/* Configure the UART for 8-bit mode, no parity */
UART_C1_REG(uartch) = 0; /* We need all default settings, so entire register is cleared */
/* Calculate baud settings */
sbr = (uint16_t)((sysclk*1000)/(baud * 16));
/* Save off the current value of the UARTx_BDH except for the SBR field */
temp = UART_BDH_REG(uartch) & ~(UART_BDH_SBR(0x1F));
UART_BDH_REG(uartch) = temp | UART_BDH_SBR(((sbr & 0x1F00) >> 8));
UART_BDL_REG(uartch) = (uint8_t)(sbr & UART_BDL_SBR_MASK);
/* Determine if a fractional divider is needed to get closer to the baud rate */
brfa = (((sysclk*32000)/(baud * 16)) - (sbr * 32));
/* Save off the current value of the UARTx_C4 register except for the BRFA field */
temp = UART_C4_REG(uartch) & ~(UART_C4_BRFA(0x1F));
UART_C4_REG(uartch) = temp | UART_C4_BRFA(brfa);
/* Enable receiver and transmitter */
UART_C2_REG(uartch) |= (UART_C2_TE_MASK | UART_C2_RE_MASK | UART_C2_RIE_MASK);
//errors – FOR DEBUG ONLY
UART_C3_REG(uartch) |= UART_C3_PEIE_MASK | UART_C3_FEIE_MASK | UART_C3_NEIE_MASK | UART_C3_ORIE_MASK;
NVIC_EnableIRQ(UART2_RX_TX_IRQn);
//FOR DEBUG ONLY
NVIC_EnableIRQ(UART2_ERR_IRQn );
}
void UART2_RX_TX_IRQHandler(void)
{
if ((UART_S1_REG(channel) & UART_S1_RDRF_MASK))
{
uint8_t chr = UART2->D;
UART_SendByte(UART2, chr);
}
}
void UART2_ERR_IRQHandler(void)
{
uint32_t st = UART2_BASE_PTR->S1;
}
And these functions to send a string.
void UART_SendByte (UART_MemMapPtr channel, char ch)
{
/* Wait until space is available in the FIFO */
while(!(UART_S1_REG(channel) & UART_S1_TDRE_MASK));
/* Send the character */
UART_D_REG(channel) = (uint8_t)ch;
}
void UART_SendString(UART_MemMapPtr uartChannel, const char *p)
{
while(*p)
{
UART_SendByte(uartChannel, *p++);
}
//time_delay_ms(100); //dosen't help!!!
}
When I run it in main
UART_Init (UART2, 60000, 115200);
UART_SendString(UART2, "HELLO EVERYBODY\0");
While (1)
{
}
I get gibberish on the terminal. But if I set a breakpoint in UART_SendByte(uartChannel, *p++); and run it per char I get the correct string on the terminal.
Also when I type a char I get an error in UART2_ERR_IRQHandler and the error is - Framing Error but I get the correct echo on the terminal.
Well...when i set a delay
void UART_SendString(UART_MemMapPtr uartChannel, const char *p)
{
while(*p)
{
UART_SendByte(uartChannel, *p++);
time_delay_ms(1);
}
}
It prints correctly on the terminal. It seems like frame error. What can cause the error?