Using mbed with Kinetis Design Studio

A brief overview of the process for building mbed projects with Kinetis Design Studio

  1. Install the necessary tools
  2. Modify the mbed toolchain to support KDS
    2a) Modify the mbed source code to be more KDS/K64F friendly
  3. Build the mbed core library
  4. Build a sample mbed project
  5. Import the mbed project into KDS
  6. Program & Play

Setup

Install Kinetis Design Studio

Obviously, to get started working working with the K64F and the Kinetis Design Studio, it is necessary to install KDS. At the time of writing, the current version is 3.0.0 and can be downloaded directly from Freescale. Downloading requires registration, but so far I have not received any unsolicited e-mails from Freescale.

Install Python

mbed will be built using the ARM GCC compiler included with Kinetis Design Studio. However, the build scripts that rely on the ARM GCC compiler are written in Python. I have only tested the mbed build scripts with Python 2.7, however, the scripts may work with the 3.x branch of Python.

Install a git client (optional)

I prefer using the git command line, however, Github provides a git GUI.

Clone mbed repository

If you installed a git client, clone the git repository for mbed

git clone https://github.com/mbedmicro/mbed.git

If you did not install a git client, download and extract the zip file of the mbed repo.

Modifying the mbed source

Add the KDS compiler

Open the private_settings.py file

/path/to/mbed/workspace_tools/private\_settings.py

Modify Line 57 to point to the /bin folder in the KDS install directory:

# GCC ARM
GCC_ARM_PATH = "C:/Freescale/KDS_3.0.0/toolchain/bin"
Change pins for K64F Rev E (optional)

Edit the pin definitions file for the K64F. More information about why this necessary is in this post.

\path\to\mbed\libraries\mbed\targets\hal\TARGET_Freescale\TARGET_KPSDK_MCUS\TARGET_MCU_K64F\TARGET_FRDM\PinNames.h

Modify the line specifying the definition of D8 from PTA0 to PTC12

D8 = PTC12,
Add gdb support for the mbed core (optional)

Occasionally, we want to debug something happening in the mbed libraries themselves. To do that with KDS, we need to build the mbed libraries with gdb support.

Modify the file specifying gcc command line options

/path/to/mbedworkspace_tools/toolchains/gcc.py

Change Line 73 to include gdb debug symbols:

        if "debug-info" in self.options:
            common_flags.append("-ggdb")
            common_flags.append("-O0")
```

### Build mbed

It is useful to run the build script with the help output to see what sort of options can be used

    python workspace_tools/build.py --help

Knowing the expected parameters, run the following command

    python workspace_tools/build.py - m K64F -t GCC_ARM -o debug-info

If you plan on using rtos, ethernet, DSP, the SD card, or any USB functionality, add their respective flags to the `build.py` command. Additionally, if you have multiple cores use `-j <number of cores - 1>`, for example, `-j 7` if you have 8 cores. 

    python workspace_tools/build.py - m K64F -t GCC_ARM -o debug-info -r -e -F -j 7

After running the command, the mbed directory should have a `build` folder containing at a minimum `mbed` as well as any other libraries specified by the additional flags.

### Create the sample project

With the mbed libraries built locally, we can create a sample project to import into KDS.

   python workspace_tools/project.py -m K64F -i kds -b -p 4

The `-p 4` flag tells `project.py` to build the 4th test program. A complete list of test programs can be seen by running `python project.py -L`

If everything was successful, there should be a `MBED_A5_kds_K64F.zip` file in `/path/to/mbed/build/export/`

### Import into KDS

Extract the *.zip file that was created in the previous step. 

Open KDS, `File\Import`. In the dialog box, select `General\Existing Projects Into Workspace`. Navigate to the extracted *.zip file and import it. 

Press the 'hammer' aka 'build' icon in the KDS toolbar. The output in the debug menu should indicate that the build was successful. 

### Run code on K64F

_Note:_ I use the [Segger J-Link OpenSDA](http://mcuoneclipse.com/2014/04/27/segger-j-link-firmware-for-opensdav2/) application for debugging. The K64F by default uses OpenOCD. I believe just pressing the 'Debug' icon while the K64F is plugged in should debug the application. I recommend using the Segger J-Link debugging interface. A post will follow on how to do this.

-----
Was this information useful? Please consider using my [Amazon Affilliate URL](https://www.amazon.com//ref=as_li_ss_tl?ie=UTF8&linkCode=ll2&tag=frdmtoplay-20&linkId=e105dadbd0d0a4e4941ead9973adc362) as a way of saying thanks.