-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw_timer.ino
188 lines (164 loc) · 3.13 KB
/
sw_timer.ino
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
uint32_t channels;
uint32_t sw_tmr_enabled;
uint32_t sw_tmr_running;
void *sw_timer_callback[MAX_SW_TIMER_CH];
uint32_t sw_tmr_timeout[MAX_SW_TIMER_CH];
uint32_t sw_tmr_pause[MAX_SW_TIMER_CH];
void sw_timer_init()
{
uint32_t i;
timer_initstruct_t timer_InitStruct;
cli();//stop interrupts
timer_InitStruct.timer = TIMER_4;
timer_InitStruct.period = 10; //[Hz]
timer_InitStruct.callback = &sw_timer_callback_from_isr;
timer_init(&timer_InitStruct);
channels = 0;
sw_tmr_enabled = 0;
sw_tmr_running = 0;
sw_timer_reset_channels();
for(i = 0; i < MAX_SW_TIMER_CH; i++)
{
sw_timer_callback[i] = NULL;
sw_tmr_timeout[i] = 0;
}
sei();//allow interrupts
}
void sw_timer_disable()
{
uint32_t i;
for(i = 0; i < MAX_SW_TIMER_CH; i++)
{
if( getbit(&channels, i) != 0)
{
clrbit(&sw_tmr_enabled, i);
}
}
}
void sw_timer_enable()
{
uint32_t i;
for(i = 0; i < MAX_SW_TIMER_CH; i++)
{
if( getbit(&sw_tmr_running, i) != 0)
{
setbit(&sw_tmr_enabled, i);
}
}
}
int32_t sw_timer_add_channel(uint32_t ch, uint32_t timeout, void (*callback)(void))
{
if(ch > (MAX_SW_TIMER_CH - 1) )
{
return -1;
}
if( getbit(&channels, ch) != 0)
{
return -2;
}
sw_tmr_timeout[ch] = timeout / 100;
sw_timer_callback[ch] = callback;
setbit(&channels, ch);
return 0;
}
int32_t sw_timer_enable_channel(uint32_t ch)
{
if(ch > (MAX_SW_TIMER_CH - 1) )
{
return -1;
}
if( getbit(&channels, ch) == 0)
{
return -2;
}
setbit(&sw_tmr_enabled, ch);
sw_tmr_running = sw_tmr_enabled;
sw_timer_cntr[ch] = sw_tmr_timeout[ch];
}
int32_t sw_timer_disable_channel(uint32_t ch)
{
if(ch > (MAX_SW_TIMER_CH - 1) )
{
return -1;
}
if( getbit(&channels, ch) == 0)
{
return -2;
}
clrbit(&sw_tmr_enabled, ch);
sw_tmr_running = sw_tmr_enabled;
sw_timer_cntr[ch] = 0;
}
bool sw_timer_is_channel_enabled(uint32_t ch)
{
if(ch > (MAX_SW_TIMER_CH - 1) )
{
return false;
}
if( getbit(&channels, ch) == 0)
{
return false;
}
if( getbit(&sw_tmr_enabled, ch) == 0)
{
return false;
}
return true;
}
void sw_timer_reset_channels()
{
uint32_t i;
for(i = 0; i < MAX_SW_TIMER_CH; i++)
{
sw_timer_cntr[i] = 0;
}
}
int32_t sw_timer_set_cntr(uint32_t ch, uint32_t cntr)
{
if(ch > (MAX_SW_TIMER_CH - 1) )
{
return -1;
}
if( getbit(&channels, ch) == 0)
{
return -2;
}
sw_timer_cntr[ch] = cntr;
return 0;
}
void sw_timer_callback_from_isr()
{
uint32_t i;
void (*callback)(void);
for( i = 0; i < MAX_SW_TIMER_CH; i++)
{
if( sw_timer_cntr[i] != 0)
{
if( getbit(&sw_tmr_enabled, i) != 0)
{
sw_timer_cntr[i]--;
if( sw_timer_cntr[i] == 0)
{
sw_timer_cntr[i] = sw_tmr_timeout[i];
callback = sw_timer_callback[i];
if( callback != NULL)
{
callback();
}
}
}
}
}
}
void setbit(uint32_t* const reg, uint32_t bit_)
{
*reg |= (1 << bit_);
}
void clrbit(uint32_t* const reg, uint32_t bit_)
{
*reg &= ~(1 << bit_);
}
uint32_t getbit(uint32_t* reg, uint32_t bit_)
{
return *reg & (1 << bit_);
}