SPI is also known as Serial Peripheral Interface, it is a synchronous serial data protocol that acts as an interface bus which operates at full-duplex where data can be sent and received simultaneously.

SPI is one of the most popular communication peripheral used by microcontrollers to send data to one or more peripheral devices like SD cards and sensors quickly over short distances.

The SPI module allows users to utilize the SPI bus. If you want to add a sensor to the MetaWear board that communicates with SPI, this is possible using the SPI module.

Functions are defined in the spi.h header file.

Data Signal

I2C data signals are retrieved by calling mbl_mw_spi_get_data_signal. You will need to pass two parameters:

  • Length variable that sets how many bytes the signal is expected to receive

  • An unique ID identifying the signal

If the id value has already been used, the length parameter will be ignored and the previously created signal will be returned.

let spi_signal= mbl_mw_spi_get_data_signal(board, 5, 0xe)


To read SPI data, use the mbl_mw_datasignal_read_with_parameters function with the parameters set by the MblMwSpiParameters struct. When reading SPI data, the byte array pointed to by the data field will be written on the bus before reading.

SPI data is always returned as a byte array.

mbl_mw_datasignal_subscribe(spi_signal, bridge(obj: self)) { (context, obj) in
    let bytes: [UInt8] = obj!.pointee.valueAs()
var parameters = MblMwSpiParameters(MBL_MW_SPI_MODE_3, MBL_MW_SPI_FREQUENCY_8_MHZ, {0xda}, 1, 0xa, 0, 0xb, 7, 0, 1)
mbl_mw_datasignal_read_with_parameters(spi_signal, &parameters)


Writing data through the SPI bus is handled with the mbl_mw_spi_write function. The same MblMwSpiParameters struct is used to wrap the required parameters into one variable.