Today we are going to see ESP32-IDF getting started.
ESP32-IDF getting started
To develop applications for ESP32 you need:
- PC loaded with either Windows, Linux or Mac operating system
- Toolchain to build the Application for ESP32
- ESP-IDF essentially contains API for ESP32 and scripts to operate the Toolchain
- A text editor to write programs (Projects) in C, e.g. Eclipse
- ESP32 board itself
For setting up the above things please refer Here.
Hello World
Esp-IDF uses FreeRTOS as an operating system. If we want to work in esp-idf, we should have knowledge on How to write a program using FreeRTOS’s API. For FreeRTOS, I will give some separate tutorials later. Whenever i’m using some FreeRTOS API that time itself I will explain that API. So don’t worry guys. If you want to learn basic concepts of RTOS Part 1 please click here. For RTOS Part 2 please click here.
Now we are going to see the first example “hello world”. Hello world is the first program for programming, who wants to learn any programming language. So we are also going to see that.
Before that, we have to look in the ESP directory. So the directory contains below Subdirectory.
+–esp-idf
|
+ – – components
|
+ – – docs
|
+ – – examples
|
+ – – make
|
+ – – tools
The components directory holds all the ‘C’ code for the ESP32. It contains all the ‘components’ that make up the ESP32. It includes Drivers for numerous peripherals, the bootloader, BT (Bluetooth), freeRTOS, etc.
Include Section
#include <stdio.h> #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "esp_system.h"
- freertos/FreeRTOS.h : Inclusion of this sets configuration required to run freeRTOS on ESP32.
- freertos/task.h: This file includes task related things such as multitasking, task creating, deleting.
- esp_system.h: This inclusion configures the peripherals in the ESP system. Think of it as system initialization. It’s like setting up the vessels before cooking!
Main Function
So, In ESP-IDF program starts with app_main. It is like main fuction in other platforms. Printf will print the strings in serial console.
void app_main() { printf("Hello world....\n"); vTaskDelay(1000 / portTICK_PERIOD_MS); for(int i = 10; i>0; i--) { printf("Module restarts in %d seconds...\n", i); vTaskDelay(1000 / portTICK_PERIOD_MS); } printf("Restarting....\n"); fflush(stdout); esp_restart(); }
esp_restart():
This function restarts the esp. So again app_main will print. It’s like while(1).
Full Code
Here we go. Go through the code.
#include <stdio.h> #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "esp_system.h" void app_main() { printf("Hello world....\n"); vTaskDelay(1000 / portTICK_PERIOD_MS); for(int i = 10; i>0; i--) { printf("Module restarts in %d seconds...\n", i); vTaskDelay(1000 / portTICK_PERIOD_MS); } printf("Restarting....\n"); fflush(stdout); esp_restart(); }
MULTITASK: LED BLINKING WITH HELLO WORLD
It is an RTOS. So we can create multiple tasks. Using that method, I am going to create the two tasks.
- LED task (It will blink the LED)
- Hello world task (It prints in serial console)
Before that, FreeRTOS has many APIs. Here we are going to use API for Task creating.
xTaskCreate() API:
This FreeRTOS API is used to create a task. Using this API we can create more tasks.
portBASE_TYPE xTaskCreate ( pdTASK_CODE pvTaskCode,
const signed portCHAR * const pcName,
unsigned portSHORT usStackDepth,
void *pvParameters,
unsigned portBASE_TYPE uxPriority,
xTaskHandle *pxCreatedTask );
- pvTaskCode: a pointer to the function where the task is implemented. (Address of the function)
- pcName: given name to the task. This is useless to FreeRTOS but is intended for debugging purposes only.
- usStackDepth: length of the stack for this task in words. The actual size of the stack depends on the microcontroller.
- pvParameters: a pointer to arguments given to the task.
- uxPriority: priority given to the task, a number between 0 and MAX_PRIORITIES – 1.
- pxCreatedTask: a pointer to an identifier that allows handling the task. If the task does not have to be handled in the future, this can be left NULL.
VtaskDelay() API:
We are using this API for delay purpose.
gpio_pad_select_gpio():
This API is used to select the GPIO to use. Here we are selecting GPIO13.
gpio_set_direction():
This function is used to set the GPIO to weather output or input.
gpio_set_level():
If I select that GPIO as an output, We can set the level of the GPIO using this function.
1 – High
0 – Low
CODE
#include <stdio.h> #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "driver/gpio.h" #include "sdkconfig.h" #define BLINK_GPIO 13 void blink_task(void *pvParameter) { 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); } } void print_task(void *pvParameter) { while(1) { printf("Hello world.... Welcome to www.embetronicx.com\n"); vTaskDelay(1000 / portTICK_PERIOD_MS); } } void app_main() { xTaskCreate(&blink_task, "blink_task", 512, NULL, 5, NULL); xTaskCreate(&print_task, "print_task", 2048, NULL, 5, NULL); }
Output
Here I haven’t connected the LED. But if you connect the LED it will blink.
Please let us know if you have any doubt by commenting below. Thank you :-).
Embedded Software | Firmware | Linux Devic Deriver | RTOS
Hi, I’m SLR. I am a tech blogger and an Embedded Engineer. I am always eager to learn and explore tech-related concepts. And also, I wanted to share my knowledge with everyone in a more straightforward way with easy practical examples. I strongly believe that learning by doing is more powerful than just learning by reading. I love to do experiments. If you want to help or support me on my journey, consider sharing my articles, or Buy me a Coffee! Thank you for reading my blog! Happy learning!