Ok

Dependencies:   mbed_rtos_types Mutex mbed_rtos_storage mbed Semaphore

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers RtosTimer.h Source File

RtosTimer.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2012 ARM Limited
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to deal
00006  * in the Software without restriction, including without limitation the rights
00007  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  * copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00020  * SOFTWARE.
00021  */
00022 #ifndef RTOS_TIMER_H
00023 #define RTOS_TIMER_H
00024 
00025 #include <stdint.h>
00026 #include "rtos/mbed_rtos_types.h"
00027 #include "rtos/mbed_rtos_storage.h"
00028 #include "platform/Callback.h"
00029 #include "platform/NonCopyable.h"
00030 #include "platform/mbed_toolchain.h"
00031 #include "rtos/mbed_rtos1_types.h"
00032 
00033 #if MBED_CONF_RTOS_PRESENT || defined(DOXYGEN_ONLY)
00034 
00035 namespace rtos {
00036 /** \addtogroup rtos */
00037 /** @{*/
00038 /**
00039  * \defgroup rtos_RtosTimer RtosTimer class
00040  * @{
00041  */
00042 
00043 /** The RtosTimer class allow creating and and controlling of timer functions in the system.
00044  A timer function is called when a time period expires whereby both on-shot and
00045  periodic timers are possible. A timer can be started, restarted, or stopped.
00046 
00047  Timers are handled in the thread osTimerThread.
00048  Callback functions run under control of this thread and may use CMSIS-RTOS API calls.
00049 
00050  @deprecated
00051  The RtosTimer has been superseded by the EventQueue. The RtosTimer and EventQueue duplicate
00052  the functionality of timing events outside of interrupt context, however the EventQueue
00053  has additional features to handle deferring other events to multiple contexts.
00054 
00055  For an example, the following code shows a simple use of the RtosTimer:
00056  @code
00057  DigitalOut led(LED1);
00058  void blink() {
00059      led = !led;
00060  }
00061 
00062  RtosTimer timer(&blink);
00063  int main() {
00064      timer.start(1000); // call blink every 1s
00065      ThisThread::sleep_for(5000);
00066      timer.stop(); // stop after 5s
00067  }
00068  @endcode
00069 
00070  This is the above example rewritten to use the EventQueue:
00071  @code
00072  DigitalOut led(LED1);
00073  void blink() {
00074      led = !led;
00075  }
00076 
00077  EventQueue queue(4*EVENTS_EVENT_SIZE);
00078  int main() {
00079     int blink_id = queue.call_every(1000, &blink); // call blink every 1s
00080     queue.dispatch(5000);
00081     queue.cancel(blink_id); // stop after 5s
00082  }
00083  @endcode
00084 
00085  @note
00086  Memory considerations: The timer control structures will be created on current thread's stack, both for the mbed OS
00087  and underlying RTOS objects (static or dynamic RTOS memory pools are not being used).
00088 */
00089 class RtosTimer : private mbed::NonCopyable<RtosTimer> {
00090 public:
00091     /** Create timer.
00092       @param   func      function to be executed by this timer.
00093       @param   type      osTimerOnce for one-shot or osTimerPeriodic for periodic behavior. (default: osTimerPeriodic)
00094       @param   argument  argument to the timer call back function. (default: nullptr)
00095       @deprecated Replaced with RtosTimer(Callback<void()>, os_timer_type)
00096       @deprecated
00097           The RtosTimer has been superseded by the EventQueue. See RtosTimer.h for more details
00098 
00099       @note You cannot call this function from ISR context.
00100      */
00101     MBED_DEPRECATED_SINCE("mbed-os-5.1",
00102                           "Replaced with RtosTimer(Callback<void()>, os_timer_type)")
00103     MBED_DEPRECATED_SINCE("mbed-os-5.2",
00104                           "The RtosTimer has been superseded by the EventQueue. See RtosTimer.h for more details")
00105     RtosTimer(void (*func)(void const *argument), os_timer_type type = osTimerPeriodic, void *argument = nullptr)
00106     {
00107         constructor(mbed::callback((void (*)(void *))func, argument), type);
00108     }
00109 
00110     /** Create timer.
00111       @param   func      function to be executed by this timer.
00112       @param   type      osTimerOnce for one-shot or osTimerPeriodic for periodic behavior. (default: osTimerPeriodic)
00113       @deprecated
00114           The RtosTimer has been superseded by the EventQueue. See RtosTimer.h for more details
00115 
00116       @note You cannot call this function from ISR context.
00117     */
00118     MBED_DEPRECATED_SINCE("mbed-os-5.2",
00119                           "The RtosTimer has been superseded by the EventQueue. See RtosTimer.h for more details")
00120     RtosTimer(mbed::Callback<void()> func, os_timer_type type = osTimerPeriodic)
00121     {
00122         constructor(func, type);
00123     }
00124 
00125     /** Create timer.
00126       @param   obj       pointer to the object to call the member function on.
00127       @param   method    member function to be executed by this timer.
00128       @param   type      osTimerOnce for one-shot or osTimerPeriodic for periodic behavior. (default: osTimerPeriodic)
00129       @deprecated
00130           The RtosTimer constructor does not support cv-qualifiers. Replaced by
00131           RtosTimer(callback(obj, method), os_timer_type).
00132       @deprecated
00133           The RtosTimer has been superseded by the EventQueue. See RtosTimer.h for more details
00134 
00135       @note You cannot call this function from ISR context.
00136     */
00137     template <typename T, typename M>
00138     MBED_DEPRECATED_SINCE("mbed-os-5.1",
00139                           "The RtosTimer constructor does not support cv-qualifiers. Replaced by "
00140                           "RtosTimer(callback(obj, method), os_timer_type).")
00141     MBED_DEPRECATED_SINCE("mbed-os-5.2",
00142                           "The RtosTimer has been superseded by the EventQueue. See RtosTimer.h for more details")
00143     RtosTimer(T *obj, M method, os_timer_type type = osTimerPeriodic)
00144     {
00145         constructor(mbed::callback(obj, method), type);
00146     }
00147 
00148     /** Stop the timer.
00149       @return  status code that indicates the execution status of the function:
00150           @a osOK the timer has been stopped.
00151           @a osErrorISR @a stop cannot be called from interrupt service routines.
00152           @a osErrorParameter internal error.
00153           @a osErrorResource the timer is not running.
00154 
00155       @note You cannot call this function from ISR context.
00156     */
00157     osStatus stop(void);
00158 
00159     /** Start or restart the timer.
00160       @param   millisec  non-zero value of the timer.
00161       @return  status code that indicates the execution status of the function:
00162           @a osOK the timer has been started or restarted.
00163           @a osErrorISR @a start cannot be called from interrupt service routines.
00164           @a osErrorParameter internal error or incorrect parameter value.
00165           @a osErrorResource internal error (the timer is in an invalid timer state).
00166 
00167       @note You cannot call this function from ISR context.
00168     */
00169     osStatus start(uint32_t millisec);
00170 
00171     /** RtosTimer destructor
00172      *
00173      * @note You cannot call this function from ISR context.
00174      */
00175     ~RtosTimer();
00176 
00177 private:
00178     // Required to share definitions without
00179     // delegated constructors
00180     void constructor(mbed::Callback<void()> func, os_timer_type type);
00181 
00182     osTimerId_t _id;
00183     mbed_rtos_storage_timer_t _obj_mem;
00184     mbed::Callback<void()> _function;
00185 };
00186 /** @}*/
00187 /** @}*/
00188 
00189 }
00190 
00191 #endif
00192 
00193 #endif
00194