1 /*
2 * This file is part of the MicroPython project, http://micropython.org/
3 *
4 * Development of the code in this file was sponsored by Microbric Pty Ltd
5 *
6 * The MIT License (MIT)
7 *
8 * Copyright (c) 2013-2015 Damien P. George
9 * Copyright (c) 2016 Paul Sokolovsky
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 */
29
30 #include <stdint.h>
31 #include <stdio.h>
32
33 #include "py/obj.h"
34 #include "py/runtime.h"
35 #include "modmachine.h"
36 #include "mphalport.h"
37
38 #include "driver/timer.h"
39 #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 1, 1)
40 #include "hal/timer_ll.h"
41 #define HAVE_TIMER_LL (1)
42 #endif
43
44 #define TIMER_INTR_SEL TIMER_INTR_LEVEL
45 #define TIMER_DIVIDER 8
46
47 // TIMER_BASE_CLK is normally 80MHz. TIMER_DIVIDER ought to divide this exactly
48 #define TIMER_SCALE (TIMER_BASE_CLK / TIMER_DIVIDER)
49
50 #define TIMER_FLAGS 0
51
52 typedef struct _machine_timer_obj_t {
53 mp_obj_base_t base;
54 mp_uint_t group;
55 mp_uint_t index;
56
57 mp_uint_t repeat;
58 // ESP32 timers are 64-bit
59 uint64_t period;
60
61 mp_obj_t callback;
62
63 intr_handle_t handle;
64
65 struct _machine_timer_obj_t *next;
66 } machine_timer_obj_t;
67
68 const mp_obj_type_t machine_timer_type;
69
70 STATIC void machine_timer_disable(machine_timer_obj_t *self);
71
machine_timer_deinit_all(void)72 void machine_timer_deinit_all(void) {
73 // Disable, deallocate and remove all timers from list
74 machine_timer_obj_t **t = &MP_STATE_PORT(machine_timer_obj_head);
75 while (*t != NULL) {
76 machine_timer_disable(*t);
77 machine_timer_obj_t *next = (*t)->next;
78 m_del_obj(machine_timer_obj_t, *t);
79 *t = next;
80 }
81 }
82
machine_timer_print(const mp_print_t * print,mp_obj_t self_in,mp_print_kind_t kind)83 STATIC void machine_timer_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
84 machine_timer_obj_t *self = self_in;
85
86 timer_config_t config;
87 mp_printf(print, "Timer(%p; ", self);
88
89 timer_get_config(self->group, self->index, &config);
90
91 mp_printf(print, "alarm_en=%d, ", config.alarm_en);
92 mp_printf(print, "auto_reload=%d, ", config.auto_reload);
93 mp_printf(print, "counter_en=%d)", config.counter_en);
94 }
95
machine_timer_make_new(const mp_obj_type_t * type,size_t n_args,size_t n_kw,const mp_obj_t * args)96 STATIC mp_obj_t machine_timer_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
97 mp_arg_check_num(n_args, n_kw, 1, 1, false);
98 mp_uint_t group = (mp_obj_get_int(args[0]) >> 1) & 1;
99 mp_uint_t index = mp_obj_get_int(args[0]) & 1;
100
101 // Check whether the timer is already initialized, if so return it
102 for (machine_timer_obj_t *t = MP_STATE_PORT(machine_timer_obj_head); t; t = t->next) {
103 if (t->group == group && t->index == index) {
104 return t;
105 }
106 }
107
108 machine_timer_obj_t *self = m_new_obj(machine_timer_obj_t);
109 self->base.type = &machine_timer_type;
110 self->group = group;
111 self->index = index;
112
113 // Add the timer to the linked-list of timers
114 self->next = MP_STATE_PORT(machine_timer_obj_head);
115 MP_STATE_PORT(machine_timer_obj_head) = self;
116
117 return self;
118 }
119
machine_timer_disable(machine_timer_obj_t * self)120 STATIC void machine_timer_disable(machine_timer_obj_t *self) {
121 if (self->handle) {
122 timer_pause(self->group, self->index);
123 esp_intr_free(self->handle);
124 self->handle = NULL;
125 }
126
127 // We let the disabled timer stay in the list, as it might be
128 // referenced elsewhere
129 }
130
machine_timer_isr(void * self_in)131 STATIC void machine_timer_isr(void *self_in) {
132 machine_timer_obj_t *self = self_in;
133 timg_dev_t *device = self->group ? &(TIMERG1) : &(TIMERG0);
134
135 #if HAVE_TIMER_LL
136
137 #if CONFIG_IDF_TARGET_ESP32
138 device->hw_timer[self->index].update = 1;
139 #else
140 device->hw_timer[self->index].update.update = 1;
141 #endif
142 timer_ll_clear_intr_status(device, self->index);
143 timer_ll_set_alarm_enable(device, self->index, self->repeat);
144
145 #else
146
147 device->hw_timer[self->index].update = 1;
148 if (self->index) {
149 device->int_clr_timers.t1 = 1;
150 } else {
151 device->int_clr_timers.t0 = 1;
152 }
153 device->hw_timer[self->index].config.alarm_en = self->repeat;
154
155 #endif
156
157 mp_sched_schedule(self->callback, self);
158 mp_hal_wake_main_task_from_isr();
159 }
160
machine_timer_enable(machine_timer_obj_t * self)161 STATIC void machine_timer_enable(machine_timer_obj_t *self) {
162 timer_config_t config;
163 config.alarm_en = TIMER_ALARM_EN;
164 config.auto_reload = self->repeat;
165 config.counter_dir = TIMER_COUNT_UP;
166 config.divider = TIMER_DIVIDER;
167 config.intr_type = TIMER_INTR_LEVEL;
168 config.counter_en = TIMER_PAUSE;
169
170 check_esp_err(timer_init(self->group, self->index, &config));
171 check_esp_err(timer_set_counter_value(self->group, self->index, 0x00000000));
172 check_esp_err(timer_set_alarm_value(self->group, self->index, self->period));
173 check_esp_err(timer_enable_intr(self->group, self->index));
174 check_esp_err(timer_isr_register(self->group, self->index, machine_timer_isr, (void *)self, TIMER_FLAGS, &self->handle));
175 check_esp_err(timer_start(self->group, self->index));
176 }
177
machine_timer_init_helper(machine_timer_obj_t * self,mp_uint_t n_args,const mp_obj_t * pos_args,mp_map_t * kw_args)178 STATIC mp_obj_t machine_timer_init_helper(machine_timer_obj_t *self, mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
179 enum {
180 ARG_mode,
181 ARG_callback,
182 ARG_period,
183 ARG_tick_hz,
184 ARG_freq,
185 };
186 static const mp_arg_t allowed_args[] = {
187 { MP_QSTR_mode, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1} },
188 { MP_QSTR_callback, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
189 { MP_QSTR_period, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0xffffffff} },
190 { MP_QSTR_tick_hz, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1000} },
191 #if MICROPY_PY_BUILTINS_FLOAT
192 { MP_QSTR_freq, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
193 #else
194 { MP_QSTR_freq, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0xffffffff} },
195 #endif
196 };
197
198 machine_timer_disable(self);
199
200 mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
201 mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
202
203 #if MICROPY_PY_BUILTINS_FLOAT
204 if (args[ARG_freq].u_obj != mp_const_none) {
205 self->period = (uint64_t)(TIMER_SCALE / mp_obj_get_float(args[ARG_freq].u_obj));
206 }
207 #else
208 if (args[ARG_freq].u_int != 0xffffffff) {
209 self->period = TIMER_SCALE / ((uint64_t)args[ARG_freq].u_int);
210 }
211 #endif
212 else {
213 self->period = (((uint64_t)args[ARG_period].u_int) * TIMER_SCALE) / args[ARG_tick_hz].u_int;
214 }
215
216 self->repeat = args[ARG_mode].u_int;
217 self->callback = args[ARG_callback].u_obj;
218 self->handle = NULL;
219
220 machine_timer_enable(self);
221
222 return mp_const_none;
223 }
224
machine_timer_deinit(mp_obj_t self_in)225 STATIC mp_obj_t machine_timer_deinit(mp_obj_t self_in) {
226 machine_timer_disable(self_in);
227
228 return mp_const_none;
229 }
230 STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_timer_deinit_obj, machine_timer_deinit);
231
machine_timer_init(size_t n_args,const mp_obj_t * args,mp_map_t * kw_args)232 STATIC mp_obj_t machine_timer_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
233 return machine_timer_init_helper(args[0], n_args - 1, args + 1, kw_args);
234 }
235 STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_timer_init_obj, 1, machine_timer_init);
236
machine_timer_value(mp_obj_t self_in)237 STATIC mp_obj_t machine_timer_value(mp_obj_t self_in) {
238 machine_timer_obj_t *self = self_in;
239 double result;
240
241 timer_get_counter_time_sec(self->group, self->index, &result);
242
243 return MP_OBJ_NEW_SMALL_INT((mp_uint_t)(result * 1000)); // value in ms
244 }
245 STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_timer_value_obj, machine_timer_value);
246
247 STATIC const mp_rom_map_elem_t machine_timer_locals_dict_table[] = {
248 { MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&machine_timer_deinit_obj) },
249 { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&machine_timer_deinit_obj) },
250 { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_timer_init_obj) },
251 { MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&machine_timer_value_obj) },
252 { MP_ROM_QSTR(MP_QSTR_ONE_SHOT), MP_ROM_INT(false) },
253 { MP_ROM_QSTR(MP_QSTR_PERIODIC), MP_ROM_INT(true) },
254 };
255 STATIC MP_DEFINE_CONST_DICT(machine_timer_locals_dict, machine_timer_locals_dict_table);
256
257 const mp_obj_type_t machine_timer_type = {
258 { &mp_type_type },
259 .name = MP_QSTR_Timer,
260 .print = machine_timer_print,
261 .make_new = machine_timer_make_new,
262 .locals_dict = (mp_obj_t)&machine_timer_locals_dict,
263 };
264