Skip to content

Commit

Permalink
00 - Semihosting
Browse files Browse the repository at this point in the history
Minimal semihosted ARM Cortex-M "Hello World"
  • Loading branch information
jserv committed May 8, 2015
1 parent 714b783 commit a89bd60
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 0 deletions.
22 changes: 22 additions & 0 deletions 00-Semihosting/Makefile
@@ -0,0 +1,22 @@
CROSS_COMPILE ?= arm-none-eabi-
CC := $(CROSS_COMPILE)gcc
CFLAGS = -fno-common -O0 -std=gnu99 \
-mcpu=cortex-m3 -mthumb \
-T semi.ld -nostartfiles \

TARGET = semi.bin
all: $(TARGET)

$(TARGET): semi.c startup.c
$(CC) $(CFLAGS) $^ -o semi.elf
$(CROSS_COMPILE)objcopy -Obinary semi.elf semi.bin
$(CROSS_COMPILE)objdump -S semi.elf > semi.list

qemu: $(TARGET)
@qemu-system-arm -M ? | grep stm32-p103 >/dev/null || exit
@echo "Press Ctrl-A and then X to exit QEMU"
@echo
qemu-system-arm -M stm32-p103 -semihosting -nographic -kernel semi.bin

clean:
rm -f *.o *.bin *.elf *.list
23 changes: 23 additions & 0 deletions 00-Semihosting/semi.c
@@ -0,0 +1,23 @@
#include <stdint.h>

static int semihost_call(int service, void *opaque)
{
register int r0 asm("r0") = service;
register void *r1 asm("r1") = opaque;
register int result asm("r0");
asm volatile("bkpt 0xab"
: "=r" (result) : "r" (r0), "r" (r1));
return result;
}

enum SEMIHOST_SVC {
SYS_WRITE = 0x05,
};

void main(void)
{
char message[] = "Hello World!\n";
uint32_t param[] = { 1, (uint32_t) message, sizeof(message) };
semihost_call(SYS_WRITE, (void *) param);
while (1);
}
15 changes: 15 additions & 0 deletions 00-Semihosting/semi.ld
@@ -0,0 +1,15 @@
ENTRY(reset_handler)

MEMORY
{
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 128K
}

SECTIONS
{
.text :
{
KEEP(*(.isr_vector))
*(.text)
} >FLASH
}
14 changes: 14 additions & 0 deletions 00-Semihosting/startup.c
@@ -0,0 +1,14 @@
#include <stdint.h>

extern void main(void);
void reset_handler(void)
{
/* jump to C entry point */
main();
}

__attribute((section(".isr_vector")))
uint32_t *isr_vectors[] = {
0,
(uint32_t *) reset_handler, /* code entry point */
};
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -17,6 +17,8 @@ make

Steps
-----
* `00-Semihosting`
- Minimal semihosted ARM Cortex-M "Hello World"
* `00-HelloWorld`
- Enable STM32 USART to print trivial greetings
* `01-HelloWorld`
Expand Down

3 comments on commit a89bd60

@victor22771659
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't exit qemu-stm32 via "Ctrl A+X"
My computer environment is x64 Lubuntu 14.04

@jserv
Copy link
Owner Author

@jserv jserv commented on a89bd60 Nov 10, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should press Ctrl-A and release the keys. Then, press 'X'.

@victor22771659
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks,I ask a stupid question 0_0

Please sign in to comment.