-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathDmaSpi.h
770 lines (679 loc) · 20.7 KB
/
DmaSpi.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
#ifndef DMASPI_H
#define DMASPI_H
#include <Arduino.h>
#include <util/atomic.h>
#if(!defined(__arm__) && defined(TEENSYDUINO))
#error This library is for teensyduino 1.21 on Teensy 3.0, 3.1 and Teensy LC only.
#endif
#include <SPI.h>
#include "DMAChannel.h"
#include "ChipSelect.h"
//#define DEBUG_DMASPI 1
#if defined(DEBUG_DMASPI)
#define DMASPI_PRINT(x) do {Serial.printf x ; Serial.flush();} while (0);
#else
#define DMASPI_PRINT(x) do {} while (0);
#endif
namespace DmaSpi
{
/** \brief describes an SPI transfer
*
* Transfers are kept in a queue (intrusive linked list) until they are processed by the DmaSpi driver.
*
**/
class Transfer
{
public:
/** \brief The Transfer's current state.
*
**/
enum State
{
idle, /**< The Transfer is idle, the DmaSpi has not seen it yet. **/
eDone, /**< The Transfer is done. **/
pending, /**< Queued, but not handled yet. **/
inProgress, /**< The DmaSpi driver is currently busy executing this Transfer. **/
error /**< An error occured. **/
};
/** \brief Creates a Transfer object.
* \param pSource pointer to the data source. If this is nullptr, the fill value is used instead.
* \param transferCount the number of SPI transfers to perform.
* \param pDest pointer to the data sink. If this is nullptr, data received from the slave will be discarded.
* \param fill if pSource is nullptr, this value is sent to the slave instead.
* \param cs pointer to a chip select object.
* If not nullptr, cs->select() is called when the Transfer is started and cs->deselect() is called when the Transfer is finished.
**/
Transfer(const uint8_t* pSource = nullptr,
const uint16_t& transferCount = 0,
volatile uint8_t* pDest = nullptr,
const uint8_t& fill = 0,
AbstractChipSelect* cs = nullptr
) : m_state(State::idle),
m_pSource(pSource),
m_transferCount(transferCount),
m_pDest(pDest),
m_fill(fill),
m_pNext(nullptr),
m_pSelect(cs)
{
DMASPI_PRINT(("Transfer @ %p\n", this));
};
/** \brief Check if the Transfer is busy, i.e. may not be modified.
**/
bool busy() const {return ((m_state == State::pending) || (m_state == State::inProgress) || (m_state == State::error));}
/** \brief Check if the Transfer is done.
**/
bool done() const {return (m_state == State::eDone);}
// private:
volatile State m_state;
const uint8_t* m_pSource;
uint16_t m_transferCount;
volatile uint8_t* m_pDest;
uint8_t m_fill;
Transfer* m_pNext;
AbstractChipSelect* m_pSelect;
};
} // namespace DmaSpi
template<typename DMASPI_INSTANCE, typename SPICLASS, SPICLASS& m_Spi>
class AbstractDmaSpi
{
public:
using Transfer = DmaSpi::Transfer;
/** \brief arduino-style initialization.
*
* During initialization, two DMA channels are allocated. If that fails, this function returns false.
* If the channels could be allocated, those DMA channel fields that don't change during DMA SPI operation
* are initialized to the values they will have at runtime.
*
* \return true if initialization was successful; false otherwise.
* \see end()
**/
static bool begin()
{
if(init_count_ > 0)
{
return true; // this is not particularly bad, so we can return true
}
init_count_++;
DMASPI_PRINT(("DmaSpi::begin() : "));
// create DMA channels, might fail
if (!createDmaChannels())
{
DMASPI_PRINT(("could not create DMA channels\n"));
return false;
}
state_ = eStopped;
// tx: known destination (SPI), no interrupt, finish silently
begin_setup_txChannel();
if (txChannel_()->error())
{
destroyDmaChannels();
DMASPI_PRINT(("tx channel error\n"));
return false;
}
// rx: known source (SPI), interrupt on completion
begin_setup_rxChannel();
if (rxChannel_()->error())
{
destroyDmaChannels();
DMASPI_PRINT(("rx channel error\n"));
return false;
}
return true;
}
static void begin_setup_txChannel() {DMASPI_INSTANCE::begin_setup_txChannel_impl();}
static void begin_setup_rxChannel() {DMASPI_INSTANCE::begin_setup_rxChannel_impl();}
/** \brief Allow the DMA SPI to start handling Transfers. This must be called after begin().
* \see running()
* \see busy()
* \see stop()
* \see stopping()
* \see stopped()
**/
static void start()
{
DMASPI_PRINT(("DmaSpi::start() : state_ = "));
switch(state_)
{
case eStopped:
DMASPI_PRINT(("eStopped\n"));
state_ = eRunning;
beginPendingTransfer();
break;
case eRunning:
DMASPI_PRINT(("eRunning\n"));
break;
case eStopping:
DMASPI_PRINT(("eStopping\n"));
state_ = eRunning;
break;
default:
DMASPI_PRINT(("unknown\n"));
state_ = eError;
break;
}
}
/** \brief check if the DMA SPI is in running state.
* \return true if the DMA SPI is in running state, false otherwise.
* \see start()
* \see busy()
* \see stop()
* \see stopping()
* \see stopped()
**/
static bool running() {return state_ == eRunning;}
/** \brief register a Transfer to be handled by the DMA SPI.
* \return false if the Transfer had an invalid transfer count (zero or greater than 32767), true otherwise.
* \post the Transfer state is Transfer::State::pending, or Transfer::State::error if the transfer count was invalid.
**/
static bool registerTransfer(Transfer& transfer)
{
DMASPI_PRINT(("DmaSpi::registerTransfer(%p)\n", &transfer));
if ((transfer.busy())
|| (transfer.m_transferCount == 0) // no zero length transfers allowed
|| (transfer.m_transferCount >= 0x8000)) // max CITER/BITER count with ELINK = 0 is 0x7FFF, so reject
{
DMASPI_PRINT((" Transfer is busy or invalid, dropped\n"));
transfer.m_state = Transfer::State::error;
return false;
}
addTransferToQueue(transfer);
if ((state_ == eRunning) && (!busy()))
{
DMASPI_PRINT((" starting transfer\n"));
ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
{
beginPendingTransfer();
}
}
return true;
}
/** \brief Check if the DMA SPI is busy, which means that it is currently handling a Transfer.
\return true if a Transfer is being handled.
* \see start()
* \see running()
* \see stop()
* \see stopping()
* \see stopped()
**/
static bool busy()
{
return (m_pCurrentTransfer != nullptr);
}
/** \brief Request the DMA SPI to stop handling Transfers.
*
* The stopping driver may finish a current Transfer, but it will then not start a new, pending one.
* \see start()
* \see running()
* \see busy()
* \see stopping()
* \see stopped()
**/
static void stop()
{
ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
{
switch(state_)
{
case eStopped:
break;
case eRunning:
if (busy())
{
state_ = eStopping;
}
else
{
// this means that the DMA SPI simply has nothing to do
state_ = eStopped;
}
break;
case eStopping:
break;
default:
state_ = eError;
break;
}
}
}
/** \brief See if the DMA SPI is currently switching from running to stopped state
* \return true if the DMA SPI is switching from running to stopped state
* \see start()
* \see running()
* \see busy()
* \see stop()
* \see stopped()
**/
static bool stopping() { return (state_ == eStopping); }
/** \brief See if the DMA SPI is stopped
* \return true if the DMA SPI is in stopped state, i.e. not handling pending Transfers
* \see start()
* \see running()
* \see busy()
* \see stop()
* \see stopping()
**/
static bool stopped() { return (state_ == eStopped); }
/** \brief Shut down the DMA SPI
*
* Deallocates DMA channels and sets the internal state to error (this might not be an intelligent name for that)
* \see begin()
**/
static void end()
{
if (init_count_ == 0)
{
state_ = eError;
return;
}
if (init_count_ == 1)
{
init_count_--;
destroyDmaChannels();
state_ = eError;
return;
}
else
{
init_count_--;
return;
}
}
/** \brief get the last value that was read from a slave, but discarded because the Transfer didn't specify a sink
**/
static uint8_t devNull()
{
return m_devNull;
}
protected:
enum EState
{
eStopped,
eRunning,
eStopping,
eError
};
static void addTransferToQueue(Transfer& transfer)
{
transfer.m_state = Transfer::State::pending;
transfer.m_pNext = nullptr;
DMASPI_PRINT((" DmaSpi::addTransferToQueue() : queueing transfer\n"));
ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
{
if (m_pNextTransfer == nullptr)
{
m_pNextTransfer = &transfer;
}
else
{
m_pLastTransfer->m_pNext = &transfer;
}
m_pLastTransfer = &transfer;
}
}
static void post_finishCurrentTransfer() {DMASPI_INSTANCE::post_finishCurrentTransfer_impl();}
static void finishCurrentTransfer()
{
if (m_pCurrentTransfer->m_pSelect != nullptr)
{
m_pCurrentTransfer->m_pSelect->deselect();
}
else
{
m_Spi.endTransaction();
}
m_pCurrentTransfer->m_state = Transfer::State::eDone;
DMASPI_PRINT((" finishCurrentTransfer() @ %p\n", m_pCurrentTransfer));
m_pCurrentTransfer = nullptr;
post_finishCurrentTransfer();
}
static bool createDmaChannels()
{
if (txChannel_() == nullptr)
{
return false;
}
if (rxChannel_() == nullptr)
{
delete txChannel_();
return false;
}
return true;
}
static void destroyDmaChannels()
{
if (rxChannel_() != nullptr)
{
delete rxChannel_();
}
if (txChannel_() != nullptr)
{
delete txChannel_();
}
}
static DMAChannel* rxChannel_()
{
static DMAChannel* pChannel = new DMAChannel();
return pChannel;
}
static DMAChannel* txChannel_()
{
static DMAChannel* pChannel = new DMAChannel();
return pChannel;
}
static void rxIsr_()
{
DMASPI_PRINT(("DmaSpi::rxIsr_()\n"));
rxChannel_()->clearInterrupt();
// end current transfer: deselect and mark as done
finishCurrentTransfer();
DMASPI_PRINT((" state = "));
switch(state_)
{
case eStopped: // this should not happen!
DMASPI_PRINT(("eStopped\n"));
state_ = eError;
break;
case eRunning:
DMASPI_PRINT(("eRunning\n"));
beginPendingTransfer();
break;
case eStopping:
DMASPI_PRINT(("eStopping\n"));
state_ = eStopped;
break;
case eError:
DMASPI_PRINT(("eError\n"));
break;
default:
DMASPI_PRINT(("eUnknown\n"));
state_ = eError;
break;
}
}
static void pre_cs() {DMASPI_INSTANCE::pre_cs_impl();}
static void post_cs() {DMASPI_INSTANCE::post_cs_impl();}
static void beginPendingTransfer()
{
if (m_pNextTransfer == nullptr)
{
DMASPI_PRINT(("DmaSpi::beginNextTransfer: no pending transfer\n"));
return;
}
m_pCurrentTransfer = m_pNextTransfer;
DMASPI_PRINT(("DmaSpi::beginNextTransfer: starting transfer @ %p\n", m_pCurrentTransfer));
m_pCurrentTransfer->m_state = Transfer::State::inProgress;
m_pNextTransfer = m_pNextTransfer->m_pNext;
if (m_pNextTransfer == nullptr)
{
DMASPI_PRINT((" this was the last in the queue\n"));
m_pLastTransfer = nullptr;
}
// configure Rx DMA
if (m_pCurrentTransfer->m_pDest != nullptr)
{
// real data sink
DMASPI_PRINT((" real sink\n"));
rxChannel_()->destinationBuffer(m_pCurrentTransfer->m_pDest,
m_pCurrentTransfer->m_transferCount);
}
else
{
// dummy data sink
DMASPI_PRINT((" dummy sink\n"));
rxChannel_()->destination(m_devNull);
rxChannel_()->transferCount(m_pCurrentTransfer->m_transferCount);
}
// configure Tx DMA
if (m_pCurrentTransfer->m_pSource != nullptr)
{
// real data source
DMASPI_PRINT((" real source\n"));
txChannel_()->sourceBuffer(m_pCurrentTransfer->m_pSource,
m_pCurrentTransfer->m_transferCount);
}
else
{
// dummy data source
DMASPI_PRINT((" dummy source\n"));
txChannel_()->source(m_pCurrentTransfer->m_fill);
txChannel_()->transferCount(m_pCurrentTransfer->m_transferCount);
}
pre_cs();
// Select Chip
if (m_pCurrentTransfer->m_pSelect != nullptr)
{
m_pCurrentTransfer->m_pSelect->select();
}
else
{
m_Spi.beginTransaction(SPISettings());
}
post_cs();
}
static size_t init_count_;
static volatile EState state_;
static Transfer* volatile m_pCurrentTransfer;
static Transfer* volatile m_pNextTransfer;
static Transfer* volatile m_pLastTransfer;
static volatile uint8_t m_devNull;
//static SPICLASS& m_Spi;
};
template<typename DMASPI_INSTANCE, typename SPICLASS, SPICLASS& m_Spi>
size_t AbstractDmaSpi<DMASPI_INSTANCE, SPICLASS, m_Spi>::init_count_ = 0;
template<typename DMASPI_INSTANCE, typename SPICLASS, SPICLASS& m_Spi>
volatile typename AbstractDmaSpi<DMASPI_INSTANCE, SPICLASS, m_Spi>::EState AbstractDmaSpi<DMASPI_INSTANCE, SPICLASS, m_Spi>::state_ = eError;
template<typename DMASPI_INSTANCE, typename SPICLASS, SPICLASS& m_Spi>
typename AbstractDmaSpi<DMASPI_INSTANCE, SPICLASS, m_Spi>::Transfer* volatile AbstractDmaSpi<DMASPI_INSTANCE, SPICLASS, m_Spi>::m_pNextTransfer = nullptr;
template<typename DMASPI_INSTANCE, typename SPICLASS, SPICLASS& m_Spi>
typename AbstractDmaSpi<DMASPI_INSTANCE, SPICLASS, m_Spi>::Transfer* volatile AbstractDmaSpi<DMASPI_INSTANCE, SPICLASS, m_Spi>::m_pCurrentTransfer = nullptr;
template<typename DMASPI_INSTANCE, typename SPICLASS, SPICLASS& m_Spi>
typename AbstractDmaSpi<DMASPI_INSTANCE, SPICLASS, m_Spi>::Transfer* volatile AbstractDmaSpi<DMASPI_INSTANCE, SPICLASS, m_Spi>::m_pLastTransfer = nullptr;
template<typename DMASPI_INSTANCE, typename SPICLASS, SPICLASS& m_Spi>
volatile uint8_t AbstractDmaSpi<DMASPI_INSTANCE, SPICLASS, m_Spi>::m_devNull = 0;
#if defined(KINETISK)
class DmaSpi0 : public AbstractDmaSpi<DmaSpi0, SPIClass, SPI>
{
public:
static void begin_setup_txChannel_impl()
{
txChannel_()->disable();
txChannel_()->destination((volatile uint8_t&)SPI0_PUSHR);
txChannel_()->disableOnCompletion();
txChannel_()->triggerAtHardwareEvent(DMAMUX_SOURCE_SPI0_TX);
}
static void begin_setup_rxChannel_impl()
{
rxChannel_()->disable();
rxChannel_()->source((volatile uint8_t&)SPI0_POPR);
rxChannel_()->disableOnCompletion();
rxChannel_()->triggerAtHardwareEvent(DMAMUX_SOURCE_SPI0_RX);
rxChannel_()->attachInterrupt(rxIsr_);
rxChannel_()->interruptAtCompletion();
}
static void pre_cs_impl()
{
SPI0_SR = 0xFF0F0000;
SPI0_RSER = SPI_RSER_RFDF_RE | SPI_RSER_RFDF_DIRS | SPI_RSER_TFFF_RE | SPI_RSER_TFFF_DIRS;
}
static void post_cs_impl()
{
rxChannel_()->enable();
txChannel_()->enable();
}
static void post_finishCurrentTransfer_impl()
{
SPI0_RSER = 0;
SPI0_SR = 0xFF0F0000;
}
private:
};
extern DmaSpi0 DMASPI0;
#if defined(__MK64FX512__) || defined(__MK66FX1M0__)
class DmaSpi1 : public AbstractDmaSpi<DmaSpi1, SPIClass, SPI1>
{
public:
static void begin_setup_txChannel_impl()
{
txChannel_()->disable();
txChannel_()->destination((volatile uint8_t&)SPI1_PUSHR);
txChannel_()->disableOnCompletion();
txChannel_()->triggerAtHardwareEvent(DMAMUX_SOURCE_SPI1_TX);
}
static void begin_setup_rxChannel_impl()
{
rxChannel_()->disable();
rxChannel_()->source((volatile uint8_t&)SPI1_POPR);
rxChannel_()->disableOnCompletion();
rxChannel_()->triggerAtHardwareEvent(DMAMUX_SOURCE_SPI1_RX);
rxChannel_()->attachInterrupt(rxIsr_);
rxChannel_()->interruptAtCompletion();
}
static void pre_cs_impl()
{
SPI1_SR = 0xFF0F0000;
SPI1_RSER = SPI_RSER_RFDF_RE | SPI_RSER_RFDF_DIRS | SPI_RSER_TFFF_RE | SPI_RSER_TFFF_DIRS;
}
static void post_cs_impl()
{
rxChannel_()->enable();
txChannel_()->enable();
}
static void post_finishCurrentTransfer_impl()
{
SPI1_RSER = 0;
SPI1_SR = 0xFF0F0000;
}
private:
};
/*
class DmaSpi2 : public AbstractDmaSpi<DmaSpi2, SPIClass, SPI2>
{
public:
static void begin_setup_txChannel_impl()
{
txChannel_()->disable();
txChannel_()->destination((volatile uint8_t&)SPI2_PUSHR);
txChannel_()->disableOnCompletion();
txChannel_()->triggerAtHardwareEvent(DMAMUX_SOURCE_SPI2_TX);
}
static void begin_setup_rxChannel_impl()
{
rxChannel_()->disable();
rxChannel_()->source((volatile uint8_t&)SPI2_POPR);
rxChannel_()->disableOnCompletion();
rxChannel_()->triggerAtHardwareEvent(DMAMUX_SOURCE_SPI2_RX);
rxChannel_()->attachInterrupt(rxIsr_);
rxChannel_()->interruptAtCompletion();
}
static void pre_cs_impl()
{
SPI2_SR = 0xFF0F0000;
SPI2_RSER = SPI_RSER_RFDF_RE | SPI_RSER_RFDF_DIRS | SPI_RSER_TFFF_RE | SPI_RSER_TFFF_DIRS;
}
static void post_cs_impl()
{
rxChannel_()->enable();
txChannel_()->enable();
}
static void post_finishCurrentTransfer_impl()
{
SPI2_RSER = 0;
SPI2_SR = 0xFF0F0000;
}
private:
};
*/
extern DmaSpi1 DMASPI1;
//extern DmaSpi2 DMASPI2;
#endif // defined(__MK64FX512__) || defined(__MK66FX1M0__)
#elif defined(KINETISL)
class DmaSpi0 : public AbstractDmaSpi<DmaSpi0, SPIClass, SPI>
{
public:
static void begin_setup_txChannel_impl()
{
txChannel_()->disable();
txChannel_()->destination((volatile uint8_t&)SPI0_DL);
txChannel_()->disableOnCompletion();
txChannel_()->triggerAtHardwareEvent(DMAMUX_SOURCE_SPI0_TX);
}
static void begin_setup_rxChannel_impl()
{
rxChannel_()->disable();
rxChannel_()->source((volatile uint8_t&)SPI0_DL);
rxChannel_()->disableOnCompletion();
rxChannel_()->triggerAtHardwareEvent(DMAMUX_SOURCE_SPI0_RX);
rxChannel_()->attachInterrupt(rxIsr_);
rxChannel_()->interruptAtCompletion();
}
static void pre_cs_impl()
{
// disable SPI and enable SPI DMA requests
SPI0_C1 &= ~(SPI_C1_SPE);
SPI0_C2 |= SPI_C2_TXDMAE | SPI_C2_RXDMAE;
}
static void post_cs_impl()
{
rxChannel_()->enable();
txChannel_()->enable();
}
static void post_finishCurrentTransfer_impl()
{
SPI0_C2 = 0;
txChannel_()->clearComplete();
rxChannel_()->clearComplete();
}
private:
};
class DmaSpi1 : public AbstractDmaSpi<DmaSpi1, SPIClass, SPI1>
{
public:
static void begin_setup_txChannel_impl()
{
txChannel_()->disable();
txChannel_()->destination((volatile uint8_t&)SPI1_DL);
txChannel_()->disableOnCompletion();
txChannel_()->triggerAtHardwareEvent(DMAMUX_SOURCE_SPI1_TX);
}
static void begin_setup_rxChannel_impl()
{
rxChannel_()->disable();
rxChannel_()->source((volatile uint8_t&)SPI1_DL);
rxChannel_()->disableOnCompletion();
rxChannel_()->triggerAtHardwareEvent(DMAMUX_SOURCE_SPI1_RX);
rxChannel_()->attachInterrupt(rxIsr_);
rxChannel_()->interruptAtCompletion();
}
static void pre_cs_impl()
{
// disable SPI and enable SPI DMA requests
SPI1_C1 &= ~(SPI_C1_SPE);
SPI1_C2 |= SPI_C2_TXDMAE | SPI_C2_RXDMAE;
}
// static void dumpCFG(const char *sz, uint32_t* p)
// {
// DMASPI_PRINT(("%s: %x %x %x %x \n", sz, p[0], p[1], p[2], p[3]));
// }
static void post_cs_impl()
{
DMASPI_PRINT(("post_cs S C1 C2: %x %x %x\n", SPI1_S, SPI1_C1, SPI1_C2));
// dumpCFG("RX", (uint32_t*)(void*)rxChannel_()->CFG);
// dumpCFG("TX", (uint32_t*)(void*)txChannel_()->CFG);
rxChannel_()->enable();
txChannel_()->enable();
}
static void post_finishCurrentTransfer_impl()
{
SPI1_C2 = 0;
txChannel_()->clearComplete();
rxChannel_()->clearComplete();
}
private:
};
extern DmaSpi0 DMASPI0;
extern DmaSpi1 DMASPI1;
#else
#error Unknown chip
#endif // KINETISK else KINETISL
#endif // DMASPI_H