Roniere

Introductory code for ESP32 Programming

Sep 18th, 2021
655
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* 1o - Reference Libraries for code development */
  2. // Adds references for library call (printf/scanf)
  3. #include <stdio.h>
  4. #include <stdbool.h>
  5. // Adds references for SDK settings
  6. #include "sdkconfig.h"
  7. // Adds FreeRTOS resources for treat management (using delay)
  8. #include "freertos/FreeRTOS.h"
  9. #include "freertos/task.h"
  10. // Adds resources for ESP32 system information references  
  11. #include "esp_system.h"
  12. // Adds resources to view flash information (SPI)
  13. #include "esp_spi_flash.h"
  14. // Adds  resources to manipulate input and output signals
  15. #include "driver/gpio.h"
  16. // Adding timer driver library of Espressif for ESP32
  17. #include "driver/timer.h"
  18.  
  19. /* 2o - Defines for data reference to be used in program */
  20. // Understands "define" as "nickname" - define nickname original
  21. //#define TRUE 1
  22. #define LED_BOARD   GPIO_NUM_2
  23. #define BUTTON_1    GPIO_NUM_22
  24. #define CONTROL_LED GPIO_NUM_21
  25.  
  26. #define TIMER_DIVIDER (16)                              // hardware timer clock divider
  27. #define TIMER_SCALE   (TIMER_BASE_CLK / TIMER_DIVIDER)  // converts counter value in seconds
  28. // timer_base_clk is 80 Mhz by default
  29.  
  30. /* 3o - Global variables (avoid if possible, but use with care) */
  31. // -> uint32_t => unsigned int 32 bits - integer variable, unsigned, with 32 bits dimension
  32. uint32_t counter = 0;
  33. bool control_led = false;
  34.  
  35. // Data reference structure of timer
  36. typedef struct  esp_timer_info_t {
  37.     int timer_group;
  38.     int timer_idx;
  39.     int alarm_interval; //
  40.     bool auto_reload;
  41. } esp_timer_info_t;
  42.  
  43. // Reference structure for timer events ("optional")
  44. typedef struct esp_timer_event_t {
  45.     esp_timer_info_t info;
  46.     uint64_t timer_counter_value;
  47. } esp_timer_event_t;
  48.  
  49. /* 4o - Functions prototype presents in code (when not used .h a part*/
  50.  
  51.  
  52. /* 5o - Method and treat implementation */
  53.  
  54. static void IRAM_ATTR gpio_isr_handler(void *arg) // IRAN_ATTR -> this function is allocated in RAM
  55. {
  56.     // verify if "BUTTON_1" was an interrupt source
  57.     // 1 - How do it develop debounce for interrupt?
  58.     if(BUTTON_1 == (uint32_t) arg) // typecast for type uint32_t - integer used in gpio definition
  59.     {
  60.         if(gpio_get_level((uint32_t) arg) == 0)
  61.         {
  62.             counter++;
  63.         }
  64.     }
  65. }
  66.  
  67. // Callback ofr time interrupt treatment
  68. static bool IRAM_ATTR timer_group_isr_callback(void *args)
  69. {
  70.     BaseType_t high_task_awoken = pdFALSE;
  71.     esp_timer_info_t *info = (esp_timer_info_t *) args;
  72.  
  73.     uint64_t timer_counter_value = timer_group_get_counter_value_in_isr(info->timer_group, info->timer_idx);
  74.  
  75.     // Load time event values = we can use this later! Per hour is just reference
  76.     esp_timer_event_t evt = {
  77.         .info.timer_group = info->timer_group,
  78.         .info.timer_idx = info->timer_idx,
  79.         .info.auto_reload = info->auto_reload,
  80.         .info.alarm_interval = info->alarm_interval,
  81.         .timer_counter_value = timer_counter_value
  82.     };
  83.  
  84.     // If we don't have auto_reload set
  85.     if(!info->auto_reload)
  86.     {
  87.         timer_counter_value += info->alarm_interval * TIMER_SCALE;
  88.         timer_group_set_alarm_value_in_isr(info->timer_group, info->timer_idx, timer_counter_value);
  89.     }
  90.  
  91.     // Blink led in interrupt occurrence -- ACTION  in certain time (or amount of events) desired
  92.     gpio_set_direction(CONTROL_LED, control_led);
  93.     control_led = !control_led; // toggle led state to the next times
  94.  
  95.     return high_task_awoken == pdTRUE;
  96. }
  97.  
  98. // code main  execution  routine of timer
  99. void app_main() // Uses the Application CPU
  100. {
  101.     // Shows message on terminal
  102.     printf("Initialing Warm Up ESP32... \n");
  103.  
  104.     // Get information of our chip:
  105.     // We create a type to store information
  106.     esp_chip_info_t chip_info;
  107.     // We charge information
  108.     esp_chip_info(&chip_info);
  109.  
  110.     printf("ESP32 - %s with %d CPU Cores - WiFi %s %s \n", // %s -> string / %d-> integer
  111.             CONFIG_IDF_TARGET,
  112.             chip_info.cores,
  113.             (chip_info.features & CHIP_FEATURE_BT) ? "/BT" :  "",
  114.             (chip_info.features & CHIP_FEATURE_BLE) ? "/BLE" : "");
  115.            
  116.         printf("Silicon Revision: %d \n", chip_info.revision);
  117.  
  118.         printf("Flash: %d MB %s \n",
  119.                 (spi_flash_get_chip_size() / (1024*1024)),
  120.                 (chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external");
  121.        
  122.         // Lets config the LED (GPIO 2)
  123. /*      
  124.         "Discrete Method -> of GPIO setting"
  125.         gpio_reset_pin(LED_BOARD);
  126.         gpio_set_direction(LED_BOARD, GPIO_MODE_OUTPUT); */
  127.  
  128.         // All that was done until here will be performed a only one time when the microcontroller start
  129.  
  130.         // Continuous perform loop - it will be perform up to we use break, or turn off chip
  131.  
  132.  
  133. //      Pin Setting Method using "gpio_config_t"
  134. //      Firstly we set button
  135. /*         gpio_config_t button_config = {
  136.             .intr_type = GPIO_INTR_DISABLE, // No Interrupts
  137.             .mode = GPIO_MODE_INPUT,
  138.             .pin_bit_mask = (1ULL << BUTTON_1), // ULL -> Unsigned Long Long
  139.             .pull_down_en = false, // Enables pull-down
  140.             .pull_up_en   = true  // Disable pull-up
  141.         }; */
  142.  
  143.         gpio_config_t button_config = {
  144.             .intr_type = GPIO_INTR_NEGEDGE, // Interrupts in falling edge 1 -> 0
  145.             .mode = GPIO_MODE_INPUT,
  146.             .pin_bit_mask = (1ULL << BUTTON_1), // ULL -> Unsigned Long Long
  147.             .pull_down_en = false, // Enables pull-down
  148.             .pull_up_en   = true  // Disable pull-up
  149.         };
  150.  
  151. //      GPIO Setting:
  152.         gpio_config(&button_config);
  153.  
  154. //      Setting Led
  155.         gpio_config_t led_config = {
  156.             .intr_type = GPIO_INTR_DISABLE, // No Interrupts
  157.             .mode = GPIO_MODE_OUTPUT,
  158.             .pin_bit_mask = (1ULL << LED_BOARD) | (1ULL << CONTROL_LED), // ULL -> Unsigned Long Long
  159.             .pull_up_en   = false  // Disable pull-up
  160.         };
  161. //      LED Setting:
  162.         gpio_config(&led_config);
  163.  
  164.         gpio_install_isr_service(ESP_INTR_FLAG_LEVEL1); // Setting a interrupt treat routine of low priority
  165.         gpio_isr_handler_add(BUTTON_1, gpio_isr_handler, (void*) BUTTON_1);
  166.  
  167.         timer_config_t config = {
  168.             .divider = TIMER_DIVIDER,      // scale factor - divider by 16
  169.             .counter_dir = TIMER_COUNT_UP, // timer as rising counter
  170.             .counter_en = TIMER_PAUSE,     // timer starts stop
  171.             .alarm_en = TIMER_ALARM_EN,    // timer as alarm (a.k.a interrupts)
  172.             .auto_reload = true            // we want auto-reload ! For while
  173.         };
  174.         timer_init(TIMER_GROUP_0, TIMER_0, &config);
  175.  
  176.         uint32_t intervalo_em_segundos = 5;
  177.  
  178.         timer_set_counter_value(TIMER_GROUP_0, TIMER_0, 0); // starts counting in 0
  179.         timer_enable_intr(TIMER_GROUP_0, TIMER_0);          // enables timer interrupt
  180.  
  181.         // calloc - allocates memory for a elements vector and initializes everything in 0
  182.         esp_timer_info_t *timer_info = calloc(1, sizeof(esp_timer_event_t));
  183.         timer_info->timer_group = TIMER_GROUP_0;
  184.         timer_info->timer_idx = TIMER_0;
  185.         timer_info->auto_reload = true;
  186.  
  187.         // 5 seconds to start alarms and interrup generation
  188.         timer_info->alarm_interval = intervalo_em_segundos;
  189.         timer_isr_callback_add(TIMER_GROUP_0, TIMER_0, timer_group_isr_callback, timer_info, 0);
  190.  
  191.         // At moment EVERYTHING was perfumary and setup ritual
  192.         // From this exactly moment, TIMER starts to count independent of CPU, and generate interrupts so on
  193.         // As configured
  194.         timer_start(TIMER_GROUP_0, TIMER_0);
  195.  
  196.         //bool previous_button_1 = false;
  197.         bool led_state = false;
  198.  
  199.         while(true)
  200.         {
  201.             vTaskDelay(1000 / portTICK_PERIOD_MS);
  202.             printf("Counter Value: %d\n", counter);
  203.             gpio_set_level(LED_BOARD, led_state);
  204.  
  205.             led_state = !led_state; // 0 -> 1 , 1 -> 0, 0 -> 1 ...
  206.         }
  207.  
  208. /*         while(true)
  209.         {
  210.             vTaskDelay(30 / portTICK_PERIOD_MS);
  211.           if((previous_Button_1 == true) && (gpio_get_level(BUTTON_1) == false))
  212.             {
  213.                 counter++;
  214.             }
  215.  
  216.             printf("Counter Value: %d\n", counter);
  217.            
  218.             //previous_Button_1 = gpio_get_level(BUTTON_1);
  219.         }*/
  220.  
  221. /*     while(TRUE)
  222.     {
  223.         printf("------------------------");
  224.         printf("Counter: %d \n", counter);
  225.  
  226.         // Turn off led?
  227.         printf("Turning off led...");
  228.         gpio_set_level(LED_BOARD, 0);
  229.         vTaskDelay(1000/ portTICK_PERIOD_MS); // wait 1000 ms
  230.  
  231.         // Turn on led?
  232.         printf("Turning on led...");
  233.         gpio_set_level(LED_BOARD, 1);
  234.         vTaskDelay(1000/ portTICK_PERIOD_MS); // wait 1000 ms
  235.  
  236.         counter++; // Increments "counter" each... 2 seconds! - counter++ -> counter = counter+1
  237.         if(counter == 4294967295)
  238.         {
  239.             counter = 0;
  240.         }
  241.     } */
  242. }
RAW Paste Data