Dear Joe,
I saw this post and I need to ask you a thing because me too I have a problem with UART on the kinetis FRDM-K64F board and I' m going crazy.
Following there is my code. I am not able to write and to read the D data regisetr, as if it was write protected.
I saw many Freescale example code and all are the same as mine.
Where is the bug?
I thank in advance for all the help that you want to give me.
/****************** uart.c ***************************/
void UART_Init ( UART_Type *pUART ){
uint16_t u16Sbr, u16Brfa;
uint8_t u8Temp;
uint32_t u32SysClk = 120000; // system clock in KHz
uint32_t u32Baud = UART_BAUDRATE;
/* Enable the clock to the selected PORT */
SIM->SCGC5 |= SIM_SCGC5_PORTB_MASK;
/* Set the pins (those that are directly connected to OpenSDA) multiplexer to UART RX/TX mode */
PORTB->PCR[16] = PORT_PCR_MUX( 3 );
PORTB->PCR[17] = PORT_PCR_MUX( 3 );
/* Enable the clock to the selected UART */
SIM->SCGC4 |= SIM_SCGC4_UART1_MASK;
/* Make sure that the transmitter and receiver are disabled while we
* change settings.
*/
pUART->C2 &= ~(UART_C2_TE_MASK | UART_C2_RE_MASK );
/* Configure the UART for 8-bit mode, no parity */
pUART->C1 = 0;
/* Calculate baud settings */
//u16Sbr = (((u32SysClk)>>4) + (u32Baud>>1))/u32Baud;
u16Sbr = (uint16_t)((((u32SysClk*1000)>>4))/u32Baud);
/* Save off the current value of the UARTx_BDH except for the SBR field */
u8Temp = pUART->BDH & ~(UART_BDH_SBR_MASK);
pUART->BDH = u8Temp | UART_BDH_SBR(((u16Sbr & 0x1F00) >> 8));
pUART->BDL = (uint8_t)(u16Sbr & UART_BDL_SBR_MASK);
/* Determine if a fractional divider is needed to get closer to the baud rate */
u16Brfa = (((u32SysClk*32000)/(u32Baud * 16)) - (u16Sbr * 32));
/* Save off the current value of the UARTx_C4 register except for the BRFA field */
u8Temp = pUART->C4 & ~(UART_C4_BRFA(0x1F));
pUART->C4 = u8Temp | UART_C4_BRFA(u16Brfa);
/* Enable receiver and transmitter */
pUART->C2 |= (UART_C2_TE_MASK | UART_C2_RE_MASK );
}
uint8_t UART_GetChar(UART_Type *pUART)
{
/* Wait until character has been received */
while (!(pUART->S1 & UART_S1_RDRF_MASK));
/* Return the 8-bit data from the receiver */
return pUART->D;
}
void UART_PutChar(UART_Type *pUART, uint8_t u8Char)
{
/* Wait until space is available in the FIFO */
while (!(pUART->S1 & UART_S1_TDRE_MASK));
/* Send the character */
pUART->D = (uint8_t)u8Char;
}
/****************** main.c ***************************/
int main()
{
//init_hardware();
uint8_t buffer[128] = "Hello World!";
int i = 0;
UART_Init(UART1);
for(;;)
{
while ( buffer[i] != '\0' )
{
UART_PutChar( UART1, buffer[i++] );
}
}
}