Skip to content

SPI

The Serial Peripheral Interface bus (SPI) is a synchronous serial communication interface specification used for short distance communication, primarily for inter-chip communication within a single PCB.

SPI devices communicate in full duplex mode using a master-slave architecture with a single master.

MiraOS supports multiple independent instances. Only SPI master is currently supported.

Enums

mira_spi_mode_t

Name Value Description
MIRA_SPI_MODE_0

SPI mode 0. CPOL=0, CPHA=0. Clock idle low, sample data on leading clock edge.

MIRA_SPI_MODE_1

SPI mode 1. CPOL=0, CPHA=1. Clock idle low, sample data on trailing clock edge.

MIRA_SPI_MODE_2

SPI mode 2. CPOL=1, CPHA=0. Clock idle high, sample data on leading clock edge.

MIRA_SPI_MODE_3

SPI mode 3. CPOL=1, CPHA=1. Clock idle high, sample data on trailing clock edge.

Structs

mira_spi_config_t

Type Name Description
uint32_t frequency SPI clock frequency in Hz. The frequency will be set to the closest, lower or equal, supported frequency.
mira_gpio_pin_t sck_pin
mira_gpio_pin_t mosi_pin
mira_gpio_pin_t miso_pin
mira_gpio_pin_t ss_pin
mira_spi_mode_t mode
mira_bit_order_t bit_order

Functions

mira_spi_init

mira_status_t mira_spi_init(
    uint8_t                   spi_id,
    const mira_spi_config_t*  config);

Initialize SPI peripheral. Before calling this function again mira_spi_uninit has to be called.

Parameters

Parameter Description
spi_id Peripheral id.
config Configuration. Can be discarded after the return of this function.

Return

See the enum definition.

Value Description
MIRA_SUCCESS The SPI interface was successfully initialized.
MIRA_ERROR_UNKNOWN An unknown error occurred.
MIRA_ERROR_INVALID_ID The SPI id is invalid.
MIRA_SPI_ERROR_CONFIG The config is invalid.
MIRA_SPI_ERROR_MODE The mode is invalid.

mira_spi_uninit

mira_status_t mira_spi_uninit(
    uint8_t       spi_id);

Uninitialize SPI peripheral.

Parameters

Parameter Description
spi_id Peripheral id.

Return

See the enum definition.

Value Description
MIRA_SUCCESS The SPI interface was successfully uninitialized.
MIRA_ERROR_UNKNOWN An unknown error occurred.
MIRA_ERROR_INVALID_ID The SPI id is invalid.

mira_spi_transfer

mira_status_t mira_spi_transfer(
    uint8_t       spi_id,
    uint8_t*      tx_buffer,
    uint16_t      tx_buffer_length,
    uint8_t*      rx_buffer,
    uint16_t      rx_buffer_length);

Transmit and receive data. The process calling this function will be sent a PROCESS_EVENT_POLL event after the transmission has completed.

Parameters

Parameter Description
spi_id Peripheral id.
tx_buffer Buffer containing the data to transmit.
tx_buffer_length Number of bytes to transmit.
rx_buffer Buffer where to store data received while transmitting.
rx_buffer_length Max number of bytes to receive.

Return

See the enum definition.

Value Description
MIRA_SUCCESS
MIRA_ERROR_UNKNOWN An unknown error occurred.
MIRA_ERROR_INVALID_ID The SPI id is invalid.
MIRA_ERROR_NOT_INITIALIZED The SPI interface is not initialized.
MIRA_SPI_ERROR_BUSY The SPI interface is busy.

mira_spi_transfer_is_in_progress

mira_bool_t mira_spi_transfer_is_in_progress(
    uint8_t       spi_id);

Check if an SPI transfer is in progress.

Parameters

Parameter Description
spi_id Peripheral id.

Return

See the enum definition.

Value Description
MIRA_TRUE SPI transfer is in progress.
MIRA_FALSE SPI transfer is not in progress.