This repository was archived by the owner on Jan 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathTeensyTimerInterrupt_Generic.h
1284 lines (998 loc) · 36.4 KB
/
TeensyTimerInterrupt_Generic.h
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************************************************************
TeensyTimerInterrupt.h
For Teensy boards
Written by Khoi Hoang
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 Hardware 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.
Based on SimpleTimer - A timer library for Arduino.
Author: mromani@ottotecnica.com
Copyright (c) 2010 OTTOTECNICA Italy
Based on BlynkTimer.h
Author: Volodymyr Shymanskyy
Built by Khoi Hoang https://github.com/khoih-prog/TimerInterrupt_Generic
Licensed under MIT license
Version: 1.13.0
Version Modified By Date Comments
------- ----------- ---------- -----------
1.1.0 K Hoang 10/11/2020 Initial Super-Library coding to merge all TimerInterrupt Libraries
1.2.0 K Hoang 12/11/2020 Add STM32_TimerInterrupt Library
1.3.0 K Hoang 01/12/2020 Add Mbed Mano-33-BLE Library. Add support to AVR UNO, Nano, Arduino Mini, Ethernet, BT. etc.
1.3.1 K.Hoang 09/12/2020 Add complex examples and board Version String. Fix SAMD bug.
1.3.2 K.Hoang 06/01/2021 Fix warnings. Optimize examples to reduce memory usage
1.4.0 K.Hoang 02/04/2021 Add support to Arduino, Adafruit, Sparkfun AVR 32u4, 328P, 128128RFA1 and Sparkfun SAMD
1.5.0 K.Hoang 17/04/2021 Add support to Arduino megaAVR ATmega4809-based boards (Nano Every, UNO WiFi Rev2, etc.)
1.6.0 K.Hoang 15/06/2021 Add T3/T4 support to 32u4. Add support to RP2040, ESP32-S2
1.7.0 K.Hoang 13/08/2021 Add support to Adafruit nRF52 core v0.22.0+
1.8.0 K.Hoang 24/11/2021 Update to use latest TimerInterrupt Libraries' versions
1.9.0 K.Hoang 09/05/2022 Update to use latest TimerInterrupt Libraries' versions
1.10.0 K.Hoang 10/08/2022 Update to use latest ESP32_New_TimerInterrupt Library version
1.11.0 K.Hoang 12/08/2022 Add support to new ESP32_C3, ESP32_S2 and ESP32_S3 boards
1.12.0 K.Hoang 29/09/2022 Update for SAMD, RP2040, MBED_RP2040
1.13.0 K.Hoang 16/11/2022 Fix doubled time for ESP32_C3,S2 and S3. Fix poor timer accuracy bug for MBED RP2040
Fix bug disabling TCB0 for megaAVR
*****************************************************************************************************************************/
#pragma once
#ifndef TEENSYTIMERINTERRUPT_H
#define TEENSYTIMERINTERRUPT_H
#if !( defined(CORE_TEENSY) || defined(TEENSYDUINO) )
#error This code is designed to run on Teensy platform! Please check your Tools->Board setting.
#endif
#ifndef TEENSY_TIMER_INTERRUPT_VERSION
#define TEENSY_TIMER_INTERRUPT_VERSION "TeensyTimerInterrupt v1.3.0"
#define TEENSY_TIMER_INTERRUPT_VERSION_MAJOR 1
#define TEENSY_TIMER_INTERRUPT_VERSION_MINOR 3
#define TEENSY_TIMER_INTERRUPT_VERSION_PATCH 0
#define TEENSY_TIMER_INTERRUPT_VERSION_INT 1003000
#endif
#ifndef TIMER_INTERRUPT_DEBUG
#define TIMER_INTERRUPT_DEBUG 0
#endif
#include "TimerInterrupt_Generic_Debug.h"
//////////////////////////////////////////////////////////
class TeensyTimerInterrupt;
typedef enum
{
TEENSY_TIMER_1 = 0,
TEENSY_TIMER_3 = 1,
TEENSY_MAX_TIMER
} TeensyTimerNumber;
typedef TeensyTimerInterrupt TeensyTimer;
typedef void (*timerCallback) ();
//////////////////////////////////////////////////////////
#if ( defined(__arm__) && defined(TEENSYDUINO) && defined(__IMXRT1062__) )
// For Teensy 4.0/4.1
#if defined(ARDUINO_TEENSY41)
#if(_TIMERINTERRUPT_LOGLEVEL_>3)
#warning Using Teensy 4.1
#endif
#ifndef BOARD_NAME
#define BOARD_NAME "Teensy 4.1"
#endif
#else
#if(_TIMERINTERRUPT_LOGLEVEL_>3)
#warning Using Teensy 4.0
#endif
#ifndef BOARD_NAME
#define BOARD_NAME "Teensy 4.0"
#endif
#endif
class TeensyTimerInterrupt;
static void ext_isr();
// Ony for Teensy 4.0/4.1
static IRQ_NUMBER_t teensy_timers_irq [TEENSY_MAX_TIMER] = { IRQ_FLEXPWM1_3, IRQ_FLEXPWM2_2 };
static TeensyTimerInterrupt* TeensyTimers [TEENSY_MAX_TIMER] = { NULL, NULL };
//////////////////////////////////////////////////////////
class TeensyTimerInterrupt
{
private:
// properties
//static unsigned short pwmPeriod;
//static unsigned char clockSelectBits;
uint8_t _timer = TEENSY_TIMER_1;
IRQ_NUMBER_t _timer_IRQ;
timerCallback _callback; // pointer to the callback function
float _frequency; // Timer frequency
uint32_t _timerCount; // count to activate timer
uint32_t _prescale = 0;
uint32_t _realPeriod;
public:
TeensyTimerInterrupt(uint8_t timer = TEENSY_TIMER_1) __attribute__((always_inline))
{
if (timer < TEENSY_MAX_TIMER)
_timer = timer;
else
{
// Error out of range, force to use TEENSY_TIMER_1
_timer = TEENSY_TIMER_1;
}
_timer_IRQ = teensy_timers_irq[_timer];
// Update to use in IRQHandler
TeensyTimers[_timer] = this;
_callback = NULL;
}
//////////////////////////////////////////////////////////
~TeensyTimerInterrupt() __attribute__((always_inline))
{
TeensyTimers[_timer] = NULL;
}
//////////////////////////////////////////////////////////
// frequency (in hertz) and duration (in milliseconds). Duration = 0 or not specified => run indefinitely
// No params and duration now. To be addes in the future by adding similar functions here or to NRF52-hal-timer.c
bool setFrequency(const float& frequency, timerCallback callback) __attribute__((always_inline))
{
return setInterval((float) (1000000.0f / frequency), callback);
}
//////////////////////////////////////////////////////////
// Interval (in microseconds)
// For Teensy 4.0/4.1, F_BUS_ACTUAL = 150 MHz => max interval/period is only 55922 us (~17.9 Hz)
bool setInterval(const unsigned long& interval, timerCallback callback) __attribute__((always_inline))
{
// This function will be called when time out interrupt will occur
if (callback)
{
_callback = callback;
}
else
{
TISR_LOGERROR(F("TeensyTimerInterrupt: ERROR: NULL callback function pointer."));
return false;
}
uint32_t period = ((float) F_BUS_ACTUAL / 2000000) * (float) interval;
uint32_t prescale = 0;
while (period > 32767)
{
period = period >> 1;
if (++prescale > 7)
{
prescale = 7;
period = 32767;
break;
}
}
// when F_BUS is 150 MHz, longest period (_realPeriod) is 55922.3467 us (~17.881939 Hz)
// 55922.3467 us = (32767 * 2000000) / (150000000 >> 7)
_realPeriod = (uint32_t) ((period * 2000000.0f) / (F_BUS_ACTUAL >> prescale));
_prescale = prescale;
_timerCount = period;
if (_timer == TEENSY_TIMER_1)
{
TISR_LOGWARN1(F("TEENSY_TIMER_1: , F_BUS_ACTUAL (MHz) ="), F_BUS_ACTUAL / 1000000);
}
else if (_timer == TEENSY_TIMER_3)
{
TISR_LOGWARN1(F("TEENSY_TIMER_3: , F_BUS_ACTUAL (MHz) ="), F_BUS_ACTUAL / 1000000);
}
TISR_LOGWARN3(F("Request interval ="), interval, F(", actual interval (us) ="), _realPeriod);
TISR_LOGWARN3(F("Prescale ="), _prescale, F(", _timerCount ="), _timerCount);
if (_timer == TEENSY_TIMER_1)
{
///////////// TEENSY_TIMER_1 code ////////////////////////
FLEXPWM1_FCTRL0 |= FLEXPWM_FCTRL0_FLVL(8); // logic high = fault
FLEXPWM1_FSTS0 = 0x0008; // clear fault status
FLEXPWM1_MCTRL |= FLEXPWM_MCTRL_CLDOK(8);
FLEXPWM1_SM3CTRL2 = FLEXPWM_SMCTRL2_INDEP;
FLEXPWM1_SM3CTRL = FLEXPWM_SMCTRL_HALF | FLEXPWM_SMCTRL_PRSC(prescale);
FLEXPWM1_SM3INIT = -period;
FLEXPWM1_SM3VAL0 = 0;
FLEXPWM1_SM3VAL1 = period;
FLEXPWM1_SM3VAL2 = 0;
FLEXPWM1_SM3VAL3 = 0;
FLEXPWM1_SM3VAL4 = 0;
FLEXPWM1_SM3VAL5 = 0;
FLEXPWM1_MCTRL |= FLEXPWM_MCTRL_LDOK(8) | FLEXPWM_MCTRL_RUN(8);
}
else if (_timer == TEENSY_TIMER_3)
{
///////////// TEENSY_TIMER_3 code ////////////////////////
FLEXPWM2_FCTRL0 |= FLEXPWM_FCTRL0_FLVL(4); // logic high = fault
FLEXPWM2_FSTS0 = 0x0008; // clear fault status
FLEXPWM2_MCTRL |= FLEXPWM_MCTRL_CLDOK(4);
FLEXPWM2_SM2CTRL2 = FLEXPWM_SMCTRL2_INDEP;
FLEXPWM2_SM2CTRL = FLEXPWM_SMCTRL_HALF | FLEXPWM_SMCTRL_PRSC(prescale);
FLEXPWM2_SM2INIT = -period;
FLEXPWM2_SM2VAL0 = 0;
FLEXPWM2_SM2VAL1 = period;
FLEXPWM2_SM2VAL2 = 0;
FLEXPWM2_SM2VAL3 = 0;
FLEXPWM2_SM2VAL4 = 0;
FLEXPWM2_SM2VAL5 = 0;
FLEXPWM2_MCTRL |= FLEXPWM_MCTRL_LDOK(4) | FLEXPWM_MCTRL_RUN(4);
}
attachInterruptVector(_timer_IRQ, &ext_isr);
if (_timer == TEENSY_TIMER_1)
{
///////////// TEENSY_TIMER_1 code ////////////////////////
FLEXPWM1_SM3STS = FLEXPWM_SMSTS_RF;
FLEXPWM1_SM3INTEN = FLEXPWM_SMINTEN_RIE;
}
else if (_timer == TEENSY_TIMER_3)
{
///////////// TEENSY_TIMER_3 code ////////////////////////
FLEXPWM2_SM2STS = FLEXPWM_SMSTS_RF;
FLEXPWM2_SM2INTEN = FLEXPWM_SMINTEN_RIE;
}
NVIC_ENABLE_IRQ(_timer_IRQ);
return true;
}
//////////////////////////////////////////////////////////
bool attachInterrupt(const float& frequency, timerCallback callback) __attribute__((always_inline))
{
return setInterval((float) (1000000.0f / frequency), callback);
}
//////////////////////////////////////////////////////////
// interval (in microseconds) and duration (in milliseconds). Duration = 0 or not specified => run indefinitely
// No params and duration now. To be addes in the future by adding similar functions here or to NRF52-hal-timer.c
bool attachInterruptInterval(const unsigned long& interval, timerCallback callback) __attribute__((always_inline))
{
return setInterval(interval, callback);
}
//////////////////////////////////////////////////////////
void detachInterrupt() __attribute__((always_inline))
{
NVIC_DISABLE_IRQ(_timer_IRQ);
if (_timer == TEENSY_TIMER_1)
{
///////////// TEENSY_TIMER_1 code ////////////////////////
FLEXPWM1_SM3INTEN = 0;
}
else if (_timer == TEENSY_TIMER_3)
{
///////////// TEENSY_TIMER_3 code ////////////////////////
FLEXPWM2_SM2INTEN = 0;
}
}
//////////////////////////////////////////////////////////
void disableTimer() __attribute__((always_inline))
{
detachInterrupt();
}
//////////////////////////////////////////////////////////
//****************************
// Run Control
//****************************
void startTimer() __attribute__((always_inline)) __attribute__((always_inline))
{
stopTimer();
resumeTimer();
}
//////////////////////////////////////////////////////////
void stopTimer() __attribute__((always_inline))
{
if (_timer == TEENSY_TIMER_1)
{
TISR_LOGWARN(F("TeensyTimerInterrupt:stopTimer TEENSY_TIMER_1"));
///////////// TEENSY_TIMER_1 code ////////////////////////
FLEXPWM1_MCTRL &= ~FLEXPWM_MCTRL_RUN(8);
}
else if (_timer == TEENSY_TIMER_3)
{
TISR_LOGWARN(F("TeensyTimerInterrupt:stopTimer TEENSY_TIMER_3"));
///////////// TEENSY_TIMER_3 code ////////////////////////
FLEXPWM2_MCTRL &= ~FLEXPWM_MCTRL_RUN(4);
}
}
//////////////////////////////////////////////////////////
void restartTimer() __attribute__((always_inline))
{
startTimer();
}
//////////////////////////////////////////////////////////
void resumeTimer() __attribute__((always_inline))
{
if (_timer == TEENSY_TIMER_1)
{
TISR_LOGWARN(F("TeensyTimerInterrupt:resumeTimer TEENSY_TIMER_1"));
///////////// TEENSY_TIMER_1 code ////////////////////////
FLEXPWM1_MCTRL |= FLEXPWM_MCTRL_RUN(8);
}
else if (_timer == TEENSY_TIMER_3)
{
TISR_LOGWARN(F("TeensyTimerInterrupt:resumeTimer TEENSY_TIMER_3"));
///////////// TEENSY_TIMER_3 code ////////////////////////
FLEXPWM2_MCTRL |= FLEXPWM_MCTRL_RUN(4);
}
}
//////////////////////////////////////////////////////////
uint32_t getPeriod() __attribute__((always_inline))
{
return _timerCount;
}
//////////////////////////////////////////////////////////
uint32_t getPrescale() __attribute__((always_inline))
{
return _prescale;
}
//////////////////////////////////////////////////////////
// Real (actual) period in us
uint32_t getRealPeriod() __attribute__((always_inline))
{
return _realPeriod;
}
//////////////////////////////////////////////////////////
timerCallback getCallback() __attribute__((always_inline))
{
return _callback;
}
//////////////////////////////////////////////////////////
IRQ_NUMBER_t getTimerIRQn() __attribute__((always_inline))
{
return _timer_IRQ;
}
};
//////////////////////////////////////////////////////////
static void ext_isr()
{
if (TeensyTimers[TEENSY_TIMER_1])
{
///////////// TEENSY_TIMER_1 code ////////////////////////
FLEXPWM1_SM3STS = FLEXPWM_SMSTS_RF;
(*(TeensyTimers[TEENSY_TIMER_1]->getCallback()))();
}
else if (TeensyTimers[TEENSY_TIMER_3])
{
///////////// TEENSY_TIMER_3 code ////////////////////////
FLEXPWM2_SM2STS = FLEXPWM_SMSTS_RF;
(*(TeensyTimers[TEENSY_TIMER_3]->getCallback()))();
}
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// For Teensy 3.x and LC
#elif defined(__arm__) && defined(TEENSYDUINO) && (defined(KINETISK) || defined(KINETISL))
#ifdef BOARD_NAME
#undef BOARD_NAME
#endif
#if defined(__MK66FX1M0__)
// For Teensy 3.6
#if(_TIMERINTERRUPT_LOGLEVEL_>3)
#warning Using Teensy 3.6
#endif
#define BOARD_NAME "Teensy 3.6"
#elif defined(__MK64FX512__)
// For Teensy 3.5
#if(_TIMERINTERRUPT_LOGLEVEL_>3)
#warning Using Teensy 3.5
#endif
#define BOARD_NAME "Teensy 3.5"
#elif defined(__MK20DX256__)
// For Teensy 3.2/3.1
#if(_TIMERINTERRUPT_LOGLEVEL_>3)
#warning Using Teensy 3.2/3.1
#endif
#define BOARD_NAME "Teensy 3.2/3.1"
#elif defined(__MK20DX128__)
// For Teensy 3.0
#if(_TIMERINTERRUPT_LOGLEVEL_>3)
#warning Using Teensy 3.0
#endif
#define BOARD_NAME "Teensy 3.0"
#elif defined(__MKL26Z64__)
// For Teensy LC
#if(_TIMERINTERRUPT_LOGLEVEL_>3)
#warning Using Teensy LC
#endif
#define BOARD_NAME "Teensy LC"
#endif
#if defined(KINETISK)
#define F_TIMER F_BUS
#elif defined(KINETISL)
#define F_TIMER (F_PLL/2)
#endif
// Teensy 3.0 has only TEENSY_TIMER_1 and IRQ_FTM1, So force to use TEENSY_TIMER_1 and IRQ_FTM1
#if defined(IRQ_FTM2)
static IRQ_NUMBER_t teensy_timers_irq [TEENSY_MAX_TIMER] = { IRQ_FTM1, IRQ_FTM2 };
#else
static IRQ_NUMBER_t teensy_timers_irq [TEENSY_MAX_TIMER] = { IRQ_FTM1, IRQ_FTM1 };
#endif
static TeensyTimerInterrupt* TeensyTimers [TEENSY_MAX_TIMER] = { NULL, NULL };
//////////////////////////////////////////////////////////
class TeensyTimerInterrupt
{
// Use only 15 bit resolution. From K66 reference manual, 45.5.7 page 1200:
// The CPWM pulse width (duty cycle) is determined by 2 x (CnV - CNTIN) and the
// period is determined by 2 x (MOD - CNTIN). See the following figure. MOD must be
// kept in the range of 0x0001 to 0x7FFF because values outside this range can produce
// ambiguous results.
#undef TIMER_RESOLUTION
#define TIMER_RESOLUTION 32768
private:
uint8_t _timer = TEENSY_TIMER_1;
IRQ_NUMBER_t _timer_IRQ;
timerCallback _callback; // pointer to the callback function
float _frequency; // Timer frequency
uint32_t _timerCount; // count to activate timer
uint32_t _prescale = 0;
uint32_t _realPeriod;
public:
TeensyTimerInterrupt(uint8_t timer = TEENSY_TIMER_1) __attribute__((always_inline))
{
#if defined(IRQ_FTM2)
if (timer < TEENSY_MAX_TIMER)
_timer = timer;
else
{
// Error out of range, force to use TEENSY_TIMER_1
_timer = TEENSY_TIMER_1;
}
#else
// Teensy 3.0 has only TEENSY_TIMER_1 and IRQ_FTM1, So force to use TEENSY_TIMER_1 and IRQ_FTM1
_timer = TEENSY_TIMER_1;
#endif
_timer_IRQ = teensy_timers_irq[_timer];
// Update to use in IRQHandler
TeensyTimers[_timer] = this;
_callback = NULL;
}
//////////////////////////////////////////////////////////
~TeensyTimerInterrupt() __attribute__((always_inline))
{
TeensyTimers[_timer] = NULL;
}
//////////////////////////////////////////////////////////
// frequency (in hertz) and duration (in milliseconds). Duration = 0 or not specified => run indefinitely
// No params and duration now. To be addes in the future by adding similar functions here or to NRF52-hal-timer.c
bool setFrequency(const float& frequency, timerCallback callback) __attribute__((always_inline))
{
return setInterval((float) (1000000.0f / frequency), callback);
}
//////////////////////////////////////////////////////////
// Interval (in microseconds)
bool setInterval(const unsigned long& interval, timerCallback callback) __attribute__((always_inline))
{
// This function will be called when time out interrupt will occur
if (callback)
{
_callback = callback;
}
else
{
TISR_LOGERROR(F("TeensyTimerInterrupt: ERROR: NULL callback function pointer."));
return false;
}
uint32_t period = ((float) F_TIMER / 2000000) * (float) interval;
uint32_t prescale = 0;
#if 1
while (period >= TIMER_RESOLUTION )
{
period = period >> 1;
if (++prescale > 7)
{
prescale = 7;
period = (TIMER_RESOLUTION - 1);
break;
}
}
#else
if (period < TIMER_RESOLUTION)
{
prescale = 0;
}
else if (period < TIMER_RESOLUTION * 2)
{
prescale = 1;
period = period >> 1;
}
else if (period < TIMER_RESOLUTION * 4)
{
prescale = 2;
period = period >> 2;
}
else if (period < TIMER_RESOLUTION * 8)
{
prescale = 3;
period = period >> 3;
}
else if (period < TIMER_RESOLUTION * 16)
{
prescale = 4;
period = period >> 4;
}
else if (period < TIMER_RESOLUTION * 32)
{
prescale = 5;
period = period >> 5;
}
else if (period < TIMER_RESOLUTION * 64)
{
prescale = 6;
period = period >> 6;
}
else if (period < TIMER_RESOLUTION * 128)
{
prescale = 7;
period = period >> 7;
}
else
{
prescale = 7;
period = TIMER_RESOLUTION - 1;
}
#endif
_realPeriod = (uint32_t) ((period * 2000000.0f) / (F_TIMER >> prescale));
_prescale = prescale;
_timerCount = period;
if (_timer == TEENSY_TIMER_1)
{
TISR_LOGWARN1(F("TEENSY_TIMER_1: , F_TIMER (MHz) ="), F_TIMER / 1000000);
}
else if (_timer == TEENSY_TIMER_3)
{
TISR_LOGWARN1(F("TEENSY_TIMER_3: , F_TIMER (MHz) ="), F_TIMER / 1000000);
}
TISR_LOGWARN3(F("Request interval ="), interval, F(", actual interval (us) ="), _realPeriod);
TISR_LOGWARN3(F("Prescale ="), _prescale, F(", _timerCount ="), _timerCount);
if (_timer == TEENSY_TIMER_1)
{
///////////// TEENSY_TIMER_1 code ////////////////////////
uint32_t sc = FTM1_SC;
FTM1_SC = 0;
FTM1_MOD = _realPeriod;
FTM1_SC = FTM_SC_CLKS(1) | FTM_SC_CPWMS | _prescale | (sc & FTM_SC_TOIE);
attachInterruptVector(_timer_IRQ, &ftm1_isr);
FTM1_SC |= FTM_SC_TOIE;
}
else if (_timer == TEENSY_TIMER_3)
{
///////////// TEENSY_TIMER_3 code ////////////////////////
uint32_t sc = FTM2_SC;
FTM2_SC = 0;
FTM2_MOD = _realPeriod;
FTM2_SC = FTM_SC_CLKS(1) | FTM_SC_CPWMS | _prescale | (sc & FTM_SC_TOIE);
attachInterruptVector(_timer_IRQ, &ftm2_isr);
FTM2_SC |= FTM_SC_TOIE;
}
NVIC_ENABLE_IRQ(_timer_IRQ);
return true;
}
//////////////////////////////////////////////////////////
bool attachInterrupt(const float& frequency, timerCallback callback) __attribute__((always_inline))
{
return setInterval((float) (1000000.0f / frequency), callback);
}
//////////////////////////////////////////////////////////
// interval (in microseconds) and duration (in milliseconds). Duration = 0 or not specified => run indefinitely
// No params and duration now. To be added in the future by adding similar functions here or to NRF52-hal-timer.c
bool attachInterruptInterval(const unsigned long& interval, timerCallback callback) __attribute__((always_inline))
{
return setInterval(interval, callback);
}
//////////////////////////////////////////////////////////
void detachInterrupt() __attribute__((always_inline))
{
NVIC_DISABLE_IRQ(_timer_IRQ);
if (_timer == TEENSY_TIMER_1)
{
///////////// TEENSY_TIMER_1 code ////////////////////////
FTM1_SC &= ~FTM_SC_TOIE;
}
else if (_timer == TEENSY_TIMER_3)
{
///////////// TEENSY_TIMER_3 code ////////////////////////
FTM2_SC &= ~FTM_SC_TOIE;
}
NVIC_DISABLE_IRQ(_timer_IRQ);
}
//////////////////////////////////////////////////////////
void disableTimer() __attribute__((always_inline))
{
detachInterrupt();
}
//////////////////////////////////////////////////////////
//****************************
// Run Control
//****************************
void startTimer() __attribute__((always_inline))
{
stopTimer();
if (_timer == TEENSY_TIMER_1)
{
TISR_LOGWARN(F("TeensyTimerInterrupt:startTimer TEENSY_TIMER_1"));
///////////// TEENSY_TIMER_1 code ////////////////////////
FTM1_CNT = 0;
}
else if (_timer == TEENSY_TIMER_3)
{
TISR_LOGWARN(F("TeensyTimerInterrupt:startTimer TEENSY_TIMER_3"));
///////////// TEENSY_TIMER_3 code ////////////////////////
FTM2_CNT = 0;
}
resumeTimer();
}
//////////////////////////////////////////////////////////
void stopTimer() __attribute__((always_inline))
{
if (_timer == TEENSY_TIMER_1)
{
TISR_LOGWARN(F("TeensyTimerInterrupt:stopTimer TEENSY_TIMER_1"));
///////////// TEENSY_TIMER_1 code ////////////////////////
FTM1_SC = FTM1_SC & (FTM_SC_TOIE | FTM_SC_CPWMS | FTM_SC_PS(7));
}
else if (_timer == TEENSY_TIMER_3)
{
TISR_LOGWARN(F("TeensyTimerInterrupt:stopTimer TEENSY_TIMER_3"));
///////////// TEENSY_TIMER_3 code ////////////////////////
FTM2_SC = FTM2_SC & (FTM_SC_TOIE | FTM_SC_CPWMS | FTM_SC_PS(7));
}
}
//////////////////////////////////////////////////////////
void restartTimer() __attribute__((always_inline))
{
startTimer();
}
//////////////////////////////////////////////////////////
void resumeTimer() __attribute__((always_inline))
{
if (_timer == TEENSY_TIMER_1)
{
TISR_LOGWARN(F("TeensyTimerInterrupt:resumeTimer TEENSY_TIMER_1"));
///////////// TEENSY_TIMER_1 code ////////////////////////
FTM1_SC = (FTM1_SC & (FTM_SC_TOIE | FTM_SC_PS(7))) | FTM_SC_CPWMS | FTM_SC_CLKS(1);
}
else if (_timer == TEENSY_TIMER_3)
{
TISR_LOGWARN(F("TeensyTimerInterrupt:resumeTimer TEENSY_TIMER_3"));
///////////// TEENSY_TIMER_3 code ////////////////////////
FTM2_SC = (FTM2_SC & (FTM_SC_TOIE | FTM_SC_PS(7))) | FTM_SC_CPWMS | FTM_SC_CLKS(1);
}
}
//////////////////////////////////////////////////////////
uint32_t getPeriod() __attribute__((always_inline))
{
return _timerCount;
}
//////////////////////////////////////////////////////////
uint32_t getPrescale() __attribute__((always_inline))
{
return _prescale;
}
//////////////////////////////////////////////////////////
// Real (actual) period in us
uint32_t getRealPeriod() __attribute__((always_inline))
{
return _realPeriod;
}
//////////////////////////////////////////////////////////
timerCallback getCallback() __attribute__((always_inline))
{
return _callback;
}
//////////////////////////////////////////////////////////
IRQ_NUMBER_t getTimerIRQn() __attribute__((always_inline))
{
return _timer_IRQ;
}
//////////////////////////////////////////////////////////
};
//////////////////////////////////////////////////////////
void ftm1_isr()
{
uint32_t sc = FTM1_SC;
#ifdef KINETISL
if (sc & 0x80)
FTM1_SC = sc;
#else
if (sc & 0x80)
FTM1_SC = sc & 0x7F;
#endif
(*(TeensyTimers[TEENSY_TIMER_1]->getCallback()))();
}
//////////////////////////////////////////////////////////
void ftm2_isr()
{
uint32_t sc = FTM2_SC;
#ifdef KINETISL
if (sc & 0x80)
FTM2_SC = sc;
#else
if (sc & 0x80)
FTM2_SC = sc & 0x7F;
#endif
(*(TeensyTimers[TEENSY_TIMER_3]->getCallback()))();
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// For Teensy 2.0 and Teensy++ 2.0
#elif ( defined(ARDUINO_ARCH_AVR) || defined(__AVR__) )
#ifdef BOARD_NAME
#undef BOARD_NAME
#endif
// For Teensy 2.0 and Teensy++ 2.0
#if(_TIMERINTERRUPT_LOGLEVEL_>3)
#warning Using Teensy 2.0 or Teensy++ 2.0
#endif
#define BOARD_NAME "Teensy 2.0 or Teensy++ 2.0"
#if defined(KINETISK)
#define F_TIMER F_BUS
#elif defined(KINETISL)
#define F_TIMER (F_PLL/2)
#endif
#define TIMER_RESOLUTION 65536UL // Timer1 and Timer 3 are 16 bit timers
static TeensyTimerInterrupt* TeensyTimers [TEENSY_MAX_TIMER] = { NULL, NULL };
//////////////////////////////////////////////////////////
class TeensyTimerInterrupt
{
// Use only 15 bit resolution. From K66 reference manual, 45.5.7 page 1200:
// The CPWM pulse width (duty cycle) is determined by 2 x (CnV - CNTIN) and the
// period is determined by 2 x (MOD - CNTIN). See the following figure. MOD must be
// kept in the range of 0x0001 to 0x7FFF because values outside this range can produce
// ambiguous results.
#undef TIMER_RESOLUTION
#define TIMER_RESOLUTION 32768
private:
uint8_t _timer = TEENSY_TIMER_1;
timerCallback _callback; // pointer to the callback function
float _frequency; // Timer frequency
uint32_t _timerCount; // count to activate timer
uint32_t _prescale = 0;
uint32_t _realPeriod;
public:
TeensyTimerInterrupt(uint8_t timer = TEENSY_TIMER_1) __attribute__((always_inline))
{
if (timer < TEENSY_MAX_TIMER)
_timer = timer;
else
{
// Error out of range, force to use TEENSY_TIMER_1
_timer = TEENSY_TIMER_1;
}
// Update to use in IRQHandler
TeensyTimers[_timer] = this;
_callback = NULL;
}
//////////////////////////////////////////////////////////
~TeensyTimerInterrupt() __attribute__((always_inline))
{
TeensyTimers[_timer] = NULL;
}
//////////////////////////////////////////////////////////
// frequency (in hertz) and duration (in milliseconds). Duration = 0 or not specified => run indefinitely
// No params and duration now. To be added in the future by adding similar functions here or to NRF52-hal-timer.c
bool setFrequency(const float& frequency, timerCallback callback) __attribute__((always_inline))
{
return setInterval((float) (1000000.0f / frequency), callback);
}
//////////////////////////////////////////////////////////
// Interval (in microseconds)
bool setInterval(const unsigned long& interval, timerCallback callback) __attribute__((always_inline))
{
// This function will be called when time out interrupt will occur
if (callback)
{
_callback = callback;
}
else
{
TISR_LOGERROR(F("TeensyTimerInterrupt: ERROR: NULL callback function pointer."));