arm: async echo app
This commit is contained in:
95
arm/main.cc
95
arm/main.cc
@@ -1,32 +1,83 @@
|
||||
#include "async.h"
|
||||
#include "aum1_cm1.h"
|
||||
#include "buffer.h"
|
||||
#include "crash.h"
|
||||
#include "gpio.h"
|
||||
#include "timer.h"
|
||||
#include "trace.h"
|
||||
#include "uart.h"
|
||||
|
||||
namespace {
|
||||
using async::AwaitableType;
|
||||
|
||||
[[maybe_unused]]
|
||||
void sleep(int ms) {
|
||||
for (int i = 0; i < ms; i++) {
|
||||
// sleep for 1 ms
|
||||
for (int j = 0; j < 22000; j++) {
|
||||
asm("");
|
||||
}
|
||||
}
|
||||
void HandleUartRxFromIsr(void*, unsigned int) {
|
||||
tracing::trace(tracing::TraceEvent::kUartRxCb);
|
||||
async::resume(AwaitableType::kUartRx);
|
||||
}
|
||||
|
||||
void Uart0Isr() {
|
||||
tracing::trace(tracing::TraceEvent::kUartIsr);
|
||||
XUartLite_InterruptHandler(uart0);
|
||||
}
|
||||
|
||||
void Timer0Isr() {
|
||||
tracing::dump();
|
||||
__builtin_trap();
|
||||
}
|
||||
|
||||
void SetupUart() {
|
||||
InitUarts();
|
||||
|
||||
auto* vector_table = reinterpret_cast<uint32_t*>(0x0);
|
||||
vector_table[16 + HardFault_IRQn] =
|
||||
reinterpret_cast<uint32_t>(crash::HardFaultHandler);
|
||||
|
||||
vector_table[16 + Uart0_IRQn] = reinterpret_cast<uint32_t>(Uart0Isr);
|
||||
vector_table[16 + Timer0_IRQn] = reinterpret_cast<uint32_t>(Timer0Isr);
|
||||
NVIC_SetPriority(Uart0_IRQn, 3);
|
||||
NVIC_EnableIRQ(Uart0_IRQn);
|
||||
|
||||
XUartLite_SetSendHandler(uart0, HandleUartTxFromIsr, nullptr);
|
||||
XUartLite_SetRecvHandler(uart0, HandleUartRxFromIsr, nullptr);
|
||||
XUartLite_EnableInterrupt(uart0);
|
||||
}
|
||||
|
||||
void SetupTimer() {
|
||||
NVIC_EnableIRQ(Timer0_IRQn);
|
||||
timer0->SetupAsWdt(100000 * 4000);
|
||||
timer0->EnableT1();
|
||||
}
|
||||
} // namespace
|
||||
|
||||
extern "C" int main() {
|
||||
uint8_t cnt = 0;
|
||||
|
||||
for (int i = 0; i < 256; i++) {
|
||||
gpio0->data = cnt;
|
||||
cnt++;
|
||||
|
||||
sleep(125);
|
||||
async::task<buffer> UartRead(int size) {
|
||||
auto buff = buffer::make(size);
|
||||
auto* data = reinterpret_cast<uint8_t*>(buff.data.data());
|
||||
size_t received = XUartLite_Recv(uart0, data, buff.data.size());
|
||||
if (received < buff.data.size()) {
|
||||
co_await async::await(AwaitableType::kUartRx);
|
||||
}
|
||||
|
||||
auto* airc = reinterpret_cast<volatile uint32_t*>(0xe000ed0c);
|
||||
uint32_t a = *airc;
|
||||
*airc = a | 0x04;
|
||||
|
||||
while (1) {}
|
||||
co_return buff;
|
||||
}
|
||||
|
||||
async::task<> echo() {
|
||||
while (1) {
|
||||
buffer buff = co_await UartRead(1);
|
||||
UartSend(buff.data);
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
SetupUart();
|
||||
SetupTimer();
|
||||
|
||||
async::schedule(echo().h);
|
||||
|
||||
async::main_loop([]() {
|
||||
static int cnt = 0;
|
||||
timer0->Pet();
|
||||
if ((cnt++ % 100000) == 0) {
|
||||
ToggleLed(0);
|
||||
}
|
||||
});
|
||||
// should never get here
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user