summaryrefslogtreecommitdiffstats
path: root/bsps/arm/raspberrypi/spi/spi.c
blob: 36db3d8db5cc430569f3fb8f8ef63df76393d998 (plain) (blame)
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
/**
 * @file
 *
 * @ingroup raspberrypi_spi
 *
 * @brief Support for the SPI bus on the Raspberry Pi GPIO P1 header (model A/B)
 *        and GPIO J8 header on model B+.
 */

/*
 *  Copyright (c) 2014-2015 Andre Marques <andre.lousa.marques at gmail.com>
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.org/license/LICENSE.
 */

/*
 * STATUS:
 * - Bi-directional mode untested
 * - Write-only devices not supported
 */

#include <bsp.h>
#include <bsp/raspberrypi.h>
#include <bsp/gpio.h>
#include <bsp/rpi-gpio.h>
#include <bsp/irq.h>
#include <bsp/spi.h>
#include <assert.h>

#define SPI_POLLING(condition)                  \
  while ( condition ) {                         \
    ;                                           \
  }

/**
 * @brief Object containing the SPI bus configuration settings.
 *
 * Encapsulates the current SPI bus configuration.
 */
typedef struct
{
  int initialized;
  uint8_t bytes_per_char;

  /* Shift to be applied on data transfers with
   * least significative bit first (LSB) devices. */
  uint8_t bit_shift;
  uint32_t dummy_char;
  uint32_t current_slave_addr;
  rtems_id task_id;
  int irq_write;
} rpi_spi_softc_t;

/**
 * @brief Object containing the SPI bus description.
 *
 * Encapsulates the current SPI bus description.
 */
typedef struct
{
  rtems_libi2c_bus_t bus_desc;
  rpi_spi_softc_t softc;
} rpi_spi_desc_t;

/* If set to FALSE uses 3-wire SPI, with 2 separate data lines (MOSI and MISO),
 * if set to TRUE uses 2-wire SPI, where the MOSI data line doubles as the
 * slave out (SO) and slave in (SI) data lines. */
static bool bidirectional = false;

/* Calculates a clock divider to be used with the GPU core clock rate
 * to set a SPI clock rate the closest (<=) to a desired frequency. */
static rtems_status_code rpi_spi_calculate_clock_divider(
  uint32_t clock_hz,
  uint16_t *clock_divider
) {
  uint16_t divider;
  uint32_t clock_rate;

  assert( clock_hz > 0 );

  /* Calculates an initial clock divider. */
  divider = GPU_CORE_CLOCK_RATE / clock_hz;

  /* Because the divider must be a power of two (as per the BCM2835 datasheet),
   * calculate the next greater power of two. */
  --divider;

  divider |= (divider >> 1);
  divider |= (divider >> 2);
  divider |= (divider >> 4);
  divider |= (divider >> 8);

  ++divider;

  clock_rate = GPU_CORE_CLOCK_RATE / divider;

  /* If the resulting clock rate is greater than the desired frequency,
   * try the next greater power of two divider. */
  while ( clock_rate > clock_hz ) {
    divider = (divider << 1);

    clock_rate = GPU_CORE_CLOCK_RATE / divider;
  }

  *clock_divider = divider;

  return RTEMS_SUCCESSFUL;
}

/**
 * @brief Set the SPI bus transfer mode.
 *
 * @param[in] bushdl Pointer to the libi2c API bus driver data structure.
 * @param[in] tfr_mode Pointer to a libi2c API transfer mode data structure.
 *
 * @retval RTEMS_SUCCESSFUL Successfully setup the bus transfer mode as desired.
 * @retval RTEMS_INVALID_NUMBER This can have two meanings:
 *                              1. The specified number of bytes per char is not
 *                                 8, 16, 24 or 32;
 *                              2. @see rpi_spi_calculate_clock_divider()
 */
static rtems_status_code rpi_spi_set_tfr_mode(
  rtems_libi2c_bus_t *bushdl,
  const rtems_libi2c_tfr_mode_t *tfr_mode
) {
  rpi_spi_softc_t *softc_ptr = &(((rpi_spi_desc_t *)(bushdl))->softc);
  rtems_status_code sc = RTEMS_SUCCESSFUL;
  uint16_t clock_divider;

  /* Set the dummy character. */
  softc_ptr->dummy_char = tfr_mode->idle_char;

  /* Calculate the most appropriate clock divider. */
  sc = rpi_spi_calculate_clock_divider(tfr_mode->baudrate, &clock_divider);

  if ( sc != RTEMS_SUCCESSFUL ) {
    return sc;
  }

  /* Set the bus clock divider. */
  BCM2835_REG(BCM2835_SPI_CLK) = clock_divider;

  /* Calculate how many bytes each character has.
   * Only multiples of 8 bits are accepted for the transaction. */
  switch ( tfr_mode->bits_per_char ) {
    case 8:
    case 16:
    case 24:
    case 32:
      softc_ptr->bytes_per_char = tfr_mode->bits_per_char / 8;
      break;

    default:
      return RTEMS_INVALID_NUMBER;
  }

  /* Check the data mode (most or least significant bit first) and calculate
   * the correcting bit shift value to apply on the data before sending. */
  if ( tfr_mode->lsb_first ) {
    softc_ptr->bit_shift = 32 - tfr_mode->bits_per_char;
  }
  /* If MSB first. */
  else {
    softc_ptr->bit_shift = 0;
  }

  /* Set SPI clock polarity.
   * If clock_inv is TRUE, the clock is active high.*/
  if ( tfr_mode->clock_inv ) {
    /* Rest state of clock is low. */
    BCM2835_REG(BCM2835_SPI_CS) &= ~(1 << 3);
  }
  else {
    /* Rest state of clock is high. */
    BCM2835_REG(BCM2835_SPI_CS) |= (1 << 3);
  }

  /* Set SPI clock phase.
   * If clock_phs is true, clock starts toggling
   * at the start of the data transfer. */
  if ( tfr_mode->clock_phs ) {
    /* First SCLK transition at beginning of data bit. */
    BCM2835_REG(BCM2835_SPI_CS) |= (1 << 2);
  }
  else {
    /* First SCLK transition at middle of data bit. */
    BCM2835_REG(BCM2835_SPI_CS) &= ~(1 << 2);
  }

  return sc;
}

/**
 * @brief Reads/writes to/from the SPI bus.
 *
 * @param[in] bushdl Pointer to the libi2c API bus driver data structure.
 * @param[in] rd_buf Read buffer. If not NULL the function will read from
 *                   the bus and store the read on this buffer.
 * @param[in] wr_buf Write buffer. If not NULL the function will write the
 *                   contents of this buffer to the bus.
 * @param[in] buffer_size Size of the non-NULL buffer.
 *
 * @retval -1 Could not send/receive data to/from the bus.
 * @retval >=0 The number of bytes read/written.
 */
static int rpi_spi_read_write(
  rtems_libi2c_bus_t * bushdl,
  unsigned char *rd_buf,
  const unsigned char *wr_buf,
  int buffer_size
) {
  rpi_spi_softc_t *softc_ptr = &(((rpi_spi_desc_t *)(bushdl))->softc);

  uint8_t bytes_per_char = softc_ptr->bytes_per_char;
  uint8_t bit_shift =  softc_ptr->bit_shift;
  uint32_t dummy_char = softc_ptr->dummy_char;

  uint32_t bytes_sent = buffer_size;
  uint32_t fifo_data;

  /* Clear SPI bus FIFOs. */
  BCM2835_REG(BCM2835_SPI_CS) |= (3 << 4);

  /* Set SPI transfer active. */
  BCM2835_REG(BCM2835_SPI_CS) |= (1 << 7);

  /* If using the SPI bus in interrupt-driven mode. */
#if SPI_IO_MODE == 1
  softc_ptr->irq_write = 1;

  BCM2835_REG(BCM2835_SPI_CS) |= (1 << 9);

  if ( rtems_event_transient_receive(RTEMS_WAIT, 0) != RTEMS_SUCCESSFUL ) {
    rtems_event_transient_clear();

    return -1;
  }

  /* If using the bus in polling mode. */
#else
  /* Poll TXD bit until there is space to write at least one byte
   * on the TX FIFO. */
  SPI_POLLING((BCM2835_REG(BCM2835_SPI_CS) & (1 << 18)) == 0);
#endif

  /* While there is data to be transferred. */
  while ( buffer_size >= bytes_per_char ) {
    /* If reading from the bus, send a dummy character to the device. */
    if ( rd_buf != NULL ) {
      BCM2835_REG(BCM2835_SPI_FIFO) = dummy_char;
    }
    /* If writing to the bus, move the buffer data to the TX FIFO. */
    else {
      switch ( bytes_per_char ) {
        case 1:
          BCM2835_REG(BCM2835_SPI_FIFO) = (((*wr_buf) & 0xFF) << bit_shift);
          break;

        case 2:
          BCM2835_REG(BCM2835_SPI_FIFO) = (((*wr_buf) & 0xFFFF) << bit_shift);
          break;

        case 3:
          BCM2835_REG(BCM2835_SPI_FIFO) = (((*wr_buf) & 0xFFFFFF) << bit_shift);
          break;

        case 4:
          BCM2835_REG(BCM2835_SPI_FIFO) = ((*wr_buf) << bit_shift);
          break;

        default:
          return -1;
      }

      wr_buf += bytes_per_char;

      buffer_size -= bytes_per_char;
    }

    /* If using bi-directional SPI. */
    if ( bidirectional ) {
      /* Change bus direction to read from the slave device. */
      BCM2835_REG(BCM2835_SPI_CS) |= (1 << 12);
    }

    /* If using the SPI bus in interrupt-driven mode. */
#if SPI_IO_MODE == 1
    softc_ptr->irq_write = 0;

    BCM2835_REG(BCM2835_SPI_CS) |= (1 << 9);

    if ( rtems_event_transient_receive(RTEMS_WAIT, 0) != RTEMS_SUCCESSFUL ) {
      rtems_event_transient_clear();

      return -1;
    }

    /* If using the bus in polling mode. */
#else
    /* Poll the Done bit until the data transfer is complete. */
    SPI_POLLING((BCM2835_REG(BCM2835_SPI_CS) & (1 << 16)) == 0);

    /* Poll the RXD bit until there is at least one byte
     * on the RX FIFO to be read. */
    SPI_POLLING((BCM2835_REG(BCM2835_SPI_CS) & (1 << 17)) == 0);
#endif

    /* If writing to the bus, read the dummy char sent by the slave device. */
    if ( rd_buf == NULL ) {
      fifo_data = BCM2835_REG(BCM2835_SPI_FIFO) & 0xFF;
    }

    /* If reading from the bus, retrieve data from the RX FIFO and
     * store it on the buffer. */
    if ( rd_buf != NULL ) {
      switch ( bytes_per_char ) {
        case 1:
          fifo_data = BCM2835_REG(BCM2835_SPI_FIFO) & 0xFF;
          (*rd_buf) = (fifo_data >> bit_shift);
          break;

        case 2:
          fifo_data = BCM2835_REG(BCM2835_SPI_FIFO) & 0xFFFF;
          (*rd_buf) = (fifo_data >> bit_shift);
          break;

        case 3:
          fifo_data = BCM2835_REG(BCM2835_SPI_FIFO) & 0xFFFFFF;
          (*rd_buf) = (fifo_data >> bit_shift);
          break;

        case 4:
          fifo_data = BCM2835_REG(BCM2835_SPI_FIFO);
          (*rd_buf) = (fifo_data >> bit_shift);
          break;

        default:
          return -1;
      }

      rd_buf += bytes_per_char;

      buffer_size -= bytes_per_char;
    }

    /* If using bi-directional SPI. */
    if ( bidirectional ) {
      /* Restore bus direction to write to the slave. */
      BCM2835_REG(BCM2835_SPI_CS) &= ~(1 << 12);
    }
  }

  /* If using the SPI bus in interrupt-driven mode. */
#if SPI_IO_MODE == 1
  softc_ptr->irq_write = 1;

  BCM2835_REG(BCM2835_SPI_CS) |= (1 << 9);

  if ( rtems_event_transient_receive(RTEMS_WAIT, 0) != RTEMS_SUCCESSFUL ) {
    rtems_event_transient_clear();

    return -1;
  }

  /* If using the bus in polling mode. */
#else
  /* Poll the Done bit until the data transfer is complete. */
  SPI_POLLING((BCM2835_REG(BCM2835_SPI_CS) & (1 << 16)) == 0);
#endif

  bytes_sent -= buffer_size;

  return bytes_sent;
}

/**
 * @brief Handler function that is called on any SPI interrupt.
 *
 *        There are 2 situations that can generate an interrupt:
 *
 *        1. Transfer (read/write) complete;
 *        2. RX FIFO full.
 *
 *        Because the 2. situation is not useful to many applications,
 *        the only interrupt that is generated and handled is the
 *        transfer complete interrupt.
 *
 *        The objective of the handler is then, depending on the transfer
 *        context (reading or writing on the bus), to check if there is enough
 *        space available on the TX FIFO to send data over the bus (if writing)
 *        or if the slave device has sent enough data to be fetched from the
 *        RX FIFO (if reading).
 *
 *        When any of these two conditions occur, disables further interrupts
 *        to be generated and sends a waking event to the transfer task
 *        which will allow the following transfer to proceed.
 *
 * @param[in] arg Void pointer to the bus data structure.
 */
#if SPI_IO_MODE == 1
static void spi_handler(void* arg)
{
  rpi_spi_softc_t *softc_ptr = (rpi_spi_softc_t *) arg;

  /* If waiting to write to the bus, expect the TXD bit to be set, or
   * if waiting to read from the bus, expect the RXD bit to be set
   * before sending a waking event to the transfer task. */
  if (
      ( softc_ptr->irq_write == 1 &&
        (BCM2835_REG(BCM2835_SPI_CS) & (1 << 18)) != 0
      ) ||
      ( softc_ptr->irq_write == 0 &&
        (BCM2835_REG(BCM2835_SPI_CS) & (1 << 17)) != 0
      )
  ) {
    /* Disable the SPI interrupt generation when a transfer is complete. */
    BCM2835_REG(BCM2835_SPI_CS) &= ~(1 << 9);

    /* Allow the transfer process to continue. */
    rtems_event_transient_send(softc_ptr->task_id);
  }
}
#endif

/**
 * @brief Low level function to initialize the SPI bus.
 *        This function is used by the libi2c API.
 *
 * @param[in] bushdl Pointer to the libi2c API bus driver data structure.
 *
 * @retval RTEMS_SUCCESSFUL SPI bus successfully initialized.
 * @retval Any other status code @see rtems_interrupt_handler_install().
 */
static rtems_status_code rpi_libi2c_spi_init(rtems_libi2c_bus_t * bushdl)
{
  rpi_spi_softc_t *softc_ptr = &(((rpi_spi_desc_t *)(bushdl))->softc);
  rtems_status_code sc = RTEMS_SUCCESSFUL;

  if ( softc_ptr->initialized == 1 ) {
    return sc;
  }

  softc_ptr->initialized = 1;

  /* If using the SPI bus in interrupt-driven mode. */
#if SPI_IO_MODE == 1
  softc_ptr->task_id = rtems_task_self();

  sc = rtems_interrupt_handler_install(
         BCM2835_IRQ_ID_SPI,
         NULL,
         RTEMS_INTERRUPT_UNIQUE,
         (rtems_interrupt_handler) spi_handler,
         softc_ptr
       );
#endif

  return sc;
}

/**
 * @brief Low level function that would send a start condition over an I2C bus.
 *        As it is not required to access a SPI bus it is here just to satisfy
 *        the libi2c API, which requires this function.
 *
 * @param[in] bushdl Pointer to the libi2c API bus driver data structure.
 *
 * @retval RTEMS_SUCCESSFUL
 */
static rtems_status_code rpi_libi2c_spi_send_start(rtems_libi2c_bus_t * bushdl)
{
  return RTEMS_SUCCESSFUL;
}

/**
 * @brief Low level function that terminates a SPI transfer.
 *        It stops the SPI transfer and unselects the current SPI slave device.
 *        This function is used by the libi2c API.
 *
 * @param[in] bushdl Pointer to the libi2c API bus driver data structure.
 *
 * @retval RTEMS_SUCCESSFUL The slave device has been successfully unselected.
 * @retval RTEMS_INVALID_ADDRESS The stored slave address is neither 0 or 1.
 */
static rtems_status_code rpi_libi2c_spi_stop(rtems_libi2c_bus_t * bushdl)
{
  rpi_spi_softc_t *softc_ptr = &(((rpi_spi_desc_t *)(bushdl))->softc);

  uint32_t addr = softc_ptr->current_slave_addr;
  uint32_t chip_select_bit = 21 + addr;

  /* Set SPI transfer as not active. */
  BCM2835_REG(BCM2835_SPI_CS) &= ~(1 << 7);

  /* Unselect the active SPI slave. */
  switch ( addr ) {
    case 0:
    case 1:
      BCM2835_REG(BCM2835_SPI_CS) |= (1 << chip_select_bit);

      break;

    default:
      return RTEMS_INVALID_ADDRESS;
  }

  return RTEMS_SUCCESSFUL;
}

/**
 * @brief Low level function which addresses a SPI slave device.
 *        This function is used by the libi2c API.
 *
 * @param[in] bushdl Pointer to the libi2c API bus driver data structure.
 * @param[in] addr SPI slave select line address (0 for CE0 or 1 for CE1).
 * @param[in] rw This values is unnecessary to address a SPI device and its
 *               presence here is only to fulfill a libi2c requirement.
 *
 * @retval RTEMS_SUCCESSFUL The slave device has been successfully addressed.
 * @retval RTEMS_INVALID_ADDRESS The received address is neither 0 or 1.
 */
static rtems_status_code rpi_libi2c_spi_send_addr(
  rtems_libi2c_bus_t * bushdl,
  uint32_t addr,
  int rw
) {
  rpi_spi_softc_t *softc_ptr = &(((rpi_spi_desc_t *)(bushdl))->softc);

  /* Calculates the bit corresponding to the received address
   * on the SPI control register. */
  uint32_t chip_select_bit = 21 + addr;

  /* Save which slave will be currently addressed,
   * so it can be unselected later. */
  softc_ptr->current_slave_addr = addr;

  /* Select one of the two available SPI slave address lines. */
  switch ( addr ) {
    case 0:
    case 1:
      BCM2835_REG(BCM2835_SPI_CS) &= ~(1 << chip_select_bit);
      break;

    default:
      return RTEMS_INVALID_ADDRESS;
  }

  return RTEMS_SUCCESSFUL;
}

/**
 * @brief Low level function that reads a number of bytes from the SPI bus
 *        on to a buffer.
 *        This function is used by the libi2c API.
 *
 * @param[in] bushdl Pointer to the libi2c API bus driver data structure.
 * @param[in] bytes Buffer where the data read from the bus will be stored.
 * @param[in] nbytes Number of bytes to be read from the bus to the bytes buffer.
 *
 * @retval @see rpi_spi_read_write().
 */
static int rpi_libi2c_spi_read_bytes(
  rtems_libi2c_bus_t * bushdl,
  unsigned char *bytes,
  int nbytes
) {
  return rpi_spi_read_write(bushdl, bytes, NULL, nbytes);
}

/**
 * @brief Low level function that writes a number of bytes from a buffer
 *        to the SPI bus.
 *        This function is used by the libi2c API.
 *
 * @param[in] bushdl Pointer to the libi2c API bus driver data structure.
 * @param[in] bytes Buffer with data to send over the SPI bus.
 * @param[in] nbytes Number of bytes to be written from the bytes buffer
                     to the bus.
 *
 * @retval @see rpi_spi_read_write().
 */
static int rpi_libi2c_spi_write_bytes(
  rtems_libi2c_bus_t * bushdl,
  unsigned char *bytes,
  int nbytes
) {
  return rpi_spi_read_write(bushdl, NULL, bytes, nbytes);
}

/**
 * @brief Low level function that is used to perform ioctl
 *        operations on the bus. Currently only setups
 *        the bus transfer mode.
 *        This function is used by the libi2c API.
 *
 * @param[in] bushdl Pointer to the libi2c API bus driver data structure.
 * @param[in] cmd IOCTL request command.
 * @param[in] arg Arguments needed to fulfill the requested IOCTL command.
 *
 * @retval -1 Unknown request command.
 * @retval >=0 @see rpi_spi_set_tfr_mode().
 */
static int rpi_libi2c_spi_ioctl(rtems_libi2c_bus_t * bushdl, int cmd, void *arg)
{
  switch ( cmd ) {
    case RTEMS_LIBI2C_IOCTL_SET_TFRMODE:
      return rpi_spi_set_tfr_mode(
               bushdl,
               (const rtems_libi2c_tfr_mode_t *)arg
             );
    default:
      return -1;
  }

  return 0;
}

static rtems_libi2c_bus_ops_t rpi_spi_ops = {
  .init = rpi_libi2c_spi_init,
  .send_start = rpi_libi2c_spi_send_start,
  .send_stop = rpi_libi2c_spi_stop,
  .send_addr = rpi_libi2c_spi_send_addr,
  .read_bytes = rpi_libi2c_spi_read_bytes,
  .write_bytes = rpi_libi2c_spi_write_bytes,
  .ioctl = rpi_libi2c_spi_ioctl
};

static rpi_spi_desc_t rpi_spi_bus_desc = {
  {
    .ops = &rpi_spi_ops,
    .size = sizeof(rpi_spi_bus_desc)
  },
  {
    .initialized = 0
  }
};

int rpi_spi_init(bool bidirectional_mode)
{
  /* Initialize the libi2c API. */
  rtems_libi2c_initialize();

  /* Enable the SPI interface on the Raspberry Pi. */
  rtems_gpio_initialize();

  assert ( rpi_gpio_select_spi() == RTEMS_SUCCESSFUL );

  bidirectional = bidirectional_mode;

  /* Clear SPI control register and clear SPI FIFOs. */
  BCM2835_REG(BCM2835_SPI_CS) = (3 << 4);

  /* Register the SPI bus. */
  return rtems_libi2c_register_bus("/dev/spi", &(rpi_spi_bus_desc.bus_desc));
}