Now let's continue with the ESP-IDF learning, starting with GPIO.
Using GPIO in ESP-IDF first requires initializing the GPIO port, which involves the gpio_config() function (referred to as API in the guide).
To use the gpio_config() function, you need to configure the gpio_mode_t structure.
Members of gpio_mode_t include:
-
uint64_t pin_bit_mask
GPIO pins, represented as a binary number where each bit indicates a GPIO pin (here it is written as "1ull<<x", where x represents the pin number, and 1ull is an unsigned long integer 1). -
gpio_mode_t mode
GPIO mode (including input, output, open-drain, push-pull, etc.). -
gpio_pullup_t pull_up_en
GPIO pull-up. -
gpio_pulldown_t pull_down_en
GPIO pull-down. -
gpio_int_type_t intr_type
Whether the GPIO triggers an interrupt.
After configuring, call gpio_config() to complete the GPIO initialization.
Then we use gpio_get_level() to read the input pin level and gpio_set_level() to change the output pin level.
Below is the actual operation, along with how to create a new project.
First, we open the new project wizard.
How to choose the project location, name, and development board; here I am using ESP-WROOM-32, just select ESP-WROVER-KIT 3.3V.
Then directly click this button in the lower right corner.
Just select the template, which is an empty project.
After completing the creation, we find that the project did not open automatically, so we need to click to open the folder and find the created project folder.
Open the main folder and the main.c file, and now we write the code.
Here I wrote a simple button control code for the onboard LED. You can see that I called the freertos library; you don't need to worry about it, I just used its delay function for button debouncing.
Next is the compilation. Before that, we need to import the vscode configuration file by searching and selecting in the command bar.
Then, first clear the compilation data, and only then click the compile button on the right.
After the compilation is complete, we select the correct port and flash it. I am using CH340 USB to serial, so just select UART serial for flashing.
Below is the final implementation effect:
So that's it for GPIO. What? Are there other GPIO functions (APIs)? Ha, we won't learn those.