Serial / USB not sending bytes with MSB on

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

Serial / USB not sending bytes with MSB on

Jump to solution
537 Views
robertpoor
Senior Contributor I

I have a FRDM-KL27Z dev board communicating with my Mac OSX via the OpenSDA USB port.  

It appears that if I write a char to the LPUART with the MSB turned on (i.e. 0x80 - 0xff), the serial line goes idle for one byte period instead of transmitting the character, so if I send the pattern:

    putchar(0x55);

    putchar(0xa5);

    putchar(0x5a);

... on the 'scope (J1-04, aka TX), I see 0x55 followed 10 bit periods of 'mark' followed by 0x5a .

Any idea what's going on?

Some details:

I'm enabling LPUART0 like this:

  lpuart_config_t config;

  LPUART_GetDefaultConfig(&config);

  config.baudRate_Bps = 9600;

  config.enableTx = true;

  config.enableRx = true;

  CLOCK_SetLpuart0Clock(0x03);

  uint32_t uartClkSrcFreq = CLOCK_GetFreq(kCLOCK_McgInternalRefClk);

  LPUART_Init(LPUART0, &config, uartClkSrcFreq);

And my putchar() routine looks like this:

void putchar(char ch) {

  while (!(LPUART0->STAT & LPUART_STAT_TDRE_MASK)) {

    // busy wait until TX buffer ready

  }

  LPUART0->DATA = ch;

}

Tags (2)
0 Kudos
1 Solution
411 Views
mjbcswitzerland
Specialist V

Hi Robert

Change

void putchar(char ch)

to

void putchar(unsigned char ch)

and it will 'magically' start working.

Regards

Mark

P.S. I think that there was a thread about 4 weeks ago where the same mistake was made, including explanation

View solution in original post

0 Kudos
2 Replies
412 Views
mjbcswitzerland
Specialist V

Hi Robert

Change

void putchar(char ch)

to

void putchar(unsigned char ch)

and it will 'magically' start working.

Regards

Mark

P.S. I think that there was a thread about 4 weeks ago where the same mistake was made, including explanation

0 Kudos
411 Views
robertpoor
Senior Contributor I

Hah!  I just figured it out independently.  :smileyhappy:

By declaring it a char (rather than uint8_t), a byte with the MSB set gets sign extended to the full 32 bit LPUART0->DATA register.  Bit 13 in the DATA register is FRETSC, or Frame Error / Transmit Special Character.  And from the docs: "For writes, indicates a break or idle character is to be transmitted instead of the contents in DATA[T9:T0]. T9 is used to indicate a break character when 0 and a idle character when 1, he contents of DATA[T8:T0] should be zero."  Which is exactly what I'm seeing.

So, yep, using a uint8_t will fix all that.

Thanks.

0 Kudos