Damien George | bc08c88 | 2016-12-06 12:20:10 +1100 | [diff] [blame] | 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 "driver/timer.h" |
| 34 | #include "py/obj.h" |
| 35 | #include "py/runtime.h" |
| 36 | #include "modmachine.h" |
Nicko van Someren | 14ab81e | 2018-07-01 20:27:10 -0600 | [diff] [blame] | 37 | #include "mphalport.h" |
Damien George | bc08c88 | 2016-12-06 12:20:10 +1100 | [diff] [blame] | 38 | |
| 39 | #define TIMER_INTR_SEL TIMER_INTR_LEVEL |
Nicko van Someren | c3c914f | 2018-06-26 15:03:51 -0600 | [diff] [blame] | 40 | #define TIMER_DIVIDER 8 |
| 41 | |
| 42 | // TIMER_BASE_CLK is normally 80MHz. TIMER_DIVIDER ought to divide this exactly |
Damien George | bc08c88 | 2016-12-06 12:20:10 +1100 | [diff] [blame] | 43 | #define TIMER_SCALE (TIMER_BASE_CLK / TIMER_DIVIDER) |
| 44 | |
| 45 | #define TIMER_FLAGS 0 |
| 46 | |
| 47 | typedef 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 Someren | c3c914f | 2018-06-26 15:03:51 -0600 | [diff] [blame] | 53 | // ESP32 timers are 64-bit |
| 54 | uint64_t period; |
Damien George | bc08c88 | 2016-12-06 12:20:10 +1100 | [diff] [blame] | 55 | |
| 56 | mp_obj_t callback; |
| 57 | |
| 58 | intr_handle_t handle; |
Damien George | 9ac9aa9 | 2019-01-28 16:16:03 +1100 | [diff] [blame] | 59 | |
| 60 | struct _machine_timer_obj_t *next; |
Damien George | bc08c88 | 2016-12-06 12:20:10 +1100 | [diff] [blame] | 61 | } machine_timer_obj_t; |
| 62 | |
| 63 | const mp_obj_type_t machine_timer_type; |
| 64 | |
Damien George | 9ac9aa9 | 2019-01-28 16:16:03 +1100 | [diff] [blame] | 65 | STATIC void machine_timer_disable(machine_timer_obj_t *self); |
| 66 | |
Damien George | bc08c88 | 2016-12-06 12:20:10 +1100 | [diff] [blame] | 67 | STATIC 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 George | 9ac9aa9 | 2019-01-28 16:16:03 +1100 | [diff] [blame] | 75 | void machine_timer_deinit_all(void) { |
Martin Dybdal | de76f73 | 2019-05-24 13:19:26 +0200 | [diff] [blame] | 76 | // 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 George | 9ac9aa9 | 2019-01-28 16:16:03 +1100 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
Damien George | bc08c88 | 2016-12-06 12:20:10 +1100 | [diff] [blame] | 86 | STATIC 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 | |
| 99 | 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) { |
| 100 | mp_arg_check_num(n_args, n_kw, 1, 1, false); |
Martin Dybdal | de76f73 | 2019-05-24 13:19:26 +0200 | [diff] [blame] | 101 | 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 George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame^] | 110 | |
Damien George | bc08c88 | 2016-12-06 12:20:10 +1100 | [diff] [blame] | 111 | machine_timer_obj_t *self = m_new_obj(machine_timer_obj_t); |
| 112 | self->base.type = &machine_timer_type; |
Martin Dybdal | de76f73 | 2019-05-24 13:19:26 +0200 | [diff] [blame] | 113 | self->group = group; |
| 114 | self->index = index; |
Damien George | bc08c88 | 2016-12-06 12:20:10 +1100 | [diff] [blame] | 115 | |
Martin Dybdal | de76f73 | 2019-05-24 13:19:26 +0200 | [diff] [blame] | 116 | // 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 George | bc08c88 | 2016-12-06 12:20:10 +1100 | [diff] [blame] | 119 | |
| 120 | return self; |
| 121 | } |
| 122 | |
| 123 | STATIC 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 George | 9ac9aa9 | 2019-01-28 16:16:03 +1100 | [diff] [blame] | 129 | |
Martin Dybdal | de76f73 | 2019-05-24 13:19:26 +0200 | [diff] [blame] | 130 | // We let the disabled timer stay in the list, as it might be |
| 131 | // referenced elsewhere |
Damien George | bc08c88 | 2016-12-06 12:20:10 +1100 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | STATIC 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 Someren | 14ab81e | 2018-07-01 20:27:10 -0600 | [diff] [blame] | 147 | mp_hal_wake_main_task_from_isr(); |
Damien George | bc08c88 | 2016-12-06 12:20:10 +1100 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | STATIC 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 George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame^] | 163 | check_esp_err(timer_isr_register(self->group, self->index, machine_timer_isr, (void *)self, TIMER_FLAGS, &self->handle)); |
Damien George | bc08c88 | 2016-12-06 12:20:10 +1100 | [diff] [blame] | 164 | check_esp_err(timer_start(self->group, self->index)); |
| 165 | } |
| 166 | |
| 167 | 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) { |
Nicko van Someren | c3c914f | 2018-06-26 15:03:51 -0600 | [diff] [blame] | 168 | enum { |
| 169 | ARG_mode, |
| 170 | ARG_callback, |
| 171 | ARG_period, |
| 172 | ARG_tick_hz, |
| 173 | ARG_freq, |
| 174 | }; |
Damien George | bc08c88 | 2016-12-06 12:20:10 +1100 | [diff] [blame] | 175 | static const mp_arg_t allowed_args[] = { |
Damien George | bc08c88 | 2016-12-06 12:20:10 +1100 | [diff] [blame] | 176 | { 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 Someren | c3c914f | 2018-06-26 15:03:51 -0600 | [diff] [blame] | 178 | { 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 George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame^] | 180 | #if MICROPY_PY_BUILTINS_FLOAT |
Nicko van Someren | c3c914f | 2018-06-26 15:03:51 -0600 | [diff] [blame] | 181 | { MP_QSTR_freq, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} }, |
Damien George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame^] | 182 | #else |
Nicko van Someren | c3c914f | 2018-06-26 15:03:51 -0600 | [diff] [blame] | 183 | { MP_QSTR_freq, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0xffffffff} }, |
Damien George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame^] | 184 | #endif |
Damien George | bc08c88 | 2016-12-06 12:20:10 +1100 | [diff] [blame] | 185 | }; |
| 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 George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame^] | 192 | #if MICROPY_PY_BUILTINS_FLOAT |
Nicko van Someren | c3c914f | 2018-06-26 15:03:51 -0600 | [diff] [blame] | 193 | 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 George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame^] | 196 | #else |
Nicko van Someren | c3c914f | 2018-06-26 15:03:51 -0600 | [diff] [blame] | 197 | if (args[ARG_freq].u_int != 0xffffffff) { |
| 198 | self->period = TIMER_SCALE / ((uint64_t)args[ARG_freq].u_int); |
| 199 | } |
Damien George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame^] | 200 | #endif |
Nicko van Someren | c3c914f | 2018-06-26 15:03:51 -0600 | [diff] [blame] | 201 | 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 George | bc08c88 | 2016-12-06 12:20:10 +1100 | [diff] [blame] | 207 | self->handle = NULL; |
| 208 | |
| 209 | machine_timer_enable(self); |
| 210 | |
| 211 | return mp_const_none; |
| 212 | } |
| 213 | |
| 214 | STATIC mp_obj_t machine_timer_deinit(mp_obj_t self_in) { |
| 215 | machine_timer_disable(self_in); |
| 216 | |
| 217 | return mp_const_none; |
| 218 | } |
| 219 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_timer_deinit_obj, machine_timer_deinit); |
| 220 | |
| 221 | STATIC 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 | } |
| 224 | STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_timer_init_obj, 1, machine_timer_init); |
| 225 | |
| 226 | STATIC 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 | } |
| 234 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_timer_value_obj, machine_timer_value); |
| 235 | |
Damien George | 4fa7d36 | 2018-05-02 22:33:41 +1000 | [diff] [blame] | 236 | STATIC 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 George | bc08c88 | 2016-12-06 12:20:10 +1100 | [diff] [blame] | 241 | { MP_ROM_QSTR(MP_QSTR_ONE_SHOT), MP_ROM_INT(false) }, |
| 242 | { MP_ROM_QSTR(MP_QSTR_PERIODIC), MP_ROM_INT(true) }, |
| 243 | }; |
| 244 | STATIC MP_DEFINE_CONST_DICT(machine_timer_locals_dict, machine_timer_locals_dict_table); |
| 245 | |
| 246 | const 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 | }; |