Mark Gottscho / HardwareTimersLib

Fork of HardwareTimersLib by Mark Gottscho

Revision:
2:5056ec8c52e8
Child:
3:dd54446143ee
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Timer_PIT.cpp	Sun Mar 09 03:02:55 2014 +0000
@@ -0,0 +1,54 @@
+/* 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;
+uint16_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++;
+}
\ No newline at end of file