mbed library sources. Supersedes mbed-src.

Fork of mbed-dev by mbed official

Revision:
175:af195413fb11
Parent:
174:b96e65c34a4d
diff -r b96e65c34a4d -r af195413fb11 drivers/Ticker.h
--- a/drivers/Ticker.h	Mon Oct 02 15:33:19 2017 +0100
+++ b/drivers/Ticker.h	Wed Oct 11 12:45:49 2017 +0100
@@ -21,13 +21,15 @@
 #include "platform/mbed_toolchain.h"
 #include "platform/NonCopyable.h"
 #include "platform/mbed_sleep.h"
+#include "hal/lp_ticker_api.h"
+#include "platform/mbed_critical.h"
 
 namespace mbed {
 /** \addtogroup drivers */
 
 /** A Ticker is used to call a function at a recurring interval
  *
- *  You can use as many seperate Ticker objects as you require.
+ *  You can use as many separate Ticker objects as you require.
  *
  * @note Synchronization level: Interrupt safe
  *
@@ -64,14 +66,18 @@
 class Ticker : public TimerEvent, private NonCopyable<Ticker> {
 
 public:
-    Ticker() : TimerEvent(), _function(0) {
+    Ticker() : TimerEvent(), _function(0), _lock_deepsleep(true) {
     }
 
-    Ticker(const ticker_data_t *data) : TimerEvent(data), _function(0) {
+    // When low power ticker is in use, then do not disable deep-sleep.
+    Ticker(const ticker_data_t *data) : TimerEvent(data), _function(0), _lock_deepsleep(true)  {
         data->interface->init();
+#if DEVICE_LOWPOWERTIMER
+        _lock_deepsleep = (data != get_lp_ticker_data());
+#endif
     }
 
-    /** Attach a function to be called by the Ticker, specifiying the interval in seconds
+    /** Attach a function to be called by the Ticker, specifying the interval in seconds
      *
      *  @param func pointer to the function to be called
      *  @param t the time between calls in seconds
@@ -80,7 +86,7 @@
         attach_us(func, t * 1000000.0f);
     }
 
-    /** Attach a member function to be called by the Ticker, specifiying the interval in seconds
+    /** Attach a member function to be called by the Ticker, specifying the interval in seconds
      *
      *  @param obj pointer to the object to call the member function on
      *  @param method pointer to the member function to be called
@@ -97,21 +103,28 @@
         attach(callback(obj, method), t);
     }
 
-    /** Attach a function to be called by the Ticker, specifiying the interval in micro-seconds
+    /** Attach a function to be called by the Ticker, specifying the interval in micro-seconds
      *
      *  @param func pointer to the function to be called
      *  @param t the time between calls in micro-seconds
+     *
+     *  @note setting @a t to a value shorter that it takes to process the ticker callback
+     *  will cause the system to hang. Ticker callback will be called constantly with no time
+     *  for threads scheduling.
+     *
      */
     void attach_us(Callback<void()> func, us_timestamp_t t) {
-        // lock only for the initial callback setup
-        if (!_function) {
+        core_util_critical_section_enter();
+        // lock only for the initial callback setup and this is not low power ticker
+        if(!_function && _lock_deepsleep) {
             sleep_manager_lock_deep_sleep();
         }
         _function = func;
         setup(t);
+        core_util_critical_section_exit();
     }
 
-    /** Attach a member function to be called by the Ticker, specifiying the interval in micro-seconds
+    /** Attach a member function to be called by the Ticker, specifying the interval in micro-seconds
      *
      *  @param obj pointer to the object to call the member function on
      *  @param method pointer to the member function to be called
@@ -143,6 +156,7 @@
 protected:
     us_timestamp_t         _delay;  /**< Time delay (in microseconds) for re-setting the multi-shot callback. */
     Callback<void()>    _function;  /**< Callback. */
+    bool          _lock_deepsleep;  /**< Flag which indicates if deep-sleep should be disabled. */
 };
 
 } // namespace mbed