-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjstimer.c
447 lines (410 loc) · 15.7 KB
/
jstimer.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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*
* This file is part of Espruino, a JavaScript interpreter for Microcontrollers
*
* Copyright (C) 2013 Gordon Williams <[email protected]>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* ----------------------------------------------------------------------------
* Common utility timer handling functions
* ----------------------------------------------------------------------------
*/
#include "jstimer.h"
#include "jsparse.h"
#include "jsinteractive.h"
UtilTimerTask utilTimerTasks[UTILTIMERTASK_TASKS];
volatile unsigned char utilTimerTasksHead = 0;
volatile unsigned char utilTimerTasksTail = 0;
volatile bool utilTimerOn = false;
unsigned int utilTimerBit;
bool utilTimerInIRQ = false;
unsigned int utilTimerData;
uint16_t utilTimerReload0H, utilTimerReload0L, utilTimerReload1H, utilTimerReload1L;
#ifndef SAVE_ON_FLASH
static void jstUtilTimerInterruptHandlerNextByte(UtilTimerTask *task) {
// move to next element in var
task->data.buffer.charIdx++;
size_t maxChars = jsvGetCharactersInVar(task->data.buffer.var);
if (task->data.buffer.charIdx >= maxChars) {
task->data.buffer.charIdx = (unsigned char)(task->data.buffer.charIdx - maxChars);
/* NOTE: We don't Lock/UnLock here. We assume that the string has already been
* referenced elsewhere (in the Waveform class) so won't get freed. Why? Because
* we can't lock easily. We could get an IRQ right as some other code was in the
* middle of read/modify/write of the flags member, and then the locks would get
* out of sync. */
if (jsvGetLastChild(task->data.buffer.var)) {
task->data.buffer.var = _jsvGetAddressOf(jsvGetLastChild(task->data.buffer.var));
} else { // else no more... move on to the next
if (task->data.buffer.nextBuffer) {
task->data.buffer.charIdx = 0;
task->data.buffer.var = _jsvGetAddressOf(task->data.buffer.nextBuffer);
// flip buffers
JsVarRef t = task->data.buffer.nextBuffer;
task->data.buffer.nextBuffer = task->data.buffer.currentBuffer;
task->data.buffer.currentBuffer = t;
} else {
task->data.buffer.var = 0;
// No more data - make sure we don't repeat!
task->repeatInterval = 0;
}
}
}
}
static inline unsigned char *jstUtilTimerInterruptHandlerByte(UtilTimerTask *task) {
return (unsigned char*)&task->data.buffer.var->varData.str[task->data.buffer.charIdx];
}
#endif
void jstUtilTimerInterruptHandler() {
if (utilTimerOn) {
utilTimerInIRQ = true;
JsSysTime time = jshGetSystemTime();
// execute any timers that are due
while (utilTimerTasksTail!=utilTimerTasksHead && utilTimerTasks[utilTimerTasksTail].time <= time) {
UtilTimerTask *task = &utilTimerTasks[utilTimerTasksTail];
void (*executeFn)(JsSysTime time) = 0;
// actually perform the task
switch (task->type) {
case UET_SET: {
int j;
for (j=0;j<UTILTIMERTASK_PIN_COUNT;j++) {
if (task->data.set.pins[j] == NC) break;
jshPinSetValue(task->data.set.pins[j], (task->data.set.value >> j)&1);
}
} break;
#ifndef SAVE_ON_FLASH
case UET_READ_SHORT: {
if (!task->data.buffer.var) break;
int v = jshPinAnalogFast(task->data.buffer.pin);
*jstUtilTimerInterruptHandlerByte(task) = (unsigned char)v; // LSB first
jstUtilTimerInterruptHandlerNextByte(task);
*jstUtilTimerInterruptHandlerByte(task) = (unsigned char)(v >> 8);
jstUtilTimerInterruptHandlerNextByte(task);
break;
}
case UET_READ_BYTE: {
if (!task->data.buffer.var) break;
*jstUtilTimerInterruptHandlerByte(task) = (unsigned char)(jshPinAnalogFast(task->data.buffer.pin) >> 8);
jstUtilTimerInterruptHandlerNextByte(task);
break;
}
case UET_WRITE_SHORT:
case UET_WRITE_BYTE: {
if (!task->data.buffer.var) break;
// get data
int sum;
if (task->type == UET_WRITE_SHORT) {
sum = *jstUtilTimerInterruptHandlerByte(task); // LSB first
jstUtilTimerInterruptHandlerNextByte(task);
} else {
sum = 0;
}
sum |= (unsigned short)(*jstUtilTimerInterruptHandlerByte(task) << 8);
jstUtilTimerInterruptHandlerNextByte(task);
task->data.buffer.currentValue = (unsigned short)sum;
// now search for other tasks writing to this pin... (polyphony)
int t = (utilTimerTasksTail+1) & (UTILTIMERTASK_TASKS-1);
while (t!=utilTimerTasksHead) {
if (UET_IS_BUFFER_WRITE_EVENT(utilTimerTasks[t].type))
sum += ((int)(unsigned int)utilTimerTasks[t].data.buffer.currentValue) - 32768;
t = (t+1) & (UTILTIMERTASK_TASKS-1);
}
// saturate
if (sum<0) sum = 0;
if (sum>65535) sum = 65535;
// and output...
jshSetOutputValue(task->data.buffer.pinFunction, sum);
break;
}
case UET_EXECUTE:
executeFn = task->data.execute;
break;
#endif
case UET_WAKEUP: // we've already done our job by waking the device up
default: break;
}
// If we need to repeat
if (task->repeatInterval) {
// update time (we know time > task->time) - what if we're being asked to do too fast? skip one (or 500 :)
unsigned int t = ((unsigned int)(time+task->repeatInterval - task->time)) / task->repeatInterval;
if (t<1) t=1;
task->time = task->time + (JsSysTime)task->repeatInterval*t;
// do an in-place bubble sort to ensure that times are still in the right order
unsigned char ta = utilTimerTasksTail;
unsigned char tb = (ta+1) & (UTILTIMERTASK_TASKS-1);
while (tb != utilTimerTasksHead) {
if (utilTimerTasks[ta].time > utilTimerTasks[tb].time) {
UtilTimerTask task = utilTimerTasks[ta];
utilTimerTasks[ta] = utilTimerTasks[tb];
utilTimerTasks[tb] = task;
}
ta = tb;
tb = (tb+1) & (UTILTIMERTASK_TASKS-1);
}
} else {
// Otherwise no repeat - just go straight to the next one!
utilTimerTasksTail = (utilTimerTasksTail+1) & (UTILTIMERTASK_TASKS-1);
}
// execute the function if we had one (we do this now, because if we did it earlier we'd have to cope with everything changing)
if (executeFn) executeFn(time);
}
// re-schedule the timer if there is something left to do
if (utilTimerTasksTail != utilTimerTasksHead) {
jshUtilTimerReschedule(utilTimerTasks[utilTimerTasksTail].time - time);
} else {
utilTimerOn = false;
jshUtilTimerDisable();
}
utilTimerInIRQ = false;
} else {
// Nothing left to do - disable the timer
jshUtilTimerDisable();
}
}
/// Return true if the utility timer is running
bool jstUtilTimerIsRunning() {
return utilTimerOn;
}
void jstUtilTimerWaitEmpty() {
WAIT_UNTIL(!jstUtilTimerIsRunning(), "Utility Timer");
}
/// Return the latest task for a pin (false if not found)
bool jstGetLastPinTimerTask(Pin pin, UtilTimerTask *task) {
unsigned char ptr = utilTimerTasksHead;
if (ptr == utilTimerTasksTail) return false; // nothing in here
ptr = (ptr+UTILTIMERTASK_TASKS-1) & (UTILTIMERTASK_TASKS-1);
// now we're at the last timer task - work back until we've just gone back past utilTimerTasksTail
while (ptr != ((utilTimerTasksTail+UTILTIMERTASK_TASKS-1) & (UTILTIMERTASK_TASKS-1))) {
if (utilTimerTasks[ptr].type == UET_SET) {
int i;
for (i=0;i<UTILTIMERTASK_PIN_COUNT;i++)
if (utilTimerTasks[ptr].data.set.pins[i] == pin) {
*task = utilTimerTasks[ptr];
return true;
} else if (utilTimerTasks[ptr].data.set.pins[i]==NC)
break;
}
ptr = (ptr+UTILTIMERTASK_TASKS-1) & (UTILTIMERTASK_TASKS-1);
}
return false;
}
#ifndef SAVE_ON_FLASH
/// Return true if a timer task for the given variable exists (and set 'task' to it)
bool jstGetLastBufferTimerTask(JsVar *var, UtilTimerTask *task) {
JsVarRef ref = jsvGetRef(var);
jshInterruptOff();
unsigned char ptr = utilTimerTasksHead;
if (ptr != utilTimerTasksTail) {
ptr = (ptr+UTILTIMERTASK_TASKS-1) & (UTILTIMERTASK_TASKS-1);
// now we're at the last timer task - work back until we've just gone back past utilTimerTasksTail
while (ptr != ((utilTimerTasksTail+UTILTIMERTASK_TASKS-1) & (UTILTIMERTASK_TASKS-1))) {
if (UET_IS_BUFFER_EVENT(utilTimerTasks[ptr].type)) {
if (utilTimerTasks[ptr].data.buffer.currentBuffer==ref || utilTimerTasks[ptr].data.buffer.nextBuffer==ref) {
*task = utilTimerTasks[ptr];
jshInterruptOn();
return true;
}
}
ptr = (ptr+UTILTIMERTASK_TASKS-1) & (UTILTIMERTASK_TASKS-1);
}
}
jshInterruptOn();
return false;
}
#endif
/// Is the timer full - can it accept any other signals?
static bool utilTimerIsFull() {
unsigned char nextHead = (utilTimerTasksHead+1) & (UTILTIMERTASK_TASKS-1);
return nextHead == utilTimerTasksTail;
}
// Queue a task up to be executed when a timer fires... return false on failure
bool utilTimerInsertTask(UtilTimerTask *task) {
// check if queue is full or not
if (utilTimerIsFull()) return false;
if (!utilTimerInIRQ) jshInterruptOff();
// find out where to insert
unsigned char insertPos = utilTimerTasksTail;
while (insertPos != utilTimerTasksHead && utilTimerTasks[insertPos].time < task->time)
insertPos = (insertPos+1) & (UTILTIMERTASK_TASKS-1);
bool haveChangedTimer = insertPos==utilTimerTasksTail;
//jsiConsolePrintf("== %d\n",haveChangedTimer);
//jsiConsolePrint("Insert at ");jsiConsolePrintInt(insertPos);jsiConsolePrint(", Tail is ");jsiConsolePrintInt(utilTimerTasksTail);jsiConsolePrint("\n");
// shift items forward
int i = utilTimerTasksHead;
while (i != insertPos) {
unsigned char next = (i+UTILTIMERTASK_TASKS-1) & (UTILTIMERTASK_TASKS-1);
utilTimerTasks[i] = utilTimerTasks[next];
i = next;
}
// add new item
utilTimerTasks[insertPos] = *task;
// increase task list size
utilTimerTasksHead = (utilTimerTasksHead+1) & (UTILTIMERTASK_TASKS-1);
//jsiConsolePrint("Head is ");jsiConsolePrintInt(utilTimerTasksHead);jsiConsolePrint("\n");
// now set up timer if not already set up...
if (!utilTimerOn || haveChangedTimer) {
utilTimerOn = true;
//jsiConsolePrintf("utilTimerTasks[utilTimerTasksTail].time - jshGetSystemTime() = %d\n",utilTimerTasks[utilTimerTasksTail].time - jshGetSystemTime());
jshUtilTimerStart(utilTimerTasks[utilTimerTasksTail].time - jshGetSystemTime());
}
if (!utilTimerInIRQ) jshInterruptOn();
return true;
}
bool jstPinOutputAtTime(JsSysTime time, Pin *pins, int pinCount, uint8_t value) {
//jsiConsolePrintf("pinCount = %d\n",pinCount);
assert(pinCount<=UTILTIMERTASK_PIN_COUNT);
UtilTimerTask task;
task.time = time;
task.repeatInterval = 0;
task.type = UET_SET;
int i;
for (i=0;i<UTILTIMERTASK_PIN_COUNT;i++)
task.data.set.pins[i] = (Pin)((i<pinCount) ? pins[i] : NC);
task.data.set.value = value;
//jsiConsolePrintf("at time 01 : %d\n",time);
WAIT_UNTIL(!utilTimerIsFull(), "Utility Timer");
//jsiConsolePrintf("at time 02\n");
return utilTimerInsertTask(&task);
}
/// Set the utility timer so we're woken up in whatever time period
bool jstSetWakeUp(JsSysTime period) {
UtilTimerTask task;
task.time = jshGetSystemTime() + period;
task.repeatInterval = 0;
task.type = UET_WAKEUP;
bool hasTimer = false;
JsSysTime nextTime;
// work out if we're waiting for a timer,
// and if so, when it's going to be
jshInterruptOff();
if (utilTimerTasksTail!=utilTimerTasksHead) {
hasTimer = true;
nextTime = utilTimerTasks[utilTimerTasksTail].time;
}
jshInterruptOn();
if (hasTimer && task.time >= nextTime) {
// we already had a timer, and it's going to wake us up sooner.
// don't create a WAKEUP timer task
return true;
}
bool ok = utilTimerInsertTask(&task);
// We wait until the timer is out of the reload event, because the reload event itself would wake us up
return ok;
}
/** If the first timer task is a wakeup task, remove it. This stops
* us filling the timer full of wakeup events if we wake up from sleep
* before the wakeup event */
void jstClearWakeUp() {
bool removedTimer = false;
jshInterruptOff();
// while the first item is a wakeup, remove it
while (utilTimerTasksTail!=utilTimerTasksHead &&
utilTimerTasks[utilTimerTasksTail].type == UET_WAKEUP) {
utilTimerTasksTail = (utilTimerTasksTail+1) & (UTILTIMERTASK_TASKS-1);
removedTimer = true;
}
// if the queue is now empty, and we stop the timer
if (utilTimerTasksTail==utilTimerTasksHead && removedTimer)
jshUtilTimerDisable();
jshInterruptOn();
}
#ifndef SAVE_ON_FLASH
bool jstStartSignal(JsSysTime startTime, JsSysTime period, Pin pin, JsVar *currentData, JsVar *nextData, UtilTimerEventType type) {
if (!jshIsPinValid(pin)) return false;
UtilTimerTask task;
task.repeatInterval = (unsigned int)period;
task.time = startTime + period;
task.type = type;
if (UET_IS_BUFFER_WRITE_EVENT(type)) {
task.data.buffer.pinFunction = jshGetCurrentPinFunction(pin);
if (!task.data.buffer.pinFunction) return false; // no pin function found...
} else if (UET_IS_BUFFER_READ_EVENT(type)) {
#ifndef LINUX
//if (pinInfo[pin].analog == JSH_ANALOG_NONE) return false; // no analog...
#endif
task.data.buffer.pin = pin;
} else {
assert(0);
return false;
}
task.data.buffer.charIdx = 0;
task.data.buffer.var = currentData; // no locks (this needs to already be reffed)
task.data.buffer.currentBuffer = jsvGetRef(currentData);
if (nextData) {
// then we're repeating!
task.data.buffer.nextBuffer = jsvGetRef(nextData);
} else {
// then we're not repeating
task.data.buffer.nextBuffer = 0;
}
return utilTimerInsertTask(&task);
}
/// Return true if a timer task for the given variable exists (and set 'task' to it)
bool jstStopBufferTimerTask(JsVar *var) {
JsVarRef ref = jsvGetRef(var);
jshInterruptOff();
unsigned char ptr = utilTimerTasksHead;
if (ptr != utilTimerTasksTail) {
unsigned char endPtr = ((utilTimerTasksTail+UTILTIMERTASK_TASKS-1) & (UTILTIMERTASK_TASKS-1));
ptr = (ptr+UTILTIMERTASK_TASKS-1) & (UTILTIMERTASK_TASKS-1);
// now we're at the last timer task - work back until we've just gone back past utilTimerTasksTail
while (ptr != endPtr) {
if (UET_IS_BUFFER_EVENT(utilTimerTasks[ptr].type)) {
if (utilTimerTasks[ptr].data.buffer.currentBuffer==ref || utilTimerTasks[ptr].data.buffer.nextBuffer==ref) {
// shift tail back along
unsigned char next = (ptr+UTILTIMERTASK_TASKS-1) & (UTILTIMERTASK_TASKS-1);
while (next!=endPtr) {
utilTimerTasks[ptr] = utilTimerTasks[next];
ptr = next;
next = (ptr+UTILTIMERTASK_TASKS-1) & (UTILTIMERTASK_TASKS-1);
}
// move 'end' pointer back
utilTimerTasksTail = (utilTimerTasksTail+1) & (UTILTIMERTASK_TASKS-1);
jshInterruptOn();
return true;
}
}
ptr = (ptr+UTILTIMERTASK_TASKS-1) & (UTILTIMERTASK_TASKS-1);
}
}
jshInterruptOn();
return false;
}
#endif
void jstReset() {
jshUtilTimerDisable();
utilTimerTasksTail = utilTimerTasksHead = 0;
}
void jstDumpUtilityTimers() {
int i;
UtilTimerTask uTimerTasks[UTILTIMERTASK_TASKS];
jshInterruptOff();
for (i=0;i<UTILTIMERTASK_TASKS;i++)
uTimerTasks[i] = utilTimerTasks[i];
unsigned char uTimerTasksHead = utilTimerTasksHead;
unsigned char uTimerTasksTail = utilTimerTasksTail;
jshInterruptOn();
unsigned char t = uTimerTasksTail;
while (t!=uTimerTasksHead) {
UtilTimerTask task = uTimerTasks[t];
jsiConsolePrintf("%08d us : ", (int)(1000*jshGetMillisecondsFromTime(task.time-jsiLastIdleTime)));
switch (task.type) {
case UET_WAKEUP : jsiConsolePrintf("WAKEUP\n"); break;
case UET_SET : jsiConsolePrintf("SET ");
for (i=0;i<UTILTIMERTASK_PIN_COUNT;i++)
jsiConsolePrintf("%p=%d,", task.data.set.pins[i], (task.data.set.value>>i)&1);
jsiConsolePrintf("\n");
break;
#ifndef SAVE_ON_FLASH
case UET_WRITE_BYTE : jsiConsolePrintf("WRITE_BYTE\n"); break;
case UET_READ_BYTE : jsiConsolePrintf("READ_BYTE\n"); break;
case UET_WRITE_SHORT : jsiConsolePrintf("WRITE_SHORT\n"); break;
case UET_READ_SHORT : jsiConsolePrintf("READ_SHORT\n"); break;
case UET_EXECUTE : jsiConsolePrintf("EXECUTE %x\n", task.data.execute); break;
#endif
default : jsiConsolePrintf("Unknown type %d\n", task.type); break;
}
t = (t+1) & (UTILTIMERTASK_TASKS-1);
}
}