Repository navigation
Expand file tree
/
Copy pathDMD2_Timer.cpp
More file actions
378 lines (327 loc) · 10.4 KB
/
Copy pathDMD2_Timer.cpp
File metadata and controls
378 lines (327 loc) · 10.4 KB
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
/*
Timer management code for the DMD library, includes implementation
for AVR-based Arduino compatible (Eleven, Uno, etc.) and for Arduino Due
Copyright (C) 2014 Freetronics, Inc. (info <at> freetronics <dot> com)
Updated by Angus Gratton, based on DMD by Marc Alexander.
---
This program is free software: you can redistribute it and/or modify it under the terms
of the version 3 GNU General Public License as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program.
If not, see <http://www.gnu.org/licenses/>.
*/
#include "DMD2.h"
#ifdef ESP8266
#include <core_version.h>
#if defined(ARDUINO_ESP8266_MAJOR) && ARDUINO_ESP8266_MAJOR >= 3
#include <Schedule.h>
#define DMD2_ESP8266_COOPERATIVE 1
#endif
#endif
/*
Uncomment the following line if you don't want DMD library to touch
any timer functionality (ie if you want to use TimerOne library or
similar, or other libraries that use timers.
With timers enabled, on AVR-based Arduinos this code will register a
Timer1 overflow handler (without disrupting any built-in library
functions.) On ARM-based Arduino Due it will use use Timer7.
*/
//#define NO_TIMERS
#define ESP8266_TIMER0_TICKS microsecondsToClockCycles(250) // 250 microseconds between calls to scan_running_dmds seems to works better than 1000.
// Assuming esp32 using frequency of 80MHz...
#define ESP32_TIMER0 0
// run counter to increment every 1 microseconds
#define ESP32_TIMER0_PRESCALER 80
// call scan_running_dmds every 250 microseconds on ESP32
#define ESP32_TIMER0_RELOAD_ON 250
#ifdef NO_TIMERS
// Timer-free stub code which gets compiled in only if NO_TIMERS is set
void BaseDMD::begin() {
beginNoTimer();
}
void BaseDMD::end() {
#if defined(ESP32)
releasePWM();
clearScreen();
#endif
}
#else // Use timers
// Forward declarations for tracking currently running DMDs
static bool register_running_dmd(BaseDMD *dmd);
static bool unregister_running_dmd(BaseDMD *dmd);
static void inline scan_running_dmds();
#if defined(ESP8266) && !defined(DMD2_ESP8266_COOPERATIVE)
static void ICACHE_RAM_ATTR esp8266_ISR_wrapper();
#endif
#ifdef __AVR__
/* This AVR timer ISR uses the standard /64 timing used by Timer1 in the Arduino core,
so none of those registers (or normal PWM timing) is changed. We do skip 50% of ISRs
as 50% timer overflows is approximately every 4ms, which is fine for flicker-free
updating.
*/
ISR(TIMER1_OVF_vect)
{
static uint8_t skip_isrs = 0;
skip_isrs = (skip_isrs + 1) % 2;
if(skip_isrs)
return;
scan_running_dmds();
}
void BaseDMD::begin()
{
beginNoTimer(); // Do any generic setup
char oldSREG = SREG;
cli();
register_running_dmd(this);
TIMSK1 = _BV(TOIE1); // set overflow interrupt
SREG = oldSREG;
}
void BaseDMD::end()
{
char oldSREG = SREG;
cli();
bool still_running = unregister_running_dmd(this);
if(!still_running)
TIMSK1 &= ~_BV(TOIE1); // disable timer interrupt, no more DMDs are running
SREG = oldSREG;
// One final (manual) scan to turn off all LEDs
clearScreen();
scanDisplay();
}
#elif defined (__arm__) // __ARM__, Due assumed for now
/* ARM timer callback (ISR context), checks timer status then scans all running DMDs */
void TC7_Handler(){
TC_GetStatus(TC2, 1);
scan_running_dmds();
}
void BaseDMD::begin()
{
beginNoTimer(); // Do any generic setup
NVIC_DisableIRQ(TC7_IRQn);
register_running_dmd(this);
pmc_set_writeprotect(false);
pmc_enable_periph_clk(TC7_IRQn);
// Timer 7 is TC2, channel 1
TC_Configure(TC2, 1, TC_CMR_WAVE | TC_CMR_WAVSEL_UP_RC | TC_CMR_TCCLKS_TIMER_CLOCK4); // counter up, /128 divisor
TC_SetRC(TC2, 1, 2500); // approx 4ms at /128 divisor
TC2->TC_CHANNEL[1].TC_IER=TC_IER_CPCS;
NVIC_ClearPendingIRQ(TC7_IRQn);
NVIC_EnableIRQ(TC7_IRQn);
TC_Start(TC2, 1);
}
void BaseDMD::end()
{
NVIC_DisableIRQ(TC7_IRQn);
bool still_running = unregister_running_dmd(this);
if(still_running)
NVIC_EnableIRQ(TC7_IRQn); // Still some DMDs running
else
TC_Stop(TC2, 1);
clearScreen();
scanDisplay();
}
#elif defined (ESP8266)
#ifdef DMD2_ESP8266_COOPERATIVE
// Recurrent callbacks can cancel only by returning false. Keep one callback
// alive across a stop/start until it observes that no displays remain.
static bool esp8266_callback_scheduled = false;
static bool esp8266_displays_running = false;
static bool esp8266_scan_callback()
{
if (!esp8266_displays_running) {
esp8266_callback_scheduled = false;
return false;
}
scan_running_dmds();
return true;
}
void BaseDMD::begin()
{
beginNoTimer();
if (!register_running_dmd(this))
return;
esp8266_displays_running = true;
if (!esp8266_callback_scheduled) {
esp8266_callback_scheduled = schedule_recurrent_function_us(esp8266_scan_callback, 250);
if (!esp8266_callback_scheduled)
esp8266_displays_running = unregister_running_dmd(this);
}
}
void BaseDMD::end()
{
esp8266_displays_running = unregister_running_dmd(this);
clearScreen();
scanDisplay();
}
#else
void BaseDMD::begin()
{
beginNoTimer();
timer0_detachInterrupt();
register_running_dmd(this);
timer0_isr_init();
timer0_attachInterrupt(esp8266_ISR_wrapper);
timer0_write(ESP.getCycleCount() + ESP8266_TIMER0_TICKS);
}
void BaseDMD::end()
{
bool still_running = unregister_running_dmd(this);
if(!still_running)
{
timer0_detachInterrupt(); // timer0 disables itself when the CPU cycle count reaches its own value, hence ESP.getCycleCount()
}
clearScreen();
scanDisplay();
}
#endif
#elif defined(ESP32)
#include <esp_timer.h>
#include <freertos/semphr.h>
// The shared esp_timer task only wakes our worker; it never performs SPI or
// waits for the refresh mutex. Missed notifications are coalesced.
static esp_timer_handle_t refresh_timer = nullptr;
static TaskHandle_t refresh_task = nullptr;
static SemaphoreHandle_t refresh_mutex() {
static SemaphoreHandle_t gate = xSemaphoreCreateMutex();
return gate;
}
static void refresh_worker(void *) {
for (;;) {
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
SemaphoreHandle_t gate = refresh_mutex();
if (gate && xSemaphoreTake(gate, portMAX_DELAY) == pdTRUE) {
scan_running_dmds();
xSemaphoreGive(gate);
}
}
}
static void refresh_callback(void *) {
if (refresh_task) xTaskNotifyGive(refresh_task);
}
void BaseDMD::begin() {
SemaphoreHandle_t gate = refresh_mutex();
if (!gate) return;
xSemaphoreTake(gate, portMAX_DELAY);
if (refresh_running) {
xSemaphoreGive(gate);
return;
}
beginNoTimer();
if (!pwm_ready || !register_running_dmd(this)) {
releasePWM();
xSemaphoreGive(gate);
return;
}
if (!refresh_timer) {
if (!refresh_task && xTaskCreate(refresh_worker, "dmd_refresh", 4096,
nullptr, 2, &refresh_task) != pdPASS) {
unregister_running_dmd(this);
releasePWM();
xSemaphoreGive(gate);
return;
}
esp_timer_create_args_t args = {};
args.callback = refresh_callback;
args.dispatch_method = ESP_TIMER_TASK;
args.name = "dmd_refresh";
if (esp_timer_create(&args, &refresh_timer) == ESP_OK) {
if (esp_timer_start_periodic(refresh_timer, 2083) != ESP_OK) {
esp_timer_delete(refresh_timer);
refresh_timer = nullptr;
}
}
if (!refresh_timer) {
unregister_running_dmd(this);
releasePWM();
}
}
refresh_running = refresh_timer != nullptr;
xSemaphoreGive(gate);
}
void BaseDMD::end() {
SemaphoreHandle_t gate = refresh_mutex();
if (!gate) return;
xSemaphoreTake(gate, portMAX_DELAY);
bool still_running = unregister_running_dmd(this);
refresh_running = false;
if (!still_running && refresh_timer) {
esp_timer_stop(refresh_timer);
esp_timer_delete(refresh_timer);
refresh_timer = nullptr;
}
// No SPI/virtual scan here: safe during SPIDMD destruction, and no extra
// scan on repeated end(). The mutex waits for any in-flight worker scan.
releasePWM();
clearScreen();
xSemaphoreGive(gate);
}
#endif // ifdef __AVR__
/* Following functions are static non-architecture-specific functions
to manage a global array that tracks all known DMD instances.
This is so a global C code interrupt can call all of the DMDs on
timer interrupt.
The array is grown with realloc. If a DMD is destroyed then it
shrink the array, it just NULLs out the entry (presuming there
isn't a lot of dynamic growth/shrinkage of DMDs within a sketch!)
*/
static volatile BaseDMD **running_dmds = 0;
static volatile int running_dmd_len = 0;
// Add a running_dmd to the list (caller must have disabled interrupts)
static bool register_running_dmd(BaseDMD *dmd)
{
int empty = -1;
for(int i = 0; i < running_dmd_len; i++) {
if(running_dmds[i] == dmd)
return true; // Already running and registered
if(!running_dmds[i])
empty = i; // Found an unused slot in the array
}
if(empty == -1) { // Grow array to fit new entry
running_dmd_len++;
BaseDMD **resized = (BaseDMD **)realloc(running_dmds, sizeof(BaseDMD *)*running_dmd_len);
if(!resized) {
// Allocation failed, bail out
running_dmd_len--;
return false;
}
empty = running_dmd_len-1;
running_dmds = (volatile BaseDMD **)resized;
}
running_dmds[empty] = dmd;
return true;
}
// Null out a running_dmd from the list (caller must have disabled interrupts)
static bool unregister_running_dmd(BaseDMD *dmd)
{
bool still_running = false;
for(int i = 0; i < running_dmd_len; i++) {
if(running_dmds[i] == dmd)
running_dmds[i] = NULL;
else if (running_dmds[i])
still_running = true;
}
return still_running;
}
// ESP8266 ISR Wrapper
#if defined(ESP8266) && !defined(DMD2_ESP8266_COOPERATIVE)
static void inline ICACHE_RAM_ATTR esp8266_ISR_wrapper()
{
if(((int)0x40200000)) { //Make sure flash isn't being accessed.
scan_running_dmds();
}
timer0_write(ESP.getCycleCount() + ESP8266_TIMER0_TICKS);
}
#endif
// This method is called from timer ISR to scan all the DMD instances present in the running sketch
static void inline __attribute__((always_inline)) scan_running_dmds()
{
for(int i = 0; i < running_dmd_len; i++) {
BaseDMD *next = (BaseDMD*)running_dmds[i];
if(next) {
next->scanDisplay();
}
}
}
#endif // ifdef NO_TIMERS