Compare commits

..

No commits in common. "0c7206f18679000cdbc8d4866f1bbb5bc5c4e7ab" and "6553fa04aa9f01e9501de6b656cf96e977343385" have entirely different histories.

24 changed files with 18 additions and 1126 deletions

View File

@ -1,9 +0,0 @@
---
# We'll use defaults from the LLVM style, but with 4 columns indentation.
BasedOnStyle: Google
IndentWidth: 4
---
Language: Cpp
# Force pointers to the type for C++.
DerivePointerAlignment: false
PointerAlignment: Left

View File

@ -14,10 +14,6 @@ timer: ## Build the timer app in docker
uart: ## Build the uart app in docker
docker build -o . --target export --build-arg TARGET=uart.bin .
.PHONY: async
async: ## Build the async app in docker
docker build -o . --target export --build-arg TARGET=async.bin .
.PHONY: dev-image
dev-image:
docker build -t mbv-dev --target dev .

View File

@ -1,168 +0,0 @@
#include "async.h"
#include <array>
#include <atomic>
#include <chrono>
#include <utility>
#include "lock.h"
#include "trace.h"
namespace async {
namespace {
using namespace std::literals::chrono_literals;
struct Stuff {
std::coroutine_handle<> h;
std::chrono::system_clock::time_point expiration;
Stuff* next;
};
struct Notification {
bool pending; // can only be true if stuff is nullptr
Stuff* stuff;
};
std::atomic<Stuff*> work = nullptr;
std::array<Notification, static_cast<size_t>(AwaitableType::kNumTypes)>
notifications = {};
} // namespace
void schedule(std::coroutine_handle<> h, int ms) {
InterruptLock lock;
TRACE(tracing::TraceEvent::kAsyncSchedule);
std::chrono::system_clock::time_point exp =
std::chrono::system_clock::now() + std::chrono::milliseconds(ms);
Stuff* news = new Stuff{
.h = h,
.expiration = exp,
};
Stuff* stuff = work;
if (!stuff || stuff->expiration > exp) {
news->next = stuff;
work = news;
return;
}
Stuff* s = stuff;
while (s->next && s->next->expiration <= exp) {
s = s->next;
}
news->next = s->next;
s->next = news;
}
void step() {
Stuff* stuff;
// ensure all previous side effects are visible
{
InterruptLock lock;
stuff = work;
};
if (stuff == nullptr) {
return;
}
auto now = std::chrono::system_clock::now();
auto dt = stuff->expiration - now;
if (dt > 0ms) {
return;
}
int stuffinqueue = 0;
for (Stuff* s = stuff; s; s = s->next) stuffinqueue++;
TRACE(tracing::TraceEvent::kAsyncTask);
stuff->h();
TRACE(tracing::TraceEvent::kAsyncTaskDone);
if (stuff->h.done()) {
stuff->h.destroy();
}
{
InterruptLock lock;
work = stuff->next;
}
delete stuff;
}
void reset() {
Stuff* stuff = work;
while (stuff) {
Stuff* byebye = stuff;
stuff = stuff->next;
delete byebye;
}
work = nullptr;
}
void main_loop(bool (*idle_function)()) {
while (1) {
if (idle_function != nullptr) {
if (idle_function()) {
reset();
break;
};
}
step();
}
}
void enqueue(std::coroutine_handle<> h, AwaitableType type) {
auto ttype = static_cast<size_t>(type);
{
InterruptLock lock;
TRACE(tracing::TraceEvent::kAsyncEnqueue);
const bool was_notified =
std::exchange(notifications[ttype].pending, false);
if (was_notified) {
TRACE(tracing::TraceEvent::kAsyncAwaitWasNotified);
schedule(h);
return;
}
Stuff* item = new Stuff{.h = h};
Stuff* stuff = notifications[ttype].stuff;
if (stuff == nullptr) {
notifications[ttype].stuff = item;
return;
}
while (stuff->next != nullptr) {
stuff = stuff->next;
}
stuff->next = item;
}
}
void resume(AwaitableType type) {
auto ttype = static_cast<size_t>(type);
Stuff* stuff = nullptr;
{
InterruptLock lock;
stuff = notifications[ttype].stuff;
if (stuff == nullptr) {
notifications[ttype].pending = true;
return;
}
notifications[ttype].stuff = stuff->next;
schedule(stuff->h);
}
delete stuff;
}
} // namespace async

View File

@ -1,208 +0,0 @@
#pragma once
#include <chrono>
#include <coroutine>
#include <utility>
#include "trace.h"
namespace async {
struct task_final_suspend {
bool await_ready() noexcept(true) { return false; }
void await_suspend(std::coroutine_handle<> h) noexcept(true) {
if (parent) {
TRACE(tracing::TraceEvent::kAsyncCallParent);
parent();
TRACE(tracing::TraceEvent::kAsyncCallParentDone);
if (parent && parent.done()) {
TRACE(tracing::TraceEvent::kAsyncDestroy);
parent.destroy();
}
}
}
void await_resume() noexcept(true) {}
std::coroutine_handle<> parent;
};
template <typename T>
struct gimme {
// child interface
bool await_ready() { return false; }
void await_suspend(std::coroutine_handle<> h) {
ha = h;
waiting = true;
TRACE(tracing::TraceEvent::kAsyncGimmeWaiting);
}
T await_resume() {
waiting = false;
TRACE(tracing::TraceEvent::kAsyncGimmeResume);
return std::move(stuff);
}
// parent interface
void feed(T&& s) {
if (!waiting) {
__builtin_trap();
}
if (!ha) {
__builtin_trap();
}
stuff = s;
ha.resume();
}
bool waiting = false;
std::coroutine_handle<> ha;
T stuff;
};
template <typename T = void>
struct task;
template <>
struct task<void> {
struct promise_type;
using handle_type = std::coroutine_handle<promise_type>;
struct promise_type {
task get_return_object() {
return {.h = handle_type::from_promise(*this)};
}
std::suspend_always initial_suspend() noexcept { return {}; }
task_final_suspend final_suspend() noexcept {
return {.parent = parent};
}
void return_void() {}
void unhandled_exception() {
TRACE(tracing::TraceEvent::kAsyncException);
}
std::coroutine_handle<> parent;
};
// awaitable
bool await_ready() {
TRACE(tracing::TraceEvent::kAsyncCoAwait);
h();
if (h.done()) {
TRACE(tracing::TraceEvent::kAsyncDestroy);
h.destroy();
return true;
}
return false;
}
void await_suspend(std::coroutine_handle<> ha) {
TRACE(tracing::TraceEvent::kAsyncSuspend);
h.promise().parent = ha;
}
void await_resume() {}
std::coroutine_handle<promise_type> h;
};
template <typename T>
struct task {
struct promise_type;
using handle_type = std::coroutine_handle<promise_type>;
struct promise_type {
task get_return_object() {
return {.h = handle_type::from_promise(*this)};
}
std::suspend_always initial_suspend() noexcept { return {}; }
task_final_suspend final_suspend() noexcept {
return {.parent = parent};
}
void return_value(T&& value) { ret_value = std::move(value); }
void unhandled_exception() {
TRACE(tracing::TraceEvent::kAsyncException);
}
template <std::convertible_to<T> From>
task_final_suspend yield_value(From&& value) {
ret_value = std::forward<From>(value);
result_ready = true;
return {.parent = parent};
}
T ret_value;
bool result_ready = false;
std::coroutine_handle<> parent;
};
// awaitable
bool await_ready() {
h.promise().parent = {};
TRACE(tracing::TraceEvent::kAsyncCoAwait);
h();
if (h.promise().result_ready) {
return true;
}
if (h.done()) {
destroyed = true;
ret_value = std::move(h.promise().ret_value);
TRACE(tracing::TraceEvent::kAsyncDestroy);
h.destroy();
return true;
}
return false;
}
void await_suspend(std::coroutine_handle<> ha) {
TRACE(tracing::TraceEvent::kAsyncSuspend);
h.promise().parent = ha;
}
T await_resume() {
if (!destroyed) {
h.promise().result_ready = false;
return std::move(h.promise().ret_value);
}
return std::move(ret_value);
}
bool destroyed = false;
T ret_value;
std::coroutine_handle<promise_type> h;
};
enum class AwaitableType {
kUnknown = 0,
kUartRx = 1,
kUartTx = 2,
kNumTypes
};
void schedule(std::coroutine_handle<> h, int ms = 0);
void enqueue(std::coroutine_handle<> h, AwaitableType type);
void resume(AwaitableType type); // typically called from an ISR
void main_loop(bool (*idle_function)());
void step();
inline auto await(AwaitableType type) {
struct awaitable {
AwaitableType type;
bool await_ready() { return false; };
void await_suspend(std::coroutine_handle<> h) { enqueue(h, type); }
void await_resume() {}
};
return awaitable{type};
}
inline auto delay(int ms) {
struct awaitable {
int ms;
bool await_ready() { return false; };
void await_suspend(std::coroutine_handle<> h) { schedule(h, ms); }
void await_resume() {}
};
return awaitable{ms};
}
} // namespace async

View File

@ -1,30 +0,0 @@
#pragma once
#include <span>
#include <utility>
struct buffer {
std::span<std::byte> data;
buffer() = default;
buffer(std::span<std::byte> d) : data(d) {}
static buffer make(size_t size) {
return buffer({new std::byte[size], size});
}
buffer(buffer& other) = delete;
buffer& operator=(buffer& other) = delete;
buffer(buffer&& other) : data(std::exchange(other.data, {})) {}
buffer& operator=(buffer&& other) {
data = std::exchange(other.data, {});
return *this;
}
~buffer() {
if (data.data()) {
delete[] data.data();
};
}
};

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,13 +0,0 @@
#pragma once
// out must be at least 8 bytes long
inline void itoa(int val, char* out) {
for (int i = 0; i < 8; i++) {
uint8_t digit = (val >> (28 - 4 * i)) & 0xf;
if (digit < 0xa) {
out[i] = '0' + digit;
} else {
out[i] = 'a' + digit - 0xa;
}
}
}

View File

@ -1,5 +0,0 @@
#include "lock.h"
#ifdef __x86_64__
std::recursive_mutex InterruptLock::m;
#endif

View File

@ -1,21 +0,0 @@
#pragma once
#ifndef __x86_64__
#include "interrupts.h"
struct InterruptLock {
bool was_on;
InterruptLock() : was_on(EnableInterrupts(false)) {}
~InterruptLock() { EnableInterrupts(was_on); }
};
#else // __x86_64__
#include <mutex>
struct InterruptLock {
static std::recursive_mutex m;
InterruptLock() { m.lock(); }
~InterruptLock() { m.unlock(); }
};
#endif // __x86_64__

View File

@ -1,141 +0,0 @@
#include "async.h"
#include "buffer.h"
#include "gpio.h"
#include "intc.h"
#include "interrupts.h"
#include "pol0.h"
#include "timer.h"
#include "trace.h"
#include "uart.h"
#include "uart_async.h"
namespace {
using async::AwaitableType;
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);
}
async::task<> echo() {
async::task<uint8_t> reader = UartReadLoop();
async::gimme<std::span<const std::byte>> feeder;
async::task<> writer = UartWriteLoop(feeder);
writer.h.resume(); // advance to first yield
while (1) {
SetLed(1);
uint8_t c = co_await reader;
ClearLed(1);
ToggleLed(2);
feeder.feed(std::as_bytes(std::span{&c, 1}));
}
}
} // namespace
#define XUL_SR_RX_FIFO_FULL 0x02 /* receive FIFO full */
#define XUL_SR_RX_FIFO_VALID_DATA 0x01 /* data in receive FIFO */
int main() {
SetupUart();
UartWriteCrash("uart setup done\r\n");
SetupTimer();
UartWriteCrash("timer setup done\r\n");
gpio0->data = 0;
SetupInterrupts();
async::schedule(echo().h);
UartWriteCrash("init done. starting main loop\r\n");
async::main_loop([]() {
static int cnt = 0;
timer0->Pet();
if ((cnt++ % 100000) == 0) {
ToggleLed(0);
}
return false;
});
// should never get here
}
/// stdlib stuff
#include <sys/time.h>
#include <cstdint>
#include "itoa.h"
#include "lock.h"
#ifndef SBRK_STATS
#define SBRK_STATS 0
#endif
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,108 +0,0 @@
#pragma once
#include <atomic>
#include <span>
#include "lock.h"
struct RingBuffer {
std::span<std::byte> buffer;
std::atomic<size_t> read_ptr = 0;
std::atomic<size_t> write_ptr = 0;
std::atomic<bool> full = 0;
bool Store(std::span<const std::byte> data) {
InterruptLock lock;
if (data.size() > FreeSpace()) {
return false;
}
const size_t to_copy = std::min(buffer.size() - write_ptr, data.size());
std::copy(data.begin(), data.begin() + to_copy,
buffer.begin() + write_ptr);
if (to_copy < data.size()) {
std::copy(data.begin() + to_copy, data.end(), buffer.begin());
}
Push(data.size());
return true;
}
bool Load(std::span<std::byte> out) {
InterruptLock lock;
if (out.size() > AvailableData()) {
return false;
}
const size_t to_copy = std::min(buffer.size() - read_ptr, out.size());
std::copy(buffer.begin() + read_ptr,
buffer.begin() + read_ptr + to_copy, out.begin());
if (to_copy < out.size()) {
std::copy(buffer.begin(), buffer.begin() + out.size() - to_copy,
out.begin() + to_copy);
}
Pop(out.size());
return true;
}
bool Push(size_t amount) {
InterruptLock lock;
if (amount > FreeSpace()) {
return false;
}
write_ptr = (write_ptr + amount) % buffer.size();
if (read_ptr == write_ptr) {
full = true;
}
return true;
}
bool Pop(size_t amount) {
InterruptLock lock;
if (amount > AvailableData()) {
return false;
}
read_ptr = (read_ptr + amount) % buffer.size();
if (amount > 0) {
full = false;
}
return true;
}
size_t FreeSpace() const {
InterruptLock lock;
return buffer.size() - AvailableData();
}
size_t AvailableData() const {
InterruptLock lock;
if (read_ptr == write_ptr) {
return full ? buffer.size() : 0;
}
return (buffer.size() + write_ptr - read_ptr) % buffer.size();
}
uint8_t* RawReadPointer() const {
InterruptLock lock;
return reinterpret_cast<uint8_t*>(buffer.data() + read_ptr);
}
size_t ContiguousAvailableData() const {
InterruptLock lock;
if (read_ptr < write_ptr) {
return AvailableData();
}
if (full) {
return 0;
}
return buffer.size() - read_ptr;
}
};

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,165 +0,0 @@
#include "uart.h"
#include "async.h"
#include "gpio.h"
#include "lock.h"
#include "pol0.h"
#include "ring_buffer.h"
#include "trace.h"
#include "uart_async.h"
#include "xuartlite.h"
namespace {
using async::AwaitableType;
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 kUartTxBufferSize = 256;
std::array<std::byte, kUartTxBufferSize> tx_buffer = {};
RingBuffer tx_ring_buffer{.buffer = tx_buffer};
XUartLite* uart0 = &uart0_inst;
} // namespace
void InitUarts() {
XUartLite_CfgInitialize(uart0, &uart0_config, uart0_config.RegBaseAddr);
XUartLite_SetSendHandler(uart0, HandleUartTxFromIsr, nullptr);
XUartLite_SetRecvHandler(uart0, HandleUartRxFromIsr, nullptr);
XUartLite_EnableInterrupt(uart0);
}
void UartWriteCrash(std::span<const std::byte> data) {
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)) {
}
}
async::task<> UartWrite(std::span<const std::byte> data) {
while (!tx_ring_buffer.Store(data)) {
tracing::trace(tracing::TraceEvent::kUartTxBufferFull);
co_await async::await(AwaitableType::kUartTx);
tracing::trace(tracing::TraceEvent::kUartTxBufferNotFull);
}
{
InterruptLock lock;
if (!XUartLite_IsSending(uart0)) {
tracing::trace(tracing::TraceEvent::kUartSend);
XUartLite_Send(uart0, tx_ring_buffer.RawReadPointer(),
tx_ring_buffer.ContiguousAvailableData());
}
}
}
async::task<> UartWriteLoop(
async::gimme<std::span<const std::byte>>& data_gen) {
while (1) {
auto data = co_await data_gen;
while (!tx_ring_buffer.Store(data)) {
tracing::trace(tracing::TraceEvent::kUartTxBufferFull);
co_await async::await(AwaitableType::kUartTx);
tracing::trace(tracing::TraceEvent::kUartTxBufferNotFull);
}
{
InterruptLock lock;
if (!XUartLite_IsSending(uart0)) {
tracing::trace(tracing::TraceEvent::kUartSend);
XUartLite_Send(uart0, tx_ring_buffer.RawReadPointer(),
tx_ring_buffer.ContiguousAvailableData());
}
}
}
}
void UartReadBlocking(std::span<std::byte> data) {
size_t bytes_received = 0;
while (bytes_received < data.size()) {
auto* buffer = reinterpret_cast<uint8_t*>(data.data() + bytes_received);
tracing::trace(tracing::TraceEvent::kUartRecv);
bytes_received +=
XUartLite_Recv(uart0, buffer, data.size() - bytes_received);
}
}
void UartWriteBlocking(std::span<const std::byte> data) {
while (!tx_ring_buffer.Store(data)) {
}
{
InterruptLock lock;
if (!XUartLite_IsSending(uart0)) {
tracing::trace(tracing::TraceEvent::kUartSend);
XUartLite_Send(uart0, tx_ring_buffer.RawReadPointer(),
tx_ring_buffer.ContiguousAvailableData());
}
}
}
async::task<uint8_t> UartReadLoop() {
uint8_t c;
while (1) {
tracing::trace(tracing::TraceEvent::kUartRecv);
size_t received = XUartLite_Recv(uart0, &c, 1);
// some data may already be in the fifo, but if not, wait:
if (received < 1) {
co_await async::await(AwaitableType::kUartRx);
}
co_yield c;
}
}
async::task<buffer> UartRead(int size) {
auto buff = buffer::make(size);
auto* data = reinterpret_cast<uint8_t*>(buff.data.data());
tracing::trace(tracing::TraceEvent::kUartRecv);
size_t received = XUartLite_Recv(uart0, data, buff.data.size());
// some data may already be in the fifo, but if not, wait:
if (received < buff.data.size()) {
co_await async::await(AwaitableType::kUartRx);
}
co_return buff;
}
void HandleUartTxFromIsr(void*, unsigned int transmitted) {
tx_ring_buffer.Pop(transmitted);
if (tx_ring_buffer.AvailableData() > 0) {
tracing::trace(tracing::TraceEvent::kUartSend);
XUartLite_Send(uart0, tx_ring_buffer.RawReadPointer(),
tx_ring_buffer.ContiguousAvailableData());
}
async::resume(AwaitableType::kUartTx);
}
void HandleUartRxFromIsr(void*, unsigned int) {
async::resume(AwaitableType::kUartRx);
}
void HandleUartIsr() { XUartLite_InterruptHandler(uart0); }
extern "C" uint8_t XUartLite_GetSR(XUartLite*);
uint8_t UartStatus() { return XUartLite_GetSR(uart0); }
void LogStuff() {
uint8_t data = gpio0->data;
data |= (uart0->ReceiveBuffer.RemainingBytes & 0xf) << 4;
gpio0->data = data;
}

View File

@ -1,35 +0,0 @@
#pragma once
#include <cstdint>
#include <span>
#include <string_view>
void InitUarts();
// block until the provided buffer is full
void UartReadBlocking(std::span<std::byte> data);
inline uint8_t UartReadByteBlocking() {
std::byte byte;
UartReadBlocking(std::span{&byte, 1});
return static_cast<uint8_t>(byte);
}
// 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()}));
}
// block until room is available in tx fifo, then send
void UartWriteBlocking(std::span<const std::byte> data);
inline void UartWriteBlocking(std::string_view s) {
return UartWriteBlocking(std::as_bytes(std::span{s.data(), s.size()}));
}
void HandleUartTxFromIsr(void*, unsigned int transmitted);
void HandleUartRxFromIsr(void*, unsigned int);
void HandleUartIsr();
uint8_t UartStatus();
void LogStuff();

View File

@ -1,15 +0,0 @@
#pragma once
#include <span>
#include <string_view>
#include "async.h"
#include "buffer.h"
async::task<buffer> UartRead(int size);
async::task<uint8_t> UartReadLoop();
async::task<> UartWrite(std::span<const std::byte> data);
inline async::task<> UartWrite(std::string_view s) {
co_await UartWrite(std::as_bytes(std::span{s.data(), s.size()}));
}
async::task<> UartWriteLoop(async::gimme<std::span<const std::byte>>& data);

View File

@ -25,8 +25,9 @@ void SetupTimer() {
timer->SetupAsWdt(100'000'000);
timer->EnableT1();
intc::SetIsr(TIMER0_IRQN, Timer0Isr);
intc::SetIrqEnabled(TIMER0_IRQN, true);
SetIsr(TIMER0_IRQN, Timer0Isr);
SetIrqEnabled(TIMER0_IRQN, true);
EnableInterrupts();
}
} // namespace
@ -36,10 +37,8 @@ int main() {
leds->data = 0xa0;
SetupTimer();
intc::EnableInterrupts();
SetExternalInterruptHandler(intc::InterruptHandler);
SetExternalInterruptHandler(InterruptHandler);
EnableExternalInterrupts();
EnableInterrupts(true);
leds->data = 0xa1;

View File

@ -41,8 +41,8 @@ void InitUarts() {
XUartLite_SetRecvHandler(uart0, HandleUartRxFromIsr, nullptr);
XUartLite_EnableInterrupt(uart0);
intc::SetIsr(UART0_IRQN, Uart0Isr);
intc::SetIrqEnabled(UART0_IRQN, true);
SetIsr(UART0_IRQN, Uart0Isr);
SetIrqEnabled(UART0_IRQN, true);
}
} // namespace
@ -52,10 +52,9 @@ int main() {
leds->data = 0xa0;
InitUarts();
intc::EnableInterrupts();
SetExternalInterruptHandler(intc::InterruptHandler);
EnableInterrupts();
SetExternalInterruptHandler(InterruptHandler);
EnableExternalInterrupts();
EnableInterrupts(true);
int counter = 0;

14
mbv/configure vendored
View File

@ -43,7 +43,6 @@ class Config:
"-g",
"-Wl,--gc-sections",
"-Wl,--print-memory-usage",
"-specs=nano.specs",
"-flto",
"-march=rv32i_zicsr",
]
@ -283,11 +282,9 @@ bootloader_image = build_image(
linker_script="bootloader/bootloader.ld",
)
def app_image(app, sources=None):
if sources is None:
sources = glob.glob(f"./apps/{app}/**/*.cc", recursive=True)
def app_image(app):
return build_image(
source_set(app, sources),
source_set(app, glob.glob(f"./apps/{app}/**/*.cc", recursive=True)),
linker_script="apps/app.ld",
dependencies=[hal],
elf_out=f"{app}.elf",
@ -301,13 +298,6 @@ all = [
app_image("helloworld"),
app_image("timer"),
app_image("uart"),
app_image("async", sources=[
"apps/async/async.cc",
"apps/async/lock.cc",
"apps/async/main.cc",
"apps/async/trace.cc",
"apps/async/uart.cc",
]),
]

View File

@ -1,9 +1,7 @@
#include "intc.h"
// this interrupt controller is tied to the pol0 design
#include "pol0.h"
namespace intc {
namespace {
struct IntC {
@ -24,7 +22,7 @@ struct IntC {
IntC* intc = reinterpret_cast<IntC*>(INTC_BASE);
Isr isrs[NIRQ] = {};
} // namespace
}
bool SetIrqEnabled(uint8_t irqn, bool enabled) {
uint32_t mask = 1 << irqn;
@ -62,5 +60,3 @@ void InterruptHandler() {
}
}
}
} // namespace intc

View File

@ -2,8 +2,6 @@
#include <cstdint>
namespace intc {
using Isr = void(*)(void);
/// Returns: true if the IRQ was previously enabled
@ -16,5 +14,3 @@ void EnableInterrupts();
// Feed this to the CPU's interrupt handler
void InterruptHandler();
} // namespace intc

View File

@ -1,6 +1,5 @@
#include <cstdint>
#include "bios.h"
#include "interrupts.h"
namespace {
@ -27,40 +26,27 @@ void TrapHandler() {
mip &= ~(kMieExternalInterruptMask);
asm volatile("csrw mip, %0" :: "r"(mip));
} else {
BiosWozmon();
}
}
} // namespace
}
void SetExternalInterruptHandler(Isr handler) {
external_handler = handler;
}
void EnableExternalInterrupts() {
uint32_t mstatus;
uint32_t mie;
Isr trap = TrapHandler;
asm volatile("csrr %0, mstatus" : "=r"(mstatus));
asm volatile("csrr %0, mie" : "=r"(mie));
asm volatile("csrw mtvec, %0" :: "r"(trap));
mie |= kMieExternalInterruptMask;
asm volatile("csrw mie, %0" :: "r"(mie));
}
bool EnableInterrupts(bool on) {
uint32_t mstatus;
bool was_on;
asm volatile("csrr %0, mstatus" : "=r"(mstatus));
was_on = (mstatus & kMstatusMieMask) > 0;
if (on) {
mstatus |= kMstatusMieMask;
} else {
mstatus &= ~kMstatusMieMask;
}
mstatus |= kMstatusMieMask;
asm volatile("csrw mstatus, %0" :: "r"(mstatus));
return was_on;
}

View File

@ -4,6 +4,3 @@ using Isr = void(*)();
void SetExternalInterruptHandler(Isr handler);
void EnableExternalInterrupts();
/** Returns true if interrupts were enabled, false otherwise */
bool EnableInterrupts(bool on);

View File

@ -8,7 +8,7 @@ import time
offset = 0x80000000
tty = '/dev/ttyUSB1'
baud = 115200
chunksize = 1024
chunksize = 128
def write(s, offset, dat):