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);
}
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;
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;
return ESP_OK;
-nomem:
+cleanup:
if (spihost[host]) {
free(spihost[host]->dmadesc_tx);
free(spihost[host]->dmadesc_rx);
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)
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 );
}
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;
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);
//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
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);
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)