mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Revision:
482:d9a48e768ce0
Parent:
460:3bcf9be0332c
--- a/targets/hal/TARGET_RENESAS/TARGET_RZ_A1H/us_ticker.c	Thu Feb 26 09:30:08 2015 +0000
+++ b/targets/hal/TARGET_RENESAS/TARGET_RZ_A1H/us_ticker.c	Fri Feb 27 10:00:08 2015 +0000
@@ -30,6 +30,7 @@
 static double count_clock = 0;
 static uint32_t last_read = 0;
 static uint32_t wrap_arround = 0;
+static uint64_t ticker_us_last64 = 0;
 
 void us_ticker_interrupt(void) {
     us_ticker_irq_handler();
@@ -61,69 +62,67 @@
     GIC_EnableIRQ(US_TICKER_TIMER_IRQn);
 }
 
-uint64_t us_ticker_read64() {
-    uint32_t val;
-    volatile uint64_t val64;
-    int check_irq_masked;
-
-    check_irq_masked = __disable_irq();
+static uint64_t ticker_read_counter64(void) {
+    uint32_t cnt_val;
+    uint64_t cnt_val64;
 
     if (!us_ticker_inited)
         us_ticker_init();
 
     /* read counter */
-    val = OSTM1CNT;
-    if ( last_read > val ) {
+    cnt_val = OSTM1CNT;
+    if (last_read > cnt_val) {
         wrap_arround++;
     }
-    last_read = val;
-    val64 = ((uint64_t)wrap_arround << 32) + val;
+    last_read = cnt_val;
+    cnt_val64 = ((uint64_t)wrap_arround << 32) + cnt_val;
+
+    return cnt_val64;
+}
 
-    /* clock to us */
-    val64 = val64 / count_clock;
+uint32_t us_ticker_read() {
+    uint64_t cnt_val64;
+    uint64_t us_val64;
+    int check_irq_masked;
+
+    check_irq_masked = __disable_irq();
+
+    cnt_val64        = ticker_read_counter64();
+    us_val64         = (cnt_val64 / count_clock);
+    ticker_us_last64 = us_val64;
 
     if (!check_irq_masked) {
         __enable_irq();
     }
 
-    return val64;
-}
-
-uint32_t us_ticker_read() {
-    return (uint32_t)us_ticker_read64();
+    /* clock to us */
+    return (uint32_t)us_val64;
 }
 
 void us_ticker_set_interrupt(timestamp_t timestamp) {
     // set match value
-    volatile uint64_t set_cmp_val = 0;
-    uint64_t  timestamp_tmp;
-    int64_t  timestamp_req;
-    int64_t  timestamp_comp;
-    uint64_t timestamp_now = us_ticker_read64();
-    
+    uint64_t timestamp64;
+    uint64_t set_cmp_val64;
+    volatile uint32_t set_cmp_val;
+    uint64_t count_val_64;
+
     /* calc compare mach timestamp */
-    set_cmp_val = (timestamp_now & 0xFFFFFFFF00000000) + timestamp;
-    
-    timestamp_tmp = (uint64_t)timestamp;
-    timestamp_req = (int64_t)timestamp_tmp;
-    
-    timestamp_tmp = (uint64_t)(timestamp_now & 0x00000000FFFFFFFF);
-    timestamp_comp = (int64_t)timestamp_tmp;
-    
-    if (timestamp_req <= timestamp_comp + 1) {
-        if (((timestamp_req - timestamp_comp) <= 1) && ((timestamp_req - timestamp_comp) >= -10)) {
-            /* This event was in the past */
-            us_ticker_irq_handler();
-            return;
-        } else {
-            /* This event is wrap arround */
-            set_cmp_val += 0x100000000;
-        }
+    timestamp64 = (ticker_us_last64 & 0xFFFFFFFF00000000) + timestamp;
+    if (timestamp < (ticker_us_last64 & 0x00000000FFFFFFFF)) {
+        /* This event is wrap arround */
+        timestamp64 += 0x100000000;
     }
-    
+
     /* calc compare mach timestamp */
-    set_cmp_val = set_cmp_val * count_clock;
-    OSTM1CMP  = (uint32_t)(set_cmp_val & 0xffffffff);
+    set_cmp_val64  = timestamp64 * count_clock;
+    set_cmp_val    = (uint32_t)(set_cmp_val64 & 0x00000000FFFFFFFF);
+    count_val_64   = ticker_read_counter64();
+    if (set_cmp_val64 <= (count_val_64 + 500)) {
+        GIC_SetPendingIRQ(US_TICKER_TIMER_IRQn);
+        GIC_EnableIRQ(US_TICKER_TIMER_IRQn);
+        return;
+    }
+    OSTM1CMP = set_cmp_val;
     GIC_EnableIRQ(US_TICKER_TIMER_IRQn);
 }
 
@@ -132,6 +131,5 @@
 }
 
 void us_ticker_clear_interrupt(void) {
-    /* There are no Flags of OSTM1 to clear here */
-    /* Do Nothing */
+    GIC_ClearPendingIRQ(US_TICKER_TIMER_IRQn);
 }