]> granicus.if.org Git - esp-idf/blob - components/esp32/ets_timer_legacy.c
Merge branch 'feature/btdm_ble_spp_server_demo' into 'master'
[esp-idf] / components / esp32 / ets_timer_legacy.c
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 "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"
36
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)
39
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.
42  */
43 #define TIMER_INITIALIZED_FIELD(p_ets_timer) ((p_ets_timer)->timer_expire)
44 #define TIMER_INITIALIZED_VAL 0x12121212
45
46 static bool timer_initialized(ETSTimer *ptimer)
47 {
48     return TIMER_INITIALIZED_FIELD(ptimer) == TIMER_INITIALIZED_VAL;
49 }
50
51 void ets_timer_setfn(ETSTimer *ptimer, ETSTimerFunc *pfunction, void *parg)
52 {
53     if (!timer_initialized(ptimer)) {
54         memset(ptimer, 0, sizeof(*ptimer));
55         TIMER_INITIALIZED_FIELD(ptimer) = TIMER_INITIALIZED_VAL;
56     }
57
58     if (ESP_TIMER(ptimer) == NULL) {
59         const esp_timer_create_args_t create_args = {
60                 .callback = pfunction,
61                 .arg = parg,
62                 .name = "ETSTimer",
63                 .dispatch_method = ESP_TIMER_TASK
64         };
65
66         ESP_ERROR_CHECK( esp_timer_create(&create_args, (esp_timer_handle_t*)&(ptimer->timer_arg)) );
67     }
68 }
69
70
71 void ets_timer_arm_us(ETSTimer *ptimer, uint32_t time_us, bool repeat_flag)
72 {
73     assert(timer_initialized(ptimer));
74     esp_timer_stop(ESP_TIMER(ptimer));  // no error check
75     if (!repeat_flag) {
76         ESP_ERROR_CHECK( esp_timer_start_once(ESP_TIMER(ptimer), time_us) );
77     } else {
78         ESP_ERROR_CHECK( esp_timer_start_periodic(ESP_TIMER(ptimer), time_us) );
79     }
80 }
81
82 void ets_timer_arm(ETSTimer *ptimer, uint32_t time_ms, bool repeat_flag)
83 {
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
87     if (!repeat_flag) {
88         ESP_ERROR_CHECK( esp_timer_start_once(ESP_TIMER(ptimer), time_us) );
89     } else {
90         ESP_ERROR_CHECK( esp_timer_start_periodic(ESP_TIMER(ptimer), time_us) );
91     }
92 }
93
94 void ets_timer_done(ETSTimer *ptimer)
95 {
96     if (timer_initialized(ptimer)) {
97         esp_timer_delete(ESP_TIMER(ptimer));
98         ptimer->timer_arg = NULL;
99         TIMER_INITIALIZED_FIELD(ptimer) = 0;
100     }
101 }
102
103 void ets_timer_disarm(ETSTimer *ptimer)
104 {
105     if (timer_initialized(ptimer)) {
106         esp_timer_stop(ESP_TIMER(ptimer));
107     }
108 }
109
110
111 void ets_timer_init(void)
112 {
113
114 }
115
116 void ets_timer_deinit(void)
117 {
118
119 }
120
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")));
126