Page 1 of 1

ESP32-S3-WROOM-1 Custom SPI Pins

Posted: Wed Mar 06, 2024 11:26 pm
by radurotaru19
Hi everyone,

I have been experiencing a problem with an SPI bus and I can't figure it out. I am using a custom developed board with the ESP32-S3-WROOM-1 module on it.
I have connected to the board an external ADC MCP3204 from Microchip which communicates over SPI with the ESP, pinout:


GPIO39 SCK FSPI_SCK
GPIO40 MISO FSPI_MISO
GPIO41 MOSI FSPI_MOSI
GPIO42 CS Analog to digital converter


I originally wrote my whole code for it using the Arduino Framework & PlatformIO which worked fine with the Arduino libraries but I have decided to switch over to the ESP-IDF.

I did find a pre-made library for the MCP3204 at: https://github.com/gfurtadoalmeida/esp32-driver-mcp320x

Code: Select all

#include "driver/spi_master.h"
#include "driver/gpio.h"
#include "esp_log.h"
#include "esp32_driver_mcp320x/mcp320x.h"

void app_main(void)
{
    spi_bus_config_t bus_cfg = {
        .mosi_io_num = GPIO_NUM_23,
        .miso_io_num = GPIO_NUM_19,
        .sclk_io_num = GPIO_NUM_18,
        .quadwp_io_num = -1,
        .quadhd_io_num = -1,
        .data4_io_num = -1,
        .data5_io_num = -1,
        .data6_io_num = -1,
        .data7_io_num = -1,
        .max_transfer_sz = 3, // 24 bits.
        .flags = SPICOMMON_BUSFLAG_MASTER,
        .isr_cpu_id = INTR_CPU_ID_AUTO,
        .intr_flags = ESP_INTR_FLAG_LEVEL3};

    mcp320x_config_t mcp320x_cfg = {
        .host = SPI3_HOST,
        .device_model = MCP3204_MODEL,
        .clock_speed_hz = 1 * 1000 * 1000, // 1 Mhz.
        .reference_voltage = 5000,         // 5V
        .cs_io_num = GPIO_NUM_5};

    // Bus initialization is up to the developer.
    spi_bus_initialize(mcp320x_cfg.host, &bus_cfg, 0);

    // Add the device to the SPI bus.
    mcp320x_t *mcp320x_handle = mcp320x_install(&mcp320x_cfg);

    // Occupy the SPI bus for multiple transactions.
    mcp320x_acquire(mcp320x_handle, portMAX_DELAY);

    uint16_t voltage = 0;

    for (size_t i = 0; i < 10; i++)
    {
        // Read voltage, sampling 1000 times.
        mcp320x_read_voltage(mcp320x_handle,
                             MCP320X_CHANNEL_0,
                             MCP320X_READ_MODE_SINGLE,
                             1000,
                             &voltage);

        ESP_LOGI("mcp320x", "Voltage: %d mV", voltage);
    }

    // Unoccupy the SPI bus.
    mcp320x_release(mcp320x_handle);

    // Free resources.
    mcp320x_delete(mcp320x_handle);
}
After couple of hours of trying to see if the library has an issue or not, I discovered that the CS line is not pulled HI/LO when the SPI transaction should take place, I have solved it by running the gpio_reset_pin(PIN_NUM_CS) at the beginning of the main function. However the device is still not communicating properly as it is not registering any inputs on the MISO line. I have tried to play with the gpio matrix to set the pin outputs to INPUT/OUTPUT but with no luck.

Is there any way to properly configure the custom SPI pins to work? Running VS Code IDF Version 5.1.

Regards,
Radu