Mark Gottscho / HardwareTimersLib

Fork of HardwareTimersLib by Mark Gottscho

Committer:
mgottscho
Date:
Sun Mar 09 03:09:42 2014 +0000
Revision:
3:dd54446143ee
Parent:
2:5056ec8c52e8
Child:
7:78f6ee57d324
Fixed PIT rollover value

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mgottscho 2:5056ec8c52e8 1 /* Timer_PIT.cpp
mgottscho 2:5056ec8c52e8 2 * Tested with mbed board: FRDM-KL46Z
mgottscho 2:5056ec8c52e8 3 * Author: Mark Gottscho
mgottscho 2:5056ec8c52e8 4 * mgottscho@ucla.edu
mgottscho 2:5056ec8c52e8 5 */
mgottscho 2:5056ec8c52e8 6
mgottscho 2:5056ec8c52e8 7 #include "mbed.h"
mgottscho 2:5056ec8c52e8 8 #include "HardwareTimer.h"
mgottscho 2:5056ec8c52e8 9 #include "Timer_PIT.h"
mgottscho 2:5056ec8c52e8 10
mgottscho 2:5056ec8c52e8 11 //Init class static variable
mgottscho 2:5056ec8c52e8 12 bool Timer_PIT::__initialized_pit = false;
mgottscho 3:dd54446143ee 13 uint32_t Timer_PIT::__rolloverValue = 0xFFFFFFFF;
mgottscho 2:5056ec8c52e8 14 uint32_t Timer_PIT::__count = 0;
mgottscho 2:5056ec8c52e8 15
mgottscho 2:5056ec8c52e8 16
mgottscho 2:5056ec8c52e8 17 Timer_PIT::Timer_PIT(float tickValue) :
mgottscho 2:5056ec8c52e8 18 HardwareTimer(tickValue)
mgottscho 2:5056ec8c52e8 19 {
mgottscho 2:5056ec8c52e8 20 }
mgottscho 2:5056ec8c52e8 21
mgottscho 2:5056ec8c52e8 22 Timer_PIT::~Timer_PIT() {}
mgottscho 2:5056ec8c52e8 23
mgottscho 2:5056ec8c52e8 24 void Timer_PIT::disableTimer() {
mgottscho 2:5056ec8c52e8 25 if (!__valid)
mgottscho 2:5056ec8c52e8 26 return;
mgottscho 2:5056ec8c52e8 27
mgottscho 2:5056ec8c52e8 28 PIT->MCR |= PIT_MCR_MDIS_MASK; //Setting MDIS bit disables the timer.
mgottscho 2:5056ec8c52e8 29 __enabled = false;
mgottscho 2:5056ec8c52e8 30 }
mgottscho 2:5056ec8c52e8 31
mgottscho 2:5056ec8c52e8 32 uint32_t Timer_PIT::getTick() {
mgottscho 2:5056ec8c52e8 33 if (!__valid)
mgottscho 2:5056ec8c52e8 34 return 0;
mgottscho 2:5056ec8c52e8 35
mgottscho 2:5056ec8c52e8 36 //Get raw time
mgottscho 2:5056ec8c52e8 37 __disable_irq();
mgottscho 2:5056ec8c52e8 38 uint32_t tick = PIT->CHANNEL[0].CVAL;
mgottscho 2:5056ec8c52e8 39 uint32_t count = __count;
mgottscho 2:5056ec8c52e8 40 __enable_irq();
mgottscho 2:5056ec8c52e8 41
mgottscho 2:5056ec8c52e8 42 //Convert to ticks
mgottscho 2:5056ec8c52e8 43 return (uint32_t) count * __rolloverValue + tick;
mgottscho 2:5056ec8c52e8 44 }
mgottscho 2:5056ec8c52e8 45
mgottscho 2:5056ec8c52e8 46 void Timer_PIT::__set_pit(uint32_t count) {
mgottscho 2:5056ec8c52e8 47 PIT->CHANNEL[0].LDVAL = count; //Load the countdown value. PIT counts downwards.
mgottscho 2:5056ec8c52e8 48 PIT->CHANNEL[0].TCTRL |= PIT_TCTRL_TEN_MASK; //Enable the timer.
mgottscho 2:5056ec8c52e8 49 }
mgottscho 2:5056ec8c52e8 50
mgottscho 2:5056ec8c52e8 51 void Timer_PIT::__isr_pit() {
mgottscho 2:5056ec8c52e8 52 PIT->CHANNEL[0].TFLG |= PIT_TFLG_TIF_MASK; //Clear the timer interrupt flag bit
mgottscho 2:5056ec8c52e8 53 __count++;
mgottscho 2:5056ec8c52e8 54 }