Semihosting

Have you ever had situation when you wanted to send string over uart from microcontroller to PC, but you didn't have UART-USB converter? I bet you have experienced it. Today I will show you a workaround of it.

Programing console or windows application on Windows or Linux is easier than programming embedded systems in some cases. I mean printf function is present default . Printf function allows show string in formatted way. Embedded systems built on microcontrollers also allows usage printf function. Today I will show you how to implement printf function in semihosting mode. Semihosting is the feature of the st-link debugger. STM32F401 development board, and SW4STM32 workbench will be used. Depending on debugger and integrated development environment(IDE) enabling of semihosting may look different.

There are just few steps to enable printf capability.

1. Add monitor arm semihosting enable command in Debug configurations
2. Add -specs=rdimon.specs -lc -lrdimon and -u_printf_float
First command enables semihosting. Second one enables the float printf formatting. Without this printf does not work with floating point numbers.

3. Add extern initialise_monitor_handles(void) and call initialise_monitor_handles() function
Now we can try it. Please click debug and see what is going on :)
It works, but works slow. How slow does it work? According to previous posts, we can try it.
For the test we will send the following well-known string.

" Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin nibh augue, suscipit a, scelerisque sed, lacinia in, mi."


As you can see it takes 357ms. It is quite long, because it can blocks another part of code.
Let's summary advantages and disadvantages of the Semihosting

Advantages:
  • Almost every development board has debugger.

  • There is no need to have UART-USB converter.

Disadvatages
  • As It has been shown semihosting is slow.

  • Semihosting works only in the debug mode, that is why, it can not be used in production code.

  • Semhosting uses breakpoint therefore CPU is halted.

Semihosting halts the CPU so I would recommend you semihosting in critical situations only, such as UART-USB converter is broken but printf is really needed.

In next post I will show you one more method how to implement printf functionality in embedded systems.

See you next time

Comments

Popular posts from this blog

Two ways to compare a variable in a condition statement

How long the piece of code is executed?