diff --git a/components/driver/spi_master.c b/components/driver/spi_master.c index f216a2831..0fa5c9b31 100644 --- a/components/driver/spi_master.c +++ b/components/driver/spi_master.c @@ -122,6 +122,8 @@ static void spi_intr(void *arg); esp_err_t spi_bus_initialize(spi_host_device_t host, const spi_bus_config_t *bus_config, int dma_chan) { bool native, spi_chan_claimed, dma_chan_claimed; + esp_err_t ret = ESP_OK; + esp_err_t err; /* ToDo: remove this when we have flash operations cooperating with this */ SPI_CHECK(host!=SPI_HOST, "SPI1 is not supported", ESP_ERR_NOT_SUPPORTED); @@ -140,17 +142,25 @@ esp_err_t spi_bus_initialize(spi_host_device_t host, const spi_bus_config_t *bus } spihost[host]=malloc(sizeof(spi_host_t)); - if (spihost[host]==NULL) goto nomem; + if (spihost[host]==NULL) { + ret = ESP_ERR_NO_MEM; + goto cleanup; + } memset(spihost[host], 0, sizeof(spi_host_t)); #ifdef CONFIG_PM_ENABLE - esp_err_t err = esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, "spi_master", + err = esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, "spi_master", &spihost[host]->pm_lock); if (err != ESP_OK) { - goto nomem; + ret = err; + goto cleanup; } #endif //CONFIG_PM_ENABLE - spicommon_bus_initialize_io(host, bus_config, dma_chan, SPICOMMON_BUSFLAG_MASTER|SPICOMMON_BUSFLAG_QUAD, &native); + err = spicommon_bus_initialize_io(host, bus_config, dma_chan, SPICOMMON_BUSFLAG_MASTER|SPICOMMON_BUSFLAG_QUAD, &native); + if (err != ESP_OK) { + ret = err; + goto cleanup; + } spihost[host]->no_gpio_matrix=native; spihost[host]->dma_chan=dma_chan; @@ -163,9 +173,17 @@ esp_err_t spi_bus_initialize(spi_host_device_t host, const spi_bus_config_t *bus spihost[host]->max_transfer_sz = dma_desc_ct*SPI_MAX_DMA_LEN; spihost[host]->dmadesc_tx=heap_caps_malloc(sizeof(lldesc_t)*dma_desc_ct, MALLOC_CAP_DMA); spihost[host]->dmadesc_rx=heap_caps_malloc(sizeof(lldesc_t)*dma_desc_ct, MALLOC_CAP_DMA); - if (!spihost[host]->dmadesc_tx || !spihost[host]->dmadesc_rx) goto nomem; + if (!spihost[host]->dmadesc_tx || !spihost[host]->dmadesc_rx) { + ret = ESP_ERR_NO_MEM; + goto cleanup; + } + } + + err = esp_intr_alloc(spicommon_irqsource_for_host(host), ESP_INTR_FLAG_INTRDISABLED, spi_intr, (void*)spihost[host], &spihost[host]->intr); + if (err != ESP_OK) { + ret = err; + goto cleanup; } - esp_intr_alloc(spicommon_irqsource_for_host(host), ESP_INTR_FLAG_INTRDISABLED, spi_intr, (void*)spihost[host], &spihost[host]->intr); spihost[host]->hw=spicommon_hw_for_host(host); spihost[host]->cur_cs = NO_CS; @@ -197,7 +215,7 @@ esp_err_t spi_bus_initialize(spi_host_device_t host, const spi_bus_config_t *bus return ESP_OK; -nomem: +cleanup: if (spihost[host]) { free(spihost[host]->dmadesc_tx); free(spihost[host]->dmadesc_rx); @@ -210,7 +228,7 @@ nomem: free(spihost[host]); spicommon_periph_free(host); spicommon_dma_chan_free(dma_chan); - return ESP_ERR_NO_MEM; + return ret; } esp_err_t spi_bus_free(spi_host_device_t host) diff --git a/components/driver/spi_slave.c b/components/driver/spi_slave.c index c65cb3b52..157829eeb 100644 --- a/components/driver/spi_slave.c +++ b/components/driver/spi_slave.c @@ -73,6 +73,8 @@ static void IRAM_ATTR spi_intr(void *arg); 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, int dma_chan) { bool native, spi_chan_claimed, dma_chan_claimed; + esp_err_t ret = ESP_OK; + esp_err_t err; //We only support HSPI/VSPI, period. SPI_CHECK(VALID_HOST(host), "invalid host", ESP_ERR_INVALID_ARG); SPI_CHECK( dma_chan >= 0 && dma_chan <= 2, "invalid dma channel", ESP_ERR_INVALID_ARG ); @@ -89,11 +91,18 @@ esp_err_t spi_slave_initialize(spi_host_device_t host, const spi_bus_config_t *b } spihost[host] = malloc(sizeof(spi_slave_t)); - if (spihost[host] == NULL) goto nomem; + if (spihost[host] == NULL) { + ret = ESP_ERR_NO_MEM; + goto cleanup; + } memset(spihost[host], 0, sizeof(spi_slave_t)); memcpy(&spihost[host]->cfg, slave_config, sizeof(spi_slave_interface_config_t)); - spicommon_bus_initialize_io(host, bus_config, dma_chan, SPICOMMON_BUSFLAG_SLAVE, &native); + err = spicommon_bus_initialize_io(host, bus_config, dma_chan, SPICOMMON_BUSFLAG_SLAVE, &native); + if (err!=ESP_OK) { + ret = err; + goto cleanup; + } gpio_set_direction(slave_config->spics_io_num, GPIO_MODE_INPUT); spicommon_cs_initialize(host, slave_config->spics_io_num, 0, native == false); spihost[host]->no_gpio_matrix = native; @@ -105,16 +114,20 @@ esp_err_t spi_slave_initialize(spi_host_device_t host, const spi_bus_config_t *b spihost[host]->max_transfer_sz = dma_desc_ct * SPI_MAX_DMA_LEN; spihost[host]->dmadesc_tx = heap_caps_malloc(sizeof(lldesc_t) * dma_desc_ct, MALLOC_CAP_DMA); spihost[host]->dmadesc_rx = heap_caps_malloc(sizeof(lldesc_t) * dma_desc_ct, MALLOC_CAP_DMA); - if (!spihost[host]->dmadesc_tx || !spihost[host]->dmadesc_rx) goto nomem; + if (!spihost[host]->dmadesc_tx || !spihost[host]->dmadesc_rx) { + ret = ESP_ERR_NO_MEM; + goto cleanup; + } } else { //We're limited to non-DMA transfers: the SPI work registers can hold 64 bytes at most. spihost[host]->max_transfer_sz = 16 * 4; } #ifdef CONFIG_PM_ENABLE - esp_err_t err = esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, "spi_slave", + err = esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, "spi_slave", &spihost[host]->pm_lock); if (err != ESP_OK) { - goto nomem; + ret = err; + goto cleanup; } // Lock APB frequency while SPI slave driver is in use esp_pm_lock_acquire(spihost[host]->pm_lock); @@ -123,9 +136,16 @@ esp_err_t spi_slave_initialize(spi_host_device_t host, const spi_bus_config_t *b //Create queues spihost[host]->trans_queue = xQueueCreate(slave_config->queue_size, sizeof(spi_slave_transaction_t *)); spihost[host]->ret_queue = xQueueCreate(slave_config->queue_size, sizeof(spi_slave_transaction_t *)); - if (!spihost[host]->trans_queue || !spihost[host]->ret_queue) goto nomem; + if (!spihost[host]->trans_queue || !spihost[host]->ret_queue) { + ret = ESP_ERR_NO_MEM; + goto cleanup; + } - esp_intr_alloc(spicommon_irqsource_for_host(host), ESP_INTR_FLAG_INTRDISABLED, spi_intr, (void *)spihost[host], &spihost[host]->intr); + err = esp_intr_alloc(spicommon_irqsource_for_host(host), ESP_INTR_FLAG_INTRDISABLED, spi_intr, (void *)spihost[host], &spihost[host]->intr); + if (err != ESP_OK) { + ret = err; + goto cleanup; + } spihost[host]->hw = spicommon_hw_for_host(host); //Configure slave @@ -191,7 +211,7 @@ esp_err_t spi_slave_initialize(spi_host_device_t host, const spi_bus_config_t *b return ESP_OK; -nomem: +cleanup: if (spihost[host]) { if (spihost[host]->trans_queue) vQueueDelete(spihost[host]->trans_queue); if (spihost[host]->ret_queue) vQueueDelete(spihost[host]->ret_queue); @@ -208,7 +228,7 @@ nomem: spihost[host] = NULL; spicommon_periph_free(host); spicommon_dma_chan_free(dma_chan); - return ESP_ERR_NO_MEM; + return ret; } esp_err_t spi_slave_free(spi_host_device_t host)