Platformio (espidf) has different results compared to arduino ide

Hi,

I have the ESP32-C3-01M board (description here).

When I program this board in the Arduino IDE, with this program:

#define RGBLED_RED    3
#define RGBLED_GREEN  4
#define RGBLED_BLUE   5
#define RED_PATTERN   0b11111110
#define GREEN_PATTERN 0b00001111
#define BLUE_PATTERN  0b11001100

uint8_t nCounter = 0;
uint8_t nChecker = 1;

void setup() {
  pinMode(RGBLED_RED, OUTPUT);
  pinMode(RGBLED_GREEN, OUTPUT);
  pinMode(RGBLED_BLUE, OUTPUT);
}

void loop() {
  digitalWrite(RGBLED_RED, ((RED_PATTERN & nChecker) != 0) ? HIGH : LOW);
  digitalWrite(RGBLED_GREEN, ((GREEN_PATTERN & nChecker) != 0) ? HIGH : LOW);
  digitalWrite(RGBLED_BLUE, ((BLUE_PATTERN & nChecker) != 0) ? HIGH : LOW);
  delay(125);
  nCounter++;
  nCounter = nCounter % 8;
  nChecker = 1 << nCounter;
}

all three LEDs do their pattern like described in their pattern.

But if I make a program almost the same as above in Visual Studio Code with the platformio extension with the Espressif32:

#include <stdio.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include "driver/gpio.h"

#define RGBLED_RED 3
#define RGBLED_GREEN 4
#define RGBLED_BLUE 5
#define RED_PATTERN   0b11111110
#define GREEN_PATTERN 0b00001111
#define BLUE_PATTERN  0b11001100

void app_main()
{
  gpio_set_direction(RGBLED_GREEN, GPIO_MODE_OUTPUT);
  gpio_set_direction(RGBLED_RED, GPIO_MODE_OUTPUT);
  gpio_set_direction(RGBLED_BLUE, GPIO_MODE_OUTPUT);

  uint8_t nCounter = 0;
  uint8_t nChecker = 1;
  while (true)
  {
    gpio_set_level(RGBLED_GREEN, ((GREEN_PATTERN & nChecker) != 0) ? 1 : 0);
    gpio_set_level(RGBLED_RED, ((RED_PATTERN & nChecker) != 0) ? 1 : 0);
    gpio_set_level(RGBLED_BLUE, ((BLUE_PATTERN & nChecker) != 0) ? 1 : 0);
    //vTaskDelay(500 / portTICK_PERIOD_MS);
    //gpio_set_level(RGBLED_BLUE, 0);
    //vTaskDelay(500 / portTICK_PERIOD_MS);
    vTaskDelay(125 / portTICK_PERIOD_MS);
    nCounter++;
    nCounter = nCounter % 8;
    nChecker = 1 << nCounter;
  }
}

Then only the red LED is doing its pattern. I cannot switch on the green or blue LED.

Even though I make a program with only turning on the green or blue LED.

What do I need to do to fix this, because I rather like the platformio extension.

With kind regards,

Erik

GPIO4 and 5 are JTAG pins by default (MTMS and MTDI). Calling gpio_set_direction() does not seem to be enough to relieve them of their JTAG functionality. Try this instead.

#include <stdio.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include "driver/gpio.h"

#define RGBLED_RED 3
#define RGBLED_GREEN 4
#define RGBLED_BLUE 5
#define RED_PATTERN   0b11111110
#define GREEN_PATTERN 0b00001111
#define BLUE_PATTERN  0b11001100

void app_main()
{
  gpio_reset_pin(RGBLED_GREEN); // JTAG MTMS
  gpio_reset_pin(RGBLED_BLUE); // JTAG MTDI
  gpio_set_direction(RGBLED_GREEN, GPIO_MODE_OUTPUT);
  gpio_set_direction(RGBLED_RED, GPIO_MODE_OUTPUT);
  gpio_set_direction(RGBLED_BLUE, GPIO_MODE_OUTPUT);

  uint8_t nCounter = 0;
  uint8_t nChecker = 1;
  while (true)
  {
    gpio_set_level(RGBLED_GREEN, ((GREEN_PATTERN & nChecker) != 0) ? 1 : 0);
    gpio_set_level(RGBLED_RED, ((RED_PATTERN & nChecker) != 0) ? 1 : 0);
    gpio_set_level(RGBLED_BLUE, ((BLUE_PATTERN & nChecker) != 0) ? 1 : 0);
    //vTaskDelay(500 / portTICK_PERIOD_MS);
    //gpio_set_level(RGBLED_BLUE, 0);
    //vTaskDelay(500 / portTICK_PERIOD_MS);
    vTaskDelay(125 / portTICK_PERIOD_MS);
    nCounter++;
    nCounter = nCounter % 8;
    nChecker = 1 << nCounter;
  }
}
1 Like

You may find it clearer to use a complete gpio_config() instead of just gpio_set_direction, this should also work.

https://randomnerdtutorials.com/esp-idf-esp32-gpio-outputs/

This did the trick. Thanks for the extremely fast answer.

Greetings,

Erik

I tried this config before, but that doesn’t work.

Maybe I should add a reset in the config as well ?!?!

Does this not work?

#include <stdio.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include "driver/gpio.h"

#define RGBLED_RED 3
#define RGBLED_GREEN 4
#define RGBLED_BLUE 5
#define RED_PATTERN   0b11111110
#define GREEN_PATTERN 0b00001111
#define BLUE_PATTERN  0b11001100

void app_main()
{
  gpio_config_t io_conf = {
      .pin_bit_mask = (1ULL << RGBLED_RED ) | (1ULL << RGBLED_GREEN) | (1ULL << RGBLED_BLUE),  // Select all LED pins
      .mode = GPIO_MODE_OUTPUT,            // Set as output
      .pull_up_en = GPIO_PULLUP_DISABLE,  // Disable pull-up
      .pull_down_en = GPIO_PULLDOWN_DISABLE,  // Disable pull-down
      .intr_type = GPIO_INTR_DISABLE             // Disable interrupts
  };
  gpio_config(&io_conf);

  uint8_t nCounter = 0;
  uint8_t nChecker = 1;
  while (true)
  {
    gpio_set_level(RGBLED_GREEN, ((GREEN_PATTERN & nChecker) != 0) ? 1 : 0);
    gpio_set_level(RGBLED_RED, ((RED_PATTERN & nChecker) != 0) ? 1 : 0);
    gpio_set_level(RGBLED_BLUE, ((BLUE_PATTERN & nChecker) != 0) ? 1 : 0);
    //vTaskDelay(500 / portTICK_PERIOD_MS);
    //gpio_set_level(RGBLED_BLUE, 0);
    //vTaskDelay(500 / portTICK_PERIOD_MS);
    vTaskDelay(125 / portTICK_PERIOD_MS);
    nCounter++;
    nCounter = nCounter % 8;
    nChecker = 1 << nCounter;
  }
}

Nope. This was the first thing I tried to solve the problem.

How interesting. gpio_config should do a lot of the same things that gpio_reset and gpio_set_direction does when I look at https://github.com/espressif/esp-idf/blob/89cb1d10d621266677ff1785f270e760ddd014a6/components/esp_driver_gpio/src/gpio.c#L338-L450. Not sure about this one.