1 // Copyright 2010-2017 Espressif Systems (Shanghai) PTE LTD
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
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
23 #include "esp_types.h"
26 #include "esp_intr_alloc.h"
27 #include "rom/ets_sys.h"
28 #include "soc/frc_timer_reg.h"
29 #include "freertos/FreeRTOS.h"
30 #include "freertos/task.h"
31 #include "freertos/semphr.h"
32 #include "freertos/xtensa_api.h"
33 #include "sdkconfig.h"
34 #include "esp_timer.h"
35 #include "esp_timer_impl.h"
37 /* We abuse 'timer_arg' field of ETSTimer structure to hold a pointer to esp_timer */
38 #define ESP_TIMER(p_ets_timer) ((esp_timer_handle_t) (p_ets_timer)->timer_arg)
40 /* We abuse 'timer_expire' field of ETSTimer structure to hold a magic value
41 * signifying that the contents of the timer was zeroed out.
43 #define TIMER_INITIALIZED_FIELD(p_ets_timer) ((p_ets_timer)->timer_expire)
44 #define TIMER_INITIALIZED_VAL 0x12121212
46 static IRAM_ATTR bool timer_initialized(ETSTimer *ptimer)
48 return TIMER_INITIALIZED_FIELD(ptimer) == TIMER_INITIALIZED_VAL;
51 void ets_timer_setfn(ETSTimer *ptimer, ETSTimerFunc *pfunction, void *parg)
53 if (!timer_initialized(ptimer)) {
54 memset(ptimer, 0, sizeof(*ptimer));
55 TIMER_INITIALIZED_FIELD(ptimer) = TIMER_INITIALIZED_VAL;
58 if (ESP_TIMER(ptimer) == NULL) {
59 const esp_timer_create_args_t create_args = {
60 .callback = pfunction,
63 .dispatch_method = ESP_TIMER_TASK
66 ESP_ERROR_CHECK( esp_timer_create(&create_args, (esp_timer_handle_t*)&(ptimer->timer_arg)) );
71 void IRAM_ATTR ets_timer_arm_us(ETSTimer *ptimer, uint32_t time_us, bool repeat_flag)
73 assert(timer_initialized(ptimer));
74 esp_timer_stop(ESP_TIMER(ptimer)); // no error check
76 ESP_ERROR_CHECK( esp_timer_start_once(ESP_TIMER(ptimer), time_us) );
78 ESP_ERROR_CHECK( esp_timer_start_periodic(ESP_TIMER(ptimer), time_us) );
82 void IRAM_ATTR ets_timer_arm(ETSTimer *ptimer, uint32_t time_ms, bool repeat_flag)
84 uint64_t time_us = 1000LL * (uint64_t) time_ms;
85 assert(timer_initialized(ptimer));
86 esp_timer_stop(ESP_TIMER(ptimer)); // no error check
88 ESP_ERROR_CHECK( esp_timer_start_once(ESP_TIMER(ptimer), time_us) );
90 ESP_ERROR_CHECK( esp_timer_start_periodic(ESP_TIMER(ptimer), time_us) );
94 void ets_timer_done(ETSTimer *ptimer)
96 if (timer_initialized(ptimer)) {
97 esp_timer_delete(ESP_TIMER(ptimer));
98 ptimer->timer_arg = NULL;
99 TIMER_INITIALIZED_FIELD(ptimer) = 0;
103 void IRAM_ATTR ets_timer_disarm(ETSTimer *ptimer)
105 if (timer_initialized(ptimer)) {
106 esp_timer_stop(ESP_TIMER(ptimer));
111 void ets_timer_init(void)
116 void ets_timer_deinit(void)
121 void os_timer_setfn(ETSTimer *ptimer, ETSTimerFunc *pfunction, void *parg) __attribute__((alias("ets_timer_setfn")));
122 void os_timer_disarm(ETSTimer *ptimer) __attribute__((alias("ets_timer_disarm")));
123 void os_timer_arm_us(ETSTimer *ptimer,uint32_t u_seconds,bool repeat_flag) __attribute__((alias("ets_timer_arm_us")));
124 void os_timer_arm(ETSTimer *ptimer,uint32_t milliseconds,bool repeat_flag) __attribute__((alias("ets_timer_arm")));
125 void os_timer_done(ETSTimer *ptimer) __attribute__((alias("ets_timer_done")));