-
Notifications
You must be signed in to change notification settings - Fork 0
/
RtosTimer.cpp
114 lines (96 loc) · 3.02 KB
/
RtosTimer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
* RtosTimer.cpp
*
* Created on: Dic 2017
* Author: raulMrello
*
*/
#include "RtosTimer.h"
#include "Mutex.h"
//------------------------------------------------------------------------------------
//--- PRIVATE TYPES ------------------------------------------------------------------
//------------------------------------------------------------------------------------
static const char* _MODULE_ = "[RtosTimer].....";
#define _EXPR_ (_defdbg && !IS_ISR())
static Mutex _mtx;
static void defaultCallback(){
}
static void vCallbackFunction(TimerHandle_t arg){
_mtx.lock();
RtosTimer* tmr = (RtosTimer*)pvTimerGetTimerID(arg);
if(tmr){
tmr->doCallback();
}
_mtx.unlock();
}
//------------------------------------------------------------------------------------
//-- PUBLIC METHODS IMPLEMENTATION ---------------------------------------------------
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
RtosTimer::RtosTimer(Callback<void()> func, os_timer_type type, const char* name, bool defdbg) : _name(name), _defdbg(defdbg) {
DEBUG_TRACE_D(_EXPR_, _MODULE_, "Creando RtosTimer <%s>...", _name);
_id = 0;
_function = func;
_type = type;
}
//------------------------------------------------------------------------------------
osStatus RtosTimer::start(uint32_t millisec) {
if(_id==0){
DEBUG_TRACE_D(_EXPR_, _MODULE_, "Creando xTimerCreate para RtosTimer <%s>", _name);
if((_id = xTimerCreate(_name, MBED_MILLIS_TO_TICK(millisec), _type, (void*)this, vCallbackFunction)) == 0){
DEBUG_TRACE_E(_EXPR_, _MODULE_, "ERROR xTimerCreate en RtosTimer <%s>", _name);
return osError;
}
if(IS_ISR()){
if(xTimerStartFromISR(_id, NULL) == pdPASS){
return osOK;
}
}
else{
if(xTimerStart(_id, 0) == pdPASS){
return osOK;
}
}
DEBUG_TRACE_E(_EXPR_, _MODULE_, "ERROR al iniciar RtosTimer <%s>", _name);
return osError;
}
DEBUG_TRACE_D(_EXPR_, _MODULE_, "Reiniciando RtosTimer <%s>", _name);
// lo reinicia cambiando el periodo
if(IS_ISR()){
if(xTimerChangePeriodFromISR(_id, MBED_MILLIS_TO_TICK(millisec), NULL) == pdPASS){
return osOK;
}
}
else{
if(xTimerChangePeriod(_id, MBED_MILLIS_TO_TICK(millisec), 0) == pdPASS){
return osOK;
}
}
DEBUG_TRACE_E(_EXPR_, _MODULE_, "ERROR al iniciar RtosTimer <%s>", _name);
return osError;
}
//------------------------------------------------------------------------------------
osStatus RtosTimer::stop(void) {
if(xTimerIsTimerActive(_id) == pdFALSE){
return osOK;
}
if(IS_ISR()){
if(xTimerStopFromISR(_id, NULL) == pdPASS){
return osOK;
}
return osError;
}
DEBUG_TRACE_D(_EXPR_, _MODULE_, "Deteniendo RtosTimer <%s>", _name);
if(xTimerStop(_id, 0) == pdPASS){
return osOK;
}
return osError;
}
//------------------------------------------------------------------------------------
RtosTimer::~RtosTimer() {
_mtx.lock();
_function = callback(defaultCallback);
xTimerDelete(_id, 0);
_id = 0;
_mtx.unlock();
}