PlatformIO Community

ESP32: blink - won't compile under pio

hello,
an attempt to compile the “blink” example supplied by expressif is failing under pio.
steps to reproduce:


[az@dell5000 blink_pio]$ ~/.platformio/penv/bin/pio init

The current working directory /home/az/Work/Arduino/blink_pio will be used for the project.

The next files/directories have been created in /home/az/Work/Arduino/blink_pio
include - Put project header files here
lib - Put here project specific (private) libraries
src - Put project source files here
platformio.ini - Project Configuration File

Project has been successfully initialized! Useful commands:
`pio run` - process/build project from the current directory
`pio run --target upload` or `pio run -t upload` - upload firmware to a target
`pio run --target clean` - clean project (remove compiled files)
`pio run --help` - additional information
[az@dell5000 blink_pio]$ 
[az@dell5000 blink_pio]$ ls -la ./
total 44
drwxrwxr-x 8 az az 4096 Sep  6 20:58 .
drwxrwxr-x 5 az az 4096 Sep  6 20:58 ..
-rw-rw-r-- 1 az az   94 Sep  6 20:58 .gitignore
drwxrwxr-x 2 az az 4096 Sep  6 20:58 include
drwxrwxr-x 2 az az 4096 Sep  6 20:58 lib
drwxrwxr-x 4 az az 4096 Sep  6 21:00 .pio
-rw-rw-r-- 1 az az  483 Sep  6 20:59 platformio.ini
drwxrwxr-x 2 az az 4096 Sep  6 21:00 src
drwxrwxr-x 2 az az 4096 Sep  6 20:58 test
-rw-rw-r-- 1 az az 1557 Sep  6 20:58 .travis.yml
drwxrwxr-x 2 az az 4096 Sep  6 20:58 .vscode

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

[env:esp32doit-devkit-v1]
platform = espressif32
board = esp32doit-devkit-v1
framework = espidf
monitor_speed = 115200
[az@dell5000 blink_pio]$ more ./src/main.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"

/* Can use project configuration menu (idf.py menuconfig) to choose the GPIO to blink,
   or you can edit the following line and set a number here.
*/
#define BLINK_GPIO CONFIG_BLINK_GPIO


extern "C" {
    void app_main(void);
    }

void app_main(void)
{
    /* 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) */
	printf("Turning off the LED\n");
        gpio_set_level(BLINK_GPIO, 0);
        vTaskDelay(1000 / portTICK_PERIOD_MS);
        /* Blink on (output high) */
	printf("Turning on the LED\n");
        gpio_set_level(BLINK_GPIO, 1);
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
}
[az@dell5000 blink_pio]$ 

[az@dell5000 blink_pio]$ ~/.platformio/penv/bin/pio run
Processing esp32doit-devkit-v1 (platform: espressif32; board: esp32doit-devkit-v1; framework: espidf)
--------------------------------------------------------------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/espressif32/esp32doit-devkit-v1.html
PLATFORM: Espressif 32 1.9.0 > DOIT ESP32 DEVKIT V1
HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
DEBUG: Current (esp-prog) External (esp-prog, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa)
PACKAGES: toolchain-xtensa32 2.50200.80 (5.2.0), framework-espidf 3.30202.190627 (3.2.2), tool-esptoolpy 1.20600.0 (2.6.0)
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 0 compatible libraries
Scanning dependencies...
No dependencies
Compiling .pio/build/esp32doit-devkit-v1/src/main.o
Generating partitions .pio/build/esp32doit-devkit-v1/partitions.bin
Generating LD script .pio/build/esp32doit-devkit-v1/esp32_out.ld
Compiling .pio/build/esp32doit-devkit-v1/bootloader/bootloader_start.o
src/main.cpp: In function 'void app_main()':
src/main.cpp:18:20: error: 'CONFIG_BLINK_GPIO' was not declared in this scope
 #define BLINK_GPIO CONFIG_BLINK_GPIO
                    ^
src/main.cpp:33:26: note: in expansion of macro 'BLINK_GPIO'
     gpio_pad_select_gpio(BLINK_GPIO);
                          ^
*** [.pio/build/esp32doit-devkit-v1/src/main.o] Error 1
================================================================= [FAILED] Took 2.29 seconds =================================================================
[az@dell5000 blink_pio]$ 

at the same time compilation with the idf toolset works fine:

[az@dell5000 blink]$ idf.py build
Checking Python dependencies…
Python requirements from /home/az/esp-idf/requirements.txt are satisfied.
Executing action: all (aliases: build)
Running ninja in directory /home/az/Work/Arduino/blink/build

enerated /home/az/Work/Arduino/blink/build/blink.bin

Project build complete. To flash, run this command:
…/…/…/esp-idf/components/esptool_py/esptool/esptool.py -p (PORT) -b 460800 --after hard_reset write_flash --flash_mode dio --flash_size detect --flash_freq 40m 0x1000 build/bootloader/bootloader.bin 0x8000 build/partition_table/partition-table.bin 0x10000 build/blink.bin
or run ‘idf.py -p (PORT) flash’
[/code]

adding ```
#define CONFIG_BLINK_GPIO 5
to the src/sdkonfig.h leads to

[az@dell5000 blink_pio]$ ~/.platformio/penv/bin/pio run
Processing esp32doit-devkit-v1 (platform: espressif32; board: esp32doit-devkit-v1; framework: espidf)
--------------------------------------------------------------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/espressif32/esp32doit-devkit-v1.html
PLATFORM: Espressif 32 1.9.0 > DOIT ESP32 DEVKIT V1
HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
DEBUG: Current (esp-prog) External (esp-prog, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa)
PACKAGES: toolchain-xtensa32 2.50200.80 (5.2.0), framework-espidf 3.30202.190627 (3.2.2), tool-esptoolpy 1.20600.0 (2.6.0)
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 0 compatible libraries
Scanning dependencies...
No dependencies
Compiling .pio/build/esp32doit-devkit-v1/src/main.o
Compiling .pio/build/esp32doit-devkit-v1/bootloader/bootloader_start.o
src/main.cpp: In function 'void app_main()':
src/main.cpp:35:52: error: invalid conversion from 'int' to 'gpio_num_t' [-fpermissive]
     gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
                                                    ^
In file included from /home/az/.platformio/packages/framework-espidf/components/esp32/include/esp_sleep.h:19:0,
                 from /home/az/.platformio/packages/framework-espidf/components/esp32/include/esp_system.h:21,
                 from /home/az/.platformio/packages/framework-espidf/components/freertos/include/freertos/portable.h:126,
                 from /home/az/.platformio/packages/framework-espidf/components/freertos/include/freertos/FreeRTOS.h:105,
                 from src/main.cpp:10:
/home/az/.platformio/packages/framework-espidf/components/driver/include/driver/gpio.h:344:11: note:   initializing argument 1 of 'esp_err_t gpio_set_direction(gpio_num_t, gpio_mode_t)'
 esp_err_t gpio_set_direction(gpio_num_t gpio_num, gpio_mode_t mode);
           ^
src/main.cpp:39:37: error: invalid conversion from 'int' to 'gpio_num_t' [-fpermissive]
         gpio_set_level(BLINK_GPIO, 0);
                                     ^
In file included from /home/az/.platformio/packages/framework-espidf/components/esp32/include/esp_sleep.h:19:0,
                 from /home/az/.platformio/packages/framework-espidf/components/esp32/include/esp_system.h:21,
                 from /home/az/.platformio/packages/framework-espidf/components/freertos/include/freertos/portable.h:126,
                 from /home/az/.platformio/packages/framework-espidf/components/freertos/include/freertos/FreeRTOS.h:105,
                 from src/main.cpp:10:
/home/az/.platformio/packages/framework-espidf/components/driver/include/driver/gpio.h:315:11: note:   initializing argument 1 of 'esp_err_t gpio_set_level(gpio_num_t, uint32_t)'
 esp_err_t gpio_set_level(gpio_num_t gpio_num, uint32_t level);

The second error you have is nearer to where you should be… but you can’t just specify an integer value for the GPIO, you need to give a gpio_num_t type value… so GPIO 2 is GPIO_NUM_2, GPIO 16 is GPIO_NUM_16, etc. So either do #define BLINK_GPIO GPIO_NUM_2 in your main.cpp or leave your main.cpp as #define BLINK_GPIO CONFIG_BLINK_GPIO and set the following in your platformio.ini

build_flags=
    -D CONFIG_BLINK_GPIO=GPIO_NUM_2

to set the value for CONFIG_BLINK_GPIO.

1 Like

This macro is undefined, just as the error says.

Yeah because you did not include the definition of CONFIG_BLINK_GPIO from the espressif example which is handled via KConfig in ESP-IDF (https://github.com/espressif/esp-idf/blob/master/examples/get-started/blink/main/Kconfig.projbuild).

Why did you not add the build flag for it, as it is shown in the official example?

1 Like

Thank you. @maxgerhardt and @pfeerick your input were very helpful!.