blob: a4662c3c725884cc3ca28e00d98e231dc8423645 [file] [log] [blame]
Damien Georgebc08c882016-12-06 12:20:10 +11001/*
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 "driver/timer.h"
34#include "py/obj.h"
35#include "py/runtime.h"
36#include "modmachine.h"
Nicko van Someren14ab81e2018-07-01 20:27:10 -060037#include "mphalport.h"
Damien Georgebc08c882016-12-06 12:20:10 +110038
39#define TIMER_INTR_SEL TIMER_INTR_LEVEL
Nicko van Somerenc3c914f2018-06-26 15:03:51 -060040#define TIMER_DIVIDER 8
41
42// TIMER_BASE_CLK is normally 80MHz. TIMER_DIVIDER ought to divide this exactly
Damien Georgebc08c882016-12-06 12:20:10 +110043#define TIMER_SCALE (TIMER_BASE_CLK / TIMER_DIVIDER)
44
45#define TIMER_FLAGS 0
46
47typedef struct _machine_timer_obj_t {
48 mp_obj_base_t base;
49 mp_uint_t group;
50 mp_uint_t index;
51
52 mp_uint_t repeat;
Nicko van Somerenc3c914f2018-06-26 15:03:51 -060053 // ESP32 timers are 64-bit
54 uint64_t period;
Damien Georgebc08c882016-12-06 12:20:10 +110055
56 mp_obj_t callback;
57
58 intr_handle_t handle;
Damien George9ac9aa92019-01-28 16:16:03 +110059
60 struct _machine_timer_obj_t *next;
Damien Georgebc08c882016-12-06 12:20:10 +110061} machine_timer_obj_t;
62
63const mp_obj_type_t machine_timer_type;
64
Damien George9ac9aa92019-01-28 16:16:03 +110065STATIC void machine_timer_disable(machine_timer_obj_t *self);
66
Damien Georgebc08c882016-12-06 12:20:10 +110067STATIC esp_err_t check_esp_err(esp_err_t code) {
68 if (code) {
69 mp_raise_OSError(code);
70 }
71
72 return code;
73}
74
Damien George9ac9aa92019-01-28 16:16:03 +110075void machine_timer_deinit_all(void) {
Martin Dybdalde76f732019-05-24 13:19:26 +020076 // Disable, deallocate and remove all timers from list
77 machine_timer_obj_t **t = &MP_STATE_PORT(machine_timer_obj_head);
78 while (*t != NULL) {
79 machine_timer_disable(*t);
80 machine_timer_obj_t *next = (*t)->next;
81 m_del_obj(machine_timer_obj_t, *t);
82 *t = next;
Damien George9ac9aa92019-01-28 16:16:03 +110083 }
84}
85
Damien Georgebc08c882016-12-06 12:20:10 +110086STATIC void machine_timer_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
87 machine_timer_obj_t *self = self_in;
88
89 timer_config_t config;
90 mp_printf(print, "Timer(%p; ", self);
91
92 timer_get_config(self->group, self->index, &config);
93
94 mp_printf(print, "alarm_en=%d, ", config.alarm_en);
95 mp_printf(print, "auto_reload=%d, ", config.auto_reload);
96 mp_printf(print, "counter_en=%d)", config.counter_en);
97}
98
99STATIC 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) {
100 mp_arg_check_num(n_args, n_kw, 1, 1, false);
Martin Dybdalde76f732019-05-24 13:19:26 +0200101 mp_uint_t group = (mp_obj_get_int(args[0]) >> 1) & 1;
102 mp_uint_t index = mp_obj_get_int(args[0]) & 1;
103
104 // Check whether the timer is already initialized, if so return it
105 for (machine_timer_obj_t *t = MP_STATE_PORT(machine_timer_obj_head); t; t = t->next) {
106 if (t->group == group && t->index == index) {
107 return t;
108 }
109 }
Damien George69661f32020-02-27 15:36:53 +1100110
Damien Georgebc08c882016-12-06 12:20:10 +1100111 machine_timer_obj_t *self = m_new_obj(machine_timer_obj_t);
112 self->base.type = &machine_timer_type;
Martin Dybdalde76f732019-05-24 13:19:26 +0200113 self->group = group;
114 self->index = index;
Damien Georgebc08c882016-12-06 12:20:10 +1100115
Martin Dybdalde76f732019-05-24 13:19:26 +0200116 // Add the timer to the linked-list of timers
117 self->next = MP_STATE_PORT(machine_timer_obj_head);
118 MP_STATE_PORT(machine_timer_obj_head) = self;
Damien Georgebc08c882016-12-06 12:20:10 +1100119
120 return self;
121}
122
123STATIC void machine_timer_disable(machine_timer_obj_t *self) {
124 if (self->handle) {
125 timer_pause(self->group, self->index);
126 esp_intr_free(self->handle);
127 self->handle = NULL;
128 }
Damien George9ac9aa92019-01-28 16:16:03 +1100129
Martin Dybdalde76f732019-05-24 13:19:26 +0200130 // We let the disabled timer stay in the list, as it might be
131 // referenced elsewhere
Damien Georgebc08c882016-12-06 12:20:10 +1100132}
133
134STATIC void machine_timer_isr(void *self_in) {
135 machine_timer_obj_t *self = self_in;
136 timg_dev_t *device = self->group ? &(TIMERG1) : &(TIMERG0);
137
138 device->hw_timer[self->index].update = 1;
139 if (self->index) {
140 device->int_clr_timers.t1 = 1;
141 } else {
142 device->int_clr_timers.t0 = 1;
143 }
144 device->hw_timer[self->index].config.alarm_en = self->repeat;
145
146 mp_sched_schedule(self->callback, self);
Nicko van Someren14ab81e2018-07-01 20:27:10 -0600147 mp_hal_wake_main_task_from_isr();
Damien Georgebc08c882016-12-06 12:20:10 +1100148}
149
150STATIC void machine_timer_enable(machine_timer_obj_t *self) {
151 timer_config_t config;
152 config.alarm_en = TIMER_ALARM_EN;
153 config.auto_reload = self->repeat;
154 config.counter_dir = TIMER_COUNT_UP;
155 config.divider = TIMER_DIVIDER;
156 config.intr_type = TIMER_INTR_LEVEL;
157 config.counter_en = TIMER_PAUSE;
158
159 check_esp_err(timer_init(self->group, self->index, &config));
160 check_esp_err(timer_set_counter_value(self->group, self->index, 0x00000000));
161 check_esp_err(timer_set_alarm_value(self->group, self->index, self->period));
162 check_esp_err(timer_enable_intr(self->group, self->index));
Damien George69661f32020-02-27 15:36:53 +1100163 check_esp_err(timer_isr_register(self->group, self->index, machine_timer_isr, (void *)self, TIMER_FLAGS, &self->handle));
Damien Georgebc08c882016-12-06 12:20:10 +1100164 check_esp_err(timer_start(self->group, self->index));
165}
166
167STATIC 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) {
Nicko van Somerenc3c914f2018-06-26 15:03:51 -0600168 enum {
169 ARG_mode,
170 ARG_callback,
171 ARG_period,
172 ARG_tick_hz,
173 ARG_freq,
174 };
Damien Georgebc08c882016-12-06 12:20:10 +1100175 static const mp_arg_t allowed_args[] = {
Damien Georgebc08c882016-12-06 12:20:10 +1100176 { MP_QSTR_mode, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1} },
177 { MP_QSTR_callback, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
Nicko van Somerenc3c914f2018-06-26 15:03:51 -0600178 { MP_QSTR_period, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0xffffffff} },
179 { MP_QSTR_tick_hz, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1000} },
Damien George69661f32020-02-27 15:36:53 +1100180 #if MICROPY_PY_BUILTINS_FLOAT
Nicko van Somerenc3c914f2018-06-26 15:03:51 -0600181 { MP_QSTR_freq, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
Damien George69661f32020-02-27 15:36:53 +1100182 #else
Nicko van Somerenc3c914f2018-06-26 15:03:51 -0600183 { MP_QSTR_freq, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0xffffffff} },
Damien George69661f32020-02-27 15:36:53 +1100184 #endif
Damien Georgebc08c882016-12-06 12:20:10 +1100185 };
186
187 machine_timer_disable(self);
188
189 mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
190 mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
191
Damien George69661f32020-02-27 15:36:53 +1100192 #if MICROPY_PY_BUILTINS_FLOAT
Nicko van Somerenc3c914f2018-06-26 15:03:51 -0600193 if (args[ARG_freq].u_obj != mp_const_none) {
194 self->period = (uint64_t)(TIMER_SCALE / mp_obj_get_float(args[ARG_freq].u_obj));
195 }
Damien George69661f32020-02-27 15:36:53 +1100196 #else
Nicko van Somerenc3c914f2018-06-26 15:03:51 -0600197 if (args[ARG_freq].u_int != 0xffffffff) {
198 self->period = TIMER_SCALE / ((uint64_t)args[ARG_freq].u_int);
199 }
Damien George69661f32020-02-27 15:36:53 +1100200 #endif
Nicko van Somerenc3c914f2018-06-26 15:03:51 -0600201 else {
202 self->period = (((uint64_t)args[ARG_period].u_int) * TIMER_SCALE) / args[ARG_tick_hz].u_int;
203 }
204
205 self->repeat = args[ARG_mode].u_int;
206 self->callback = args[ARG_callback].u_obj;
Damien Georgebc08c882016-12-06 12:20:10 +1100207 self->handle = NULL;
208
209 machine_timer_enable(self);
210
211 return mp_const_none;
212}
213
214STATIC mp_obj_t machine_timer_deinit(mp_obj_t self_in) {
215 machine_timer_disable(self_in);
216
217 return mp_const_none;
218}
219STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_timer_deinit_obj, machine_timer_deinit);
220
221STATIC mp_obj_t machine_timer_init(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
222 return machine_timer_init_helper(args[0], n_args - 1, args + 1, kw_args);
223}
224STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_timer_init_obj, 1, machine_timer_init);
225
226STATIC mp_obj_t machine_timer_value(mp_obj_t self_in) {
227 machine_timer_obj_t *self = self_in;
228 double result;
229
230 timer_get_counter_time_sec(self->group, self->index, &result);
231
232 return MP_OBJ_NEW_SMALL_INT((mp_uint_t)(result * 1000)); // value in ms
233}
234STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_timer_value_obj, machine_timer_value);
235
Damien George4fa7d362018-05-02 22:33:41 +1000236STATIC const mp_rom_map_elem_t machine_timer_locals_dict_table[] = {
237 { MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&machine_timer_deinit_obj) },
238 { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&machine_timer_deinit_obj) },
239 { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_timer_init_obj) },
240 { MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&machine_timer_value_obj) },
Damien Georgebc08c882016-12-06 12:20:10 +1100241 { MP_ROM_QSTR(MP_QSTR_ONE_SHOT), MP_ROM_INT(false) },
242 { MP_ROM_QSTR(MP_QSTR_PERIODIC), MP_ROM_INT(true) },
243};
244STATIC MP_DEFINE_CONST_DICT(machine_timer_locals_dict, machine_timer_locals_dict_table);
245
246const mp_obj_type_t machine_timer_type = {
247 { &mp_type_type },
248 .name = MP_QSTR_Timer,
249 .print = machine_timer_print,
250 .make_new = machine_timer_make_new,
251 .locals_dict = (mp_obj_t)&machine_timer_locals_dict,
252};