SPI polling transaction fails because previous is not terminated

rtborg
Posts: 67
Joined: Wed Oct 23, 2019 6:15 am

SPI polling transaction fails because previous is not terminated

Postby rtborg » Wed Jun 01, 2022 6:12 am

I have a board with three SPI devices on Host 3 (VSPI), using pins 18 for clock, 23 for MOSI, and 19 for MISO. Clock is 10 MHz:
  • W5500 Etherned module, CS 33
  • SD card, CS 25
  • MPC23S08 GPIO expander, CS 32
My code begins with initializing the SD card, then the expander and finally the Ethernet chip.

I only do direct R/W on the SPI bus in the MPC23S08 driver routines. Here is the initialization code; it is called after the bus has been initialized for the SD card:

Code: Select all

static esp_err_t mpc23s08_spi_initialize(spi_host_device_t spi_host, int clock_mhz, int miso, int mosi, int sclk, int cs)
{
    esp_err_t ret;

    spi_bus_config_t bus_cfg = {
        .miso_io_num = miso,
        .mosi_io_num = mosi,
        .sclk_io_num = sclk,
        .quadwp_io_num = -1,
        .quadhd_io_num = -1,
    };

    ret = spi_bus_initialize(spi_host, &bus_cfg, SPI_DMA_CH_AUTO);

    switch (ret)
    {
    case ESP_ERR_INVALID_STATE:
        ESP_LOGE(TAG, "SPI driver already initialized.");
        break;
    case ESP_OK:
        ESP_LOGI(TAG, "SPI driver initialized");
        break;
    default:
        ESP_LOGE(TAG, "Failed to initialize bus.");
        ESP_ERROR_CHECK(ret); /* Bomb if error is not any of the above */
    }

    spi_device_interface_config_t devcfg = {
        .command_bits = 0,
        .address_bits = 0,
        .dummy_bits = 0,
        .mode = 0,
        .spics_io_num = cs,
        .clock_speed_hz = 1000 * 1000 * clock_mhz,
        .flags = 0,
        .queue_size = 1,
    };

    ESP_ERROR_CHECK(spi_bus_add_device(spi_host, &devcfg, &mcp_spi_handle));
    gpio_set_pull_mode(cs, GPIO_PULLUP_ONLY); /* Use only when board does not have a pull-up on the CS line */

    return 0;
}
And here's the transfer routine, which is used by the driver:

Code: Select all

static int mpc23s08_spi_transfer(uint8_t *tx_buffer, uint8_t tx_len, uint8_t *rx_buffer, uint8_t *rx_len)
{
    esp_err_t ret = ESP_OK;
    
    spi_transaction_t trans;
    memset(&trans, 0x00, sizeof(trans));
    trans.length = 8 * tx_len;
    trans.tx_buffer = tx_buffer;
    trans.rx_buffer = rx_buffer;

    if (spi_device_polling_transmit(mcp_spi_handle, &trans) != ESP_OK)
    {
        ESP_LOGE(TAG, "%s(%d): spi transmit failed", __FUNCTION__, __LINE__);
        ret = ESP_FAIL;
    }

    *rx_len = trans.rxlength;

    return ret;
}
Quite often the transfer functions fails with the following message:
spi_master: spi_device_polling_start(933): Cannot send polling transaction while the previous polling transaction is not terminated.
I only use the MCP23S08 driver in one task, so there's no mutex in the driver. SD card storage is used by many tasks, I am not sure if that could be a cause of a problem.

Thanks

ESP_ondrej
Posts: 210
Joined: Fri May 07, 2021 10:35 am

Re: SPI polling transaction fails because previous is not terminated

Postby ESP_ondrej » Wed Jun 01, 2022 6:21 am

Your problem could be possibly similar to https://github.com/espressif/esp-idf/issues/8179.

It hasn't been resolved yet but there are some workarounds proposed by users.

rtborg
Posts: 67
Joined: Wed Oct 23, 2019 6:15 am

Re: SPI polling transaction fails because previous is not terminated

Postby rtborg » Wed Jun 01, 2022 11:44 am

Thanks for the quick reply, the problem seems the same.

The code breaks at the following line of spi_master.c:

Code: Select all

assert(handle == get_acquiring_dev(host));
I'll try the proposed workaround.

gjgsmithvr
Posts: 5
Joined: Fri Jun 17, 2022 4:41 am

Re: SPI polling transaction fails because previous is not terminated

Postby gjgsmithvr » Fri Jul 01, 2022 3:15 am

@rtborg, did you find a solution for your issue?

ESP_ondrej
Posts: 210
Joined: Fri May 07, 2021 10:35 am

Re: SPI polling transaction fails because previous is not terminated

Postby ESP_ondrej » Fri Oct 07, 2022 11:51 am


Who is online

Users browsing this forum: MSN [Bot] and 85 guests