Now let's continue with ESP-IDF learning, starting with GPIO.
Using GPIO in ESP-IDF 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 represents 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 at the bottom right.
Just select template, this 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.c file in the main folder, 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; this is not important, I just used its delay function for button debouncing.
Then comes the compilation. Before that, we need to import the vscode configuration file, search for and select it in the command bar.
Next, clear the compilation data first, and then click the compile button on the right.
After the compilation is complete, we select the appropriate 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 them.