The Espressif Internet Development Framework (ESP-IDF) uses FreeRTOS to make better use of the two high speed processors and manage the numerous built-in peripherals. It is done by creating tasks. Let's look at the hello world, that looks a little different from the ones that you might have seen.

This hello world prints the string on UART (eventually on the computer terminal). We will first look at the ESP-IDF structure. Then look at the hello world example and modify it blink an LED while still continuously printing the 'hello world' string.

  1.  
  2. esp-idf
  3. |
  4. |
  5. + - - components
  6. |
  7. + - - docs
  8. |
  9. + - - examples
  10. |
  11. + - - make
  12. |
  13. + - - 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. If we expand the components the tree looks like so:

  1. +---components
  2. | +---app_update
  3. | | | component.mk
  4. | | | esp_ota_ops.c
  5. | | |
  6. | | \---include
  7. | | esp_ota_ops.h
  8. | |
  9. | +---bootloader
  10. | | | component.mk
  11. | | | README.rst
  12. | | |
  13. | | +---include
  14. | | | esp_image_format.h
  15. | | | esp_secure_boot.h
  16. | | |
  17. | | +---include_priv
  18. | | | bootloader_flash.h
  19. | | |
  20. | | \---src
  21. | | bootloader_flash.c
  22. | | esp_image_format.c
  23. | | secure_boot.c
  24. | | secure_boot_signatures.c
  25. | |
  26. | +---bt
  27. | | | bt.c
  28. . .
  29. | |
  30. | +---driver
  31. | | | component.mk
  32. | | | gpio.c
  33. . .
  34. | |
  35. | +---esp32
  36. | | | abi.cpp
  37. . .
  38. | |
  39. | +---esptool_py
  40. | | | Kconfig.projbuild
  41. . .
  42. | |
  43. | +---ethernet
  44. | | | component.mk
  45. . .
  46. | |
  47. | +---expat
  48. . .
  49. | |
  50. | +---freertos
  51. | | | component.mk
  52. . .
  53. | |
  54. | +---idf_test
  55. . .
  56. | |
  57. | +---newlib
  58. . .
  59. | |
  60. | +---nghttp
  61. . .
  62. | |
  63. | +---nvs_flash
  64. | | | .gitignore
  65. | |
  66. . .
  67. | |
  68. | +---openssl
  69. | |
  70. . .
  71. | |
  72. | +---partition_table
  73. . .
  74. | |
  75. | +---spi_flash
  76. | |
  77. . .
  78. | |
  79. | +---tcpip_adapter
  80. . .
  81. | |
  82. | +---ulp
  83. | |
  84. . .
  85. | |
  86. | +---vfs
  87. | |
  88. . .
  89. | +---wpa_supplicant
  90. | |
  91. | \---xtensa-debug-module
  92. | | component.mk
  93. . .
  94. |
  95. |
  96. +--- docs
  1. /* Hello World Example
  2.  
  3. This example code is in the Public Domain (or CC0 licensed, at your option.)
  4.  
  5. Unless required by applicable law or agreed to in writing, this
  6. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  7. CONDITIONS OF ANY KIND, either express or implied.
  8. */
  9. #include <stdio.h>
  10. #include "freertos/FreeRTOS.h"
  11. #include "freertos/task.h"
  12. #include "esp_system.h"
  13. #include "nvs_flash.h"
  14.  
  15. void hello_task(void *pvParameter)
  16. {
  17. printf("Hello world!\n");
  18. for (int i = 10; i >= 0; i--) {
  19. printf("Restarting in %d seconds...\n", i);
  20. vTaskDelay(1000 / portTICK_RATE_MS);
  21. }
  22. printf("Restarting now.\n");
  23. fflush(stdout);
  24. esp_restart();
  25. }
  26.  
  27. void app_main()
  28. {
  29. nvs_flash_init();
  30. xTaskCreate(&hello_task, "hello_task", 2048, NULL, 5, NULL);
  31. }