spi.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef _HARDWARE_SPI_H
8 #define _HARDWARE_SPI_H
9 
10 #include "pico.h"
11 #include "hardware/structs/spi.h"
12 #include "hardware/regs/dreq.h"
13 
14 // PICO_CONFIG: PARAM_ASSERTIONS_ENABLED_SPI, Enable/disable assertions in the SPI module, type=bool, default=0, group=hardware_spi
15 #ifndef PARAM_ASSERTIONS_ENABLED_SPI
16 #define PARAM_ASSERTIONS_ENABLED_SPI 0
17 #endif
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
38 // PICO_CONFIG: PICO_DEFAULT_SPI, Define the default SPI for a board, min=0, max=1, group=hardware_spi
39 // PICO_CONFIG: PICO_DEFAULT_SPI_SCK_PIN, Define the default SPI SCK pin, min=0, max=29, group=hardware_spi
40 // PICO_CONFIG: PICO_DEFAULT_SPI_TX_PIN, Define the default SPI TX pin, min=0, max=29, group=hardware_spi
41 // PICO_CONFIG: PICO_DEFAULT_SPI_RX_PIN, Define the default SPI RX pin, min=0, max=29, group=hardware_spi
42 // PICO_CONFIG: PICO_DEFAULT_SPI_CSN_PIN, Define the default SPI CSN pin, min=0, max=29, group=hardware_spi
43 
47 typedef struct spi_inst spi_inst_t;
48 
55 #define spi0 ((spi_inst_t *)spi0_hw)
56 
63 #define spi1 ((spi_inst_t *)spi1_hw)
64 
65 #if !defined(PICO_DEFAULT_SPI_INSTANCE) && defined(PICO_DEFAULT_SPI)
66 #define PICO_DEFAULT_SPI_INSTANCE (__CONCAT(spi,PICO_DEFAULT_SPI))
67 #endif
68 
69 #ifdef PICO_DEFAULT_SPI_INSTANCE
70 #define spi_default PICO_DEFAULT_SPI_INSTANCE
71 #endif
72 
76 typedef enum {
77  SPI_CPHA_0 = 0,
78  SPI_CPHA_1 = 1
79 } spi_cpha_t;
80 
84 typedef enum {
85  SPI_CPOL_0 = 0,
86  SPI_CPOL_1 = 1
87 } spi_cpol_t;
88 
92 typedef enum {
93  SPI_LSB_FIRST = 0,
94  SPI_MSB_FIRST = 1
95 } spi_order_t;
96 
97 // ----------------------------------------------------------------------------
98 // Setup
99 
112 uint spi_init(spi_inst_t *spi, uint baudrate);
113 
121 void spi_deinit(spi_inst_t *spi);
122 
133 uint spi_set_baudrate(spi_inst_t *spi, uint baudrate);
134 
143 uint spi_get_baudrate(const spi_inst_t *spi);
144 
151 static inline uint spi_get_index(const spi_inst_t *spi) {
152  invalid_params_if(SPI, spi != spi0 && spi != spi1);
153  return spi == spi1 ? 1 : 0;
154 }
155 
156 static inline spi_hw_t *spi_get_hw(spi_inst_t *spi) {
157  spi_get_index(spi); // check it is a hw spi
158  return (spi_hw_t *)spi;
159 }
160 
161 static inline const spi_hw_t *spi_get_const_hw(const spi_inst_t *spi) {
162  spi_get_index(spi); // check it is a hw spi
163  return (const spi_hw_t *)spi;
164 }
165 
177 static inline void spi_set_format(spi_inst_t *spi, uint data_bits, spi_cpol_t cpol, spi_cpha_t cpha, __unused spi_order_t order) {
178  invalid_params_if(SPI, data_bits < 4 || data_bits > 16);
179  // LSB-first not supported on PL022:
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);
183  hw_write_masked(&spi_get_hw(spi)->cr0,
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);
190 }
191 
201 static inline void spi_set_slave(spi_inst_t *spi, bool slave) {
202  if (slave)
203  hw_set_bits(&spi_get_hw(spi)->cr1, SPI_SSPCR1_MS_BITS);
204  else
205  hw_clear_bits(&spi_get_hw(spi)->cr1, SPI_SSPCR1_MS_BITS);
206 }
207 
208 // ----------------------------------------------------------------------------
209 // Generic input/output
210 
217 static inline bool spi_is_writable(const spi_inst_t *spi) {
218  return (spi_get_const_hw(spi)->sr & SPI_SSPSR_TNF_BITS);
219 }
220 
227 static inline bool spi_is_readable(const spi_inst_t *spi) {
228  return (spi_get_const_hw(spi)->sr & SPI_SSPSR_RNE_BITS);
229 }
230 
237 static inline bool spi_is_busy(const spi_inst_t *spi) {
238  return (spi_get_const_hw(spi)->sr & SPI_SSPSR_BSY_BITS);
239 }
240 
253 int spi_write_read_blocking(spi_inst_t *spi, const uint8_t *src, uint8_t *dst, size_t len);
254 
266 int spi_write_blocking(spi_inst_t *spi, const uint8_t *src, size_t len);
267 
283 int spi_read_blocking(spi_inst_t *spi, uint8_t repeated_tx_data, uint8_t *dst, size_t len);
284 
285 // ----------------------------------------------------------------------------
286 // SPI-specific operations and aliases
287 
288 // FIXME need some instance-private data for select() and deselect() if we are going that route
289 
304 int spi_write16_read16_blocking(spi_inst_t *spi, const uint16_t *src, uint16_t *dst, size_t len);
305 
319 int spi_write16_blocking(spi_inst_t *spi, const uint16_t *src, size_t len);
320 
338 int spi_read16_blocking(spi_inst_t *spi, uint16_t repeated_tx_data, uint16_t *dst, size_t len);
339 
346 static inline uint spi_get_dreq(spi_inst_t *spi, bool is_tx) {
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, "");
350  return DREQ_SPI0_TX + spi_get_index(spi) * 2 + !is_tx;
351 }
352 
353 #ifdef __cplusplus
354 }
355 #endif
356 
357 #endif
hw_clear_bits
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
spi_set_slave
static void spi_set_slave(spi_inst_t *spi, bool slave)
Set SPI master/slave.
Definition: spi.h:201
spi_is_busy
static bool spi_is_busy(const spi_inst_t *spi)
Check whether SPI is busy.
Definition: spi.h:237
spi_read_blocking
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
spi_hw_t
Definition: spi.h:23
spi_is_readable
static bool spi_is_readable(const spi_inst_t *spi)
Check whether a read can be done on SPI device.
Definition: spi.h:227
spi_deinit
void spi_deinit(spi_inst_t *spi)
Deinitialise SPI instances.
Definition: spi.c:35
spi_cpha_t
spi_cpha_t
Enumeration of SPI CPHA (clock phase) values.
Definition: spi.h:76
spi_cpol_t
spi_cpol_t
Enumeration of SPI CPOL (clock polarity) values.
Definition: spi.h:84
spi_write_read_blocking
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
hw_write_masked
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
hw_set_bits
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
spi_write16_blocking
int spi_write16_blocking(spi_inst_t *spi, const uint16_t *src, size_t len)
Write to an SPI device.
Definition: spi.c:170
spi_get_baudrate
uint spi_get_baudrate(const spi_inst_t *spi)
Get SPI baudrate.
Definition: spi.c:68
spi_inst_t
struct spi_inst spi_inst_t
Definition: spi.h:47
spi_is_writable
static bool spi_is_writable(const spi_inst_t *spi)
Check whether a write can be done on SPI device.
Definition: spi.h:217
spi_set_baudrate
uint spi_set_baudrate(spi_inst_t *spi, uint baudrate)
Set SPI baudrate.
Definition: spi.c:41
pico.h
spi_get_index
static uint spi_get_index(const spi_inst_t *spi)
Convert SPI instance to hardware instance number.
Definition: spi.h:151
spi_write16_read16_blocking
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
spi_init
uint spi_init(spi_inst_t *spi, uint baudrate)
Initialise SPI instances.
Definition: spi.c:21
spi1
#define spi1
Definition: spi.h:63
spi_set_format
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
spi_write_blocking
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
spi_get_dreq
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
spi_order_t
Enumeration of SPI bit-order values.
Definition: spi.h:92
spi0
#define spi0
Definition: spi.h:55
spi_read16_blocking
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