SPI

Overview

  • Serial Peripheral Interface (SPI) is a serial bus specification used for high-speed, full-duplex, and synchronous communication.

  • SPI is developed by Motorola. It is commonly used for communication with flash memory, real-time clocks, sensors, and analog-to-digital (A/D) converters.

  • SPI works in controller/device mode. Generally, there is one SPI controller that controls one or more SPI devices. They are connected via four wires:

    • SCLK: clock signals output from the SPI controller
    • MOSI: data output from the SPI controller and input into an SPI device
    • MISO: data output from an SPI device and input into the SPI controller
    • CS: signals enabled by an SPI device and controlled by the SPI controller
  • Figure 1 shows the connection between one SPI controller and two SPI devices (device A and device B). In this figure, device A and device B share three pins (SCLK, MISO, and MOSI) of the controller. CS0 of device A and CS1 of device B are connected to CS0 and CS1 of the controller, respectively.

Figure 1 SPI controller/device connection

  • SPI communication is usually initiated by the SPI controller and is operated as follows:
  1. A single SPI device is selected at a time via the CS to communicate with the SPI controller.
  2. Clock signals are provided for the selected SPI device via the SCLK.
  3. The SPI controller sends data to SPI devices via the MOSI, and receives data from SPI devices via the MISO.
  • SPI can work in one of the following four modes, equivalent to one of the four possible states for Clock Polarity (CPOL) and Clock Phase (CPHA):

    • If both CPOL and CPHA are 0, the clock signal level is low in the idle state and data is sampled on the first clock edge.
    • If CPOL is 0 and CPHA is 1, the clock signal level is low in the idle state and data is sampled on the second clock edge.
    • If CPOL is 1 and CPHA is 0, the clock signal level is high in the idle state and data is sampled on the first clock edge.
    • If both CPOL and CPHA are 1, the clock signal level is high in the idle state and data is sampled on the second clock edge.
  • SPI defines a set of common functions for operating an SPI device, including those for:

    • Obtaining and releasing the handle of an SPI device.
    • Reading or writing data of a specified length from or into an SPI device.
    • Customizing data reading or writing via SpiMsg.
    • Obtaining and setting SPI device configuration parameters.

NOTE: Currently, these functions are only applicable in the communication initiated by the SPI controller.

Available APIs

Table 1 APIs for the SPI driver

Capability

Function

Description

SPI device handle obtaining/releasing

SpiOpen

Obtains an SPI device handle.

SpiClose

Releases an SPI device handle.

SPI reading/writing

SpiRead

Reads data of a specified length from an SPI device.

SpiWrite

Writes data of a specified length into an SPI device.

SpiTransfer

Transfers SPI data.

SPI device configuration

SpiSetCfg

Sets configuration parameters for an SPI device.

SpiGetCfg

Obtains configuration parameters of an SPI device.

NOTE: All functions provided in this document can be called only in kernel space.

Usage Guidelines

How to Use

Figure 2 shows the process of using an SPI device.

Figure 2 Process of using an SPI device

Obtaining an SPI Device Handle

Before performing SPI communication, obtain an SPI device handle by calling SpiOpen. This function returns an SPI device handle with a specified bus number and CS number.

DevHandle SpiOpen(const struct SpiDevInfo *info);

Table 2 Description of SpiOpen

Parameter

Description

info

Pointer to the SPI device descriptor.

Return Value

Description

NULL

Failed to obtain an SPI device handle.

Device handle

Returns the pointer to the SPI device handle.

The following example shows how to obtain an SPI device handle based on the assumption that both the bus number and CS number of the SPI device are 0.

struct SpiDevInfo spiDevinfo;       /* SPI device descriptor */
DevHandle spiHandle = NULL; /* SPI device handle */
spiDevinfo.busNum = 0;              /* SPI device bus number */
spiDevinfo.csNum = 0;               /* SPI device CS number */

/* Obtain an SPI device handle. */
spiHandle = SpiOpen(&spiDevinfo);
if (spiHandle == NULL) {
    HDF_LOGE("SpiOpen: failed\n");
    return;
}

Obtaining SPI Device Configuration Parameters

After obtaining the SPI device handle, obtain the SPI device configuration parameters by calling the following function:

int32_t SpiGetCfg(DevHandle handle, struct SpiCfg *cfg);

Table 3 Description of SpiGetCfg

Parameter

Description

handle

SPI device handle.

cfg

Pointer to SPI device configuration parameters.

Return Value

Description

0

Succeeded in obtaining SPI device configuration parameters.

Negative value

Failed to obtain SPI device configuration parameters.

int32_t ret;
struct SpiCfg cfg = {0};                /* SPI configuration information */
ret = SpiGetCfg(spiHandle, &cfg);       /* Obtain SPI device configuration parameters. */
if (ret != 0) {
    HDF_LOGE("SpiGetCfg: failed, ret %d\n", ret);
}

Setting SPI Device Configuration Parameters

After obtaining the SPI device handle, set SPI device configuration parameters by calling the following function:

int32_t SpiSetCfg(DevHandle handle, struct SpiCfg *cfg);

Table 4 Description of SpiSetCfg

Parameter

Description

handle

SPI device handle.

cfg

Pointer to SPI device configuration parameters.

Return Value

Description

0

Succeeded in setting SPI device configuration parameters.

Negative value

Failed to set SPI device configuration parameters.

int32_t ret;
struct SpiCfg cfg = {0};                     /* SPI configuration information */
cfg.mode = SPI_MODE_LOOP;                    /* Communication in loopback mode */
cfg.transferMode = PAL_SPI_POLLING_TRANSFER; /* Communication in polling mode */
cfg.maxSpeedHz = 115200;                     /* Maximum transmission frequency */
cfg.bitsPerWord = 8;                         /* The width of per word to be read or written is 8 bits. */
ret = SpiSetCfg(spiHandle, &cfg);            /* Set SPI device configuration parameters. */
if (ret != 0) {
    HDF_LOGE("SpiSetCfg: failed, ret %d\n", ret);
}

Performing SPI Communication

  • Writing data of a specific length into an SPI device

To write data into an SPI device only once, call the following function:

int32_t SpiWrite(DevHandle handle, uint8_t *buf, uint32_t len);

Table 5 Description of SpiWrite

Parameter

Description

handle

SPI device handle.

buf

Pointer to the data to write.

len

Length of the data to write.

Return Value

Description

0

Succeeded in writing data into an SPI device.

Negative value

Failed to write data into an SPI device.

int32_t ret;
uint8_t wbuff[4] = {0x12, 0x34, 0x56, 0x78};
/* Write data of a specific length into an SPI device. */
ret = SpiWrite(spiHandle, wbuff, 4);
if (ret != 0) {
    HDF_LOGE("SpiWrite: failed, ret %d\n", ret);
}
  • Reading data of a specific length from an SPI device

To read data from an SPI device only once, call the following function:

int32_t SpiRead(DevHandle handle, uint8_t *buf, uint32_t len);

Table 6 Description of SpiRead

Parameter

Description

handle

SPI device handle.

buf

Pointer to the data to read.

len

Length of the data to read.

Return Value

Description

0

Succeeded in reading data from an SPI device.

Negative value

Failed to read data from an SPI device.

int32_t ret;
uint8_t rbuff[4] = {0};
/* Read data of a specific length from an SPI device. */
ret = SpiRead(spiHandle, rbuff, 4);
if (ret != 0) {
    HDF_LOGE("SpiRead: failed, ret %d\n", ret);
}
  • Launching a custom transfer

To launch a custom transfer, call the following function:

int32_t SpiTransfer(DevHandle handle, struct SpiMsg *msgs, uint32_t count);

Table 7 Description of SpiTransfer

Parameter

Description

handle

SPI device handle.

msgs

Pointer to the message array to be transferred.

count

Number of messages in the message array.

Return Value

Description

0

Succeeded in launching the custom transfer.

Negative value

Failed to launch the custom transfer.

int32_t ret;
uint8_t wbuff[1] = {0x12};
uint8_t rbuff[1] = {0};
struct SpiMsg msg;        /* Custom message to be transferred */
msg.wbuf = wbuff;         /* Pointer to the data to write */
msg.rbuf = rbuff;         /* Pointer to the data to read */
msg.len = 1;              /* The length of the data to read or write is 1 bit. */
msg.csChange = 1;         /* Disable the CS before the next transfer. */
msg.delayUs = 0;          /* No delay before the next transfer */
msg.speed = 115200;       /* Speed of this transfer */
/* Launch a custom transfer. The number of messages to be transferred is 1. */
ret = SpiTransfer(spiHandle, &msg, 1);
if (ret != 0) {
    HDF_LOGE("SpiTransfer: failed, ret %d\n", ret);
}

Destroying the SPI Device Handle

After the SPI communication, destroy the SPI device handle by calling the following function:

void SpiClose(DevHandle handle);

This function will release the resources previously obtained.

Table 8 Description of SpiClose

Parameter

Description

handle

SPI device handle.

SpiClose(spiHandle); /* Destroy the SPI device handle. */

Usage Example

The following example shows how to obtain an SPI device handle, set the configuration parameters, and then read or write data from or into the SPI device, and finally destroy the SPI device handle.

#include "hdf_log.h"
#include "spi_if.h"

void SpiTestSample(void)
{
    int32_t ret;
    struct SpiCfg cfg;                  /* SPI device configuration information */
    struct SpiDevInfo spiDevinfo;       /* SPI device descriptor */
    DevHandle spiHandle = NULL; /* SPI device handle */
    struct SpiMsg msg;                  /* Custom message to be transferred */
    uint8_t rbuff[4] = { 0 };
    uint8_t wbuff[4] = { 0x12, 0x34, 0x56, 0x78 };
    uint8_t wbuff2[4] = { 0xa1, 0xb2, 0xc3, 0xd4 };

    spiDevinfo.busNum = 0;              /* SPI device bus number */
    spiDevinfo.csNum = 0;               /* SPI device CS number */
    spiHandle = SpiOpen(&spiDevinfo);   /* Obtain an SPI device handle based on spiDevinfo. */
    if (spiHandle == NULL) {
        HDF_LOGE("SpiOpen: failed\n");
        return;
    }
    /* Obtain configuration parameters of an SPI device. */
    ret = SpiGetCfg(spiHandle, &cfg);
    if (ret != 0) {
        HDF_LOGE("SpiGetCfg: failed, ret %d\n", ret);
        goto err;
    }
    cfg.maxSpeedHz = 115200;                /* Change the maximum clock frequency to 115200. */
    cfg.bitsPerWord = 8;                    /* Change the word width to 8 bits. */
    /* Set configuration parameters for an SPI device. */
    ret = SpiSetCfg(spiHandle, &cfg);
    if (ret != 0) {
        HDF_LOGE("SpiSetCfg: failed, ret %d\n", ret);
        goto err;
    }
    /* Write specified length of data into an SPI device. */
    ret = SpiWrite(spiHandle, wbuff, 4);
    if (ret != 0) {
        HDF_LOGE("SpiWrite: failed, ret %d\n", ret);
        goto err;
    }
    /* Read data of a specific length from an SPI device. */
    ret = SpiRead(spiHandle, rbuff, 4);
    if (ret != 0) {
        HDF_LOGE("SpiRead: failed, ret %d\n", ret);
        goto err;
    }
    msg.wbuf = wbuff2;  /* Pointer to the data to write */
    msg.rbuf = rbuff;   /* Pointer to the data to read */
    msg.len = 4;        /* The length of the data to read or write is 4 bits. */
    msg.csChange = 1;   /* Disable the CS before the next transfer. */
    msg.delayUs = 0;    /* No delay before the next transfer */
    msg.speed = 115200; /* Speed of this transfer */
    /* Launch a custom transfer. The number of messages to be transferred is 1. */
    ret = SpiTransfer(spiHandle, &msg, 1);
    if (ret != 0) {
        HDF_LOGE("SpiTransfer: failed, ret %d\n", ret);
        goto err;
    }
err:
    /* Destroy the SPI device handle. */
    SpiClose(spiHandle);
}