Skip to content

Commit

Permalink
Add mps2-500
Browse files Browse the repository at this point in the history
  • Loading branch information
potsrevennil committed Oct 22, 2024
1 parent 313e222 commit bb4e607
Show file tree
Hide file tree
Showing 21 changed files with 240 additions and 8,513 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/mps2-an500.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: mps2-an500 build
on:
push:
branches:
- master
pull_request:
branches: [ "master" ]
jobs:
build-all:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
submodules: recursive
- name: Install Toolchain
uses: carlosperate/[email protected]
with:
release: 13.2.Rel1
- name: Build All (mps2-an500)
run: make PLATFORM=mps2-an500 -j2
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@
path = slothy
url = https://github.com/slothy-optimizer/slothy
branch = armv7m
[submodule "mbed-os"]
path = mbed-os
url = https://github.com/ARMmbed/mbed-os.git
186 changes: 88 additions & 98 deletions common/hal-mps2.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
// SPDX-License-Identifier: Apache-2.0 or CC0-1.0
#include <hal.h>
#if defined(MPS2_AN386)
#include <CMSDK_CM4.h>
#elif defined(MPS2_AN500)
#include <CMSDK_CM7.h>
#else
#error Unsupported mps2 board
#endif

#define BAUD 38400

Expand All @@ -10,11 +16,10 @@
#endif

/* The startup file calls a SystemInit function. */
void SystemInit(void)
{
void SystemInit(void) {
/* Enable the FPU */
SCB->CPACR |= ((3UL << 10 * 2) | /* set CP10 Full Access */
(3UL << 11 * 2)); /* set CP11 Full Access */
SCB->CPACR |= ((3UL << 10 * 2) | /* set CP10 Full Access */
(3UL << 11 * 2)); /* set CP11 Full Access */
/* Enable UART */
/* TODO: Validate this on a *real* MPS2 board (works in QEMU) */
CMSDK_GPIO0->ALTFUNCSET |= 1u;
Expand All @@ -27,19 +32,16 @@ void SystemInit(void)
NVIC_SetPriority(SysTick_IRQn, (1UL << __NVIC_PRIO_BITS) - 1UL);
NVIC_EnableIRQ(SysTick_IRQn);
SysTick->VAL = 0UL;
SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_TICKINT_Msk | SysTick_CTRL_ENABLE_Msk;
SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_TICKINT_Msk |
SysTick_CTRL_ENABLE_Msk;
}

static volatile unsigned long long overflowcnt = 0;

/* SysTick Interrupt */
void SysTick_Handler(void)
{
++overflowcnt;
}
void SysTick_Handler(void) { ++overflowcnt; }

uint64_t hal_get_time()
{
uint64_t hal_get_time() {
while (1) {
unsigned long long before = overflowcnt;
unsigned long long result = (before + 1) * 16777216llu - SysTick->VAL;
Expand All @@ -49,20 +51,16 @@ uint64_t hal_get_time()
}
}

void hal_setup(const enum clock_mode clock)
{
(void) clock;
}
void hal_setup(const enum clock_mode clock) { (void)clock; }

static inline void uart_putc(int c)
{
while(CMSDK_UART0->STATE & CMSDK_UART_STATE_TXBF_Msk);
static inline void uart_putc(int c) {
while (CMSDK_UART0->STATE & CMSDK_UART_STATE_TXBF_Msk)
;
CMSDK_UART0->DATA = c & 0xFFu;
}

void hal_send_str(const char* in)
{
const char* cur = in;
void hal_send_str(const char *in) {
const char *cur = in;
while (*cur) {
uart_putc(*cur);
cur += 1;
Expand All @@ -72,7 +70,7 @@ void hal_send_str(const char* in)

#if !defined(NO_SEMIHOSTING_EXIT)
// TODO(dsprenkels) Currently, we only exit the QEMU host when a the program
// exists sucessfully. We should also populate some interrupts handlers that
// exists successfully. We should also populate some interrupts handlers that
// occur on errors and/or other exception.

// These two syscall values are used at the end of the program, when we want
Expand All @@ -83,19 +81,20 @@ static const uint32_t ApplicationExit = 0x20026;

// Do a system call towards QEMU or the debugger.
static uint32_t semihosting_syscall(uint32_t nr, const uint32_t arg) {
__asm__ volatile (
"mov r0, %[nr]\n"
"mov r1, %[arg]\n"
"bkpt 0xAB\n"
"mov %[nr], r0\n"
: [nr] "+r" (nr) : [arg] "r" (arg) : "0", "1");
return nr;
__asm__ volatile("mov r0, %[nr]\n"
"mov r1, %[arg]\n"
"bkpt 0xAB\n"
"mov %[nr], r0\n"
: [nr] "+r"(nr)
: [arg] "r"(arg)
: "0", "1");
return nr;
}

// Register a destructor that will call qemu telling them that the program
// has exited successfully.
static void __attribute__ ((destructor)) semihosting_exit(void) {
semihosting_syscall(REPORT_EXCEPTION, ApplicationExit);
static void __attribute__((destructor)) semihosting_exit(void) {
semihosting_syscall(REPORT_EXCEPTION, ApplicationExit);
}

void NMI_Handler(void) {
Expand Down Expand Up @@ -146,43 +145,41 @@ void Default_Handler(void) {

/* End of BSS is where the heap starts (defined in the linker script) */
extern char end;
static char* heap_end = &end;
static char *heap_end = &end;

void* __wrap__sbrk (int incr)
{
char* prev_heap_end;
void *__wrap__sbrk(int incr) {
char *prev_heap_end;

prev_heap_end = heap_end;
heap_end += incr;

return (void *) prev_heap_end;
return (void *)prev_heap_end;
}

size_t hal_get_stack_size(void)
{
register char* cur_stack;
__asm__ volatile ("mov %0, sp" : "=r" (cur_stack));
size_t hal_get_stack_size(void) {
register char *cur_stack;
__asm__ volatile("mov %0, sp" : "=r"(cur_stack));
return cur_stack - heap_end;
}

const uint32_t stackpattern = 0xDEADBEEFlu;

static void* last_sp = NULL;

void hal_spraystack(void)
{

char* _heap_end = heap_end;
asm volatile ("mov %0, sp\n"
".L%=:\n\t"
"str %2, [%1], #4\n\t"
"cmp %1, %0\n\t"
"blt .L%=\n\t"
: "+r" (last_sp), "+r" (_heap_end) : "r" (stackpattern) : "cc", "memory");
static void *last_sp = NULL;

void hal_spraystack(void) {

char *_heap_end = heap_end;
asm volatile("mov %0, sp\n"
".L%=:\n\t"
"str %2, [%1], #4\n\t"
"cmp %1, %0\n\t"
"blt .L%=\n\t"
: "+r"(last_sp), "+r"(_heap_end)
: "r"(stackpattern)
: "cc", "memory");
}

size_t hal_checkstack(void)
{
size_t hal_checkstack(void) {
size_t result = 0;
asm volatile("sub %0, %1, %2\n"
".L%=:\n\t"
Expand All @@ -194,7 +191,9 @@ size_t hal_checkstack(void)
"cmp %2, %1\n\t"
"blt .L%=\n\t"
".LE%=:\n"
: "+r"(result) : "r" (last_sp), "r" (heap_end), "r" (stackpattern) : "ip", "cc");
: "+r"(result)
: "r"(last_sp), "r"(heap_end), "r"(stackpattern)
: "ip", "cc");
return result;
}

Expand All @@ -204,76 +203,67 @@ size_t hal_checkstack(void)
#undef errno
extern int errno;

int __wrap__open(char *file, int flags, int mode)
{
(void) file;
(void) flags;
(void) mode;
int __wrap__open(char *file, int flags, int mode) {
(void)file;
(void)flags;
(void)mode;
errno = ENOSYS;
return -1;
}

int __wrap__close(int fd)
{
int __wrap__close(int fd) {
errno = ENOSYS;
(void) fd;
return -1;
(void)fd;
return -1;
}

#include <sys/stat.h>

int __wrap__fstat(int fd, struct stat* buf)
{
(void) fd;
(void) buf;
int __wrap__fstat(int fd, struct stat *buf) {
(void)fd;
(void)buf;
errno = ENOSYS;
return -1;
return -1;
}

int __wrap__getpid(void)
{
int __wrap__getpid(void) {
errno = ENOSYS;
return -1;
return -1;
}

int __wrap__isatty(int file)
{
(void) file;
int __wrap__isatty(int file) {
(void)file;
errno = ENOSYS;
return 0;
}

int __wrap__kill(int pid, int sig)
{
(void) pid;
(void) sig;
int __wrap__kill(int pid, int sig) {
(void)pid;
(void)sig;
errno = ENOSYS;
return -1;
return -1;
}

int __wrap__lseek(int fd, int ptr, int dir)
{
(void) fd;
(void) ptr;
(void) dir;
int __wrap__lseek(int fd, int ptr, int dir) {
(void)fd;
(void)ptr;
(void)dir;
errno = ENOSYS;
return -1;
return -1;
}

int __wrap__read(int fd, char* ptr, int len)
{
(void) fd;
(void) ptr;
(void) len;
int __wrap__read(int fd, char *ptr, int len) {
(void)fd;
(void)ptr;
(void)len;
errno = ENOSYS;
return -1;
return -1;
}

int __wrap__write(int fd, const char* ptr, int len)
{
(void) fd;
(void) ptr;
(void) len;
int __wrap__write(int fd, const char *ptr, int len) {
(void)fd;
(void)ptr;
(void)len;
errno = ENOSYS;
return -1;
return -1;
}
Loading

0 comments on commit bb4e607

Please sign in to comment.