Lee Shen / FTHR_USB_serial_qSPI
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 "cmsis_os2.h"
00027 #include "mbed_rtos_storage.h"
00028 #include "platform/Callback.h"
00029 #include "platform/NonCopyable.h"
00030 #include "platform/mbed_toolchain.h"
00031 #include "mbed_rtos1_types.h"
00032 
00033 namespace rtos {
00034 /** \addtogroup rtos */
00035 /** @{*/
00036 
00037 /** The RtosTimer class allow creating and and controlling of timer functions in the system.
00038  A timer function is called when a time period expires whereby both on-shot and
00039  periodic timers are possible. A timer can be started, restarted, or stopped.
00040 
00041  Timers are handled in the thread osTimerThread.
00042  Callback functions run under control of this thread and may use CMSIS-RTOS API calls.
00043 
00044  @deprecated
00045  The RtosTimer has been superseded by the EventQueue. The RtosTimer and EventQueue duplicate
00046  the functionality of timing events outside of interrupt context, however the EventQueue
00047  has additional features to handle deferring other events to multiple contexts.
00048 
00049  For an example, the following code shows a simple use of the RtosTimer:
00050  @code
00051  DigitalOut led(LED1);
00052  void blink() {
00053      led = !led;
00054  }
00055 
00056  RtosTimer timer(&blink);
00057  int main() {
00058      timer.start(1000); // call blink every 1s
00059      wait_ms(5000);
00060      timer.stop(); // stop after 5s
00061  }
00062  @endcode
00063 
00064  This is the above example rewritten to use the EventQueue:
00065  @code
00066  DigitalOut led(LED1);
00067  void blink() {
00068      led = !led;
00069  }
00070 
00071  EventQueue queue(4*EVENTS_EVENT_SIZE);
00072  int main() {
00073     int blink_id = queue.call_every(1000, &blink); // call blink every 1s
00074     queue.dispatch(5000);
00075     queue.cancel(blink_id); // stop after 5s
00076  }
00077  @endcode
00078 
00079  @note
00080  Memory considerations: The timer control structures will be created on current thread's stack, both for the mbed OS
00081  and underlying RTOS objects (static or dynamic RTOS memory pools are not being used).
00082 */
00083 class RtosTimer : private mbed::NonCopyable<RtosTimer> {
00084 public:
00085     /** Create timer.
00086       @param   func      function to be executed by this timer.
00087       @param   type      osTimerOnce for one-shot or osTimerPeriodic for periodic behaviour. (default: osTimerPeriodic)
00088       @param   argument  argument to the timer call back function. (default: NULL)
00089       @deprecated Replaced with RtosTimer(Callback<void()>, os_timer_type)
00090       @deprecated
00091           The RtosTimer has been superseded by the EventQueue. See RtosTimer.h for more details
00092      */
00093     MBED_DEPRECATED_SINCE("mbed-os-5.1",
00094         "Replaced with RtosTimer(Callback<void()>, os_timer_type)")
00095     MBED_DEPRECATED_SINCE("mbed-os-5.2",
00096         "The RtosTimer has been superseded by the EventQueue. See RtosTimer.h for more details")
00097     RtosTimer(void (*func)(void const *argument), os_timer_type type=osTimerPeriodic, void *argument=NULL) {
00098         constructor(mbed::callback((void (*)(void *))func, argument), type);
00099     }
00100     
00101     /** Create timer.
00102       @param   func      function to be executed by this timer.
00103       @param   type      osTimerOnce for one-shot or osTimerPeriodic for periodic behaviour. (default: osTimerPeriodic)
00104       @deprecated
00105           The RtosTimer has been superseded by the EventQueue. See RtosTimer.h for more details
00106     */
00107     MBED_DEPRECATED_SINCE("mbed-os-5.2",
00108         "The RtosTimer has been superseded by the EventQueue. See RtosTimer.h for more details")
00109     RtosTimer(mbed::Callback<void()> func, os_timer_type type=osTimerPeriodic) {
00110         constructor(func, type);
00111     }
00112     
00113     /** Create timer.
00114       @param   obj       pointer to the object to call the member function on.
00115       @param   method    member function to be executed by this timer.
00116       @param   type      osTimerOnce for one-shot or osTimerPeriodic for periodic behaviour. (default: osTimerPeriodic)
00117       @deprecated
00118           The RtosTimer constructor does not support cv-qualifiers. Replaced by
00119           RtosTimer(callback(obj, method), os_timer_type).
00120       @deprecated
00121           The RtosTimer has been superseded by the EventQueue. See RtosTimer.h for more details
00122     */
00123     template <typename T, typename M>
00124     MBED_DEPRECATED_SINCE("mbed-os-5.1",
00125         "The RtosTimer constructor does not support cv-qualifiers. Replaced by "
00126         "RtosTimer(callback(obj, method), os_timer_type).")
00127     MBED_DEPRECATED_SINCE("mbed-os-5.2",
00128         "The RtosTimer has been superseded by the EventQueue. See RtosTimer.h for more details")
00129     RtosTimer(T *obj, M method, os_timer_type type=osTimerPeriodic) {
00130         constructor(mbed::callback(obj, method), type);
00131     }
00132 
00133     /** Stop the timer.
00134       @return  status code that indicates the execution status of the function:
00135           @a osOK the timer has been stopped.
00136           @a osErrorISR @a stop cannot be called from interrupt service routines.
00137           @a osErrorParameter internal error.
00138           @a osErrorResource the timer is not running.
00139     */
00140     osStatus stop(void);
00141 
00142     /** Start or restart the timer.
00143       @param   millisec  non-zero value of the timer.
00144       @return  status code that indicates the execution status of the function:
00145           @a osOK the timer has been started or restarted.
00146           @a osErrorISR @a start cannot be called from interrupt service routines.
00147           @a osErrorParameter internal error or incorrect parameter value.
00148           @a osErrorResource internal error (the timer is in an invalid timer state).
00149     */
00150     osStatus start(uint32_t millisec);
00151 
00152     ~RtosTimer();
00153 
00154 private:
00155     // Required to share definitions without
00156     // delegated constructors
00157     void constructor(mbed::Callback<void()> func, os_timer_type type);
00158 
00159     osTimerId_t _id;
00160     osTimerAttr_t _attr;
00161     mbed_rtos_storage_timer_t _obj_mem;
00162     mbed::Callback<void()> _function;
00163 };
00164 
00165 }
00166 
00167 #endif
00168 
00169 /** @}*/