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) {
for (volatile u32 i=0; i<500; i++);
FPTE->PSOR = (1 << 23);
tx(0x00);
tx(0x40);
tx(0x50);
FPTE->PCOR = (1 << 23);
for (volatile u32 i=0; i<500; i++);
}
int main(void) {
SIM->SCGC5 |= SIM_SCGC5_PORTE_MASK;
PORTE->PCR[0] = PORT_PCR_MUX(3);
PORTE->PCR[23] = PORT_PCR_MUX(1);
FPTE->PDDR = (1 << 23);
PORTE->PCR[0] = PORT_PCR_MUX(3);
SIM->SCGC4 |= SIM_SCGC4_UART1_MASK;
UART1->BDH = 0;
UART1->BDL = 10;
UART1->C1 = 0;
UART1->C3 = 0;
UART1->C2 = UART_C2_TE_MASK;
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:

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.