From 1393520e789d4e555e820de65e87a8dc90e7326a Mon Sep 17 00:00:00 2001 From: Armando Date: Thu, 10 Sep 2020 19:11:51 +0800 Subject: [PATCH] spi_slave: fix crash issue caused by setting cs to -1 Setting cs num to -1 means cs pin is not connected. But passing the value -1 to the spicommon_cs_initialize() will lead to program crash caused by "array index out of bounds". We expect the driver to execute well but the driver should not be in charge of users' behaviour, so this commit prevents the crash. https://github.com/espressif/esp-idf/issues/5784 --- components/driver/spi_slave.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/components/driver/spi_slave.c b/components/driver/spi_slave.c index dbb332cc35..0b6b10f431 100644 --- a/components/driver/spi_slave.c +++ b/components/driver/spi_slave.c @@ -124,6 +124,7 @@ esp_err_t spi_slave_initialize(spi_host_device_t host, const spi_bus_config_t *b #ifndef CONFIG_SPI_SLAVE_ISR_IN_IRAM 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); #endif + SPI_CHECK(slave_config->spics_io_num < 0 || GPIO_IS_VALID_GPIO(slave_config->spics_io_num), "spics pin invalid", ESP_ERR_INVALID_ARG); spi_chan_claimed=spicommon_periph_claim(host, "spi slave"); SPI_CHECK(spi_chan_claimed, "host already in use", ESP_ERR_INVALID_STATE); @@ -151,7 +152,10 @@ esp_err_t spi_slave_initialize(spi_host_device_t host, const spi_bus_config_t *b ret = err; goto cleanup; } - spicommon_cs_initialize(host, slave_config->spics_io_num, 0, !bus_is_iomux(spihost[host])); + if (slave_config->spics_io_num >= 0) { + spicommon_cs_initialize(host, slave_config->spics_io_num, 0, !bus_is_iomux(spihost[host])); + } + // The slave DMA suffers from unexpected transactions. Forbid reading if DMA is enabled by disabling the CS line. if (use_dma) freeze_cs(spihost[host]);