This repository has been archived by the owner on Jan 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dx_Slow_PWM.hpp
386 lines (294 loc) · 13.3 KB
/
Dx_Slow_PWM.hpp
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
/****************************************************************************************************************************
Dx_Slow_PWM.hpp
For Arduino AVRDx-based boards (AVR128Dx, AVR64Dx, AVR32Dx, etc.) using DxCore
Written by Khoi Hoang
Built by Khoi Hoang https://github.com/khoih-prog/Dx_Slow_PWM
Licensed under MIT license
Now even you use all these new 16 ISR-based timers,with their maximum interval practically unlimited (limited only by
unsigned long miliseconds), you just consume only one AVRDx-based timer and avoid conflicting with other cores' tasks.
The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers
Therefore, their executions are not blocked by bad-behaving functions / tasks.
This important feature is absolutely necessary for mission-critical tasks.
Version: 1.0.1
Version Modified By Date Comments
------- ----------- ---------- -----------
1.0.0 K.Hoang 25/08/2022 Initial coding to support AVR Dx (AVR128Dx, AVR64Dx, AVR32Dx, etc.) using DxCore
1.0.1 K.Hoang 25/08/2022 Make MAX_NUMBER_CHANNELS configurable to max 64 PWM channels
*****************************************************************************************************************************/
#pragma once
#ifndef DX_SLOW_PWM_HPP
#define DX_SLOW_PWM_HPP
#if defined(DXCORE)
#define TIMER_INTERRUPT_USING_DX_CORE true
#if !defined(BOARD_NAME)
////////////////////////// __AVR_DA__ //////////////////////////
#if ( defined(__AVR_AVR128DA64__) || defined(__AVR_AVR128DA48__) || defined(__AVR_AVR128DA32__) || defined(__AVR_AVR128DA28__) )
#define BOARD_NAME F("AVR128DA" )
#elif ( defined(__AVR_AVR64DA64__) || defined(__AVR_AVR64DA48__) || defined(__AVR_AVR64DA32__) || defined(__AVR_AVR64DA28__) )
#define BOARD_NAME F("AVR64DA")
#elif ( defined(__AVR_AVR32DA48__) || defined(__AVR_AVR32DA32__) || defined(__AVR_AVR32DA28__) )
#define BOARD_NAME F("AVR32DA")
////////////////////////// __AVR_DB__ //////////////////////////
#elif ( defined(__AVR_AVR128DB64__) || defined(__AVR_AVR128DB48__) || defined(__AVR_AVR128DB32__) || defined(__AVR_AVR128DB28__) )
#define BOARD_NAME F("AVR128DB")
#elif ( defined(__AVR_AVR64DB64__) || defined(__AVR_AVR64DB48__) || defined(__AVR_AVR64DB32__) || defined(__AVR_AVR64DB28__) )
#define BOARD_NAME F("AVR64DB")
#elif ( defined(__AVR_AVR32DB48__) || defined(__AVR_AVR32DB32__) || defined(__AVR_AVR32DB28__) )
#define BOARD_NAME F("AVR32DB")
////////////////////////// __AVR_DD__ //////////////////////////
#elif ( defined(__AVR_AVR64DD32__) || defined(__AVR_AVR64DD28__) || defined(__AVR_AVR64DD20__) || defined(__AVR_AVR64DD14__) )
#define BOARD_NAME F("AVR64DD")
#error AVR64DD not supported yet by the DxCore
#elif ( defined(__AVR_AVR32DD32__) || defined(__AVR_AVR32DD28__) || defined(__AVR_AVR32DD20__) || defined(__AVR_AVR32DD14__) )
#define BOARD_NAME F("AVR32DD")
#error AVR32DD not supported yet by the DxCore
#elif ( defined(__AVR_AVR16DD32__) || defined(__AVR_AVR16DD28__) || defined(__AVR_AVR16DD20__) || defined(__AVR_AVR16DD14__) )
#define BOARD_NAME F("AVR16DD")
#error AVR16DD not supported yet by the DxCore
////////////////////////// __AVR_DU__ //////////////////////////
#elif ( defined(__AVR_AVR64DU32__) || defined(__AVR_AVR64DU28__) )
#define BOARD_NAME F("AVR64DU")
#elif ( defined(__AVR_AVR32DU32__) || defined(__AVR_AVR32DU28__) || defined(__AVR_AVR32DU20__) || defined(__AVR_AVR32DU14__) )
#define BOARD_NAME F("AVR32DU")
#elif ( defined(__AVR_AVR16DU32__) || defined(__AVR_AVR16DU28__) || defined(__AVR_AVR16DU20__) || defined(__AVR_AVR16DU14__) )
#define BOARD_NAME F("AVR16DU")
////////////////////////// __AVR_EA__ //////////////////////////
#elif ( defined(__AVR_AVR64EA48__) || defined(__AVR_AVR64EA32__) || defined(__AVR_AVR64EA28__) )
#define BOARD_NAME F("AVR64EA")
#elif ( defined(__AVR_AVR32EA48__) || defined(__AVR_AVR32EA32__) || defined(__AVR_AVR32EA28__) )
#define BOARD_NAME F("AVR32EA")
#elif ( defined(__AVR_AVR16EA48__) || defined(__AVR_AVR16EA32__) || defined(__AVR_AVR16EA28__) )
#define BOARD_NAME F("AVR16EA")
#elif ( defined(__AVR_AVR8EA48__) || defined(__AVR_AVR8EA32__) || defined(__AVR_AVR8EA28__) )
#define BOARD_NAME F("AVR8EA")
////////////////////////////////////////////////////////////////
#endif
#endif // #if !defined(BOARD_NAME)
#elif defined(MEGATINYCORE)
#define TIMER_INTERRUPT_USING_MEGATINYCORE true
#define BOARD_NAME F("MEGATINYCORE Board")
#error Support for megaTinyCore not ready yet! Please check your Tools->Board setting
#else
#error This is designed only for AVRDx boards using DxCore or megaTinyCore ! Please check your Tools->Board setting
#endif
///////////////////////////////////////////////////////////////////////////////
#ifndef DX_SLOW_PWM_VERSION
#define DX_SLOW_PWM_VERSION F("Dx_Slow_PWM v1.0.1")
#define DX_SLOW_PWM_VERSION_MAJOR 1
#define DX_SLOW_PWM_VERSION_MINOR 0
#define DX_SLOW_PWM_VERSION_PATCH 1
#define DX_SLOW_PWM_VERSION_INT 1000001
#endif
#ifndef _PWM_LOGLEVEL_
#define _PWM_LOGLEVEL_ 1
#endif
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include "Arduino.h"
#include "pins_arduino.h"
#include "PWM_Generic_Debug.h"
#define MAX_COUNT_16BIT 65535UL
typedef void (*timer_callback)();
typedef void (*timer_callback_p)(void *);
// AVRDx28 and AVRDx32 have 3 TCB timers, (TCB0-TCB2)
// AVRDx48 have 4 TCB timers, (TCB0-TCB3)
// AVRDx64 have 5 TCB timers, (TCB0-TCB4)
// Count only TCB0-TCB3
enum
{
HW_TIMER_0 = 0,
HW_TIMER_1,
HW_TIMER_2,
#if ( ( defined(DX_64_PINS) || defined(DX_48_PINS) ) && defined(TCB3) )
HW_TIMER_3,
#endif
#if ( defined(DX_64_PINS) && defined(TCB4) )
HW_TIMER_4,
#endif
NUM_HW_TIMERS
};
#if ( defined(MILLIS_USE_TIMERB0) && USE_TIMER_0)
#error TCB0 has been used by millis()
#elif ( defined(MILLIS_USE_TIMERB1) && USE_TIMER_1)
#error TCB1 has been used by millis()
#elif ( defined(MILLIS_USE_TIMERB2) && USE_TIMER_2)
#error TCB2 has been used by millis()
#elif ( defined(MILLIS_USE_TIMERB3) && USE_TIMER_3)
#error TCB3 has been used by millis()
#elif ( defined(MILLIS_USE_TIMERB4) && USE_TIMER_4)
#error TCB3 has been used by millis()
#endif
class TimerInterrupt
{
private:
bool _timerDone;
int8_t _timer;
uint32_t _CCMPValue;
uint32_t _CCMPValueRemaining;
volatile long _toggle_count;
double _frequency;
void* _callback; // pointer to the callback function
void* _params; // function parameter
void set_CCMP();
public:
TimerInterrupt()
{
_timer = -1;
_frequency = 0;
_callback = NULL;
_params = NULL;
_timerDone = false;
_CCMPValue = 0;
_CCMPValueRemaining = 0;
_toggle_count = -1;
};
explicit TimerInterrupt(const uint8_t& timerNo)
{
_timer = timerNo;
_frequency = 0;
_callback = NULL;
_params = NULL;
_timerDone = false;
_CCMPValue = 0;
_CCMPValueRemaining = 0;
_toggle_count = -1;
};
void callback() __attribute__((always_inline))
{
if (_callback != NULL)
{
if (_params != NULL)
(*(timer_callback_p)_callback)(_params);
else
(*(timer_callback)_callback)();
}
}
void init(const int8_t& timer);
void init()
{
init(_timer);
};
// frequency (in hertz) and duration (in milliseconds). Duration = 0 or not specified => run indefinitely
bool setFrequency(const float& frequency, timer_callback_p callback, const uint32_t& params, const unsigned long& duration = 0);
// frequency (in hertz) and duration (in milliseconds). Duration = 0 or not specified => run indefinitely
bool setFrequency(const float& frequency, timer_callback callback, const unsigned long& duration = 0)
{
return setFrequency(frequency, reinterpret_cast<timer_callback_p>(callback), /*NULL*/ 0, duration);
}
// interval (in ms) and duration (in milliseconds). Duration = 0 or not specified => run indefinitely
template<typename TArg>
bool setInterval(const unsigned long& interval, void (*callback)(TArg), const TArg& params, const unsigned long& duration = 0)
{
static_assert(sizeof(TArg) <= sizeof(uint32_t), "setInterval() callback argument size must be <= 4 bytes");
return setFrequency((float) (1000.0f / interval), reinterpret_cast<timer_callback_p>(callback), (uint32_t) params, duration);
}
// interval (in ms) and duration (in milliseconds). Duration = 0 or not specified => run indefinitely
bool setInterval(const unsigned long& interval, timer_callback callback, const unsigned long& duration = 0)
{
return setFrequency((float) (1000.0f / interval), reinterpret_cast<timer_callback_p>(callback), /*NULL*/ 0, duration);
}
template<typename TArg>
bool attachInterrupt(const float& frequency, void (*callback)(TArg), const TArg& params, const unsigned long& duration = 0)
{
static_assert(sizeof(TArg) <= sizeof(uint32_t), "attachInterrupt() callback argument size must be <= 4 bytes");
return setFrequency(frequency, reinterpret_cast<timer_callback_p>(callback), (uint32_t) params, duration);
}
bool attachInterrupt(const float& frequency, timer_callback callback, const unsigned long& duration = 0)
{
return setFrequency(frequency, reinterpret_cast<timer_callback_p>(callback), /*NULL*/ 0, duration);
}
// Interval (in ms) and duration (in milliseconds). Duration = 0 or not specified => run indefinitely
template<typename TArg>
bool attachInterruptInterval(const unsigned long& interval, void (*callback)(TArg), const TArg& params, const unsigned long& duration = 0)
{
static_assert(sizeof(TArg) <= sizeof(uint32_t), "attachInterruptInterval() callback argument size must be <= 4 bytes");
return setFrequency( (float) ( 1000.0f / interval), reinterpret_cast<timer_callback_p>(callback), (uint32_t) params, duration);
}
// Interval (in ms) and duration (in milliseconds). Duration = 0 or not specified => run indefinitely
bool attachInterruptInterval(const unsigned long& interval, timer_callback callback, const unsigned long& duration = 0)
{
return setFrequency( (float) ( 1000.0f / interval), reinterpret_cast<timer_callback_p> (callback), /*NULL*/ 0, duration);
}
void detachInterrupt();
void disableTimer()
{
detachInterrupt();
}
// Duration (in milliseconds). Duration = 0 or not specified => run indefinitely
void reattachInterrupt(const unsigned long& duration = 0);
// Duration (in milliseconds). Duration = 0 or not specified => run indefinitely
void enableTimer(const unsigned long& duration = 0) __attribute__((always_inline))
{
reattachInterrupt(duration);
}
// Just stop clock source, still keep the count
void pauseTimer();
// Just reconnect clock source, continue from the current count
void resumeTimer();
// Just stop clock source, clear the count
void stopTimer()
{
detachInterrupt();
}
// Just reconnect clock source, start current count from 0
void restartTimer(const unsigned long& duration = 0)
{
reattachInterrupt(duration);
}
int8_t getTimer() __attribute__((always_inline))
{
return _timer;
};
long getCount() __attribute__((always_inline))
{
return _toggle_count;
};
void setCount(const long& countInput) __attribute__((always_inline))
{
//noInterrupts();
_toggle_count = countInput;
//interrupts();
};
uint32_t /*long*/ get_CCMPValue() __attribute__((always_inline))
{
return _CCMPValue;
};
uint32_t /*long*/ get_CCMPValueRemaining() __attribute__((always_inline))
{
return _CCMPValueRemaining;
};
void adjust_CCMPValue() //__attribute__((always_inline))
{
noInterrupts();
if (_CCMPValueRemaining < MAX_COUNT_16BIT)
{
set_CCMP();
}
interrupts();
_CCMPValueRemaining -= min(MAX_COUNT_16BIT, _CCMPValueRemaining);
if (_CCMPValueRemaining <= 0)
{
// Reset value for next cycle
_CCMPValueRemaining = _CCMPValue;
PWM_LOGDEBUG1(F("adjust_CCMPValue: reset _CCMPValueRemaining = "), _CCMPValue);
_timerDone = true;
}
else
_timerDone = false;
};
void reload_CCMPValue() //__attribute__((always_inline))
{
noInterrupts();
// Reset value for next cycle, have to deduct the value already loaded to CCMP register
_CCMPValueRemaining = _CCMPValue;
set_CCMP();
_timerDone = false;
interrupts();
};
bool checkTimerDone() //__attribute__((always_inline))
{
return _timerDone;
};
}; // class TimerInterrupt
//////////////////////////////////////////////
#endif // DX_SLOW_PWM_HPP