Mistake on this page?
Report an issue in GitHub or email us


SPI class hierarchy

The SPI Interface provides a Serial Peripheral Interface Master.

You can use this interface for communication with SPI slave devices, such as FLASH memory, LCD screens and other modules or integrated circuits.


A pinout map.

The default settings of the SPI interface are 1MHz, 8-bit, Mode 0.

You can use the SPI interface to write data words out of the SPI port, returning the data received back from the SPI slave. You can also configure the SPI clock frequency and format. The format is set to data word length 8 to 16 bits, and the mode as per the table below:

Mode Polarity Phase
0 0 0
1 0 1
2 1 0
3 1 1

The SPI master generates a clock to synchronously drive a serial bit stream slave. The slave returns a bit stream, also synchronous to the clock. To communicate with multiple slave devices connected over the same SPI peripheral, you must use multiple SPI objects, one for each slave, but instantiate them with a different Slave select (SSEL) and configuration corresponding to each slave. Note that the SPI object automatically configures the SPI peripheral with the current object's configuration when the application code invokes the interfaces on the SPI object.

SPI class reference

Public Member Functions
 SPI (PinName mosi, PinName miso, PinName sclk, PinName ssel=NC)
void format (int bits, int mode=0)
void frequency (int hz=1000000)
virtual int write (int value)
virtual int write (const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length)
virtual void lock (void)
virtual void unlock (void)
void set_default_write_value (char data)
template<typename Type >
int transfer (const Type *tx_buffer, int tx_length, Type *rx_buffer, int rx_length, const event_callback_t &callback, int event=SPI_EVENT_COMPLETE)
void abort_transfer ()
void clear_transfer_buffer ()
void abort_all_transfers ()
int set_dma_usage (DMAUsage usage)
Protected Member Functions
void irq_handler_asynch (void)
int transfer (const void *tx_buffer, int tx_length, void *rx_buffer, int rx_length, unsigned char bit_width, const event_callback_t &callback, int event)
int queue_transfer (const void *tx_buffer, int tx_length, void *rx_buffer, int rx_length, unsigned char bit_width, const event_callback_t &callback, int event)
void start_transfer (const void *tx_buffer, int tx_length, void *rx_buffer, int rx_length, unsigned char bit_width, const event_callback_t &callback, int event)
void aquire (void)
Protected Attributes
spi_t _spi
CThunk< SPI_irq
event_callback_t _callback
DMAUsage _usage
bool _deep_sleep_locked
int _bits
int _mode
int _hz
char _write_fill
Static Protected Attributes
static SPI_owner = NULL
static SingletonPtr< PlatformMutex_mutex

SPI hello, world

/* mbed Example Program
 * Copyright (c) 2006-2014 ARM Limited
 * 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,
 * See the License for the specific language governing permissions and
 * limitations under the License.
#include "mbed.h"
SPI spi(D11, D12, D13); // mosi, miso, sclk
DigitalOut cs(D0);
int main() {
    // Chip must be deselected
    cs = 1;

    // Setup the spi for 8 bit data, high steady state clock,
    // second edge capture, with a 1MHz clock rate
    // Select the device by seting chip select low
    cs = 0;
    // Send 0x8f, the command to read the WHOAMI register
    // Send a dummy byte to receive the contents of the WHOAMI register
    int whoami = spi.write(0x00);
    printf("WHOAMI register = 0x%X\n", whoami);
    // Deselect the device
    cs = 1;
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.