Page 1 of 1

ESP32-S2先初始化GPIO0再初始化SPI,GPIO0操作失效

Posted: Wed Aug 09, 2023 3:50 pm
by Eltrion
ESP32-S2使用GPIO0与SPI时,如果先初始化GPIO0,再初始化SPI,使用gpio_set_level函数,GPIO0并没有输出。而先初始化SPI,再初始化GPIO0,使用gpio_set_level控制GPIO0有效
这样写GPIO0操作无效

Code: Select all

    gpio_reset_pin(GPIO_NUM_0);
    gpio_set_direction(GPIO_NUM_0, GPIO_MODE_OUTPUT);
    gpio_set_level(GPIO_NUM_0, 1);

    spi_device_handle_t spi;

    spi_bus_config_t bus_cfg = {
        .mosi_io_num = GPIO_NUM_37,
        .miso_io_num = GPIO_NUM_46,
        .sclk_io_num = GPIO_NUM_45,
        .max_transfer_sz = 100,
    };
    spi_bus_initialize(SPI2_HOST, &bus_cfg, SPI_DMA_CH_AUTO);

    spi_device_interface_config_t dev_cfg = {
        .clock_speed_hz = 1 * 1000 * 1000,
        .command_bits = 0,
        .address_bits = 0,
        .dummy_bits = 0,
        .mode = 2,
        .spics_io_num = -1,
        .queue_size = 1,
    };
    spi_bus_add_device(SPI2_HOST, &dev_cfg, &spi);

    while(1)
    {
        gpio_set_level(GPIO_NUM_0, 0);
        gpio_set_level(GPIO_NUM_0, 1);
        vTaskDelay(pdMS_TO_TICKS(100));
    }
这样写GPIO0操作有效

Code: Select all

    gpio_reset_pin(GPIO_NUM_0);
    gpio_set_direction(GPIO_NUM_0, GPIO_MODE_OUTPUT);
    gpio_set_level(GPIO_NUM_0, 1);

    spi_device_handle_t spi;

    spi_bus_config_t bus_cfg = {
        .mosi_io_num = GPIO_NUM_37,
        .miso_io_num = GPIO_NUM_46,
        .sclk_io_num = GPIO_NUM_45,
        .max_transfer_sz = 100,
    };
    spi_bus_initialize(SPI2_HOST, &bus_cfg, SPI_DMA_CH_AUTO);

    spi_device_interface_config_t dev_cfg = {
        .clock_speed_hz = 1 * 1000 * 1000,
        .command_bits = 0,
        .address_bits = 0,
        .dummy_bits = 0,
        .mode = 2,
        .spics_io_num = -1,
        .queue_size = 1,
    };
    spi_bus_add_device(SPI2_HOST, &dev_cfg, &spi);

    while(1)
    {
        gpio_set_level(GPIO_NUM_0, 0);
        gpio_set_level(GPIO_NUM_0, 1);
        vTaskDelay(pdMS_TO_TICKS(100));
    }
请问这是什么原因

Re: ESP32-S2先初始化GPIO0再初始化SPI,GPIO0操作失效

Posted: Thu Aug 10, 2023 6:35 am
by ESP_Penguin_Helper
你好,大概率是因为你 SPI 的配置干扰到了 GPIO0。使用 idf 中 SPI 的 example,先初始化 GPIO0,再初始化 SPI 后是可以正常使用gpio_set_level 控制GPIO0 的。你可以检查一下代码的 SPI 配置在哪里占用了 GPIO0。