Mark Gottscho / HardwareTimersLib

Fork of HardwareTimersLib by Mark Gottscho

Revision:
10:12c1c5cef918
Parent:
9:dff7c891ec77
Child:
11:e0442c8b182b
--- a/Timer_TPM.cpp	Tue Mar 11 03:56:01 2014 +0000
+++ b/Timer_TPM.cpp	Tue Mar 11 04:18:04 2014 +0000
@@ -38,11 +38,16 @@
     uint16_t tick;
     uint32_t count;
     
-    __disable_irq(); //CRITICAL SECTION -- ALL INTERRUPTS MUST BE STOPPED!
-    TPM0->CNT = 0; //Need to write to the TPM CNT register in order to read it (buffering)
-    tick = (uint16_t) TPM0->CNT;
-    count = __count;
-    __enable_irq(); //END CRITICAL SECTION
+    //__disable_irq(); //CRITICAL SECTION -- ALL INTERRUPTS MUST BE STOPPED!
+    TPM0->CNT = 0; //Need to write to the TPM CNT register in order to latch current value.
+    
+    //It is theoretically possible that the timer interrupt is invoked here, between these two lines.
+    //However, the timer doesn't appear to work properly if I put these two in a critical section.
+    
+    count = __count; //Latch current count value.
+    //__enable_irq(); //END CRITICAL SECTION
+    
+    tick = (uint16_t) TPM0->CNT; //Value was previously latched, just read it
     
     //Convert to ticks
     return count * __rolloverValue + (uint32_t) tick;