FRDM-KL25Z UART0 Serial port reading wrong

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

FRDM-KL25Z UART0 Serial port reading wrong

4,584 Views
pengliang
Contributor III

Hi all,

I wrote a program for UART0 driver. Test the program by typing keyboard and send back on the screen the char that i typed. I am using pooling method to check if RDRF is 1, then get the value from UART0->D register.

However, this program works for sending out the chars. it does not work for read the char from the keyboard.

for example, I can send a string "uart0 test" to the screen without problem. but if I type 'a' on the keyboard. it returns weird char on the screen. I checked return value in hex is 0xB0. it suppose to be 0x61.

any idea why? this so strange.

I have no problem sending out a string. just cannot read a right value back. I have tried many ways, bypass openSDA, connect a serial port directly; using static volatile variable. but it does not work at all.

Thanks in advance

Labels (1)
Tags (2)
0 Kudos
7 Replies

1,268 Views
hectorsanchez
Contributor IV

Try this fix i found on the community,

UART0 blocks due to UART_S1_TDRE_MASK checking

Keep me informed if this helped you.

0 Kudos

1,268 Views
pengliang
Contributor III

Hector,

I had this code on void init_UART0(uint32_t sysclk, uint32_t baud, uint8_t osr) function

Here:

SIM->SOPT2 |= SIM_SOPT2_UART0SRC(1);    /* UART0 clock source select as MCGFLLCLK/MCGPLLCLK/2*/

SIM->SOPT2 |= SIM_SOPT2_PLLFLLSEL_MASK; /* UART0 clock source select as MCGPLLCLK/2*/

0 Kudos

1,268 Views
ajinkyaj
Contributor I

Hello Peng Liang,

Sorry to post my issue here since time has travelled more than year.

I'm also facing issue in reception of Uart0. I'm using rx interrupt.

Can u let me know how did your issue get solved ??

0 Kudos

1,268 Views
pengliang
Contributor III

I am post my code here

I am using Keil uversion 4 as my IDE, then the <MKL25Z4.H> header file for mcu is sort of different than freescale provided. however, it wont be the issue

in uart0.c file

void init_UART0(uint32_t sysclk, uint32_t baud, uint8_t osr)

{

    register uint16_t sbr;

    uint8_t temp;

   

    /* PORTA->PCR1: ISF=1,MUX=2, UART0_RX alterative */   

    PORTA->PCR[1] = PORT_PCR_ISF_MASK | PORT_PCR_MUX(2);        /* Interrupt is detected | UART0_RX */

    /* PORTA->PCR2: ISF=1,MUX=2, UART0_TX alterative */

    PORTA->PCR[2] = PORT_PCR_ISF_MASK | PORT_PCR_MUX(2);        /* Interrupt is detected | UART0_TX */

   

    SIM->SOPT2 |= SIM_SOPT2_UART0SRC(1);    /* UART0 clock source select as MCGFLLCLK/MCGPLLCLK/2*/

    SIM->SOPT2 |= SIM_SOPT2_PLLFLLSEL_MASK; /* UART0 clock source select as MCGPLLCLK/2*/

    SIM->SCGC4 |= SIM_SCGC4_UART0_MASK;     /* UART0 clock enabled*/

   

    /* Make sure that the transmitter and receiver are disabled while we

    * change settings.

    */

    UART0->C2 &= ~(UART_C2_TE_MASK | UART_C2_RE_MASK);

   

    /* Configure the UART0 for 8-bit mode, no parity */

    UART0->C1 = 0; /* We need all default settings, so entire register is cleared */

   

    UART0->C3 = 0x00;

    UART0->MA1 = 0x00;

    UART0->S1 |= 0x1F;

    UART0->S2 |= 0xC0;

   

    /* Setting OSR register, ONLY for UART0*/

    UART0->C4 = UART0_C4_OSR(osr - 1);

       

    /* Calculate baud settings */

    sbr = (uint16_t)((sysclk*1000) / (baud * osr));

   

    /* Save off the current value of the UARTx_BDH except for the SBR field */

    temp = UART0->BDH & (~(UART_BDH_SBR_MASK));

   

    UART0->BDH = temp | ((sbr & 0x1F00) >> 8);          /* Setting UART0_BDH register */

    UART0->BDL = (uint8_t)(sbr & UART_BDL_SBR_MASK);    /* Setting UART0_BDL register */

   

   

    /* Enable receiver and transmitter */

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

}

uint8_t UART0_getChar(void)

{  

    /* Wait until character has been received */

    while(!(UART0->S1 & UART0_S1_RDRF_MASK));

      

    /* Return the 8-bit data from the receiver */

    return UART0->D;

}

void UART0_putChar(uint8_t ch)

{

    /* Wait until space is available in the FIFO */

    while(!(UART0->S1 & UART0_S1_TDRE_MASK));

   

    /* Send the char */

    UART0->D = ch;

}

void UART0_putStr(const uint8_t *str)

{

    while(*str != '\0')

    {

        UART0_putChar(*str++);

    }

}

basically, in mian.c file i have the following code. clock generator, port, and  uart0 have been initialized in system_MKL25Z4.c file

int main (void) {

    UART0_putStr((uint8_t*)"MyString\r\nTest");

   

    while(1)

  {

//        in = UART0_getChar();

       

//        UART0_putChar(in);

        while(!(UART0->S1 & UART0_S1_RDRF_MASK));

        c = UART0->D;

        while(!(UART0->S1 & UART0_S1_TDRE_MASK) && !(UART0->S1 & UART0_S1_TC_MASK));

        UART0->D = c;

    }

}

c variable never give me correct value.

Please!

0 Kudos

1,268 Views
hectorsanchez
Contributor IV

It's possible that you can post your read method?

0 Kudos

1,268 Views
hectorsanchez
Contributor IV

Hi Peng,

I'm having a hunch that you are missing a bit shift on your code.

0xB0 is 01100001 in binary
0x61  is 10110000 in binary

Hope this helps,

Hector

0 Kudos

1,268 Views
pengliang
Contributor III

i thought it was missing a bit shift when I first saw it.

However, it is not. typed 'b' = 0x62, UART0-.D gives 0xD8

0x62 is 0110 0010 (I expected)

0xD8 is 1101 1000 ????

Thanks for replying

0 Kudos