-
Notifications
You must be signed in to change notification settings - Fork 6
/
DMAChannel.h
1075 lines (949 loc) · 33.3 KB
/
DMAChannel.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
#ifndef DMAChannel_h_
#define DMAChannel_h_
#include "kinetis.h"
// Discussion about DMAChannel is here:
// http://forum.pjrc.com/threads/25778-Could-there-be-something-like-an-ISR-template-function/page3
#define DMACHANNEL_HAS_BEGIN
#define DMACHANNEL_HAS_BOOLEAN_CTOR
// The channel allocation bitmask is accessible from "C" namespace,
// so C-only code can reserve DMA channels
#ifdef __cplusplus
extern "C" {
#endif
extern uint16_t dma_channel_allocated_mask;
#ifdef __cplusplus
}
#endif
#ifdef __cplusplus
// known libraries with DMA usage (in need of porting to this new scheme):
//
// https://github.com/PaulStoffregen/Audio
// https://github.com/PaulStoffregen/OctoWS2811
// https://github.com/pedvide/ADC
// https://github.com/duff2013/SerialEvent
// https://github.com/pixelmatix/SmartMatrix
// https://github.com/crteensy/DmaSpi <-- DmaSpi has adopted this scheme
/****************************************************************/
/** Teensy 3.0 & 3.1 **/
/****************************************************************/
#if defined(KINETISK)
class DMABaseClass {
public:
typedef struct __attribute__((packed)) {
volatile const void * volatile SADDR;
int16_t SOFF;
union { uint16_t ATTR;
struct { uint8_t ATTR_DST; uint8_t ATTR_SRC; }; };
union { uint32_t NBYTES; uint32_t NBYTES_MLNO;
uint32_t NBYTES_MLOFFNO; uint32_t NBYTES_MLOFFYES; };
int32_t SLAST;
volatile void * volatile DADDR;
int16_t DOFF;
union { volatile uint16_t CITER;
volatile uint16_t CITER_ELINKYES; volatile uint16_t CITER_ELINKNO; };
int32_t DLASTSGA;
volatile uint16_t CSR;
union { volatile uint16_t BITER;
volatile uint16_t BITER_ELINKYES; volatile uint16_t BITER_ELINKNO; };
} TCD_t;
TCD_t *TCD;
/***************************************/
/** Data Transfer **/
/***************************************/
// Use a single variable as the data source. Typically a register
// for receiving data from one of the hardware peripherals is used.
void source(volatile const signed char &p) { source(*(volatile const uint8_t *)&p); }
void source(volatile const unsigned char &p) {
TCD->SADDR = &p;
TCD->SOFF = 0;
TCD->ATTR_SRC = 0;
if ((uint32_t)&p < 0x40000000 || TCD->NBYTES == 0) TCD->NBYTES = 1;
TCD->SLAST = 0;
}
void source(volatile const signed short &p) { source(*(volatile const uint16_t *)&p); }
void source(volatile const unsigned short &p) {
TCD->SADDR = &p;
TCD->SOFF = 0;
TCD->ATTR_SRC = 1;
if ((uint32_t)&p < 0x40000000 || TCD->NBYTES == 0) TCD->NBYTES = 2;
TCD->SLAST = 0;
}
void source(volatile const signed int &p) { source(*(volatile const uint32_t *)&p); }
void source(volatile const unsigned int &p) { source(*(volatile const uint32_t *)&p); }
void source(volatile const signed long &p) { source(*(volatile const uint32_t *)&p); }
void source(volatile const unsigned long &p) {
TCD->SADDR = &p;
TCD->SOFF = 0;
TCD->ATTR_SRC = 2;
if ((uint32_t)&p < 0x40000000 || TCD->NBYTES == 0) TCD->NBYTES = 4;
TCD->SLAST = 0;
}
// Use a buffer (array of data) as the data source. Typically a
// buffer for transmitting data is used.
void sourceBuffer(volatile const signed char p[], unsigned int len) {
sourceBuffer((volatile const uint8_t *)p, len); }
void sourceBuffer(volatile const unsigned char p[], unsigned int len) {
TCD->SADDR = p;
TCD->SOFF = 1;
TCD->ATTR_SRC = 0;
TCD->NBYTES = 1;
TCD->SLAST = -len;
TCD->BITER = len;
TCD->CITER = len;
}
void sourceBuffer(volatile const signed short p[], unsigned int len) {
sourceBuffer((volatile const uint16_t *)p, len); }
void sourceBuffer(volatile const unsigned short p[], unsigned int len) {
TCD->SADDR = p;
TCD->SOFF = 2;
TCD->ATTR_SRC = 1;
TCD->NBYTES = 2;
TCD->SLAST = -len;
TCD->BITER = len / 2;
TCD->CITER = len / 2;
}
void sourceBuffer(volatile const signed int p[], unsigned int len) {
sourceBuffer((volatile const uint32_t *)p, len); }
void sourceBuffer(volatile const unsigned int p[], unsigned int len) {
sourceBuffer((volatile const uint32_t *)p, len); }
void sourceBuffer(volatile const signed long p[], unsigned int len) {
sourceBuffer((volatile const uint32_t *)p, len); }
void sourceBuffer(volatile const unsigned long p[], unsigned int len) {
TCD->SADDR = p;
TCD->SOFF = 4;
TCD->ATTR_SRC = 2;
TCD->NBYTES = 4;
TCD->SLAST = -len;
TCD->BITER = len / 4;
TCD->CITER = len / 4;
}
// Use a circular buffer as the data source
void sourceCircular(volatile const signed char p[], unsigned int len) {
sourceCircular((volatile const uint8_t *)p, len); }
void sourceCircular(volatile const unsigned char p[], unsigned int len) {
TCD->SADDR = p;
TCD->SOFF = 1;
TCD->ATTR_SRC = ((31 - __builtin_clz(len)) << 3);
TCD->NBYTES = 1;
TCD->SLAST = 0;
TCD->BITER = len;
TCD->CITER = len;
}
void sourceCircular(volatile const signed short p[], unsigned int len) {
sourceCircular((volatile const uint16_t *)p, len); }
void sourceCircular(volatile const unsigned short p[], unsigned int len) {
TCD->SADDR = p;
TCD->SOFF = 2;
TCD->ATTR_SRC = ((31 - __builtin_clz(len)) << 3) | 1;
TCD->NBYTES = 2;
TCD->SLAST = 0;
TCD->BITER = len / 2;
TCD->CITER = len / 2;
}
void sourceCircular(volatile const signed int p[], unsigned int len) {
sourceCircular((volatile const uint32_t *)p, len); }
void sourceCircular(volatile const unsigned int p[], unsigned int len) {
sourceCircular((volatile const uint32_t *)p, len); }
void sourceCircular(volatile const signed long p[], unsigned int len) {
sourceCircular((volatile const uint32_t *)p, len); }
void sourceCircular(volatile const unsigned long p[], unsigned int len) {
TCD->SADDR = p;
TCD->SOFF = 4;
TCD->ATTR_SRC = ((31 - __builtin_clz(len)) << 3) | 2;
TCD->NBYTES = 4;
TCD->SLAST = 0;
TCD->BITER = len / 4;
TCD->CITER = len / 4;
}
// Use a single variable as the data destination. Typically a register
// for transmitting data to one of the hardware peripherals is used.
void destination(volatile signed char &p) { destination(*(volatile uint8_t *)&p); }
void destination(volatile unsigned char &p) {
TCD->DADDR = &p;
TCD->DOFF = 0;
TCD->ATTR_DST = 0;
if ((uint32_t)&p < 0x40000000 || TCD->NBYTES == 0) TCD->NBYTES = 1;
TCD->DLASTSGA = 0;
}
void destination(volatile signed short &p) { destination(*(volatile uint16_t *)&p); }
void destination(volatile unsigned short &p) {
TCD->DADDR = &p;
TCD->DOFF = 0;
TCD->ATTR_DST = 1;
if ((uint32_t)&p < 0x40000000 || TCD->NBYTES == 0) TCD->NBYTES = 2;
TCD->DLASTSGA = 0;
}
void destination(volatile signed int &p) { destination(*(volatile uint32_t *)&p); }
void destination(volatile unsigned int &p) { destination(*(volatile uint32_t *)&p); }
void destination(volatile signed long &p) { destination(*(volatile uint32_t *)&p); }
void destination(volatile unsigned long &p) {
TCD->DADDR = &p;
TCD->DOFF = 0;
TCD->ATTR_DST = 2;
if ((uint32_t)&p < 0x40000000 || TCD->NBYTES == 0) TCD->NBYTES = 4;
TCD->DLASTSGA = 0;
}
// Use a buffer (array of data) as the data destination. Typically a
// buffer for receiving data is used.
void destinationBuffer(volatile signed char p[], unsigned int len) {
destinationBuffer((volatile uint8_t *)p, len); }
void destinationBuffer(volatile unsigned char p[], unsigned int len) {
TCD->DADDR = p;
TCD->DOFF = 1;
TCD->ATTR_DST = 0;
TCD->NBYTES = 1;
TCD->DLASTSGA = -len;
TCD->BITER = len;
TCD->CITER = len;
}
void destinationBuffer(volatile signed short p[], unsigned int len) {
destinationBuffer((volatile uint16_t *)p, len); }
void destinationBuffer(volatile unsigned short p[], unsigned int len) {
TCD->DADDR = p;
TCD->DOFF = 2;
TCD->ATTR_DST = 1;
TCD->NBYTES = 2;
TCD->DLASTSGA = -len;
TCD->BITER = len / 2;
TCD->CITER = len / 2;
}
void destinationBuffer(volatile signed int p[], unsigned int len) {
destinationBuffer((volatile uint32_t *)p, len); }
void destinationBuffer(volatile unsigned int p[], unsigned int len) {
destinationBuffer((volatile uint32_t *)p, len); }
void destinationBuffer(volatile signed long p[], unsigned int len) {
destinationBuffer((volatile uint32_t *)p, len); }
void destinationBuffer(volatile unsigned long p[], unsigned int len) {
TCD->DADDR = p;
TCD->DOFF = 4;
TCD->ATTR_DST = 2;
TCD->NBYTES = 4;
TCD->DLASTSGA = -len;
TCD->BITER = len / 4;
TCD->CITER = len / 4;
}
// Use a circular buffer as the data destination
void destinationCircular(volatile signed char p[], unsigned int len) {
destinationCircular((volatile uint8_t *)p, len); }
void destinationCircular(volatile unsigned char p[], unsigned int len) {
TCD->DADDR = p;
TCD->DOFF = 1;
TCD->ATTR_DST = ((31 - __builtin_clz(len)) << 3);
TCD->NBYTES = 1;
TCD->DLASTSGA = 0;
TCD->BITER = len;
TCD->CITER = len;
}
void destinationCircular(volatile signed short p[], unsigned int len) {
destinationCircular((volatile uint16_t *)p, len); }
void destinationCircular(volatile unsigned short p[], unsigned int len) {
TCD->DADDR = p;
TCD->DOFF = 2;
TCD->ATTR_DST = ((31 - __builtin_clz(len)) << 3) | 1;
TCD->NBYTES = 2;
TCD->DLASTSGA = 0;
TCD->BITER = len / 2;
TCD->CITER = len / 2;
}
void destinationCircular(volatile signed int p[], unsigned int len) {
destinationCircular((volatile uint32_t *)p, len); }
void destinationCircular(volatile unsigned int p[], unsigned int len) {
destinationCircular((volatile uint32_t *)p, len); }
void destinationCircular(volatile signed long p[], unsigned int len) {
destinationCircular((volatile uint32_t *)p, len); }
void destinationCircular(volatile unsigned long p[], unsigned int len) {
TCD->DADDR = p;
TCD->DOFF = 4;
TCD->ATTR_DST = ((31 - __builtin_clz(len)) << 3) | 2;
TCD->NBYTES = 4;
TCD->DLASTSGA = 0;
TCD->BITER = len / 4;
TCD->CITER = len / 4;
}
/*************************************************/
/** Quantity of Data to Transfer **/
/*************************************************/
// Set the data size used for each triggered transfer
void transferSize(unsigned int len) {
if (len == 16) {
TCD->NBYTES = 16;
if (TCD->SOFF != 0) TCD->SOFF = 16;
if (TCD->DOFF != 0) TCD->DOFF = 16;
TCD->ATTR = (TCD->ATTR & 0xF8F8) | 0x0404;
} else if (len == 4) {
TCD->NBYTES = 4;
if (TCD->SOFF != 0) TCD->SOFF = 4;
if (TCD->DOFF != 0) TCD->DOFF = 4;
TCD->ATTR = (TCD->ATTR & 0xF8F8) | 0x0202;
} else if (len == 2) {
TCD->NBYTES = 2;
if (TCD->SOFF != 0) TCD->SOFF = 2;
if (TCD->DOFF != 0) TCD->DOFF = 2;
TCD->ATTR = (TCD->ATTR & 0xF8F8) | 0x0101;
} else {
TCD->NBYTES = 1;
if (TCD->SOFF != 0) TCD->SOFF = 1;
if (TCD->DOFF != 0) TCD->DOFF = 1;
TCD->ATTR = TCD->ATTR & 0xF8F8;
}
}
// Set the number of transfers (number of triggers until complete)
void transferCount(unsigned int len) {
if (len > 32767) return;
if (len >= 512) {
TCD->BITER = len;
TCD->CITER = len;
} else {
TCD->BITER = (TCD->BITER & 0xFE00) | len;
TCD->CITER = (TCD->CITER & 0xFE00) | len;
}
}
/*************************************************/
/** Special Options / Features **/
/*************************************************/
void interruptAtCompletion(void) {
TCD->CSR |= DMA_TCD_CSR_INTMAJOR;
}
void interruptAtHalf(void) {
TCD->CSR |= DMA_TCD_CSR_INTHALF;
}
void disableOnCompletion(void) {
TCD->CSR |= DMA_TCD_CSR_DREQ;
}
void replaceSettingsOnCompletion(const DMABaseClass &settings) {
TCD->DLASTSGA = (int32_t)(settings.TCD);
TCD->CSR &= ~DMA_TCD_CSR_DONE;
TCD->CSR |= DMA_TCD_CSR_ESG;
}
protected:
// users should not be able to create instances of DMABaseClass, which
// require the inheriting class to initialize the TCD pointer.
DMABaseClass() {}
static inline void copy_tcd(TCD_t *dst, const TCD_t *src) {
const uint32_t *p = (const uint32_t *)src;
uint32_t *q = (uint32_t *)dst;
uint32_t t1, t2, t3, t4;
t1 = *p++; t2 = *p++; t3 = *p++; t4 = *p++;
*q++ = t1; *q++ = t2; *q++ = t3; *q++ = t4;
t1 = *p++; t2 = *p++; t3 = *p++; t4 = *p++;
*q++ = t1; *q++ = t2; *q++ = t3; *q++ = t4;
}
};
// DMASetting represents settings stored only in memory, which can be
// applied to any DMA channel.
class DMASetting : public DMABaseClass {
public:
DMASetting() {
TCD = &tcddata;
}
DMASetting(const DMASetting &c) {
TCD = &tcddata;
*this = c;
}
DMASetting(const DMABaseClass &c) {
TCD = &tcddata;
*this = c;
}
DMASetting & operator = (const DMABaseClass &rhs) {
copy_tcd(TCD, rhs.TCD);
return *this;
}
private:
TCD_t tcddata __attribute__((aligned(32)));
};
// DMAChannel reprents an actual DMA channel and its current settings
class DMAChannel : public DMABaseClass {
public:
/*************************************************/
/** Channel Allocation **/
/*************************************************/
DMAChannel() {
begin();
}
DMAChannel(const DMAChannel &c) {
TCD = c.TCD;
channel = c.channel;
}
DMAChannel(const DMASetting &c) {
begin();
copy_tcd(TCD, c.TCD);
}
DMAChannel(bool allocate) {
if (allocate) begin();
}
DMAChannel & operator = (const DMAChannel &rhs) {
if (channel != rhs.channel) {
release();
TCD = rhs.TCD;
channel = rhs.channel;
}
return *this;
}
DMAChannel & operator = (const DMASetting &rhs) {
copy_tcd(TCD, rhs.TCD);
return *this;
}
~DMAChannel() {
release();
}
void begin(bool force_initialization = false);
private:
void release(void);
public:
/***************************************/
/** Triggering **/
/***************************************/
// Triggers cause the DMA channel to actually move data. Each
// trigger moves a single data unit, which is typically 8, 16 or
// 32 bits. If a channel is configured for 200 transfers
// Use a hardware trigger to make the DMA channel run
void triggerAtHardwareEvent(uint8_t source) {
volatile uint8_t *mux;
mux = (volatile uint8_t *)&(DMAMUX0_CHCFG0) + channel;
*mux = 0;
*mux = (source & 63) | DMAMUX_ENABLE;
}
// Use another DMA channel as the trigger, causing this
// channel to trigger after each transfer is makes, except
// the its last transfer. This effectively makes the 2
// channels run in parallel until the last transfer
void triggerAtTransfersOf(DMABaseClass &ch) {
ch.TCD->BITER = (ch.TCD->BITER & ~DMA_TCD_BITER_ELINKYES_LINKCH_MASK)
| DMA_TCD_BITER_ELINKYES_LINKCH(channel) | DMA_TCD_BITER_ELINKYES_ELINK;
ch.TCD->CITER = ch.TCD->BITER ;
}
// Use another DMA channel as the trigger, causing this
// channel to trigger when the other channel completes.
void triggerAtCompletionOf(DMABaseClass &ch) {
ch.TCD->CSR = (ch.TCD->CSR & ~(DMA_TCD_CSR_MAJORLINKCH_MASK|DMA_TCD_CSR_DONE))
| DMA_TCD_CSR_MAJORLINKCH(channel) | DMA_TCD_CSR_MAJORELINK;
}
// Cause this DMA channel to be continuously triggered, so
// it will move data as rapidly as possible, without waiting.
// Normally this would be used with disableOnCompletion().
void triggerContinuously(void) {
volatile uint8_t *mux = (volatile uint8_t *)&DMAMUX0_CHCFG0;
mux[channel] = 0;
#if DMAMUX_NUM_SOURCE_ALWAYS >= DMA_NUM_CHANNELS
mux[channel] = DMAMUX_SOURCE_ALWAYS0 + channel;
#else
// search for an unused "always on" source
unsigned int i = DMAMUX_SOURCE_ALWAYS0;
for (i = DMAMUX_SOURCE_ALWAYS0;
i < DMAMUX_SOURCE_ALWAYS0 + DMAMUX_NUM_SOURCE_ALWAYS; i++) {
unsigned int ch;
for (ch=0; ch < DMA_NUM_CHANNELS; ch++) {
if (mux[ch] == i) break;
}
if (ch >= DMA_NUM_CHANNELS) {
mux[channel] = (i | DMAMUX_ENABLE);
return;
}
}
#endif
}
// Manually trigger the DMA channel.
void triggerManual(void) {
DMA_SSRT = channel;
}
/***************************************/
/** Interrupts **/
/***************************************/
// An interrupt routine can be run when the DMA channel completes
// the entire transfer, and also optionally when half of the
// transfer is completed.
void attachInterrupt(void (*isr)(void)) {
_VectorsRam[channel + IRQ_DMA_CH0 + 16] = isr;
NVIC_ENABLE_IRQ(IRQ_DMA_CH0 + channel);
}
void detachInterrupt(void) {
NVIC_DISABLE_IRQ(IRQ_DMA_CH0 + channel);
}
void clearInterrupt(void) {
DMA_CINT = channel;
}
/***************************************/
/** Enable / Disable **/
/***************************************/
void enable(void) {
DMA_SERQ = channel;
}
void disable(void) {
DMA_CERQ = channel;
}
/***************************************/
/** Status **/
/***************************************/
bool complete(void) {
if (TCD->CSR & DMA_TCD_CSR_DONE) return true;
return false;
}
void clearComplete(void) {
DMA_CDNE = channel;
}
bool error(void) {
if (DMA_ERR & (1<<channel)) return true;
return false;
}
void clearError(void) {
DMA_CERR = channel;
}
void * sourceAddress(void) {
return (void *)(TCD->SADDR);
}
void * destinationAddress(void) {
return (void *)(TCD->DADDR);
}
/***************************************/
/** Direct Hardware Access **/
/***************************************/
// For complex and unusual configurations not possible with the above
// functions, the Transfer Control Descriptor (TCD) and channel number
// can be used directly. This leads to less portable and less readable
// code, but direct control of all parameters is possible.
uint8_t channel;
// TCD is accessible due to inheritance from DMABaseClass
};
// arrange the relative priority of 2 or more DMA channels
void DMAPriorityOrder(DMAChannel &ch1, DMAChannel &ch2);
void DMAPriorityOrder(DMAChannel &ch1, DMAChannel &ch2, DMAChannel &ch3);
void DMAPriorityOrder(DMAChannel &ch1, DMAChannel &ch2, DMAChannel &ch3, DMAChannel &ch4);
/****************************************************************/
/** Teensy-LC **/
/****************************************************************/
#elif defined(KINETISL)
class DMABaseClass {
public:
typedef struct __attribute__((packed)) {
volatile const void * volatile SAR;
volatile void * volatile DAR;
volatile uint32_t DSR_BCR;
volatile uint32_t DCR;
} CFG_t;
CFG_t *CFG;
/***************************************/
/** Data Transfer **/
/***************************************/
// Use a single variable as the data source. Typically a register
// for receiving data from one of the hardware peripherals is used.
void source(volatile const signed char &p) { source(*(volatile const uint8_t *)&p); }
void source(volatile const unsigned char &p) {
CFG->SAR = &p;
CFG->DCR = (CFG->DCR & 0xF08E0FFF) | DMA_DCR_SSIZE(1);
}
void source(volatile const signed short &p) { source(*(volatile const uint16_t *)&p); }
void source(volatile const unsigned short &p) {
CFG->SAR = &p;
CFG->DCR = (CFG->DCR & 0xF08E0FFF) | DMA_DCR_SSIZE(2);
}
void source(volatile const signed int &p) { source(*(volatile const uint32_t *)&p); }
void source(volatile const unsigned int &p) { source(*(volatile const uint32_t *)&p); }
void source(volatile const signed long &p) { source(*(volatile const uint32_t *)&p); }
void source(volatile const unsigned long &p) {
CFG->SAR = &p;
CFG->DCR = (CFG->DCR & 0xF08E0FFF) | DMA_DCR_SSIZE(0);
}
// Use a buffer (array of data) as the data source. Typically a
// buffer for transmitting data is used.
void sourceBuffer(volatile const signed char p[], unsigned int len) {
sourceBuffer((volatile const uint8_t *)p, len); }
void sourceBuffer(volatile const unsigned char p[], unsigned int len) {
if (len > 0xFFFFF) return;
CFG->SAR = p;
CFG->DCR = (CFG->DCR & 0xF08E0FFF) | DMA_DCR_SSIZE(1) | DMA_DCR_SINC;
CFG->DSR_BCR = len;
}
void sourceBuffer(volatile const signed short p[], unsigned int len) {
sourceBuffer((volatile const uint16_t *)p, len); }
void sourceBuffer(volatile const unsigned short p[], unsigned int len) {
if (len > 0xFFFFF) return;
CFG->SAR = p;
CFG->DCR = (CFG->DCR & 0xF08E0FFF) | DMA_DCR_SSIZE(2) | DMA_DCR_SINC;
CFG->DSR_BCR = len;
}
void sourceBuffer(volatile const signed int p[], unsigned int len) {
sourceBuffer((volatile const uint32_t *)p, len); }
void sourceBuffer(volatile const unsigned int p[], unsigned int len) {
sourceBuffer((volatile const uint32_t *)p, len); }
void sourceBuffer(volatile const signed long p[], unsigned int len) {
sourceBuffer((volatile const uint32_t *)p, len); }
void sourceBuffer(volatile const unsigned long p[], unsigned int len) {
if (len > 0xFFFFF) return;
CFG->SAR = p;
CFG->DCR = (CFG->DCR & 0xF08E0FFF) | DMA_DCR_SSIZE(0) | DMA_DCR_SINC;
CFG->DSR_BCR = len;
}
// Use a circular buffer as the data source
void sourceCircular(volatile const signed char p[], unsigned int len) {
sourceCircular((volatile const uint8_t *)p, len); }
void sourceCircular(volatile const unsigned char p[], unsigned int len) {
uint32_t mod = len2mod(len);
if (mod == 0) return;
CFG->SAR = p;
CFG->DCR = (CFG->DCR & 0xF08E0FFF) | DMA_DCR_SSIZE(1) | DMA_DCR_SINC
| DMA_DCR_SMOD(mod);
CFG->DSR_BCR = len;
}
void sourceCircular(volatile const signed short p[], unsigned int len) {
sourceCircular((volatile const uint16_t *)p, len); }
void sourceCircular(volatile const unsigned short p[], unsigned int len) {
uint32_t mod = len2mod(len);
if (mod == 0) return;
CFG->SAR = p;
CFG->DCR = (CFG->DCR & 0xF08E0FFF) | DMA_DCR_SSIZE(2) | DMA_DCR_SINC
| DMA_DCR_SMOD(mod);
CFG->DSR_BCR = len;
}
void sourceCircular(volatile const signed int p[], unsigned int len) {
sourceCircular((volatile const uint32_t *)p, len); }
void sourceCircular(volatile const unsigned int p[], unsigned int len) {
sourceCircular((volatile const uint32_t *)p, len); }
void sourceCircular(volatile const signed long p[], unsigned int len) {
sourceCircular((volatile const uint32_t *)p, len); }
void sourceCircular(volatile const unsigned long p[], unsigned int len) {
uint32_t mod = len2mod(len);
if (mod == 0) return;
CFG->SAR = p;
CFG->DCR = (CFG->DCR & 0xF08E0FFF) | DMA_DCR_SSIZE(0) | DMA_DCR_SINC
| DMA_DCR_SMOD(mod);
CFG->DSR_BCR = len;
}
// Use a single variable as the data destination. Typically a register
// for transmitting data to one of the hardware peripherals is used.
void destination(volatile signed char &p) { destination(*(volatile uint8_t *)&p); }
void destination(volatile unsigned char &p) {
CFG->DAR = &p;
CFG->DCR = (CFG->DCR & 0xF0F0F0FF) | DMA_DCR_DSIZE(1);
}
void destination(volatile signed short &p) { destination(*(volatile uint16_t *)&p); }
void destination(volatile unsigned short &p) {
CFG->DAR = &p;
CFG->DCR = (CFG->DCR & 0xF0F0F0FF) | DMA_DCR_DSIZE(2);
}
void destination(volatile signed int &p) { destination(*(volatile uint32_t *)&p); }
void destination(volatile unsigned int &p) { destination(*(volatile uint32_t *)&p); }
void destination(volatile signed long &p) { destination(*(volatile uint32_t *)&p); }
void destination(volatile unsigned long &p) {
CFG->DAR = &p;
CFG->DCR = (CFG->DCR & 0xF0F0F0FF) | DMA_DCR_DSIZE(0);
}
// Use a buffer (array of data) as the data destination. Typically a
// buffer for receiving data is used.
void destinationBuffer(volatile signed char p[], unsigned int len) {
destinationBuffer((volatile uint8_t *)p, len); }
void destinationBuffer(volatile unsigned char p[], unsigned int len) {
CFG->DAR = p;
CFG->DCR = (CFG->DCR & 0xF0F0F0FF) | DMA_DCR_DSIZE(1) | DMA_DCR_DINC;
CFG->DSR_BCR = len;
}
void destinationBuffer(volatile signed short p[], unsigned int len) {
destinationBuffer((volatile uint16_t *)p, len); }
void destinationBuffer(volatile unsigned short p[], unsigned int len) {
CFG->DAR = p;
CFG->DCR = (CFG->DCR & 0xF0F0F0FF) | DMA_DCR_DSIZE(2) | DMA_DCR_DINC;
CFG->DSR_BCR = len;
}
void destinationBuffer(volatile signed int p[], unsigned int len) {
destinationBuffer((volatile uint32_t *)p, len); }
void destinationBuffer(volatile unsigned int p[], unsigned int len) {
destinationBuffer((volatile uint32_t *)p, len); }
void destinationBuffer(volatile signed long p[], unsigned int len) {
destinationBuffer((volatile uint32_t *)p, len); }
void destinationBuffer(volatile unsigned long p[], unsigned int len) {
CFG->DAR = p;
CFG->DCR = (CFG->DCR & 0xF0F0F0FF) | DMA_DCR_DSIZE(0) | DMA_DCR_DINC;
CFG->DSR_BCR = len;
}
// Use a circular buffer as the data destination
void destinationCircular(volatile signed char p[], unsigned int len) {
destinationCircular((volatile uint8_t *)p, len); }
void destinationCircular(volatile unsigned char p[], unsigned int len) {
uint32_t mod = len2mod(len);
if (mod == 0) return;
CFG->DAR = p;
CFG->DCR = (CFG->DCR & 0xF0F0F0FF) | DMA_DCR_DSIZE(1) | DMA_DCR_DINC
| DMA_DCR_DMOD(mod);
CFG->DSR_BCR = len;
}
void destinationCircular(volatile signed short p[], unsigned int len) {
destinationCircular((volatile uint16_t *)p, len); }
void destinationCircular(volatile unsigned short p[], unsigned int len) {
uint32_t mod = len2mod(len);
if (mod == 0) return;
CFG->DAR = p;
CFG->DCR = (CFG->DCR & 0xF0F0F0FF) | DMA_DCR_DSIZE(1) | DMA_DCR_DINC
| DMA_DCR_DMOD(mod);
CFG->DSR_BCR = len;
}
void destinationCircular(volatile signed int p[], unsigned int len) {
destinationCircular((volatile uint32_t *)p, len); }
void destinationCircular(volatile unsigned int p[], unsigned int len) {
destinationCircular((volatile uint32_t *)p, len); }
void destinationCircular(volatile signed long p[], unsigned int len) {
destinationCircular((volatile uint32_t *)p, len); }
void destinationCircular(volatile unsigned long p[], unsigned int len) {
uint32_t mod = len2mod(len);
if (mod == 0) return;
CFG->DAR = p;
CFG->DCR = (CFG->DCR & 0xF0F0F0FF) | DMA_DCR_DSIZE(1) | DMA_DCR_DINC
| DMA_DCR_DMOD(mod);
CFG->DSR_BCR = len;
}
/*************************************************/
/** Quantity of Data to Transfer **/
/*************************************************/
// Set the data size used for each triggered transfer
void transferSize(unsigned int len) {
uint32_t dcr = CFG->DCR & 0xF0C8FFFF;
if (len == 4) {
CFG->DCR = dcr | DMA_DCR_DSIZE(0) | DMA_DCR_DSIZE(0);
} else if (len == 2) {
CFG->DCR = dcr | DMA_DCR_DSIZE(2) | DMA_DCR_DSIZE(2);
} else {
CFG->DCR = dcr | DMA_DCR_DSIZE(1) | DMA_DCR_DSIZE(1);
}
}
// Set the number of transfers (number of triggers until complete)
void transferCount(unsigned int len) {
uint32_t s, d, n = 0; // 0 = 8 bit, 1 = 16 bit, 2 = 32 bit
uint32_t dcr = CFG->DCR;
s = (dcr >> 20) & 3;
d = (dcr >> 17) & 3;
if (s == 0 || d == 0) n = 2;
else if (s == 2 || d == 2) n = 1;
CFG->DSR_BCR = len << n;
}
/*************************************************/
/** Special Options / Features **/
/*************************************************/
void interruptAtCompletion(void) {
CFG->DCR |= DMA_DCR_EINT;
}
void disableOnCompletion(void) {
CFG->DCR |= DMA_DCR_D_REQ;
}
// Kinetis-L DMA does not have these features :-(
//
// void interruptAtHalf(void) {}
// void replaceSettingsOnCompletion(const DMABaseClass &settings) {};
// TODO: can a 2nd linked channel be used to emulate this?
protected:
// users should not be able to create instances of DMABaseClass, which
// require the inheriting class to initialize the TCD pointer.
DMABaseClass() {}
static inline void copy_cfg(CFG_t *dst, const CFG_t *src) {
dst->SAR = src->SAR;
dst->DAR = src->DAR;
dst->DSR_BCR = src->DSR_BCR;
dst->DCR = src->DCR;
}
private:
static inline uint32_t len2mod(uint32_t len) {
if (len < 16) return 0;
if (len < 32) return 1;
if (len < 64) return 2;
if (len < 128) return 3;
if (len < 256) return 4;
if (len < 512) return 5;
if (len < 1024) return 6;
if (len < 2048) return 7;
if (len < 4096) return 8;
if (len < 8192) return 9;
if (len < 16384) return 10;
if (len < 32768) return 11;
if (len < 65536) return 12;
if (len < 131072) return 13;
if (len < 262144) return 14;
return 15;
}
};
// DMASetting represents settings stored only in memory, which can be
// applied to any DMA channel.
class DMASetting : public DMABaseClass {
public:
DMASetting() {
cfgdata.SAR = NULL;
cfgdata.DAR = NULL;
cfgdata.DSR_BCR = 0;
cfgdata.DCR = DMA_DCR_CS;
CFG = &cfgdata;
}
DMASetting(const DMASetting &c) {
CFG = &cfgdata;
*this = c;
}
DMASetting(const DMABaseClass &c) {
CFG = &cfgdata;
*this = c;
}
DMASetting & operator = (const DMABaseClass &rhs) {
copy_cfg(CFG, rhs.CFG);
return *this;
}
private:
CFG_t cfgdata __attribute__((aligned(4)));
};
// DMAChannel reprents an actual DMA channel and its current settings
class DMAChannel : public DMABaseClass {
public:
/*************************************************/
/** Channel Allocation **/
/*************************************************/
DMAChannel() {
begin();
}
DMAChannel(const DMAChannel &c) {
CFG = c.CFG;
channel = c.channel;
}
DMAChannel(const DMASetting &c) {
begin();
copy_cfg(CFG, c.CFG);
}
DMAChannel(bool allocate) {
if (allocate) begin();
}
DMAChannel & operator = (const DMAChannel &rhs) {
if (channel != rhs.channel) {
release();
CFG = rhs.CFG;
channel = rhs.channel;
}
return *this;
}
DMAChannel & operator = (const DMASetting &rhs) {
copy_cfg(CFG, rhs.CFG);
return *this;
}
~DMAChannel() {
release();
}
void begin(bool force_initialization = false);
private:
void release(void);
public:
/***************************************/
/** Triggering **/
/***************************************/
// Triggers cause the DMA channel to actually move data. Each
// trigger moves a single data unit, which is typically 8, 16 or
// 32 bits. If a channel is configured for 200 transfers
// Use a hardware trigger to make the DMA channel run
void triggerAtHardwareEvent(uint8_t source) {
volatile uint8_t *mux;
CFG->DCR |= DMA_DCR_CS;
mux = (volatile uint8_t *)&(DMAMUX0_CHCFG0) + channel;
*mux = 0;
*mux = (source & 63) | DMAMUX_ENABLE;
}
// Use another DMA channel as the trigger, causing this
// channel to trigger after each transfer is makes, including
// the its last transfer. This effectively makes the 2
// channels run in parallel. Note, on Teensy 3.0 & 3.1,
// this feature triggers on every transfer except the last.
// On Teensy-LC, it triggers on every one, including the last.
void triggerAtTransfersOf(DMABaseClass &ch) {
uint32_t dcr = ch.CFG->DCR;
uint32_t linkcc = (dcr >> 4) & 3;
if (linkcc == 0 || linkcc == 2) {
ch.CFG->DCR = (dcr & ~DMA_DCR_LCH1(3)) |
DMA_DCR_LINKCC(2) | DMA_DCR_LCH1(channel);
} else if (linkcc == 1) {
ch.CFG->DCR = (dcr & ~DMA_DCR_LCH1(3)) |
DMA_DCR_LCH1(channel);
} else {
uint32_t lch1 = (dcr >> 2) & 3;
ch.CFG->DCR = (dcr
& ~(DMA_DCR_LINKCC(3) | DMA_DCR_LCH2(3) | DMA_DCR_LCH1(3)))
| DMA_DCR_LINKCC(1) | DMA_DCR_LCH2(lch1) | DMA_DCR_LCH1(channel);
}
}
// Use another DMA channel as the trigger, causing this
// channel to trigger when the other channel completes.
void triggerAtCompletionOf(DMABaseClass &ch) {
uint32_t dcr = ch.CFG->DCR;
uint32_t linkcc = (dcr >> 4) & 3;
if (linkcc == 0 || linkcc == 3) {
ch.CFG->DCR = (dcr & ~DMA_DCR_LCH1(3)) |
DMA_DCR_LINKCC(3) | DMA_DCR_LCH1(channel);
} else {
ch.CFG->DCR = (dcr
& ~(DMA_DCR_LINKCC(3) | DMA_DCR_LCH2(3)))
| DMA_DCR_LINKCC(1) | DMA_DCR_LCH2(channel);
}
}
// Cause this DMA channel to be continuously triggered, so
// it will move data as rapidly as possible, without waiting.
// Normally this would be used with disableOnCompletion().
void triggerContinuously(void) {
uint32_t dcr = CFG->DCR;
dcr &= ~(DMA_DCR_ERQ | DMA_DCR_CS);
CFG->DCR = dcr;
CFG->DCR = dcr | DMA_DCR_START;
}
// Manually trigger the DMA channel.
void triggerManual(void) {
CFG->DCR = (CFG->DCR & ~DMA_DCR_ERQ) | (DMA_DCR_CS | DMA_DCR_START);
}
/***************************************/
/** Interrupts **/
/***************************************/
// An interrupt routine can be run when the DMA channel completes
// the entire transfer, and also optionally when half of the