mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Revision:
159:612c381a210f
Parent:
149:156823d33999
Child:
167:e84263d55307
--- a/targets/TARGET_ONSEMI/TARGET_NCS36510/ncs36510_us_ticker_api.c	Tue Feb 14 14:44:10 2017 +0000
+++ b/targets/TARGET_ONSEMI/TARGET_NCS36510/ncs36510_us_ticker_api.c	Tue Feb 28 17:13:35 2017 +0000
@@ -37,7 +37,7 @@
 
 static void us_timer_init(void);
 
-static uint32_t us_ticker_int_counter = 0;
+static uint32_t us_ticker_target = 0;
 static volatile uint32_t msb_counter = 0;
 
 void us_ticker_init(void)
@@ -168,20 +168,25 @@
     /* Clear IRQ flag */
     TIM1REG->CLEAR = 0;
 
-    /* If this is a longer timer it will take multiple full hw counter cycles */
-    if (us_ticker_int_counter > 0) {
-        ticker_set(0xFFFF);
-        us_ticker_int_counter--;
-    } else {
+    int32_t delta = us_ticker_target - us_ticker_read();
+    if (delta <= 0) {
         TIM1REG->CONTROL.BITS.ENABLE = False;
         us_ticker_irq_handler();
+    } else {
+        // Clamp at max value of timer
+        if (delta > 0xFFFF) {
+            delta = 0xFFFF;
+        }
+
+        ticker_set(delta);
     }
 }
 
 /* Set timer 1 ticker interrupt */
 void us_ticker_set_interrupt(timestamp_t timestamp)
 {
-    int32_t delta = (uint32_t)timestamp - us_ticker_read();
+    us_ticker_target = (uint32_t)timestamp;
+    int32_t delta = us_ticker_target - us_ticker_read();
 
     if (delta <= 0) {
         /* This event was in the past */
@@ -195,10 +200,10 @@
         return;
     }
 
-    /* Calculate how much delta falls outside the 16-bit counter range. */
-    /* You will have to perform a full timer overflow for each bit above */
-    /* that range. */
-    us_ticker_int_counter = (uint32_t)(delta >> 16);
+    // Clamp at max value of timer
+    if (delta > 0xFFFF) {
+        delta = 0xFFFF;
+    }
 
     ticker_set(delta);
 }