UART Txing Junk bytes

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

UART Txing Junk bytes

2,265 Views
chandrasekhar
Contributor I

Hi

I am using K21 controller(Baremetal). When I try to write some bytes to UART0, it is writing junk bytes. I tapped the data and checked. But for the second time it is writing complete data correctly.

Can anyone please help me

Labels (1)
Tags (3)
0 Kudos
Reply
8 Replies

1,647 Views
Designer11
Contributor IV

I apologize for reviving this old thread. However, i'm in the same boat. Below is the link to the question i had 

Kinetis K64 UART0 Baud Issue ? 

Thanks 

Vu

0 Kudos
Reply

1,647 Views
sudhanshumehta
Contributor IV

Any solution you got chandra?

Team Freescale is there is any problem in driver. I am running with same problem.


Using MQX4.2 Codewarrior 10.6.

0 Kudos
Reply

1,647 Views
Paul_Tian
NXP Employee
NXP Employee

Hi, Chandra

Do you use interrupt method or polling flag method to send data? It is better to share your code for my checking. Thanks.

Best Regards

Paul

0 Kudos
Reply

1,647 Views
chandrasekhar
Contributor I

Hi,

It worked with the UART4 without any glitch. But with UART0 it doesnt work as expected. I think it has some baud issues.


Thanks ,

Chandra

0 Kudos
Reply

1,647 Views
Paul_Tian
NXP Employee
NXP Employee

Hi, Chandra

UART0 and UART1 are clocked from the core clock, the remaining UARTs are clocked on the bus clock. Would you please check if this factor cause your problem? Thanks.

Best Regards

Paul

0 Kudos
Reply

1,647 Views
chandrasekhar
Contributor I

Hi Zhe TIan,

Yes I have checked the clock settings and I am giving it right. UART0 is giving problem for the first time only. I have already attached the code.

It is behaving the same way in MQX also.

Regards,

Chandrasekhar

0 Kudos
Reply

1,647 Views
johnbaker
Contributor IV

I know that this is an older post but I ran into a similar issue with UART5 on a k64 TWR board recently.  I started out testing at 2400 baud and periodically received junk characters.  I cycled power to the board and it sometimes worked correctly and other times not.  I was sending 'A' through 'G' characters (0x41 through 0x47).  The junk characters received were the actual characters, but not shifted correctly.  For example:

'A' = 01000001 came across as 00001011,

'B' = 01000010 came across as 00010011,

'C' = 01000011 came across as 00011011, etc.

When I changed the baud rate up to 9600 or higher (I went all the way up to 600,000 baud) it worked perfectly.  I suspect that it was just a timing issue in the way I set up the baud rate.  If I had used Processor Expert I could have fine tuned the Baud Rate and it would have worked better at 2400 baud.

I was using some example code that I found on a forum somewhere else. (pasted below)  I modified it to use the UART5_RX_TX_IRQn interrupt.

John Baker

--------------------------------------------------------------------------------------------------------------------------------------------------

void UART_Init ( UART_Type *pUART ){

  uint16_t u16Sbr, u16Brfa;

  uint8_t u8Temp;

  uint32_t u32SysClk = 120000;

  uint32_t u32Baud = UART_BAUDRATE;

  /* Enable the clock to the selected PORT */

  SIM->SCGC5 |= SIM_SCGC5_PORTE_MASK;

  /* Set the pins multiplexer to UART RX/TX mode */

  PORTE->PCR[8] = PORT_PCR_MUX( 3 );

  PORTE->PCR[9] = PORT_PCR_MUX( 3 );

  /* Enable the clock to the selected UART */

  SIM->SCGC1 |= SIM_SCGC1_UART5_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)>>5))/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 interrupt */

  NVIC_SetPriority(UART5_RX_TX_IRQn,64);

  NVIC_ClearPendingIRQ(UART5_RX_TX_IRQn);

  NVIC_EnableIRQ(UART5_RX_TX_IRQn);

  pUART->C2 |= UART_C2_RIE_MASK;

  /* Enable receiver and transmitter */

  pUART->C2 |= ( UART_C2_TE_MASK | UART_C2_RE_MASK );

}

0 Kudos
Reply

1,647 Views
chandrasekhar
Contributor I

Hi Zhe Tian

I am using polling based only, I am checking only this condition

while(!(UART_S1_REG(UART0_BASE_PTR) & UART_S1_TDRE_MASK));.

Attached is my code.

Meanwhile I try with other UART and check whether it givesthe same behaviour or not.

0 Kudos
Reply