mbv: move a few things around

This commit is contained in:
Paul Mathieu 2025-09-04 15:01:50 +02:00
parent dd51b5d610
commit 9edebe637b
58 changed files with 68 additions and 494 deletions

View File

@ -28,14 +28,10 @@ dev: dev-image ## Run a dev container
.PHONY: clean .PHONY: clean
clean: ## Remove generated files clean: ## Remove generated files
rm -rf *.elf *.bin $(all_objects) $(deps) rm -rf *.elf *.bin
rm -rf test/ *.dSYM $(test_deps) *.o
.PHONY: help .PHONY: help
help: ## Show this help help: ## Show this help
@echo Noteworthy targets: @echo Noteworthy targets:
@egrep '^[a-zA-Z_-]+:.*?## .*$$' $(firstword $(MAKEFILE_LIST)) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}' @egrep '^[a-zA-Z_-]+:.*?## .*$$' $(firstword $(MAKEFILE_LIST)) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}'
.DEFAULT_GOAL := help .DEFAULT_GOAL := help
#-include $(deps)
#-include $(test_deps)

View File

@ -1,27 +0,0 @@
#pragma once
#include <cstdint>
struct Gpio {
volatile uint32_t data;
};
#define gpio0 ((Gpio*)0x40000000)
inline void ToggleLed(int which) {
uint8_t data = gpio0->data;
data ^= (0x1 << which);
gpio0->data = data;
}
inline void SetLed(int which) {
uint8_t data = gpio0->data;
data |= (0x1 << which);
gpio0->data = data;
}
inline void ClearLed(int which) {
uint8_t data = gpio0->data;
data &= ~(0x1 << which);
gpio0->data = data;
}

View File

@ -1,11 +1,11 @@
#include "async.h" #include "hal/gpio.h"
#include "buffer.h" #include "hal/intc.h"
#include "gpio.h" #include "hal/interrupts.h"
#include "intc.h" #include "hal/pol0.h"
#include "interrupts.h" #include "hal/timer.h"
#include "pol0.h" #include "lib/async.h"
#include "timer.h" #include "lib/buffer.h"
#include "trace.h"
#include "uart.h" #include "uart.h"
#include "uart_async.h" #include "uart_async.h"
@ -21,6 +21,26 @@ Timer* timer0;
volatile uint32_t t1overflowsecs = 0; volatile uint32_t t1overflowsecs = 0;
volatile uint32_t t1overflowusecs = 0; volatile uint32_t t1overflowusecs = 0;
Gpio* gpio0;
void ToggleLed(int which) {
uint8_t data = gpio0->data;
data ^= (0x1 << which);
gpio0->data = data;
}
void SetLed(int which) {
uint8_t data = gpio0->data;
data |= (0x1 << which);
gpio0->data = data;
}
void ClearLed(int which) {
uint8_t data = gpio0->data;
data &= ~(0x1 << which);
gpio0->data = data;
}
void Uart0Isr() { HandleUartIsr(); } void Uart0Isr() { HandleUartIsr(); }
void Timer0Isr() { void Timer0Isr() {
@ -85,12 +105,14 @@ async::task<> blink() {
} // namespace } // namespace
int main() { int main() {
gpio0 = Gpio::Instance(GPIO0_BASE);
gpio0->data = 0;
SetupUart(); SetupUart();
UartWriteBlocking("uart setup done\r\n"); UartWriteBlocking("uart setup done\r\n");
SetupTimer(); SetupTimer();
UartWriteBlocking("timer setup done\r\n"); UartWriteBlocking("timer setup done\r\n");
gpio0->data = 0;
SetupInterrupts(); SetupInterrupts();
auto e = echo(); auto e = echo();
@ -112,8 +134,7 @@ int main() {
#include <cstdint> #include <cstdint>
#include "itoa.h" #include "lib/lock.h"
#include "lock.h"
extern unsigned char _heap_begin, _heap_end; extern unsigned char _heap_begin, _heap_end;

View File

@ -1,108 +0,0 @@
#include "buffer.h"
#include "gpio.h"
#include "intc.h"
#include "interrupts.h"
#include "pol0.h"
#include "timer.h"
#include "trace.h"
#include "uart2.h"
namespace {
Timer* timer0;
void Uart0Isr() {
// ToggleLed(7);
HandleUartIsr();
}
void Timer0Isr() {
SetLed(6);
__builtin_trap();
}
void SetupUart() {
InitUarts();
intc::SetIsr(UART0_IRQN, Uart0Isr);
intc::SetIrqEnabled(UART0_IRQN, true);
}
void SetupTimer() {
timer0 = Timer::Instance(TIMER0_BASE);
// timer0->SetupAsWdt(100'000 * 1000);
timer0->EnableT1();
intc::SetIsr(TIMER0_IRQN, Timer0Isr);
intc::SetIrqEnabled(TIMER0_IRQN, true);
}
void SetupInterrupts() {
intc::EnableInterrupts();
SetExternalInterruptHandler(intc::InterruptHandler);
EnableExternalInterrupts();
EnableInterrupts(true);
}
} // namespace
int main() {
SetupUart();
UartWriteCrash("uart setup done\r\n");
SetupTimer();
UartWriteCrash("timer setup done\r\n");
SetupInterrupts();
UartWriteCrash("init done. starting main loop\r\n");
UartEcho();
// should never get here
}
/// stdlib stuff
#include <sys/time.h>
#include <cstdint>
#include "itoa.h"
#include "lock.h"
extern unsigned char _heap_begin, _heap_end;
extern "C" void* _sbrk(int increment) {
static unsigned char* heap = &_heap_begin;
unsigned char* prev_heap = heap;
if (heap + increment >= &_heap_end) {
UartWriteCrash("Heap overflow!\r\n");
return reinterpret_cast<void*>(-1);
}
heap += increment;
return prev_heap;
}
extern "C" int _gettimeofday(struct timeval* tv, void* tzvp) {
(void)tzvp;
uint32_t ticks = timer0->GetT1Ticks();
tv->tv_sec = ticks / 100000000;
tv->tv_usec = (ticks % 100000000) / 100;
return 0;
}
extern "C" uint8_t __atomic_exchange_1(volatile void* ptr, uint8_t val,
int memorder) {
(void)memorder;
auto* dest = reinterpret_cast<volatile uint8_t*>(ptr);
bool ret;
{
InterruptLock lock;
ret = *dest;
*dest = val;
}
return ret;
}

View File

@ -1,71 +0,0 @@
#include "trace.h"
#include <algorithm>
#include <chrono>
#include "itoa.h"
#include "lock.h"
#include "uart.h"
namespace tracing {
namespace {
struct Event {
uint32_t timestamp;
TraceEvent event;
};
constexpr size_t kTraceBufferSize = 256;
std::array<Event, kTraceBufferSize> buffer;
size_t write_ptr = 0;
size_t size = 0;
} // namespace
void trace(int raw_event) { trace(static_cast<TraceEvent>(raw_event)); }
void trace(TraceEvent event) {
const std::chrono::system_clock::time_point now =
std::chrono::system_clock::now();
const uint32_t uptime_ticks = now.time_since_epoch().count();
{
InterruptLock lock;
buffer[write_ptr] = {.timestamp = uptime_ticks, .event = event};
write_ptr = (write_ptr + 1) % buffer.size();
size = std::min(size + 1, kTraceBufferSize);
#if TRACE_DUMP_WHEN_FULL
if (size == kTraceBufferSize) {
dump();
}
#endif // TRACE_DUMP_WHEN_FULL
}
}
void dump() {
InterruptLock lock;
if (size == kTraceBufferSize) {
std::rotate(buffer.begin(), buffer.begin() + write_ptr, buffer.end());
}
char number[] = "00000000";
UartWriteCrash("----\r\n");
for (Event event : std::span{buffer}.subspan(0, size)) {
itoa(static_cast<int>(event.timestamp), number);
UartWriteCrash(number);
UartWriteCrash(" ");
itoa(static_cast<int>(event.event), number);
UartWriteCrash(number);
UartWriteCrash("\r\n");
}
UartWriteCrash("----\r\n");
size = 0;
write_ptr = 0;
}
} // namespace tracing

View File

@ -1,51 +0,0 @@
#pragma once
#define TRACE_DUMP_WHEN_FULL 0
#ifdef __x86_64__
#include <cstdio>
#define TRACE(x) printf(#x "\n")
#else // __x86_64__
#define TRACE(...) tracing::trace(__VA_ARGS__)
#endif // __x86_64__
#include <cstdint>
namespace tracing {
enum class TraceEvent : uint8_t {
kUnknown = 0,
kUartIsr = 1,
kUartRxCb = 2,
kUartTxCb = 3,
kUartSend = 10,
kUartRecv = 11,
kUartTxBufferFull = 12,
kUartTxBufferNotFull = 13,
kUartWriteDone = 20,
kAsyncResume = 4,
kAsyncEnqueue = 5,
kAsyncTask = 6,
kAsyncResumeSetPending = 7,
kAsyncAwaitWasNotified = 8,
kAsyncSchedule = 9,
kAsyncTaskDone = 14,
kAsyncException = 15,
kAsyncCallParent = 16,
kAsyncCallParentDone = 17,
kAsyncCoAwait = 18,
kAsyncSuspend = 19,
kAsyncDestroy = 21,
kAsyncGimmeWaiting = 22,
kAsyncGimmeResume = 23,
};
void trace(TraceEvent event);
void trace(int raw_event);
void dump();
} // namespace tracing

View File

@ -1,14 +1,14 @@
#include "uart.h" #include "uart.h"
#include "async.h" #include "hal/gpio.h"
#include "gpio.h" #include "hal/pol0.h"
#include "lock.h" #include "lib/async.h"
#include "pol0.h" #include "lib/lock.h"
#include "ring_buffer.h" #include "lib/ring_buffer.h"
#include "trace.h"
#include "uart_async.h"
#include "xuartlite.h" #include "xuartlite.h"
#include "uart_async.h"
namespace { namespace {
using async::AwaitableType; using async::AwaitableType;

View File

@ -1,122 +0,0 @@
#include "uart2.h"
#include "gpio.h"
#include "lock.h"
#include "pol0.h"
#include "ring_buffer.h"
#include "xuartlite.h"
namespace {
constexpr uintptr_t kUart0BaseAddress = UART0_BASE;
XUartLite uart0_inst;
XUartLite_Config uart0_config = {
.DeviceId = 0,
.RegBaseAddr = kUart0BaseAddress,
.BaudRate = 115200,
.UseParity = false,
.DataBits = 8,
};
constexpr size_t kUartRxBufferSize = 256;
std::array<std::byte, kUartRxBufferSize> rx_buffer = {};
RingBuffer rx_ring_buffer{.buffer = rx_buffer};
constexpr size_t kUartTxBufferSize = 256;
std::array<std::byte, kUartTxBufferSize> tx_buffer = {};
RingBuffer tx_ring_buffer{.buffer = tx_buffer};
XUartLite* uart0 = &uart0_inst;
volatile int sending = 0;
void StartReceiving() {
while (1) {
if (rx_ring_buffer.FreeSpace() < 1) {
// woops, full. discard some data
// TODO: keep track of overrun stats
rx_ring_buffer.Pop(1);
}
if (XUartLite_Recv(uart0, rx_ring_buffer.RawWritePointer(), 1) < 1) {
break;
}
rx_ring_buffer.Push(1);
}
}
void StartSending() {
if (sending > 0) {
return;
}
size_t tosend = tx_ring_buffer.ContiguousAvailableData();
if (tosend < 1) {
return;
}
sending += 1;
XUartLite_Send(uart0, tx_ring_buffer.RawReadPointer(), tosend);
}
std::byte UartReadByte() {
std::byte c;
while (!rx_ring_buffer.Load(std::span{&c, 1})) {
}
return c;
}
void UartWriteByte(std::byte c) {
while (!tx_ring_buffer.Store(std::span{&c, 1})) {
}
{
InterruptLock lock;
StartSending();
}
}
} // namespace
void InitUarts() {
XUartLite_CfgInitialize(uart0, &uart0_config, uart0_config.RegBaseAddr);
XUartLite_SetSendHandler(uart0, HandleUartTxFromIsr, nullptr);
XUartLite_SetRecvHandler(uart0, HandleUartRxFromIsr, nullptr);
StartReceiving();
XUartLite_EnableInterrupt(uart0);
}
void UartWriteCrash(std::span<const std::byte> data) {
XUartLite_DisableInterrupt(uart0);
while (data.size() > 0) {
while (XUartLite_IsSending(uart0)) {
}
auto* dat =
reinterpret_cast<uint8_t*>(const_cast<std::byte*>(data.data()));
uint8_t sent = XUartLite_Send(uart0, dat, data.size());
data = data.subspan(sent);
}
while (XUartLite_IsSending(uart0)) {
}
XUartLite_Send(uart0, nullptr,
0); // reset buffer before enabling interrupts
XUartLite_EnableInterrupt(uart0);
}
void UartEcho() {
while (1) {
std::byte c = UartReadByte();
UartWriteByte(c);
}
}
void HandleUartTxFromIsr(void*, unsigned int transmitted) {
sending -= 1;
tx_ring_buffer.Pop(transmitted);
StartSending();
}
void HandleUartRxFromIsr(void*, unsigned int transmitted) {
rx_ring_buffer.Push(transmitted);
StartReceiving();
}
void HandleUartIsr() { XUartLite_InterruptHandler(uart0); }

View File

@ -1,18 +0,0 @@
#pragma once
#include <span>
#include <string_view>
void InitUarts();
// send and poll the uart until transmitted
void UartWriteCrash(std::span<const std::byte> data);
inline void UartWriteCrash(std::string_view s) {
return UartWriteCrash(std::as_bytes(std::span{s.data(), s.size()}));
}
void UartEcho();
void HandleUartTxFromIsr(void*, unsigned int transmitted);
void HandleUartRxFromIsr(void*, unsigned int);
void HandleUartIsr();

View File

@ -3,8 +3,8 @@
#include <span> #include <span>
#include <string_view> #include <string_view>
#include "async.h" #include "lib/async.h"
#include "buffer.h" #include "lib/buffer.h"
async::task<buffer> UartRead(int size); async::task<buffer> UartRead(int size);
async::task<std::byte> UartReadLoop(); async::task<std::byte> UartReadLoop();

View File

@ -1,7 +1,7 @@
#include <cstdint> #include <cstdint>
#include "gpio.h" #include "hal/gpio.h"
#include "pol0.h" #include "hal/pol0.h"
namespace { namespace {

View File

@ -1,12 +1,12 @@
#include "timer.h"
#include <cstdint> #include <cstdint>
#include "bios.h" #include "hal/bios.h"
#include "gpio.h" #include "hal/gpio.h"
#include "intc.h" #include "hal/intc.h"
#include "interrupts.h" #include "hal/interrupts.h"
#include "pol0.h" #include "hal/pol0.h"
#include "hal/timer.h"
namespace { namespace {

View File

@ -1,9 +1,9 @@
#include <cstdint> #include <cstdint>
#include "gpio.h" #include "hal/gpio.h"
#include "intc.h" #include "hal/intc.h"
#include "interrupts.h" #include "hal/interrupts.h"
#include "pol0.h" #include "hal/pol0.h"
#include "xuartlite.h" #include "xuartlite.h"
namespace { namespace {

View File

@ -1,6 +1,6 @@
#include <cstdint> #include <cstdint>
#include "pol0.h" #include "hal/pol0.h"
#include "xuartlite.h" #include "xuartlite.h"
uint8_t UartRead(); uint8_t UartRead();

19
mbv/configure vendored
View File

@ -25,9 +25,9 @@ class Config:
hostlibs = "-lgtest -lgmock -lgtest_main" hostlibs = "-lgtest -lgmock -lgtest_main"
hostldflags = "-fprofile-instr-generate -fcoverage-mapping" hostldflags = "-fprofile-instr-generate -fcoverage-mapping"
include_dirs = [ include_dirs = [
"hal", ".",
"hal/uart", "hal/uart",
"hal/lib/common", "hal/xilinx",
] ]
common_flags = [ common_flags = [
"-g", "-g",
@ -269,10 +269,15 @@ hal = source_set("hal", [
"hal/intc.cc", "hal/intc.cc",
"hal/interrupts.cc", "hal/interrupts.cc",
"hal/start.cc", "hal/start.cc",
"hal/lib/common/xil_assert.c",
"hal/uart/xuartlite.c", "hal/uart/xuartlite.c",
"hal/uart/xuartlite_stats.c",
"hal/uart/xuartlite_intr.c", "hal/uart/xuartlite_intr.c",
"hal/uart/xuartlite_stats.c",
"hal/xilinx/xil_assert.c",
])
lib = source_set("lib", [
"lib/async.cc",
"lib/lock.cc",
]) ])
bootloader = source_set("bootloader", glob.glob("./bootloader/**/*.cc", recursive=True)) bootloader = source_set("bootloader", glob.glob("./bootloader/**/*.cc", recursive=True))
@ -289,23 +294,21 @@ def app_image(app, sources=None):
return build_image( return build_image(
source_set(app, sources), source_set(app, sources),
linker_script="apps/app.ld", linker_script="apps/app.ld",
dependencies=[hal], dependencies=[hal, lib],
elf_out=f"{app}.elf", elf_out=f"{app}.elf",
bin_out=f"{app}.bin", bin_out=f"{app}.bin",
) )
all = [ all = [
build_source_set(hal), build_source_set(hal),
build_source_set(lib),
bootloader_image, bootloader_image,
app_image("helloworld"), app_image("helloworld"),
app_image("timer"), app_image("timer"),
app_image("uart"), app_image("uart"),
app_image("async", sources=[ app_image("async", sources=[
"apps/async/async.cc",
"apps/async/lock.cc",
"apps/async/main.cc", "apps/async/main.cc",
"apps/async/trace.cc",
"apps/async/uart.cc", "apps/async/uart.cc",
]), ]),
] ]

View File

@ -1,39 +0,0 @@
DRIVER_LIB_VERSION = 1.0
COMPILER=
ARCHIVER=
CP=cp
COMPILER_FLAGS=
EXTRA_COMPILER_FLAGS=
LIB=libxil.a
CC_FLAGS = $(COMPILER_FLAGS)
ECC_FLAGS = $(EXTRA_COMPILER_FLAGS)
RELEASEDIR=../../../lib/
INCLUDEDIR=../../../include/
INCLUDES=-I./. -I$(INCLUDEDIR)
SRCFILES:=$(wildcard *.c)
OBJECTS = $(addprefix $(RELEASEDIR), $(addsuffix .o, $(basename $(wildcard *.c))))
libs: $(OBJECTS)
DEPFILES := $(SRCFILES:%.c=$(RELEASEDIR)%.d)
include $(wildcard $(DEPFILES))
include $(wildcard ../../../../dep.mk)
$(RELEASEDIR)%.o: %.c
${COMPILER} $(CC_FLAGS) $(ECC_FLAGS) $(INCLUDES) $(DEPENDENCY_FLAGS) $< -o $@
.PHONY: include
include: $(addprefix $(INCLUDEDIR),$(wildcard *.h))
$(INCLUDEDIR)%.h: %.h
$(CP) $< $@
clean:
rm -rf ${OBJECTS}
rm -rf $(DEPFILES)

View File

@ -6,7 +6,6 @@
#include <utility> #include <utility>
#include "lock.h" #include "lock.h"
#include "trace.h"
namespace async { namespace async {
namespace { namespace {
@ -33,7 +32,6 @@ std::array<Notification, static_cast<size_t>(AwaitableType::kNumTypes)>
void schedule(std::coroutine_handle<> h, int ms) { void schedule(std::coroutine_handle<> h, int ms) {
InterruptLock lock; InterruptLock lock;
TRACE(tracing::TraceEvent::kAsyncSchedule);
std::chrono::system_clock::time_point exp = std::chrono::system_clock::time_point exp =
std::chrono::system_clock::now() + std::chrono::milliseconds(ms); std::chrono::system_clock::now() + std::chrono::milliseconds(ms);
Stuff* news = new Stuff{ Stuff* news = new Stuff{
@ -80,9 +78,7 @@ void step() {
int stuffinqueue = 0; int stuffinqueue = 0;
for (Stuff* s = stuff; s; s = s->next) stuffinqueue++; for (Stuff* s = stuff; s; s = s->next) stuffinqueue++;
TRACE(tracing::TraceEvent::kAsyncTask);
stuff->h(); stuff->h();
TRACE(tracing::TraceEvent::kAsyncTaskDone);
{ {
InterruptLock lock; InterruptLock lock;
@ -120,12 +116,10 @@ void enqueue(std::coroutine_handle<> h, AwaitableType type) {
{ {
InterruptLock lock; InterruptLock lock;
TRACE(tracing::TraceEvent::kAsyncEnqueue);
const bool was_notified = const bool was_notified =
std::exchange(notifications[ttype].pending, false); std::exchange(notifications[ttype].pending, false);
if (was_notified) { if (was_notified) {
TRACE(tracing::TraceEvent::kAsyncAwaitWasNotified);
schedule(h); schedule(h);
return; return;
} }

View File

@ -4,8 +4,6 @@
#include <coroutine> #include <coroutine>
#include <utility> #include <utility>
#include "trace.h"
namespace async { namespace async {
struct continuation : std::suspend_always { struct continuation : std::suspend_always {
@ -153,14 +151,12 @@ struct gimme {
void await_suspend(std::coroutine_handle<> h) { void await_suspend(std::coroutine_handle<> h) {
ha = h; ha = h;
waiting = true; waiting = true;
TRACE(tracing::TraceEvent::kAsyncGimmeWaiting);
if (parent) { if (parent) {
schedule(parent); schedule(parent);
} }
} }
T await_resume() { T await_resume() {
waiting = false; waiting = false;
TRACE(tracing::TraceEvent::kAsyncGimmeResume);
return std::move(stuff); return std::move(stuff);
} }

View File

@ -1,7 +1,7 @@
#pragma once #pragma once
#ifndef __x86_64__ #ifndef __x86_64__
#include "interrupts.h" #include "hal/interrupts.h"
struct InterruptLock { struct InterruptLock {
bool was_on; bool was_on;