1 // Copyright 2010-2017 Espressif Systems (Shanghai) PTE LTD
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
16 #ifndef _DRIVER_SPI_COMMON_H_
17 #define _DRIVER_SPI_COMMON_H_
22 #include "soc/spi_struct.h"
23 #include "rom/lldesc.h"
32 //Maximum amount of bytes that can be put in one DMA descriptor
33 #define SPI_MAX_DMA_LEN (4096-4)
37 * @brief Enum with the three SPI peripherals that are software-accessible in it
40 SPI_HOST=0, ///< SPI1, SPI
41 HSPI_HOST=1, ///< SPI2, HSPI
42 VSPI_HOST=2 ///< SPI3, VSPI
46 * @brief This is a configuration structure for a SPI bus.
48 * You can use this structure to specify the GPIO pins of the bus. Normally, the driver will use the
49 * GPIO matrix to route the signals. An exception is made when all signals either can be routed through
50 * the IO_MUX or are -1. In that case, the IO_MUX is used, allowing for >40MHz speeds.
52 * @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.
55 int mosi_io_num; ///< GPIO pin for Master Out Slave In (=spi_d) signal, or -1 if not used.
56 int miso_io_num; ///< GPIO pin for Master In Slave Out (=spi_q) signal, or -1 if not used.
57 int sclk_io_num; ///< GPIO pin for Spi CLocK signal, or -1 if not used.
58 int quadwp_io_num; ///< GPIO pin for WP (Write Protect) signal which is used as D2 in 4-bit communication modes, or -1 if not used.
59 int quadhd_io_num; ///< GPIO pin for HD (HolD) signal which is used as D3 in 4-bit communication modes, or -1 if not used.
60 int max_transfer_sz; ///< Maximum transfer size, in bytes. Defaults to 4094 if 0.
65 * @brief Try to claim a SPI peripheral
67 * Call this if your driver wants to manage a SPI peripheral.
69 * @param host Peripheral to claim
70 * @return True if peripheral is claimed successfully; false if peripheral already is claimed.
72 bool spicommon_periph_claim(spi_host_device_t host);
75 * @brief Return the SPI peripheral so another driver can claim it.
77 * @param host Peripheral to claim
78 * @return True if peripheral is returned successfully; false if peripheral was free to claim already.
80 bool spicommon_periph_free(spi_host_device_t host);
83 #define SPICOMMON_BUSFLAG_SLAVE 0 ///< Initialize I/O in slave mode
84 #define SPICOMMON_BUSFLAG_MASTER (1<<0) ///< Initialize I/O in master mode
85 #define SPICOMMON_BUSFLAG_QUAD (1<<1) ///< Also initialize WP/HD pins, if specified
88 * @brief Connect a SPI peripheral to GPIO pins
90 * This routine is used to connect a SPI peripheral to the IO-pads and DMA channel given in
91 * the arguments. Depending on the IO-pads requested, the routing is done either using the
92 * IO_mux or using the GPIO matrix.
94 * @param host SPI peripheral to be routed
95 * @param bus_config Pointer to a spi_bus_config struct detailing the GPIO pins
96 * @param dma_chan DMA-channel (1 or 2) to use, or 0 for no DMA.
97 * @param flags Combination of SPICOMMON_BUSFLAG_* flags
98 * @param[out] is_native A value of 'true' will be written to this address if the GPIOs can be
99 * routed using the IO_mux, 'false' if the GPIO matrix is used.
101 * - ESP_ERR_INVALID_ARG if parameter is invalid
102 * - ESP_OK on success
104 esp_err_t spicommon_bus_initialize_io(spi_host_device_t host, const spi_bus_config_t *bus_config, int dma_chan, int flags, bool *is_native);
107 * @brief Free the IO used by a SPI peripheral
109 * @param host SPI peripheral to be freed
111 * - ESP_ERR_INVALID_ARG if parameter is invalid
112 * - ESP_OK on success
115 esp_err_t spicommon_bus_free_io(spi_host_device_t host);
118 * @brief Initialize a Chip Select pin for a specific SPI peripheral
121 * @param host SPI peripheral
122 * @param cs_io_num GPIO pin to route
123 * @param cs_num CS id to route
124 * @param force_gpio_matrix If true, CS will always be routed through the GPIO matrix. If false,
125 * if the GPIO number allows it, the routing will happen through the IO_mux.
128 void spicommon_cs_initialize(spi_host_device_t host, int cs_io_num, int cs_num, int force_gpio_matrix);
131 * @brief Free a chip select line
133 * @param host SPI peripheral
134 * @param cs_num CS id to free
136 void spicommon_cs_free(spi_host_device_t host, int cs_num);
140 * @brief Setup a DMA link chain
142 * This routine will set up a chain of linked DMA descriptors in the array pointed to by
143 * ``dmadesc``. Enough DMA descriptors will be used to fit the buffer of ``len`` bytes in, and the
144 * descriptors will point to the corresponding positions in ``buffer`` and linked together. The
145 * end result is that feeding ``dmadesc[0]`` into DMA hardware results in the entirety ``len`` bytes
146 * of ``data`` being read or written.
148 * @param dmadesc Pointer to array of DMA descriptors big enough to be able to convey ``len`` bytes
149 * @param len Length of buffer
150 * @param data Data buffer to use for DMA transfer
151 * @param isrx True if data is to be written into ``data``, false if it's to be read from ``data``.
153 void spicommon_setup_dma_desc_links(lldesc_t *dmadesc, int len, const uint8_t *data, bool isrx);
156 * @brief Get the position of the hardware registers for a specific SPI host
158 * @param host The SPI host
160 * @return A register descriptor stuct pointer, pointed at the hardware registers
162 spi_dev_t *spicommon_hw_for_host(spi_host_device_t host);
165 * @brief Get the IRQ source for a specific SPI host
167 * @param host The SPI host
169 * @return The hosts IRQ source
171 int spicommon_irqsource_for_host(spi_host_device_t host);
177 * Callback, to be called when a DMA engine reset is completed
179 typedef void(*dmaworkaround_cb_t)(void *arg);
183 * @brief Request a reset for a certain DMA channel
185 * @note In some (well-defined) cases in the ESP32 (at least rev v.0 and v.1), a SPI DMA channel will get confused. This can be remedied
186 * by resetting the SPI DMA hardware in case this happens. Unfortunately, the reset knob used for thsi will reset _both_ DMA channels, and
187 * as such can only done safely when both DMA channels are idle. These functions coordinate this.
189 * Essentially, when a reset is needed, a driver can request this using spicommon_dmaworkaround_req_reset. This is supposed to be called
190 * with an user-supplied function as an argument. If both DMA channels are idle, this call will reset the DMA subsystem and return true.
191 * If the other DMA channel is still busy, it will return false; as soon as the other DMA channel is done, however, it will reset the
192 * DMA subsystem and call the callback. The callback is then supposed to be used to continue the SPI drivers activity.
194 * @param dmachan DMA channel associated with the SPI host that needs a reset
195 * @param cb Callback to call in case DMA channel cannot be reset immediately
196 * @param arg Argument to the callback
198 * @return True when a DMA reset could be executed immediately. False when it could not; in this
199 * case the callback will be called with the specified argument when the logic can execute
200 * a reset, after that reset.
202 bool spicommon_dmaworkaround_req_reset(int dmachan, dmaworkaround_cb_t cb, void *arg);
206 * @brief Check if a DMA reset is requested but has not completed yet
208 * @return True when a DMA reset is requested but hasn't completed yet. False otherwise.
210 bool spicommon_dmaworkaround_reset_in_progress();
214 * @brief Mark a DMA channel as idle.
216 * A call to this function tells the workaround logic that this channel will
217 * not be affected by a global SPI DMA reset.
219 void spicommon_dmaworkaround_idle(int dmachan);
222 * @brief Mark a DMA channel as active.
224 * A call to this function tells the workaround logic that this channel will
225 * be affected by a global SPI DMA reset, and a reset like that should not be attempted.
227 void spicommon_dmaworkaround_transfer_active(int dmachan);