Over a million developers have joined DZone.
Platinum Partner

printf() with the FRDM-KL25Z Board and without Processor Expert

· Performance Zone

The Performance Zone is brought to you in partnership with New Relic. New Relic APM provides constant monitoring of your apps so you don't have to.

In this tutorial I explored how to use printf(), and this tutorial is so generic that it works for any processor/microcontroller. That flexibility is because I’m using Processor Expert. In case Processor Expert shall not be used, then some tweaks are needed. Here I show what is needed to have printf() working with the FRDM-KL25Z board. I use the UART0 connected to OpenSDA USB CDC for this.

Creating the Project

I create a new bareboard project. Nothing special here, except that ‘UART’ should be selected:

UART set for new project settings

UART set for new project settings

Low Level UART Support

As explained in this tutorial I, I need low-level UART support. CodeWarrior comes with the needed files for the TWR-KL25Z in “\MCU\ARM_GCC_Support\UART\TWR-KL25Z128″:

Low Level UART support files

Low Level UART support files

I copy these files into my project:

Copied Low Level UART Support Files

Copied Low Level UART Support Files

The TWR-KL25Z is using UART0 with PTA14/PTA15, while the FRDM-KL25Z is using UART0, but PTA1/PTA2. So I need to change the pin settings.

For this I disable in ConsoleIO.c, in ConsoleIO_Init() the settings for the Tower board, and add my change to use PTA1/PTA2:

void ConsoleIO_Init()
    /* SIM_SCGC4: UART0=1 */
#if 0 /* TWR version: PTA14, PTA15 */
    /* PORTA_PCR15: ISF=0,MUX=3 */
    PORTA_PCR15 = (uint32_t)((PORTA_PCR15 & (uint32_t)~(uint32_t)(
                 PORT_PCR_ISF_MASK |
                )) | (uint32_t)(
    /* PORTA_PCR14: ISF=0,MUX=3 */
    PORTA_PCR14 = (uint32_t)((PORTA_PCR14 & (uint32_t)~(uint32_t)(
                 PORT_PCR_ISF_MASK |
                )) | (uint32_t)(
#else /* FRDM-KL25Z: PTA1/PTA2 */
      /* PORTA_PCR1: ISF=0,MUX=2 */
      PORTA_PCR1 = (uint32_t)((PORTA_PCR1 & (uint32_t)~0x01000500UL) | (uint32_t)0x0200UL);
      /* PORTA_PCR2: ISF=0,MUX=2 */
      PORTA_PCR2 = (uint32_t)((PORTA_PCR2 & (uint32_t)~0x01000500UL) | (uint32_t)0x0200UL);
    UART0_PDD_EnableTransmitter(UART0_BASE_PTR, PDD_DISABLE); /* Disable transmitter. */
    UART0_PDD_EnableReceiver(UART0_BASE_PTR, PDD_DISABLE); /* Disable receiver. */
    /* UART0_C1: LOOPS=0,DOZEEN=0,RSRC=0,M=0,WAKE=0,ILT=0,PE=0,PT=0 */
    UART0_C1 = 0x00U;                    /*  Set the C1 register */
    /* UART0_C3: R8T9=0,R9T8=0,TXDIR=0,TXINV=0,ORIE=0,NEIE=0,FEIE=0,PEIE=0 */
    UART0_C3 = 0x00U;                    /*  Set the C3 register */
    UART0_S2 = 0x00U;                    /*  Set the S2 register */
    UART0_PDD_SetBaudRate(UART0_BASE_PTR, 313U); /* Set the baud rate register. */
    UART0_PDD_SetOversamplingRatio(UART0_BASE_PTR, 3U);
    UART0_PDD_EnableSamplingOnBothEdges(UART0_BASE_PTR, PDD_ENABLE);
    UART0_PDD_EnableTransmitter(UART0_BASE_PTR, PDD_ENABLE); /* Enable transmitter */
    UART0_PDD_EnableReceiver(UART0_BASE_PTR, PDD_ENABLE); /* Enable receiver */

:idea: To use different UART/port settings, the easiest way is to use a Processor Expert project as explained in this tutorial.

Example Code

For testing, I print a ‘hello world’ from the main() in main.c:

 * main implementation: use this 'C' sample to create your own application
#include "derivative.h" /* include peripheral declarations */
#include "ConsoleIO.h"
int main(void)
    int counter = 0;
    for(;;) {
        printf("Hello world!\r\n");
    return 0;

:!: Do not forget to include the correct header files, and to call ConsoleIO_Init()!

That’s it! Build, download and hopefully you see the messages printed to the console :-) .

Project on GitHub

For reference, I have committed the CodeWarrior project and sources on GitHub here.

Happy Printing :-)

The Performance Zone is brought to you in partnership with New Relic. New Relic’s SaaS-based Application Performance Monitoring helps you build, deploy, and maintain great web software.


Published at DZone with permission of Erich Styger , DZone MVB .

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}