• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2010-2017 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 /*
16  * ets_timer module implements a set of legacy timer APIs which are
17  * used by the WiFi driver. This is done on top of the newer esp_timer APIs.
18  * Applications should not use ets_timer functions, as they may change without
19  * notice.
20  */
21 
22 #include <string.h>
23 #include "esp_types.h"
24 #include "esp_log.h"
25 #include "esp_attr.h"
26 #include "esp_intr_alloc.h"
27 #include "esp_osal/esp_osal.h"
28 #include "esp_osal/task.h"
29 #include "esp_osal/semphr.h"
30 #include "sdkconfig.h"
31 #include "esp_timer.h"
32 #if CONFIG_IDF_TARGET_ESP32
33 #include "esp32/rom/ets_sys.h"  // for ETSTimer type
34 #elif CONFIG_IDF_TARGET_ESP32S2
35 #include "esp32s2/rom/ets_sys.h"
36 #elif CONFIG_IDF_TARGET_ESP32S3
37 #include "esp32s3/rom/ets_sys.h"
38 #endif
39 
40 /* We abuse 'timer_arg' field of ETSTimer structure to hold a pointer to esp_timer */
41 #define ESP_TIMER(p_ets_timer) ((esp_timer_handle_t) (p_ets_timer)->timer_arg)
42 
43 /* We abuse 'timer_expire' field of ETSTimer structure to hold a magic value
44  * signifying that the contents of the timer was zeroed out.
45  */
46 #define TIMER_INITIALIZED_FIELD(p_ets_timer) ((p_ets_timer)->timer_expire)
47 #define TIMER_INITIALIZED_VAL 0x12121212
48 
timer_initialized(ETSTimer * ptimer)49 static IRAM_ATTR bool timer_initialized(ETSTimer *ptimer)
50 {
51     return TIMER_INITIALIZED_FIELD(ptimer) == TIMER_INITIALIZED_VAL;
52 }
53 
ets_timer_setfn(ETSTimer * ptimer,ETSTimerFunc * pfunction,void * parg)54 void ets_timer_setfn(ETSTimer *ptimer, ETSTimerFunc *pfunction, void *parg)
55 {
56     if (!timer_initialized(ptimer)) {
57         memset(ptimer, 0, sizeof(*ptimer));
58         TIMER_INITIALIZED_FIELD(ptimer) = TIMER_INITIALIZED_VAL;
59     }
60 
61     if (ESP_TIMER(ptimer) == NULL) {
62         const esp_timer_create_args_t create_args = {
63                 .callback = pfunction,
64                 .arg = parg,
65                 .name = "ETSTimer",
66                 .dispatch_method = ESP_TIMER_TASK
67         };
68 
69         ESP_ERROR_CHECK( esp_timer_create(&create_args, (esp_timer_handle_t*)&(ptimer->timer_arg)) );
70     }
71 }
72 
73 
ets_timer_arm_us(ETSTimer * ptimer,uint32_t time_us,bool repeat_flag)74 void IRAM_ATTR ets_timer_arm_us(ETSTimer *ptimer, uint32_t time_us, bool repeat_flag)
75 {
76     assert(timer_initialized(ptimer));
77     esp_timer_stop(ESP_TIMER(ptimer));  // no error check
78     if (!repeat_flag) {
79         ESP_ERROR_CHECK( esp_timer_start_once(ESP_TIMER(ptimer), time_us) );
80     } else {
81         ESP_ERROR_CHECK( esp_timer_start_periodic(ESP_TIMER(ptimer), time_us) );
82     }
83 }
84 
ets_timer_arm(ETSTimer * ptimer,uint32_t time_ms,bool repeat_flag)85 void IRAM_ATTR ets_timer_arm(ETSTimer *ptimer, uint32_t time_ms, bool repeat_flag)
86 {
87     uint64_t time_us = 1000LL * (uint64_t) time_ms;
88     assert(timer_initialized(ptimer));
89     esp_timer_stop(ESP_TIMER(ptimer));  // no error check
90     if (!repeat_flag) {
91         ESP_ERROR_CHECK( esp_timer_start_once(ESP_TIMER(ptimer), time_us) );
92     } else {
93         ESP_ERROR_CHECK( esp_timer_start_periodic(ESP_TIMER(ptimer), time_us) );
94     }
95 }
96 
ets_timer_done(ETSTimer * ptimer)97 void ets_timer_done(ETSTimer *ptimer)
98 {
99     if (timer_initialized(ptimer)) {
100         esp_timer_delete(ESP_TIMER(ptimer));
101         ptimer->timer_arg = NULL;
102         TIMER_INITIALIZED_FIELD(ptimer) = 0;
103     }
104 }
105 
ets_timer_disarm(ETSTimer * ptimer)106 void IRAM_ATTR ets_timer_disarm(ETSTimer *ptimer)
107 {
108     if (timer_initialized(ptimer)) {
109         esp_timer_stop(ESP_TIMER(ptimer));
110     }
111 }
112 
113 
ets_timer_init(void)114 void ets_timer_init(void)
115 {
116 
117 }
118 
ets_timer_deinit(void)119 void ets_timer_deinit(void)
120 {
121 
122 }
123 
124 void os_timer_setfn(ETSTimer *ptimer, ETSTimerFunc *pfunction, void *parg) __attribute__((alias("ets_timer_setfn")));
125 void os_timer_disarm(ETSTimer *ptimer) __attribute__((alias("ets_timer_disarm")));
126 void os_timer_arm_us(ETSTimer *ptimer,uint32_t u_seconds,bool repeat_flag) __attribute__((alias("ets_timer_arm_us")));
127 void os_timer_arm(ETSTimer *ptimer,uint32_t milliseconds,bool repeat_flag) __attribute__((alias("ets_timer_arm")));
128 void os_timer_done(ETSTimer *ptimer) __attribute__((alias("ets_timer_done")));
129