summaryrefslogtreecommitdiff
path: root/c
diff options
context:
space:
mode:
Diffstat (limited to 'c')
-rw-r--r--c/phb/Makefile31
-rw-r--r--c/phb/phb.c74
-rw-r--r--c/phb/rvcontroller.ld45
3 files changed, 150 insertions, 0 deletions
diff --git a/c/phb/Makefile b/c/phb/Makefile
new file mode 100644
index 0000000..dd42b2d
--- /dev/null
+++ b/c/phb/Makefile
@@ -0,0 +1,31 @@
+CC ?= clang
+CFLAGS_MARCH ?= -march=rv32imacb_zicntr_zicond_zicsr_zifencei_zihintpause_zilsd_zclsd_zabha_zacas_zbkb_zbkx_zcb_zcmp_zcmt
+CFLAGS_OPT ?= -O3
+CFLAGS ?= ${CFLAGS_MARCH} ${CFLAGS_OPT}
+
+ifeq (${CC}, clang)
+ CC := ${CC} -target riscv32-none-elf
+endif
+
+.PHONY: all dump load clean
+
+all: phb.hex
+
+phb.o: phb.c
+ clang -target riscv32-none-elf -I../rvcontroller-libraries -march=rv32imacb_zicntr_zicond_zicsr_zifencei_zihintpause_zilsd_zclsd_zabha_zacas_zbkb_zbkx_zcb_zcmp_zcmt -ffreestanding -O3 -c -DRRXING_DE -o phb.o phb.c
+
+phb.elf: ../rvcontroller-libraries/rvcontroller-init.o ../rvcontroller-libraries/rvcontroller-ecalls.o phb.o
+ clang -target riscv32-none-elf -T rvcontroller.ld -nostdlib -nostartfiles -Xlinker --no-warn-rwx-segments -o phb.elf ../rvcontroller-libraries/rvcontroller-init.o phb.o ../rvcontroller-libraries/rvcontroller-ecalls.o
+
+dump: phb.elf
+ riscv32-none-elf-objdump -d phb.elf
+
+phb.hex: phb.elf
+ riscv32-none-elf-objcopy -O ihex phb.elf phb.hex
+
+load: phb.hex
+ bash -c "wl-copy < phb.hex"
+
+clean:
+ rm -f phb.hex phb.elf phb.o init.o
+
diff --git a/c/phb/phb.c b/c/phb/phb.c
new file mode 100644
index 0000000..6f85d09
--- /dev/null
+++ b/c/phb/phb.c
@@ -0,0 +1,74 @@
+#include <stdint.h>
+#include <stdbool.h>
+#include "rvcontroller-ecalls.h"
+
+bool streq(char *a, char *b) {
+ for (int i=0;a[i] != 0 || b[i] != 0;i++) {
+ if (a[i] != b[i]) {
+ return false;
+ }
+ }
+ return true;
+}
+
+void sleep(uint32_t delay) {
+ //This will start having problems after about 130 years
+ //of uptime, but that's probably acceptable
+ uint32_t endtime = rdtime() + delay;
+ while (rdtime() < endtime) {}
+ return;
+}
+
+bool checkdetector(void) {
+ char channelbuf[16];
+ char msgbuf[16];
+ while (digiline_bufferlevel() > 0) {
+ lightweight_mode(0);
+ digiline_receive(channelbuf,16,msgbuf,16);
+ if (streq(channelbuf,"detect")) {
+ return true;
+ }
+ }
+ lightweight_mode(1);
+ return false;
+}
+
+void main() {
+ printstr("Ped Hybrid Beacon\n");
+ printstr("for RVController\nInitializing HW\n");
+ digiline_send("beacon","OFF");
+ digiline_send("ped","RED");
+ digiline_clearbuffer();
+ while (true) {
+ printstr("Idle\n");
+ lightweight_mode(1);
+ printstr("Lightweight: On\n");
+ while (!checkdetector()) {}
+ printstr("Ped detected\nLightweight: Off\n");
+ lightweight_mode(0);
+ printstr("Beacon: Flash Yellow\n");
+ digiline_send("beacon","FLASHYELLOW");
+ sleep(3);
+ printstr("Beacon: Yellow\n");
+ digiline_send("beacon","YELLOW");
+ sleep(5);
+ printstr("Beacon: Red\n");
+ digiline_send("beacon","RED");
+ sleep(1);
+ printstr("Ped: Walk\n");
+ digiline_send("ped","GREEN");
+ sleep(7);
+ digiline_clearbuffer();
+ printstr("Ped: Flash DW\n");
+ digiline_send("ped","FLASHRED");
+ printstr("Beacon: Flash Red\n");
+ digiline_send("beacon","FLASHRED");
+ sleep(15);
+ printstr("Ped: Don't Walk\n");
+ digiline_send("ped","RED");
+ sleep(1);
+ printstr("Beacon: Off\n");
+ digiline_send("beacon","OFF");
+ sleep(15);
+ }
+}
diff --git a/c/phb/rvcontroller.ld b/c/phb/rvcontroller.ld
new file mode 100644
index 0000000..985892b
--- /dev/null
+++ b/c/phb/rvcontroller.ld
@@ -0,0 +1,45 @@
+/* Thanks https://github.com/darklife/darkriscv */
+ __heap_size = 0x200; /* required amount of heap */
+ __stack_size = 0x800; /* required amount of stack */
+ ENTRY(_start);
+ MEMORY
+ {
+ RAM (rwx) : ORIGIN = 0x00000000, LENGTH = 0x10000
+ }
+ SECTIONS
+ {
+ .text :
+ {
+ *(.text.startup)
+ *(.text)
+ *(.text)
+ *(.rodata*)
+ } > RAM
+ .data :
+ {
+ *(.sbss)
+ *(.data)
+ *(.bss)
+ *(.rela*)
+ *(COMMON)
+ } > RAM
+
+ .heap :
+ {
+ . = ALIGN(4);
+ PROVIDE ( end = . );
+ _sheap = .;
+ . = . + __heap_size;
+ . = ALIGN(4);
+ _eheap = .;
+ } >RAM
+
+ .stack :
+ {
+ . = ALIGN(4);
+ _estack = .;
+ . = . + __stack_size;
+ . = ALIGN(4);
+ _sstack = .;
+ } >RAM
+ }