Home | Projects | Notes > ARM Cortex-M3/M4 Processor > Semihosting
Semihosting is a mechanism that enabels code running on an ARM target to communicate and use the input/output facilities on a host computer that is running a debugger.
Examples of these facilities include keyboard input, screen output, and disk I/O.
To use semihosting feature, the host, OpenOCD in our case, must be running already.
The printf()
message will be pulled by the host application (i.e., OpenOCD), and then will be displayed on the OpenOCD console. That is why OpenOCD must be running and should be in connection with the target hardware.
To use the semihosting feature, you just need to specify the spec: --specs=rdimon.specs
.
Newlib-Nano (libc_nano.a
) was used when the specified spec was --specs=nano.specs
.
Also, in the main application of your project, you need to initialize the semihosting feature.
xxxxxxxxxx
51/* main application */
2extern void initialise_monitor_handles(void);
3...
4initialise_monitor_handles(); /* this function must be called */
5...
initialise_monitor_handles()
initializes routines and libraries that are necessary for semihosting.
When using semihosting feature, make sure that you are not linking syscalls.o
that you wrote on your own. Now, the semihosting libraries will be providing the low-level system calls. So, if it is still included in your makefile dependency/recipie, exclude it from building.
Error resolution:
I got the following error.
xxxxxxxxxx
131$ make sh
2
3arm-none-eabi-gcc -c -mcpu=cortex-m4 -mthumb -mfloat-abi=soft -std=gnu11 -Wall -o0 -o main.o main.c # recipie: command to generate the target
4# arm-none-eabi-gcc -c -mcpu=cortex-m4 -mthumb -mfloat-abi=soft -std=gnu11 -Wall -o0 main.c -o main.o
5# arm-none-eabi-gcc -c -mcpu=cortex-m4 -mthumb -mfloat-abi=soft -std=gnu11 -Wall -o0 main.c -o main.o
6# 'main.c' represents dependency, 'main.o' represents target (@ does look like a target :))
7arm-none-eabi-gcc -c -mcpu=cortex-m4 -mthumb -mfloat-abi=soft -std=gnu11 -Wall -o0 -o led.o led.c
8arm-none-eabi-gcc -c -mcpu=cortex-m4 -mthumb -mfloat-abi=soft -std=gnu11 -Wall -o0 -o stm32_startup.o stm32_startup.c
9arm-none-eabi-gcc -mcpu=cortex-m4 -mthumb -mfloat-abi=soft --specs=rdimon.specs -T stm32_ls.ld -Wl,-Map=final.map -o final_sh.elf main.o led.o stm32_startup.o
10/usr/lib/gcc/arm-none-eabi/10.3.1/../../../arm-none-eabi/bin/ld: /usr/lib/gcc/arm-none-eabi/10.3.1/../../../arm-none-eabi/lib/thumb/v7e-m/nofp/rdimon-crt0.o: in function `_mainCRTStartup':
11/build/newlib-pB30de/newlib-3.3.0/build/arm-none-eabi/thumb/v7e-m/nofp/libgloss/arm/../../../../../../../libgloss/arm/crt0.S:547: undefined reference to `__end__'
12collect2: error: ld returned 1 exit status
13make: *** [makefile:35: final_sh.elf] Error 1
It means that the semihosting needs
__end__
instead of what we have (end
). Define it in the.bss
section.
When using printf()
to display any message with semihosting, make sure to end your message with \n
character to specify the end of the string. This is required for semihosting to know where the string ends.
Connect your board to the host machine.
Run OpenOCD with the board configuration file
xxxxxxxxxx
11openocd -f /board/stm32f4discovery.cfg
Full path to the board configuration file:
\usr\share\openocd\scripts\board\stm32f4discovery.cfg
Leave this terminal busy, and launch another terminal. Go to project directory and do Step 3.
Connect to the OpenOCD via GDB Client or Telnet Client
Using GDB Client:
xxxxxxxxxx
31gdb-multiarch
2(gdb) target remote localhost:3333
3(gdb) monitor reset init
Keyword
monitor
allows you to issue OpenOCD command on GDB client. (This keyword is not necessary if you are issuing OpenOCD command on Telnet.)
Using Telnet
xxxxxxxxxx
21telnet localhost 4444
2reset init
You are not bypassing GDB Client, so
monitor
keyword is not necessary. Just directly type in commands.c.f. To terminate Telnet session,
Ctrl+]
close
.
Issue commands over GDB Client or Telnet to OpenOCD to download and debug the code.
xxxxxxxxxx
11(gdb) monitor flash write_image erase final_sh.elf
xxxxxxxxxx
11flash write_image erase final_sh.elf
Enable the semihosting feature in the OpenOCD
xxxxxxxxxx
11(gdb) monitor arm semihosting enable
xxxxxxxxxx
11arm semihosting enable
Nayak, K. (2022). Embedded Systems Programming on ARM Cortex-M3/M4 Processor [Video file]. Retrieved from https://www.udemy.com/course/embedded-system-programming-on-arm-cortex-m3m4/