How do I use printf() with the FRDM-KL25Z Board without using the Processor Expert?

To use printf() with the FRDM-KL25Z Board without using the Processor Expert, follow these steps:

  1. Create a new bareboard project, and in the Language and Build Tools Options page select UART from the I/O Support group.
    Figure 1. Language and Build Tools Options Page - Selecting Uart Option

    Language and Build Tools Options Page - Selecting Uart Option

    Note: For more information about creating a new project using the New Bareboard Project wizard, refer to the Codewarrior For Microcontrollers Targeting Manual.
  2. Copy the ConsoleIO.c, ConsoleIO.h, PDD_Types.h and UART0_PDD.h files for the TWR-KL25Z from the following location in the Sources folder of your project:

    CW_InstallDir\MCU\ARM_GCC_Support\UART\TWR-KL25Z128

    where, CW_InstallDir is the installation directory of your CodeWarrior software.

    Figure 2. Copied Low Level UART Support Files

    Copied Low Level UART Support Files

  3. The TWR-KL25Z uses UART0 with PTA14/PTA15, while the FRDM-KL25Z uses UART0, but PTA1/PTA2, So you need to change the pin settings. For this disable the settings for the Tower board in ConsoleIO.c and ConsoleIO_Init(), and add changes to use PTA1/PTA2, as the following code shows:
    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 */
    }
    
    The following code shows an example to print a hello world from the main() in main.c, to test the changes you made:
    /*
     * 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;
    }
    
    Tip: Include the correct header files and call ConsoleIO_Init() while you create your project.

Build and run the project, the message prints and appears on the console.