v1.5.0
Go to the documentation of this file.
7 #ifndef _HARDWARE_SPI_H
8 #define _HARDWARE_SPI_H
11 #include "hardware/structs/spi.h"
12 #include "hardware/regs/dreq.h"
15 #ifndef PARAM_ASSERTIONS_ENABLED_SPI
16 #define PARAM_ASSERTIONS_ENABLED_SPI 0
55 #define spi0 ((spi_inst_t *)spi0_hw)
63 #define spi1 ((spi_inst_t *)spi1_hw)
65 #if !defined(PICO_DEFAULT_SPI_INSTANCE) && defined(PICO_DEFAULT_SPI)
66 #define PICO_DEFAULT_SPI_INSTANCE (__CONCAT(spi,PICO_DEFAULT_SPI))
69 #ifdef PICO_DEFAULT_SPI_INSTANCE
70 #define spi_default PICO_DEFAULT_SPI_INSTANCE
152 invalid_params_if(SPI, spi !=
spi0 && spi !=
spi1);
153 return spi ==
spi1 ? 1 : 0;
178 invalid_params_if(SPI, data_bits < 4 || data_bits > 16);
180 invalid_params_if(SPI, order != SPI_MSB_FIRST);
181 invalid_params_if(SPI, cpol != SPI_CPOL_0 && cpol != SPI_CPOL_1);
182 invalid_params_if(SPI, cpha != SPI_CPHA_0 && cpha != SPI_CPHA_1);
184 ((uint)(data_bits - 1)) << SPI_SSPCR0_DSS_LSB |
185 ((uint)cpol) << SPI_SSPCR0_SPO_LSB |
186 ((uint)cpha) << SPI_SSPCR0_SPH_LSB,
187 SPI_SSPCR0_DSS_BITS |
188 SPI_SSPCR0_SPO_BITS |
189 SPI_SSPCR0_SPH_BITS);
203 hw_set_bits(&spi_get_hw(spi)->cr1, SPI_SSPCR1_MS_BITS);
218 return (spi_get_const_hw(spi)->sr & SPI_SSPSR_TNF_BITS);
228 return (spi_get_const_hw(spi)->sr & SPI_SSPSR_RNE_BITS);
238 return (spi_get_const_hw(spi)->sr & SPI_SSPSR_BSY_BITS);
347 static_assert(DREQ_SPI0_RX == DREQ_SPI0_TX + 1,
"");
348 static_assert(DREQ_SPI1_RX == DREQ_SPI1_TX + 1,
"");
349 static_assert(DREQ_SPI1_TX == DREQ_SPI0_TX + 2,
"");
static __force_inline void hw_clear_bits(io_rw_32 *addr, uint32_t mask)
Atomically clear the specified bits to 0 in a HW register.
Definition: address_mapped.h:131
static void spi_set_slave(spi_inst_t *spi, bool slave)
Set SPI master/slave.
Definition: spi.h:201
static bool spi_is_busy(const spi_inst_t *spi)
Check whether SPI is busy.
Definition: spi.h:237
int spi_read_blocking(spi_inst_t *spi, uint8_t repeated_tx_data, uint8_t *dst, size_t len)
Read from an SPI device.
Definition: spi.c:128
static bool spi_is_readable(const spi_inst_t *spi)
Check whether a read can be done on SPI device.
Definition: spi.h:227
void spi_deinit(spi_inst_t *spi)
Deinitialise SPI instances.
Definition: spi.c:35
spi_cpha_t
Enumeration of SPI CPHA (clock phase) values.
Definition: spi.h:76
spi_cpol_t
Enumeration of SPI CPOL (clock polarity) values.
Definition: spi.h:84
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 device.
Definition: spi.c:76
static __force_inline void hw_write_masked(io_rw_32 *addr, uint32_t values, uint32_t write_mask)
Set new values for a sub-set of the bits in a HW register.
Definition: address_mapped.h:157
static __force_inline void hw_set_bits(io_rw_32 *addr, uint32_t mask)
Atomically set the specified bits to 1 in a HW register.
Definition: address_mapped.h:121
int spi_write16_blocking(spi_inst_t *spi, const uint16_t *src, size_t len)
Write to an SPI device.
Definition: spi.c:170
uint spi_get_baudrate(const spi_inst_t *spi)
Get SPI baudrate.
Definition: spi.c:68
struct spi_inst spi_inst_t
Definition: spi.h:47
static bool spi_is_writable(const spi_inst_t *spi)
Check whether a write can be done on SPI device.
Definition: spi.h:217
uint spi_set_baudrate(spi_inst_t *spi, uint baudrate)
Set SPI baudrate.
Definition: spi.c:41
static uint spi_get_index(const spi_inst_t *spi)
Convert SPI instance to hardware instance number.
Definition: spi.h:151
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 device.
Definition: spi.c:148
uint spi_init(spi_inst_t *spi, uint baudrate)
Initialise SPI instances.
Definition: spi.c:21
#define spi1
Definition: spi.h:63
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 SPI.
Definition: spi.h:177
int spi_write_blocking(spi_inst_t *spi, const uint8_t *src, size_t len)
Write to an SPI device, blocking.
Definition: spi.c:99
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.
Definition: spi.h:346
spi_order_t
Enumeration of SPI bit-order values.
Definition: spi.h:92
#define spi0
Definition: spi.h:55
int spi_read16_blocking(spi_inst_t *spi, uint16_t repeated_tx_data, uint16_t *dst, size_t len)
Read from an SPI device.
Definition: spi.c:195