//#DOC Main, program entry and main loop
#include "main.h"
#include <otSerial.h>
#include <otTimer.h>
#include <otPrintfLite.h>

U32 counts = 0;

// For all interrupts service routine you MUST specify FAST_CODE_1 or
// FAST_CODE_2 as locator attribute!
FAST_CODE_2 void _coreTimerCallBack()
{
    counts++;
}

void setup()
{
    UartEnableSignals(UART0);
    UartSetBaud(UART0, 115200);
    setStdout(UART0);   // printf output is now directed on UART0
    // Prescaler action 400MHz / 40 (39+1) = 10MHz (100ns)
    // Period: 5000000 * 100ns = 0.5s
    CoreTimerSetInterruptService(39, 5000000, true, _coreTimerCallBack);
    CoreTimerEnable(true);
}

void loop()
{
    U32     c = counts;
    
    while(c == counts) DelayMs(1);
    printf("CoreTimer: %5u\r", counts);
}