aboutsummaryrefslogtreecommitdiffstats
path: root/os/hal/platforms/SPC5xx/DSPI_v1/spi_lld.c
blob: f16e6cf0aa7de97860feaee00be3f1d70e92c5f3 (plain)
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
/*
    SPC5 HAL - Copyright (C) 2013 STMicroelectronics

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

/**
 * @file    SPC5xx/DSPI_v1/spi_lld.c
 * @brief   SPC5xx SPI subsystem low level driver source.
 *
 * @addtogroup SPI
 * @{
 */

#include "ch.h"
#include "hal.h"

#if HAL_USE_SPI || defined(__DOXYGEN__)

/* Some forward declarations.*/
static void spi_serve_rx_irq(edma_channel_t channel, void *p);
static void spi_serve_tx_irq(edma_channel_t channel, void *p);
static void spi_serve_dma_error_irq(edma_channel_t channel,
                                    void *p,
                                    uint32_t esr);

/*===========================================================================*/
/* Driver local definitions.                                                 */
/*===========================================================================*/

/* Enforced MCR bits.*/
#define DSPI_MCR_ENFORCED_BITS      (SPC5_MCR_MSTR)

/* Excluded MCR bits.*/
#define DSPI_MCR_EXCLUDED_BITS      (SPC5_MCR_CONT_SCKE     |               \
                                     SPC5_MCR_DCONF_MASK    |               \
                                     SPC5_MCR_ROOE          |               \
                                     SPC5_MCR_MDIS          |               \
                                     SPC5_MCR_DIS_TXF       |               \
                                     SPC5_MCR_DIS_RXF       |               \
                                     SPC5_MCR_CLR_TXF       |               \
                                     SPC5_MCR_CLR_RXF       |               \
                                     SPC5_MCR_HALT)

/* Excluded PUSHR bits.*/
#define DSPI_PUSHR_EXCLUDED_BITS    (SPC5_PUSHR_CTAS_MASK   |               \
                                     SPC5_PUSHR_EOQ         |               \
                                     SPC5_PUSHR_TXDATA_MASK)

#define DSPI_POPR8_ADDRESS(spip)    (((uint32_t)&(spip)->dspi->POPR.R) + 3)
#define DSPI_POPR16_ADDRESS(spip)   (((uint32_t)&(spip)->dspi->POPR.R) + 2)

/*===========================================================================*/
/* Driver exported variables.                                                */
/*===========================================================================*/

/**
 * @brief   SPID1 driver identifier.
 */
#if SPC5_SPI_USE_DSPI0 || defined(__DOXYGEN__)
SPIDriver SPID1;
#endif

/**
 * @brief   SPID2 driver identifier.
 */
#if SPC5_SPI_USE_DSPI1 || defined(__DOXYGEN__)
SPIDriver SPID2;
#endif

/**
 * @brief   SPID3 driver identifier.
 */
#if SPC5_SPI_USE_DSPI2 || defined(__DOXYGEN__)
SPIDriver SPID3;
#endif

/**
 * @brief   SPID4 driver identifier.
 */
#if SPC5_SPI_USE_DSPI3 || defined(__DOXYGEN__)
SPIDriver SPID4;
#endif

/*===========================================================================*/
/* Driver local variables and types.                                         */
/*===========================================================================*/

#if SPC5_SPI_USE_DSPI0 || defined(__DOXYGEN__)
/**
 * @brief   DMA configuration for DSPI0 TX1.
 */
static const edma_channel_config_t spi_dspi0_tx1_dma_config = {
  SPC5_DSPI0_TX1_DMA_DEV_ID, SPC5_SPI_DSPI0_DMA_PRIO, SPC5_SPI_DSPI0_DMA_IRQ_PRIO,
  NULL, spi_serve_dma_error_irq, &SPID1
};

/**
 * @brief   DMA configuration for DSPI0 TX2.
 */
static const edma_channel_config_t spi_dspi0_tx2_dma_config = {
  SPC5_DSPI0_TX2_DMA_DEV_ID, SPC5_SPI_DSPI0_DMA_PRIO, SPC5_SPI_DSPI0_DMA_IRQ_PRIO,
  spi_serve_tx_irq, spi_serve_dma_error_irq, &SPID1
};

/**
 * @brief   DMA configuration for DSPI0 RX.
 */
static const edma_channel_config_t spi_dspi0_rx_dma_config = {
  SPC5_DSPI0_RX_DMA_DEV_ID, SPC5_SPI_DSPI0_DMA_PRIO, SPC5_SPI_DSPI0_DMA_IRQ_PRIO,
  spi_serve_rx_irq, spi_serve_dma_error_irq, &SPID1
};
#endif /* SPC5_SPI_USE_DSPI0 */

#if SPC5_SPI_USE_DSPI1 || defined(__DOXYGEN__)
/**
 * @brief   DMA configuration for DSPI1 TX1.
 */
static const edma_channel_config_t spi_dspi1_tx1_dma_config = {
  SPC5_DSPI1_TX1_DMA_DEV_ID, SPC5_SPI_DSPI1_DMA_PRIO, SPC5_SPI_DSPI1_DMA_IRQ_PRIO,
  NULL, spi_serve_dma_error_irq, &SPID2
};

/**
 * @brief   DMA configuration for DSPI1 TX2.
 */
static const edma_channel_config_t spi_dspi1_tx2_dma_config = {
  SPC5_DSPI1_TX2_DMA_DEV_ID, SPC5_SPI_DSPI1_DMA_PRIO, SPC5_SPI_DSPI1_DMA_IRQ_PRIO,
  spi_serve_tx_irq, spi_serve_dma_error_irq, &SPID2
};

/**
 * @brief   DMA configuration for DSPI1 RX.
 */
static const edma_channel_config_t spi_dspi1_rx_dma_config = {
  SPC5_DSPI1_RX_DMA_DEV_ID, SPC5_SPI_DSPI1_DMA_PRIO, SPC5_SPI_DSPI1_DMA_IRQ_PRIO,
  spi_serve_rx_irq, spi_serve_dma_error_irq, &SPID2
};
#endif /* SPC5_SPI_USE_DSPI1 */

#if SPC5_SPI_USE_DSPI2 || defined(__DOXYGEN__)
/**
 * @brief   DMA configuration for DSPI2 TX1.
 */
static const edma_channel_config_t spi_dspi2_tx1_dma_config = {
  SPC5_DSPI2_TX1_DMA_DEV_ID, SPC5_SPI_DSPI2_DMA_PRIO, SPC5_SPI_DSPI2_DMA_IRQ_PRIO,
  NULL, spi_serve_dma_error_irq, &SPID3
};

/**
 * @brief   DMA configuration for DSPI2 TX2.
 */
static const edma_channel_config_t spi_dspi2_tx2_dma_config = {
  SPC5_DSPI2_TX2_DMA_DEV_ID, SPC5_SPI_DSPI2_DMA_PRIO, SPC5_SPI_DSPI2_DMA_IRQ_PRIO,
  spi_serve_tx_irq, spi_serve_dma_error_irq, &SPID3
};

/**
 * @brief   DMA configuration for DSPI2 RX.
 */
static const edma_channel_config_t spi_dspi2_rx_dma_config = {
  SPC5_DSPI2_RX_DMA_DEV_ID, SPC5_SPI_DSPI2_DMA_PRIO, SPC5_SPI_DSPI2_DMA_IRQ_PRIO,
  spi_serve_rx_irq, spi_serve_dma_error_irq, &SPID3
};
#endif /* SPC5_SPI_USE_DSPI2 */

#if SPC5_SPI_USE_DSPI3 || defined(__DOXYGEN__)
/**
 * @brief   DMA configuration for DSPI3 TX1.
 */
static const edma_channel_config_t spi_dspi3_tx1_dma_config = {
  SPC5_DSPI3_TX1_DMA_DEV_ID, SPC5_SPI_DSPI3_DMA_PRIO, SPC5_SPI_DSPI3_DMA_IRQ_PRIO,
  NULL, spi_serve_dma_error_irq, &SPID4
};

/**
 * @brief   DMA configuration for DSPI3 TX2.
 */
static const edma_channel_config_t spi_dspi3_tx2_dma_config = {
  SPC5_DSPI3_TX2_DMA_DEV_ID, SPC5_SPI_DSPI3_DMA_PRIO, SPC5_SPI_DSPI3_DMA_IRQ_PRIO,
  spi_serve_tx_irq, spi_serve_dma_error_irq, &SPID4
};

/**
 * @brief   DMA configuration for DSPI3 RX.
 */
static const edma_channel_config_t spi_dspi3_rx_dma_config = {
  SPC5_DSPI3_RX_DMA_DEV_ID, SPC5_SPI_DSPI3_DMA_PRIO, SPC5_SPI_DSPI3_DMA_IRQ_PRIO,
  spi_serve_rx_irq, spi_serve_dma_error_irq, &SPID4
};
#endif /* SPC5_SPI_USE_DSPI3 */

/*===========================================================================*/
/* Driver local functions.                                                   */
/*===========================================================================*/

/**
 * @brief   Starts reception using DMA for frames up to 8 bits.
 *
 * @param[in] spip      pointer to the @p SPIDriver object
 * @param[in] n         number of words to be exchanged
 * @param[out] rxbuf    the pointer to the receive buffer
 *
 * @notapi
 */
static void spi_start_dma_rx8(SPIDriver *spip,
                              size_t n,
                              uint8_t *rxbuf) {

  edmaChannelSetup(spip->rx_channel,            /* channel.                 */
                   DSPI_POPR8_ADDRESS(spip),    /* src.                     */
                   rxbuf,                       /* dst.                     */
                   0,                           /* soff, do not advance.    */
                   1,                           /* doff, advance by one.    */
                   0,                           /* ssize, 8 bits transfers. */
                   0,                           /* dsize, 8 bits transfers. */
                   1,                           /* nbytes, always one.      */
                   n,                           /* iter.                    */
                   0,                           /* slast.                   */
                   0,                           /* dlast.                   */
                   EDMA_TCD_MODE_DREQ | EDMA_TCD_MODE_INT_END);     /* mode.*/

  edmaChannelStart(spip->rx_channel);
}

/**
 * @brief   Starts reception using DMA for frames up to 16 bits.
 *
 * @param[in] spip      pointer to the @p SPIDriver object
 * @param[in] n         number of words to be exchanged
 * @param[out] rxbuf    the pointer to the receive buffer
 *
 * @notapi
 */
static void spi_start_dma_rx16(SPIDriver *spip,
                               size_t n,
                               uint16_t *rxbuf) {

  edmaChannelSetup(spip->rx_channel,            /* channel.                 */
                   DSPI_POPR16_ADDRESS(spip),   /* src.                     */
                   rxbuf,                       /* dst.                     */
                   0,                           /* soff, do not advance.    */
                   2,                           /* doff, advance by two.    */
                   1,                           /* ssize, 16 bits transfers.*/
                   1,                           /* dsize, 16 bits transfers.*/
                   2,                           /* nbytes, always two.      */
                   n,                           /* iter.                    */
                   0,                           /* slast, no source adjust. */
                   0,                           /* dlast.                   */
                   EDMA_TCD_MODE_DREQ | EDMA_TCD_MODE_INT_END); /* mode.    */

  edmaChannelStart(spip->rx_channel);
}

/**
 * @brief   Starts transmission using DMA for frames up to 8 bits.
 *
 * @param[in] spip      pointer to the @p SPIDriver object
 * @param[in] n         number of words to be exchanged
 * @param[in] txbuf     the pointer to the transmit buffer
 *
 * @notapi
 */
static void spi_start_dma_tx8(SPIDriver *spip,
                              size_t n,
                              const uint8_t *txbuf) {

  /* Preparing the TX intermediate buffer with the fixed part.*/
  spip->tx_intbuf = spip->config->pushr;

  /* The first frame is pushed by the CPU, then the DMA is activated to
     send the following frames. This should reduce latency on the operation
     start.*/
  spip->dspi->PUSHR.R = spip->tx_intbuf | (uint32_t)*txbuf;

  /* Setting up TX1 DMA TCD parameters for 8 bits transfers.*/
  edmaChannelSetupLinked(
                   spip->tx1_channel,           /* channel.                 */
                   spip->tx2_channel,           /* linkch.                  */
                   txbuf + 1,                   /* src.                     */
                   ((const uint8_t *)&spip->tx_intbuf) + 3,     /* dst.     */
                   1,                           /* soff, advance by 1.      */
                   0,                           /* doff, do not advance.    */
                   0,                           /* ssize, 8 bits transfers. */
                   0,                           /* dsize, 8 bits transfers. */
                   1,                           /* nbytes, always one.      */
                   n - 2,                       /* iter.                    */
                   0,                           /* slast, no source adjust. */
                   0,                           /* dlast, no dest.adjust.   */
                   EDMA_TCD_MODE_DREQ);         /* mode.                    */

  /* Setting up TX2 DMA TCD parameters for 32 bits transfers.*/
  edmaChannelSetup(spip->tx2_channel,           /* channel.                 */
                   &spip->tx_intbuf,            /* src.                     */
                   &spip->dspi->PUSHR.R,        /* dst.                     */
                   0,                           /* soff, do not advance.    */
                   0,                           /* doff, do not advance.    */
                   2,                           /* ssize, 32 bits transfers.*/
                   2,                           /* dsize, 32 bits transfers.*/
                   4,                           /* nbytes, always four.     */
                   n - 2,                       /* iter.                    */
                   0,                           /* slast, no source adjust. */
                   0,                           /* dlast, no dest.adjust.   */
                   EDMA_TCD_MODE_DREQ | EDMA_TCD_MODE_INT_END); /* mode.    */

  /* The last frame will be pushed by the TX DMA operation completion
     callback.*/
  spip->tx_last = txbuf[n - 1];

  /* Starting TX DMA channels.*/
  edmaChannelStart(spip->tx2_channel);
  edmaChannelStart(spip->tx1_channel);
}

/**
 * @brief   Starts transmission using DMA for frames up to 16 bits.
 *
 * @param[in] spip      pointer to the @p SPIDriver object
 * @param[in] n         number of words to be exchanged
 * @param[in] txbuf     the pointer to the transmit buffer
 *
 * @notapi
 */
static void spi_start_dma_tx16(SPIDriver *spip,
                               size_t n,
                               const uint16_t *txbuf) {

  /* Preparing the TX intermediate buffer with the fixed part.*/
  spip->tx_intbuf = spip->config->pushr;

  /* The first frame is pushed by the CPU, then the DMA is activated to
     send the following frames. This should reduce latency on the operation
     start.*/
  spip->dspi->PUSHR.R = spip->tx_intbuf | (uint32_t)*txbuf;

  /* Setting up TX1 DMA TCD parameters for 8 bits transfers.*/
  edmaChannelSetupLinked(
                   spip->tx1_channel,           /* channel.                 */
                   spip->tx2_channel,           /* linkch.                  */
                   txbuf + 1,                   /* src.                     */
                   ((const uint8_t *)&spip->tx_intbuf) + 2,     /* dst.     */
                   1,                           /* soff, advance by 1.      */
                   0,                           /* doff, do not advance.    */
                   1,                           /* ssize, 16 bits transfers.*/
                   1,                           /* dsize, 16 bits transfers.*/
                   1,                           /* nbytes, always one.      */
                   n - 2,                       /* iter.                    */
                   0,                           /* slast, no source adjust. */
                   0,                           /* dlast, no dest.adjust.   */
                   EDMA_TCD_MODE_DREQ);         /* mode.                    */

  /* Setting up TX2 DMA TCD parameters for 32 bits transfers.*/
  edmaChannelSetup(spip->tx2_channel,           /* channel.                 */
                   &spip->tx_intbuf,            /* src.                     */
                   &spip->dspi->PUSHR.R,        /* dst.                     */
                   0,                           /* soff, do not advance.    */
                   0,                           /* doff, do not advance.    */
                   2,                           /* ssize, 32 bits transfers.*/
                   2,                           /* dsize, 32 bits transfers.*/
                   4,                           /* nbytes, always four.     */
                   n - 2,                       /* iter.                    */
                   0,                           /* slast, no source adjust. */
                   0,                           /* dlast, no dest.adjust.   */
                   EDMA_TCD_MODE_DREQ | EDMA_TCD_MODE_INT_END); /* mode.    */

  /* The last frame will be pushed by the TX DMA operation completion
     callback.*/
  spip->tx_last = txbuf[n - 1];

  /* Starting TX DMA channels.*/
  edmaChannelStart(spip->tx2_channel);
  edmaChannelStart(spip->tx1_channel);
}

/**
 * @brief   Starts transmission using FIFO pre-filling for frames up to 8 bits.
 *
 * @param[in] spip      pointer to the @p SPIDriver object
 * @param[in] n         number of words to be exchanged
 * @param[in] txbuf     the pointer to the transmit buffer
 *
 * @notapi
 */
static void spi_tx_prefill8(SPIDriver *spip,
                            size_t n,
                            const uint8_t *txbuf) {
  uint32_t cmd = spip->config->pushr;

  do {
    if (--n == 0) {
      spip->dspi->PUSHR.R = (SPC5_PUSHR_EOQ | cmd | (uint32_t)*txbuf) &
                            ~SPC5_PUSHR_CONT;
      break;
    }
    spip->dspi->PUSHR.R = cmd | (uint32_t)*txbuf;
    txbuf++;
  } while (TRUE);
}

/**
 * @brief   Starts transmission using FIFO pre-filling for frames up to 16 bits.
 *
 * @param[in] spip      pointer to the @p SPIDriver object
 * @param[in] n         number of words to be exchanged
 * @param[in] txbuf     the pointer to the transmit buffer
 *
 * @notapi
 */
static void spi_tx_prefill16(SPIDriver *spip,
                             size_t n,
                             const uint16_t *txbuf) {
  uint32_t cmd = spip->config->pushr;

  do {
    if (--n == 0) {
      spip->dspi->PUSHR.R = SPC5_PUSHR_EOQ | cmd | (uint32_t)*txbuf;
      break;
    }
    spip->dspi->PUSHR.R = cmd | (uint32_t)*txbuf;
    txbuf++;
  } while (TRUE);
}

/**
 * @brief   Shared RX DMA events service routine.
 *
 * @param[in] channel   the channel number
 * @param[in] p         parameter for the registered function
 *
 * @notapi
 */
static void spi_serve_rx_irq(edma_channel_t channel, void *p) {
  SPIDriver *spip = (SPIDriver *)p;

  (void)channel;

  /* Stops the DSPI and clears the queues.*/
  spip->dspi->MCR.R     = DSPI_MCR_ENFORCED_BITS | SPC5_MCR_HALT |
                          SPC5_MCR_CLR_TXF | SPC5_MCR_CLR_RXF |
                          spip->config->mcr;

  /* Portable SPI ISR code defined in the high level driver, note, it is
     a macro.*/
  _spi_isr_code(spip);
}

/**
 * @brief   Shared TX2 DMA events service routine.
 *
 * @param[in] channel   the channel number
 * @param[in] p         parameter for the registered function
 *
 * @notapi
 */
static void spi_serve_tx_irq(edma_channel_t channel, void *p) {
  SPIDriver *spip = (SPIDriver *)p;

  (void)channel;

  /* If the TX FIFO is full then the push of the last frame is delagated to
     an interrupt handler else it is performed immediately. Both conditions
     can be true depending on the SPI speed and ISR latency.*/
  if (spip->dspi->SR.B.TFFF) {
    spip->dspi->PUSHR.R = (spip->config->pushr | spip->tx_last | SPC5_PUSHR_EOQ) &
                          ~SPC5_PUSHR_CONT;
  }
  else {
    spip->dspi->RSER.B.TFFFDIRS = 0;
  }
}

/**
 * @brief   Shared ISR for DMA error events.
 *
 * @param[in] channel   the channel number
 * @param[in] p         parameter for the registered function
 * @param[in] esr       content of the ESR register
 *
 * @notapi
 */
static void spi_serve_dma_error_irq(edma_channel_t channel,
                                    void *p,
                                    uint32_t esr) {
  SPIDriver *spip = (SPIDriver *)p;

  (void)channel;
  (void)esr;

  /* Stops the DSPI and clears the queues.*/
  spip->dspi->MCR.R = DSPI_MCR_ENFORCED_BITS | SPC5_MCR_HALT |
                      SPC5_MCR_CLR_TXF | SPC5_MCR_CLR_RXF;

  edmaChannelStop(spip->tx1_channel);
  edmaChannelStop(spip->tx2_channel);
  edmaChannelStop(spip->rx_channel);

  SPC5_SPI_DMA_ERROR_HOOK(spip);
}

/*===========================================================================*/
/* Driver interrupt handlers.                                                */
/*===========================================================================*/

#if SPC5_SPI_USE_DSPI0 || defined(__DOXYGEN__)
#if !defined(SPC5_DSPI0_TFFF_HANDLER)
#error "SPC5_DSPI0_TFFF_HANDLER not defined"
#endif
/**
 * @brief   DSPI0 TFFF interrupt handler.
 *
 * @isr
 */
CH_IRQ_HANDLER(SPC5_DSPI0_TFFF_HANDLER) {

  CH_IRQ_PROLOGUE();

  chSysLockFromIsr();

  /* Interrupt served and back to DMA mode.*/
  SPC5_DSPI0.RSER.B.TFFFDIRS = 1;
  SPC5_DSPI0.SR.B.TFFF = 1;

  /* Pushing last frame.*/
  SPC5_DSPI0.PUSHR.R = (SPID1.config->pushr | SPID1.tx_last | SPC5_PUSHR_EOQ) &
                        ~SPC5_PUSHR_CONT;

  chSysUnlockFromIsr();

  CH_IRQ_EPILOGUE();
}
#endif /* SPC5_SPI_USE_DSPI0 */

#if SPC5_SPI_USE_DSPI1 || defined(__DOXYGEN__)
#if !defined(SPC5_DSPI1_TFFF_HANDLER)
#error "SPC5_DSPI1_TFFF_HANDLER not defined"
#endif
/**
 * @brief   DSPI1 TFFF interrupt handler.
 *
 * @isr
 */
CH_IRQ_HANDLER(SPC5_DSPI1_TFFF_HANDLER) {

  CH_IRQ_PROLOGUE();

  chSysLockFromIsr();

  /* Interrupt served and back to DMA mode.*/
  SPC5_DSPI1.RSER.B.TFFFDIRS = 1;
  SPC5_DSPI1.SR.B.TFFF = 1;

  /* Pushing last frame.*/
  SPC5_DSPI1.PUSHR.R = (SPID2.config->pushr | SPID2.tx_last | SPC5_PUSHR_EOQ) &
                        ~SPC5_PUSHR_CONT;

  chSysUnlockFromIsr();

  CH_IRQ_EPILOGUE();
}
#endif /* SPC5_SPI_USE_DSPI1 */

#if SPC5_SPI_USE_DSPI2 || defined(__DOXYGEN__)
#if !defined(SPC5_DSPI2_TFFF_HANDLER)
#error "SPC5_DSPI2_TFFF_HANDLER not defined"
#endif
/**
 * @brief   DSPI2 TFFF interrupt handler.
 *
 * @isr
 */
CH_IRQ_HANDLER(SPC5_DSPI2_TFFF_HANDLER) {

  CH_IRQ_PROLOGUE();

  chSysLockFromIsr();

  /* Interrupt served and back to DMA mode.*/
  SPC5_DSPI2.RSER.B.TFFFDIRS = 1;
  SPC5_DSPI2.SR.B.TFFF = 1;

  /* Pushing last frame.*/
  SPC5_DSPI2.PUSHR.R = (SPID3.config->pushr | SPID3.tx_last | SPC5_PUSHR_EOQ) &
                        ~SPC5_PUSHR_CONT;

  chSysUnlockFromIsr();

  CH_IRQ_EPILOGUE();
}
#endif /* SPC5_SPI_USE_DSPI2 */

#if SPC5_SPI_USE_DSPI3 || defined(__DOXYGEN__)
#if !defined(SPC5_DSPI3_TFFF_HANDLER)
#error "SPC5_DSPI3_TFFF_HANDLER not defined"
#endif
/**
 * @brief   DSPI3 TFFF interrupt handler.
 *
 * @isr
 */
CH_IRQ_HANDLER(SPC5_DSPI3_TFFF_HANDLER) {

  CH_IRQ_PROLOGUE();

  chSysLockFromIsr();

  /* Interrupt served and back to DMA mode.*/
  SPC5_DSPI3.RSER.B.TFFFDIRS = 1;
  SPC5_DSPI3.SR.B.TFFF = 1;

  /* Pushing last frame.*/
  SPC5_DSPI3.PUSHR.R = (SPID4.config->pushr | SPID4.tx_last | SPC5_PUSHR_EOQ) &
                        ~SPC5_PUSHR_CONT;

  chSysUnlockFromIsr();

  CH_IRQ_EPILOGUE();
}
#endif /* SPC5_SPI_USE_DSPI3 */

/*===========================================================================*/
/* Driver exported functions.                                                */
/*===========================================================================*/

/**
 * @brief   Low level SPI driver initialization.
 *
 * @notapi
 */
void spi_lld_init(void) {

#if SPC5_SPI_USE_DSPI0
  /* Driver initialization.*/
  spiObjectInit(&SPID1);
  SPID1.dspi = &SPC5_DSPI0;
  SPID1.tx1_channel = EDMA_ERROR;
  SPID1.tx2_channel = EDMA_ERROR;
  SPID1.rx_channel  = EDMA_ERROR;
  INTC.PSR[SPC5_DSPI0_TFFF_NUMBER].R = SPC5_SPI_DSPI0_IRQ_PRIO;
#endif /* SPC5_SPI_USE_DSPI0 */

#if SPC5_SPI_USE_DSPI1
  /* Driver initialization.*/
  spiObjectInit(&SPID2);
  SPID2.dspi = &SPC5_DSPI1;
  SPID2.tx1_channel = EDMA_ERROR;
  SPID2.tx2_channel = EDMA_ERROR;
  SPID2.rx_channel  = EDMA_ERROR;
  INTC.PSR[SPC5_DSPI1_TFFF_NUMBER].R = SPC5_SPI_DSPI1_IRQ_PRIO;
#endif /* SPC5_SPI_USE_DSPI1 */

#if SPC5_SPI_USE_DSPI2
  /* Driver initialization.*/
  spiObjectInit(&SPID3);
  SPID3.dspi = &SPC5_DSPI2;
  SPID3.tx1_channel = EDMA_ERROR;
  SPID3.tx2_channel = EDMA_ERROR;
  SPID3.rx_channel  = EDMA_ERROR;
  INTC.PSR[SPC5_DSPI2_TFFF_NUMBER].R = SPC5_SPI_DSPI2_IRQ_PRIO;
#endif /* SPC5_SPI_USE_DSPI2 */

#if SPC5_SPI_USE_DSPI03
  /* Driver initialization.*/
  spiObjectInit(&SPID4);
  SPID4.dspi = &SPC5_DSPI3;
  SPID4.tx1_channel = EDMA_ERROR;
  SPID4.tx2_channel = EDMA_ERROR;
  SPID4.rx_channel  = EDMA_ERROR;
  INTC.PSR[SPC5_DSPI3_TFFF_NUMBER].R = SPC5_SPI_DSPI3_IRQ_PRIO;
#endif /* SPC5_SPI_USE_DSPI3 */
}

/**
 * @brief   Configures and activates the SPI peripheral.
 *
 * @param[in] spip      pointer to the @p SPIDriver object
 *
 * @notapi
 */
void spi_lld_start(SPIDriver *spip) {

  chDbgAssert((spip->config->pushr & DSPI_PUSHR_EXCLUDED_BITS) == 0,
              "spi_lld_start(), #1", "invalid PUSHR bits specified");
  chDbgAssert((spip->config->mcr & DSPI_MCR_EXCLUDED_BITS) == 0,
              "spi_lld_start(), #2", "invalid PUSHR bits specified");

  if (spip->state == SPI_STOP) {
    /* Enables the peripheral.*/

#if SPC5_SPI_USE_DSPI0
    if (&SPID1 == spip) {
      SPC5_DSPI0_ENABLE_CLOCK();
      spip->tx1_channel = edmaChannelAllocate(&spi_dspi0_tx1_dma_config);
      spip->tx2_channel = edmaChannelAllocate(&spi_dspi0_tx2_dma_config);
      spip->rx_channel = edmaChannelAllocate(&spi_dspi0_rx_dma_config);
    }
#endif /* SPC5_SPI_USE_DSPI0 */

#if SPC5_SPI_USE_DSPI1
    if (&SPID2 == spip) {
      SPC5_DSPI1_ENABLE_CLOCK();
      spip->tx1_channel = edmaChannelAllocate(&spi_dspi1_tx1_dma_config);
      spip->tx2_channel = edmaChannelAllocate(&spi_dspi1_tx2_dma_config);
      spip->rx_channel = edmaChannelAllocate(&spi_dspi1_rx_dma_config);
    }
#endif /* SPC5_SPI_USE_DSPI1 */

#if SPC5_SPI_USE_DSPI2
    if (&SPID3 == spip) {
      SPC5_DSPI2_ENABLE_CLOCK();
      spip->tx1_channel = edmaChannelAllocate(&spi_dspi2_tx1_dma_config);
      spip->tx2_channel = edmaChannelAllocate(&spi_dspi2_tx2_dma_config);
      spip->rx_channel = edmaChannelAllocate(&spi_dspi2_rx_dma_config);
    }
#endif /* SPC5_SPI_USE_DSPI2 */

#if SPC5_SPI_USE_DSPI3
    if (&SPID4 == spip) {
      SPC5_DSPI3_ENABLE_CLOCK();
      spip->tx1_channel = edmaChannelAllocate(&spi_dspi3_tx1_dma_config);
      spip->tx2_channel = edmaChannelAllocate(&spi_dspi3_tx2_dma_config);
      spip->rx_channel = edmaChannelAllocate(&spi_dspi3_rx_dma_config);
    }
#endif /* SPC5_SPI_USE_DSPI3 */

    chDbgAssert((spip->tx1_channel != EDMA_ERROR) &&
                (spip->tx2_channel != EDMA_ERROR) &&
                (spip->rx_channel != EDMA_ERROR),
                "spi_lld_start(), #3", "channel cannot be allocated");
  }

  /* Configures the peripheral.*/
  spip->dspi->MCR.R     = DSPI_MCR_ENFORCED_BITS | SPC5_MCR_HALT |
                          spip->config->mcr;
  spip->dspi->CTAR[0].R = spip->config->ctar0;
  spip->dspi->RSER.R    = SPC5_RSER_TFFF_RE | SPC5_RSER_TFFF_DIRS |
                          SPC5_RSER_RFDF_RE | SPC5_RSER_RFDF_DIRS;
  spip->dspi->SR.R      = spip->dspi->SR.R;
}

/**
 * @brief   Deactivates the SPI peripheral.
 *
 * @param[in] spip      pointer to the @p SPIDriver object
 *
 * @notapi
 */
void spi_lld_stop(SPIDriver *spip) {

  if (spip->state == SPI_READY) {
    /* Releases the allocated EDMA channels.*/
    edmaChannelRelease(spip->tx1_channel);
    edmaChannelRelease(spip->tx2_channel);
    edmaChannelRelease(spip->rx_channel);

    /* Resets the peripheral.*/
    spip->dspi->CTAR[0].R = 0;
    spip->dspi->RSER.R    = 0;
    spip->dspi->SR.R      = spip->dspi->SR.R;
    spip->dspi->MCR.R     = DSPI_MCR_ENFORCED_BITS | SPC5_MCR_MDIS    |
                            SPC5_MCR_CLR_TXF | SPC5_MCR_CLR_RXF |
                            SPC5_MCR_HALT;

#if SPC5_SPI_USE_DSPI0
    if (&SPID1 == spip) {
      SPC5_DSPI0_DISABLE_CLOCK();
    }
#endif /* SPC5_SPI_USE_DSPI0 */

#if SPC5_SPI_USE_DSPI1
    if (&SPID2 == spip) {
      SPC5_DSPI1_DISABLE_CLOCK();
    }
#endif /* SPC5_SPI_USE_DSPI1 */

#if SPC5_SPI_USE_DSPI2
    if (&SPID3 == spip) {
      SPC5_DSPI2_DISABLE_CLOCK();
    }
#endif /* SPC5_SPI_USE_DSPI2 */

#if SPC5_SPI_USE_DSPI3
    if (&SPID4 == spip) {
      SPC5_DSPI3_DISABLE_CLOCK();
    }
#endif /* SPC5_SPI_USE_DSPI3 */
  }
}

/**
 * @brief   Asserts the slave select signal and prepares for transfers.
 *
 * @param[in] spip      pointer to the @p SPIDriver object
 *
 * @notapi
 */
void spi_lld_select(SPIDriver *spip) {

  (void)spip;

}

/**
 * @brief   Deasserts the slave select signal.
 * @details The previously selected peripheral is unselected.
 *
 * @param[in] spip      pointer to the @p SPIDriver object
 *
 * @notapi
 */
void spi_lld_unselect(SPIDriver *spip) {

  (void)spip;

}

/**
 * @brief   Ignores data on the SPI bus.
 * @details This asynchronous function starts the transmission of a series of
 *          idle words on the SPI bus and ignores the received data.
 * @post    At the end of the operation the configured callback is invoked.
 *
 * @param[in] spip      pointer to the @p SPIDriver object
 * @param[in] n         number of words to be ignored
 *
 * @notapi
 */
void spi_lld_ignore(SPIDriver *spip, size_t n) {

  (void)spip;
  (void)n;

}

/**
 * @brief   Exchanges data on the SPI bus.
 * @details This asynchronous function starts a simultaneous transmit/receive
 *          operation.
 * @post    At the end of the operation the configured callback is invoked.
 * @note    The buffers are organized as uint8_t arrays for data sizes below or
 *          equal to 8 bits else it is organized as uint16_t arrays.
 *
 * @param[in] spip      pointer to the @p SPIDriver object
 * @param[in] n         number of words to be exchanged
 * @param[in] txbuf     the pointer to the transmit buffer
 * @param[out] rxbuf    the pointer to the receive buffer
 *
 * @notapi
 */
void spi_lld_exchange(SPIDriver *spip, size_t n,
                      const void *txbuf, void *rxbuf) {

  /* Starting transfer.*/
  spip->dspi->SR.R  = spip->dspi->SR.R;
  spip->dspi->MCR.R = DSPI_MCR_ENFORCED_BITS | spip->config->mcr;

  /* DMAs require a different setup depending on the frame size.*/
  if (spip->dspi->CTAR[0].B.FMSZ < 8) {
    /* Setting up the RX DMA channel.*/
    spi_start_dma_rx8(spip, n, rxbuf);

    if (n <= SPC5_DSPI_FIFO_DEPTH) {
      /* If the total transfer size is smaller than the TX FIFO size then
         the whole transmitted data is pushed here and the TX DMA is not
         activated.*/
      spi_tx_prefill8(spip, n, txbuf);
    }
    else {
      spi_start_dma_tx8(spip, n, txbuf);
    }
  }
  else {
    /* Setting up the RX DMA channel.*/
    spi_start_dma_rx16(spip, n, rxbuf);

    if (n <= SPC5_DSPI_FIFO_DEPTH) {
      /* If the total transfer size is smaller than the TX FIFO size then
         the whole transmitted data is pushed here and the TX DMA is not
         activated.*/
      spi_tx_prefill16(spip, n, txbuf);
    }
    else {
      spi_start_dma_tx16(spip, n, txbuf);
    }
  }
}

/**
 * @brief   Sends data over the SPI bus.
 * @details This asynchronous function starts a transmit operation.
 * @post    At the end of the operation the configured callback is invoked.
 * @note    The buffers are organized as uint8_t arrays for data sizes below or
 *          equal to 8 bits else it is organized as uint16_t arrays.
 *
 * @param[in] spip      pointer to the @p SPIDriver object
 * @param[in] n         number of words to send
 * @param[in] txbuf     the pointer to the transmit buffer
 *
 * @notapi
 */
void spi_lld_send(SPIDriver *spip, size_t n, const void *txbuf) {

  (void)spip;
  (void)n;
  (void)txbuf;

}

/**
 * @brief   Receives data from the SPI bus.
 * @details This asynchronous function starts a receive operation.
 * @post    At the end of the operation the configured callback is invoked.
 * @note    The buffers are organized as uint8_t arrays for data sizes below or
 *          equal to 8 bits else it is organized as uint16_t arrays.
 *
 * @param[in] spip      pointer to the @p SPIDriver object
 * @param[in] n         number of words to receive
 * @param[out] rxbuf    the pointer to the receive buffer
 *
 * @notapi
 */
void spi_lld_receive(SPIDriver *spip, size_t n, void *rxbuf) {

  (void)spip;
  (void)n;
  (void)rxbuf;

}

/**
 * @brief   Exchanges one frame using a polled wait.
 * @details This synchronous function exchanges one frame using a polled
 *          synchronization method. This function is useful when exchanging
 *          small amount of data on high speed channels, usually in this
 *          situation is much more efficient just wait for completion using
 *          polling than suspending the thread waiting for an interrupt.
 *
 * @param[in] spip      pointer to the @p SPIDriver object
 * @param[in] frame     the data frame to send over the SPI bus
 * @return              The received data frame from the SPI bus.
 */
uint16_t spi_lld_polled_exchange(SPIDriver *spip, uint16_t frame) {

  (void)spip;
  (void)frame;

  return 0;
}

#endif /* HAL_USE_SPI */

/** @} */