1 /* esp_timer (high resolution timer) 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
10 #include <stdio.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include "esp_timer.h"
14 #include "esp_log.h"
15 #include "esp_sleep.h"
16 #include "sdkconfig.h"
17
18 static void periodic_timer_callback(void* arg);
19 static void oneshot_timer_callback(void* arg);
20
21 static const char* TAG = "example";
22
app_main(void)23 void app_main(void)
24 {
25 /* Create two timers:
26 * 1. a periodic timer which will run every 0.5s, and print a message
27 * 2. a one-shot timer which will fire after 5s, and re-start periodic
28 * timer with period of 1s.
29 */
30
31 const esp_timer_create_args_t periodic_timer_args = {
32 .callback = &periodic_timer_callback,
33 /* name is optional, but may help identify the timer when debugging */
34 .name = "periodic"
35 };
36
37 esp_timer_handle_t periodic_timer;
38 ESP_ERROR_CHECK(esp_timer_create(&periodic_timer_args, &periodic_timer));
39 /* The timer has been created but is not running yet */
40
41 const esp_timer_create_args_t oneshot_timer_args = {
42 .callback = &oneshot_timer_callback,
43 /* argument specified here will be passed to timer callback function */
44 .arg = (void*) periodic_timer,
45 .name = "one-shot"
46 };
47 esp_timer_handle_t oneshot_timer;
48 ESP_ERROR_CHECK(esp_timer_create(&oneshot_timer_args, &oneshot_timer));
49
50 /* Start the timers */
51 ESP_ERROR_CHECK(esp_timer_start_periodic(periodic_timer, 500000));
52 ESP_ERROR_CHECK(esp_timer_start_once(oneshot_timer, 5000000));
53 ESP_LOGI(TAG, "Started timers, time since boot: %lld us", esp_timer_get_time());
54
55 /* Print debugging information about timers to console every 2 seconds */
56 for (int i = 0; i < 5; ++i) {
57 ESP_ERROR_CHECK(esp_timer_dump(stdout));
58 usleep(2000000);
59 }
60
61 /* Timekeeping continues in light sleep, and timers are scheduled
62 * correctly after light sleep.
63 */
64 ESP_LOGI(TAG, "Entering light sleep for 0.5s, time since boot: %lld us",
65 esp_timer_get_time());
66
67 ESP_ERROR_CHECK(esp_sleep_enable_timer_wakeup(500000));
68 esp_light_sleep_start();
69
70 ESP_LOGI(TAG, "Woke up from light sleep, time since boot: %lld us",
71 esp_timer_get_time());
72
73 /* Let the timer run for a little bit more */
74 usleep(2000000);
75
76 /* Clean up and finish the example */
77 ESP_ERROR_CHECK(esp_timer_stop(periodic_timer));
78 ESP_ERROR_CHECK(esp_timer_delete(periodic_timer));
79 ESP_ERROR_CHECK(esp_timer_delete(oneshot_timer));
80 ESP_LOGI(TAG, "Stopped and deleted timers");
81 }
82
periodic_timer_callback(void * arg)83 static void periodic_timer_callback(void* arg)
84 {
85 int64_t time_since_boot = esp_timer_get_time();
86 ESP_LOGI(TAG, "Periodic timer called, time since boot: %lld us", time_since_boot);
87 }
88
oneshot_timer_callback(void * arg)89 static void oneshot_timer_callback(void* arg)
90 {
91 int64_t time_since_boot = esp_timer_get_time();
92 ESP_LOGI(TAG, "One-shot timer called, time since boot: %lld us", time_since_boot);
93 esp_timer_handle_t periodic_timer_handle = (esp_timer_handle_t) arg;
94 /* To start the timer which is running, need to stop it first */
95 ESP_ERROR_CHECK(esp_timer_stop(periodic_timer_handle));
96 ESP_ERROR_CHECK(esp_timer_start_periodic(periodic_timer_handle, 1000000));
97 time_since_boot = esp_timer_get_time();
98 ESP_LOGI(TAG, "Restarted periodic timer with 1s period, time since boot: %lld us",
99 time_since_boot);
100 }
101