-
Notifications
You must be signed in to change notification settings - Fork 0
/
PR_Timers.c
237 lines (210 loc) · 10.6 KB
/
PR_Timers.c
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*******************************************************************************************************************************//**
*
* @file PR_Timers.c
* @brief Maquinaria de timers
* @date 2 de jun. de 2017
* @author Ing. Marcelo Trujillo
*
**********************************************************************************************************************************/
/***********************************************************************************************************************************
*** INCLUDES
**********************************************************************************************************************************/
#include "PR_Timers.h"
/***********************************************************************************************************************************
*** DEFINES PRIVADOS AL MODULO
**********************************************************************************************************************************/
#define DECIMAS 40
#define SEGUNDOS 10
#define MINUTOS 60
/***********************************************************************************************************************************
*** MACROS PRIVADAS AL MODULO
**********************************************************************************************************************************/
/***********************************************************************************************************************************
*** TIPOS DE DATOS PRIVADOS AL MODULO
**********************************************************************************************************************************/
/***********************************************************************************************************************************
*** TABLAS PRIVADAS AL MODULO
**********************************************************************************************************************************/
/***********************************************************************************************************************************
*** VARIABLES GLOBALES PUBLICAS
**********************************************************************************************************************************/
/***********************************************************************************************************************************
*** VARIABLES GLOBALES PRIVADAS AL MODULO
**********************************************************************************************************************************/
static volatile uint8_t TmrBase [ N_TIMERS ] ; // Base de tiempo del evento
static volatile uint32_t TmrRun [ N_TIMERS ] ; // Se lo carga con el valor del tiempo del evento
static volatile uint8_t TmrEvents [ N_TIMERS ] ; // Flag del timer para el evento, el cual, avisa cuando vence el timer
static volatile uint8_t TmrStandBy [ N_TIMERS ] ;
static void ( *TmrHandlers [ N_TIMERS ] )( void ) ;
/***********************************************************************************************************************************
*** PROTOTIPO DE FUNCIONES PRIVADAS AL MODULO
**********************************************************************************************************************************/
/***********************************************************************************************************************************
*** FUNCIONES PRIVADAS AL MODULO
**********************************************************************************************************************************/
/***********************************************************************************************************************************
*** FUNCIONES GLOBALES AL MODULO
**********************************************************************************************************************************/
/**********************************************************************************************************************************
\fn void TimerStart( uint8_t event , uint32_t time , uint8_t base , void (*handler)(void) )
\brief Inicia un timer \details Inicia el timer \a event al transcurrir el tiempo especificado por \a time se llama a la funcion apuntada por \a handler
\param [in] event: Numero de evento entre 0 y 29
\param [in] time: Tiempo del evento. Dependiente de la base de tiempos
\param [in] base: base de tiempo ( DEC, SEG, MIN )
\param [in] handler: Callback del evento
\return void
************************************************************************************************************************************/
void TimerStart( uint8_t event , uint32_t time , uint8_t base , TmrHandler handler )
{
switch ( base )
{
case DEC:
time *= DECIMAS;
break;
case SEG:
time *= ( SEGUNDOS * DECIMAS );
break;
case MIN:
time *= ( MINUTOS * SEGUNDOS * DECIMAS );
break;
}
TmrBase [ event ] = base ; // Lo carga con la base de tiempo del evento dado
if( time != 0 ) // El tiempo no es 0, lo cargo
{
TmrRun [ event ] = time ; // Carga el timer del evento dado
TmrEvents [ event ] = 0 ; // Flag del timer para el evento dado en 0
}
else // El tiempo es 0, el timer vence automáticamente
{
TmrRun [ event ] = 0 ; // Lo carga con el valor cero
TmrEvents [ event ] = 1 ; // Pone en 1 el flag, avisa que termino
}
TmrHandlers [ event ] = handler;
}
/**********************************************************************************************************************************
\fn void TimerSet( uint8_t event , timer_t time )
\brief Inicia un timer
\details Reinicia el timer con el valor `time´ (No lo resetea)
\param [in] event: Numero de evento entre 0 y 29
\param [in] time: Tiempo del evento. Dependiente de la base de tiempos
\return void
************************************************************************************************************************************/
void TimerSet( uint8_t event , uint32_t time )
{
switch ( TmrBase [ event ] )
{
case DEC:
time *= DECIMAS ;
break ;
case SEG:
time *= ( SEGUNDOS * DECIMAS ) ;
break ;
case MIN:
time *= ( MINUTOS * SEGUNDOS * DECIMAS ) ;
break ;
}
TmrRun [ event ] = time;
}
/**********************************************************************************************************************************
\fn TimerGet( uint8_t event )
\brief Toma el valor al vuelo del timer en cuestion
\details Lee el timer
\param [in] event: Numero de evento entre 0 y 29
\return Valor del timer
************************************************************************************************************************************/
uint32_t TimerGet( uint8_t event )
{
uint32_t time = TmrRun [ event ];
switch ( TmrBase [ event ] )
{
case DEC:
time /= DECIMAS;
break;
case SEG:
time /= ( SEGUNDOS * DECIMAS );
break;
case MIN:
time /= ( MINUTOS * SEGUNDOS * DECIMAS );
break;
}
return time;
}
/**********************************************************************************************************************************
\fn TimerStandBy ( uint8_t event , uint8_t accion )
\brief Detiene/Arranca el timer, NO lo resetea
\details Lo pone o lo saca de stand by
\param [in] event: Numero de evento entre 0 y 29
\param [in] accion: RUN lo arranca, STOP lo pone en stand by
\return Valor del timer
************************************************************************************************************************************/
void TimerStandBy( uint8_t event , uint8_t accion )
{
TmrStandBy [ event ] = accion ;
}
/**********************************************************************************************************************************
\fn void Timer Stop ( uint8_t event )
\brief Detiene un timer
\details Detiene el timer event
\param [in] event: Numero de evento entre 0 y 29
\return void
************************************************************************************************************************************/
void TimerStop( uint8_t event )
{
TmrRun [ event ] = 0 ; // Carga el timer con el valor 0
TmrEvents [ event ] = 0 ; // Flag en 0
TmrHandlers [ event ] = NULL ; // No selecciona ninguna tarea
TmrBase [ event ] = 0 ; // Pone la base de tiempo en 0
TmrStandBy [ event ] = RUN ; // No esta en stand by, sino que lo detiene
}
/**********************************************************************************************************************************
\fn void TimerClose ( void )
\brief Detiene todos los timers
\details Detiene todos los timers
\return void
************************************************************************************************************************************/
void TimerClose( void )
{
uint32_t i ;
for ( i = 0 ; i < N_TIMERS ; i++ )
TimerStop ( i ) ;
}
/**********************************************************************************************************************************
\fn void TimerCheck ( void )
\brief Decremento periodico de los contadores
\details Decrementa los contadores de los timers en ejecucion. Debe ser llamada periodicamente con la base de tiempos
\return void
************************************************************************************************************************************/
void TimerCheck( void )
{
uint32_t i ;
for ( i = 0 ; i < N_TIMERS ; i++ )
{
if( TmrRun [ i ] ) // Si esta cargado
{
if ( !TmrStandBy [ i ] ) // Si no esta en stand by
{
TmrRun [ i ]-- ; // Decrementa el contador
if(!TmrRun [ i ] ) // Si llego a 0
TmrEvents [ i ] = 1 ; // Flag en 1, dispara el evento
}
}
}
}
/**********************************************************************************************************************************
\fn void TimerEvent ( void )
\brief Chequeo de timers vencidos
\details Llama a los callbacks de los timers vencidos. Debe llamarse desde el lazo principal del programa
\return void
************************************************************************************************************************************/
void TimerEvent( void )
{
uint8_t i;
for ( i = 0 ; i < N_TIMERS ; i++ )
{
if( TmrEvents[ i ] ) // Si algun timer vencio (Flag en 1)
{
if( TmrHandlers [ i ]!=NULL) TmrHandlers [ i ]( ) ; // Realiza la tarea para dicho evento
TmrEvents [ i ] = 0 ; // Pone en 0 el flag
}
}
}