How to use openOCD semi-hosting support

This page will guide you on how to run bare-metal newlib printf demo on following target boards.

Nitrogen Cortex-M4 target:

Startup code for cortex M4 MCUs CMSIS:

Source:

Building a semi-hosting printf demo for Nitrogen Board:

Make sure your gcc_arm.ld reflects memory layout of nitrogen board in MEMORY section.

You may use startup code either from startup_ARMCM4.S or startup_ARMCM4.c

Pre-requisite:

  • arm-none-eabi-gcc 

Sample demo code:

#include <stdio.h>
int main()
{
    printf("Hello, world!");
	printf("\n");
	return 0;
}


#ifndef __NO_SYSTEM_INIT
void SystemInit()
{}
#endif

Build command line:

arm-none-eabi-gcc semihost.c startup_ARMCM4.S -mthumb -mcpu=cortex-m4  -ffunction-sections -fdata-sections --specs = rdimon.specs -lc -T gcc_arm.ld -o semihost-CM4.axf

Start openOCD connected to Nitrogen board via CMSIS-DAP:

Instructions to build openOCD from source can be found here:

./src/openocd -s tcl/ -f interface/cmsis-dap.cfg -c "transport select swd" -f target/nrf52.cfg

Run demo using GDB:

  • Start arm-none-eabi-gdb with Nitrogen board semihosting bare-metal demo

    arm-none-eabi-gdb semihost-CM4.axf
  • Connect to OpenOCD GDB stub while OpenOCD is connected to Nitrogen board via CMSIS-DAP

    target extended-remote :3333
  • Send command to OpenOCD to enable arm semihosting via gdb monitor command

    monitor arm semihosting enable
  • Load executable to the tagert

    load
  • Run demo and you will see semihosting printf output on OpenOCD console

    run
  • Enable OpenOCD semihosting_filio to view printf output on gdb console

    monitor arm semihosting_fileio enable


Beaglebone Black Arm cortex A8 target:

Make sure following configuration is applied to openOCD source to enable bare-metal debugging on Beaglebone Black.

Pre-requisite:

  • arm-none-eabi-gcc 

Sample demo code:

#include <stdio.h>
int main()
{
    printf("Hello, world!");
	printf("\n");
	return 0;
}

Build command line:

arm-none-eabi-gcc --specs=rdimon.specs -Xlinker -Ttext-segment=0x80000000 -Xlinker --defsym=__stack=0x80040000 -o arm-test-rdimon sample_demo.c

Start openOCD connected to Beaglebone Black via flyswatter2 debug adapter:

Instructions to build openOCD from source can be found here:

./src/openocd -s tcl/ -f interface/ftdi/flyswatter2.cfg -f board/ti_beaglebone_black.cfg

Run demo using GDB:

  • Start arm-none-eabi-gdb with Beaglebone Black board semihosting bare-metal demo

    arm-none-eabi-gdb arm-test-rdimon
  • Connect to OpenOCD GDB stub while OpenOCD is connected to Beaglebone Black via flyswatter2 debug adapter

    target extended-remote :3333
  • Send command to OpenOCD to enable arm semihosting via gdb monitor command

    monitor arm semihosting enable
  • Load executable to the tagert

    load
  • Run demo and you will see semihosting printf output on OpenOCD console

    run
  • Enable OpenOCD semihosting_filio to view printf output on gdb console

    monitor arm semihosting_fileio enable

Note: You might have to connect/disconnect GDB with target a couple of times to ensure target is in stable connected state with openOCD.


Hikey 96Board AArch64 cortex A53 target:

Make sure following configuration is applied to openOCD source to enable bare-metal debugging on Hikey.

Pre-requisite:

  • aarch64-elf-gcc 

Note: You may also need Newlib sisroot and in that case have to build newlib for aarch64 targets

Sample demo code:

#include <stdio.h>
int main()
{
    printf("Hello, world!");
	printf("\n");
	return 0;
}

Build command line:

aarch64-elf-gcc -g -O0 -I${newlib-install}/aarch64-elf/include --sysroot=${newlib-install}/aarch64-elf/ -Xlinker --defsym=__stack=0x05D00000 --specs=rdimon.specs -o aarch64-test-rdimon sample_demo.c

Start openOCD connected to Hikey Board via BusBlaster v2 debug adapter:

Instructions to build openOCD from source can be found here:

./src/openocd -s tcl/ -f interface/ftdi/dp_busblaster.cfg -f board/lemaker_hikey.cfg

Run demo using GDB:

  • Start AArch64-elf-gdb with Hikey board semihosting bare-metal demo

    aarch64-elf-gdb aarch64-test-rdimon
  • Connect to OpenOCD GDB stub while OpenOCD is connected to Hikey via BusBlaster v2 debug adapter

    target extended-remote :3333
  • Send command to OpenOCD to enable arm semihosting via gdb monitor command

    monitor arm semihosting enable
  • Load executable to the tagert

    load
  • Run demo and you will see semihosting printf output on OpenOCD console

    run
  • Enable OpenOCD semihosting_filio to view printf output on gdb console

    monitor arm semihosting_fileio enable

Note: You might have to connect/disconnect GDB with target a couple of times to ensure target is in stable connected state with openOCD.