MKL25Z, UART1 configuration. What is wrong?

cancel
Showing results for 
Search instead for 
Did you mean: 

MKL25Z, UART1 configuration. What is wrong?

Jump to solution
1,261 Views
madagaskar
Contributor II

Hello everyone!

I try  to connect PC with FRDM-KL25Z board via USB-UART adapter. Unfortunately when I configured UART1 using “Kinetis L Peripheral Quick Reference” and “Reference Manual” it didn’t work correctly… I can receives and transfers data but during the sending, all messages are badly received by FRDM-KL25Z and PC.

I tried to program whole configuration by myself but with no effect. Probably I have some easy mistake but I have no idea where… (maybe it's problem with baud?)

On PC I use PUTTY to connect with KL25Z and I’ m sure that it isn’t problem with USB-UART adapter because he worked great when I communicated with my beaglebone black.

Maybe anybody have had the same problem and know what should I improve?

Below UART1 settings and program code.

UART1 Settings:

  • baud: 2400
  • 8-bit character length
  • One stop bit
  • Parity disabled

Program Code:

#include "derivative.h" /* include peripheral declarations */

#include "stdio.h"

#define CLOCK_UART1        (SIM_SCGC4 |= SIM_SCGC4_UART1_MASK)

#define CLOCKC             (SIM_SCGC5 |= SIM_SCGC5_PORTC_MASK)

#define UART_TX            (PORTC_PCR4 |= PORT_PCR_MUX(3))

#define UART_RX            (PORTC_PCR3 |= PORT_PCR_MUX(3))

int main(void)

{

      

       uint8_t data;

      

       CLOCKC;

      

       UART_RX;

       UART_TX;

      

       //clock init

       CLOCKB;

       CLOCK_UART1;

      

       //transfer and receive disable

UART1_C2 &= ~(UART_C2_TE_MASK | UART_C2_RE_MASK);

//BAUD: 2400        48 000 000/(16*2400) = 0b10011100010

       UART1_BDH = UART_BDH_SBR(0x4);

       UART1_BDL = UART_BDL_SBR(0xE2);                                

      

//transfer and receive enable

       UART1_C2 |= (UART_C2_TE_MASK | UART_C2_RE_MASK);                

       for(;;){

            

       if(UART1_S1 & UART_S1_RDRF_MASK)

       {

          //receive data  

          data = UART1_D;

            

             while(!(UART1_S1 & UART_S1_TDRE_MASK) && !(UART1_S1 & UART_S1_TC_MASK))

             {}

             //transfer data    

             UART1_D = data;

             puts("sent!");

       }

                   

       }

return 0;

}

1 Solution
239 Views
Jorge_Gonzalez
NXP Employee
NXP Employee

Hello Pawel:

Besides of my colleague Paul's suggestion, I cannot see some call to a MCU clock configuration function, while baud rate according to your code is calculated from a 48 MHz clock. Are you configuring the MCU core/bus clock during initialization routines?

Regards!

Jorge González

View solution in original post

0 Kudos
6 Replies
240 Views
Jorge_Gonzalez
NXP Employee
NXP Employee

Hello Pawel:

Besides of my colleague Paul's suggestion, I cannot see some call to a MCU clock configuration function, while baud rate according to your code is calculated from a 48 MHz clock. Are you configuring the MCU core/bus clock during initialization routines?

Regards!

Jorge González

View solution in original post

0 Kudos
239 Views
Paul_Tian
NXP Employee
NXP Employee

Hi, Pawel Szulc

Yes, you should confirm your clock setting first, then change your code as I mentioned. Waiting for your testing result. Please also help to tell us more. You cannot receive data, receive a wrong data or not send data, please give us more.

Best Regards

Paul

0 Kudos
239 Views
madagaskar
Contributor II

I receive a wrong data and I had no idea why. But now it works! Thanks Zhe Tian and Jorge_Gonzalez.:) You have right, It was problem with bus clock configuration. When I corrected my program, UART start working without any problems.

239 Views
ricardoontivero
Contributor I

hello Pawel, How was the code finally? I understand its missing first clock setting, but how you did it?   I've been workin with UART0 with no problems but im trying communicate with another microncontroller by UART1 /2.
Thanks!

0 Kudos
239 Views
madagaskar
Contributor II

Hello Ricardo Ontiveros Torres,

I'm sorry that I reply so late... It was problem with bus clock because i didn't initialize him (baud rate = bus clock/(16×BR)). So to fix it I use "Kinetis L Peripheral Module Quick Reference" (you can download it from freescale home website) and chapter 4: Clocking System. There are everything explained step by step.:)  If you will have any problem please let me know.

Best Regards

Paweł

0 Kudos
239 Views
Paul_Tian
NXP Employee
NXP Employee

Hi, Pawel Szulc

I checked your code. Would you please have a try on following change?

"while(!(UART1_S1 & UART_S1_TDRE_MASK) && !(UART1_S1 & UART_S1_TC_MASK))

to be

while(!(UART1_S1 & UART_S1_TDRE_MASK));

Hope my reply can help you.

Best Regards

Paul