mbv: add uart echo app

This commit is contained in:
Paul Mathieu 2025-06-23 08:23:59 -07:00
parent d91ddeea22
commit 73edb77577
3 changed files with 92 additions and 0 deletions

View File

@ -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 .

87
mbv/apps/uart/uart.cc Normal file
View File

@ -0,0 +1,87 @@
#include <cstdint>
#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<Gpio*>(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;
}
}
}

1
mbv/configure vendored
View File

@ -277,6 +277,7 @@ all = [
app_image("helloworld"),
app_image("wozmon"),
app_image("timer"),
app_image("uart"),
]