UART with the FRDM-KL02Z Board

In my classes I’m mainly using the Freescale FRDM-KL25Z board, as it provides the best value for the money, and 128 kByte FLASH with 16 kByte of RAM is enough for many smaller projects. I do have as well the FRDM-KL02Z Board (32 KByte FLASH, 4 KByte of RAM) which is an inexpensive board to evaluate the smaller KL02Z microcontroller. Because someone reported a problem not being able to use the UART over OpenSDA/USB-to-CDC bridge, I have created a demo project which communicates with a console on the host.

FRDM-KL02Z Board

FRDM-KL02Z Board

The project is shared on GitHub, using GNU ARM Eclipse plugins and Eclipse (Kinetis Design Studio) with Processor Expert here:

https://github.com/ErichStyger/mcuoneclipse/tree/master/Examples/KDS/FRDM-KL02Z/FRDM-KL02Z_UART

Quick steps how you can create the project:

  1. Make sure you have loaded the latest Processor Expert components, see “McuOnEclipse Releases on SourceForge
  2. Create a project with the wizard (File > New > Kinetis Design Studio Project). Select the device (MKL02Z32) with the Processor Expert
  3. There are several ways to send/receive characters using the UART. The low-level driver used for this in Processor Expert is the ‘AsynchroSerial‘ component. I’m using it with the ‘Shell’ component, as the Shell component offers a SendStr() method and other useful functionality. So add the Shell component for the project, and use the AsynchroSerial with the ‘Serial’ template.
  4. Configure the AsynchroSerial settings: Enable interrupts with a useful size for the input and output buffers. According to the FRDM-KL02Z schematics the Rx pin is connected to PTB2 and the Tx pin is connected to the PTB1. Additionally specify a baud matching your needs. I usually use 38400 as this works even with slow bus clock settings.

    AsynchroSerial Configuration for FRDM-KL02Z

    AsynchroSerial Configuration for FRDM-KL02Z

  5. Add more components to your project as needed. Then generate code:

    Generating Code

    Generating Code

UART Read/Write

As mentioned above, the Shell component makes it easy to use the UART. With the function SendStr() I can write a string. GetStdio() is used to select the output channel.

Text can be read in with ReadLine() method: it appends incoming characters to a buffer, that’s why it needs to be initialized with a zero byte first (line 4 below) until there is a newline/line feed entered.

Below is an example code using it (see full example on GitHub):

static unsigned char buffer[64];

void DoUART(void) {
  buffer[0] = '\0'; /* initialize buffer for ReadLine() */
  for(;;) {
    CLS1_SendStr("Type in some text with CR or LF at the end...\r\n", CLS1_GetStdio()->stdOut);
    LED1_Neg();
    WAIT1_Waitms(1000);
    if (CLS1_ReadLine(buffer, buffer, sizeof(buffer), CLS1_GetStdio())) {
      /* line read */
      CLS1_SendStr("You entered:\r\n", CLS1_GetStdio()->stdOut);
      CLS1_SendStr(buffer, CLS1_GetStdio()->stdOut);
      buffer[0] = '\0';
    }
  }
}
Terminal Connection

Terminal Connection

Summary

Using the UART to communicate with the OpenSDA USB CDC (either P&E or Segger J-Link OpenSDA) is not that difficult. Using Processor Expert components makes it very easy: just make sure you are using the correct pins ;-). The example is on GitHub here: https://github.com/ErichStyger/mcuoneclipse/tree/master/Examples/KDS/FRDM-KL02Z/FRDM-KL02Z_UART

Happy UARTing 🙂

26 thoughts on “UART with the FRDM-KL02Z Board

  1. Thanks Eric. That will help us in our evaluation of this next generation of Freescale’s Kinetis series. The price point on some of these cores are incredible (Less than $1 per 10000 for many parts). I’m not sure I’m ever going back to 8-bits again.

    Like

    • I think there are still cases where 8bit mircos are very good, e.g. if a device has a special periopheral matching exactly your needs. Costs for the parts is an important factor, but not always the decision factor. And you are right: these devices are very inexpensive, and I think they will be even at lower prices in the future.

      Like

  2. Hi Erich,
    I am using UART with many other modules in my program. The problem i am facing is as follows.

    * I am using SecureCRT desktop app (similar to putty, hyperterminal etc).
    * I am running program in the debug mode. COnsider scenario where
    Statement 1: Sends “String1” to the the UART.
    Statement 2: Sends “String2” to the UART.

    * I am debugging step by step. String1 is printed on the app after delay. once the execution of statement2 is complete, String1 gets printed.

    * If i include lot of instruction between them, “String1” gets printed only after Statement2 executions. Which means that the String is getting delayed in the UART buffer.

    Any idea why this might be happening? Any suggestions?

    Like

  3. Hi Erich,

    I am using the KSDK uart driver and it works fine! However, I am not see a way to set a rx buffer to receive the data, even when a read function is not used.
    I found in component inspector, in the Inicialization tab, a option to set a global variable with role of rx buffer, but it doesnt work!

    Did you know if I am missing something?

    Thanks,

    Matheus

    Like

      • I am trying to use the KSDK API, because the community says that to use the KSDK now. I rather use another beans. You think that the is better to avoid de KSDK?

        Like

  4. Hi Erich,

    I would like to know if there is a way to receive from a UART or LPSCI not knowing the length of the string you are receiving. Is there a flag I could check to know if there is another byte in the buffer and then call UART_HAL_Getchar ? I tried UART_DRV_ReceiveData with param rxSize=1 but when I call it for the second time I lose all characters but the first one. If I use UART_DRV_ReceiveData or UART_HAL_ReceiveDataPolling with rxSize>1, it will block in line: while (!UART0_BRD_S1_RDRF(base)) waiting for all rxSize bytes.

    Hope you could help me.

    Thanks in advance,

    Valentín

    Like

    • Hi Valentin,
      that might depend on the UART/Device. There should be a bit in the device telling you if something is in the buffer (one byte, usually, not more). As for the KL25Z, there is the SFIFO (Fifo status register) and there is a bit RXEMPT and TXEMPT which can be used if the fifos are empty or not.
      There are as well the following flags (in UART0_PDD.h):
      /* Status flags constants. */
      #define UART0_PDD_TX_DATA_EMPTY_FLAG UART0_S1_TDRE_MASK /**< Transmitter FIFO word count is at or below watermark */
      #define UART0_PDD_TX_IDLE_FLAG UART0_S1_TC_MASK /**< No transmission in progress (transmission activity complete) */
      #define UART0_PDD_RX_DATA_FULL_FLAG UART0_S1_RDRF_MASK /**< Receiver FIFO word count is above watermark */
      #define UART0_PDD_RX_IDLE_FLAG UART0_S1_IDLE_MASK /**< Receiver input has become idle (after receiving a valid frame) */
      #define UART0_PDD_RX_OVERRUN_FLAG UART0_S1_OR_MASK /**< Receiver buffer overrun */
      #define UART0_PDD_RX_NOISE_FLAG UART0_S1_NF_MASK /**< Receiver input detect a noise. */
      #define UART0_PDD_RX_FRAMING_ERROR_FLAG UART0_S1_FE_MASK /**< Receiver framing error detect */
      #define UART0_PDD_RX_PARITY_ERROR_FLAG UART0_S1_PF_MASK /**< Receiver parity error detect */

      I hope this helps.

      Like

  5. Hi Erich, I have a scenario where I’m wanting to use a UART on pins which are used for JTAG. I’ve added an AsynchroSerial component to my project but as expected, PE complains that the selected TX and RX pins are already in use by the JTAG. What I’m wanting is, by default, the JTAG pins to be default and when a certain mode is entered, the pin mux is then swapped over to a UART. Is there any way of fooling KDS and PE so it doesn’t show an error against the pins? Thanks

    Like

  6. Hello, Eric Sir,
    I want to use UART without using Processor Expert..
    Also I want it to run with Interrupt basis…

    Currently I am able to read and write uart characters without interrupt…
    But I am stuck at Interrupt driven UART…
    I want to know how do I use IRQ handler …
    I am using k64f FRDM board and (UART4) PORTC14 and PORTC15 pins

    my current settings are as follows….

    void uart_init (UART_MemMapPtr uartch)
    {
    register uint32_t sbr;
    uint8_t temp;

    PORTC_PCR14 = PORT_PCR_MUX(0x03);
    PORTC_PCR15 = PORT_PCR_MUX(0x03);

    SIM_SCGC1 |= SIM_SCGC1_UART4_MASK;

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

    UART_C2_REG(uartch) &= ~(UART_C2_TE_MASK
    | UART_C2_RE_MASK );

    /* Configure the uart for 8-bit mode, no parity */
    UART_C1_REG(uartch) = 0; /* We need all default settings, so entire register is cleared */

    /* Calculate baud settings */
    sbr = (uint32_t)((60000*1000)/(4800 * 16));

    /* Save off the current value of the uartx_BDH except for the SBR field */
    temp = UART_BDH_REG(uartch) & ~(UART_BDH_SBR(0x1F));

    UART_BDH_REG(uartch) = temp | UART_BDH_SBR(((sbr & 0x1F00) >> 8)) ;
    UART_BDL_REG(uartch) = (uint8_t)(sbr & UART_BDL_SBR_MASK);

    PORTC_ISFR = PORT_ISFR_ISF(0x6000); /* Clear interrupt status flag */
    NVIC_ICPR(2)= 1 << (66%32);

    NVIC_ISER(2)= 1 << (66%32);

    prio_reg = (uint32_t *)((uint32_t)&NVIC_IP(16));
    *prio_reg |= ( (0x00 & 0x3) << (8 – 2) ) << (2 * 8);

    UART_C2_REG(uartch) |= UART_C2_RIE_MASK;
    /* Enable receiver and transmitter */
    UART_C2_REG(uartch) |= (UART_C2_TE_MASK
    | UART_C2_RE_MASK |UART_C2_RIE_MASK |UART_C2_TCIE_MASK);

    }

    void UART4_RX_TX_IRQHandler (void)
    {

    if (UART4_S1&UART_S1_RDRF_MASK)
    {
    UART_S1_REG(UART4_BASE_PTR) = 0x00;
    c = UART4_D;

    if ((UART4_S1&UART_S1_TDRE_MASK)||(UART4_S1&UART_S1_TC_MASK))
    {
    UART4_D = c; // again print received character.
    }
    }
    }

    Thanking you….

    Like

    • Seriously: Can you use (or at least try using) Processor Expert? Then it would work right away. Or at least use it and then take the code (instead re-inventing the wheel)?
      Because there could be so many things wrong in your code.

      Like

  7. HI Guys, I am developing a nextion TFT monitoring system for my school project. I facing a issue. I am not sure how to get a serial/UART input character using processor expert KL25Z. The Nextion TFT will send a character then base on that I need to do the output by sending a digit or character back to TFT. Pls help.

    Like

  8. Hello!
    I’m having problems with the component fsl_uart with interrupt. UART is sending a character repeatedly, instead of send only once. I’m using FRDMKL43Z with 38400 baud rate on transmission. Any tip how can I fix this?

    Like

  9. Hi Erick ! look I have a project and I need to connect to KL25Z to each other ( board A and board B) , and these two boards should send/receive from each other. board A is connected to pc but board B not. so I want to know how can I use UART to connect these two? (I should use UART case it is in my project description).

    Thank U .

    Like

  10. Hi Erich,
    using a K22 (my favorite if you haven’t noticed by now), I am using asynchroSerial to talk to a RS485 peripheral. I’m doing some debugging, and of course if I stick a breakpoint on the OnRxChar event, the whole 8 packet vanishes when resting at the breakpoint. It is clear I am getting the first char, and I think the second is queued up, with the rest of the bits lost on the wire when the CPU stopped. BUT, when I went to look at the EmbSys registers I can see that the UART2 actually has a 8 byte FIFO. The AS bean doesn’t seem to know anything about it. It looks like there is a Init_UART bean which lets me set a few FIFO related parameters, but no recvChar function. Having a full 8 byte FIFO would probably let me debug this whole packet easily.

    Is there a bean that lets me use the full capability of the UART? Or any other code handy?

    Like

    • Yes, K22 is my favorite too if you haven’t noticed too :-). And yes, the component does not seem to handle the FIFO buffer, and I believe I have seen community reports that the FIFO buffer has a hardware problem, so I never used it.
      If you want to use it, you might have a look at the SDK driver (not sure if it supports it at all), but it sounds you have to implement your own routine.

      Like

  11. Hi Erich.

    I am configuring an MKE04Z128 processor to communicate with an MC9S08PT32 by uart. Both are with the internal oscillator set at 31,250 kHz. I created an application for the master (MKE04Z128) send 1 byte and the slave (MC9S08PT32) return 1 byte, and I noticed through the oscilloscope that one sends the byte faster than the other. Both should send the byte at the same speed because they are configured with the same internal frequency (31,250 kHz)? Can a communication failure occur because one is sending faster than the other?

    Like

What do you think?

This site uses Akismet to reduce spam. Learn how your comment data is processed.