September 12, 2013

How to flash/debug KL05 with mbed library offline in KEIL (tutorial)


I finally allocated one evening to finish what I should have done earlier. I did not anticipate that at this time KL05 wouldn't be online on mbed (no mbed interface for this board yet). This is step guide how to debug it with free version of KEIL (I'll add ARM GCC later).

1. step - clone mbed git repository from github
mbed git repository is here mbed git repository []
If you don't use git, or github, download repository as zip file. This is crucial for offline debugging.

Create a new file named in mbed/workspace_tools. Put the following source code there (I am using KEIL 4.6 at the moment, KEIL seldomly changes their folder structure).

from os.path import join

ARM_PATH = "C:/Program Files/Keil/TAD/ARM"
ARM_BIN = join(ARM_PATH,"ARMCC","bin")
ARM_INC = join(ARM_PATH,"RV31","INC")
ARM_LIB    = join(ARM_PATH,"ARMCC","lib")
ARM_CPPLIB = join(ARM_PATH, "ARMCC","lib","cpplib")

BUILD_OPTIONS = ["debug-info"] 

Time to build our own mbed library, type on your command line python -m KL05Z -t ARM from workspace_tool directory.

If everything went ok, the output should state Build successes: * ARM::KL05Z. Built library is now inside build folder (same level than workspace_tools and libraries folders).

2. step - create KEIL project

Create new folder (I name mine KL05_demo) and copy there entire folder mbed which is inside build directory. Open KEIL, select Project on the top toolbar and create a new project inside our new created directory. It asks you to choose a processor, MKL05Z32xxx4 is assembled on the freedom KL05Z.

Click OK, it ask if you want to copy startup file, click No. We don't need it. This should be an output inside the Project window

Remove that Source Group 1.

3. step - main code file
Create new main.cpp inside the project directory and paste there the simple demo. Here's my code:

#include "mbed.h"

DigitalOut myled(LED1);

int main() {
    while(1) {
        myled = 1;
        myled = 0;

4. step - Set our new created project for KL05Z
The tab which we start with, it's User. Add there fromelf --bin -o test05_KL05Z.bin test05.axf
This step can be skipped because KL05Z does not have the mbed interface yet. Once it does, this command creates a .bin file which you can copy to a mbed drive.

5.step - C/C++ tab
Fill Define with

The include paths should contain folders (add folder with the first icon, a small rectangle):

The misc Controls should contain --gnu command.

The asm tab should also have filled same Include Paths as C/C++. Copy them there.

6. step - Linker tab
Uncheck Memory Layout from Target Dialog. We need to add scatter file which is located inside mbed/TARGET_KL05/TOOLCHAIN_ARM_STD, named MKL05Z4.sct. Add it there.
This is what it should display : .\mbed\TARGET_KL05Z\TOOLCHAIN_ARM_STD\MKL05Z4.sct

Add precompiled mbed files to Misc controls which is right below Scatter file:

7. step - Debug tab
Now select the right radio button Use and select from drop-down menu CMSIS-DAP

8. step - Utilities
Same applies for Use Target Driver for Flash Programming - CMSIS-DAP. Click on Settings and verify everything is set according to target.

10. step - adding sources
Create new group (I name it sources) and click with your right mouse button on the group and select Add files to group .... Here's my workspace.

Now it is finally time to compile, if you followed my steps, you should get no errors ! The picture below shows window after reflashing my KL05Z, part starting with a line LOAD "C...." it's a flashing part.

We can't proceed with a bin file, due to a lack of mbed interface for our board. But CMSIS-DAP allows us to flash/debug it. Before we proceed to a debugging part, if your KL05 has not been flashed with CMSIS-DAP firmware, download it from freescale webpage - (quick start package). There's CMSIS-DAP_OpenSDA.S19 file inside OpenSDA Applications folder. I set my board to bootloader mode and flash it with this file. Reconnect your board, and let's flash it.

Build (F7) and download your code (LOAD icon on top). This flashes KL05Z and LED should be blinking. If you want to debug it , press Ctrl+F5 (or top selection Debug -> Start/stop debuging session). Happy flashing your KL05!

If anything not clear, leave a comment.