-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathticker.c
113 lines (97 loc) · 2.96 KB
/
ticker.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
/**
* @file ticker.c
* @author Atakan S.
* @date 01/01/2018
* @version 1.0
* @brief Implements abstraction routines for Ticker usage.
*
* @copyright Copyright (c) 2018 Atakan SARIOGLU ~ www.atakansarioglu.com
*
* 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.
*/
#include "porty.h"
#ifdef _H_TICKER // Exclude from compile when not included in projech.h
#include "ticker.h"
#include "ticker_hw.h"
// Type Definitions.
typedef struct{
uint32_t ms:32; ///< Counts ms.
uint32_t sub:8; ///< Counts sub-ms.
uint32_t ticks:24; ///< Counts every tick.
}tTickerTime;
// Variables.
tTickerTime TickerTime; ///< Holds the time.
/**
* @brief The virtual TickerISR.
*/
void TickerISR(void){
// Tick.
TickerTime.ticks++;
// Increase the sub and the ms counter.
if((++TickerTime.sub) >= TICKER_SUBMS_COUNT){
TickerTime.sub = 0;
TickerTime.ms++;
// Hook for scheduled tasks.
mTickerHook_ms();
}
// Hook for scheduled tasks.
mTickerHook_fast();
}
/**
* @brief Initializes Ticker. After calling this function, enable global interrupts manually.
*/
void TickerInit(void){
uint8_t wasIntEn;
// Save state and disable the interrupts.
wasIntEn = mIsIntEnabled();
mIntDisable();
// HW Init.
TickerHwInit();
// Initialize the counters.
TickerTime.ms = 0;
TickerTime.sub = 0;
TickerTime.ticks = 0;
// Re-enable interrupts.
if(wasIntEn){
mIntEnable();
}
}
/**
* @brief Reads thread-safe time ticks.
* @return 16-bit Ticker count.
*/
uint16_t TickerRead_ticks(void){
uint16_t ticks;
do{
ticks = TickerTime.ticks;
}while(ticks != TickerTime.ticks);
return ticks;
}
/**
* @brief Reads thread-safe time in ms.
* @return 32-bit ms time, only relative-time usage.
*/
uint32_t TickerRead_ms(void){
uint32_t ms;
do{
ms = TickerTime.ms;
}while(ms != TickerTime.ms);
return ms;
}
#endif // Exclude from compile when not included in projech.h