arm: async echo app
This commit is contained in:
44
arm/uart.cc
44
arm/uart.cc
@@ -1,5 +1,9 @@
|
||||
#include "uart.h"
|
||||
|
||||
#include "gpio.h"
|
||||
#include "ring_buffer.h"
|
||||
#include "trace.h"
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr uintptr_t kUart0BaseAddress = 0x40001000;
|
||||
@@ -12,6 +16,10 @@ XUartLite_Config uart0_config = {
|
||||
.DataBits = 8,
|
||||
};
|
||||
|
||||
constexpr size_t kUartTxBufferSize = 256;
|
||||
std::array<std::byte, kUartTxBufferSize> tx_buffer = {};
|
||||
RingBuffer tx_ring_buffer{.buffer = tx_buffer};
|
||||
|
||||
} // namespace
|
||||
|
||||
XUartLite* uart0 = &uart0_inst;
|
||||
@@ -19,3 +27,39 @@ XUartLite* uart0 = &uart0_inst;
|
||||
void InitUarts() {
|
||||
XUartLite_CfgInitialize(uart0, &uart0_config, uart0_config.RegBaseAddr);
|
||||
}
|
||||
|
||||
extern "C" uint8_t XUartLite_GetSR(XUartLite* InstancePtr);
|
||||
#define XUL_SR_TX_FIFO_EMPTY 0x04 /* transmit FIFO empty */
|
||||
|
||||
void UartSendCrash(std::span<const std::byte> data) {
|
||||
while (data.size() > 0) {
|
||||
while ((XUartLite_GetSR(uart0) & XUL_SR_TX_FIFO_EMPTY) == 0) {
|
||||
}
|
||||
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_GetSR(uart0) & XUL_SR_TX_FIFO_EMPTY) == 0) {
|
||||
}
|
||||
}
|
||||
|
||||
// blocking
|
||||
void UartSend(std::span<const std::byte> data) {
|
||||
while (!tx_ring_buffer.Store(data)) {
|
||||
}
|
||||
|
||||
if (!XUartLite_IsSending(uart0)) {
|
||||
XUartLite_Send(uart0, tx_ring_buffer.RawReadPointer(),
|
||||
tx_ring_buffer.ContiguousAvailableData());
|
||||
}
|
||||
}
|
||||
|
||||
void HandleUartTxFromIsr(void*, unsigned int transmitted) {
|
||||
tracing::trace(tracing::TraceEvent::kUartTxCb);
|
||||
tx_ring_buffer.Pop(transmitted);
|
||||
if (tx_ring_buffer.AvailableData() > 0) {
|
||||
XUartLite_Send(uart0, tx_ring_buffer.RawReadPointer(),
|
||||
tx_ring_buffer.ContiguousAvailableData());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user