2013-02-18 15:32:11 +00:00
|
|
|
/* mbed Microcontroller Library
|
|
|
|
* Copyright (c) 2006-2012 ARM Limited
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
|
|
|
#ifndef RTOS_TIMER_H
|
|
|
|
#define RTOS_TIMER_H
|
|
|
|
|
|
|
|
#include <stdint.h>
|
2017-05-15 14:55:45 +00:00
|
|
|
#include "cmsis_os2.h"
|
|
|
|
#include "rtx_lib.h"
|
2016-10-01 07:11:36 +00:00
|
|
|
#include "platform/Callback.h"
|
2017-06-20 12:00:20 +00:00
|
|
|
#include "platform/NonCopyable.h"
|
2017-01-27 11:10:28 +00:00
|
|
|
#include "platform/mbed_toolchain.h"
|
2017-05-15 14:55:45 +00:00
|
|
|
#include "mbed_rtos1_types.h"
|
2013-02-18 15:32:11 +00:00
|
|
|
|
|
|
|
namespace rtos {
|
2016-10-04 20:02:44 +00:00
|
|
|
/** \addtogroup rtos */
|
|
|
|
/** @{*/
|
2013-02-18 15:32:11 +00:00
|
|
|
|
|
|
|
/** The RtosTimer class allow creating and and controlling of timer functions in the system.
|
|
|
|
A timer function is called when a time period expires whereby both on-shot and
|
|
|
|
periodic timers are possible. A timer can be started, restarted, or stopped.
|
|
|
|
|
|
|
|
Timers are handled in the thread osTimerThread.
|
2014-05-29 13:36:51 +00:00
|
|
|
Callback functions run under control of this thread and may use CMSIS-RTOS API calls.
|
2016-11-07 18:51:52 +00:00
|
|
|
|
|
|
|
@deprecated
|
|
|
|
The RtosTimer has been superseded by the EventQueue. The RtosTimer and EventQueue duplicate
|
|
|
|
the functionality of timing events outside of interrupt context, however the EventQueue
|
|
|
|
has additional features to handle deferring other events to multiple contexts.
|
|
|
|
|
|
|
|
For an example, the following code shows a simple use of the RtosTimer:
|
|
|
|
@code
|
|
|
|
DigitalOut led(LED1);
|
|
|
|
void blink() {
|
|
|
|
led = !led;
|
|
|
|
}
|
|
|
|
|
|
|
|
RtosTimer timer(&blink);
|
|
|
|
int main() {
|
|
|
|
timer.start(1000); // call blink every 1s
|
|
|
|
wait_ms(5000);
|
|
|
|
timer.stop(); // stop after 5s
|
|
|
|
}
|
|
|
|
@endcode
|
|
|
|
|
|
|
|
This is the above example rewritten to use the EventQueue:
|
|
|
|
@code
|
|
|
|
DigitalOut led(LED1);
|
|
|
|
void blink() {
|
|
|
|
led = !led;
|
|
|
|
}
|
|
|
|
|
|
|
|
EventQueue queue(4*EVENTS_EVENT_SIZE);
|
|
|
|
int main() {
|
|
|
|
int blink_id = queue.call_every(1000, &blink); // call blink every 1s
|
|
|
|
queue.dispatch(5000);
|
|
|
|
queue.cancel(blink_id); // stop after 5s
|
|
|
|
}
|
|
|
|
@endcode
|
2017-05-15 14:55:45 +00:00
|
|
|
|
|
|
|
@note
|
|
|
|
Memory considerations: The timer control structures will be created on current thread's stack, both for the mbed OS
|
|
|
|
and underlying RTOS objects (static or dynamic RTOS memory pools are not being used).
|
2013-02-18 15:32:11 +00:00
|
|
|
*/
|
2017-06-20 12:00:20 +00:00
|
|
|
class RtosTimer : private mbed::NonCopyable<RtosTimer> {
|
2013-02-18 15:32:11 +00:00
|
|
|
public:
|
2016-07-20 22:07:12 +00:00
|
|
|
/** Create timer.
|
|
|
|
@param func function to be executed by this timer.
|
|
|
|
@param type osTimerOnce for one-shot or osTimerPeriodic for periodic behaviour. (default: osTimerPeriodic)
|
|
|
|
@param argument argument to the timer call back function. (default: NULL)
|
|
|
|
@deprecated Replaced with RtosTimer(Callback<void()>, os_timer_type)
|
2016-11-07 18:51:52 +00:00
|
|
|
@deprecated
|
|
|
|
The RtosTimer has been superseded by the EventQueue. See RtosTimer.h for more details
|
2016-07-20 22:07:12 +00:00
|
|
|
*/
|
2016-08-17 01:30:20 +00:00
|
|
|
MBED_DEPRECATED_SINCE("mbed-os-5.1",
|
2016-08-08 22:12:48 +00:00
|
|
|
"Replaced with RtosTimer(Callback<void()>, os_timer_type)")
|
2016-11-07 18:51:52 +00:00
|
|
|
MBED_DEPRECATED_SINCE("mbed-os-5.2",
|
|
|
|
"The RtosTimer has been superseded by the EventQueue. See RtosTimer.h for more details")
|
2016-07-20 22:07:12 +00:00
|
|
|
RtosTimer(void (*func)(void const *argument), os_timer_type type=osTimerPeriodic, void *argument=NULL) {
|
2016-09-23 08:29:14 +00:00
|
|
|
constructor(mbed::callback((void (*)(void *))func, argument), type);
|
2016-07-20 22:07:12 +00:00
|
|
|
}
|
|
|
|
|
2016-07-20 16:51:45 +00:00
|
|
|
/** Create timer.
|
|
|
|
@param func function to be executed by this timer.
|
2013-02-18 15:32:11 +00:00
|
|
|
@param type osTimerOnce for one-shot or osTimerPeriodic for periodic behaviour. (default: osTimerPeriodic)
|
2016-11-07 18:51:52 +00:00
|
|
|
@deprecated
|
|
|
|
The RtosTimer has been superseded by the EventQueue. See RtosTimer.h for more details
|
2013-02-18 15:32:11 +00:00
|
|
|
*/
|
2016-11-07 18:51:52 +00:00
|
|
|
MBED_DEPRECATED_SINCE("mbed-os-5.2",
|
|
|
|
"The RtosTimer has been superseded by the EventQueue. See RtosTimer.h for more details")
|
2016-07-20 16:51:45 +00:00
|
|
|
RtosTimer(mbed::Callback<void()> func, os_timer_type type=osTimerPeriodic) {
|
|
|
|
constructor(func, type);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Create timer.
|
|
|
|
@param obj pointer to the object to call the member function on.
|
|
|
|
@param method member function to be executed by this timer.
|
|
|
|
@param type osTimerOnce for one-shot or osTimerPeriodic for periodic behaviour. (default: osTimerPeriodic)
|
Added support for cv-qualifiers in Callback class
Additionally, the following changes were don to avoid combinatorial
explosion in function overloads as a result of adding cv-qualifiers:
- Added convenience function for inferred type
- Deprecated callback overloads qhere cv-qualifiers are not scalable
Supported overloads:
callback(void (*f)(A...));
callback(const Callback<R(A...)> &);
callback(T *t, void (*f)(T*, A...));
callback(const T *t, void (*f)(const T*, A...));
callback(volatile T *t, void (*f)(volatile T*, A...));
callback(const volatile T *t, void (*f)(const volatile T*, A...));
callback(T *t, void (T::*f)(A...));
callback(const T *t, void (T::*f)(A...) const);
callback(volatile T *t, void (T::*f)(A...) volatile);
callback(const volatile T *t, void (T::*f)(A...) const volatile);
2016-08-18 18:28:57 +00:00
|
|
|
@deprecated
|
|
|
|
The RtosTimer constructor does not support cv-qualifiers. Replaced by
|
|
|
|
RtosTimer(callback(obj, method), os_timer_type).
|
2016-11-07 18:51:52 +00:00
|
|
|
@deprecated
|
|
|
|
The RtosTimer has been superseded by the EventQueue. See RtosTimer.h for more details
|
2016-07-20 16:51:45 +00:00
|
|
|
*/
|
|
|
|
template <typename T, typename M>
|
Added support for cv-qualifiers in Callback class
Additionally, the following changes were don to avoid combinatorial
explosion in function overloads as a result of adding cv-qualifiers:
- Added convenience function for inferred type
- Deprecated callback overloads qhere cv-qualifiers are not scalable
Supported overloads:
callback(void (*f)(A...));
callback(const Callback<R(A...)> &);
callback(T *t, void (*f)(T*, A...));
callback(const T *t, void (*f)(const T*, A...));
callback(volatile T *t, void (*f)(volatile T*, A...));
callback(const volatile T *t, void (*f)(const volatile T*, A...));
callback(T *t, void (T::*f)(A...));
callback(const T *t, void (T::*f)(A...) const);
callback(volatile T *t, void (T::*f)(A...) volatile);
callback(const volatile T *t, void (T::*f)(A...) const volatile);
2016-08-18 18:28:57 +00:00
|
|
|
MBED_DEPRECATED_SINCE("mbed-os-5.1",
|
|
|
|
"The RtosTimer constructor does not support cv-qualifiers. Replaced by "
|
|
|
|
"RtosTimer(callback(obj, method), os_timer_type).")
|
2016-11-07 18:51:52 +00:00
|
|
|
MBED_DEPRECATED_SINCE("mbed-os-5.2",
|
|
|
|
"The RtosTimer has been superseded by the EventQueue. See RtosTimer.h for more details")
|
2016-07-20 16:51:45 +00:00
|
|
|
RtosTimer(T *obj, M method, os_timer_type type=osTimerPeriodic) {
|
Added support for cv-qualifiers in Callback class
Additionally, the following changes were don to avoid combinatorial
explosion in function overloads as a result of adding cv-qualifiers:
- Added convenience function for inferred type
- Deprecated callback overloads qhere cv-qualifiers are not scalable
Supported overloads:
callback(void (*f)(A...));
callback(const Callback<R(A...)> &);
callback(T *t, void (*f)(T*, A...));
callback(const T *t, void (*f)(const T*, A...));
callback(volatile T *t, void (*f)(volatile T*, A...));
callback(const volatile T *t, void (*f)(const volatile T*, A...));
callback(T *t, void (T::*f)(A...));
callback(const T *t, void (T::*f)(A...) const);
callback(volatile T *t, void (T::*f)(A...) volatile);
callback(const volatile T *t, void (T::*f)(A...) const volatile);
2016-08-18 18:28:57 +00:00
|
|
|
constructor(mbed::callback(obj, method), type);
|
2016-07-20 16:51:45 +00:00
|
|
|
}
|
2014-05-29 13:36:51 +00:00
|
|
|
|
2013-02-18 15:32:11 +00:00
|
|
|
/** Stop the timer.
|
2014-05-29 13:36:51 +00:00
|
|
|
@return status code that indicates the execution status of the function.
|
2013-02-18 15:32:11 +00:00
|
|
|
*/
|
|
|
|
osStatus stop(void);
|
2014-05-29 13:36:51 +00:00
|
|
|
|
2016-07-20 16:51:45 +00:00
|
|
|
/** Start the timer.
|
2013-02-18 15:32:11 +00:00
|
|
|
@param millisec time delay value of the timer.
|
2014-05-29 13:36:51 +00:00
|
|
|
@return status code that indicates the execution status of the function.
|
2013-02-18 15:32:11 +00:00
|
|
|
*/
|
|
|
|
osStatus start(uint32_t millisec);
|
2014-05-29 13:36:51 +00:00
|
|
|
|
2013-02-18 15:32:11 +00:00
|
|
|
~RtosTimer();
|
|
|
|
|
|
|
|
private:
|
2016-07-20 16:51:45 +00:00
|
|
|
// Required to share definitions without
|
|
|
|
// delegated constructors
|
|
|
|
void constructor(mbed::Callback<void()> func, os_timer_type type);
|
2017-05-15 14:55:45 +00:00
|
|
|
|
|
|
|
osTimerId_t _id;
|
|
|
|
osTimerAttr_t _attr;
|
|
|
|
os_timer_t _obj_mem;
|
2016-07-20 16:51:45 +00:00
|
|
|
mbed::Callback<void()> _function;
|
2013-02-18 15:32:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2016-10-04 20:02:44 +00:00
|
|
|
|
|
|
|
/** @}*/
|