aboutsummaryrefslogtreecommitdiffstats
path: root/os/hal/ports/MSP430X/hal_spi_lld.c
blob: 70a357e339a4112adf70273d84f1ddb6cfcd70fc (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
/*
    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.c
 * @brief   MSP430X SPI subsystem low level driver source.
 *
 * @addtogroup SPI
 * @{
 */

#include "hal.h"

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

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

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

/**
 * @brief   SPIA0 driver identifier.
 */
#if (MSP430X_SPI_USE_SPIA0 == TRUE) || defined(__DOXYGEN__)
SPIDriver SPIDA0;
#endif

/**
 * @brief   SPIA1 driver identifier.
 */
#if (MSP430X_SPI_USE_SPIA1 == TRUE) || defined(__DOXYGEN__)
SPIDriver SPIDA1;
#endif

/**
 * @brief   SPIA2 driver identifier.
 */
#if (MSP430X_SPI_USE_SPIA2 == TRUE) || defined(__DOXYGEN__)
SPIDriver SPIDA2;
#endif

/**
 * @brief   SPIA3 driver identifier.
 */
#if (MSP430X_SPI_USE_SPIA3 == TRUE) || defined(__DOXYGEN__)
SPIDriver SPIDA3;
#endif

/**
 * @brief   SPIB0 driver identifier.
 */
#if (MSP430X_SPI_USE_SPIB0 == TRUE) || defined(__DOXYGEN__)
SPIDriver SPIDB0;
#endif

/**
 * @brief   SPIB1 driver identifier.
 */
#if (MSP430X_SPI_USE_SPIB1 == TRUE) || defined(__DOXYGEN__)
SPIDriver SPIDB1;
#endif

/**
 * @brief   SPIB2 driver identifier.
 */
#if (MSP430X_SPI_USE_SPIB2 == TRUE) || defined(__DOXYGEN__)
SPIDriver SPIDB2;
#endif

/**
 * @brief   SPIB3 driver identifier.
 */
#if (MSP430X_SPI_USE_SPIB3 == TRUE) || defined(__DOXYGEN__)
SPIDriver SPIDB3;
#endif

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

static const uint16_t dummytx = 0xFFFFU;
static uint16_t dummyrx;

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

static void init_transfer(SPIDriver * spip) {

#if MSP430X_SPI_EXCLUSIVE_DMA == TRUE || defined(__DOXYGEN__)
  if (spip->config->dmarx_index > MSP430X_DMA_CHANNELS) {
    dmaRequest(&(spip->rx_req), TIME_INFINITE);
  }
  else {
    dmaTransfer(&(spip->dmarx), &(spip->rx_req));
  }
  if (spip->config->dmatx_index > MSP430X_DMA_CHANNELS) {
    dmaRequest(&(spip->tx_req), TIME_INFINITE);
  }
  else {
    dmaTransfer(&(spip->dmatx), &(spip->tx_req));
  }
#else
  dmaRequest(&(spip->rx_req), TIME_INFINITE);
  dmaRequest(&(spip->tx_req), TIME_INFINITE);
#endif

  *(spip->ifg) |= UCTXIFG;
}

/**
 * @brief   Shared end-of-transfer callback.
 *
 * @param[in] spip    pointer to the @p SPIDriver object
 * @note    This function is called in ISR context by the DMA code.
 */
static void spi_lld_end_of_transfer(void * spip) {

  /* So that future transfers will actually work */
  *(((SPIDriver *)spip)->ifg) &= ~(UCTXIFG);
  /* NOTE to future me - this macro sets the driver state and calls the
   * configured callback end_cb, if applicable. That callback doesn't seem to
   * be modifiable without reconfiguring the whole driver. */
  _spi_isr_code((SPIDriver *)spip);
}
/*===========================================================================*/
/* Driver interrupt handlers.                                                */
/*===========================================================================*/

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

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

#if MSP430X_SPI_USE_SPIA0 == TRUE
  /* Driver initialization.*/
  spiObjectInit(&SPIDA0);
  SPIDA0.regs                     = (msp430x_spi_reg_t *)(&UCA0CTLW0);
  SPIDA0.ifg                      = (volatile uint16_t *)&UCA0IFG;
  SPIDA0.tx_req.trigger           = DMA_TRIGGER_MNEM(UCA0TXIFG);
  SPIDA0.rx_req.trigger           = DMA_TRIGGER_MNEM(UCA0RXIFG);
  SPIDA0.tx_req.dest_addr         = &(SPIDA0.regs->txbuf);
  SPIDA0.rx_req.source_addr       = &(SPIDA0.regs->rxbuf);
  SPIDA0.tx_req.data_mode         = MSP430X_DMA_SRCBYTE | MSP430X_DMA_DSTBYTE;
  SPIDA0.rx_req.data_mode         = MSP430X_DMA_SRCBYTE | MSP430X_DMA_DSTBYTE;
  SPIDA0.tx_req.transfer_mode     = MSP430X_DMA_SINGLE;
  SPIDA0.rx_req.transfer_mode     = MSP430X_DMA_SINGLE;
  SPIDA0.tx_req.callback.callback = NULL;
  SPIDA0.tx_req.callback.args     = NULL;
  SPIDA0.rx_req.callback.callback = spi_lld_end_of_transfer;
  SPIDA0.rx_req.callback.args     = &SPIDA0;
/* NOTE to my future self - this must be SINGLE because BLOCK and BURST
 * don't wait for triggers and would overflow both buffers. Don't worry, it
 * still works - the transfer isn't complete until SZ bytes are transferred */
#endif

#if MSP430X_SPI_USE_SPIA1 == TRUE
  /* Driver initialization.*/
  spiObjectInit(&SPIDA1);
  SPIDA1.regs                     = (msp430x_spi_reg_t *)(&UCA1CTLW0);
  SPIDA1.ifg                      = (volatile uint16_t *)&UCA1IFG;
  SPIDA1.tx_req.trigger           = DMA_TRIGGER_MNEM(UCA1TXIFG);
  SPIDA1.rx_req.trigger           = DMA_TRIGGER_MNEM(UCA1RXIFG);
  SPIDA1.tx_req.dest_addr         = &(SPIDA1.regs->txbuf);
  SPIDA1.rx_req.source_addr       = &(SPIDA1.regs->rxbuf);
  SPIDA1.tx_req.data_mode         = MSP430X_DMA_SRCBYTE | MSP430X_DMA_DSTBYTE;
  SPIDA1.rx_req.data_mode         = MSP430X_DMA_SRCBYTE | MSP430X_DMA_DSTBYTE;
  SPIDA1.tx_req.transfer_mode     = MSP430X_DMA_SINGLE;
  SPIDA1.rx_req.transfer_mode     = MSP430X_DMA_SINGLE;
  SPIDA1.tx_req.callback.callback = NULL;
  SPIDA1.tx_req.callback.args     = NULL;
  SPIDA1.rx_req.callback.callback = spi_lld_end_of_transfer;
  SPIDA1.rx_req.callback.args     = &SPIDA1;
#endif

#if MSP430X_SPI_USE_SPIA2 == TRUE
  /* Driver initialization.*/
  spiObjectInit(&SPIDA2);
  SPIDA2.regs                     = (msp430x_spi_reg_t *)(&UCA2CTLW0);
  SPIDA2.ifg                      = (volatile uint16_t *)&UCA2IFG;
  SPIDA2.tx_req.trigger           = DMA_TRIGGER_MNEM(UCA2TXIFG);
  SPIDA2.rx_req.trigger           = DMA_TRIGGER_MNEM(UCA2RXIFG);
  SPIDA2.tx_req.dest_addr         = &(SPIDA2.regs->txbuf);
  SPIDA2.rx_req.source_addr       = &(SPIDA2.regs->rxbuf);
  SPIDA2.tx_req.data_mode         = MSP430X_DMA_SRCBYTE | MSP430X_DMA_DSTBYTE;
  SPIDA2.rx_req.data_mode         = MSP430X_DMA_SRCBYTE | MSP430X_DMA_DSTBYTE;
  SPIDA2.tx_req.transfer_mode     = MSP430X_DMA_SINGLE;
  SPIDA2.rx_req.transfer_mode     = MSP430X_DMA_SINGLE;
  SPIDA2.tx_req.callback.callback = NULL;
  SPIDA2.tx_req.callback.args     = NULL;
  SPIDA2.rx_req.callback.callback = spi_lld_end_of_transfer;
  SPIDA2.rx_req.callback.args     = &SPIDA2;
#endif

#if MSP430X_SPI_USE_SPIA3 == TRUE
  /* Driver initialization.*/
  spiObjectInit(&SPIDA3);
  SPIDA3.regs                     = (msp430x_spi_reg_t *)(&UCA3CTLW0);
  SPIDA3.ifg                      = (volatile uint16_t *)&UCA3IFG;
  SPIDA3.tx_req.trigger           = DMA_TRIGGER_MNEM(UCA3TXIFG);
  SPIDA3.rx_req.trigger           = DMA_TRIGGER_MNEM(UCA3RXIFG);
  SPIDA3.tx_req.dest_addr         = &(SPIDA3.regs->txbuf);
  SPIDA3.rx_req.source_addr       = &(SPIDA3.regs->rxbuf);
  SPIDA3.tx_req.data_mode         = MSP430X_DMA_SRCBYTE | MSP430X_DMA_DSTBYTE;
  SPIDA3.rx_req.data_mode         = MSP430X_DMA_SRCBYTE | MSP430X_DMA_DSTBYTE;
  SPIDA3.tx_req.transfer_mode     = MSP430X_DMA_SINGLE;
  SPIDA3.rx_req.transfer_mode     = MSP430X_DMA_SINGLE;
  SPIDA3.tx_req.callback.callback = NULL;
  SPIDA3.tx_req.callback.args     = NULL;
  SPIDA3.rx_req.callback.callback = spi_lld_end_of_transfer;
  SPIDA3.rx_req.callback.args     = &SPIDA3;
#endif

#if MSP430X_SPI_USE_SPIB0 == TRUE
  /* Driver initialization.*/
  spiObjectInit(&SPIDB0);
  SPIDB0.regs                     = (msp430x_spi_reg_t *)(&UCB0CTLW0);
  SPIDB0.ifg                      = (volatile uint16_t *)&UCB0IFG;
  SPIDB0.tx_req.trigger           = DMA_TRIGGER_MNEM(UCB0TXIFG0);
  SPIDB0.rx_req.trigger           = DMA_TRIGGER_MNEM(UCB0RXIFG0);
  SPIDB0.tx_req.dest_addr         = &(SPIDB0.regs->txbuf);
  SPIDB0.rx_req.source_addr       = &(SPIDB0.regs->rxbuf);
  SPIDB0.tx_req.data_mode         = MSP430X_DMA_SRCBYTE | MSP430X_DMA_DSTBYTE;
  SPIDB0.rx_req.data_mode         = MSP430X_DMA_SRCBYTE | MSP430X_DMA_DSTBYTE;
  SPIDB0.tx_req.transfer_mode     = MSP430X_DMA_SINGLE;
  SPIDB0.rx_req.transfer_mode     = MSP430X_DMA_SINGLE;
  SPIDB0.tx_req.callback.callback = NULL;
  SPIDB0.tx_req.callback.args     = NULL;
  SPIDB0.rx_req.callback.callback = spi_lld_end_of_transfer;
  SPIDB0.rx_req.callback.args     = &SPIDB0;
#endif

#if MSP430X_SPI_USE_SPIB1 == TRUE
  /* Driver initialization.*/
  spiObjectInit(&SPIDB1);
  SPIDB1.regs                     = (msp430x_spi_reg_t *)(&UCB1CTLW0);
  SPIDB1.ifg                      = (volatile uint16_t *)&UCB1IFG;
  SPIDB1.tx_req.trigger           = DMA_TRIGGER_MNEM(UCB1TXIFG0);
  SPIDB1.rx_req.trigger           = DMA_TRIGGER_MNEM(UCB1RXIFG0);
  SPIDB1.tx_req.dest_addr         = &(SPIDB1.regs->txbuf);
  SPIDB1.rx_req.source_addr       = &(SPIDB1.regs->rxbuf);
  SPIDB1.tx_req.data_mode         = MSP430X_DMA_SRCBYTE | MSP430X_DMA_DSTBYTE;
  SPIDB1.rx_req.data_mode         = MSP430X_DMA_SRCBYTE | MSP430X_DMA_DSTBYTE;
  SPIDB1.tx_req.transfer_mode     = MSP430X_DMA_SINGLE;
  SPIDB1.rx_req.transfer_mode     = MSP430X_DMA_SINGLE;
  SPIDB1.tx_req.callback.callback = NULL;
  SPIDB1.tx_req.callback.args     = NULL;
  SPIDB1.rx_req.callback.callback = spi_lld_end_of_transfer;
  SPIDB1.rx_req.callback.args     = &SPIDB1;
#endif

#if MSP430X_SPI_USE_SPIB2 == TRUE
  /* Driver initialization.*/
  spiObjectInit(&SPIDB2);
  SPIDB2.regs                     = (msp430x_spi_reg_t *)(&UCB2CTLW0);
  SPIDB2.ifg                      = (volatile uint16_t *)&UCB2IFG;
  SPIDB2.tx_req.trigger           = DMA_TRIGGER_MNEM(UCB2TXIFG0);
  SPIDB2.rx_req.trigger           = DMA_TRIGGER_MNEM(UCB2RXIFG0);
  SPIDB2.tx_req.dest_addr         = &(SPIDB2.regs->txbuf);
  SPIDB2.rx_req.source_addr       = &(SPIDB2.regs->rxbuf);
  SPIDB2.tx_req.data_mode         = MSP430X_DMA_SRCBYTE | MSP430X_DMA_DSTBYTE;
  SPIDB2.rx_req.data_mode         = MSP430X_DMA_SRCBYTE | MSP430X_DMA_DSTBYTE;
  SPIDB2.tx_req.transfer_mode     = MSP430X_DMA_SINGLE;
  SPIDB2.rx_req.transfer_mode     = MSP430X_DMA_SINGLE;
  SPIDB2.tx_req.callback.callback = NULL;
  SPIDB2.tx_req.callback.args     = NULL;
  SPIDB2.rx_req.callback.callback = spi_lld_end_of_transfer;
  SPIDB2.rx_req.callback.args     = &SPIDB2;
#endif

#if MSP430X_SPI_USE_SPIB3 == TRUE
  /* Driver initialization.*/
  spiObjectInit(&SPIDB3);
  SPIDB3.regs                     = (msp430x_spi_reg_t *)(&UCB3CTLW0);
  SPIDB3.ifg                      = (volatile uint16_t *)&UCB3IFG;
  SPIDB3.tx_req.trigger           = DMA_TRIGGER_MNEM(UCB3TXIFG0);
  SPIDB3.rx_req.trigger           = DMA_TRIGGER_MNEM(UCB3RXIFG0);
  SPIDB3.tx_req.dest_addr         = &(SPIDB3.regs->txbuf);
  SPIDB3.rx_req.source_addr       = &(SPIDB3.regs->rxbuf);
  SPIDB3.tx_req.data_mode         = MSP430X_DMA_SRCBYTE | MSP430X_DMA_DSTBYTE;
  SPIDB3.rx_req.data_mode         = MSP430X_DMA_SRCBYTE | MSP430X_DMA_DSTBYTE;
  SPIDB3.tx_req.transfer_mode     = MSP430X_DMA_SINGLE;
  SPIDB3.rx_req.transfer_mode     = MSP430X_DMA_SINGLE;
  SPIDB3.tx_req.callback.callback = NULL;
  SPIDB3.tx_req.callback.args     = NULL;
  SPIDB3.rx_req.callback.callback = spi_lld_end_of_transfer;
  SPIDB3.rx_req.callback.args     = &SPIDB3;
#endif
}

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

  if (spip->state == SPI_STOP) {
/* Enables the peripheral.*/
#if MSP430X_SPI_EXCLUSIVE_DMA == TRUE
    /* Claim DMA streams here */
    bool b;
    if (spip->config->dmatx_index < MSP430X_DMA_CHANNELS) {
      b = dmaAcquire(&(spip->dmatx), spip->config->dmatx_index);
      osalDbgAssert(!b, "stream already allocated");
    }
    if (spip->config->dmarx_index < MSP430X_DMA_CHANNELS) {
      b = dmaAcquire(&(spip->dmarx), spip->config->dmarx_index);
      osalDbgAssert(!b, "stream already allocated");
    }
#endif /* MSP430X_SPI_EXCLUSIVE_DMA */
  }
  uint16_t brw = 0;
  uint8_t ssel = 0;
#if MSP430X_SPI_USE_SPIA0
  if (spip == &SPIDA0) {
    brw  = MSP430X_SPIA0_CLK_FREQ / spip->config->bit_rate;
    ssel = MSP430X_SPIA0_UCSSEL;
  }
#endif
#if MSP430X_SPI_USE_SPIA1
  if (spip == &SPIDA1) {
    brw  = MSP430X_SPIA1_CLK_FREQ / spip->config->bit_rate;
    ssel = MSP430X_SPIA1_UCSSEL;
  }
#endif
#if MSP430X_SPI_USE_SPIA2
  if (spip == &SPIDA2) {
    brw  = MSP430X_SPIA2_CLK_FREQ / spip->config->bit_rate;
    ssel = MSP430X_SPIA2_UCSSEL;
  }
#endif
#if MSP430X_SPI_USE_SPIA3
  if (spip == &SPIDA3) {
    brw  = MSP430X_SPIA3_CLK_FREQ / spip->config->bit_rate;
    ssel = MSP430X_SPIA3_UCSSEL;
  }
#endif
#if MSP430X_SPI_USE_SPIB0
  if (spip == &SPIDB0) {
    brw  = MSP430X_SPIB0_CLK_FREQ / spip->config->bit_rate;
    ssel = MSP430X_SPIB0_UCSSEL;
  }
#endif
#if MSP430X_SPI_USE_SPIB1
  if (spip == &SPIDB1) {
    brw  = MSP430X_SPIB1_CLK_FREQ / spip->config->bit_rate;
    ssel = MSP430X_SPIB1_UCSSEL;
  }
#endif
#if MSP430X_SPI_USE_SPIB2
  if (spip == &SPIDB2) {
    brw  = MSP430X_SPIB2_CLK_FREQ / spip->config->bit_rate;
    ssel = MSP430X_SPIB2_UCSSEL;
  }
#endif
#if MSP430X_SPI_USE_SPIB3
  if (spip == &SPIDB3) {
    brw  = MSP430X_SPIB3_CLK_FREQ / spip->config->bit_rate;
    ssel = MSP430X_SPIB3_UCSSEL;
  }
#endif
  /* Configures the peripheral.*/
  spip->regs->ctlw0 = UCSWRST;
  spip->regs->brw   = brw;
  spip->regs->ctlw0 =
      (spip->config->spi_mode << 14) | (spip->config->bit_order << 13) |
      (spip->config->data_size << 12) | (UCMST) |
      ((spip->config->ss_line ? 0 : 2) << 9) | (UCSYNC) | (ssel) | (UCSTEM);
  *(spip->ifg) = 0;
}

/**
 * @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) {
/* Disables the peripheral.*/
#if MSP430X_SPI_EXCLUSIVE_DMA == TRUE
    dmaRelease(&(spip->dmatx));
    dmaRelease(&(spip->dmarx));
#endif
    spip->regs->ctlw0 = UCSWRST;
  }
}

/**
 * @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) {

  if (spip->config->ss_line) {
    palClearLine(spip->config->ss_line);
  }
}

/**
 * @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) {

  if (spip->config->ss_line) {
    palSetLine(spip->config->ss_line);
  }
}

/**
 * @brief   Ignores data on the SPI bus.
 * @details This asynchronous function starts the transmission of a series of
 *          idle bytes 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 bytes to be ignored
 *
 * @notapi
 */
void spi_lld_ignore(SPIDriver * spip, size_t n) {

  spip->tx_req.source_addr = &dummytx;
  spip->tx_req.size        = n;
  spip->tx_req.addr_mode   = 0;

  spip->rx_req.dest_addr = &dummyrx;
  spip->rx_req.size      = n;
  spip->rx_req.addr_mode = 0;

  init_transfer(spip);
}

/**
 * @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) {

  spip->tx_req.source_addr = txbuf;
  spip->tx_req.size        = n;
  spip->tx_req.addr_mode   = MSP430X_DMA_SRCINCR;

  spip->rx_req.dest_addr = rxbuf;
  spip->rx_req.size      = n;
  spip->rx_req.addr_mode = MSP430X_DMA_DSTINCR;

  init_transfer(spip);
}

/**
 * @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) {

  spip->tx_req.source_addr = txbuf;
  spip->tx_req.size        = n;
  spip->tx_req.addr_mode   = MSP430X_DMA_SRCINCR;

  spip->rx_req.dest_addr = &dummyrx;
  spip->rx_req.size      = n;
  spip->rx_req.addr_mode = 0;

  init_transfer(spip);
}

/**
 * @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) {

  spip->tx_req.source_addr = &dummytx;
  spip->tx_req.size        = n;
  spip->tx_req.addr_mode   = 0;

  spip->rx_req.dest_addr = rxbuf;
  spip->rx_req.size      = n;
  spip->rx_req.addr_mode = MSP430X_DMA_DSTINCR;

  init_transfer(spip);
}

/**
 * @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) {

  osalDbgAssert(!(frame & 0xFF00U), "16-bit transfers not supported");

  while (!(*(spip->ifg) & UCTXIFG))
    ;
  spip->regs->txbuf = frame;
  while (!(*(spip->ifg) & UCRXIFG))
    ;
  return spip->regs->rxbuf;
}

#endif /* HAL_USE_SPI == TRUE */

/** @} */