SPI Device API
To create an SPI device, first call spi_init
, passing in an spi_config_t
struct. This struct defines the clock and MOSI/MISO pins, the SPI mode, and the done
callback.
spi_dev_t spi_init(spi_config_t *config)
Initializes an SPI device interface. The config
argument defines the pins, mode, and callbacks for the SPI device. It contains the following fields:
Field | Type | Description |
---|---|---|
sck | pin_t | The clock pin |
mosi | pin_t | The MOSI data pin (or NO_PIN to disable MOSI) |
miso | pin_t | The MISO data pin (or NO_PIN to disable MISO) |
mode | uint32_t | SPI mode: 0 , 1 , 2 , or 3 (default: 0 ) |
done | callback | Called when an SPI transaction finishes (see below) |
user_data | void \* | Data that will be passed in the first argument of the done callback |
The API does not support a CS/SS pin: it is up to the user to select/deselect the SPI interface by calling spi_start()
and spi_stop()
.
注意
Note: spi_init
can only be called from chip_init()
. Do not call it at a later time.
Example:
const spi_config_t spi1 = {
.sck = pin_init("SCK", INPUT),
.mosi = pin_init("MOSI", INPUT),
.miso = pin_init("MISO", INPUT),
.mode = 0,
.done = chip_spi_done, // See the example below
.user_data = chip,
};