extern "C" {
    #include "nrf52.h"
}

#include "virtual_timer/VirtualTimerDistributor.h"
#include "platform/cm4/InterruptHandler.h"
#include "platform/cm4/InterruptGuardian.h"
#include "platform/cm4/SystemTick.h"

using namespace pinetime::platform::cm4;
using namespace pinetime::virtual_timer;

SystemTick::SystemTick()
    : InterruptHandler(InterruptGuardian::Cm4IrqN::SYS_TICK_IRQ)
    , sys_tick(0)
{
}

void SystemTick::handle()
{
    this->sys_tick++;
    VirtualTimerDistributor::instance().notify(this->sys_tick);
}

void SystemTick::enable_timer()
{
    SysTick_Config(SystemCoreClock / 1000);
}

void SystemTick::disable_timer()
{
    SysTick->CTRL = 0;
}

uint64_t SystemTick::tick()
{
    return this->sys_tick;
}
