Semihosting

From SEGGER Wiki
Jump to: navigation, search

Semihosting is a mechanism for ARM based target devices to provide a way to communicate with a host system to allow different operations to be performed. Such operations can be sending and receiving data from and to the host system and file I/O operations. These can also be automated and used for e.g. unit testing. The biggest drawback is that the target device needs to be halted for each interaction between host and target device which will impact the real-time behaviour of a system. Additional information about semihosting can be found in the J-Link user manual (UM08001).

Semihosting with Embedded Studio

The implementation of semihosting in Embedded Studio is straight forward. Standard library functions for fopen, fwrite, fread etc. are already implemented and just need to be called. It is recommended to use the project wizard when creating a new project so all default settings are set correctly.

The following example project opens a file on your host system and writes a string into it triggered by your target device: Semihosting Embedded Studio

Make sure the file path is existing otherwise the project will not run. You can of course change the path by simply editing variable pFilename and rebuild it with Embedded Studio.

Prerequisites for the example project are:

  • Embedded Studio V4.16 or later
  • Cortex-M Trace Reference Board (ST STM32F407VE)
  • J-Link V10 or later

Baremetal Semihosting with Ozone

Ozone is a debugger so it does not come with standard libraries and predefined semihosting code like it does with some IDEs. Thus to make use of semihosting the semihosting calls need to be implemented manually by the user in the target application. The big benefit of this approach without predefined library functions is that you get full control about the complexity of the semihosting implementation so you can write more efficient code as the library overhead is missing.

A baremetal example implementation can be found in the following example project: Semihosting Ozone

The example project will open a file on the host system and write a string to it. Make sure the file path is existing otherwise the project will not run. You can of course change the path by simply editing variable pFilename and rebuild it with Embedded Studio.

Prerequisites for the example project are:

  • Ozone V2.62 or later
  • Embedded Studio V4.16 or later
  • Cortex-M Trace Reference Board (ST STM32F407VE)
  • J-Link V10 or later

This baremetal code can be used with any debug software which supports semihosting handling.

Semihosting with newlib and GNU Arm toolchain

This section will cover how semihosting can be used with Eclipse in combination with GDB and newlib. Generally newlib will only provide a rudimentary setup for semihosting so it will not work out-of-thebox except for printf calls. All callbacks used by e.g. fopen need to be implemented by the user to utilize semihosting.

The following project will show such an example project and create a file at a certain path, fill it with a string and close that file: Semihosting Eclipse

Make sure the file path is existing otherwise the project will not run. You can of course change the path by simply editing variable pFilename and rebuild it with Eclipse. As a base project a default J-Link debug project was created for Cortex-M. Then a debug config with the correct target device was set up. Next make sure that in the debug config semihosting is enabled.

The example project comes also with an Ozone setup that runs out-of-the-box should Eclipse not be the debugger of your choice.

Prerequisites for the example project are:

  • Eclipse Cpp 2019-03
  • Latest GNU ARM Eclipse plugin
  • Cortex-M Trace Reference Board (ST STM32F407VE)
  • J-Link V10 or later
  • (optional) Ozone V2.62 or later

Semihosting with EWARM

The implementation of semihosting in EWARM is generally available but requires some extra settings in your project. Standard library functions for fopen, fwrite, fread etc. are implemented and just need to be called. Under project options semihosting must be enabled as well as the library support must be set to "full" which will drastically increase memory usage of your project.

The project options can be found at the following project settings:

EWARM Library setting.png

Set the library level to "Full" and the library low level implementation to semihosting each.

Note: This will also increase your application size immensely so make sure your target device has enough memory resources left.

The following example project opens a file on your host system and writes a string into it triggered by your target device: Semihosting EWARM

Make sure the file path is existing otherwise the project will not run. You can of course change the path by simply editing variable pFilename and rebuild it with Embedded Studio.

Prerequisites for the example project are:

  • EWARM V8.322 or later
  • Cortex-M Trace Reference Board (ST STM32F407VE)
  • J-Link V10 or later