Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | pj | 1 | /* Project: OSLib |
2 | * Description: The OS Construction Kit |
||
3 | * Date: 1.6.2000 |
||
4 | * Idea by: Luca Abeni & Gerardo Lamastra |
||
5 | * |
||
6 | * OSLib is an SO project aimed at developing a common, easy-to-use |
||
7 | * low-level infrastructure for developing OS kernels and Embedded |
||
8 | * Applications; it partially derives from the HARTIK project but it |
||
9 | * currently is independently developed. |
||
10 | * |
||
11 | * OSLib is distributed under GPL License, and some of its code has |
||
12 | * been derived from the Linux kernel source; also some important |
||
13 | * ideas come from studying the DJGPP go32 extender. |
||
14 | * |
||
15 | * We acknowledge the Linux Community, Free Software Foundation, |
||
16 | * D.J. Delorie and all the other developers who believe in the |
||
17 | * freedom of software and ideas. |
||
18 | * |
||
19 | * For legalese, check out the included GPL license. |
||
20 | */ |
||
21 | |||
22 | /* Time Event routines */ |
||
23 | |||
24 | #include <ll/i386/stdlib.h> |
||
25 | #include <ll/i386/mem.h> |
||
26 | #include <ll/i386/error.h> |
||
27 | #include <ll/i386/hw-arch.h> |
||
28 | #include <ll/i386/pit.h> |
||
29 | #include <ll/sys/ll/ll-data.h> |
||
30 | #include <ll/sys/ll/ll-instr.h> |
||
31 | #include <ll/sys/ll/time.h> |
||
32 | #include <ll/sys/ll/event.h> |
||
33 | |||
34 | FILE(Event); |
||
35 | |||
36 | extern LL_ARCH ll_arch; |
||
37 | |||
38 | BYTE frc; |
||
39 | |||
40 | /* Timer 0 usec base tick */ |
||
41 | DWORD ticksize; |
||
42 | |||
43 | /* Timer 0 loaded time constant (= ticksize * 1.197) */ |
||
44 | WORD pit_time_const; |
||
45 | DWORD timermode; |
||
46 | |||
47 | static DWORD nts; /* System tick in nanoSeconds... */ |
||
48 | struct timespec actTime; /* Time (in nanosecs)... */ |
||
49 | extern int activeInt; |
||
50 | |||
51 | WORD lastTime; |
||
52 | struct pitspec globalCounter; |
||
53 | |||
54 | struct event eventlist[MAX_EVENT]; |
||
55 | |||
56 | struct event *freeevents; |
||
57 | struct event *firstevent; |
||
58 | |||
59 | extern void *last_handler; |
||
60 | extern void (*evt_prol) (void); |
||
61 | extern void (*evt_epil) (void); |
||
62 | |||
63 | void event_setlasthandler(void *p) { last_handler = p; } |
||
64 | |||
65 | void event_setprologue(void *p) |
||
66 | { |
||
67 | evt_prol = p; |
||
68 | } |
||
69 | |||
70 | void event_setepilogue(void *p) |
||
71 | { |
||
72 | evt_epil = p; |
||
73 | } |
||
74 | |||
75 | /* Switched to timespec */ |
||
76 | int periodic_event_post(struct timespec time, void (*handler) (void *p), |
||
77 | void *par) |
||
78 | { |
||
79 | struct event *p; |
||
80 | struct event *p1, *t; |
||
81 | |||
82 | if (!freeevents) { |
||
83 | return -1; |
||
84 | } |
||
85 | |||
86 | /* Extract from the ``free events'' queue */ |
||
87 | p = freeevents; |
||
88 | freeevents = p->next; |
||
89 | |||
90 | /* Fill the event fields */ |
||
91 | p->handler = handler; |
||
92 | TIMESPEC_ASSIGN(&(p->time), &time); |
||
93 | p->par = par; |
||
94 | |||
95 | /* ...And insert it in the event queue!!! */ |
||
96 | |||
97 | t = NULL; |
||
98 | /* walk through list, finding spot, adjusting ticks parameter */ |
||
99 | for (p1 = firstevent; p1; p1 = t->next) { |
||
100 | /* |
||
101 | SUBTIMESPEC(&time, &(p1->time), &tmp); |
||
102 | if ((tmp.tv_sec > 0) && (tmp.tv_nsec > 0)) { |
||
103 | */ |
||
104 | if (TIMESPEC_A_GT_B(&time, &p1->time)) |
||
105 | t = p1; |
||
106 | else |
||
107 | break; |
||
108 | } |
||
109 | |||
110 | /* adjust next entry */ |
||
111 | if (t) { |
||
112 | t->next = p; |
||
113 | } else { |
||
114 | firstevent = p; |
||
115 | } |
||
116 | p->next = p1; |
||
117 | |||
118 | return p->index; |
||
119 | } |
||
120 | |||
121 | int periodic_event_delete(int index) |
||
122 | { |
||
123 | struct event *p1, *t; |
||
124 | |||
125 | t = NULL; |
||
126 | /* walk through list, finding spot, adjusting ticks parameter */ |
||
127 | for (p1 = firstevent; (p1) && (index != p1->index); p1 = t->next) { |
||
128 | t = p1; |
||
129 | } |
||
130 | |||
131 | if (p1 == NULL) { |
||
132 | return -1; |
||
133 | } |
||
134 | |||
135 | if (t == NULL) { |
||
136 | firstevent = p1->next; |
||
137 | } else { |
||
138 | t->next = p1->next; |
||
139 | } |
||
140 | p1->next = freeevents; |
||
141 | freeevents = p1; |
||
142 | |||
143 | return 1; |
||
144 | } |
||
145 | |||
146 | void periodic_wake_up(void) |
||
147 | { /* CHANGE the NAME, please... */ |
||
148 | struct event *p, *pp; |
||
149 | WORD tmp; |
||
150 | |||
151 | tmp = pit_read(frc); |
||
152 | ADDPITSPEC((WORD) (lastTime - tmp), &globalCounter); |
||
153 | lastTime = tmp; |
||
154 | |||
155 | activeInt++; |
||
156 | if (activeInt == 1 && evt_prol != NULL) { |
||
157 | evt_prol(); |
||
158 | } |
||
159 | |||
160 | ADDNANO2TIMESPEC(nts, &actTime); |
||
161 | |||
162 | for (p = firstevent; p != NULL; p = pp) { |
||
163 | /* |
||
164 | SUBTIMESPEC(&(p->time), &actTime, &tmp); |
||
165 | if ((tmp.tv_sec > 0) && (tmp.tv_nsec > 0)) { |
||
166 | break; |
||
167 | } */ |
||
168 | if ((p->time.tv_sec > actTime.tv_sec) || |
||
169 | ((p->time.tv_sec == actTime.tv_sec) |
||
170 | && (p->time.tv_nsec > actTime.tv_nsec))) { |
||
171 | break; |
||
172 | } |
||
173 | pp = p->next; |
||
174 | p->next = freeevents; |
||
175 | freeevents = p; |
||
176 | firstevent = pp; |
||
177 | p->handler(p->par); |
||
178 | } |
||
179 | |||
180 | if (activeInt == 1 && evt_epil != NULL) { |
||
181 | evt_epil(); |
||
182 | } |
||
183 | activeInt--; |
||
184 | } |
||
185 | |||
186 | void event_init(struct ll_initparms *l) |
||
187 | { |
||
188 | extern void ll_timer(void); |
||
189 | int i; |
||
190 | BYTE mask; |
||
191 | TIME t; |
||
192 | |||
193 | IDT_place(0x40, ll_timer); |
||
194 | |||
195 | if (l->mode != LL_PERIODIC) { |
||
196 | message("One-shot timer selected...\n"); |
||
197 | t = 0; |
||
198 | /* Mode: Binary/Mode 4/16 bit Time_const/Counter 0 */ |
||
199 | pit_init(0, TMR_MD4, 0xFFFF); /* Timer 0, Mode 4, constant 0xFFFF */ |
||
200 | } else { |
||
201 | message("Periodic timer selected...\n"); |
||
202 | t = l->tick; |
||
203 | /* Translate the tick value in usec into a suitable time constant */ |
||
204 | /* for 8254 timer chip; the chip is driven with a 1.19718 MHz */ |
||
205 | /* frequency; then the effective frequency is given by the base */ |
||
206 | /* frequency divided for the time constant; the tick is the inverse */ |
||
207 | /* of this effective frequency (in usec!) */ |
||
208 | /* Time-Constant = f_base (MHz) * tick (usec) */ |
||
209 | /* If T-C == 0 -> T-C = 65536 (Max available) */ |
||
210 | ticksize = t; |
||
211 | t = t * 1197; |
||
212 | t = t / 1000; |
||
213 | /* Only for security! This should cause timer overrun */ |
||
214 | /* While 0 would set maximum period on timer */ |
||
215 | if (t == 0) |
||
216 | t = 1; |
||
217 | pit_time_const = (WORD) (t & 0xFFFF); |
||
218 | /* Mode: Binary/Mode 2/16 bit Time_const/Counter 0 */ |
||
219 | pit_init(0, TMR_MD2, t); /* Timer 0, Mode 2, Time constant t */ |
||
220 | |||
221 | |||
222 | } |
||
223 | timermode = l->mode; |
||
224 | |||
225 | if (ll_arch.x86.cpu > 4) { |
||
226 | /* Timer1: mode 0, time const 0... */ |
||
227 | pit_init(1, TMR_MD0, 0); |
||
228 | frc = 1; |
||
229 | } else { |
||
230 | frc = 2; |
||
231 | pit_init(2, TMR_MD0, 0); |
||
232 | outp(0x61, 3); |
||
233 | } |
||
234 | |||
235 | mask = ll_in(0x21); |
||
236 | mask &= 0xFE; /* 0xFE = ~0x01 */ |
||
237 | ll_out(0x21, mask); |
||
238 | |||
239 | |||
240 | /* Init the event list... */ |
||
241 | for (i = 0; i < MAX_EVENT; i++) { |
||
242 | if (i < MAX_EVENT - 1) { |
||
243 | eventlist[i].next = &(eventlist[i + 1]); |
||
244 | } |
||
245 | eventlist[i].index = i; |
||
246 | } |
||
247 | eventlist[MAX_EVENT - 1].next = NULL; |
||
248 | freeevents = &(eventlist[0]); |
||
249 | |||
250 | evt_prol = NULL; |
||
251 | evt_epil = NULL; |
||
252 | |||
253 | /* Initialization of the time variables for periodic mode */ |
||
254 | nts = ticksize * 1000; |
||
255 | NULL_TIMESPEC(&actTime); |
||
256 | |||
257 | /* Initialization of the general time variables */ |
||
258 | NULLPITSPEC(&globalCounter); |
||
259 | lastTime = 0; |
||
260 | |||
261 | if (timermode == LL_PERIODIC) { |
||
262 | event_post = periodic_event_post; |
||
263 | event_delete = periodic_event_delete; |
||
264 | } else { |
||
265 | event_post = oneshot_event_post; |
||
266 | event_delete = oneshot_event_delete; |
||
267 | } |
||
268 | } |