Hello Community,
I’m already on the edge of a mental breakdown because I don’t know what else I can do… 
So I ask for your help!
The Questions sounds pretty easy, but I cannot bring the gpio on my esp32 c6 to work with espidf.
With Arduino Platform or even on ESP IDF the code and board works without any problems.
platform.io-ini:
[env:esp32-c6-devkitc-1]
platform = espressif32
board = esp32-c6-devkitc-1
framework = espidf
sketch:
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#define GPIO_OUTPUT_IO GPIO_NUM_2
#define GPIO_OUTPUT_PIN_SEL (1ULL<<GPIO_OUTPUT_IO)
void app_main(void)
{
gpio_config_t io_conf = {
.pin_bit_mask = GPIO_OUTPUT_PIN_SEL,
.mode = GPIO_MODE_OUTPUT,
.pull_up_en = GPIO_PULLUP_DISABLE,
.pull_down_en = GPIO_PULLDOWN_DISABLE,
.intr_type = GPIO_INTR_DISABLE
};
gpio_config(&io_conf);
gpio_set_level(GPIO_OUTPUT_IO, 1);
while (1) {
vTaskDelay(pdMS_TO_TICKS(1000));
}
}
I tried every pin, every configuration, but I don’t get the gpio to work??
As I mentioned, with other platforms everything just works fine…
Maybe there is something wrong with the menuconfig, especially in combination c6 and espidf via platformio?
Maybe there is someone, who can help me.
Thank you in advance and have a great easter break!
Is there any serial output at all from the bootloader? Maybe the microcontroller is just not booting the firmware correctly.
Is the code above written in a .c
file or .cpp
file?
Yes @maxgerhardt, you are correct. The esp is not booting the firmware.
I figured out, that when I use cout, the firmware is booting and the gpio is working correctly.
For example, in this version the code is not working (commenting out the cout):
#include <iostream>
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#define LED_PIN GPIO_NUM_2
#define INPUT_PIN GPIO_NUM_23
extern "C" {
void app_main() {
gpio_reset_pin(LED_PIN);
gpio_reset_pin(INPUT_PIN);
gpio_set_direction(LED_PIN, GPIO_MODE_OUTPUT);
gpio_set_direction(INPUT_PIN, GPIO_MODE_INPUT);
while(true) {
gpio_set_level(LED_PIN, 1);
//std::cout << "Turning LED on - " << gpio_get_level(INPUT_PIN) << std::endl;
vTaskDelay(1000 / portTICK_PERIOD_MS);
gpio_set_level(LED_PIN, 0);
//std::cout << "Turning LED off - " << gpio_get_level(INPUT_PIN) << std::endl;
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
}
In this version, everything is working fine, when using cout as well:
#include <iostream>
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#define LED_PIN GPIO_NUM_2
#define INPUT_PIN GPIO_NUM_23
extern "C" {
void app_main() {
gpio_reset_pin(LED_PIN);
gpio_reset_pin(INPUT_PIN);
gpio_set_direction(LED_PIN, GPIO_MODE_OUTPUT);
gpio_set_direction(INPUT_PIN, GPIO_MODE_INPUT);
while(true) {
gpio_set_level(LED_PIN, 1);
std::cout << "Turning LED on - " << gpio_get_level(INPUT_PIN) << std::endl;
vTaskDelay(1000 / portTICK_PERIOD_MS);
gpio_set_level(LED_PIN, 0);
std::cout << "Turning LED off - " << gpio_get_level(INPUT_PIN) << std::endl;
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
}
What’s the reason for that?
Why does the esp reboot, when I don’t use the cout?
That is extremly weird. One technical reason I can think of is that, when you don’t print out the variable, the compiler might optimize the actual gpio_set_level
call away. This should however never ever happen because the registers it writes to are marked as volatile. So I have no idea.
Is this reproducible with the regular https://github.com/espressif/esp-idf/? If yes, file a bug report there.
1 Like
With the regular esp-idf everything just works fine…
It seems that something might be optimized, yes.
Also when configuring SPI for example, the esp goes into boot loop.
I tried the example on regular esp-idf as well, there it works as well…
After comparing the sdkconfig from esp-idf and platformio, booth have the same content.
I also have to mention, that this only occurs with the c6 version.
For example after uploading the spi-example on an older az delivery esp32 (I think its the s3?), the code worked as well.
This is a stripped down example code where we just tried to initialize SPI. It is working fine with ESP32-C6 on ESP-IDF but leading to a boot loop in PlatformIO.
#include <driver/gpio.h>
#include <driver/spi_master.h>
#include <esp_log.h>
void app_main(void) {
gpio_reset_pin(GPIO_NUM_23);
gpio_set_direction(GPIO_NUM_23, GPIO_MODE_OUTPUT);
gpio_set_level(GPIO_NUM_23, 1);
// std::cout << "app_main() called" << std::endl;
spi_bus_free(SPI2_HOST);
spi_bus_config_t spi_cfg = {
.mosi_io_num = 20, .miso_io_num = 21, .sclk_io_num = 19
};
esp_err_t ret;
ret = spi_bus_initialize(SPI2_HOST, &spi_cfg, SPI_DMA_CH_AUTO);
// ESP_ERROR_CHECK(spi_bus_initialize(SPI2_HOST, &spi_cfg, 1));
if (ret != ESP_OK) {
ESP_LOGE("SPI", "Failed to init SPI bus: %s", esp_err_to_name(ret));
}
while (true) {
vTaskDelay(2500 / portTICK_PERIOD_MS);
}
}
Thank you @maxgerhardt for your help!