Mark Gottscho / HardwareTimersLib

Fork of HardwareTimersLib by Mark Gottscho

Timer_PIT.cpp

Committer:
mgottscho
Date:
2014-03-09
Revision:
3:dd54446143ee
Parent:
2:5056ec8c52e8
Child:
7:78f6ee57d324

File content as of revision 3:dd54446143ee:

/* Timer_PIT.cpp
 * Tested with mbed board: FRDM-KL46Z
 * Author: Mark Gottscho
 * mgottscho@ucla.edu
 */

#include "mbed.h"
#include "HardwareTimer.h"
#include "Timer_PIT.h"

//Init class static variable
bool Timer_PIT::__initialized_pit = false;
uint32_t Timer_PIT::__rolloverValue = 0xFFFFFFFF;
uint32_t Timer_PIT::__count = 0;


Timer_PIT::Timer_PIT(float tickValue) :
        HardwareTimer(tickValue)
        {   
}

Timer_PIT::~Timer_PIT() {}

void Timer_PIT::disableTimer() {
    if (!__valid)
        return;
        
    PIT->MCR |= PIT_MCR_MDIS_MASK; //Setting MDIS bit disables the timer.
    __enabled = false;
}

uint32_t Timer_PIT::getTick() {
    if (!__valid)
        return 0;
        
    //Get raw time
    __disable_irq();
    uint32_t tick = PIT->CHANNEL[0].CVAL;
    uint32_t count = __count;
    __enable_irq();
    
    //Convert to ticks
    return (uint32_t) count * __rolloverValue + tick;
}

void Timer_PIT::__set_pit(uint32_t count) {
    PIT->CHANNEL[0].LDVAL = count; //Load the countdown value. PIT counts downwards.
    PIT->CHANNEL[0].TCTRL |= PIT_TCTRL_TEN_MASK; //Enable the timer.
}

void Timer_PIT::__isr_pit() {
    PIT->CHANNEL[0].TFLG |= PIT_TFLG_TIF_MASK; //Clear the timer interrupt flag bit
    __count++;
}