AnsweredAssumed Answered

KL26 UART1 won't tx very first byte after reset

Question asked by Bernd Kreuss on Mar 21, 2017
Latest reply on Mar 23, 2017 by Robin_Shen

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:

screenshot from oscilloscope

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.

Outcomes