-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystem_timer.c
429 lines (365 loc) · 14.2 KB
/
system_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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/*
* Copyright (C) 2022 (Unix69@github)
*
* 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 "types.h"
struct sys_timer {
uint32_t st_id;
uint8_t st_owner;
uint8_t st_free;
uint8_t st_en;
uint8_t st_running;
volatile unsigned long int* st_cmp;
volatile unsigned long int* st_cnt_lo;
volatile unsigned long int* st_cnt_hi;
volatile unsigned long int* st_stat;
void *st_irq_handler;
void *st_fiq_handler;
void *st_err_handler;
void *st_sync_handler;
};
#include "system_timer.h"
//Defines
#define N_SYSTEM_TIMERS 4
//Identify system timer status match
#define ST_MATCH 1
//Identify system timer status no match
#define ST_NOMATCH 0
//Identify system timer status clear
#define ST_STATUS_CLEAR 1
//Identify system timer status set
#define ST_STATUS_SET 1
//Identify system timer status set
#define ST_STATUS_RESET 0
//Identify system timer free
#define ST_FREE 1
//Identify system timer not free
#define ST_BUSY 0
//Identify system timer without owner
#define ST_NO_OWNER 0
//Identify system timer without id
#define ST_NO_ID 0
//Identify system timer free
#define ST_ENABLED 1
//Identify system timer not free
#define ST_DISABLED 0
//Identify system timer running status
#define ST_RUNNING 1
//Identify system timer stopping status
#define ST_STOPPED 0
//Identify system timer reset compare value
#define ST_CMP_RESET 0
//System Timer (ST) Macros
#define ST_IS_VALID_TIMER_COMPARE(ST_CMP) ST_CMP > 0 && ST_CMP < (2 << 32)
#define ST_IS_TIMER(ST_NR) ST_NR == SYSTEM_TIMER_0 || ST_NR == SYSTEM_TIMER_1 || ST_NR == SYSTEM_TIMER_2 || ST_NR == SYSTEM_TIMER_3
#define ST_IS_TIMER_EXPIRED(ST_CNT, ST_CMP) ST_CNT >= ST_CMP
#define ST_SELECT_TIMER_STATUS(ST_STATUS, ST_NR) ST_STATUS & (1 << ST_NR)
#define ST_IS_TIMER_MATCH(ST_STATUS, ST_NR) ((ST_STATUS & (1 << ST_NR)) >> ST_NR) == ST_MATCH
#define SYSTEM_TIMER_CS ((volatile unsigned long int*)(MMIO_BASE+0x3000)) //System Timer Control/Status 32 bit
#define SYSTEM_TIMER_LO ((volatile unsigned long int*)(MMIO_BASE+0x3004)) //System Timer Counter Lower 32 bit
#define SYSTEM_TIMER_HI ((volatile unsigned long int*)(MMIO_BASE+0x3008)) //System Timer Counter Higher 32 bit
#define SYSTEM_TIMER_C0 ((volatile unsigned long int*)(MMIO_BASE+0x300C)) //System Timer Compare 0 32 bit
#define SYSTEM_TIMER_C1 ((volatile unsigned long int*)(MMIO_BASE+0x3010)) //System Timer Compare 1 32 bit
#define SYSTEM_TIMER_C2 ((volatile unsigned long int*)(MMIO_BASE+0x3014)) //System Timer Compare 2 32 bit
#define SYSTEM_TIMER_C3 ((volatile unsigned long int*)(MMIO_BASE+0x3018)) //System Timer Compare 3 32 bit
sys_timer_t sys_timers[N_SYSTEM_TIMERS];
//BrainOS System Timer internal routines
void sys_timer_boot(){
for (register uint8_t i = 0; i < N_SYSTEM_TIMERS; i++)
{
sys_timers[i].st_en = ST_DISABLED;
sys_timers[i].st_running = ST_STOPPED;
sys_timers[i].st_free = ST_FREE;
sys_timers[i].st_owner = ST_NO_OWNER;
sys_timers[i].st_id = ST_NO_ID;
sys_timers[i].st_cmp = SYSTEM_TIMER_C0 + (i * 4);
sys_timers[i].st_cnt_hi = SYSTEM_TIMER_HI;
sys_timers[i].st_cnt_lo = SYSTEM_TIMER_LO;
sys_timers[i].st_stat = SYSTEM_TIMER_CS;
sys_timers[i].st_irq_handler = NULL;
sys_timers[i].st_fiq_handler = NULL;
sys_timers[i].st_err_handler = NULL;
sys_timers[i].st_sync_handler = NULL;
sys_timer_stat_clear(i);
sys_timer_cmp_reset(i);
}
}
void sys_timer_shutdown(){
for (register uint8_t i = 0; i < N_SYSTEM_TIMERS; i++)
{
sys_timers[i].st_en = ST_DISABLED;
sys_timers[i].st_running = ST_STOPPED;
sys_timers[i].st_free = ST_FREE;
sys_timers[i].st_owner = ST_NO_OWNER;
sys_timers[i].st_id = ST_NO_ID;
sys_timers[i].st_cmp = NULL;
sys_timers[i].st_cnt_hi = NULL;
sys_timers[i].st_cnt_lo = NULL;
sys_timers[i].st_stat = NULL;
sys_timers[i].st_irq_handler = NULL;
sys_timers[i].st_fiq_handler = NULL;
sys_timers[i].st_err_handler = NULL;
sys_timers[i].st_sync_handler = NULL;
sys_timer_stat_clear(i);
sys_timer_cmp_reset(i);
sys_timers[i].st_running = ST_STOPPED;
}
}
//BrainOS System Timer control level API
void sys_timer_acquire(register uint32_t sys_timer, register uint8_t sys_timer_owner, void* sys_timer_irq_handler, void* sys_timer_fiq_handler, void* sys_timer_err_handler, void* sys_timer_sync_handler){
if(ST_IS_TIMER(sys_timer)){
if(sys_timers[sys_timer].st_owner == ST_NO_OWNER){
sys_timers[sys_timer].st_en = ST_ENABLED;
sys_timers[sys_timer].st_free = ST_BUSY;
sys_timers[sys_timer].st_owner = sys_timer_owner;
sys_timers[sys_timer].st_id = sys_timer;
sys_timers[sys_timer].st_irq_handler = sys_timer_irq_handler;
sys_timers[sys_timer].st_fiq_handler = sys_timer_fiq_handler;
sys_timers[sys_timer].st_err_handler = sys_timer_err_handler;
sys_timers[sys_timer].st_sync_handler = sys_timer_sync_handler;
sys_timer_stat_clear(sys_timer);
}
}
}
void sys_timer_start(register uint32_t sys_timer, register uint8_t sys_timer_owner, uint32_t sys_timer_cmp_val){
if(ST_IS_TIMER(sys_timer)){
if(sys_timers[sys_timer].st_owner == sys_timer_owner && sys_timers[sys_timer].st_en){
sys_timer_stat_clear(sys_timer);
if(ST_IS_VALID_TIMER_COMPARE(sys_timer_cmp_val)){
sys_timer_cmp_set(sys_timer, sys_timer_cmp_val);
}
sys_timers[sys_timer].st_running = ST_RUNNING;
}
}
}
void sys_timer_stop(register uint32_t sys_timer, register uint8_t sys_timer_owner){
if(ST_IS_TIMER(sys_timer)){
if(sys_timers[sys_timer].st_owner == sys_timer_owner && sys_timers[sys_timer].st_en){
sys_timer_cmp_reset(sys_timer);
sys_timer_stat_clear(sys_timer);
sys_timers[sys_timer].st_running = ST_STOPPED;
}
}
}
void sys_timer_restart(register uint32_t sys_timer, register uint8_t sys_timer_owner){
if(ST_IS_TIMER(sys_timer)){
if(sys_timers[sys_timer].st_owner == sys_timer_owner && sys_timers[sys_timer].st_en){
sys_timer_stat_clear(sys_timer);
}
}
}
void sys_timer_release(register uint32_t sys_timer, register uint8_t sys_timer_owner){
if(ST_IS_TIMER(sys_timer)){
if(sys_timers[sys_timer].st_owner == sys_timer_owner){
sys_timer_stat_clear(sys_timer);
sys_timer_cmp_reset(sys_timer);
sys_timers[sys_timer].st_en = ST_DISABLED;
sys_timers[sys_timer].st_free = ST_FREE;
sys_timers[sys_timer].st_owner = ST_NO_OWNER;
sys_timers[sys_timer].st_id = ST_NO_ID;
sys_timers[sys_timer].st_irq_handler = NULL;
sys_timers[sys_timer].st_fiq_handler = NULL;
sys_timers[sys_timer].st_err_handler = NULL;
sys_timers[sys_timer].st_sync_handler = NULL;
}
}
}
void sys_timer_irq_handler_set(register uint32_t sys_timer, register uint8_t sys_timer_owner, void* sys_timer_irq_handler){
if(ST_IS_TIMER(sys_timer)){
if(sys_timers[sys_timer].st_owner == sys_timer_owner && sys_timers[sys_timer].st_en){
sys_timers[sys_timer].st_irq_handler = sys_timer_irq_handler;
}
}
}
void sys_timer_fiq_handler_set(register uint32_t sys_timer, register uint8_t sys_timer_owner, void* sys_timer_fiq_handler){
if(ST_IS_TIMER(sys_timer)){
if(sys_timers[sys_timer].st_owner == sys_timer_owner && sys_timers[sys_timer].st_en){
sys_timers[sys_timer].st_fiq_handler = sys_timer_fiq_handler;
}
}
}
void sys_timer_sync_handler_set(register uint32_t sys_timer, register uint8_t sys_timer_owner, void* sys_timer_sync_handler){
if(ST_IS_TIMER(sys_timer)){
if(sys_timers[sys_timer].st_owner == sys_timer_owner && sys_timers[sys_timer].st_en){
sys_timers[sys_timer].st_sync_handler = sys_timer_sync_handler;
}
}
}
void sys_timer_err_handler_set(register uint32_t sys_timer, register uint8_t sys_timer_owner, void* sys_timer_err_handler){
if(ST_IS_TIMER(sys_timer)){
if(sys_timers[sys_timer].st_owner == sys_timer_owner && sys_timers[sys_timer].st_en){
sys_timers[sys_timer].st_err_handler = sys_timer_err_handler;
}
}
}
void sys_timer_enable(register uint8_t sys_timer, register uint8_t sys_timer_owner){
if(ST_IS_TIMER(sys_timer)){
if(sys_timers[sys_timer].st_owner == (sys_timer_owner)){
sys_timers[sys_timer].st_en = ST_ENABLED;
}
}
}
void sys_timer_disable(register uint8_t sys_timer, register uint8_t sys_timer_owner){
if(ST_IS_TIMER(sys_timer)){
if(sys_timers[sys_timer].st_owner == (sys_timer_owner)){
sys_timers[sys_timer].st_en = ST_DISABLED;
}
}
}
//BrainOS System Timer system level API
void sys_timer_stat_set(register uint8_t sys_timer){
uint32_t sys_timer_status = 0;
if (ST_IS_TIMER(sys_timer)){
sys_timer_status = *SYSTEM_TIMER_CS;
sys_timer_status |= (ST_STATUS_SET << sys_timer);
*SYSTEM_TIMER_CS = sys_timer_status;
}
return;
}
void sys_timer_stat_reset(register uint8_t sys_timer){
register uint32_t sys_timer_status = 0;
if (ST_IS_TIMER(sys_timer)){
sys_timer_status = *SYSTEM_TIMER_CS;
sys_timer_status |= (ST_STATUS_RESET << sys_timer);
*SYSTEM_TIMER_CS = sys_timer_status;
}
return;
}
void sys_timer_stat_clear(register uint8_t sys_timer){
register uint32_t sys_timer_status = 0;
if(ST_IS_TIMER(sys_timer)){
sys_timer_status = *SYSTEM_TIMER_CS;
if(ST_IS_TIMER_MATCH(sys_timer_status, sys_timer)){
sys_timer_status |= (ST_STATUS_CLEAR << sys_timer);
*SYSTEM_TIMER_CS = sys_timer_status;
}
}
return;
}
uint8_t sys_timer_stat_get(register uint8_t sys_timer){
register uint32_t sys_timer_status = 0;
if (ST_IS_TIMER(sys_timer)){
sys_timer_status = *SYSTEM_TIMER_CS;
sys_timer_status = ST_SELECT_TIMER_STATUS(sys_timer_status, sys_timer);
sys_timer_status >>= sys_timer;
}
return ((uint8_t)sys_timer_status);
}
uint64_t sys_timer_cnt_get(){
register uint32_t sys_timer_cnt_lo = 0;
register uint32_t sys_timer_cnt_hi = 0;
sys_timer_cnt_hi = *SYSTEM_TIMER_HI;
sys_timer_cnt_lo = *SYSTEM_TIMER_LO;
while(sys_timer_cnt_hi != *SYSTEM_TIMER_HI){
sys_timer_cnt_hi = *SYSTEM_TIMER_HI;
sys_timer_cnt_lo = *SYSTEM_TIMER_LO;
}
return((uint64_t) (((unsigned long int) sys_timer_cnt_hi << 32) | sys_timer_cnt_lo));
}
uint32_t sys_timer_cnt_lo_get(){
register uint32_t sys_timer_cnt_lo = 0;
sys_timer_cnt_lo = *SYSTEM_TIMER_LO;
return((uint32_t)sys_timer_cnt_lo);
}
uint32_t sys_timer_cnt_hi_get(){
register uint32_t sys_timer_cnt_hi = 0;
sys_timer_cnt_hi = *SYSTEM_TIMER_LO;
return((uint32_t)sys_timer_cnt_hi);
}
uint32_t sys_timer_cmp_get(register uint8_t sys_timer){
register uint32_t* sys_timer_cmp = SYSTEM_TIMER_C0;
if(ST_IS_TIMER(sys_timer)){
sys_timer_cmp += sys_timer * 4;
}
return((uint32_t)*sys_timer_cmp);
}
void sys_timer_cmp_set(register uint8_t sys_timer, uint32_t sys_timer_cmp_val){
register uint32_t* sys_timer_cmp = SYSTEM_TIMER_C0;
if(ST_IS_TIMER(sys_timer)){
sys_timer_cmp += sys_timer * 4;
*sys_timer_cmp = sys_timer_cmp_val;
}
return;
}
void sys_timer_cmp_reset(register uint8_t sys_timer){
register uint32_t* sys_timer_cmp = SYSTEM_TIMER_C0;
if(ST_IS_TIMER(sys_timer)){
sys_timer_cmp += sys_timer * 4;
*sys_timer_cmp = ST_CMP_RESET;
}
return;
}
int sys_timer_is_expired(register uint8_t sys_timer){
register uint32_t sys_timer_cmp = sys_timer_cmp_get(sys_timer);
register uint32_t sys_timer_cnt_hi = sys_timer_cnt_hi_get();
if(ST_IS_TIMER(sys_timer)){
return(ST_IS_TIMER_EXPIRED(sys_timer_cnt_hi, sys_timer_cmp));
}
return 0;
}
int sys_timer_is_match(register uint8_t sys_timer){
register uint32_t sys_timer_status = 0;
if(ST_IS_TIMER(sys_timer)){
sys_timer_status = *SYSTEM_TIMER_CS;
return(ST_IS_TIMER_MATCH(sys_timer_status, sys_timer));
}
return 0;
}
/*
int main(int argc, char* argv[]){
register uint8_t sys_timer_0 = SYSTEM_TIMER_0;
register uint8_t sys_timer_1 = SYSTEM_TIMER_1;
register uint8_t sys_timer_2 = SYSTEM_TIMER_2;
register uint8_t sys_timer_3 = SYSTEM_TIMER_3;
register uint32_t i;
register uint8_t st_owner = 0;
uint32_t st_comparer[N_SYSTEM_TIMERS];
sys_timer_boot();
for (i = 0; i < N_SYSTEM_TIMERS; i++)
{
sys_timer_acquire(i, st_owner, NULL, NULL, NULL, NULL);
st_comparer[i] = 1024 * (i + 1);
}
for (i = 0; i < N_SYSTEM_TIMERS; i++)
{
sys_timer_start(i, st_owner, st_comparer[i]);
}
for (i = 0; i < N_SYSTEM_TIMERS; i++)
{
sys_timer_stop(i, st_owner);
}
for (i = 0; i < N_SYSTEM_TIMERS; i++)
{
sys_timer_restart(i, st_owner);
}
for (i = 0; i < N_SYSTEM_TIMERS; i++)
{
sys_timer_stop(i, st_owner);
}
for (i = 0; i < N_SYSTEM_TIMERS; i++)
{
sys_timer_release(i, st_owner);
}
sys_timer_shutdown();
}
*/