1 /*
2  * SPDX-FileCopyrightText: 2010-2021 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #pragma once
8 
9 #include <stdint.h>
10 #include <stdbool.h>
11 #include "esp_err.h"
12 #ifndef SPI_MOCK
13 #include "soc/lldesc.h"
14 #include "soc/spi_periph.h"
15 #endif
16 #include "hal/spi_types.h"
17 #include "sdkconfig.h"
18 
19 #ifdef __cplusplus
20 extern "C"
21 {
22 #endif
23 
24 //Maximum amount of bytes that can be put in one DMA descriptor
25 #define SPI_MAX_DMA_LEN (4096-4)
26 
27 /**
28  * Transform unsigned integer of length <= 32 bits to the format which can be
29  * sent by the SPI driver directly.
30  *
31  * E.g. to send 9 bits of data, you can:
32  *
33  *      uint16_t data = SPI_SWAP_DATA_TX(0x145, 9);
34  *
35  * Then points tx_buffer to ``&data``.
36  *
37  * @param DATA Data to be sent, can be uint8_t, uint16_t or uint32_t.
38  * @param LEN Length of data to be sent, since the SPI peripheral sends from
39  *      the MSB, this helps to shift the data to the MSB.
40  */
41 #define SPI_SWAP_DATA_TX(DATA, LEN) __builtin_bswap32((uint32_t)(DATA)<<(32-(LEN)))
42 
43 /**
44  * Transform received data of length <= 32 bits to the format of an unsigned integer.
45  *
46  * E.g. to transform the data of 15 bits placed in a 4-byte array to integer:
47  *
48  *      uint16_t data = SPI_SWAP_DATA_RX(*(uint32_t*)t->rx_data, 15);
49  *
50  * @param DATA Data to be rearranged, can be uint8_t, uint16_t or uint32_t.
51  * @param LEN Length of data received, since the SPI peripheral writes from
52  *      the MSB, this helps to shift the data to the LSB.
53  */
54 #define SPI_SWAP_DATA_RX(DATA, LEN) (__builtin_bswap32(DATA)>>(32-(LEN)))
55 
56 #define SPICOMMON_BUSFLAG_SLAVE         0          ///< Initialize I/O in slave mode
57 #define SPICOMMON_BUSFLAG_MASTER        (1<<0)     ///< Initialize I/O in master mode
58 #define SPICOMMON_BUSFLAG_IOMUX_PINS    (1<<1)     ///< Check using iomux pins. Or indicates the pins are configured through the IO mux rather than GPIO matrix.
59 #define SPICOMMON_BUSFLAG_GPIO_PINS     (1<<2)     ///< Force the signals to be routed through GPIO matrix. Or indicates the pins are routed through the GPIO matrix.
60 #define SPICOMMON_BUSFLAG_SCLK          (1<<3)     ///< Check existing of SCLK pin. Or indicates CLK line initialized.
61 #define SPICOMMON_BUSFLAG_MISO          (1<<4)     ///< Check existing of MISO pin. Or indicates MISO line initialized.
62 #define SPICOMMON_BUSFLAG_MOSI          (1<<5)     ///< Check existing of MOSI pin. Or indicates MOSI line initialized.
63 #define SPICOMMON_BUSFLAG_DUAL          (1<<6)     ///< Check MOSI and MISO pins can output. Or indicates bus able to work under DIO mode.
64 #define SPICOMMON_BUSFLAG_WPHD          (1<<7)     ///< Check existing of WP and HD pins. Or indicates WP & HD pins initialized.
65 #define SPICOMMON_BUSFLAG_QUAD          (SPICOMMON_BUSFLAG_DUAL|SPICOMMON_BUSFLAG_WPHD)     ///< Check existing of MOSI/MISO/WP/HD pins as output. Or indicates bus able to work under QIO mode.
66 #define SPICOMMON_BUSFLAG_IO4_IO7       (1<<8)     ///< Check existing of IO4~IO7 pins. Or indicates IO4~IO7 pins initialized.
67 #define SPICOMMON_BUSFLAG_OCTAL         (SPICOMMON_BUSFLAG_QUAD|SPICOMMON_BUSFLAG_IO4_IO7)  ///< Check existing of MOSI/MISO/WP/HD/SPIIO4/SPIIO5/SPIIO6/SPIIO7 pins as output. Or indicates bus able to work under octal mode.
68 #define SPICOMMON_BUSFLAG_NATIVE_PINS   SPICOMMON_BUSFLAG_IOMUX_PINS
69 
70 /**
71  * @brief SPI DMA channels
72  */
73 typedef enum {
74   SPI_DMA_DISABLED = 0,     ///< Do not enable DMA for SPI
75 #if CONFIG_IDF_TARGET_ESP32
76   SPI_DMA_CH1      = 1,     ///< Enable DMA, select DMA Channel 1
77   SPI_DMA_CH2      = 2,     ///< Enable DMA, select DMA Channel 2
78 #endif
79   SPI_DMA_CH_AUTO  = 3,     ///< Enable DMA, channel is automatically selected by driver
80 } spi_common_dma_t;
81 
82 #if __cplusplus
83 /* Needed for C++ backwards compatibility with earlier ESP-IDF where this argument is a bare 'int'. Can be removed in ESP-IDF 5 */
84 typedef int spi_dma_chan_t;
85 #else
86 typedef spi_common_dma_t spi_dma_chan_t;
87 #endif
88 
89 /**
90  * @brief This is a configuration structure for a SPI bus.
91  *
92  * You can use this structure to specify the GPIO pins of the bus. Normally, the driver will use the
93  * GPIO matrix to route the signals. An exception is made when all signals either can be routed through
94  * the IO_MUX or are -1. In that case, the IO_MUX is used, allowing for >40MHz speeds.
95  *
96  * @note Be advised that the slave driver does not use the quadwp/quadhd lines and fields in spi_bus_config_t refering to these lines will be ignored and can thus safely be left uninitialized.
97  */
98 typedef struct {
99     union {
100       int mosi_io_num;    ///< GPIO pin for Master Out Slave In (=spi_d) signal, or -1 if not used.
101       int data0_io_num;   ///< GPIO pin for spi data0 signal in quad/octal mode, or -1 if not used.
102     };
103     union {
104       int miso_io_num;    ///< GPIO pin for Master In Slave Out (=spi_q) signal, or -1 if not used.
105       int data1_io_num;   ///< GPIO pin for spi data1 signal in quad/octal mode, or -1 if not used.
106     };
107     int sclk_io_num;      ///< GPIO pin for SPI Clock signal, or -1 if not used.
108     union {
109       int quadwp_io_num;  ///< GPIO pin for WP (Write Protect) signal, or -1 if not used.
110       int data2_io_num;   ///< GPIO pin for spi data2 signal in quad/octal mode, or -1 if not used.
111     };
112     union {
113       int quadhd_io_num;  ///< GPIO pin for HD (Hold) signal, or -1 if not used.
114       int data3_io_num;   ///< GPIO pin for spi data3 signal in quad/octal mode, or -1 if not used.
115     };
116     int data4_io_num;     ///< GPIO pin for spi data4 signal in octal mode, or -1 if not used.
117     int data5_io_num;     ///< GPIO pin for spi data5 signal in octal mode, or -1 if not used.
118     int data6_io_num;     ///< GPIO pin for spi data6 signal in octal mode, or -1 if not used.
119     int data7_io_num;     ///< GPIO pin for spi data7 signal in octal mode, or -1 if not used.
120     int max_transfer_sz;  ///< Maximum transfer size, in bytes. Defaults to 4092 if 0 when DMA enabled, or to `SOC_SPI_MAXIMUM_BUFFER_SIZE` if DMA is disabled.
121     uint32_t flags;       ///< Abilities of bus to be checked by the driver. Or-ed value of ``SPICOMMON_BUSFLAG_*`` flags.
122     int intr_flags;       /**< Interrupt flag for the bus to set the priority, and IRAM attribute, see
123                            *  ``esp_intr_alloc.h``. Note that the EDGE, INTRDISABLED attribute are ignored
124                            *  by the driver. Note that if ESP_INTR_FLAG_IRAM is set, ALL the callbacks of
125                            *  the driver, and their callee functions, should be put in the IRAM.
126                            */
127 } spi_bus_config_t;
128 
129 
130 /**
131  * @brief Initialize a SPI bus
132  *
133  * @warning SPI0/1 is not supported
134  *
135  * @param host_id       SPI peripheral that controls this bus
136  * @param bus_config    Pointer to a spi_bus_config_t struct specifying how the host should be initialized
137  * @param dma_chan      - Selecting a DMA channel for an SPI bus allows transactions on the bus with size only limited by the amount of internal memory.
138  *                      - Selecting SPI_DMA_DISABLED limits the size of transactions.
139  *                      - Set to SPI_DMA_DISABLED if only the SPI flash uses this bus.
140  *                      - Set to SPI_DMA_CH_AUTO to let the driver to allocate the DMA channel.
141  *
142  * @warning If a DMA channel is selected, any transmit and receive buffer used should be allocated in
143  *          DMA-capable memory.
144  *
145  * @warning The ISR of SPI is always executed on the core which calls this
146  *          function. Never starve the ISR on this core or the SPI transactions will not
147  *          be handled.
148  *
149  * @return
150  *         - ESP_ERR_INVALID_ARG   if configuration is invalid
151  *         - ESP_ERR_INVALID_STATE if host already is in use
152  *         - ESP_ERR_NOT_FOUND     if there is no available DMA channel
153  *         - ESP_ERR_NO_MEM        if out of memory
154  *         - ESP_OK                on success
155  */
156 esp_err_t spi_bus_initialize(spi_host_device_t host_id, const spi_bus_config_t *bus_config, spi_dma_chan_t dma_chan);
157 
158 /**
159  * @brief Free a SPI bus
160  *
161  * @warning In order for this to succeed, all devices have to be removed first.
162  *
163  * @param host_id SPI peripheral to free
164  * @return
165  *         - ESP_ERR_INVALID_ARG   if parameter is invalid
166  *         - ESP_ERR_INVALID_STATE if not all devices on the bus are freed
167  *         - ESP_OK                on success
168  */
169 esp_err_t spi_bus_free(spi_host_device_t host_id);
170 
171 #ifdef __cplusplus
172 }
173 #endif
174