Skip to content

Instantly share code, notes, and snippets.

@nuft
Created December 20, 2015 14:24
Show Gist options
  • Save nuft/fa44ba827aff567ea410 to your computer and use it in GitHub Desktop.
Save nuft/fa44ba827aff567ea410 to your computer and use it in GitHub Desktop.
ARMv7-M Semihosting test
#include "ch.h"
#include "hal.h"
#include <string.h>
static THD_WORKING_AREA(waThread1, 128);
static THD_FUNCTION(Thread1, arg)
{
(void)arg;
chRegSetThreadName("blinker");
while (true) {
palSetPad(GPIOD, GPIOD_LED3); /* Orange. */
chThdSleepMilliseconds(500);
palClearPad(GPIOD, GPIOD_LED3); /* Orange. */
chThdSleepMilliseconds(500);
}
}
int send_command(int command, void *message)
{
int ret;
asm("mov r0, %[cmd];"
"mov r1, %[msg];"
"bkpt #0xAB;"
"mov %[ret], r0;"
: [ret] "=r" (ret)
: [cmd] "r" (command), [msg] "r" (message)
: "r0", "r1", "memory");
return ret;
}
// source: http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0471l/pge1358787045051.html
// mode number: 0 1 2 3 4 5 6 7 8 9 10 11
// ISO C fopen mode: r rb r+ r+b w wb w+ w+b a ab a+ a+b
#define SYS_OPEN 0x01
#define SYS_CLOSE 0x02
#define SYS_WRITE 0x05
#define SYS_READ 0x06
int main(void)
{
halInit();
chSysInit();
chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
const char *filename = "hello.txt";
uint32_t opencmd[] = {(uint32_t)filename, 0 /*"r"*/, strlen(filename)};
int file = send_command(SYS_OPEN, opencmd);
uint32_t read_len = 20;
int res;
static char buf[42];
uint32_t readcmd[] = {file, (uint32_t)&buf[0], read_len};
res = send_command(SYS_READ, readcmd);
uint32_t closecmd[] = {file};
res = send_command(SYS_CLOSE, closecmd);
while (true) {
chThdSleepMilliseconds(500);
uint32_t m[] = {1/*stdout*/, (uint32_t)&buf[0], read_len};
send_command(SYS_WRITE, m);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment