This project demonstrates UART through OpenSDA (character is printed back on console through the CDC port of the board) with the FRDM-K64F Board using New Kinetis Design Studio 3.0 IDE and using Kinetis Software development kit (KSDK) 1.2.0

This example will examine the efficiency of the transmit/receive uart drivers with using polling method. Transfer data between board and PC. Board will transfer and receive characters with PC through UART interface. Type characters from keyboard, the board will receive and then echo them to terminal screen.

 

Before we begin make sure below requirements are there as setup

  Required Hardware and Software:

 

  • Kinetis Design Studio 3.0

(download link: KDS 3.0)

 

  • Kinetis Software Development Kit (KSDK-v1.2.0)

(Download Link Kinetis SDK 1.2 Mainline )

 

  • FRDM-K64F Board configured with JLink Debugger

(The firmware can be downloaded here: Firmware download)

 

  • Install ‘KSDK_1.2.0_Eclipse_Update’ you can find the update file and the instructions to install it in

‘C:\Freescale\KSDK_1.2.0\tools\eclipse_update’.

 

  • It is necessary to build KSDK Platform Driver Library (libksdk_platform.a)

Open ‘Getting Started with Kinetis SDK (KSDK) v.1.2.pdf’ located in ‘C:\Freescale\KSDK_1.2.0\doc’ and go to chapter ‘5.3 Build the platform library’.

 

  • Reference of FRDM-K64 user guide for schematic

http://www.freescale.com/webapp/sps/site/prod_summary.jsp?code=FRDM-K64F&fpsp=1&tab=Design_Tools_Tab#

  • Micro USB Cable

 

 

Create KDS project using KSDK


Open KDS IDE by double clicking the KDS icon present in your desktop:

It will ask for the Workspace, give the path of your choice and click ‘ok’


Next create a New project by clicking

File -> New  -> Kinetis Design Studio Project



Choose project name of your choice, i have given it as “K64+KSDK-uart polling” and click Next.


You can select device either by ‘Boards’ or ‘Processor’ option

I am selecting the target device by Boards option and selected “FRDM-K64F” and click Next as shown below:

You can see the Device in target board is “MK64FN1M0LL12”


In the next screen you can find below options to be selected in Rapid Application Development window

Select the check box “Kinetis SDK”


And select the radio button “Environment variable” by which default it displays “KSDK_PATH” which is in installed directory.


Make sure that "Processor Expert" is not checked.

Next click Finish button and proceed further.


Develop the application


Right click over project folder and select New > Folder to create a new folder inside ‘project’ folder.


Name this folder ‘Board’ and click ‘Finish’.

Go to <C:\Freescale\KSDK_1.2.0\examples\frdmk64f> and copy all the source files (except the file FRDM-K64F.peb) in this folder to the folder ‘Board’ that was created in the project.

 

  • board.c
  • board.h
  • gpio_pins.c
  • gpio_pins.h
  • pin_mux.c
  • pin_mux.h


Right click copy the files and go to KDS project explorer select folder ‘Board’ right click and paste it.

Similarly add one more file i.e “hardware_init.c” from the location

“C:\Freescale\KSDK_1.2.0\examples\frdmk64f\driver_examples\gpio\hardware_init.c”

It looks like as shown below in KDS window

  1. board.c/h: The header file contains board board-specific configuration macros for things such as debug terminal configuration, push buttons, LEDs and other board board-specific items. The C file contains clock and oscillator initialization functions.

 

gpio_pins.c/h: Definitions used by the KSDK GPIO driver for the platform’s GPIO pins. These include push buttons and LEDs, but can include other items such as interrupt pins for external sensors, for example.

 

 

pin_mux.c/h: Contains peripheral-specific pin mux configurations. These functions can be called by the hardware_init() function or individually by the demo application.

 

hardware_init.c: This file Enable clock for PORTs, setup board clock source, config pin which is required for hardware_init(); function called in main().

Now create another folder “Utilities” inside our project folder as we did earlier



Copy the below 2 files to this folder as we did earlier:

fsl_debug_console.c and fsl_debug_console.h

 

you can find them from below folders:

 

C:\Freescale\KSDK_1.2.0\platform\utilities\src\fsl_debug_console.c

C:\Freescale\KSDK_1.2.0\platform\utilities\inc\fsl_debug_console.h

 

fsl_debug_console.c/h: Provides initialization and basic functionality for the UART module that is connected to the debug interface.

 

NOTE: After copying ‘fsl_debug_console.c’ to utilities folder the prompt below may appear, in this case click ‘Yes’ and continue.


After copying these 2 files into folder ‘Utilities’ it looks as shown below:


Now we need to add these newly added folders to our ‘include paths’ into our project.

 

Select project folder right click and select properties as shown below


Then goto

Project > Properties > C/C++ Build > Settings > Cross ARM C compiler > includes

 

And add the Board and Utilities folder paths into this section as shown in below snapshots

Select Workspace > project folder and “Board” folder as shown below

 

Next, do similar way and add “Utilities” folder into include path:



Now you need to add platform related include paths that are required for build the project:

You need to add the below 4 paths to

Properties > C/C++ Build > Settings > Cross ARM C compiler > includes

 

C:\Freescale\KSDK_1.2.0\platform\osa\inc

C:\Freescale\KSDK_1.2.0\platform\hal\inc

C:\Freescale\KSDK_1.2.0\platform\system\inc

C:\Freescale\KSDK_1.2.0\platform\drivers\inc

After adding all the 4 paths it looks as shown below:


Build the platform driver library

 

Please refer to my earlier blog build a platform library

Click Here

http://www.element14.com/community/community/designcenter/kinetis_kl2_freedom_board/blog/2015/05/29/frdm-k64f-kds-30-software-dev-kit-sdk-120-tutorial-gpio-toggling-using-interrupt-without-processor-expert

 

 

After successfully build you can see the library “libksdk_platform.a” is created under folder:

“C:\Freescale\KSDK_1.2.0\lib\ksdk_platform_lib\kds\K64F12\debug”


Adding the Platform Library to our project

 

We need to provide this path in our Newly created project.

 

Goto Project > Properties > C/C++ Build > Settings > Cross ARM C++ Linker > Miscellaneous


Click on “Apply” and then click “ok”

 

Now we have added all required include paths and libraries for our project we can precede further in writing our code:

 

Open the main.c file from source folder:

 

Delete all the contents and paste the below code:


#include "board.h"
#include "fsl_uart_hal.h"
#include "fsl_clock_manager.h"


int main(void)
{
    uint8_t  rxChar          = 0;
    uint32_t byteCountBuff   = 0;
    uint32_t uartSourceClock = 0;
    UART_Type * baseAddr     = BOARD_DEBUG_UART_BASEADDR;

    // Enable clock for PORTs, setup board clock source, config pin
    hardware_init();
    dbg_uart_init();


    // Initialize the uart module with base address and config structure
    CLOCK_SYS_EnableUartClock(BOARD_DEBUG_UART_INSTANCE);

    // Get working uart clock
    uartSourceClock = CLOCK_SYS_GetUartFreq(BOARD_DEBUG_UART_INSTANCE);

    // Initialize UART baud rate, bit count, parity and stop bit
    UART_HAL_SetBaudRate(baseAddr, uartSourceClock, BOARD_DEBUG_UART_BAUD);
    UART_HAL_SetBitCountPerChar(baseAddr, kUart8BitsPerChar);
    UART_HAL_SetParityMode(baseAddr, kUartParityDisabled);
#if FSL_FEATURE_UART_HAS_STOP_BIT_CONFIG_SUPPORT
    UART_HAL_SetStopBitCount(baseAddr, kUartOneStopBit);
#endif

    // Enable the UART transmitter and receiver
    UART_HAL_EnableTransmitter(baseAddr);
    UART_HAL_EnableReceiver(baseAddr);

    // Inform to start polling example

    // Inform user of what to do
  
    printf("\n\r UART Send/Receive Polling Example\n\r");
    printf("\n\rType characters from keyboard, the board will receive and then echo them to terminal screen\n\r");


    while(true)
    {
        // Wait to receive input data
    if (kStatus_UART_Success == UART_HAL_ReceiveDataPolling(baseAddr, &rxChar, 1u))
        {
            // Send any character that received
            UART_HAL_SendDataPolling(baseAddr, &rxChar, 1u);
        }
    }

}





Explanation of Code:


hardware_init();

This function enable clock for PORTs, setup board clock source, config pin which is required for hardware.

 

dbg_uart_init();

This function enables us for printing the user data to the terminal using printf()


   CLOCK_SYS_EnableUartClock(BOARD_DEBUG_UART_INSTANCE);    
   uartSourceClock = CLOCK_SYS_GetUartFreq(BOARD_DEBUG_UART_INSTANCE); 
   UART_HAL_SetBaudRate(baseAddr, uartSourceClock, BOARD_DEBUG_UART_BAUD);
   UART_HAL_SetBitCountPerChar(baseAddr, kUart8BitsPerChar);
   UART_HAL_SetParityMode(baseAddr, kUartParityDisabled);
#if FSL_FEATURE_UART_HAS_STOP_BIT_CONFIG_SUPPORT
   UART_HAL_SetStopBitCount(baseAddr, kUartOneStopBit);
#endif  
   UART_HAL_EnableTransmitter(baseAddr);
   UART_HAL_EnableReceiver(baseAddr);



The above function is required to Get working uart clock, Initialize UART baud rate, bit count, parity and stop bit, Enable the UART transmitter and receiver

 

The below lines performs the main task of our project requirement which is inside while(1)


if (kStatus_UART_Success == UART_HAL_ReceiveDataPolling(baseAddr, &rxChar, 1u))
        {
            // Send any character that received
            UART_HAL_SendDataPolling(baseAddr, &rxChar, 1u);
        }



Here the function UART_HAL_ReceiveDataPolling() keeps checking from user whether any keys have been pressed or not. If this is true then the kStatus_UART_Success is true and it enters inside the if statement where the data eentered by user which is saved in buffer rxChar is sent to uart port pins using the function UART_HAL_SendDataPolling().

 

Now it’s time to clean and compile/Build the project


Or click on hammer button as shown

 

Debug the application

 

Now we will proceed further for debugging and test our written code:

 

Go to menu Run > Debug Configurations


As i have loaded PE micro OpenSDA driver for my K64F board,

Select GDB PEMicro interface debugging option as shown below:


Click on Debug and proceed

 

Now we need connect the FRDM-K64 board to our computer via USB cable provided and open the serial hyperterminal window by knowing the port number from Device manager:

 


Note: You can run this project either by going to Debug configuration and run by single step or by creating binary image


Go to Debugger and select the OpenSDA option as shown below, click ‘Apply’ and ‘Debug’ button

 



You can see the process of debugging happening as shown below:


Now it is ready for debug/run our project

 

To run your program plug on K64F board to your computer and open the corresponding COM port (com port number can be noted from device manager after plugging the board) and settings as shown below.


My serial hyper terminal is connected to COM29 port number with baud rate of 115200 and click on ‘open’

Now click on resume button as shown below:


 


You can see the output as shown below: type a character it is printed back on console through the CDC port of the board.




I have enclosed the project folder and binary file for quick reference and testing.


Happy Async uart testing