Build error and inconsistent build with two different boards

Hi,

We are running into a very curious problem when we try to build (espidf-arduino-blink) on two different boards.

Here is the code in Blink.cpp:

/* Blink Example
   This example code is in the Public Domain (or CC0 licensed, at your option.)
   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
*/

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

#include <WiFi.h>


// /* Can run 'make menuconfig' to choose the GPIO to blink,
//    or you can edit the following line and set a number here.
// */
// #define BLINK_GPIO (gpio_num_t)CONFIG_BLINK_GPIO

// #ifndef LED_BUILTIN
// #define LED_BUILTIN 4
// #endif

// void blink_task(void *pvParameter)
// {
//     /* Configure the IOMUX register for pad BLINK_GPIO (some pads are
//        muxed to GPIO on reset already, but some default to other
//        functions and need to be switched to GPIO. Consult the
//        Technical Reference for a list of pads and their default
//        functions.)
//     */
//     gpio_pad_select_gpio(BLINK_GPIO);
//     /* Set the GPIO as a push/pull output */
//     gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
//     while(1) {
//         /* Blink off (output low) */
//         gpio_set_level(BLINK_GPIO, 0);
//         vTaskDelay(1000 / portTICK_PERIOD_MS);
//         /* Blink on (output high) */
//         gpio_set_level(BLINK_GPIO, 1);
//         vTaskDelay(1000 / portTICK_PERIOD_MS);
//     }
// }

// #if !CONFIG_AUTOSTART_ARDUINO
// void arduinoTask(void *pvParameter) {
//     pinMode(LED_BUILTIN, OUTPUT);
//     while(1) {
//         digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
//         delay(1000);
//     }
// }

// extern "C" void app_main()
// {
//     // initialize arduino library before we start the tasks
//     initArduino();

//     xTaskCreate(&blink_task, "blink_task", configMINIMAL_STACK_SIZE, NULL, 5, NULL);
//     xTaskCreate(&arduinoTask, "arduino_task", configMINIMAL_STACK_SIZE, NULL, 5, NULL);
// }
// #else
// void setup() {
//     Serial.begin(115200);
//     xTaskCreate(&blink_task, "blink_task", configMINIMAL_STACK_SIZE, NULL, 5, NULL);
//     pinMode(LED_BUILTIN, OUTPUT);
// }
// void loop() {
//     digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
//     Serial.println("Hello!");
//     delay(1000);
// }
// #endif


void wifiScan() {
    // WiFi.scanNetworks will return the number of networks found
    int n = WiFi.scanNetworks();
    Serial.println("scan done");
    if (n == 0) {
        Serial.println("no networks found");
    } else {
        Serial.print(n);
        Serial.println(" networks found");
        for (int i = 0; i < n; ++i) {
            // Print SSID and RSSI for each network found
            Serial.print(i + 1);
            Serial.print(": ");
            Serial.print(WiFi.SSID(i));
            Serial.print(" (");
            Serial.print(WiFi.RSSI(i));
            Serial.print(")");
            Serial.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN)?" ":"*");
            delay(10);
        }
    }
    Serial.println("");
}

#if !CONFIG_AUTOSTART_ARDUINO
void arduinoTask(void *pvParameter) {
    // Set WiFi to station mode and disconnect from an AP if it was previously connected
    WiFi.mode(WIFI_STA);
    WiFi.disconnect();
    Serial.begin(115200);
    delay(100);

    while(1) {
        wifiScan();

        // Wait a bit before scanning again
        delay(5000);
    }
}

extern "C" void app_main()
{
    // initialize arduino library before we start the tasks
    initArduino();

    xTaskCreate(&arduinoTask, "arduino_task", configMINIMAL_STACK_SIZE, NULL, 5, NULL);
}
#else
void setup() {
    // Set WiFi to station mode and disconnect from an AP if it was previously connected
    WiFi.mode(WIFI_STA);
    WiFi.disconnect();
    Serial.begin(115200);
    delay(100);
}

void loop() {
    wifiScan();
    // Wait a bit before scanning again
    delay(5000);
}
#endif 



The first platformio.ini config works without any issue:

; PlatformIO Project Configuration File

;

;   Build options: build flags, source filter, extra scripting

;   Upload options: custom port, speed and extra flags

;   Library options: dependencies, extra library storages

;

; Please visit documentation for the other options and examples

; https://docs.platformio.org/page/projectconf.html

[env]

platform = espressif32

framework = arduino, espidf

build_flags =

    -D CONFIG_BLINK_GPIO=2

monitor_speed = 115200

[env:esp32-s3-devkitc-1]

board = esp32-s3-devkitc-1

board_build.flash_mode = dio

The following one however does not work and generates an error :

; PlatformIO Project Configuration File
;
;   Build options: build flags, source filter, extra scripting
;   Upload options: custom port, speed and extra flags
;   Library options: dependencies, extra library storages
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env]
platform = espressif32
framework = arduino, espidf
build_flags =
    -D CONFIG_BLINK_GPIO=2
monitor_speed = 115200

[env:adafruit_feather_esp32s3]
board = adafruit_feather_esp32s3
board_build.flash_mode = dio

The error is :

Building in release mode
Compiling .pio\build\adafruit_feather_esp32s3\src\Blink.o
Archiving .pio\build\adafruit_feather_esp32s3\esp-idf\asio\libasio.a
Compiling .pio\build\adafruit_feather_esp32s3\bootloader_support\src\bootloader_random_esp32s3.o
Compiling .pio\build\adafruit_feather_esp32s3\bootloader_support\src\bootloader_utility.o
Compiling .pio\build\adafruit_feather_esp32s3\bootloader_support\src\esp_image_format.o
Compiling .pio\build\adafruit_feather_esp32s3\bootloader_support\src\flash_encrypt.o
Compiling .pio\build\adafruit_feather_esp32s3\bootloader_support\src\secure_boot.o
Compiling .pio\build\adafruit_feather_esp32s3\bootloader_support\src\flash_partitions.o
Compiling .pio\build\adafruit_feather_esp32s3\bootloader_support\src\flash_qio_mode.o
Compiling .pio\build\adafruit_feather_esp32s3\bootloader_support\src\bootloader_flash_config_esp32s3.o
src/Blink.cpp: In function 'void wifiScan()':
src/Blink.cpp:82:5: error: 'Serial' was not declared in this scope
     Serial.println("scan done");
     ^~~~~~
src/Blink.cpp:82:5: note: suggested alternative: 'Serial2'
     Serial.println("scan done");
     ^~~~~~
     Serial2
src/Blink.cpp: In function 'void setup()':
src/Blink.cpp:131:5: error: 'Serial' was not declared in this scope
     Serial.begin(115200);
     ^~~~~~
src/Blink.cpp:131:5: note: suggested alternative: 'Serial2'
     Serial.begin(115200);
     ^~~~~~
     Serial2
*** [.pio\build\adafruit_feather_esp32s3\src\Blink.o] Error 1

Can anyone help us with this issue please?

Well the ESP32S3 has the option of a UART serial or USB-CDC serial. The working

board

sets

while

With ARDUINO_USB_MODE meaning 1 = Hardware CDC/JTAG, 0 = TinyUSB.

You might be running in a special case here with the defines or activated components (TinyUSB) being weird so that no Serial ends up actviated. Have you tried using

[env]
platform = espressif32
framework = arduino, espidf
build_flags =
    -D CONFIG_BLINK_GPIO=2
monitor_speed = 115200

[env:adafruit_feather_esp32s3]
board = adafruit_feather_esp32s3
board_build.flash_mode = dio
; overwrite build flags
build_flags = 
  -D CONFIG_BLINK_GPIO=2
  -DARDUINO_USB_MODE=1
build_unflags = -DARDUINO_USB_CDC_ON_BOOT=1

Wow !!! I’m impressed @maxgerhardt ! It worked ! Thank you so much !
You learned me new aspects (as build_unflags in platformio.ini). I’m recently learning about platformio and ESP32. There are so many things to understand ! I will read even more on the topic.

Could you tell me if the [env] section is a “common” section (for all boards) ? If so, can I set the build_flags = -D CONFIG_BLINK_GPIO=2 in the [env] section only ? Same for board_build.flash_mode = dio. So for both boards, so it would look like this :

[env]
platform = espressif32
framework = arduino, espidf
build_flags =
    -D CONFIG_BLINK_GPIO=2
monitor_speed = 115200
board_build.flash_mode = dio

[env:esp32-s3-devkitc-1]
board = esp32-s3-devkitc-1
upload_port = COM11
monitor_port = COM11

[env:adafruit_feather_esp32s3]
board = adafruit_feather_esp32s3
; overwrite build flags
build_flags = 
  -DARDUINO_USB_MODE=1
build_unflags = -DARDUINO_USB_CDC_ON_BOOT=1
upload_port = COM13
monitor_port = COM13

There is something else (that may not be related to these config): There are no output on the serial monitor at all for either board. I was able to see the serial monitor on other projects but can’t identify what are the differences.

Mmm… It seems that the “build_flags” need to be in the specific boards “env:…” sections. Otherwise it doesn’t seem to build.

As for the serial monitor, it still the same. I’ve tried monitor_dtr = 0 and monitor_rts = 0 in platformio.ini, but it still doesn’t work.

All entries in [env] will be copied over to all other environments, however, when the target environment already has the same property, the local one is used (instead of e.g. appended). I was too lazy to do it the correct (merging) way:

https://docs.platformio.org/en/latest/projectconf/interpolation.html