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_init
config argument defines the pins, mode, and callbacks for the SPI device. It contains the following fields:
The clock pin
The MOSI data pin (or
NO_PIN to disable MOSI)The MISO data pin (or
NO_PIN to disable MISO)SPI mode:
0, 1, 2, or 3 (default: 0)Called when an SPI transaction finishes (see below)
Data that will be passed in the first argument of the
done callbackspi_start() and spi_stop().
Example
spi_start
count bytes to/from the given buffer.
You will usually listen for the CS (chip select) pin with pin_watch. Call spi_start() when the CS pin goes low, and spi_stop() when the CS pin goes high.
When creating a device that transfers large amounts of data (e.g. an LCD display), it’s recommended to use a large buffer size (few kilobytes). The simulator can use the larger buffer to optimize DMA-controlled SPI transfer and speed up the simulation.
For simple devices that transfer small amounts of data, you can use a single-byte buffer, and process each byte as it arrives in the done callback.
spi_stop
The done callback
The signature for thedone callback is as follows:
done callback runs when an SPI transaction finishes: either when the buffer provided to spi_start is full, or when spi_stop was called. The buffer contains the data received (it is the same buffer given to spi_start), and count is the number of bytes that have been transferred (or 0 if spi_stop was called before a complete byte has been transferred).
Your done callback should check the status of the CS pin, and if it is still low, it should call spi_start() again to receive the next chunk of data from the microcontroller.