Macros | |
#define | spi0 ((spi_inst_t *)spi0_hw) |
#define | spi1 ((spi_inst_t *)spi1_hw) |
Enumerations | |
enum | spi_cpha_t { SPI_CPHA_0 = 0, SPI_CPHA_1 = 1 } |
Enumeration of SPI CPHA (clock phase) values. | |
enum | spi_cpol_t { SPI_CPOL_0 = 0, SPI_CPOL_1 = 1 } |
Enumeration of SPI CPOL (clock polarity) values. | |
enum | spi_order_t { SPI_LSB_FIRST = 0, SPI_MSB_FIRST = 1 } |
Enumeration of SPI bit-order values. | |
Functions | |
uint | spi_init (spi_inst_t *spi, uint baudrate) |
Initialise SPI instancesPuts the SPI into a known state, and enable it. Must be called before other functions. More... | |
void | spi_deinit (spi_inst_t *spi) |
Deinitialise SPI instancesPuts the SPI into a disabled state. Init will need to be called to reenable the device functions. More... | |
uint | spi_set_baudrate (spi_inst_t *spi, uint baudrate) |
Set SPI baudrateSet SPI frequency as close as possible to baudrate, and return the actual achieved rate. More... | |
uint | spi_get_baudrate (const spi_inst_t *spi) |
Get SPI baudrateGet SPI baudrate which was set by. More... | |
static uint | spi_get_index (const spi_inst_t *spi) |
Convert SPI instance to hardware instance number. More... | |
static void | spi_set_format (spi_inst_t *spi, uint data_bits, spi_cpol_t cpol, spi_cpha_t cpha, __unused spi_order_t order) |
Configure SPIConfigure how the SPI serialises and deserialises data on the wire. More... | |
static void | spi_set_slave (spi_inst_t *spi, bool slave) |
Set SPI master/slaveConfigure the SPI for master- or slave-mode operation. By default, spi_init() sets master-mode. More... | |
static bool | spi_is_writable (const spi_inst_t *spi) |
Check whether a write can be done on SPI device. More... | |
static bool | spi_is_readable (const spi_inst_t *spi) |
Check whether a read can be done on SPI device. More... | |
static bool | spi_is_busy (const spi_inst_t *spi) |
Check whether SPI is busy. More... | |
int | spi_write_read_blocking (spi_inst_t *spi, const uint8_t *src, uint8_t *dst, size_t len) |
Write/Read to/from an SPI deviceWrite len bytes from src to SPI. Simultaneously read len bytes from SPI to dst . Blocks until all data is transferred. No timeout, as SPI hardware always transfers at a known data rate. More... | |
int | spi_write_blocking (spi_inst_t *spi, const uint8_t *src, size_t len) |
Write to an SPI device, blockingWrite len bytes from src to SPI, and discard any data received back Blocks until all data is transferred. No timeout, as SPI hardware always transfers at a known data rate. More... | |
int | spi_read_blocking (spi_inst_t *spi, uint8_t repeated_tx_data, uint8_t *dst, size_t len) |
Read from an SPI deviceRead len bytes from SPI to dst . Blocks until all data is transferred. No timeout, as SPI hardware always transfers at a known data rate. repeated_tx_data is output repeatedly on TX as data is read in from RX. Generally this can be 0, but some devices require a specific value here, e.g. SD cards expect 0xff. More... | |
int | spi_write16_read16_blocking (spi_inst_t *spi, const uint16_t *src, uint16_t *dst, size_t len) |
Write/Read half words to/from an SPI deviceWrite len halfwords from src to SPI. Simultaneously read len halfwords from SPI to dst . Blocks until all data is transferred. No timeout, as SPI hardware always transfers at a known data rate. More... | |
int | spi_write16_blocking (spi_inst_t *spi, const uint16_t *src, size_t len) |
Write to an SPI deviceWrite len halfwords from src to SPI. Discard any data received back. Blocks until all data is transferred. No timeout, as SPI hardware always transfers at a known data rate. More... | |
int | spi_read16_blocking (spi_inst_t *spi, uint16_t repeated_tx_data, uint16_t *dst, size_t len) |
Read from an SPI deviceRead len halfwords from SPI to dst . Blocks until all data is transferred. No timeout, as SPI hardware always transfers at a known data rate. repeated_tx_data is output repeatedly on TX as data is read in from RX. Generally this can be 0, but some devices require a specific value here, e.g. SD cards expect 0xff. More... | |
static uint | spi_get_dreq (spi_inst_t *spi, bool is_tx) |
Return the DREQ to use for pacing transfers to/from a particular SPI instance. More... | |
Hardware SPI API
RP2040 has 2 identical instances of the Serial Peripheral Interface (SPI) controller.
The PrimeCell SSP is a master or slave interface for synchronous serial communication with peripheral devices that have Motorola SPI, National Semiconductor Microwire, or Texas Instruments synchronous serial interfaces.
Controller can be defined as master or slave using the spi_set_slave function.
Each controller can be connected to a number of GPIO pins, see the datasheet GPIO function selection table for more information.
#define spi0 ((spi_inst_t *)spi0_hw) |
Identifier for the first (SPI 0) hardware SPI instance (for use in SPI functions).
e.g. spi_init(spi0, 48000)
#define spi1 ((spi_inst_t *)spi1_hw) |
Identifier for the second (SPI 1) hardware SPI instance (for use in SPI functions).
e.g. spi_init(spi1, 48000)
void spi_deinit | ( | spi_inst_t * | spi | ) |
uint spi_get_baudrate | ( | const spi_inst_t * | spi | ) |
Get SPI baudrateGet SPI baudrate which was set by.
|
inlinestatic |
|
inlinestatic |
Convert SPI instance to hardware instance number.
spi | SPI instance |
uint spi_init | ( | spi_inst_t * | spi, |
uint | baudrate | ||
) |
Initialise SPI instancesPuts the SPI into a known state, and enable it. Must be called before other functions.
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |
int spi_read16_blocking | ( | spi_inst_t * | spi, |
uint16_t | repeated_tx_data, | ||
uint16_t * | dst, | ||
size_t | len | ||
) |
Read from an SPI deviceRead len
halfwords from SPI to dst
. Blocks until all data is transferred. No timeout, as SPI hardware always transfers at a known data rate. repeated_tx_data
is output repeatedly on TX as data is read in from RX. Generally this can be 0, but some devices require a specific value here, e.g. SD cards expect 0xff.
spi | SPI instance specifier, either spi0 or spi1 |
repeated_tx_data | Buffer of data to write |
dst | Buffer for read data |
len | Length of buffer dst in halfwords |
int spi_read_blocking | ( | spi_inst_t * | spi, |
uint8_t | repeated_tx_data, | ||
uint8_t * | dst, | ||
size_t | len | ||
) |
Read from an SPI deviceRead len
bytes from SPI to dst
. Blocks until all data is transferred. No timeout, as SPI hardware always transfers at a known data rate. repeated_tx_data
is output repeatedly on TX as data is read in from RX. Generally this can be 0, but some devices require a specific value here, e.g. SD cards expect 0xff.
spi | SPI instance specifier, either spi0 or spi1 |
repeated_tx_data | Buffer of data to write |
dst | Buffer for read data |
len | Length of buffer dst |
uint spi_set_baudrate | ( | spi_inst_t * | spi, |
uint | baudrate | ||
) |
Set SPI baudrateSet SPI frequency as close as possible to baudrate, and return the actual achieved rate.
spi | SPI instance specifier, either spi0 or spi1 |
baudrate | Baudrate required in Hz, should be capable of a bitrate of at least 2Mbps, or higher, depending on system clock settings. |
|
inlinestatic |
Configure SPIConfigure how the SPI serialises and deserialises data on the wire.
spi | SPI instance specifier, either spi0 or spi1 |
data_bits | Number of data bits per transfer. Valid values 4..16. |
cpol | SSPCLKOUT polarity, applicable to Motorola SPI frame format only. |
cpha | SSPCLKOUT phase, applicable to Motorola SPI frame format only |
order | Must be SPI_MSB_FIRST, no other values supported on the PL022 |
|
inlinestatic |
Set SPI master/slaveConfigure the SPI for master- or slave-mode operation. By default, spi_init() sets master-mode.
int spi_write16_blocking | ( | spi_inst_t * | spi, |
const uint16_t * | src, | ||
size_t | len | ||
) |
Write to an SPI deviceWrite len
halfwords from src
to SPI. Discard any data received back. Blocks until all data is transferred. No timeout, as SPI hardware always transfers at a known data rate.
spi | SPI instance specifier, either spi0 or spi1 |
src | Buffer of data to write |
len | Length of buffers |
int spi_write16_read16_blocking | ( | spi_inst_t * | spi, |
const uint16_t * | src, | ||
uint16_t * | dst, | ||
size_t | len | ||
) |
Write/Read half words to/from an SPI deviceWrite len
halfwords from src
to SPI. Simultaneously read len
halfwords from SPI to dst
. Blocks until all data is transferred. No timeout, as SPI hardware always transfers at a known data rate.
spi | SPI instance specifier, either spi0 or spi1 |
src | Buffer of data to write |
dst | Buffer for read data |
len | Length of BOTH buffers in halfwords |
int spi_write_blocking | ( | spi_inst_t * | spi, |
const uint8_t * | src, | ||
size_t | len | ||
) |
Write to an SPI device, blockingWrite len
bytes from src
to SPI, and discard any data received back Blocks until all data is transferred. No timeout, as SPI hardware always transfers at a known data rate.
spi | SPI instance specifier, either spi0 or spi1 |
src | Buffer of data to write |
len | Length of src |
int spi_write_read_blocking | ( | spi_inst_t * | spi, |
const uint8_t * | src, | ||
uint8_t * | dst, | ||
size_t | len | ||
) |
Write/Read to/from an SPI deviceWrite len
bytes from src
to SPI. Simultaneously read len
bytes from SPI to dst
. Blocks until all data is transferred. No timeout, as SPI hardware always transfers at a known data rate.
spi | SPI instance specifier, either spi0 or spi1 |
src | Buffer of data to write |
dst | Buffer for read data |
len | Length of BOTH buffers |