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_HARDWARE_SPI, Enable/disable assertions in the hardware_spi module, type=bool, default=0, group=hardware_spi
15 #ifndef PARAM_ASSERTIONS_ENABLED_HARDWARE_SPI
16 #ifdef PARAM_ASSERTIONS_ENABLED_SPI // backwards compatibility with SDK < 2.0.0
17 #define PARAM_ASSERTIONS_ENABLED_HARDWARE_SPI PARAM_ASSERTIONS_ENABLED_SPI
18 #else
19 #define PARAM_ASSERTIONS_ENABLED_HARDWARE_SPI 0
20 #endif
21 #endif
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 /** \file hardware/spi.h
28  *  \defgroup hardware_spi hardware_spi
29  *
30  * \brief Hardware SPI API
31  *
32  * RP-series microcontrollers have 2 identical instances of the Serial Peripheral Interface (SPI) controller.
33  *
34  * The PrimeCell SSP is a master or slave interface for synchronous serial communication with peripheral devices that have
35  * Motorola SPI, National Semiconductor Microwire, or Texas Instruments synchronous serial interfaces.
36  *
37  * Controller can be defined as master or slave using the \ref spi_set_slave function.
38  *
39  * Each controller can be connected to a number of GPIO pins, see the datasheet GPIO function selection table for more information.
40  */
41 
42 // PICO_CONFIG: PICO_DEFAULT_SPI, Define the default SPI for a board, min=0, max=1, default=Usually provided via board header, group=hardware_spi
43 // PICO_CONFIG: PICO_DEFAULT_SPI_SCK_PIN, Define the default SPI SCK pin, min=0, max=47 on RP2350B, 29 otherwise, default=Usually provided via board header, group=hardware_spi
44 // PICO_CONFIG: PICO_DEFAULT_SPI_TX_PIN, Define the default SPI TX pin, min=0, max=47 on RP2350B, 29 otherwise, default=Usually provided via board header, group=hardware_spi
45 // PICO_CONFIG: PICO_DEFAULT_SPI_RX_PIN, Define the default SPI RX pin, min=0, max=47 on RP2350B, 29 otherwise, default=Usually provided via board header, group=hardware_spi
46 // PICO_CONFIG: PICO_DEFAULT_SPI_CSN_PIN, Define the default SPI CSN pin, min=0, max=47 on RP2350B, 29 otherwise, default=Usually provided via board header, group=hardware_spi
47 
48 /**
49  * \brief Opaque type representing an SPI instance.
50  */
51 typedef struct spi_inst spi_inst_t;
52 
53 /** Identifier for the first (SPI 0) hardware SPI instance (for use in SPI functions).
54  *
55  * e.g. spi_init(spi0, 48000)
56  *
57  *  \ingroup hardware_spi
58  */
59 #define spi0 ((spi_inst_t *)spi0_hw)
60 
61 /** Identifier for the second (SPI 1) hardware SPI instance (for use in SPI functions).
62  *
63  * e.g. spi_init(spi1, 48000)
64  *
65  *  \ingroup hardware_spi
66  */
67 #define spi1 ((spi_inst_t *)spi1_hw)
68 
69 /**
70  * \def PICO_DEFAULT_SPI_INSTANCE()
71  * \ingroup hardware_spi
72  * \hideinitializer
73  * \brief Returns the default SPI instance
74  */
75 #if !defined(PICO_DEFAULT_SPI_INSTANCE) && defined(PICO_DEFAULT_SPI)
76 #define PICO_DEFAULT_SPI_INSTANCE() (__CONCAT(spi,PICO_DEFAULT_SPI))
77 #endif
78 
79 /**
80  * \def PICO_DEFAULT_SPI
81  * \ingroup hardware_spi
82  * \hideinitializer
83  * \brief The default SPI instance number
84  */
85 
86 /**
87  * \def PICO_DEFAULT_SPI_INSTANCE()
88  * \ingroup hardware_spi
89  * \hideinitializer
90  * \brief Returns the default SPI instance
91  */
92 #ifdef PICO_DEFAULT_SPI_INSTANCE
93 #define spi_default PICO_DEFAULT_SPI_INSTANCE()
94 #endif
95 
96 /**
97  * \def SPI_NUM(spi)
98  * \ingroup hardware_spi
99  * \hideinitializer
100  * \brief Returns the SPI number for a SPI instance
101  *
102  * Note this macro is intended to resolve at compile time, and does no parameter checking
103  */
104 #ifndef SPI_NUM
105 static_assert(NUM_SPIS == 2, "");
106 #define SPI_NUM(spi) ((spi) == spi1)
107 #endif
108 
109 /**
110  * \def SPI_INSTANCE(spi_num)
111  * \ingroup hardware_spi
112  * \hideinitializer
113  * \brief Returns the SPI instance with the given SPI number
114  *
115  * Note this macro is intended to resolve at compile time, and does no parameter checking
116  */
117 #ifndef SPI_INSTANCE
118 static_assert(NUM_SPIS == 2, "");
119 #define SPI_INSTANCE(num) ((num) ? spi1 : spi0)
120 #endif
121 
122 /**
123  * \def SPI_DREQ_NUM(spi, is_tx)
124  * \ingroup hardware_spi
125  * \hideinitializer
126  * \brief Returns the \ref dreq_num_t used for pacing DMA transfers to or from this SPI instance.
127  * If is_tx is true, then it is for transfers to the SPI else for transfers from the SPI.
128  *
129  * Note this macro is intended to resolve at compile time, and does no parameter checking
130  */
131 #ifndef SPI_DREQ_NUM
132 static_assert(DREQ_SPI0_RX == DREQ_SPI0_TX + 1, "");
133 static_assert(DREQ_SPI1_RX == DREQ_SPI1_TX + 1, "");
134 static_assert(DREQ_SPI1_TX == DREQ_SPI0_TX + 2, "");
135 #define SPI_DREQ_NUM(spi, is_tx) (DREQ_SPI0_TX + SPI_NUM(spi) * 2 + !(is_tx))
136 #endif
137 
138 /** \brief Enumeration of SPI CPHA (clock phase) values.
139  *  \ingroup hardware_spi
140  */
141 typedef enum {
142     SPI_CPHA_0 = 0,
143     SPI_CPHA_1 = 1
144 } spi_cpha_t;
145 
146 /** \brief Enumeration of SPI CPOL (clock polarity) values.
147  *  \ingroup hardware_spi
148  */
149 typedef enum {
150     SPI_CPOL_0 = 0,
151     SPI_CPOL_1 = 1
152 } spi_cpol_t;
153 
154 /** \brief Enumeration of SPI bit-order values.
155  *  \ingroup hardware_spi
156  */
157 typedef enum {
158     SPI_LSB_FIRST = 0,
159     SPI_MSB_FIRST = 1
160 } spi_order_t;
161 
162 // ----------------------------------------------------------------------------
163 // Setup
164 
165 /*! \brief Initialise SPI instances
166  *  \ingroup hardware_spi
167  *
168  * Puts the SPI into a known state, and enable it. Must be called before other
169  * functions.
170  *
171  * \note There is no guarantee that the baudrate requested can be achieved exactly; the nearest will be chosen
172  * and returned
173  *
174  * \param spi SPI instance specifier, either \ref spi0 or \ref spi1
175  * \param baudrate Baudrate requested in Hz
176  * \return the actual baud rate set
177  */
178 uint spi_init(spi_inst_t *spi, uint baudrate);
179 
180 /*! \brief Deinitialise SPI instances
181  *  \ingroup hardware_spi
182  *
183  * Puts the SPI into a disabled state. Init will need to be called to re-enable the device
184  * functions.
185  *
186  * \param spi SPI instance specifier, either \ref spi0 or \ref spi1
187  */
188 void spi_deinit(spi_inst_t *spi);
189 
190 /*! \brief Set SPI baudrate
191  *  \ingroup hardware_spi
192  *
193  * Set SPI frequency as close as possible to baudrate, and return the actual
194  * achieved rate.
195  *
196  * \param spi SPI instance specifier, either \ref spi0 or \ref spi1
197  * \param baudrate Baudrate required in Hz, should be capable of a bitrate of at least 2Mbps, or higher, depending on system clock settings.
198  * \return The actual baudrate set
199  */
200 uint spi_set_baudrate(spi_inst_t *spi, uint baudrate);
201 
202 /*! \brief Get SPI baudrate
203  *  \ingroup hardware_spi
204  *
205  * Get SPI baudrate which was set by \see spi_set_baudrate
206  *
207  * \param spi SPI instance specifier, either \ref spi0 or \ref spi1
208  * \return The actual baudrate set
209  */
210 uint spi_get_baudrate(const spi_inst_t *spi);
211 
212 /*! \brief Convert SPI instance to hardware instance number
213  *  \ingroup hardware_spi
214  *
215  * \param spi SPI instance
216  * \return Number of SPI, 0 or 1.
217  */
spi_get_index(const spi_inst_t * spi)218 static inline uint spi_get_index(const spi_inst_t *spi) {
219     invalid_params_if(HARDWARE_SPI, spi != spi0 && spi != spi1);
220     return SPI_NUM(spi);
221 }
222 
spi_get_hw(spi_inst_t * spi)223 static inline spi_hw_t *spi_get_hw(spi_inst_t *spi) {
224     spi_get_index(spi); // check it is a hw spi
225     return (spi_hw_t *)spi;
226 }
227 
spi_get_const_hw(const spi_inst_t * spi)228 static inline const spi_hw_t *spi_get_const_hw(const spi_inst_t *spi) {
229     spi_get_index(spi);  // check it is a hw spi
230     return (const spi_hw_t *)spi;
231 }
232 
233 /*! \brief Configure SPI
234  *  \ingroup hardware_spi
235  *
236  * Configure how the SPI serialises and deserialises data on the wire
237  *
238  * \param spi SPI instance specifier, either \ref spi0 or \ref spi1
239  * \param data_bits Number of data bits per transfer. Valid values 4..16.
240  * \param cpol SSPCLKOUT polarity, applicable to Motorola SPI frame format only.
241  * \param cpha SSPCLKOUT phase, applicable to Motorola SPI frame format only
242  * \param order Must be SPI_MSB_FIRST, no other values supported on the PL022
243  */
spi_set_format(spi_inst_t * spi,uint data_bits,spi_cpol_t cpol,spi_cpha_t cpha,__unused spi_order_t order)244 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) {
245     invalid_params_if(HARDWARE_SPI, data_bits < 4 || data_bits > 16);
246     // LSB-first not supported on PL022:
247     invalid_params_if(HARDWARE_SPI, order != SPI_MSB_FIRST);
248     invalid_params_if(HARDWARE_SPI, cpol != SPI_CPOL_0 && cpol != SPI_CPOL_1);
249     invalid_params_if(HARDWARE_SPI, cpha != SPI_CPHA_0 && cpha != SPI_CPHA_1);
250 
251     // Disable the SPI
252     uint32_t enable_mask = spi_get_hw(spi)->cr1 & SPI_SSPCR1_SSE_BITS;
253     hw_clear_bits(&spi_get_hw(spi)->cr1, SPI_SSPCR1_SSE_BITS);
254 
255     hw_write_masked(&spi_get_hw(spi)->cr0,
256                     ((uint)(data_bits - 1)) << SPI_SSPCR0_DSS_LSB |
257                     ((uint)cpol) << SPI_SSPCR0_SPO_LSB |
258                     ((uint)cpha) << SPI_SSPCR0_SPH_LSB,
259         SPI_SSPCR0_DSS_BITS |
260         SPI_SSPCR0_SPO_BITS |
261         SPI_SSPCR0_SPH_BITS);
262 
263     // Re-enable the SPI
264     hw_set_bits(&spi_get_hw(spi)->cr1, enable_mask);
265 }
266 
267 /*! \brief Set SPI master/slave
268  *  \ingroup hardware_spi
269  *
270  * Configure the SPI for master- or slave-mode operation. By default,
271  * spi_init() sets master-mode.
272  *
273  * \param spi SPI instance specifier, either \ref spi0 or \ref spi1
274  * \param slave true to set SPI device as a slave device, false for master.
275  */
spi_set_slave(spi_inst_t * spi,bool slave)276 static inline void spi_set_slave(spi_inst_t *spi, bool slave) {
277     // Disable the SPI
278     uint32_t enable_mask = spi_get_hw(spi)->cr1 & SPI_SSPCR1_SSE_BITS;
279     hw_clear_bits(&spi_get_hw(spi)->cr1, SPI_SSPCR1_SSE_BITS);
280 
281     if (slave)
282         hw_set_bits(&spi_get_hw(spi)->cr1, SPI_SSPCR1_MS_BITS);
283     else
284         hw_clear_bits(&spi_get_hw(spi)->cr1, SPI_SSPCR1_MS_BITS);
285 
286     // Re-enable the SPI
287     hw_set_bits(&spi_get_hw(spi)->cr1, enable_mask);
288 }
289 
290 // ----------------------------------------------------------------------------
291 // Generic input/output
292 
293 /*! \brief Check whether a write can be done on SPI device
294  *  \ingroup hardware_spi
295  *
296  * \param spi SPI instance specifier, either \ref spi0 or \ref spi1
297  * \return false if no space is available to write. True if a write is possible
298  */
spi_is_writable(const spi_inst_t * spi)299 static inline bool spi_is_writable(const spi_inst_t *spi) {
300     return (spi_get_const_hw(spi)->sr & SPI_SSPSR_TNF_BITS);
301 }
302 
303 /*! \brief Check whether a read can be done on SPI device
304  *  \ingroup hardware_spi
305  *
306  * \param spi SPI instance specifier, either \ref spi0 or \ref spi1
307  * \return true if a read is possible i.e. data is present
308  */
spi_is_readable(const spi_inst_t * spi)309 static inline bool spi_is_readable(const spi_inst_t *spi) {
310     return (spi_get_const_hw(spi)->sr & SPI_SSPSR_RNE_BITS);
311 }
312 
313 /*! \brief Check whether SPI is busy
314  *  \ingroup hardware_spi
315  *
316  * \param spi SPI instance specifier, either \ref spi0 or \ref spi1
317  * \return true if SPI is busy
318  */
spi_is_busy(const spi_inst_t * spi)319 static inline bool spi_is_busy(const spi_inst_t *spi) {
320     return (spi_get_const_hw(spi)->sr & SPI_SSPSR_BSY_BITS);
321 }
322 
323 /*! \brief Write/Read to/from an SPI device
324  *  \ingroup hardware_spi
325  *
326  * Write \p len bytes from \p src to SPI. Simultaneously read \p len bytes from SPI to \p dst.
327  * Blocks until all data is transferred. No timeout, as SPI hardware always transfers at a known data rate.
328  *
329  * \param spi SPI instance specifier, either \ref spi0 or \ref spi1
330  * \param src Buffer of data to write
331  * \param dst Buffer for read data
332  * \param len Length of BOTH buffers
333  * \return Number of bytes written/read
334 */
335 int spi_write_read_blocking(spi_inst_t *spi, const uint8_t *src, uint8_t *dst, size_t len);
336 
337 /*! \brief Write to an SPI device, blocking
338  *  \ingroup hardware_spi
339  *
340  * Write \p len bytes from \p src to SPI, and discard any data received back
341  * Blocks until all data is transferred. No timeout, as SPI hardware always transfers at a known data rate.
342  *
343  * \param spi SPI instance specifier, either \ref spi0 or \ref spi1
344  * \param src Buffer of data to write
345  * \param len Length of \p src
346  * \return Number of bytes written/read
347  */
348 int spi_write_blocking(spi_inst_t *spi, const uint8_t *src, size_t len);
349 
350 /*! \brief Read from an SPI device
351  *  \ingroup hardware_spi
352  *
353  * Read \p len bytes from SPI to \p dst.
354  * Blocks until all data is transferred. No timeout, as SPI hardware always transfers at a known data rate.
355  * \p repeated_tx_data is output repeatedly on TX as data is read in from RX.
356  * Generally this can be 0, but some devices require a specific value here,
357  * e.g. SD cards expect 0xff
358  *
359  * \param spi SPI instance specifier, either \ref spi0 or \ref spi1
360  * \param repeated_tx_data Buffer of data to write
361  * \param dst Buffer for read data
362  * \param len Length of buffer \p dst
363  * \return Number of bytes written/read
364  */
365 int spi_read_blocking(spi_inst_t *spi, uint8_t repeated_tx_data, uint8_t *dst, size_t len);
366 
367 // ----------------------------------------------------------------------------
368 // SPI-specific operations and aliases
369 
370 // FIXME need some instance-private data for select() and deselect() if we are going that route
371 
372 /*! \brief Write/Read half words to/from an SPI device
373  *  \ingroup hardware_spi
374  *
375  * Write \p len halfwords from \p src to SPI. Simultaneously read \p len halfwords from SPI to \p dst.
376  * Blocks until all data is transferred. No timeout, as SPI hardware always transfers at a known data rate.
377  *
378  * \note SPI should be initialised with 16 data_bits using \ref spi_set_format first, otherwise this function will only read/write 8 data_bits.
379  *
380  * \param spi SPI instance specifier, either \ref spi0 or \ref spi1
381  * \param src Buffer of data to write
382  * \param dst Buffer for read data
383  * \param len Length of BOTH buffers in halfwords
384  * \return Number of halfwords written/read
385 */
386 int spi_write16_read16_blocking(spi_inst_t *spi, const uint16_t *src, uint16_t *dst, size_t len);
387 
388 /*! \brief Write to an SPI device
389  *  \ingroup hardware_spi
390  *
391  * Write \p len halfwords from \p src to SPI. Discard any data received back.
392  * Blocks until all data is transferred. No timeout, as SPI hardware always transfers at a known data rate.
393  *
394  * \note SPI should be initialised with 16 data_bits using \ref spi_set_format first, otherwise this function will only write 8 data_bits.
395  *
396  * \param spi SPI instance specifier, either \ref spi0 or \ref spi1
397  * \param src Buffer of data to write
398  * \param len Length of buffers
399  * \return Number of halfwords written/read
400 */
401 int spi_write16_blocking(spi_inst_t *spi, const uint16_t *src, size_t len);
402 
403 /*! \brief Read from an SPI device
404  *  \ingroup hardware_spi
405  *
406  * Read \p len halfwords from SPI to \p dst.
407  * Blocks until all data is transferred. No timeout, as SPI hardware always transfers at a known data rate.
408  * \p repeated_tx_data is output repeatedly on TX as data is read in from RX.
409  * Generally this can be 0, but some devices require a specific value here,
410  * e.g. SD cards expect 0xff
411  *
412  * \note SPI should be initialised with 16 data_bits using \ref spi_set_format first, otherwise this function will only read 8 data_bits.
413  *
414  * \param spi SPI instance specifier, either \ref spi0 or \ref spi1
415  * \param repeated_tx_data Buffer of data to write
416  * \param dst Buffer for read data
417  * \param len Length of buffer \p dst in halfwords
418  * \return Number of halfwords written/read
419  */
420 int spi_read16_blocking(spi_inst_t *spi, uint16_t repeated_tx_data, uint16_t *dst, size_t len);
421 
422 /*! \brief Return the DREQ to use for pacing transfers to/from a particular SPI instance
423  *  \ingroup hardware_spi
424  *
425  * \param spi SPI instance specifier, either \ref spi0 or \ref spi1
426  * \param is_tx true for sending data to the SPI instance, false for receiving data from the SPI instance
427  */
spi_get_dreq(spi_inst_t * spi,bool is_tx)428 static inline uint spi_get_dreq(spi_inst_t *spi, bool is_tx) {
429     return SPI_DREQ_NUM(spi, is_tx);
430 }
431 
432 #ifdef __cplusplus
433 }
434 #endif
435 
436 #endif
437