From 3fff6023602822531efdae30bc8ebf862967f1ef Mon Sep 17 00:00:00 2001 From: Friedrich Beckmann Date: Mon, 25 Jul 2022 17:55:39 +0200 Subject: Initial Commit --- .../projects/murax/wip/.vscode/launch.json | 33 +++++++++++ VexRiscvSocSoftware/projects/murax/wip/makefile | 14 +++++ VexRiscvSocSoftware/projects/murax/wip/readme.md | 10 ++++ VexRiscvSocSoftware/projects/murax/wip/src/crt.c | 64 ++++++++++++++++++++++ VexRiscvSocSoftware/projects/murax/wip/src/main.c | 58 ++++++++++++++++++++ 5 files changed, 179 insertions(+) create mode 100644 VexRiscvSocSoftware/projects/murax/wip/.vscode/launch.json create mode 100755 VexRiscvSocSoftware/projects/murax/wip/makefile create mode 100644 VexRiscvSocSoftware/projects/murax/wip/readme.md create mode 100644 VexRiscvSocSoftware/projects/murax/wip/src/crt.c create mode 100755 VexRiscvSocSoftware/projects/murax/wip/src/main.c (limited to 'VexRiscvSocSoftware/projects/murax/wip') diff --git a/VexRiscvSocSoftware/projects/murax/wip/.vscode/launch.json b/VexRiscvSocSoftware/projects/murax/wip/.vscode/launch.json new file mode 100644 index 0000000..ef5432a --- /dev/null +++ b/VexRiscvSocSoftware/projects/murax/wip/.vscode/launch.json @@ -0,0 +1,33 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": + [ + { + "name": "riscv64-unknown-elf-gdb", + "type": "cppdbg", + "request": "launch", + "MIMode": "gdb", + "miDebuggerPath": "/opt/riscv/bin/riscv64-unknown-elf-gdb", + "miDebuggerServerAddress": "localhost:3333", + "stopAtEntry": true, + //"preLaunchTask": "make", + "preLaunchTask": null, + "program": "${workspaceFolder}/build/democ.elf", + "args": [], + "environment": [], + "cwd": "${workspaceRoot}/build", + "externalConsole": true, + "setupCommands": + [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/VexRiscvSocSoftware/projects/murax/wip/makefile b/VexRiscvSocSoftware/projects/murax/wip/makefile new file mode 100755 index 0000000..b49287c --- /dev/null +++ b/VexRiscvSocSoftware/projects/murax/wip/makefile @@ -0,0 +1,14 @@ +PROJ_NAME=democ +DEBUG=yes +BENCH=no +MULDIV=no + +SRCS = $(wildcard src/*.c) \ + $(wildcard src/*.cpp) + +INC += -I../../../libs/ +INC += -I../libs/ +LDSCRIPT = ../libs/linker.ld + +include ../../../resources/gcc.mk +include ../../../resources/subproject.mk diff --git a/VexRiscvSocSoftware/projects/murax/wip/readme.md b/VexRiscvSocSoftware/projects/murax/wip/readme.md new file mode 100644 index 0000000..14330c1 --- /dev/null +++ b/VexRiscvSocSoftware/projects/murax/wip/readme.md @@ -0,0 +1,10 @@ +# DemoC + +This demo was inpired by: + +https://jacobmossberg.se/posts/2018/08/11/run-c-program-bare-metal-on-arm-cortex-m3.html + +It demonstrates using C to program bare metal app running on RISCV architecture. + +Included is also launch.json, which can be used for debugging the app, from VSCode. To run/debug, program Murax to FPGA and connect JTAG with openocd. + diff --git a/VexRiscvSocSoftware/projects/murax/wip/src/crt.c b/VexRiscvSocSoftware/projects/murax/wip/src/crt.c new file mode 100644 index 0000000..70e6c06 --- /dev/null +++ b/VexRiscvSocSoftware/projects/murax/wip/src/crt.c @@ -0,0 +1,64 @@ +/* +Created by: roman3017 +Inspired by: https://jacobmossberg.se/posts/2018/08/11/run-c-program-bare-metal-on-arm-cortex-m3.html +*/ + +extern unsigned int __global_pointer$; +extern unsigned int _stack_start; +extern unsigned int _bss_start; +extern unsigned int _bss_end; +extern unsigned int _ctors_start; +extern unsigned int _ctors_end; + +void crtStart(); +void irqCallback(); +void main(); + +static void trapEntry(); +static void bssInit(); +static void ctorsInit(); + +unsigned int * initVectors[2] __attribute__ ((section("vectors"))) = { + (unsigned int *) crtStart, // code entry + (unsigned int *) trapEntry, // trap entry +}; + +void crtStart() +{ + /* Initialize sp and gp registers */ + asm volatile ( + "la gp, %0\n\t" + "la sp, %1\n\t" + :: "i"(&__global_pointer$), "i"(&_stack_start) + ); + + bssInit(); + ctorsInit(); + main(); + asm volatile("j ."); +} + +static void trapEntry() +{ + irqCallback(); +} + +static void bssInit() +{ + unsigned int * bss_start_p = &_bss_start; + unsigned int * bss_end_p = &_bss_end; + while(bss_start_p != bss_end_p) { + *bss_start_p++ = 0; + } +} + +static void ctorsInit() +{ + unsigned int * ctors_start_p = &_ctors_start; + unsigned int * ctors_end_p = &_ctors_end; + void(*ctor)(); + while(ctors_start_p != ctors_end_p) { + ctor = (void *)*ctors_start_p++; + ctor(); + } +} diff --git a/VexRiscvSocSoftware/projects/murax/wip/src/main.c b/VexRiscvSocSoftware/projects/murax/wip/src/main.c new file mode 100755 index 0000000..2d92b76 --- /dev/null +++ b/VexRiscvSocSoftware/projects/murax/wip/src/main.c @@ -0,0 +1,58 @@ +#include +#include + +/* bss section */ +uint32_t a, b, c; + +/* ctor section */ +void __attribute__ ((constructor)) defaultConstructor() +{ + c += 3; +} + +/* text section */ +void main() +{ + uint32_t result; + + interruptCtrl_init(TIMER_INTERRUPT); + prescaler_init(TIMER_PRESCALER); + timer_init(TIMER_A); + + TIMER_PRESCALER->LIMIT = 12000-1; //1 ms rate + + TIMER_A->LIMIT = 1000-1; //1 second rate + TIMER_A->CLEARS_TICKS = 0x00010002; + + TIMER_INTERRUPT->PENDINGS = 0xF; + TIMER_INTERRUPT->MASKS = 0x1; + + GPIO_A->OUTPUT_ENABLE = 0x000000FF; + GPIO_A->OUTPUT = 0x00000000; + + UART->STATUS = 2; //Enable RX interrupts + UART->DATA = 'A'; + + result = 0; + a = 1, b = 2; + while(1){ + result += a; + result += b + c; + for(uint32_t idx = 0;idx < 50000;idx++) asm volatile(""); + GPIO_A->OUTPUT = (GPIO_A->OUTPUT & ~0x3F) | ((GPIO_A->OUTPUT + 1) & 0x3F); //Counter on LED[5:0] + } +} + +void irqCallback() +{ + if(TIMER_INTERRUPT->PENDINGS & 1){ //Timer A interrupt + GPIO_A->OUTPUT ^= 0x80; //Toogle led 7 + TIMER_INTERRUPT->PENDINGS = 1; + } + while(UART->STATUS & (1 << 9)){ //UART RX interrupt + UART->DATA = (UART->DATA) & 0xFF; + } +} + + + -- cgit v1.2.3