C++ with Kinetis Design Studio


Unlike CodeWarrior, the Kinetis Design Studio (at least in V1.1.1) does not offer a choice between C and C++ projects. That makes sense with the GNU ARM Eclipse plugins, other than the CodeWarrior gcc integration, there is no need for setting up a special tool chain for C++ (see “Compiling C Files with GNU ARM G++“). While this is great, things are not perfect yet, so I’m providing in this post the information needed to properly setup a C++ project with Kinetis Design Studio V1.1.1.

Debug View of Startup Code Calling C++ Constructors

Debug View of Startup Code Calling C++ Constructors

With Kinetis Design Studio, I can create four types of project using the menu File > New > Kinetis Design Studio:

New Kinetis Design Studio Project

New Kinetis Design Studio Project

❗ The usual way in Eclipse to create a project with File > New > C Project of File > New C++ Project is not supported, as KDS is has implemented its own wizard.

The project type selection is implemented with two check boxes:

  1. Kinetis SDK
  2. Processor Expert
KDS Project Type

KDS Project Type

The combination of the two check boxes gives four different project types:

  1. Bare projects: no Kinetis SDK, no Processor Expert, just a plain project, available for all Kinetis devices: Both options
  2. Processor Expert projects, available for all Kinetis Devices listed
  3. Kinetis SDK projects (for devices where the Kinetis is supported)
  4. Processor Expert projects with Kinetis SDK (for Kinetis SDK devices)

Bare Projects with C++

If using C++ with a bare project type, then everything is setup correctly. You only need to make sure that header files included for C++ files are listed in the compiler include path settings:

C++ Include Path Settings

C++ Include Path Settings

If you are *NOT* seeing the C++ Compiler and Linker options (like below), then you probably have loaded a project from the KDS beta version. As the beta projects are not fully supported in the non-beta version of KDS, I recommend you create a new project in KDS V1.1.1.

KDS Project from Beta KDS does not show C++ Options

KDS Project from Beta KDS does not show C++ Options

Processor Expert Projects

The same point as above (C++ Include Path settings) apply to Processor Expert projects as well. However, there is the first issue with the provided startup code that the constructors for the static class objects are not called in startup.c:

Startup Code not calling static contructors

Startup Code not calling static constructors

Simply enabling __call_static_initializers() will not help, as this function is not defined :-(. Instead, the usual _start() function of the GNU library needs to be called. For this, I’m doing the following changes to the Processor Expert startup code:

Fixed Processor Expert Startup Code for C++

Fixed Processor Expert Startup Code for C++

  1.  Add a prototype to _start() function
  2. Remove the zero_fill_bss() call, as this will be performed by the library in _start()
  3. Instead of calling main(), call _start() from the GNU library

That should it be. But wait! You might run into a hard fault 😦

Hard Fault During _start()

Hard Fault During _start()

💡 See “Oh my! An Interrupt…” how to generate an interupt handler for each interrupt vector, and “A Processor Expert Component to Help with Hard Faults” how to debug hard faults.

The issue is with the newlib-nano libraries in KDS which need an excessive amount of heap memory (see “Switching ARM GNU Tool Chain and Libraries in Kinetis Design Studio“). The solution is to add at least 1 KByte of Heap memory. This is configured in the Build options tab of the CPU component of the project:

Heap Size in the Build options of the CPU component

Heap Size in the Build options of the CPU component

With this, the project runs as expected and initializes all static object constructors.

Kinetis SDK Projects and Kinetis SDK with Processor Expert Projects

For Kinetis SDK projects (with or without Processor Expert), no other changes are needed as adding the C++ header file include paths, as for the bare projects. The Kinetis SDK startup code already calls _start(), and a heap size should be already configured. All good :-).

KDS with Launchpad and C++

If using KDS with the launchpad tools (see “Switching ARM GNU Tool Chain and Libraries in Kinetis Design Studio“), the GNU library code, it is necessary to add the following symbol __stack to the linker file:

__stack = _estack;
__stack and _estack in Linker File

__stack and _estack in Linker File

Otherwise there will be a crash during _start() function in the library.

Summary

While I do not need to select C or C++ project type during project creation, the current V1.1.1 Kinetis Design Studio created projects still need some tweaking to properly support C++ projects, most for the Processor Expert (only) projects as they use a different startup code. It is not clear why, but for sure the SDK startup code is handling things as it should be done. I hope the above steps help you to get C++ projects running with Kinetis Design Studio.

Happy C++ Constructing 🙂

Advertisements

21 thoughts on “C++ with Kinetis Design Studio

  1. My experience with the KDS wizard and PE is limited, so please excuse me if this does not apply.

    For C++ projects it is recommended to also add “-fno-exceptions -fno-rtti -fno-use-cxa-atexit -fno-threadsafe-statics”.

    It might also be useful to redefine __gnu_cxx::__verbose_terminate_handler() and the “C” function __cxa_pure_virtual(). An example of such redefinitions is in /system/src/newlib/_cxx.cpp for projects generated with the GNU ARM Eclipse C++ templates.

    Like

  2. Hi

    I’m having similar problems with the KL05Z and Kinetis Design Studio. Under KDS 1.1.1, I’ve created a new Kinetis Design Studio Project as bare metal (options KINETIS SDK and Processor Expert disabled). The generated project compiles correctly, but hangs when arrives to _start. I’ve reviewed the include directories, and they are OK. By replacing the _start call by a main call, the application can run, but there’re still problems, because variables (global, local or static) are not being initialized. Must be a problem related to the startup files, but I don’t know how to fix it. Any suggestion? All help would be welcome.

    Thanks

    Like

  3. Is there any way that you could post an example project with Processor Expert and c++ support and files for KDS? still struggling to get it working.

    Like

    • Hi Bryan,
      Well, I would need to find some time for this right now, I see if I can spend some time on the week end on it, but chances are rather low right now 😦
      What is the problem you are running into?

      Erich

      Like

      • I was trying to enable c++, but could not get any c++ code to run on the board. Turns out it was a gdb server problem and not the code (I’m still very new with Processor Expert and KDS).

        Like

  4. Hi Erich, I’m running KDS 2.0.0 and using the httpsrv_frdmk64f example project from KSDK 1.1.0. When I go into the project settings, I do not see C++ in the list of tools. From what you’ve said, the best thing to do is to create a new project from scratch. I was hoping to avoid this, and since I’m using a more recent version of the KSDK, I thought I’d ask you if you know of any way to add the C++ settings to Tool Settings, or of any conversion process that would work?

    Like

    • I have no idea why the Kinetis SDK projects are not including the C++ panels. I guess they probably have generated the projects, and somehow missed the C++ settings in the generated project files. The easiest way is probably to create a new project 😦

      Like

      • Ok, I’ve already started on that and porting over everything. Thanks for helping everyone in the community out like this!

        Like

  5. Pingback: Semihosting with GNU ARM Embedded (Launchpad) and Kinetis Design Studio | MCU on Eclipse

  6. Hi Erich!
    I’m running KDS 3.1.0 and KSDK 1.2 Processor-Expert using FRDM-KL43Z, and trying to call C++ member functions (typical FIR filter) from main.c so I can filter a test signal, but I am having trouble doing this. My problem may be in the wrapper functions. To be honest this is the first time I try it, and I am assuming it is possible to do this.
    Do you have any tutorial on how to call C++ member functions from main.c?

    (I attach the main.c and .cpp file, in case you want to check what I have done:)
    https://www.dropbox.com/s/lypxlqhge1agsj9/K43Z-%20example%20for%20PIT.zip?dl=0

    As usual, Many thanks for your time and effort!
    Domingos

    Like

      • Right. So in main.c I just declared the wrapper functions I wrote in the .cpp. But when it runs, they seem to be ignored and in the end I end up with an array with the same values as before the filtering(C++). Do you have any clear example showing how to do this?

        Many thanks,
        Joao

        Like

      • Hi Joao,
        I don’t have an exact example for this, as this is a very generic thing. You need to debug/step through your code so you can see what is going on. Have you tried that?
        Erich

        Like

      • Hi again Erich,
        I have been able to finally call and run C++ code from C, but always had trouble in debugging since I cannot simply step into the C++ functions from main.c. But if I place a breakpoint inside the C++ functions, it will stop there. I wonder why this happen?
        Anyway, what is the typical way to check the amount of SRAM being used currently at any given point during debugging? I am running some RAM-consuming C++ functions and the mcu SRAM is very limited, so for code optimization it would be nice to know how much SRAM is being used at any given breakpoint when debugging.

        Many thanks again for your time!

        Like

      • C++ stepping might not work correctly in the context of inlined functions, not sure if this is your case here. You might try assembly stepping instead (see https://mcuoneclipse.com/2012/10/27/assembly-instruction-stepping/).
        As for the free heap memory: that depends on the heap implementation. With the standard heap you won’t have much visibility here. But you could check in the debugger the heap_end variable (see https://mcuoneclipse.com/2014/03/16/freertos-malloc-and-sp-check-with-gnu-tools/): if you know where the heap starts, you can use this to calculate the free heap.

        Like

  7. HI Guys, I’m trying to do something simple. I want include some files .c with some rotines. The KDS doens’t compile them. What’s wrong?
    Regards

    Like

What do you think?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s