Performance Zone is brought to you in partnership with:

Erich is Professor at Lucerne University of Applied Sciences and Arts and Distinguished Member of Technical Staff at Freescale Semiconductor. Erich has a MsCS degree and 18+ years of experience in the embedded software and tools world. He created many embedded cross C/C++ compilers and debuggers. Additionally he is researching in the domain of programming languages, real time and mechatronic systems. Erich is a DZone MVB and is not an employee of DZone and has posted 98 posts at DZone. You can read more from them at their website. View Full User Profile

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

04.21.2013
| 3986 views |
  • submit to reddit

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()
{
    InitClock();
 
    /* SIM_SCGC4: UART0=1 */
    SIM_SCGC4 |= SIM_SCGC4_UART0_MASK;
#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 |
                 PORT_PCR_MUX(0x04)
                )) | (uint32_t)(
                 PORT_PCR_MUX(0x03)
                ));
    /* PORTA_PCR14: ISF=0,MUX=3 */
    PORTA_PCR14 = (uint32_t)((PORTA_PCR14 & (uint32_t)~(uint32_t)(
                 PORT_PCR_ISF_MASK |
                 PORT_PCR_MUX(0x04)
                )) | (uint32_t)(
                 PORT_PCR_MUX(0x03)
                ));
#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);
#endif
    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: LBKDIF=0,RXEDGIF=0,MSBF=0,RXINV=0,RWUID=0,BRK13=0,LBKDE=0,RAF=0 */
    UART0_S2 = 0x00U;                    /*  Set the S2 register */
    UART0_PDD_SetClockSource(UART0_BASE_PTR, UART0_PDD_PLL_FLL_CLOCK);
    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
#include "ConsoleIO.h"
 
int main(void)
{
    int counter = 0;
 
    ConsoleIO_Init();
    for(;;) {
        counter++;
        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 :-)




Published at DZone with permission of Erich Styger, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)