|
| 1 | +#include "freertos/FreeRTOS.h" |
| 2 | +#include "esp_wifi.h" |
| 3 | +#include "esp_system.h" |
| 4 | +#include "esp_event.h" |
| 5 | +#include "esp_event_loop.h" |
| 6 | +#include "nvs_flash.h" |
| 7 | +#include "driver/gpio.h" |
| 8 | +#include <esp_task_wdt.h> |
| 9 | + |
| 10 | +esp_err_t event_handler(void *ctx, system_event_t *event) |
| 11 | +{ |
| 12 | + return ESP_OK; |
| 13 | +} |
| 14 | + |
| 15 | +extern "C" |
| 16 | +{ |
| 17 | + void app_main(void); |
| 18 | +} |
| 19 | + |
| 20 | +void highPriorityTask(void *myData) |
| 21 | +{ |
| 22 | + printf("High priority task started and now looping for 10 seconds. Our priority is %d.\n", uxTaskPriorityGet(nullptr)); |
| 23 | + TickType_t startTicks = xTaskGetTickCount(); |
| 24 | + while (xTaskGetTickCount() - startTicks < (10 * 1000 / portTICK_PERIOD_MS)) |
| 25 | + { |
| 26 | + // Do nothing but loop |
| 27 | + } |
| 28 | + printf("High priority task ended\n"); |
| 29 | + vTaskDelete(nullptr); |
| 30 | +} |
| 31 | + |
| 32 | +void hardLoopTask(void *myData) |
| 33 | +{ |
| 34 | + printf("Hard loop task started ...\n"); |
| 35 | + while (1) |
| 36 | + { |
| 37 | + // do nothing but burn CPU |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +void hardLoopTaskNoInterrupts(void *myData) |
| 42 | +{ |
| 43 | + printf("Hard loop task disabling interrupts started ...\n"); |
| 44 | + taskDISABLE_INTERRUPTS(); |
| 45 | + while (1) |
| 46 | + { |
| 47 | + // do nothing but burn CPU |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +void myTask(void *myData) |
| 52 | +{ |
| 53 | + printf("# Running in myTask\n"); |
| 54 | + printf("# Registering our new task with the task watchdog.\n"); |
| 55 | + esp_task_wdt_add(nullptr); |
| 56 | + |
| 57 | + printf("# Looping 5 times with a delay of 1 second and not feeding the watchdog.\n"); |
| 58 | + for (int i = 0; i < 5; i++) |
| 59 | + { |
| 60 | + vTaskDelay(1000 / portTICK_PERIOD_MS); |
| 61 | + printf("Tick\n"); |
| 62 | + } |
| 63 | + |
| 64 | + printf("# Looping 5 times with a delay of 1 second and positively feeding the watchdog.\n"); |
| 65 | + esp_task_wdt_reset(); |
| 66 | + for (int i = 0; i < 5; i++) |
| 67 | + { |
| 68 | + vTaskDelay(1000 / portTICK_PERIOD_MS); |
| 69 | + printf("Tick\n"); |
| 70 | + esp_task_wdt_reset(); |
| 71 | + } |
| 72 | + |
| 73 | + printf("# Removing our watchdog registration so we can do something expensive.\n"); |
| 74 | + esp_task_wdt_delete(nullptr); |
| 75 | + |
| 76 | + printf("# Looping 5 times with a delay of 1 second and not feeding the watchdog.\n"); |
| 77 | + for (int i = 0; i < 5; i++) |
| 78 | + { |
| 79 | + vTaskDelay(1000 / portTICK_PERIOD_MS); |
| 80 | + printf("Tick\n"); |
| 81 | + } |
| 82 | + |
| 83 | + printf("# Re-registering our task with the task watchdog.\n"); |
| 84 | + esp_task_wdt_add(nullptr); |
| 85 | + printf("# Looping 5 times with a delay of 1 second and not feeding the watchdog.\n"); |
| 86 | + for (int i = 0; i < 5; i++) |
| 87 | + { |
| 88 | + vTaskDelay(1000 / portTICK_PERIOD_MS); |
| 89 | + printf("Tick\n"); |
| 90 | + } |
| 91 | + |
| 92 | + printf("# Our current task priority is %d.\n", uxTaskPriorityGet(nullptr)); |
| 93 | + printf("# Spwaning a higher priority task\n"); |
| 94 | + xTaskCreate(highPriorityTask, // Task code |
| 95 | + "Priority task", // Name of task |
| 96 | + 16 * 1024, // Stack size |
| 97 | + nullptr, // Task data |
| 98 | + 5, // Priority |
| 99 | + nullptr // task handle |
| 100 | + ); |
| 101 | + |
| 102 | + printf("# Looping 5 times with a delay of 1 second and positively feeding the watchdog.\n"); |
| 103 | + esp_task_wdt_reset(); |
| 104 | + for (int i = 0; i < 5; i++) |
| 105 | + { |
| 106 | + vTaskDelay(1000 / portTICK_PERIOD_MS); |
| 107 | + printf("Tick\n"); |
| 108 | + esp_task_wdt_reset(); |
| 109 | + } |
| 110 | + |
| 111 | + printf("Spawning a hard-loop function!\n"); |
| 112 | + xTaskCreate(hardLoopTaskNoInterrupts, // Task code |
| 113 | + "Hard Loop", // Name of task |
| 114 | + 16 * 1024, // Stack size |
| 115 | + nullptr, // Task data |
| 116 | + 5, // Priority |
| 117 | + nullptr // task handle |
| 118 | + ); |
| 119 | + |
| 120 | + printf("# Looping 5 times with a delay of 1 second and positively feeding the watchdog.\n"); |
| 121 | + esp_task_wdt_reset(); |
| 122 | + for (int i = 0; i < 5; i++) |
| 123 | + { |
| 124 | + vTaskDelay(1000 / portTICK_PERIOD_MS); |
| 125 | + printf("Tick\n"); |
| 126 | + esp_task_wdt_reset(); |
| 127 | + } |
| 128 | + |
| 129 | + |
| 130 | + printf("# Removing our watchdog registration before we end the task.\n"); |
| 131 | + esp_task_wdt_delete(nullptr); |
| 132 | + |
| 133 | + printf("# Ending myTask\n"); |
| 134 | + vTaskDelete(nullptr); |
| 135 | +} // myTask |
| 136 | + |
| 137 | +void app_main(void) |
| 138 | +{ |
| 139 | + xTaskHandle handle; |
| 140 | + printf("App starting\n"); |
| 141 | + printf("Initializing the task watchdog subsystem with an interval of 2 seconds.\n"); |
| 142 | + esp_task_wdt_init(2, false); |
| 143 | + |
| 144 | + printf("Creatign a new task.\n"); |
| 145 | + // Now let us create a new task. |
| 146 | + xTaskCreate(myTask, // Task code |
| 147 | + "My Task", // Name of task |
| 148 | + 16 * 1024, // Stack size |
| 149 | + nullptr, // Task data |
| 150 | + 0, // Priority |
| 151 | + &handle // task handle |
| 152 | + ); |
| 153 | + |
| 154 | + //printf("App Ended!\n"); |
| 155 | +} // app_main |
0 commit comments