synth/mbv/apps/async/main2.cc

109 lines
2.1 KiB
C++

#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;
}