aboutsummaryrefslogtreecommitdiffstats
path: root/os/hal/ports/MSP430X/hal_spi_lld.h
blob: 949a8a091446228d553f51b14aa4cd0ce24901a9 (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
/*
    ChibiOS - Copyright (C) 2016 Andrew Wygle aka awygle

    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    hal_spi_lld.h
 * @brief   MSP430X SPI subsystem low level driver header.
 *
 * @addtogroup SPI
 * @{
 */

#ifndef HAL_SPI_LLD_H
#define HAL_SPI_LLD_H

#if (HAL_USE_SPI == TRUE) || defined(__DOXYGEN__)

#include "hal_dma_lld.h"
/*===========================================================================*/
/* Driver constants.                                                         */
/*===========================================================================*/

/*===========================================================================*/
/* Driver pre-compile time settings.                                         */
/*===========================================================================*/

/**
 * @name    MSP430X configuration options
 * @{
 */
/**
 * @brief   SPIA0 driver enable switch.
 * @details If set to @p TRUE the support for SPIA0 is included.
 * @note    The default is @p FALSE.
 */
#if !defined(MSP430X_SPI_USE_SPIA0) || defined(__DOXYGEN__)
#define MSP430X_SPI_USE_SPIA0                  FALSE
#endif

/**
 * @brief   SPIA1 driver enable switch.
 * @details If set to @p TRUE the support for SPIA1 is included.
 * @note    The default is @p FALSE.
 */
#if !defined(MSP430X_SPI_USE_SPIA1) || defined(__DOXYGEN__)
#define MSP430X_SPI_USE_SPIA1                  FALSE
#endif

/**
 * @brief   SPIA2 driver enable switch.
 * @details If set to @p TRUE the support for SPIA2 is included.
 * @note    The default is @p FALSE.
 */
#if !defined(MSP430X_SPI_USE_SPIA2) || defined(__DOXYGEN__)
#define MSP430X_SPI_USE_SPIA2                  FALSE
#endif

/**
 * @brief   SPIA3 driver enable switch.
 * @details If set to @p TRUE the support for SPIA3 is included.
 * @note    The default is @p FALSE.
 */
#if !defined(MSP430X_SPI_USE_SPIA3) || defined(__DOXYGEN__)
#define MSP430X_SPI_USE_SPIA3                  FALSE
#endif

/**
 * @brief   SPIB0 driver enable switch.
 * @details If set to @p TRUE the support for SPIB0 is included.
 * @note    The default is @p FALSE.
 */
#if !defined(MSP430X_SPI_USE_SPIB0) || defined(__DOXYGEN__)
#define MSP430X_SPI_USE_SPIB0                  FALSE
#endif

/**
 * @brief   SPIB1 driver enable switch.
 * @details If set to @p TRUE the support for SPIB1 is included.
 * @note    The default is @p FALSE.
 */
#if !defined(MSP430X_SPI_USE_SPIB1) || defined(__DOXYGEN__)
#define MSP430X_SPI_USE_SPIB1                  FALSE
#endif

/**
 * @brief   SPIB2 driver enable switch.
 * @details If set to @p TRUE the support for SPIB2 is included.
 * @note    The default is @p FALSE.
 */
#if !defined(MSP430X_SPI_USE_SPIB2) || defined(__DOXYGEN__)
#define MSP430X_SPI_USE_SPIB2                  FALSE
#endif

/**
 * @brief   SPIB3 driver enable switch.
 * @details If set to @p TRUE the support for SPIB3 is included.
 * @note    The default is @p FALSE.
 */
#if !defined(MSP430X_SPI_USE_SPIB3) || defined(__DOXYGEN__)
#define MSP430X_SPI_USE_SPIB3                  FALSE
#endif

/**
 * @brief   Exclusive DMA enable switch.
 * @details If set to @p TRUE the support for exclusive DMA is included.
 * @note    This increases the size of the compiled executable somewhat.
 * @note    The default is @p FALSE.
 */
#if !defined(MSP430X_SPI_EXCLUSIVE_DMA) || defined(__DOXYGEN__)
#define MSP430X_SPI_EXCLUSIVE_DMA              FALSE
#endif

/**
 * @brief   SPIA0 clock source switch.
 * @details Sets the clock source for SPIA0.
 * @note    Legal values are @p MSP430X_SMCLK_SRC or @p MSP430X_ACLK_SRC.
 * @note    The default is @p MSP430X_SMCLK_SRC.
 */
#if !defined(MSP430X_SPIA0_CLK_SRC)
  #define MSP430X_SPIA0_CLK_SRC MSP430X_SMCLK_SRC
#endif

/**
 * @brief   SPIA1 clock source switch.
 * @details Sets the clock source for SPIA1.
 * @note    Legal values are @p MSP430X_SMCLK_SRC or @p MSP430X_ACLK_SRC.
 * @note    The default is @p MSP430X_SMCLK_SRC.
 */
#if !defined(MSP430X_SPIA1_CLK_SRC)
  #define MSP430X_SPIA1_CLK_SRC MSP430X_SMCLK_SRC
#endif

/**
 * @brief   SPIA2 clock source switch.
 * @details Sets the clock source for SPIA2.
 * @note    Legal values are @p MSP430X_SMCLK_SRC or @p MSP430X_ACLK_SRC.
 * @note    The default is @p MSP430X_SMCLK_SRC.
 */
#if !defined(MSP430X_SPIA2_CLK_SRC)
  #define MSP430X_SPIA2_CLK_SRC MSP430X_SMCLK_SRC
#endif

/**
 * @brief   SPIA3 clock source switch.
 * @details Sets the clock source for SPIA3.
 * @note    Legal values are @p MSP430X_SMCLK_SRC or @p MSP430X_ACLK_SRC.
 * @note    The default is @p MSP430X_SMCLK_SRC.
 */
#if !defined(MSP430X_SPIA3_CLK_SRC)
  #define MSP430X_SPIA3_CLK_SRC MSP430X_SMCLK_SRC
#endif

/**
 * @brief   SPIB0 clock source switch.
 * @details Sets the clock source for SPIB0.
 * @note    Legal values are @p MSP430X_SMCLK_SRC or @p MSP430X_ACLK_SRC.
 * @note    The default is @p MSP430X_SMCLK_SRC.
 */
#if !defined(MSP430X_SPIB0_CLK_SRC)
  #define MSP430X_SPIB0_CLK_SRC MSP430X_SMCLK_SRC
#endif

/**
 * @brief   SPIB1 clock source switch.
 * @details Sets the clock source for SPIB1.
 * @note    Legal values are @p MSP430X_SMCLK_SRC or @p MSP430X_ACLK_SRC.
 * @note    The default is @p MSP430X_SMCLK_SRC.
 */
#if !defined(MSP430X_SPIB1_CLK_SRC)
  #define MSP430X_SPIB1_CLK_SRC MSP430X_SMCLK_SRC
#endif

/**
 * @brief   SPIB2 clock source switch.
 * @details Sets the clock source for SPIB2.
 * @note    Legal values are @p MSP430X_SMCLK_SRC or @p MSP430X_ACLK_SRC.
 * @note    The default is @p MSP430X_SMCLK_SRC.
 */
#if !defined(MSP430X_SPIB2_CLK_SRC)
  #define MSP430X_SPIB2_CLK_SRC MSP430X_SMCLK_SRC
#endif

/**
 * @brief   SPIB3 clock source switch.
 * @details Sets the clock source for SPIB3.
 * @note    Legal values are @p MSP430X_SMCLK_SRC or @p MSP430X_ACLK_SRC.
 * @note    The default is @p MSP430X_SMCLK_SRC.
 */
#if !defined(MSP430X_SPIB3_CLK_SRC)
  #define MSP430X_SPIB3_CLK_SRC MSP430X_SMCLK_SRC
#endif
/** @} */

/*===========================================================================*/
/* Derived constants and error checks.                                       */
/*===========================================================================*/

#if MSP430X_SPI_USE_SPIA0 && !defined(__MSP430_HAS_EUSCI_A0__)
  #error "Cannot find MSP430X_USCI module to use for SPIA0"
#endif

#if MSP430X_SPI_USE_SPIA1 && !defined(__MSP430_HAS_EUSCI_A1__)
  #error "Cannot find MSP430X_USCI module to use for SPIA1"
#endif

#if MSP430X_SPI_USE_SPIA2 && !defined(__MSP430_HAS_EUSCI_A2__)
  #error "Cannot find MSP430X_USCI module to use for SPIA2"
#endif

#if MSP430X_SPI_USE_SPIA3 && !defined(__MSP430_HAS_EUSCI_A3__)
  #error "Cannot find MSP430X_USCI module to use for SPIA3"
#endif

#if MSP430X_SPI_USE_SPIB0 && !defined(__MSP430_HAS_EUSCI_B0__)
  #error "Cannot find MSP430X_USCI module to use for SPIB0"
#endif

#if MSP430X_SPI_USE_SPIB1 && !defined(__MSP430_HAS_EUSCI_B1__)
  #error "Cannot find MSP430X_USCI module to use for SPIB1"
#endif

#if MSP430X_SPI_USE_SPIB2 && !defined(__MSP430_HAS_EUSCI_B2__)
  #error "Cannot find MSP430X_USCI module to use for SPIB2"
#endif

#if MSP430X_SPI_USE_SPIB3 && !defined(__MSP430_HAS_EUSCI_B3__)
  #error "Cannot find MSP430X_USCI module to use for SPIB3"
#endif

#if MSP430X_SPI_USE_SPIA0
  #ifdef MSP430X_USCI_A0_USED
    #error "USCI module A0 already in use - SPIA0 not available"
  #else
    #define MSP430X_USCI_A0_USED
  #endif
#endif

#if MSP430X_SPI_USE_SPIA1
  #ifdef MSP430X_USCI_A1_USED
    #error "USCI module A1 already in use - SPIA1 not available"
  #else
    #define MSP430X_USCI_A1_USED
  #endif
#endif

#if MSP430X_SPI_USE_SPIA2
  #ifdef MSP430X_USCI_A2_USED
    #error "USCI module A2 already in use - SPIA2 not available"
  #else
    #define MSP430X_USCI_A2_USED
  #endif
#endif

#if MSP430X_SPI_USE_SPIA3
  #ifdef MSP430X_USCI_A3_USED
    #error "USCI module A3 already in use - SPIA3 not available"
  #else
    #define MSP430X_USCI_A3_USED
  #endif
#endif

#if MSP430X_SPI_USE_SPIB0
  #ifdef MSP430X_USCI_B0_USED
    #error "USCI module B0 already in use - SPIB0 not available"
  #else
    #define MSP430X_USCI_B0_USED
  #endif
#endif

#if MSP430X_SPI_USE_SPIB1
  #ifdef MSP430X_USCI_B1_USED
    #error "USCI module B1 already in use - SPIB1 not available"
  #else
    #define MSP430X_USCI_B1_USED
  #endif
#endif

#if MSP430X_SPI_USE_SPIB2
  #ifdef MSP430X_USCI_B2_USED
    #error "USCI module B2 already in use - SPIB2 not available"
  #else
    #define MSP430X_USCI_B2_USED
  #endif
#endif

#if MSP430X_SPI_USE_SPIB3
  #ifdef MSP430X_USCI_B3_USED
    #error "USCI module B3 already in use - SPIB3 not available"
  #else
    #define MSP430X_USCI_B3_USED
  #endif
#endif

#if defined(MSP430X_SPIA0_TX_DMA) && (MSP430X_SPI_DMA >= MSP430X_DMA_CHANNELS)
  #error "Requested DMA for SPIA0 TX, but requested index is invalid"
#endif
#if defined(MSP430X_SPIA0_RX_DMA) && (MSP430X_SPI_DMA >= MSP430X_DMA_CHANNELS)
  #error "Requested DMA for SPIA0 RX, but requested index is invalid"
#endif

#if defined(MSP430X_SPIA1_TX_DMA) && (MSP430X_SPI_DMA >= MSP430X_DMA_CHANNELS)
  #error "Requested DMA for SPIA1 TX, but requested index is invalid"
#endif
#if defined(MSP430X_SPIA1_RX_DMA) && (MSP430X_SPI_DMA >= MSP430X_DMA_CHANNELS)
  #error "Requested DMA for SPIA1 RX, but requested index is invalid"
#endif

#if defined(MSP430X_SPIA2_TX_DMA) && (MSP430X_SPI_DMA >= MSP430X_DMA_CHANNELS)
  #error "Requested DMA for SPIA2 TX, but requested index is invalid"
#endif
#if defined(MSP430X_SPIA2_RX_DMA) && (MSP430X_SPI_DMA >= MSP430X_DMA_CHANNELS)
  #error "Requested DMA for SPIA2 RX, but requested index is invalid"
#endif

#if defined(MSP430X_SPIA3_TX_DMA) && (MSP430X_SPI_DMA >= MSP430X_DMA_CHANNELS)
  #error "Requested DMA for SPIA3 TX, but requested index is invalid"
#endif
#if defined(MSP430X_SPIA3_RX_DMA) && (MSP430X_SPI_DMA >= MSP430X_DMA_CHANNELS)
  #error "Requested DMA for SPIA3 RX, but requested index is invalid"
#endif

#if defined(MSP430X_SPIB0_TX_DMA) && (MSP430X_SPI_DMA >= MSP430X_DMA_CHANNELS)
  #error "Requested DMA for SPIB0 TX, but requested index is invalid"
#endif
#if defined(MSP430X_SPIB0_RX_DMA) && (MSP430X_SPI_DMA >= MSP430X_DMA_CHANNELS)
  #error "Requested DMA for SPIB0 RX, but requested index is invalid"
#endif

#if defined(MSP430X_SPIB1_TX_DMA) && (MSP430X_SPI_DMA >= MSP430X_DMA_CHANNELS)
  #error "Requested DMA for SPIB1 TX, but requested index is invalid"
#endif
#if defined(MSP430X_SPIB1_RX_DMA) && (MSP430X_SPI_DMA >= MSP430X_DMA_CHANNELS)
  #error "Requested DMA for SPIB1 RX, but requested index is invalid"
#endif

#if defined(MSP430X_SPIB2_TX_DMA) && (MSP430X_SPI_DMA >= MSP430X_DMA_CHANNELS)
  #error "Requested DMA for SPIB2 TX, but requested index is invalid"
#endif
#if defined(MSP430X_SPIB2_RX_DMA) && (MSP430X_SPI_DMA >= MSP430X_DMA_CHANNELS)
  #error "Requested DMA for SPIB2 RX, but requested index is invalid"
#endif

#if defined(MSP430X_SPIB3_TX_DMA) && (MSP430X_SPI_DMA >= MSP430X_DMA_CHANNELS)
  #error "Requested DMA for SPIB3 TX, but requested index is invalid"
#endif
#if defined(MSP430X_SPIB3_RX_DMA) && (MSP430X_SPI_DMA >= MSP430X_DMA_CHANNELS)
  #error "Requested DMA for SPIB3 RX, but requested index is invalid"
#endif

/* TODO figure out a way to check for conflicting DMA channels */

#if MSP430X_SPIA0_CLK_SRC == MSP430X_ACLK_SRC
  #define MSP430X_SPIA0_CLK_FREQ MSP430X_ACLK_FREQ
  #define MSP430X_SPIA0_UCSSEL UCSSEL__ACLK
#elif MSP430X_SPIA0_CLK_SRC == MSP430X_SMCLK_SRC
  #define MSP430X_SPIA0_CLK_FREQ MSP430X_SMCLK_FREQ
  #define MSP430X_SPIA0_UCSSEL UCSSEL__SMCLK
#endif

#if MSP430X_SPIA1_CLK_SRC == MSP430X_ACLK_SRC
  #define MSP430X_SPIA1_CLK_FREQ MSP430X_ACLK_FREQ
  #define MSP430X_SPIA1_UCSSEL UCSSEL__ACLK
#elif MSP430X_SPIA1_CLK_SRC == MSP430X_SMCLK_SRC
  #define MSP430X_SPIA1_CLK_FREQ MSP430X_SMCLK_FREQ
  #define MSP430X_SPIA1_UCSSEL UCSSEL__SMCLK
#endif

#if MSP430X_SPIA2_CLK_SRC == MSP430X_ACLK_SRC
  #define MSP430X_SPIA2_CLK_FREQ MSP430X_ACLK_FREQ
  #define MSP430X_SPIA2_UCSSEL UCSSEL__ACLK
#elif MSP430X_SPIA2_CLK_SRC == MSP430X_SMCLK_SRC
  #define MSP430X_SPIA2_CLK_FREQ MSP430X_SMCLK_FREQ
  #define MSP430X_SPIA2_UCSSEL UCSSEL__SMCLK
#endif

#if MSP430X_SPIA3_CLK_SRC == MSP430X_ACLK_SRC
  #define MSP430X_SPIA3_CLK_FREQ MSP430X_ACLK_FREQ
  #define MSP430X_SPIA3_UCSSEL UCSSEL__ACLK
#elif MSP430X_SPIA3_CLK_SRC == MSP430X_SMCLK_SRC
  #define MSP430X_SPIA3_CLK_FREQ MSP430X_SMCLK_FREQ
  #define MSP430X_SPIA3_UCSSEL UCSSEL__SMCLK
#endif

#if MSP430X_SPIB0_CLK_SRC == MSP430X_ACLK_SRC
  #define MSP430X_SPIB0_CLK_FREQ MSP430X_ACLK_FREQ
  #define MSP430X_SPIB0_UCSSEL UCSSEL__ACLK
#elif MSP430X_SPIB0_CLK_SRC == MSP430X_SMCLK_SRC
  #define MSP430X_SPIB0_CLK_FREQ MSP430X_SMCLK_FREQ
  #define MSP430X_SPIB0_UCSSEL UCSSEL__SMCLK
#endif

#if MSP430X_SPIB1_CLK_SRC == MSP430X_ACLK_SRC
  #define MSP430X_SPIB1_CLK_FREQ MSP430X_ACLK_FREQ
  #define MSP430X_SPIB1_UCSSEL UCSSEL__ACLK
#elif MSP430X_SPIB1_CLK_SRC == MSP430X_SMCLK_SRC
  #define MSP430X_SPIB1_CLK_FREQ MSP430X_SMCLK_FREQ
  #define MSP430X_SPIB1_UCSSEL UCSSEL__SMCLK
#endif

#if MSP430X_SPIB2_CLK_SRC == MSP430X_ACLK_SRC
  #define MSP430X_SPIB2_CLK_FREQ MSP430X_ACLK_FREQ
  #define MSP430X_SPIB2_UCSSEL UCSSEL__ACLK
#elif MSP430X_SPIB2_CLK_SRC == MSP430X_SMCLK_SRC
  #define MSP430X_SPIB2_CLK_FREQ MSP430X_SMCLK_FREQ
  #define MSP430X_SPIB2_UCSSEL UCSSEL__SMCLK
#endif

#if MSP430X_SPIB3_CLK_SRC == MSP430X_ACLK_SRC
  #define MSP430X_SPIB3_CLK_FREQ MSP430X_ACLK_FREQ
  #define MSP430X_SPIB3_UCSSEL UCSSEL__ACLK
#elif MSP430X_SPIB3_CLK_SRC == MSP430X_SMCLK_SRC
  #define MSP430X_SPIB3_CLK_FREQ MSP430X_SMCLK_FREQ
  #define MSP430X_SPIB3_UCSSEL UCSSEL__SMCLK
#endif

/*===========================================================================*/
/* Driver data structures and types.                                         */
/*===========================================================================*/

/**
 * @brief   Type of a structure representing an SPI driver.
 */
typedef struct SPIDriver SPIDriver;

/**
 * @brief   SPI notification callback type.
 *
 * @param[in] spip      pointer to the @p SPIDriver object triggering the
 *                      callback
 */
typedef void (*spicallback_t)(SPIDriver *spip);

/**
 * @brief   Enumerated type for SPI bit order.
 */
typedef enum {
  MSP430X_SPI_BO_LSB = 0,
  MSP430X_SPI_BO_MSB = 1
} msp430x_spi_bit_order_t;

/**
 * @brief   Enumerated type for SPI data size.
 */
typedef enum {
  MSP430X_SPI_DS_EIGHT = 0,
  MSP430X_SPI_DS_SEVEN = 1
} msp430x_spi_data_size_t;

/**
 * @brief   Driver configuration structure.
 * @note    Implementations may extend this structure to contain more,
 *          architecture dependent, fields.
 */
typedef struct {
  /**
   * @brief Operation complete callback or @p NULL.
   */
  spicallback_t             end_cb;
  /* End of the mandatory fields.*/
  /**
   * @brief The chip select line.
   * @note  This may be PAL_NOLINE to indicate that hardware chip select is used.
   */
  ioline_t                  ss_line;
  /**
   * @brief The bit rate of the SPI interface.
   * @note  Nearest available rate is used.
   */
  uint32_t                  bit_rate;
  /**
   * @brief The bit order of the peripheral - LSB or MSB first.
   */
  msp430x_spi_bit_order_t bit_order;
  /**
   * @brief The data size of the peripheral - 7 or 8 bits.
   */
  msp430x_spi_data_size_t data_size;
  /**
   * @brief The SPI mode to use - 0 through 3.
   */
  uint8_t spi_mode;
#if MSP430X_SPI_EXCLUSIVE_DMA == TRUE || defined(__DOXYGEN__)
  /**
   * @brief The index of the TX DMA channel.
   * @note  This may be >MSP430X_DMA_CHANNELS to indicate that exclusive DMA is not used.
   */
  uint8_t dmatx_index;
  /**
   * @brief The index of the RX DMA channel.
   * @note  This may be >MSP430X_DMA_CHANNELS to indicate that exclusive DMA is not used.
   */
  uint8_t dmarx_index;
#endif
} SPIConfig;

/**
 * @brief   MSP430X SPI register structure.
 */
typedef struct {
  uint16_t ctlw0;
  uint16_t _padding0;
  uint16_t _padding1;
  uint16_t brw;
  uint16_t statw_b;
  uint16_t statw_a;
  uint16_t rxbuf;
  uint16_t txbuf;
} msp430x_spi_reg_t;

/**
 * @brief   Structure representing an SPI driver.
 * @note    Implementations may extend this structure to contain more,
 *          architecture dependent, fields.
 */
struct SPIDriver {
  /**
   * @brief Driver state.
   */
  spistate_t                state;
  /**
   * @brief Current configuration data.
   */
  const SPIConfig           *config;
#if (SPI_USE_WAIT == TRUE) || defined(__DOXYGEN__)
  /**
   * @brief   Waiting thread.
   */
  thread_reference_t        thread;
#endif /* SPI_USE_WAIT */
#if (SPI_USE_MUTUAL_EXCLUSION == TRUE) || defined(__DOXYGEN__)
  /**
   * @brief   Mutex protecting the peripheral.
   */
  mutex_t                   mutex;
#endif
#if defined(SPI_DRIVER_EXT_FIELDS)
  SPI_DRIVER_EXT_FIELDS
#endif
  /* End of the mandatory fields.*/
  /**
   * @brief   Configuration registers.
   */
  msp430x_spi_reg_t * regs;
  /**
   * @brief   Interrupt flag register.
   */
  volatile uint16_t * ifg;
  /**
   * @brief   TX DMA request.
   */
  msp430x_dma_req_t tx_req;
  /**
   * @brief   RX DMA request.
   */
  msp430x_dma_req_t rx_req;
#if MSP430X_SPI_EXCLUSIVE_DMA == TRUE || defined(__DOXYGEN__)
  /**
   * @brief   TX DMA stream.
   */
  msp430x_dma_ch_t dmatx;
  /**
   * @brief   RX DMA stream.
   */
  msp430x_dma_ch_t dmarx;
#endif
};

/*===========================================================================*/
/* Driver macros.                                                            */
/*===========================================================================*/

/*===========================================================================*/
/* External declarations.                                                    */
/*===========================================================================*/

#if (MSP430X_SPI_USE_SPIA0 == TRUE) && !defined(__DOXYGEN__)
extern SPIDriver SPIDA0;
#endif

#if (MSP430X_SPI_USE_SPIA1 == TRUE) && !defined(__DOXYGEN__)
extern SPIDriver SPIDA1;
#endif

#if (MSP430X_SPI_USE_SPIA2 == TRUE) && !defined(__DOXYGEN__)
extern SPIDriver SPIDA2;
#endif

#if (MSP430X_SPI_USE_SPIA3 == TRUE) && !defined(__DOXYGEN__)
extern SPIDriver SPIDA3;
#endif

#if (MSP430X_SPI_USE_SPIB0 == TRUE) && !defined(__DOXYGEN__)
extern SPIDriver SPIDB0;
#endif

#if (MSP430X_SPI_USE_SPIB1 == TRUE) && !defined(__DOXYGEN__)
extern SPIDriver SPIDB1;
#endif

#if (MSP430X_SPI_USE_SPIB2 == TRUE) && !defined(__DOXYGEN__)
extern SPIDriver SPIDB2;
#endif

#if (MSP430X_SPI_USE_SPIB3 == TRUE) && !defined(__DOXYGEN__)
extern SPIDriver SPIDB3;
#endif

#ifdef __cplusplus
extern "C" {
#endif
  void spi_lld_init(void);
  void spi_lld_start(SPIDriver *spip);
  void spi_lld_stop(SPIDriver *spip);
  void spi_lld_select(SPIDriver *spip);
  void spi_lld_unselect(SPIDriver *spip);
  void spi_lld_ignore(SPIDriver *spip, size_t n);
  void spi_lld_exchange(SPIDriver *spip, size_t n,
                        const void *txbuf, void *rxbuf);
  void spi_lld_send(SPIDriver *spip, size_t n, const void *txbuf);
  void spi_lld_receive(SPIDriver *spip, size_t n, void *rxbuf);
  uint8_t spi_lld_polled_exchange(SPIDriver *spip, uint8_t frame);
#ifdef __cplusplus
}
#endif

#endif /* HAL_USE_SPI == TRUE */

#endif /* HAL_SPI_LLD_H */

/** @} */