Wed, 01 May 2019 21:27:28 +0200
Version 0.2.10. Fixed spelling error in websocket component. Updated esp-idf to v4.0-dev-459-gba1ff1692 and adjusted several sources for changed headers. Finalized keeping the AP running at all times. Increased websocket server stack depth from 6000 to 7000.
0 | 1 | // Copyright 2010-2016 Espressif Systems (Shanghai) PTE LTD |
2 | // | |
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 | |
6 | ||
7 | // http://www.apache.org/licenses/LICENSE-2.0 | |
8 | // | |
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. | |
14 | ||
15 | ||
16 | #ifndef _DRIVER_SPI_MASTER_LOBO_H_ | |
17 | #define _DRIVER_SPI_MASTER_LOBO_H_ | |
18 | ||
19 | #include "esp_err.h" | |
20 | #include "freertos/FreeRTOS.h" | |
21 | #include "freertos/semphr.h" | |
22 | #include "soc/spi_struct.h" | |
23 | ||
24 | #include "esp_intr_alloc.h" | |
34
5c92103c5e72
Version 0.2.10. Fixed spelling error in websocket component. Updated esp-idf to v4.0-dev-459-gba1ff1692 and adjusted several sources for changed headers. Finalized keeping the AP running at all times. Increased websocket server stack depth from 6000 to 7000.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
25 | #include "esp32/rom/lldesc.h" |
0 | 26 | |
27 | ||
28 | #ifdef __cplusplus | |
29 | extern "C" | |
30 | { | |
31 | #endif | |
32 | ||
33 | ||
34 | //Maximum amount of bytes that can be put in one DMA descriptor | |
35 | #define SPI_MAX_DMA_LEN (4096-4) | |
36 | ||
37 | /** | |
38 | * @brief Enum with the three SPI peripherals that are software-accessible in it | |
39 | */ | |
40 | typedef enum { | |
41 | TFT_SPI_HOST=0, ///< SPI1, SPI; Cannot be used in this driver! | |
42 | TFT_HSPI_HOST=1, ///< SPI2, HSPI | |
43 | TFT_VSPI_HOST=2 ///< SPI3, VSPI | |
44 | } spi_lobo_host_device_t; | |
45 | ||
46 | ||
47 | /** | |
48 | * @brief This is a configuration structure for a SPI bus. | |
49 | * | |
50 | * You can use this structure to specify the GPIO pins of the bus. Normally, the driver will use the | |
51 | * GPIO matrix to route the signals. An exception is made when all signals either can be routed through | |
52 | * the IO_MUX or are -1. In that case, the IO_MUX is used, allowing for >40MHz speeds. | |
53 | */ | |
54 | typedef struct { | |
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. | |
61 | } spi_lobo_bus_config_t; | |
62 | ||
63 | ||
64 | #define LB_SPI_DEVICE_TXBIT_LSBFIRST (1<<0) ///< Transmit command/address/data LSB first instead of the default MSB first | |
65 | #define LB_SPI_DEVICE_RXBIT_LSBFIRST (1<<1) ///< Receive data LSB first instead of the default MSB first | |
66 | #define LB_SPI_DEVICE_BIT_LSBFIRST (SPI_TXBIT_LSBFIRST|SPI_RXBIT_LSBFIRST); ///< Transmit and receive LSB first | |
67 | #define LB_SPI_DEVICE_3WIRE (1<<2) ///< Use spiq for both sending and receiving data | |
68 | #define LB_SPI_DEVICE_POSITIVE_CS (1<<3) ///< Make CS positive during a transaction instead of negative | |
69 | #define LB_SPI_DEVICE_HALFDUPLEX (1<<4) ///< Transmit data before receiving it, instead of simultaneously | |
70 | #define LB_SPI_DEVICE_CLK_AS_CS (1<<5) ///< Output clock on CS line if CS is active | |
71 | ||
72 | #define SPI_ERR_OTHER_CONFIG 7001 | |
73 | ||
74 | typedef struct spi_lobo_transaction_t spi_lobo_transaction_t; | |
75 | typedef void(*spi_lobo_transaction_cb_t)(spi_lobo_transaction_t *trans); | |
76 | ||
77 | /** | |
78 | * @brief This is a configuration for a SPI slave device that is connected to one of the SPI buses. | |
79 | */ | |
80 | typedef struct { | |
81 | uint8_t command_bits; ///< Amount of bits in command phase (0-16) | |
82 | uint8_t address_bits; ///< Amount of bits in address phase (0-64) | |
83 | uint8_t dummy_bits; ///< Amount of dummy bits to insert between address and data phase | |
84 | uint8_t mode; ///< SPI mode (0-3) | |
85 | uint8_t duty_cycle_pos; ///< Duty cycle of positive clock, in 1/256th increments (128 = 50%/50% duty). Setting this to 0 (=not setting it) is equivalent to setting this to 128. | |
86 | uint8_t cs_ena_pretrans; ///< Amount of SPI bit-cycles the cs should be activated before the transmission (0-16). This only works on half-duplex transactions. | |
87 | uint8_t cs_ena_posttrans; ///< Amount of SPI bit-cycles the cs should stay active after the transmission (0-16) | |
88 | int clock_speed_hz; ///< Clock speed, in Hz | |
89 | int spics_io_num; ///< CS GPIO pin for this device, handled by hardware; set to -1 if not used | |
90 | int spics_ext_io_num; ///< CS GPIO pin for this device, handled by software (spi_lobo_device_select/spi_lobo_device_deselect); only used if spics_io_num=-1 | |
91 | uint32_t flags; ///< Bitwise OR of LB_SPI_DEVICE_* flags | |
92 | spi_lobo_transaction_cb_t pre_cb; ///< Callback to be called before a transmission is started. This callback from 'spi_lobo_transfer_data' function. | |
93 | spi_lobo_transaction_cb_t post_cb; ///< Callback to be called after a transmission has completed. This callback from 'spi_lobo_transfer_data' function. | |
94 | uint8_t selected; ///< **INTERNAL** 1 if the device's CS pin is active | |
95 | } spi_lobo_device_interface_config_t; | |
96 | ||
97 | ||
98 | #define LB_SPI_TRANS_MODE_DIO (1<<0) ///< Transmit/receive data in 2-bit mode | |
99 | #define LB_SPI_TRANS_MODE_QIO (1<<1) ///< Transmit/receive data in 4-bit mode | |
100 | #define LB_SPI_TRANS_MODE_DIOQIO_ADDR (1<<2) ///< Also transmit address in mode selected by SPI_MODE_DIO/SPI_MODE_QIO | |
101 | #define LB_SPI_TRANS_USE_RXDATA (1<<3) ///< Receive into rx_data member of spi_lobo_transaction_t instead into memory at rx_buffer. | |
102 | #define LB_SPI_TRANS_USE_TXDATA (1<<4) ///< Transmit tx_data member of spi_lobo_transaction_t instead of data at tx_buffer. Do not set tx_buffer when using this. | |
103 | ||
104 | /** | |
105 | * This structure describes one SPI transmission | |
106 | */ | |
107 | struct spi_lobo_transaction_t { | |
108 | uint32_t flags; ///< Bitwise OR of LB_SPI_TRANS_* flags | |
109 | uint16_t command; ///< Command data. Specific length was given when device was added to the bus. | |
110 | uint64_t address; ///< Address. Specific length was given when device was added to the bus. | |
111 | size_t length; ///< Total data length to be transmitted to the device, in bits; if 0, no data is transmitted | |
112 | size_t rxlength; ///< Total data length to be received from the device, in bits; if 0, no data is received | |
113 | void *user; ///< User-defined variable. Can be used to store eg transaction ID or data to be used by pre_cb and/or post_cb callbacks. | |
114 | union { | |
115 | const void *tx_buffer; ///< Pointer to transmit buffer, or NULL for no MOSI phase | |
116 | uint8_t tx_data[4]; ///< If SPI_USE_TXDATA is set, data set here is sent directly from this variable. | |
117 | }; | |
118 | union { | |
119 | void *rx_buffer; ///< Pointer to receive buffer, or NULL for no MISO phase | |
120 | uint8_t rx_data[4]; ///< If SPI_USE_RXDATA is set, data is received directly to this variable | |
121 | }; | |
122 | }; | |
123 | ||
124 | #define NO_CS 3 // Number of CS pins per SPI host | |
125 | #define NO_DEV 6 // Number of spi devices per SPI host; more than 3 devices can be attached to the same bus if using software CS's | |
126 | #define SPI_SEMAPHORE_WAIT 2000 // Time in ms to wait for SPI mutex | |
127 | ||
128 | typedef struct spi_lobo_device_t spi_lobo_device_t; | |
129 | ||
130 | typedef struct { | |
131 | spi_lobo_device_t *device[NO_DEV]; | |
132 | intr_handle_t intr; | |
133 | spi_dev_t *hw; | |
134 | //spi_lobo_transaction_t *cur_trans; | |
135 | int cur_device; | |
136 | lldesc_t *dmadesc_tx; | |
137 | lldesc_t *dmadesc_rx; | |
138 | bool no_gpio_matrix; | |
139 | int dma_chan; | |
140 | int max_transfer_sz; | |
141 | QueueHandle_t spi_lobo_bus_mutex; | |
142 | spi_lobo_bus_config_t cur_bus_config; | |
143 | } spi_lobo_host_t; | |
144 | ||
145 | struct spi_lobo_device_t { | |
146 | spi_lobo_device_interface_config_t cfg; | |
147 | spi_lobo_host_t *host; | |
148 | spi_lobo_bus_config_t bus_config; | |
149 | spi_lobo_host_device_t host_dev; | |
150 | }; | |
151 | ||
152 | typedef spi_lobo_device_t* spi_lobo_device_handle_t; ///< Handle for a device on a SPI bus | |
153 | typedef spi_lobo_host_t* spi_lobo_host_handle_t; | |
154 | typedef spi_lobo_device_interface_config_t* spi_lobo_device_interface_config_handle_t; | |
155 | ||
156 | ||
157 | /** | |
158 | * @brief Add a device. This allocates a CS line for the device, allocates memory for the device structure and hooks | |
159 | * up the CS pin to whatever is specified. | |
160 | * | |
161 | * This initializes the internal structures for a device, plus allocates a CS pin on the indicated SPI master | |
162 | * peripheral and routes it to the indicated GPIO. All SPI master devices have three hw CS pins and can thus control | |
163 | * up to three devices. Software handled CS pin can also be used for additional devices on the same SPI bus. | |
164 | * | |
165 | * ### If selected SPI host device bus is not yet initialized, it is initialized first with 'bus_config' function ### | |
166 | * | |
167 | * @note While in general, speeds up to 80MHz on the dedicated SPI pins and 40MHz on GPIO-matrix-routed pins are | |
168 | * supported, full-duplex transfers routed over the GPIO matrix only support speeds up to 26MHz. | |
169 | * | |
170 | * @param host SPI peripheral to allocate device on (HSPI or VSPI) | |
171 | * @param dev_config SPI interface protocol config for the device | |
172 | * @param bus_config Pointer to a spi_lobo_bus_config_t struct specifying how the host device bus should be initialized | |
173 | * @param handle Pointer to variable to hold the device handle | |
174 | * @return | |
175 | * - ESP_ERR_INVALID_ARG if parameter is invalid | |
176 | * - ESP_ERR_NOT_FOUND if host doesn't have any free CS slots | |
177 | * - ESP_ERR_NO_MEM if out of memory | |
178 | * - ESP_OK on success | |
179 | */ | |
180 | esp_err_t spi_lobo_bus_add_device(spi_lobo_host_device_t host, spi_lobo_bus_config_t *bus_config, spi_lobo_device_interface_config_t *dev_config, spi_lobo_device_handle_t *handle); | |
181 | ||
182 | /** | |
183 | * @brief Remove a device from the SPI bus. If after removal no other device is attached to the spi bus device, it is freed. | |
184 | * | |
185 | * @param handle Device handle to free | |
186 | * @return | |
187 | * - ESP_ERR_INVALID_ARG if parameter is invalid | |
188 | * - ESP_ERR_INVALID_STATE if device already is freed | |
189 | * - ESP_OK on success | |
190 | */ | |
191 | esp_err_t spi_lobo_bus_remove_device(spi_lobo_device_handle_t handle); | |
192 | ||
193 | ||
194 | /** | |
195 | * @brief Return the actuall SPI bus speed for the spi device in Hz | |
196 | * | |
197 | * Some frequencies cannot be set, for example 30000000 will actually set SPI clock to 26666666 Hz | |
198 | * | |
199 | * @param handle Device handle obtained using spi_lobo_bus_add_device | |
200 | * | |
201 | * @return | |
202 | * - actuall SPI clock | |
203 | */ | |
204 | uint32_t spi_lobo_get_speed(spi_lobo_device_handle_t handle); | |
205 | ||
206 | /** | |
207 | * @brief Set the new clock speed for the device, return the actuall SPI bus speed set, in Hz | |
208 | * This function can be used after the device is initialized | |
209 | * | |
210 | * Some frequencies cannot be set, for example 30000000 will actually set SPI clock to 26666666 Hz | |
211 | * | |
212 | * @param handle Device handle obtained using spi_lobo_bus_add_device | |
213 | * @param speed New device spi clock to be set in Hz | |
214 | * | |
215 | * @return | |
216 | * - actuall SPI clock | |
217 | * - 0 if speed cannot be set | |
218 | */ | |
219 | uint32_t spi_lobo_set_speed(spi_lobo_device_handle_t handle, uint32_t speed); | |
220 | ||
221 | /** | |
222 | * @brief Select spi device for transmission | |
223 | * | |
224 | * It configures spi bus with selected spi device parameters if previously selected device was different than the current | |
225 | * If device's spics_io_num=-1 and spics_ext_io_num > 0 'spics_ext_io_num' pin is set to active state (low) | |
226 | * | |
227 | * spi bus device's semaphore is taken before selecting the device | |
228 | * | |
229 | * @param handle Device handle obtained using spi_lobo_bus_add_device | |
230 | * @param force configure spi bus even if the previous device was the same | |
231 | * | |
232 | * @return | |
233 | * - ESP_ERR_INVALID_ARG if parameter is invalid | |
234 | * - ESP_OK on success | |
235 | */ | |
236 | esp_err_t spi_lobo_device_select(spi_lobo_device_handle_t handle, int force); | |
237 | ||
238 | /** | |
239 | * @brief De-select spi device | |
240 | * | |
241 | * If device's spics_io_num=-1 and spics_ext_io_num > 0 'spics_ext_io_num' pin is set to inactive state (high) | |
242 | * | |
243 | * spi bus device's semaphore is given after selecting the device | |
244 | * | |
245 | * @param handle Device handle obtained using spi_lobo_bus_add_device | |
246 | * | |
247 | * @return | |
248 | * - ESP_ERR_INVALID_ARG if parameter is invalid | |
249 | * - ESP_OK on success | |
250 | */ | |
251 | esp_err_t spi_lobo_device_deselect(spi_lobo_device_handle_t handle); | |
252 | ||
253 | ||
254 | /** | |
255 | * @brief Check if spi bus uses native spi pins | |
256 | * | |
257 | * @param handle Device handle obtained using spi_lobo_bus_add_device | |
258 | * | |
259 | * @return | |
260 | * - true if native spi pins are used | |
261 | * - false if spi pins are routed through gpio matrix | |
262 | */ | |
263 | bool spi_lobo_uses_native_pins(spi_lobo_device_handle_t handle); | |
264 | ||
265 | /** | |
266 | * @brief Get spi bus native spi pins | |
267 | * | |
268 | * @param handle Device handle obtained using spi_lobo_bus_add_device | |
269 | * | |
270 | * @return | |
271 | * places spi bus native pins in provided pointers | |
272 | */ | |
273 | void spi_lobo_get_native_pins(int host, int *sdi, int *sdo, int *sck); | |
274 | ||
275 | /** | |
276 | * @brief Transimit and receive data to/from spi device based on transaction data | |
277 | * | |
278 | * TRANSMIT 8-bit data to spi device from 'trans->tx_buffer' or 'trans->tx_data' (trans->lenght/8 bytes) | |
279 | * and RECEIVE data to 'trans->rx_buffer' or 'trans->rx_data' (trans->rx_length/8 bytes) | |
280 | * Lengths must be 8-bit multiples! | |
281 | * If trans->rx_buffer is NULL or trans->rx_length is 0, only transmits data | |
282 | * If trans->tx_buffer is NULL or trans->length is 0, only receives data | |
283 | * If the device is in duplex mode (LB_SPI_DEVICE_HALFDUPLEX flag NOT set), data are transmitted and received simultaneously. | |
284 | * If the device is in half duplex mode (LB_SPI_DEVICE_HALFDUPLEX flag IS set), data are received after transmission | |
285 | * 'address', 'command' and 'dummy bits' are transmitted before data phase IF set in device's configuration | |
286 | * and IF 'trans->length' and 'trans->rx_length' are NOT both 0 | |
287 | * If device was not previously selected, it will be selected before transmission and deselected after transmission. | |
288 | * | |
289 | * @param handle Device handle obtained using spi_lobo_bus_add_device | |
290 | * | |
291 | * @param trans Pointer to variable containing the description of the transaction that is executed | |
292 | * | |
293 | * @return | |
294 | * - ESP_ERR_INVALID_ARG if parameter is invalid | |
295 | * - ESP error code if device cannot be selected | |
296 | * - ESP_OK on success | |
297 | * | |
298 | */ | |
299 | esp_err_t spi_lobo_transfer_data(spi_lobo_device_handle_t handle, spi_lobo_transaction_t *trans); | |
300 | ||
301 | ||
302 | /* | |
303 | * SPI transactions uses the semaphore (taken in select function) to protect the transfer | |
304 | */ | |
305 | esp_err_t spi_lobo_device_TakeSemaphore(spi_lobo_device_handle_t handle); | |
306 | void spi_lobo_device_GiveSemaphore(spi_lobo_device_handle_t handle); | |
307 | ||
308 | ||
309 | /** | |
310 | * @brief Setup a DMA link chain | |
311 | * | |
312 | * This routine will set up a chain of linked DMA descriptors in the array pointed to by | |
313 | * ``dmadesc``. Enough DMA descriptors will be used to fit the buffer of ``len`` bytes in, and the | |
314 | * descriptors will point to the corresponding positions in ``buffer`` and linked together. The | |
315 | * end result is that feeding ``dmadesc[0]`` into DMA hardware results in the entirety ``len`` bytes | |
316 | * of ``data`` being read or written. | |
317 | * | |
318 | * @param dmadesc Pointer to array of DMA descriptors big enough to be able to convey ``len`` bytes | |
319 | * @param len Length of buffer | |
320 | * @param data Data buffer to use for DMA transfer | |
321 | * @param isrx True if data is to be written into ``data``, false if it's to be read from ``data``. | |
322 | */ | |
323 | void spi_lobo_setup_dma_desc_links(lldesc_t *dmadesc, int len, const uint8_t *data, bool isrx); | |
324 | ||
325 | /** | |
326 | * @brief Check if a DMA reset is requested but has not completed yet | |
327 | * | |
328 | * @return True when a DMA reset is requested but hasn't completed yet. False otherwise. | |
329 | */ | |
330 | bool spi_lobo_dmaworkaround_reset_in_progress(); | |
331 | ||
332 | ||
333 | /** | |
334 | * @brief Mark a DMA channel as idle. | |
335 | * | |
336 | * A call to this function tells the workaround logic that this channel will | |
337 | * not be affected by a global SPI DMA reset. | |
338 | */ | |
339 | void spi_lobo_dmaworkaround_idle(int dmachan); | |
340 | ||
341 | /** | |
342 | * @brief Mark a DMA channel as active. | |
343 | * | |
344 | * A call to this function tells the workaround logic that this channel will | |
345 | * be affected by a global SPI DMA reset, and a reset like that should not be attempted. | |
346 | */ | |
347 | void spi_lobo_dmaworkaround_transfer_active(int dmachan); | |
348 | ||
349 | ||
350 | #ifdef __cplusplus | |
351 | } | |
352 | #endif | |
353 | ||
354 | #endif |