-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw_timer.c
352 lines (303 loc) · 10.7 KB
/
sw_timer.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/******************************************************************************
@Company:
Microchip Technology Inc.
@File Name:
sw_timer.c
@Summary:
This is the SW Timer source file which provides SW Timer service to the LoRa
@Description:
This source file provides SW Timer service implementation.
Copyright (c) 2013 - 2016 released Microchip Technology Inc. All rights reserved.
Microchip licenses to you the right to use, modify, copy and distribute
Software only when embedded on a Microchip microcontroller or digital signal
controller that is integrated into your product or third party product
(pursuant to the sublicense terms in the accompanying license agreement).
You should refer to the license agreement accompanying this Software for
additional information regarding your rights and obligations.
SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF
MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
IN NO EVENT SHALL MICROCHIP OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER
CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR
OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE OR
CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT OF
SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
(INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
*************************************************************************
* sw_timer.c
*
* SW Timer source file
*
******************************************************************************/
#include <stddef.h>
#include <xc.h>
#include "sw_timer.h"
#include "tmr_lora_addons.h"
#include "mcc_lora_config.h"
#include "mcc_generated_files/interrupt_manager.h"
#include "mcc_generated_files/mcc.h"
#include "shell.h"
#define TIMER_CORRECTION_TICKS 32
typedef struct
{
uint32_t ticksRemaining;
uint8_t running;
uint8_t callbackParameter;
void (*callback)(uint8_t);
} SwTimer_t;
volatile static SwTimer_t swTimers[MAX_TIMERS];
volatile static uint8_t allocatedTimers;
volatile static uint32_t ticksAccounted;
// This function needs to be called with interrupts stopped, shortly after
// GetDeltaTime (i.e. when ticksPassed ~= 0)
//
// update timer with new value so hw interrupt occur after ticksRemaining
// if ticksToScheduledInterrupt is greater, then ticksRemaining
void TMR_OverrideRemaining(uint32_t ticksRemaining)
{
uint16_t tmrVal;
uint16_t oldReloadVal;
// Make sure we don't mess with the timer too soon before it expires
if (ticksToScheduledInterrupt > 10)
{
if (ticksRemaining < ticksToScheduledInterrupt)
{
ticksToScheduledInterrupt = ticksRemaining;
oldReloadVal = reloadVal;
// Refresh timer value
reloadVal = HW_MAX_TIMER_VAL - ticksRemaining;
tmrVal = TMR_SwapTimer(reloadVal);
ticksAdded += tmrVal - oldReloadVal;
//PIR1bits.TMR1IF = 0;
}
}
}
// This function needs to be called with interrupts stopped
uint32_t TMR_GetDeltaTime(void)
{
static uint32_t retVal;
static uint16_t tmrVal;
tmrVal = TMR_ReadTimer();
if (timerOverflow == 1)
{
timerOverflow = 0;
retVal = HW_MAX_TIMER_VAL - reloadVal + tmrVal - ticksAccounted + ticksAdded;
ticksAccounted = tmrVal;
ticksAdded = 0;
reloadVal = 0;
}
else
{
retVal = tmrVal - reloadVal - ticksAccounted + ticksAdded;
ticksAccounted += retVal;
}
return retVal;
}
uint16_t TMR_SwapTimer(uint16_t timerVal)
{
static uint16_t oldVal;
static uint16_t retVal;
oldVal = TMR1_ReadTimer();
// Block until timer increments. This way during the following ~120
// instructions we won't run the risk of having the timer change again and
// we can safely update its value with something new.
while (oldVal == TMR1_ReadTimer())
;
TMR1_WriteTimer(timerVal);
// Correct output since we've let the timer increment after reading it
oldVal++;
retVal = oldVal;
return retVal;
}
void SystemTimerInit(void)
{
allocatedTimers = 0;
}
// This function should allow interrupts to happen (unless it was called from
// an interrupt itself) and keep its timing accurate. Ideally it should do the
// waiting with the MCU in sleep.
// Find out how long it takes the MCU to go to and wake up from sleep to see if
// it makes sense to go to sleep at all.
void SystemBlockingWaitMs(uint32_t ms)
{
while (ms > 0)
{
__delay_ms(1);
ms--;
}
}
uint8_t SwTimerCreate(void)
{
uint8_t retVal;
if (allocatedTimers < MAX_TIMERS)
{
INTERRUPT_GlobalInterruptDisable();
retVal = allocatedTimers;
swTimers[retVal].running = 0;
swTimers[retVal].ticksRemaining = 0;
swTimers[retVal].callbackParameter = 0;
swTimers[retVal].callback = NULL;
allocatedTimers++;
INTERRUPT_GlobalInterruptEnable();
}
else
{
send_chars("Allocated > MAX_TIMERS\r\n");
while(1)
{
// If you reach this spot it means the MAX_TIMERS is #defined to a
// lower value than the number of timers that have been
// SwTimerCreate()d
}
}
return retVal;
}
void SwTimerSetCallback(uint8_t timerId, void (*callback)(uint8_t), uint8_t callbackParameter)
{
swTimers[timerId].callback = callback;
swTimers[timerId].callbackParameter = callbackParameter;
}
void SwTimerSetTimeout(uint8_t timerId, uint32_t timeout)
{
INTERRUPT_GlobalInterruptDisable();
swTimers[timerId].ticksRemaining = timeout - TIMER_CORRECTION_TICKS;
INTERRUPT_GlobalInterruptEnable();
}
uint32_t SwTimerReadValue(uint8_t timerId)
{
uint32_t ticksCount;
INTERRUPT_GlobalInterruptDisable();
// Calling this function synchronizes all timers to the current moment, i.e.
// updates the elapsed times on all timers
SwTimersInterrupt();
ticksCount = swTimers[timerId].ticksRemaining;
INTERRUPT_GlobalInterruptEnable();
return ticksCount;
}
uint8_t SwTimerIsRunning(uint8_t timerId)
{
uint8_t isRunning;
INTERRUPT_GlobalInterruptDisable();
isRunning = swTimers[timerId].running;
INTERRUPT_GlobalInterruptEnable();
return isRunning;
}
void SwTimerStart(uint8_t timerId)
{
uint32_t ticksRemaining;
// Need to synchronize all timers
INTERRUPT_GlobalInterruptDisable();
ticksRemaining = SwTimersInterrupt();
if (swTimers[timerId].ticksRemaining < ticksRemaining)
{
ticksRemaining = swTimers[timerId].ticksRemaining;
}
TMR_OverrideRemaining(ticksRemaining);
swTimers[timerId].running = 1;
INTERRUPT_GlobalInterruptEnable();
}
void SwTimerStartNew(uint8_t newtimerId,uint8_t oldtimerId, uint32_t delta)
{
uint32_t ticksRemaining;
// Need to synchronize all timers
INTERRUPT_GlobalInterruptDisable();
ticksRemaining = SwTimersInterrupt();
swTimers[oldtimerId].running = 0;
swTimers[newtimerId].ticksRemaining=swTimers[oldtimerId].ticksRemaining-delta;
printVar("oldTimer ticksRemaining=",PAR_UI32,&swTimers[oldtimerId].ticksRemaining,false,true);
printVar("newTimer ticksRemaining=",PAR_UI32,&swTimers[newtimerId].ticksRemaining,false,true);
if (swTimers[newtimerId].ticksRemaining < ticksRemaining)
{
ticksRemaining = swTimers[newtimerId].ticksRemaining;
}
TMR_OverrideRemaining(ticksRemaining);
swTimers[newtimerId].running = 1;
INTERRUPT_GlobalInterruptEnable();
}
void SwTimerStop(uint8_t timerId)
{
INTERRUPT_GlobalInterruptDisable();
swTimers[timerId].running = 0;
INTERRUPT_GlobalInterruptEnable();
}
// This function needs to be called with interrupts disabled. Returns 1 if MCU
// may go to sleep and 0 if it may not.
uint8_t SwTimersCanSleep(void)
{
uint8_t i;
// Calling this function synchronizes all timers to the current moment, i.e.
// updates the elapsed times on all timers
SwTimersInterrupt();
for (i = 0; i < allocatedTimers; i++)
{
if ((swTimers[i].running == 1) && (0 == swTimers[i].ticksRemaining))
{
return 0;
}
}
return 1;
}
void SwTimersExecute(void)
{
uint8_t i;
INTERRUPT_GlobalInterruptDisable();
for (i = 0; i < allocatedTimers; i++)
{
// Subtract time elapsed from all running timers and if any hits 0
// execute its callback (if not null)
if (swTimers[i].running == 1)
{
if (0 == swTimers[i].ticksRemaining)
{
swTimers[i].running = 0;
if (NULL != swTimers[i].callback)
{
INTERRUPT_GlobalInterruptEnable();
swTimers[i].callback(swTimers[i].callbackParameter);
INTERRUPT_GlobalInterruptDisable();
}
}
}
}
INTERRUPT_GlobalInterruptEnable();
}
// Function called from ISR AND mainline code
// proceed corrections of ticksRemainig of all active timers after last call of this function
// and return minimun of ticksRemainig
//
uint32_t SwTimersInterrupt(void)
{
uint8_t i;
uint32_t timeElapsed;
uint32_t ticksToNextTimerEvent = 0xFFFFFFFF;
// Find out how much time has passed since the last execution of this
// function.
timeElapsed = TMR_GetDeltaTime();
for (i = 0; i < allocatedTimers; i++)
{
// Subtract time elapsed from all running timers and if any hits 0
// register its callback for execution (msRemaining = 0)
if (swTimers[i].running == 1)
{
if (swTimers[i].ticksRemaining <= timeElapsed)
{
swTimers[i].ticksRemaining = 0;
}
else
{
swTimers[i].ticksRemaining -= timeElapsed;
// Tell TMR1 subsystem to schedule the next interrupt at the
// minimum timeout value of all timers
if (swTimers[i].ticksRemaining < ticksToNextTimerEvent)
{
ticksToNextTimerEvent = swTimers[i].ticksRemaining;
}
}
}
}
return ticksToNextTimerEvent;
}
/**
End of File
*/