1 // Copyright 2015-2018 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 #include <string.h>
16 #include "esp_types.h"
17 #include "esp_attr.h"
18 #include "esp_intr_alloc.h"
19 #include "esp_log.h"
20 #include "esp_err.h"
21 #include "esp_heap_caps.h"
22 #include "esp_rom_gpio.h"
23 #include "esp_rom_sys.h"
24 #include "soc/lldesc.h"
25 #include "soc/soc_caps.h"
26 #include "soc/spi_periph.h"
27 #include "soc/soc_memory_layout.h"
28 #include "hal/spi_ll.h"
29 #include "hal/spi_slave_hal.h"
30 #include "esp_osal/esp_osal.h"
31 #include "esp_osal/semphr.h"
32 #include "esp_osal/task.h"
33 #include "sdkconfig.h"
34
35 #include "driver/gpio.h"
36 #include "driver/spi_common_internal.h"
37 #include "driver/spi_slave.h"
38 #include "hal/spi_slave_hal.h"
39
40 static const char *SPI_TAG = "spi_slave";
41 #define SPI_CHECK(a, str, ret_val) \
42 if (!(a)) { \
43 ESP_LOGE(SPI_TAG,"%s(%d): %s", __FUNCTION__, __LINE__, str); \
44 return (ret_val); \
45 }
46
47 #ifdef CONFIG_SPI_SLAVE_ISR_IN_IRAM
48 #define SPI_SLAVE_ISR_ATTR IRAM_ATTR
49 #else
50 #define SPI_SLAVE_ISR_ATTR
51 #endif
52
53 #ifdef CONFIG_SPI_SLAVE_IN_IRAM
54 #define SPI_SLAVE_ATTR IRAM_ATTR
55 #else
56 #define SPI_SLAVE_ATTR
57 #endif
58
59 typedef struct {
60 int id;
61 spi_slave_interface_config_t cfg;
62 intr_handle_t intr;
63 spi_slave_hal_context_t hal;
64 spi_slave_transaction_t *cur_trans;
65 uint32_t flags;
66 int max_transfer_sz;
67 QueueHandle_t trans_queue;
68 QueueHandle_t ret_queue;
69 bool dma_enabled;
70 uint32_t tx_dma_chan;
71 uint32_t rx_dma_chan;
72 #ifdef CONFIG_PM_ENABLE
73 esp_pm_lock_handle_t pm_lock;
74 #endif
75 } spi_slave_t;
76
77 static spi_slave_t *spihost[SOC_SPI_PERIPH_NUM];
78
79 static void IRAM_ATTR spi_intr(void *arg);
80
is_valid_host(spi_host_device_t host)81 static inline bool is_valid_host(spi_host_device_t host)
82 {
83 //SPI1 can be used as GPSPI only on ESP32
84 #if CONFIG_IDF_TARGET_ESP32
85 return host >= SPI1_HOST && host <= SPI3_HOST;
86 #elif (SOC_SPI_PERIPH_NUM == 2)
87 return host == SPI2_HOST;
88 #elif (SOC_SPI_PERIPH_NUM == 3)
89 return host >= SPI2_HOST && host <= SPI3_HOST;
90 #endif
91 }
92
bus_is_iomux(spi_slave_t * host)93 static inline bool bus_is_iomux(spi_slave_t *host)
94 {
95 return host->flags&SPICOMMON_BUSFLAG_IOMUX_PINS;
96 }
97
freeze_cs(spi_slave_t * host)98 static void freeze_cs(spi_slave_t *host)
99 {
100 esp_rom_gpio_connect_in_signal(GPIO_MATRIX_CONST_ONE_INPUT, spi_periph_signal[host->id].spics_in, false);
101 }
102
103 // Use this function instead of cs_initial to avoid overwrite the output config
104 // This is used in test by internal gpio matrix connections
restore_cs(spi_slave_t * host)105 static inline void restore_cs(spi_slave_t *host)
106 {
107 if (bus_is_iomux(host)) {
108 gpio_iomux_in(host->cfg.spics_io_num, spi_periph_signal[host->id].spics_in);
109 } else {
110 esp_rom_gpio_connect_in_signal(host->cfg.spics_io_num, spi_periph_signal[host->id].spics_in, false);
111 }
112 }
113
spi_slave_initialize(spi_host_device_t host,const spi_bus_config_t * bus_config,const spi_slave_interface_config_t * slave_config,spi_dma_chan_t dma_chan)114 esp_err_t spi_slave_initialize(spi_host_device_t host, const spi_bus_config_t *bus_config, const spi_slave_interface_config_t *slave_config, spi_dma_chan_t dma_chan)
115 {
116 bool spi_chan_claimed;
117 uint32_t actual_tx_dma_chan = 0;
118 uint32_t actual_rx_dma_chan = 0;
119 esp_err_t ret = ESP_OK;
120 esp_err_t err;
121 SPI_CHECK(is_valid_host(host), "invalid host", ESP_ERR_INVALID_ARG);
122 #ifdef CONFIG_IDF_TARGET_ESP32
123 SPI_CHECK(dma_chan >= SPI_DMA_DISABLED && dma_chan <= SPI_DMA_CH_AUTO, "invalid dma channel", ESP_ERR_INVALID_ARG );
124 #elif CONFIG_IDF_TARGET_ESP32S2
125 SPI_CHECK( dma_chan == SPI_DMA_DISABLED || dma_chan == (int)host || dma_chan == SPI_DMA_CH_AUTO, "invalid dma channel", ESP_ERR_INVALID_ARG );
126 #elif SOC_GDMA_SUPPORTED
127 SPI_CHECK( dma_chan == SPI_DMA_DISABLED || dma_chan == SPI_DMA_CH_AUTO, "invalid dma channel, chip only support spi dma channel auto-alloc", ESP_ERR_INVALID_ARG );
128 #endif
129 SPI_CHECK((bus_config->intr_flags & (ESP_INTR_FLAG_HIGH|ESP_INTR_FLAG_EDGE|ESP_INTR_FLAG_INTRDISABLED))==0, "intr flag not allowed", ESP_ERR_INVALID_ARG);
130 #ifndef CONFIG_SPI_SLAVE_ISR_IN_IRAM
131 SPI_CHECK((bus_config->intr_flags & ESP_INTR_FLAG_IRAM)==0, "ESP_INTR_FLAG_IRAM should be disabled when CONFIG_SPI_SLAVE_ISR_IN_IRAM is not set.", ESP_ERR_INVALID_ARG);
132 #endif
133 SPI_CHECK(slave_config->spics_io_num < 0 || GPIO_IS_VALID_GPIO(slave_config->spics_io_num), "spics pin invalid", ESP_ERR_INVALID_ARG);
134
135 spi_chan_claimed=spicommon_periph_claim(host, "spi slave");
136 SPI_CHECK(spi_chan_claimed, "host already in use", ESP_ERR_INVALID_STATE);
137
138 spihost[host] = malloc(sizeof(spi_slave_t));
139 if (spihost[host] == NULL) {
140 ret = ESP_ERR_NO_MEM;
141 goto cleanup;
142 }
143 memset(spihost[host], 0, sizeof(spi_slave_t));
144 memcpy(&spihost[host]->cfg, slave_config, sizeof(spi_slave_interface_config_t));
145 spihost[host]->id = host;
146
147 bool use_dma = (dma_chan != SPI_DMA_DISABLED);
148 spihost[host]->dma_enabled = use_dma;
149 if (use_dma) {
150 ret = spicommon_slave_dma_chan_alloc(host, dma_chan, &actual_tx_dma_chan, &actual_rx_dma_chan);
151 if (ret != ESP_OK) {
152 goto cleanup;
153 }
154 }
155
156 err = spicommon_bus_initialize_io(host, bus_config, SPICOMMON_BUSFLAG_SLAVE|bus_config->flags, &spihost[host]->flags);
157 if (err!=ESP_OK) {
158 ret = err;
159 goto cleanup;
160 }
161 if (slave_config->spics_io_num >= 0) {
162 spicommon_cs_initialize(host, slave_config->spics_io_num, 0, !bus_is_iomux(spihost[host]));
163 }
164
165 // The slave DMA suffers from unexpected transactions. Forbid reading if DMA is enabled by disabling the CS line.
166 if (use_dma) freeze_cs(spihost[host]);
167
168 int dma_desc_ct = 0;
169 spihost[host]->tx_dma_chan = actual_tx_dma_chan;
170 spihost[host]->rx_dma_chan = actual_rx_dma_chan;
171 if (use_dma) {
172 //See how many dma descriptors we need and allocate them
173 dma_desc_ct = (bus_config->max_transfer_sz + SPI_MAX_DMA_LEN - 1) / SPI_MAX_DMA_LEN;
174 if (dma_desc_ct == 0) dma_desc_ct = 1; //default to 4k when max is not given
175 spihost[host]->max_transfer_sz = dma_desc_ct * SPI_MAX_DMA_LEN;
176 } else {
177 //We're limited to non-DMA transfers: the SPI work registers can hold 64 bytes at most.
178 spihost[host]->max_transfer_sz = SOC_SPI_MAXIMUM_BUFFER_SIZE;
179 }
180 #ifdef CONFIG_PM_ENABLE
181 err = esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, "spi_slave",
182 &spihost[host]->pm_lock);
183 if (err != ESP_OK) {
184 ret = err;
185 goto cleanup;
186 }
187 // Lock APB frequency while SPI slave driver is in use
188 esp_pm_lock_acquire(spihost[host]->pm_lock);
189 #endif //CONFIG_PM_ENABLE
190
191 //Create queues
192 spihost[host]->trans_queue = xQueueCreate(slave_config->queue_size, sizeof(spi_slave_transaction_t *));
193 spihost[host]->ret_queue = xQueueCreate(slave_config->queue_size, sizeof(spi_slave_transaction_t *));
194 if (!spihost[host]->trans_queue || !spihost[host]->ret_queue) {
195 ret = ESP_ERR_NO_MEM;
196 goto cleanup;
197 }
198
199 int flags = bus_config->intr_flags | ESP_INTR_FLAG_INTRDISABLED;
200 err = esp_intr_alloc(spicommon_irqsource_for_host(host), flags, spi_intr, (void *)spihost[host], &spihost[host]->intr);
201 if (err != ESP_OK) {
202 ret = err;
203 goto cleanup;
204 }
205
206 spi_slave_hal_context_t *hal = &spihost[host]->hal;
207 //assign the SPI, RX DMA and TX DMA peripheral registers beginning address
208 spi_slave_hal_config_t hal_config = {
209 .host_id = host,
210 .dma_in = SPI_LL_GET_HW(host),
211 .dma_out = SPI_LL_GET_HW(host)
212 };
213 spi_slave_hal_init(hal, &hal_config);
214
215 if (dma_desc_ct) {
216 hal->dmadesc_tx = heap_caps_malloc(sizeof(lldesc_t) * dma_desc_ct, MALLOC_CAP_DMA);
217 hal->dmadesc_rx = heap_caps_malloc(sizeof(lldesc_t) * dma_desc_ct, MALLOC_CAP_DMA);
218 if (!hal->dmadesc_tx || !hal->dmadesc_rx) {
219 ret = ESP_ERR_NO_MEM;
220 goto cleanup;
221 }
222 }
223 hal->dmadesc_n = dma_desc_ct;
224 hal->rx_lsbfirst = (slave_config->flags & SPI_SLAVE_RXBIT_LSBFIRST) ? 1 : 0;
225 hal->tx_lsbfirst = (slave_config->flags & SPI_SLAVE_TXBIT_LSBFIRST) ? 1 : 0;
226 hal->mode = slave_config->mode;
227 hal->use_dma = use_dma;
228 hal->tx_dma_chan = actual_tx_dma_chan;
229 hal->rx_dma_chan = actual_rx_dma_chan;
230
231 spi_slave_hal_setup_device(hal);
232
233 return ESP_OK;
234
235 cleanup:
236 if (spihost[host]) {
237 if (spihost[host]->trans_queue) vQueueDelete(spihost[host]->trans_queue);
238 if (spihost[host]->ret_queue) vQueueDelete(spihost[host]->ret_queue);
239 free(spihost[host]->hal.dmadesc_tx);
240 free(spihost[host]->hal.dmadesc_rx);
241 #ifdef CONFIG_PM_ENABLE
242 if (spihost[host]->pm_lock) {
243 esp_pm_lock_release(spihost[host]->pm_lock);
244 esp_pm_lock_delete(spihost[host]->pm_lock);
245 }
246 #endif
247 }
248 spi_slave_hal_deinit(&spihost[host]->hal);
249 if (spihost[host]->dma_enabled) {
250 spicommon_slave_free_dma(host);
251 }
252
253 free(spihost[host]);
254 spihost[host] = NULL;
255 spicommon_periph_free(host);
256
257 return ret;
258 }
259
spi_slave_free(spi_host_device_t host)260 esp_err_t spi_slave_free(spi_host_device_t host)
261 {
262 SPI_CHECK(is_valid_host(host), "invalid host", ESP_ERR_INVALID_ARG);
263 SPI_CHECK(spihost[host], "host not slave", ESP_ERR_INVALID_ARG);
264 if (spihost[host]->trans_queue) vQueueDelete(spihost[host]->trans_queue);
265 if (spihost[host]->ret_queue) vQueueDelete(spihost[host]->ret_queue);
266 if (spihost[host]->dma_enabled) {
267 spicommon_slave_free_dma(host);
268 }
269 free(spihost[host]->hal.dmadesc_tx);
270 free(spihost[host]->hal.dmadesc_rx);
271 esp_intr_free(spihost[host]->intr);
272 #ifdef CONFIG_PM_ENABLE
273 esp_pm_lock_release(spihost[host]->pm_lock);
274 esp_pm_lock_delete(spihost[host]->pm_lock);
275 #endif //CONFIG_PM_ENABLE
276 free(spihost[host]);
277 spihost[host] = NULL;
278 spicommon_periph_free(host);
279 return ESP_OK;
280 }
281
282
spi_slave_queue_trans(spi_host_device_t host,const spi_slave_transaction_t * trans_desc,TickType_t ticks_to_wait)283 esp_err_t SPI_SLAVE_ATTR spi_slave_queue_trans(spi_host_device_t host, const spi_slave_transaction_t *trans_desc, TickType_t ticks_to_wait)
284 {
285 BaseType_t r;
286 SPI_CHECK(is_valid_host(host), "invalid host", ESP_ERR_INVALID_ARG);
287 SPI_CHECK(spihost[host], "host not slave", ESP_ERR_INVALID_ARG);
288 SPI_CHECK(spihost[host]->dma_enabled == 0 || trans_desc->tx_buffer==NULL || esp_ptr_dma_capable(trans_desc->tx_buffer),
289 "txdata not in DMA-capable memory", ESP_ERR_INVALID_ARG);
290 SPI_CHECK(spihost[host]->dma_enabled == 0 || trans_desc->rx_buffer==NULL ||
291 (esp_ptr_dma_capable(trans_desc->rx_buffer) && esp_ptr_word_aligned(trans_desc->rx_buffer) &&
292 (trans_desc->length%4==0)),
293 "rxdata not in DMA-capable memory or not WORD aligned", ESP_ERR_INVALID_ARG);
294
295 SPI_CHECK(trans_desc->length <= spihost[host]->max_transfer_sz * 8, "data transfer > host maximum", ESP_ERR_INVALID_ARG);
296 r = xQueueSend(spihost[host]->trans_queue, (void *)&trans_desc, ticks_to_wait);
297 if (!r) return ESP_ERR_TIMEOUT;
298 esp_intr_enable(spihost[host]->intr);
299 return ESP_OK;
300 }
301
302
spi_slave_get_trans_result(spi_host_device_t host,spi_slave_transaction_t ** trans_desc,TickType_t ticks_to_wait)303 esp_err_t SPI_SLAVE_ATTR spi_slave_get_trans_result(spi_host_device_t host, spi_slave_transaction_t **trans_desc, TickType_t ticks_to_wait)
304 {
305 BaseType_t r;
306 SPI_CHECK(is_valid_host(host), "invalid host", ESP_ERR_INVALID_ARG);
307 SPI_CHECK(spihost[host], "host not slave", ESP_ERR_INVALID_ARG);
308 r = xQueueReceive(spihost[host]->ret_queue, (void *)trans_desc, ticks_to_wait);
309 if (!r) return ESP_ERR_TIMEOUT;
310 return ESP_OK;
311 }
312
313
spi_slave_transmit(spi_host_device_t host,spi_slave_transaction_t * trans_desc,TickType_t ticks_to_wait)314 esp_err_t SPI_SLAVE_ATTR spi_slave_transmit(spi_host_device_t host, spi_slave_transaction_t *trans_desc, TickType_t ticks_to_wait)
315 {
316 esp_err_t ret;
317 spi_slave_transaction_t *ret_trans;
318 //ToDo: check if any spi transfers in flight
319 ret = spi_slave_queue_trans(host, trans_desc, ticks_to_wait);
320 if (ret != ESP_OK) return ret;
321 ret = spi_slave_get_trans_result(host, &ret_trans, ticks_to_wait);
322 if (ret != ESP_OK) return ret;
323 assert(ret_trans == trans_desc);
324 return ESP_OK;
325 }
326
spi_slave_restart_after_dmareset(void * arg)327 static void SPI_SLAVE_ISR_ATTR spi_slave_restart_after_dmareset(void *arg)
328 {
329 spi_slave_t *host = (spi_slave_t *)arg;
330 esp_intr_enable(host->intr);
331 }
332
333 //This is run in interrupt context and apart from initialization and destruction, this is the only code
334 //touching the host (=spihost[x]) variable. The rest of the data arrives in queues. That is why there are
335 //no muxes in this code.
spi_intr(void * arg)336 static void SPI_SLAVE_ISR_ATTR spi_intr(void *arg)
337 {
338 BaseType_t r;
339 BaseType_t do_yield = pdFALSE;
340 spi_slave_transaction_t *trans = NULL;
341 spi_slave_t *host = (spi_slave_t *)arg;
342 spi_slave_hal_context_t *hal = &host->hal;
343
344 assert(spi_slave_hal_usr_is_done(hal));
345
346 bool use_dma = host->dma_enabled;
347 if (host->cur_trans) {
348 // When DMA is enabled, the slave rx dma suffers from unexpected transactions. Forbid reading until transaction ready.
349 if (use_dma) freeze_cs(host);
350
351 spi_slave_hal_store_result(hal);
352 host->cur_trans->trans_len = spi_slave_hal_get_rcv_bitlen(hal);
353
354 if (spi_slave_hal_dma_need_reset(hal)) {
355 //On ESP32 and ESP32S2, actual_tx_dma_chan and actual_rx_dma_chan are always same
356 spicommon_dmaworkaround_req_reset(host->tx_dma_chan, spi_slave_restart_after_dmareset, host);
357 }
358 if (host->cfg.post_trans_cb) host->cfg.post_trans_cb(host->cur_trans);
359 //Okay, transaction is done.
360 //Return transaction descriptor.
361 xQueueSendFromISR(host->ret_queue, &host->cur_trans, &do_yield);
362 host->cur_trans = NULL;
363 }
364 if (use_dma) {
365 //On ESP32 and ESP32S2, actual_tx_dma_chan and actual_rx_dma_chan are always same
366 spicommon_dmaworkaround_idle(host->tx_dma_chan);
367 if (spicommon_dmaworkaround_reset_in_progress()) {
368 //We need to wait for the reset to complete. Disable int (will be re-enabled on reset callback) and exit isr.
369 esp_intr_disable(host->intr);
370 if (do_yield) portYIELD_FROM_ISR();
371 return;
372 }
373 }
374
375 //Disable interrupt before checking to avoid concurrency issue.
376 esp_intr_disable(host->intr);
377 //Grab next transaction
378 r = xQueueReceiveFromISR(host->trans_queue, &trans, &do_yield);
379 if (r) {
380 //enable the interrupt again if there is packet to send
381 esp_intr_enable(host->intr);
382
383 //We have a transaction. Send it.
384 host->cur_trans = trans;
385
386 hal->bitlen = trans->length;
387 hal->rx_buffer = trans->rx_buffer;
388 hal->tx_buffer = trans->tx_buffer;
389
390 if (use_dma) {
391 //On ESP32 and ESP32S2, actual_tx_dma_chan and actual_rx_dma_chan are always same
392 spicommon_dmaworkaround_transfer_active(host->tx_dma_chan);
393 }
394
395 spi_slave_hal_prepare_data(hal);
396
397 //The slave rx dma get disturbed by unexpected transaction. Only connect the CS when slave is ready.
398 if (use_dma) {
399 restore_cs(host);
400 }
401
402 //Kick off transfer
403 spi_slave_hal_user_start(hal);
404 if (host->cfg.post_setup_cb) host->cfg.post_setup_cb(trans);
405 }
406 if (do_yield) portYIELD_FROM_ISR();
407 }
408