Version 2

    Here is a tutorial for toggling RGB Led’s on FRDM-K64F board using Kinetis Design Studio IDE and using Kinetis Software development kit (KSDK).

    Before we begin make sure below requirements are there as setup


    Required Hardware and Software:

    • FRDM-K64F Board configured with JLink Debugger

         (The firmware can be downloaded here: Firmware download)

    • Micro USB Cable
    • Kinetis Design Studio (v1.1.1 or newer)

         (You can now download the GA release, KDS 1.1.1, at: KDS-WINDOWS_IDE  )

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

         (Download Link Freescale Kinetis SDK_1.0.0 - Windows installer )

    • Eclipse Update for Freescale Kinetis SDK 1.0.0-GA

         (http://cache.freescale.com/lgfiles/sdk/SDK_1.0.0-GA_Update_for_Eclipse_1.0.1.zip)

    • 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#)


    The activity has been split into different sections as below:

    1. Loading the OpenSDA V2 firmware to K64F board provided from Segger:

    (refer to the link: HERE )

    2. Setting up Kinetis Design Studio Environment Variable

    3. Eclipse Update for Freescale Kinetis SDK 1.0.0-GA

    (refer to the link: HERE )

    4. Build the platform driver library

    5. Creating New Project:

     

     

    • Setting up Kinetis Design Studio Environment Variable

     

    After the installation of KDS the environment variable “KSDK_PATH” must be defined.

    In the start-up menu, right click “Computer” and choose “Properties.”

    In the left column, click “Advanced system settings.” You should get the following window:

    1.jpg

     

    Click the “Environment Variables” and make sure the KSDK_PATH is correct in the “User variables for <user name>”.


    Add the new variable under “User variables…”. The name should be “KSDK_PATH”. The path is the same where by default the KSDK is installed. In this case the path is “C:\Freescale\KSDK_1.0.0”.

     

    • Build the platform driver library

     

    Before building and debugging any demo application in KSDK, the driver library project should be built to generate the library archives: ksdk_platform_lib.a. Because this library contains all binary codes for HAL and the peripheral drivers specific to the chip, each SoC has its own platform.a library archives.


    We need to Importing platform library from the KSDK installation path and build the library, follow below steps to import and build the platform library.


    Open the KDS IDE by giving your default workspace and select ‘import’ option as shown below:


    Select File>Import

    1.jpg

    Next select the option “Existing projects into Workspace” from General Tab and click “Next”

    1.jpg

    Click on “Browse” button as shown below:

    1.jpg

    Browse to navigate to KSDK_1.0.0 installation folder and select the K64F12 folder and select ‘ok’.

    1.jpg

    Make sure the project is selected and click Finish as shown below

    1.jpg

    Now select the Project, right click, clean and build the project as shown below:

    1.jpg

    1.jpg 

    You can see the build progress / status in console window, if everything goes well you can see build finishes as shown:

    1.jpg

    When the build is complete, the ‘ksdk_platform_lib.a’ is generated in the directory according to the build target which is located here “C:/Freescale/KSDK_1.0.0/lib/ksdk_platform_lib/kds/K64F12/debug/ksdk_platform_lib.a”


    We need to provide this path in our new project in "Cross ARM C++ Linker" option to add the KSDK library in the compiler. (this will be shown later while creating new project)


    • Creating New Project:


    So let’s start in creating a new project using KDS + KSDK IDE

     

    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’

    1.jpg

    Next create a New project by clicking

     

    File -> New  -> Kinetis Design Studio Project

    1.jpg

     

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

    q.jpg 

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

    q.jpg

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

    1.jpg

    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.

    1.jpg

    You can see a KDS project window created for our New project. To see a relevant perspective windows click on ‘Windows>Reset Perspective’ as shown below:

    1.jpg 

    You can see below updated project window and is created and ready to include the source code.

    1.jpg

    It is necessary to include the required ‘board’ folder for this project. To add it right click on the project just created and chose "Import".

    1.jpg 

    Select the “File System” and click on Next as shown below:

    1.jpg

    Now look for the board folder in the following path: C:\Freescale\KSDK_1.0.0\boards\  as shown below:

    1.jpg

    Select only *.c and *.h files from option as shown below and proceed to Finish.

    1.jpg

    You can see the board header folder “frdmk64f120m” is added to our project folder as shown:

    1.jpg

    Next we need to add the KSDK library in the compiler, this step is very important else u will not be able to compile your project.


    To add this you need to right click the project and click in "Properties".

    1.jpg

    select "C/C++ Build" menu go to "Settings".

    Then click on “Tool Settings” and select "Miscellaneous" under "Cross ARM C++ Linker" option as shown below:

    1.jpg

    Click on add object button    as shown above and add the library here.

    The default path is "C:\Freescale\KSDK_1.0.0\lib\ksdk_platform_lib\kds\K64F12\Debug\ksdk_platform_lib.a".

    1.jpg 

    And proceed further

    01.jpg

    Now our platform is ready and we need to write our code to toggle the RGB


    Now, let’s toggle an RGB LED. It is necessary to include the boards.h file:

    #include "board.h"

     

    And build once to check it is error free.

    1.jpg

    After compiling i got an error saying that ‘board.h’ is not present the error is as shown below:

    1.jpg

    To overcome this error we need to add the path of board.h to #includes of our project.

    Right click on project and select properties and select the choice as shown:

    1.jpg

    Now we need to add the path, click on ‘ADD..’ button and you can see a small windows appears as shown

    1.jpg

     

    Tick the check box ‘Is a workspace path’ and click on Workspace... as shown.

     

    Select the directory that contains ‘board.h’ which we added earlier as shown:

    1.jpg

    Proceed further by clicking ‘ok’

    You can see the path we selected appears in the last entry in listed down as shown:

    1.jpg

    Click on Apply and ‘OK’ and proceed further.

    Now clean and build the project you can see the compilation results without errors.

     

    We are now proceeding further in coding our requirement to toggle RGB LEDs.

    Refer to the K64F board schematic the 3 LED’s are connected to Port-B and Port-E pins

     

    1.jpg

    You can see these LED’s are connected in active-Low connection, i.e common Anode, we should provide Low signal to port pins to glow a LED.

     

    To know how GPIO have been defined in KSDK library click on include folder and navigate to /drivers/gpio and select ‘fsl_gpio_driver.h’ as shown below:

    (we are accessing the GPIO feature through driver layer not from HAL)

    1.jpg

     

    The GPIO driver uses the virtual GPIO name rather than an actual port and a pin number.


    By using the virtual name, each pin name is self-explanatory. To use the GPIO driver, an enumeration variable must be predefined in the user application files. The variable saves all GPIO pin information used in a project.

     

    enum _gpio_pins

    {

        kGpioLED11  = GPIO_MAKE_PIN(HW_GPIOB, 0x15), // PTB-21 = 0x15 -> BLUE LED.

        kGpioLED22  = GPIO_MAKE_PIN(HW_GPIOB, 0x16), // PTB-22 = 0x16 -> RED LED.

        kGpioLED33  = GPIO_MAKE_PIN(HW_GPIOE, 0x1A), // PTE-26 = 0x1A -> Green LED.

    };

    Actually by default kGpioLED1, kGpioLED2, kGpioLED3 have been already defined in library, just for our understanding i am creating another enum port variables for 3-LED as kGpioLED11, kGpioLED22, kGpioLED33

     

    Now we shall toggle all the 3 LED’s one by one i.e RED on with delay/ RED off with delay,  Blue on with delay/ Blue off with delay, Green on with delay/ Green off with delay and loops back.

     

    Below is my line of code to do this task

     

    #include "fsl_device_registers.h"

    #include "board.h"

     

    enum _gpio_pins

    {

        kGpioLED11  = GPIO_MAKE_PIN(HW_GPIOB, 0x15), // PTB-21 = 0x15 -> BLUE LED.

        kGpioLED22  = GPIO_MAKE_PIN(HW_GPIOB, 0x16), // PTB-22 = 0x16 -> RED LED.

        kGpioLED33  = GPIO_MAKE_PIN(HW_GPIOE, 0x1A), // PTE-26 = 0x1A -> Green LED.

    };

     

    static int i = 0;

    int main(void)

    {

        /* Write your code here */

              short value = 0;

              hardware_init();

     

              GPIO_DRV_SetPinDir(kGpioLED11, kGpioDigitalOutput); //configuring PTB21 port pin as output

              GPIO_DRV_WritePinOutput(kGpioLED11, 1); // driving the PTB21 port pin to HIGH == LED off

     

              GPIO_DRV_SetPinDir(kGpioLED22, kGpioDigitalOutput); //configuring PTB22 port pin as output

              GPIO_DRV_WritePinOutput(kGpioLED22, 1); // driving the PTB22 port pin to HIGH == LED off

     

              GPIO_DRV_SetPinDir(kGpioLED33, kGpioDigitalOutput);//configuring PTE26 port pin as output

              GPIO_DRV_WritePinOutput(kGpioLED33, 1); // driving the PTE26 port pin to HIGH == LED off

     

        /* This for loop should be replaced. By default this loop allows a single stepping. */

     

     

        for (;;) {

     

              value=0;

              GPIO_DRV_WritePinOutput(kGpioLED11, value); //Blue LED on

                       for (i = 0; i<10000000; i++); // software delay

              value = value^1; // value=1

              GPIO_DRV_WritePinOutput(kGpioLED11, value); //Blue LED off

                       for (i = 0; i<10000000; i++);  // software delay

     

              value=0;

              GPIO_DRV_WritePinOutput(kGpioLED22, value); // RED LED on

                       for (i = 0; i<10000000; i++); // software delay

              value = value^1; // value=1

              GPIO_DRV_WritePinOutput(kGpioLED22, value); // RED LED off

                       for (i = 0; i<10000000; i++);  // software delay

     

               value=0;

               GPIO_DRV_WritePinOutput(kGpioLED33, value); // Green LED on

                   for (i = 0; i<10000000; i++);  // software delay

               value = value^1; //1

               GPIO_DRV_WritePinOutput(kGpioLED33, value); // Green LED off

                   for (i = 0; i<10000000; i++);  // software delay

     

        }

        /* Never leave main */

        return 0;

    }

    ////////////////////////////////////////////////////////////////////////////////

    // EOF

    ////////////////////////////////////////////////////////////////////////////////

     

    Explanation of the code:

     

    Initializing the Port pin


    enum _gpio_pins

    {

        kGpioLED11  = GPIO_MAKE_PIN(HW_GPIOB, 0x15), // PTB-21 = 0x15 -> BLUE LED.

        kGpioLED22  = GPIO_MAKE_PIN(HW_GPIOB, 0x16), // PTB-22 = 0x16 -> RED LED.

        kGpioLED33  = GPIO_MAKE_PIN(HW_GPIOE, 0x1A), // PTE-26 = 0x1A -> Green LED.

    };

    Here we are enumerating the port pins as hardware GPIO ports and creating variables for it.

     

    Configuring the Port pin


    GPIO_DRV_SetPinDir(kGpioLED11, kGpioDigitalOutput); //configuring PTB21 port pin as output

     

    This function configures the PortB 21 pin as digital output and by default this will be pull down to GND (i.e port pin driven to zero)

     

    GPIO_DRV_WritePinOutput(kGpioLED11, 1); // driving the PTB21 port pin to HIGH == LED off

     

    This function assigns/writes PortB-21 pin to ‘1’ which means the LED is in off condition.

     

    Same thing is done for other 2 port led’s

     

    Next in the infinite for loop we are toggling i.e performing logical ‘X-OR’ operation on the variable ‘value’ and assigning this to port pins

    value=0;

              GPIO_DRV_WritePinOutput(kGpioLED11, value); //Blue LED on

                       for (i = 0; i<10000000; i++); // software delay

              value = value^1; // value=1

              GPIO_DRV_WritePinOutput(kGpioLED11, value); //Blue LED off

                       for (i = 0; i<10000000; i++);  // software delay

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

    1.jpg

    You can see the build output as shown below:

    1.jpg 

    Now we need to setup for Debug configuration:

     

    As a pre-requisite the board is loaded with “JLink_OpenSDA_V2.bin” OpenSDA_V2 application driver file. And same will be used for connecting hardware to PC.

    1.jpg

    Select ‘GDB SEGGER J-Link Debugging and double click or right click and select ‘New’


    a.jpg

     

    You can see a default name as project name as shown below: make sure under ‘Main’ tab C/C++ application there is *.elf file name of your project.

    a.jpg

     

    Next click on Debuger tab check ‘Device name’ field by default it will blank.

     

    Now specify device name “MK64FN1M0xxx12” and click on apply and close the Debug configuration window as shown below:

    a.jpg

    Now connect FRDM-K64F board to PC through USB mini connector connecting to “SDA USB” port of board as shown:

    a.jpg

    Click on Debug icon as shown and select Debug Configuration and proceed to debug your project:

    a.jpg

    Click on Debug

    a.jpg

    Now you can see the Debug window opens ready for single stepping or Running the project as shown:

    a.jpg

    Click on Resume button or press ‘F8’ button for executing the project.

     

    You can see the output as shown

    a.jpg

     

    You can see below video recording of the project execution from below link.

    RGB Led Toggle using KDS+SDK using K64F Freedom board


    And i have enclosed the project folder for quick view and execution.

     

     

    Happy Toggling