Maclain Lobdell

gcc compiling + gdb debugging on Kinetis on a Linux host - Kinetis L Examples available

Discussion created by Maclain Lobdell Employee on Dec 18, 2013
Latest reply on May 20, 2014 by Ewout Boks


A few weeks ago I posted on gdb debugging on Kinetis with Segger gdb server on LinuxI have a few updates which I am re-posting here as well as new info on available examples with makefiles for compiling/linking with the arm gcc compiler on all these boards - FRDM-KL02Z, FRDM-KL05Z, FRDM-KL25Z, FRDM-KL26Z, and FRDM-KL46Z!


This is focused on Segger GDB Server + J-Link, but other GDB Server + debug interface should work the same like the P&E GDB Server + USB Multilink

The Segger solution performs very well and is completely *FREE* with FRDM- boards because of the free J-Link app that can be programmed in the board to emulate a J-Link.


Why use gdb to debug bare-metal code on a microcontroller?    

The answer is simple.  gdb is a widely used open source debugger that you can get for free with the free gcc toolchain.  As with many other gnu/Linux command line tools, you have a great deal of control at your fingertips (but there is a learning curve).  Eclipse and other GUI debug interfaces such as DDD use gdb under the hood. 

How can gdb debug my microcontroller software?

You might be saying to yourself, “I thought gdb was just for debugging Linux applications?”  If you had a target board running Linux, you would use the built-in gdb server running on the target and the gdb client (on the target or remotely on a host) to debug applications.  But gdb is not limited to debugging Linux applications.  You can debug any embedded software including bare-metal or RTOS software.  With the gdb server solutions for J-Link and P&E, the client and server both run on the host machine.  The server handles all the communication to the target board.

Let’s get started…


[Note: I'm using Ubuntu 12.04 running in VirtualBox.]


Install the J-Link App for OpenSDA (on-board debug circuit)


1.     Get the latest J-Link app at the Segger website here.  See the very bottom section called Getting started with OpenSDA. 


2.     With the USB cable unplugged, hold down the reset button on your FRDM- board and then plug in the USB cable to the OpenSDA usb connector.


3.     The OpenSDA will enumerate as a USB storage drive on your computer.  Drop on the J-Link App software (JLink_OpenSDA.sda) onto the drive called BOOTLOADER.  Wait a second for it to program.


[Note: I am using VirtualBox which requires that I a select the USB device to be accessed by my guest OS (Ubuntu).  I go to Devices…USB Devices…and check the FREESCALE SEMICONDUCTOR INC. OpenSDA MSD APP]


4.     Unplug the USB cable, then plug it back in. Now, OpenSDA looks like a J-Link to your computer!



Install GCC & GDB Client


(Details at )


mac@mac-VirtualBox:/$ sudo add-apt-repository ppa:terry.guo/gcc-arm-embedded

mac@mac-VirtualBox:/$sudo apt-get update

mac@mac-VirtualBox:/$sudo apt-get install gcc-arm-none-eabi


Install Segger GBD Server

After clicking to install the pack, Segger asks you to enter the J-Link serial number to get the download.  However there is no serial number with the J-Link app for OpenSDA, so they give you the option to download without the serial number.

See section: “Having a problem finding the serial number?” at the bottom of the page.  Click “I do not have a serial number because I own an eval board with J-Link on-board. How can I download J-Link software for it?”


Get bare-metal software examples

Martin Kojtal (a.k.a 0xc0170) has created some simple example projects with makefiles for Kinetis L series devices at his github repository.  Martin uses the standard CMSIS-core files from ARM & Freescale for initialization.  The examples typically blink an LED.  Accessing the examples is simple with git (which I assume you have installed).  Just clone his repository. 

Clone the repository in the directory of your choice.

mac@mac-VirtualBox:/$cd  <my_sw>

mac@mac-VirtualBox:/$ git clone

Build an example

1.    Go to the directory of the example you want to build

mac@mac-VirtualBox:/$ cd kinetis_klxx_gcc 

mac@mac-VirtualBox:/$ cd gpio_demo_frdmkl25z

2.     Compile/link the example with make

mac@mac-VirtualBox:/$ make

3.     Go into the build folder to confirm the .elf file is there.

mac@mac-VirtualBox:/$ cd build

mac@mac-VirtualBox:/$ ls


Connect the board and start the gdb server and client


[Note: I am using VirtualBox which requires that I a select the USB device to be accessed by my guest OS (Ubuntu).  I go to Devices…USB Devices…and check the SEGGER J-Link]


1.     Start the GDB Server

You can put these connection steps into shell scripts for easier execution.  Also, it’s good to first kill the GDB Server if it’s already running.


mac@mac-VirtualBox:/$ killall JLinkGDBServer   

mac@mac-VirtualBox:/$ JLinkGDBServer -device MKL25z128xxx4 -if SWD -speed 1000 -endian little &


For the FRDM-KL25Z, select MKL25z128xxx4 as the device. See list of all Segger supported devices here.  A couple things to note here: (1) the part numbers use a lowercase “z”. (2) some devices are supported but don’t appear on Segger’s list.  These are MKL02z32xxx4, MKL26z128xxx4, & MKL46z128xxx4. 


Kinetis L-series devices use SWD for debugging, so that is selected as the interface.


2.     Start GDB client

mac@mac-VirtualBox:/$   arm-none-eabi-gdb


3.     Connect to GDB Server

The client and server communicate via the localhost (internal TCP/IP connection)

(gdb) target remote localhost: 2331


4.      Load symbols from elf file

(gdb) file gpio_demo_frdmkl25z.elf


5.     Program the flash on the board

(gdb) load gpio_demo_frdmkl25z.elf


6.     See where you are at in the code.

(gdb) list

7.     Set a breakpoint

(gdb) break main

8.     Start executing

(gdb) continue

9.    Type Ctrl + C to halt execution

10.    View registers

(gdb) info registers

11.    View the stack frames (back trace)

(gdb) bt

12.    Dump memory to a file

(gdb) dump srec memory file 0xstart 0xend

13.    Step into a function

(gdb) step

14.     Step over a function

(gdb) next

15.    Type “monitor” before gdbserver commands found in the segger gdb server guide

(gdb) monitor reset

(gdb) monitor halt

16.    Type ! preceding a bash shell command to execute a command back in the shell

(gdb) !ls  


More testing is needed.  I have noticed a few hiccups here and there.  More updates to come. 


Good luck!