- W5500 Etherned module, CS 33
- SD card, CS 25
- MPC23S08 GPIO expander, CS 32
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;
}
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;
}
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.spi_master: spi_device_polling_start(933): Cannot send polling transaction while the previous polling transaction is not terminated.
Thanks