KL26 UART1 won't tx very first byte after reset

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

KL26 UART1 won't tx very first byte after reset

355 Views
prof7bit
Contributor II

I have a problem with the UART1 on an MKL26Z128. It will not send the very first byte after reset.

consider this minimal sample code that reproduces the problem:

static void tx(u8 b) {
     UART1->D = b;
     while((UART1->S1 & UART_S1_TC_MASK) == 0);
}

static void test_proc(void) {

     // wait some time
     for (volatile u32 i=0; i<500; i++);

     // enable external transceiver
     FPTE->PSOR = (1 << 23);

     // transmit 3 bytes and wait for completion
     tx(0x00);
     tx(0x40);
     tx(0x50);

     // disable external transceiver
     FPTE->PCOR = (1 << 23);

     // wait some more time
     for (volatile u32 i=0; i<500; i++);

}

int main(void) {
     SIM->SCGC5 |= SIM_SCGC5_PORTE_MASK;
     PORTE->PCR[0] = PORT_PCR_MUX(3);

     // io-link transceiver TXEN control pin
     // yellow trace in oscillogram
     PORTE->PCR[23] = PORT_PCR_MUX(1);
     FPTE->PDDR = (1 << 23);

     // route output pin to UART1 TX
     // blue trace in oscillogram
     PORTE->PCR[0] = PORT_PCR_MUX(3);

     // configure UART 1
     SIM->SCGC4 |= SIM_SCGC4_UART1_MASK;
     UART1->BDH = 0;
     UART1->BDL = 10;
     UART1->C1 = 0;
     UART1->C3 = 0;
     UART1->C2 = UART_C2_TE_MASK;
     
     // send the 3-byte test packet twice
     test_proc();
     test_proc();

     while(1);
}

I had initially noticed this problem with a much more sophisticated interrupt driven UART driver with fifos and whatnot and then tried to reproduce the problem with as little code as possible (above code) in an attempt to figure out what needs to be done so that the UART1 will not eat the very first byte that is ever sent since reset.

The above code will produce the following oscillogram:

20170321_114833.jpg

The yellow line is the Pin E23 which I drive high prior to sending and drive low after transmission complete, it will later be used to drive an external transceiver. The blue line is the pin E0 which is routed to UART1 TX.

I would have expected to see 3 bytes transmitted two times. Instead the very first byte is silently eaten by the UART. Writing it to UART1->D does neither change TDRE nor TC flag nor does it make the TX output do anythiing. Only the second byte and from then onwards all following bytes are transmitted correctly.

What is the reason for this behavior and how do I need to change my code to make it transmit the first byte (without ugly workarounds like transmittig a dummy byte after initialization)? I want to understand what is going on and what is the reason for this behavior.

TIA,

Bernd K.

Labels (1)
0 Kudos
1 Reply

287 Views
Robin_Shen
NXP TechSupport
NXP TechSupport

Hi Bernd,

There is a note in Reference Manual "Always read UART_S1 before writing to UART_D to allow data to be transmitted."

Please try to test the code shown below:



Best Regards,

Robin

 

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos