MRT.cpp

#include "MRT.h"

MRT::MRT(int channel)
{
     LPC_SYSCON->SYSAHBCLKCTRL |= (1<<10); // enable MRT
     LPC_SYSCON->PRESETCTRL |= (1<<7); // reset MRT

    _ch = &LPC_MRT->Channel[channel];
    _ch->CTRL |= (1<<1); // one-shot
}

void MRT::write(uint32_t interval)
{
    _ch->INTVAL = interval | (1<<31); // and LOAD
    _ch->STAT |= 0x01;
}

int MRT::status()
{
    if (_ch->STAT & 1) {
        return IDLE;
    }
    return RUNNING;
}

void MRT::wait_ms(uint32_t timeout_ms)
{
    write((SystemCoreClock/1000) * timeout_ms);
    while(status() == RUNNING);
}

void MRT::wait_us(uint32_t timeout_us)
{
    write((SystemCoreClock/1000000) * timeout_us);
    while(status() == RUNNING);
}

uint32_t MRT::read()
{
     return _ch->TIMER;
}