Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 124 lines (97 sloc) 2.215 kb
59ab516 clock and timers added
Tyler Riddle authored
1 /*
2 * timer.c
3 *
4 * Created on: Jul 13, 2012
5 * Author: Tyler
6 */
7
8 #include <string.h>
1edc70b added ADC support; added command to subscribe to ADC data - jitter is ve...
Tyler Riddle authored
9 #include <stdlib.h>
a16eaa6 Huge set of changes:
Tyler Riddle authored
10 #include <util/atomic.h>
59ab516 clock and timers added
Tyler Riddle authored
11
12 #include "config.h"
13 #include "timer.h"
14 #include "fault.h"
15
16 volatile struct timer timers[TIMER_NUM_SLOTS];
17
18 uint8_t timer_create(uint16_t top, uint16_t offset, bool repeat, timer_cb *handler, void *handlerArg) {
19 uint8_t i;
20
21 for(i = 0; i < TIMER_NUM_SLOTS; i++) {
22 if (! timers[i].taken) {
23 break;
24 }
25 }
26
27 if (timers[i].taken) {
28 //oops no available timers
29 fault_fatal(FAULT_TIMER_NO_SLOTS_AVAILABLE);
30 }
31
a16eaa6 Huge set of changes:
Tyler Riddle authored
32 timers[i].dirty = false;
59ab516 clock and timers added
Tyler Riddle authored
33 timers[i].taken = true;
34 timers[i].offset = offset;
35 timers[i].top = top;
36 timers[i].repeat = repeat;
37 timers[i].handler = handler;
38 timers[i].handlerArg = handlerArg;
39
40 return i;
41 }
42
43 void timer_start(uint8_t timerId) {
44 timers[timerId].active = true;
45 }
46
1edc70b added ADC support; added command to subscribe to ADC data - jitter is ve...
Tyler Riddle authored
47 void timer_pause(uint8_t timerId) {
59ab516 clock and timers added
Tyler Riddle authored
48 timers[timerId].active = false;
1edc70b added ADC support; added command to subscribe to ADC data - jitter is ve...
Tyler Riddle authored
49 }
50
51 void timer_stop(uint8_t timerId) {
52 timer_pause(timerId);
59ab516 clock and timers added
Tyler Riddle authored
53 timers[timerId].count = 0;
54 }
55
56 void timer_delete(uint8_t timerId) {
1edc70b added ADC support; added command to subscribe to ADC data - jitter is ve...
Tyler Riddle authored
57 if (timers[timerId].handlerArg != NULL) {
58 free(timers[timerId].handlerArg);
59 }
60
59ab516 clock and timers added
Tyler Riddle authored
61 timer_stop(timerId);
62 timers[timerId].taken = false;
1edc70b added ADC support; added command to subscribe to ADC data - jitter is ve...
Tyler Riddle authored
63 timers[timerId].handler = NULL;
64 timers[timerId].handlerArg = NULL;
65 }
66
67 void timer_delete_all(void) {
68 static int i;
69
70 for(i = 0; i < TIMER_NUM_SLOTS; i++) {
71 timer_delete(i);
72 }
59ab516 clock and timers added
Tyler Riddle authored
73 }
74
75 static void timer_update(volatile struct timer *p) {
76 if (! p->active) {
77 return;
78 }
79
80 if (p->offset > 0) {
81 p->offset--;
82 } else if (++p->count >= p->top) {
a16eaa6 Huge set of changes:
Tyler Riddle authored
83 p->dirty = true;
59ab516 clock and timers added
Tyler Riddle authored
84 p->count = 0;
85 }
86 }
87
88 void timer_tick(void) {
89 static uint8_t i;
90
91 for(i = 0; i < TIMER_NUM_SLOTS; i++) {
92 timer_update(&timers[i]);
93 }
94 }
95
a16eaa6 Huge set of changes:
Tyler Riddle authored
96 void timer_run(void) {
97 static uint8_t i;
98 static bool busy = false;
99
100 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
101 if (busy) {
102 return;
103 }
104
105 busy = true;
106 }
107
108 for(i = 0; i < TIMER_NUM_SLOTS; i++) {
109 if (timers[i].dirty) {
110 if (timers[i].handler != NULL) {
111 timers[i].handler(&timers[i]);
112 }
113
114 timers[i].dirty = false;
115 }
116 }
117
118 busy = false;
119 }
120
59ab516 clock and timers added
Tyler Riddle authored
121 void timer_init(void) {
122 memset((void *)timers, 0, sizeof(timers));
123 }
Something went wrong with that request. Please try again.