mbed

Fork of mbed-dev by mbed official

Revision:
175:b96e65c34a4d
Parent:
168:9672193075cf
diff -r e131a1973e81 -r b96e65c34a4d targets/TARGET_STM/us_ticker_32b.c
--- a/targets/TARGET_STM/us_ticker_32b.c	Fri Sep 15 14:59:18 2017 +0100
+++ b/targets/TARGET_STM/us_ticker_32b.c	Mon Oct 02 15:33:19 2017 +0100
@@ -23,49 +23,44 @@
 
 TIM_HandleTypeDef TimMasterHandle;
 
-static int us_ticker_inited = 0;
-
 void us_ticker_init(void)
 {
-    if (us_ticker_inited) return;
-    us_ticker_inited = 1;
-
-    TimMasterHandle.Instance = TIM_MST;
-
-    HAL_InitTick(0); // The passed value is not used
+    /* NOTE: assuming that HAL tick has already been initialized! */
 }
 
 uint32_t us_ticker_read()
 {
-    if (!us_ticker_inited) us_ticker_init();
     return TIM_MST->CNT;
 }
 
 void us_ticker_set_interrupt(timestamp_t timestamp)
 {
-    TimMasterHandle.Instance = TIM_MST;
+    // NOTE: This function must be called with interrupts disabled to keep our
+    //       timer interrupt setup atomic
+
     // disable IT while we are handling the correct timestamp
     __HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC1);
     // Set new output compare value
     __HAL_TIM_SET_COMPARE(&TimMasterHandle, TIM_CHANNEL_1, (uint32_t)timestamp);
-    // Check if timestamp has already passed, and if so, set the event immediately
-    if ((int32_t)(timestamp - TIM_MST->CNT) <= 0) {
-        LL_TIM_GenerateEvent_CC1(TimMasterHandle.Instance);
-    }
     // Enable IT
     __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC1);
 }
 
+void us_ticker_fire_interrupt(void)
+{
+    LL_TIM_GenerateEvent_CC1(TimMasterHandle.Instance);
+}
+
+/* NOTE: must be called with interrupts disabled! */
 void us_ticker_disable_interrupt(void)
 {
-    TimMasterHandle.Instance = TIM_MST;
     __HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC1);
 }
 
+/* NOTE: must be called with interrupts disabled! */
 void us_ticker_clear_interrupt(void)
 {
-    TimMasterHandle.Instance = TIM_MST;
-    __HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC1);
+    __HAL_TIM_CLEAR_FLAG(&TimMasterHandle, TIM_FLAG_CC1);
 }
 
 #endif // !TIM_MST_16BIT