From 2f210dd5616e09e85d9cc913ad0199ab046b4d86 Mon Sep 17 00:00:00 2001 From: Paul Mathieu Date: Mon, 22 Feb 2021 20:37:18 -0800 Subject: [PATCH] uart: add ledctrl app --- uart/led.h | 3 +++ uart/ledctrl.c | 24 ++++++++++++++++++++++++ uart/makefile | 15 +++++++++++++++ uart/util.c | 11 +++++++++++ uart/util.h | 2 ++ 5 files changed, 55 insertions(+) create mode 100644 uart/led.h create mode 100644 uart/ledctrl.c create mode 100644 uart/makefile create mode 100644 uart/util.c create mode 100644 uart/util.h diff --git a/uart/led.h b/uart/led.h new file mode 100644 index 0000000..d7a9db0 --- /dev/null +++ b/uart/led.h @@ -0,0 +1,3 @@ +#define LED_ADDR ((volatile char*)0xC000) + +#define led_write(x) (*LED_ADDR = (x)) diff --git a/uart/ledctrl.c b/uart/ledctrl.c new file mode 100644 index 0000000..38fdddd --- /dev/null +++ b/uart/ledctrl.c @@ -0,0 +1,24 @@ +#include "led.h" +#include "uart.h" + +int main() { + int c; + int led = 0; + + while (1) { + led_write(1 << led); + c = uart_read(); + + if (c == 'a') { + led++; + } else if (c == 'd') { + led--; + } + + if (led == 8) { + led = 0; + } else if (led < 0) { + led = 7; + } + } +} diff --git a/uart/makefile b/uart/makefile new file mode 100644 index 0000000..28a0669 --- /dev/null +++ b/uart/makefile @@ -0,0 +1,15 @@ +CC = ../bin/cc +LD = ../bin/ld + +all: echo.vhdl.inc ledctrl.vhdl.inc + +echo.vhdl.inc: echo.o uart.o +ledctrl.vhdl.inc: ledctrl.o uart.o + +%.vhdl.inc: + $(LD) -o $@ --vhdl $^ + +PHONY: clean + +clean: + rm -rf *.o *.vhdl.inc diff --git a/uart/util.c b/uart/util.c new file mode 100644 index 0000000..8f76cc4 --- /dev/null +++ b/uart/util.c @@ -0,0 +1,11 @@ +#define CYCLES_PER_MS 6666 // ish + +/** waits a general amount of time */ +void busy_sleep(int ms) { + for (int i = 0; i < ms; ++i) { + // wait for 1 ms + for (int i = 0; i < CYCLES_PER_MS; ++i) { + // nothing + } + } +} diff --git a/uart/util.h b/uart/util.h new file mode 100644 index 0000000..665b791 --- /dev/null +++ b/uart/util.h @@ -0,0 +1,2 @@ +/** waits a general amount of time */ +void busy_sleep(int ms);