In “Semihosting with Kinetis Design Studio” I used printf() to exchange text and data between the target board and the host using the debug connection. Kinetis Design Studio (KDS) has that semihosting baked into its libraries. What about if using the GNU ARM Embedded (launchpad) tools and libraries (see “Switching ARM GNU Tool Chain and Libraries in Kinetis Design Studio“)? Actually it requires two more steps, but is very easy too.
There are three things to be in place to use semihosting with the GNU ARM Embedded (Launchpad) libraries:
- Option in the GNU linker settings
- Enabling semihosting in the debugger settings
- Initializing the GNU libraries
To enable semihosting for the GNU ARM Embedded (Launchpad) libraries, I need to add
to the linker options:
In case I’m using newlib-nano and want to use printf() and/or scanf() with floating point support, I need to pull in some symbols explicitly with the linker options ‘u':
-u _scanf_float -u _printf_float
In the GNU ARM Eclipse plugins, I need to enable semihosting.
For Segger J-Link, I enable the console in the launch configuration:
Additionally I enable semihosting options in the startup options of the debugger:
For P&E the following settings are used:
Settings for OpenOCD
The following settings are used for OpenOCD:
Initializing the GNU Libraries
If you would now try to use semihosting with running the debugger, you probably will get error messages like this (e.g. from Segger J-Link):
WARNING: Semihosting command SYS_FLEN failed. Handle is 0. WARNING: Semihosting command SYS_WRITE failed. Handle is 0. WARNING: Semihosting command SYS_WRITE failed. Handle is 0. WARNING: Semihosting command SYS_WRITE failed. Handle is 0.
The reason is that the semihosting needs to be enabled by the application. I need to call
initialise_monitor_handles() before I’m using
With this, I can use printf() and scanf() through a debugger connection.
While I don’t like printf() for many reasons, sometimes it is useful to exchange data with the host. Using semihosting no physical connection is required, as the communication goes through the debugger. It is somewhat intrusive, and adds code and data overhead, but the GNU ARM Embedded (launchpad) libraries (both newlib and newlib-nano) have semihosting built-in. It is a matter to enable it in the linker and debugger settings, and to initialize the handles in the application.
Happy Semihosting :-)