From 677a0c438d2494e17443c0e5663b4bbc9dec9ceb Mon Sep 17 00:00:00 2001 From: meawoppl Date: Sun, 12 Jun 2022 13:10:42 -0700 Subject: [PATCH 1/2] spi_master: added a device flag "SPI_DEVICE_NO_RETURN_RESULT" Add this flag to select if returning done transaction descriptors from ISR. You should get the finished transaction descriptor by the callback "post_cb" if you using this flag, if not, same as the past. Close https://github.com/espressif/esp-idf/pull/9141 --- components/driver/include/driver/spi_master.h | 2 +- components/driver/spi_master.c | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/components/driver/include/driver/spi_master.h b/components/driver/include/driver/spi_master.h index 3c599ae9e01..16c9e26e791 100644 --- a/components/driver/include/driver/spi_master.h +++ b/components/driver/include/driver/spi_master.h @@ -43,7 +43,7 @@ extern "C" */ #define SPI_DEVICE_NO_DUMMY (1<<6) #define SPI_DEVICE_DDRCLK (1<<7) - +#define SPI_DEVICE_NO_RETURN_RESULT (1<<8) ///< Don't return the descriptor to the host on completion (use post_cb to notify instead) typedef struct spi_transaction_t spi_transaction_t; typedef void(*transaction_cb_t)(spi_transaction_t *trans); diff --git a/components/driver/spi_master.c b/components/driver/spi_master.c index 0a7cac3523f..c6f9d5004cb 100644 --- a/components/driver/spi_master.c +++ b/components/driver/spi_master.c @@ -612,9 +612,13 @@ static void SPI_MASTER_ISR_ATTR spi_intr(void *arg) //cur_cs is changed to DEV_NUM_MAX here spi_post_trans(host); + + if (!(host->device[cs]->cfg.flags & SPI_DEVICE_NO_RETURN_RESULT)) { + //Return transaction descriptor. + xQueueSendFromISR(host->device[cs]->ret_queue, &host->cur_trans_buf, &do_yield); + } + // spi_bus_lock_bg_pause(bus_attr->lock); - //Return transaction descriptor. - xQueueSendFromISR(host->device[cs]->ret_queue, &host->cur_trans_buf, &do_yield); #ifdef CONFIG_PM_ENABLE //Release APB frequency lock esp_pm_lock_release(bus_attr->pm_lock); From 5726f3d8cb8d117889a2969e655f248c9d79b8a8 Mon Sep 17 00:00:00 2001 From: wanlei Date: Wed, 17 Aug 2022 15:03:56 +0800 Subject: [PATCH 2/2] spi_master: add check for 'post_cb' when flag 'SPI_DEVICE_NO_RETURN_RESULT' is set There are two ways to know which transaction descriptor is finished, by either calling `spi_device_get_trans_result`, or getting it from `post_cb`. When `SPI_DEVICE_NO_RETURN_RESULT` is set, driver will not push finished transaction descriptors into the queue. So you can't get it from `spi_device_get_trans_result`. The only way to know this is via `post_cb`. update document for function `spi_bus_add_device` --- components/driver/include/driver/spi_master.h | 3 ++- components/driver/spi_master.c | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/components/driver/include/driver/spi_master.h b/components/driver/include/driver/spi_master.h index 16c9e26e791..6623a9b5d0d 100644 --- a/components/driver/include/driver/spi_master.h +++ b/components/driver/include/driver/spi_master.h @@ -166,7 +166,8 @@ typedef struct spi_device_t *spi_device_handle_t; ///< Handle for a device on a * @param dev_config SPI interface protocol config for the device * @param handle Pointer to variable to hold the device handle * @return - * - ESP_ERR_INVALID_ARG if parameter is invalid + * - ESP_ERR_INVALID_ARG if parameter is invalid or configuration combination is not supported (e.g. + * `dev_config->post_cb` isn't set while flag `SPI_DEVICE_NO_RETURN_RESULT` is enabled) * - ESP_ERR_NOT_FOUND if host doesn't have any free CS slots * - ESP_ERR_NO_MEM if out of memory * - ESP_OK on success diff --git a/components/driver/spi_master.c b/components/driver/spi_master.c index c6f9d5004cb..d31fdf2c54a 100644 --- a/components/driver/spi_master.c +++ b/components/driver/spi_master.c @@ -329,6 +329,12 @@ esp_err_t spi_bus_add_device(spi_host_device_t host_id, const spi_device_interfa SPI_CHECK(dev_config->cs_ena_pretrans <= 1 || (dev_config->address_bits == 0 && dev_config->command_bits == 0) || (dev_config->flags & SPI_DEVICE_HALFDUPLEX), "In full-duplex mode, only support cs pretrans delay = 1 and without address_bits and command_bits", ESP_ERR_INVALID_ARG); #endif + + //Check post_cb status when `SPI_DEVICE_NO_RETURN_RESULT` flag is set. + if (dev_config->flags & SPI_DEVICE_NO_RETURN_RESULT) { + SPI_CHECK(dev_config->post_cb != NULL, "use feature flag 'SPI_DEVICE_NO_RETURN_RESULT' but no post callback function sets", ESP_ERR_INVALID_ARG); + } + uint32_t lock_flag = ((dev_config->spics_io_num != -1)? SPI_BUS_LOCK_DEV_FLAG_CS_REQUIRED: 0); spi_bus_lock_dev_config_t lock_config = {