synth/arm/main.cc

113 lines
2.6 KiB
C++
Raw Normal View History

2022-05-17 03:56:25 +00:00
#include "async.h"
#include "aum1_cm1.h"
#include "buffer.h"
#include "crash.h"
#include "gpio.h"
2022-05-17 03:56:25 +00:00
#include "timer.h"
#include "trace.h"
#include "uart.h"
#include "uart_async.h"
2022-05-08 06:00:22 +00:00
2022-06-19 07:43:36 +00:00
extern volatile uint32_t _vector_table[];
2022-05-09 03:55:58 +00:00
namespace {
2022-05-17 03:56:25 +00:00
using async::AwaitableType;
2022-05-08 06:00:22 +00:00
2022-05-17 03:56:25 +00:00
void Uart0Isr() {
2022-06-19 07:50:22 +00:00
ToggleLed(7);
//tracing::trace(tracing::TraceEvent::kUartIsr);
2022-05-17 17:17:56 +00:00
HandleUartIsr();
2022-05-17 03:56:25 +00:00
}
2022-06-19 07:48:28 +00:00
void Timer0Isr() { __builtin_trap(); }
2022-05-17 03:56:25 +00:00
void SetupUart() {
InitUarts();
2022-06-19 07:43:36 +00:00
_vector_table[16 + Uart0_IRQn] = reinterpret_cast<uint32_t>(Uart0Isr);
2022-05-17 03:56:25 +00:00
NVIC_SetPriority(Uart0_IRQn, 3);
NVIC_EnableIRQ(Uart0_IRQn);
}
2022-05-09 03:55:58 +00:00
2022-05-17 03:56:25 +00:00
void SetupTimer() {
timer0->SetupAsWdt(100000 * 1); //4000);
2022-05-17 03:56:25 +00:00
timer0->EnableT1();
2022-06-19 07:43:36 +00:00
_vector_table[16 + Timer0_IRQn] = reinterpret_cast<uint32_t>(Timer0Isr);
NVIC_EnableIRQ(Timer0_IRQn);
2022-05-17 03:56:25 +00:00
}
2022-05-09 03:55:58 +00:00
2022-06-19 07:50:22 +00:00
#if 1 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101133
2022-05-17 03:56:25 +00:00
async::task<> echo() {
2022-06-19 07:50:22 +00:00
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
2022-05-17 03:56:25 +00:00
while (1) {
2022-06-19 07:50:22 +00:00
SetLed(1);
uint8_t c = co_await reader;
ClearLed(1);
ToggleLed(2);
2023-06-03 06:44:28 +00:00
feeder.feed(std::as_bytes(std::span{&c, 1}));
2022-06-19 07:50:22 +00:00
}
}
#else
2022-06-19 07:50:22 +00:00
async::task<> echo() {
async::task<uint8_t> reader = UartReadLoop();
while (1) {
SetLed(1);
uint8_t c = co_await reader;
ClearLed(1);
ToggleLed(2);
co_await UartWrite(std::as_bytes(std::span{&c, 1}));
}
}
#endif
2022-06-19 07:50:22 +00:00
[[maybe_unused]]
2022-06-19 07:50:22 +00:00
async::task<> dump_trace() {
while (1) {
co_await async::delay(5000);
// tracing::dump();
2022-05-17 03:56:25 +00:00
}
}
} // namespace
2022-05-17 03:56:25 +00:00
2022-06-19 07:50:22 +00:00
#define XUL_SR_RX_FIFO_FULL 0x02 /* receive FIFO full */
#define XUL_SR_RX_FIFO_VALID_DATA 0x01 /* data in receive FIFO */
2022-05-17 03:56:25 +00:00
int main() {
2022-06-19 07:43:36 +00:00
_vector_table[16 + HardFault_IRQn] =
reinterpret_cast<uint32_t>(crash::HardFaultHandler);
2022-05-17 03:56:25 +00:00
SetupUart();
2022-06-19 07:50:22 +00:00
UartWriteBlocking("uart setup done\r\n");
2022-05-17 03:56:25 +00:00
SetupTimer();
2022-06-19 07:50:22 +00:00
UartWriteBlocking("timer setup done\r\n");
2022-05-17 03:56:25 +00:00
async::schedule(echo().h);
// async::schedule(dump_trace().h);
2022-06-19 07:50:22 +00:00
tracing::trace(tracing::TraceEvent::kUartIsr);
tracing::trace(tracing::TraceEvent::kUartIsr);
UartWriteBlocking("init done. starting main loop\r\n");
2022-05-17 03:56:25 +00:00
async::main_loop([]() {
static int cnt = 0;
timer0->Pet();
if ((cnt++ % 100000) == 0) {
ToggleLed(0);
2022-06-19 07:50:22 +00:00
ClearLed(7);
ClearLed(2);
ClearLed(3);
ClearLed(4);
LogStuff();
2022-05-17 03:56:25 +00:00
}
return false;
2022-05-17 03:56:25 +00:00
});
// should never get here
}