diff --git a/mbv/Makefile b/mbv/Makefile index 854fda4..cc9e485 100644 --- a/mbv/Makefile +++ b/mbv/Makefile @@ -14,6 +14,10 @@ wozmon: ## Build the wozmon app in docker timer: ## Build the timer app in docker docker build -o . --target export --build-arg TARGET=timer.bin . +.PHONY: uart +uart: ## Build the uart app in docker + docker build -o . --target export --build-arg TARGET=uart.bin . + .PHONY: dev-image dev-image: docker build -t mbv-dev --target dev . diff --git a/mbv/apps/uart/uart.cc b/mbv/apps/uart/uart.cc new file mode 100644 index 0000000..9f2c538 --- /dev/null +++ b/mbv/apps/uart/uart.cc @@ -0,0 +1,87 @@ +#include + +#include "intc.h" +#include "interrupts.h" +#include "pol0.h" +#include "xuartlite.h" + +extern "C" { +void BiosWozmon(); +} + +namespace { + +struct Gpio { + volatile uint32_t data; +}; + +Gpio* leds = reinterpret_cast(GPIO0_BASE); + +XUartLite uart0_inst; +XUartLite_Config uart0_config = { + .DeviceId = 0, + .RegBaseAddr = UART0_BASE, + .BaudRate = 115200, + .UseParity = false, + .DataBits = 8, +}; + +XUartLite* uart0 = &uart0_inst; + +volatile int incoming = 0; + +void HandleUartRxFromIsr(void*, unsigned int) { + incoming += 1; +} + +void HandleUartTxFromIsr(void*, unsigned int) { +} + +void Uart0Isr() { + XUartLite_InterruptHandler(uart0); +} + +void InitUarts() { + XUartLite_CfgInitialize(uart0, &uart0_config, uart0_config.RegBaseAddr); + + XUartLite_SetSendHandler(uart0, HandleUartTxFromIsr, nullptr); + XUartLite_SetRecvHandler(uart0, HandleUartRxFromIsr, nullptr); + XUartLite_EnableInterrupt(uart0); + + SetIsr(UART0_IRQN, Uart0Isr); + SetIrqEnabled(UART0_IRQN, true); +} + +} + +int main() { + leds->data = 0xa0; + + InitUarts(); + EnableInterrupts(); + SetExternalInterruptHandler(InterruptHandler); + EnableExternalInterrupts(); + + int counter = 0; + + uint8_t c; + while (XUartLite_Recv(uart0, &c, 1) > 0) { + XUartLite_Send(uart0, &c, 1); + while (XUartLite_IsSending(uart0)) {} + } + leds->data = 0xa1; + while (1) { + // should disable interrupts around this + if (incoming > 0) { + counter += 1; + leds->data = counter; + XUartLite_Send(uart0, &c, 1); + while (XUartLite_IsSending(uart0)) {} + while (XUartLite_Recv(uart0, &c, 1) > 0) { + XUartLite_Send(uart0, &c, 1); + while (XUartLite_IsSending(uart0)) {} + } + incoming -= 1; + } + } +} diff --git a/mbv/configure b/mbv/configure index 3acef53..bba3a6c 100755 --- a/mbv/configure +++ b/mbv/configure @@ -277,6 +277,7 @@ all = [ app_image("helloworld"), app_image("wozmon"), app_image("timer"), + app_image("uart"), ]