Freescale has released the v3.0.0 version of the Kinetis Design Studio: this one comes with a great positive change: instead of a custom toolchain, it is coming with the standard GNU ARM Embedded (launchpad) toolchain from ARM. Beside of better code density and less RAM needed, there is one change which affects semihosting. Previously, semihosting was enabled by default in the V2.0.0 libraries. Now semihosting needs to be turned on. This post is how to do this.
With the GNU ARM Embedded (launchpad) toolchain used in KDS v3.0.0, there are several differences compared to the KDS v2.0.0 version.
Linker Options to Enable Semihosting
As for semihosting, the difference is that in KDS v2.0.0 libraries it is enabled by default in the libraries, while in the launchpad tools it needs to be turned on.
💡 Semihosting is a technique to use printf() through the debugger connection. So instead using a physical UART, as communication channel the debugger is used.
To enable semihosting, check your linker options in the project properties (menu Project > Properties > C/C++ Build > Settings > Tool Settings). By default, it is using
Tells the linker to replace the ‘system’ stubs (reading/writing files, etc) with ‘no operation’ ones. Effectively, this disables semihosting.
💡 if using floating point support with nano library, turn on -u _printf_float and/or -u _scanf_float.
The linker option -specs=nosys.specs need now to be changed to specify
💡 the latest GNU ARM Eclipse plugins have a check box option for the newlib-nano option which I have used in the above screenshot.
With this, semihosting is turned on in the libraries.
All three debug connections in KDS (OpenOCD/CMSIS-DAP, P&E and Segger) have an option to enable semihosting in the debugger.
💡 If not using semihosting in your application, it would make sense to *disable* the semihosting option in the debugger.
The option is found in the Debug Configuration (menu Run > Debug Configurations).
For OpenOCD it is in the Startup tab:
For P&E it is here:
And for Segger it is here:
Debugging with Semihosting
Now while debugging, there should be a ‘Semihosting’ thread visible in the ‘Debug’ view, and output written to the Semihosting console on the bottom:
If the Semihosting output is not visible, you can bring it to the foreground with selecting the Semihosting thread in the Debug view:
Another tip is to select the semihosting console from the console view icon:
Another tip: To avoid that the console is changing depending on the current thread/view selected, I pin that console:
Happy Hosting 🙂
- Semihosting with Kinetis Design Studio v2.0.0: Semihosting with GNU ARM Embedded (Launchpad) and Kinetis Design Studio
- Semihosting with stock Eclipse and GNU ARM Embedded: Semihosting with GNU ARM Embedded (LaunchPad) and GNU ARM Eclipse Debug Plugins
Pingback: Problem: undefined reference to ‘__end__’ if using Semihosting | MCU on Eclipse
Just wondering – why do you say this?
> If not using semihosting in your application, it would make sense to *disable* the semihosting option in the debugger.
I usually leave the OpenOCD debug semi-hosting option enabled even when not using it. That way when I DO use it I don’t have to remember to switch it on. 🙂 I haven’t had any problems leaving it enabled and using debug sessions that don’t use semi-hosting.
you might not see any difference. But I say that it would make sense to disable functionality if it is not used. It is just a general practice I follow. And yes, I have not seen any downside of this, so you might keep it enabled :-).
OK – thanks. I agree with the general principle. I just thought that you had seen some specific adverse behaviour or something. 🙂
Thanks for lots of interesting and useful tutorials on Kinetis MCU’s. I’ve successfully used many of your ideas. Hope you can help me with the following problem.
I’m running KDS V3.0.0 on Windows 7 and using TWR-K70 with P&E debugging. No RTOS, just bare metal code.
After creating a new Processor Expert project (with not a single character added by myself to the generated code), the build process run successfully. Then I applied the modification you suggested (replacing ‘nosys.specs’ with ‘rdimon.specs’ in the linker options) and changing absolutely nothing else. The build returns with this error:
‘Building target: HelloWorld.elf’
‘Invoking: Cross ARM C++ Linker’
arm-none-eabi-g++ -mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 -O0 -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections -g3 -T “C:/Users/840102/workspace3.kds/HelloWorld/Project_Settings/Linker_Files/ProcessorExpert.ld” -Xlinker –gc-sections -L”C:/Users/840102/workspace3.kds/HelloWorld/Project_Settings/Linker_Files” -Wl,-Map,”HelloWorld.map” -specs=nano.specs -specs=rdimon.specs -o “HelloWorld.elf” ./Sources/Events.o ./Sources/main.o ./Project_Settings/Startup_Code/startup.o ./Generated_Code/Cpu.o ./Generated_Code/PE_LDD.o ./Generated_Code/Vectors.o
c:/freescale/kds_3.0.0/toolchain/bin/../lib/gcc/arm-none-eabi/4.8.4/../../../../arm-none-eabi/lib/armv7e-m/fpu/rdimon-crt0.o: In function `_start’:
(.text+0xd8): undefined reference to `__end__’
collect2.exe: error: ld returned 1 exit status
make: *** [HelloWorld.elf] Error 1
13:36:24 Build Finished (took 2s.670ms)
see https://mcuoneclipse.com/2015/05/30/problem-undefined-reference-to-__end__-if-using-semihosting/ as I have been running into that issue myself already :-).
I hope this helps,
My apologies, Erich. I found the answer to my question in one of your other posts…
Ah, comments crossed 🙂
And no problem: I have to google my own articles many times too: many times I know that I had the same issue or a similar thing, then I use google to find it 🙂
Pingback: Using Segger Real Time Terminal (RTT) with Eclipse | MCU on Eclipse
First of all, thank you very much for your tutorials !!! I fixed a lot issues with your help!!
My question is similar to that of Ben: I’m running KDS V3.0.0 on Windows 7 and using TWR-K70 with P&E debugging. No RTOS, just bare metal code with processor expert. I think that I followed all your recommendations:
– Linker options: I checked “use newlib-nano” and I modified the linker flags to “-specs=rdimon.specs”
– Debug configuration > Startup: I checked “Enable Semihosting”, “Console routed to Telnet”, “Enable Telnet console”, “Telnet port: 51794”
– I modified the heap size to 0xC00
The build process runs successfully. I can see the “Semihosting console” thread. The application runs OK (just a LED blinking) but the printf() contents isn’t displayed into the Semihosting console.
Thanks in advance for your help.
I found it!…It was a stupid mistake…I forgot the ‘\n’ in the printf() command. Now, it’s working!!
Ah, yes, that one :-). It actually depends on the semihosting implementation and library implemenation. It is called sometimes ‘buffered I/O’ where the string is only send at the presence of a new line or CR (or both).
Pingback: Tutorial: lwip with the FreeRTOS and the Freescale FRDM-K64F Board | MCU on Eclipse
Thanks for useful tutorials, I’ve successfully used semihosting to printf to the console, but when i try to scanf any character, It doesn’t work as expected, I don’t get any character from console. Hope you can help me?
ch = getchar();
I assume you are indeed sending characters? Have you debugged the code? What is ‘ch’ if no character is received (does it block)?
that might depend on your implementation of semihosting (e.g. P&E and Segger are different) too.
I hope this helps,
Thank Erich for quick respond.
I use Segger J-link. There is not problem with printf, but with getchar function. it’s blocked. The messenger in console is: Starting target CPU…
…Target halted for semihosting, waiting for GDB handling. I try to input some character in console but nothing happens.
Is your console really sending the characters, or does it wait until you send a CR/LF?
Yes, My console really sending the characters, but after that, it’s blocking at getchar function. “ch = getchar();”.My problem is I dont get any charater when i type it to console. thể is othing happend and the MCU still watting.
Hmm, it crashes for me when I try to read from semihosting :-(. I’m using the 4.9q2 GCC ARM Embedded.
I’m using the 4.9q2 and i spend a lot of time trying to scanf with Semihosting but it does not work.
I tried as well 4.9q3 without success.
FYI, I tried it as well with the P&E debugger: here the debugger is running into a hard fault. I think the problem is somehow with the GNU library.
Hello Erich, I do not get any printf output.
I’m using FRDM-K64F, KDS v3.20, Segger J-Link and a hello-world project.
I replaced nosys with rdimon in Cross Arm Link>Libraries and in Miscellaneous>Other linker flags.
Do you have any experience with Version 3.20?
Thank you, Joerg
You can ignore my question. I thought debug output will be displayed in Semihosting window but it’s routed to UART3 in board.h of the hello world project. My bad, Joerg
Ah, great! I was just about to look into your question. Glad to hear that it has been resolved 🙂
Hello Erich, I use a FRDM-K22F with KDS 3.2.0 and I’m currently switching an application from a UART output to semihosting.
After removing the initialization of the UART port I followed your instructions:
– Change “nosys.specs” to “rdimon.specs”
– Enable semihosting in debugger settings
– Ensure that _start is called in startup code (instead of main)
Unfortunately the semihosting console only prompts “P&E Semihosting Console”.
I also tried this in a blank project with the same result.
What am I doing wrong? Thank you in advance!
all wha you list is what I would do as well. Not sure what you have missed. To help you, I have put an example project on GitHub: https://github.com/ErichStyger/mcuoneclipse/tree/master/Examples/KDS/FRDM-K22F/FRDM-K22F_Semihosting_bare
It works for me both with Segger and P&E debug connection.
I hope this helps?
I’ve set everything up as described here, but I cannot see anywhere the Semihosting view. And during Debugging if I try to step into printf() the Debugger silently overjumps it. I have a Feeling that printf is simply nop. What could be wrong?
that’s why I don’t like semihosting (and printf()): because it depends on the toolchain/library version and even more on the SDK. Maybe you are stuck with that, and if you are using the Kinetis SDK, then this article might do the trick: https://mcuoneclipse.com/2016/08/06/semihosting-again-with-nxp-kinetis-sdk-v2-0/
Additionally, try to do assembly level stepping to see what is going on with your printf call.
I hope this helps,
thanks for your quick Reply. Yes, I am using SDK ver. 2. I’ll look through your post and let’s see what will happen
For those of you who might be launching openocd from outside KDS (the only alternative if you are on a Mac), this is how you would enable semihosting:
openocd -f interface/cmsis-dap.cfg -f target/kx.cfg -c ‘init; arm semihosting enable’
This would replace the Debugger Settings section above.
thanks for sharing! I’m sure this is good information for anyone using Mac OS X.
First of all thank you very much for your great tutorials. They helped me out a lot.
I have a hard time using the semihosting with my frdm kl25z.
I have a “blank” sdk2 project with a main that does nothing more then the
followed by a printf () and a NOP loop.
I set the linker options as you specified (although I also have the linker options -Xlinker -z -Xlinker muldefs enabled by default). Also Semihosting is enabled for P&E debugging.
But I cannot get the semihosting terminal to print anything but “P&E Semihosting Console”.
I am using Linux Ubuntu Gnome 15.10 and Kinetis Design Studio version 3.2.
Do you have any ideas what could be wrong?
Thanks for the flowers, appreciated 🙂
Yes, semihosting with the Kinetis SDK (or in general) is problematic, and things are somewhat fluid too. Maybe https://mcuoneclipse.com/2016/04/09/semihosting-with-eclipse-and-the-nxp-kinetis-sdk-v2-0/ and especially https://mcuoneclipse.com/2016/08/06/semihosting-again-with-nxp-kinetis-sdk-v2-0/ can help you?
Ok it took me literally hours to try to get it working and now it works. Thanks, the second link was the solution. Thanks again.
It seems like, this is the only reliable source in the internet (at least from the sites that I found) on this topic.
I will be visiting regularely
glad to hear that it things are useful for you. And you can subscribe (by email or RSS) on the main page of this blog too. That way you get a notification about new posts.