|
| 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/mphal.h" |
| 34 | +#include "py/obj.h" |
| 35 | +#include "py/runtime.h" |
| 36 | +#include "modmachine.h" |
| 37 | +#include "py/gc.h" |
| 38 | +#include "py/stackctrl.h" |
| 39 | +#include "mphalport.h" |
| 40 | + |
| 41 | +#include "esp_timer.h" |
| 42 | +#include "rom/ets_sys.h" |
| 43 | +#include "esp_system.h" |
| 44 | +#include "esp_cpu.h" |
| 45 | + |
| 46 | +#include "machine_timer.h" |
| 47 | + |
| 48 | + |
| 49 | +const mp_obj_type_t machine_timer_type; |
| 50 | + |
| 51 | +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); |
| 52 | + |
| 53 | + |
| 54 | +void machine_timer_deinit_all(void) |
| 55 | +{ |
| 56 | + // Disable, deallocate and remove all timers from list |
| 57 | + machine_timer_obj_t **t = &MP_STATE_PORT(machine_timer_obj_head); |
| 58 | + while (*t != NULL) { |
| 59 | + machine_timer_disable(*t); |
| 60 | + machine_timer_obj_t *next = (*t)->next; |
| 61 | + m_del_obj(machine_timer_obj_t, *t); |
| 62 | + *t = next; |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | + |
| 67 | +static void machine_timer_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) |
| 68 | +{ |
| 69 | + machine_timer_obj_t *self = self_in; |
| 70 | + qstr mode = self->repeat ? MP_QSTR_PERIODIC : MP_QSTR_ONE_SHOT; |
| 71 | + float period = (float)self->period / 1000.0f; |
| 72 | + mp_printf(print, "Timer(%u, mode=%q, period=%f)", self->id, mode, period); |
| 73 | +} |
| 74 | + |
| 75 | + |
| 76 | +machine_timer_obj_t *machine_timer_create(mp_uint_t timer) |
| 77 | +{ |
| 78 | + machine_timer_obj_t *self = NULL; |
| 79 | + |
| 80 | + // Check whether the timer is already initialized, if so use it |
| 81 | + for (machine_timer_obj_t *t = MP_STATE_PORT(machine_timer_obj_head); t; t = t->next) { |
| 82 | + if (t->id == (uint8_t)timer) { |
| 83 | + self = t; |
| 84 | + break; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + // The timer does not exist, create it. |
| 89 | + if (self == NULL) { |
| 90 | + self = mp_obj_malloc(machine_timer_obj_t, &machine_timer_type); |
| 91 | + self->handle = NULL; |
| 92 | + self->id = timer; |
| 93 | + |
| 94 | + // Add the timer to the linked-list of timers |
| 95 | + self->next = MP_STATE_PORT(machine_timer_obj_head); |
| 96 | + MP_STATE_PORT(machine_timer_obj_head) = self; |
| 97 | + } |
| 98 | + return self; |
| 99 | +} |
| 100 | + |
| 101 | + |
| 102 | +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) |
| 103 | +{ |
| 104 | + mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true); |
| 105 | + |
| 106 | + // Create the new timer. |
| 107 | + uint32_t timer_number = mp_obj_get_int(args[0]); |
| 108 | + machine_timer_obj_t *self = machine_timer_create(timer_number); |
| 109 | + |
| 110 | + if (n_args > 1 || n_kw > 0) { |
| 111 | + mp_map_t kw_args; |
| 112 | + mp_map_init_fixed_table(&kw_args, n_kw, args + n_args); |
| 113 | + machine_timer_init_helper(self, n_args - 1, args + 1, &kw_args); |
| 114 | + } |
| 115 | + |
| 116 | + return self; |
| 117 | +} |
| 118 | + |
| 119 | + |
| 120 | +void machine_timer_disable(machine_timer_obj_t *self) |
| 121 | +{ |
| 122 | + if (self->handle) { |
| 123 | + if (esp_timer_is_active(self->handle)) { |
| 124 | + esp_timer_stop(self->handle); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | + |
| 130 | +static void machine_timer_isr(void *self_in) |
| 131 | +{ |
| 132 | + machine_timer_obj_t *self = self_in; |
| 133 | + |
| 134 | + volatile uint32_t sp = (uint32_t)esp_cpu_get_sp(); |
| 135 | + |
| 136 | + void *old_state = mp_thread_get_state(); |
| 137 | + |
| 138 | + mp_state_thread_t ts; |
| 139 | + mp_thread_set_state(&ts); |
| 140 | + mp_stack_set_top((void*)sp); |
| 141 | + mp_stack_set_limit(CONFIG_FREERTOS_IDLE_TASK_STACKSIZE - 1024); |
| 142 | + mp_locals_set(mp_state_ctx.thread.dict_locals); |
| 143 | + mp_globals_set(mp_state_ctx.thread.dict_globals); |
| 144 | + |
| 145 | + mp_sched_lock(); |
| 146 | + gc_lock(); |
| 147 | + |
| 148 | + nlr_buf_t nlr; |
| 149 | + if (nlr_push(&nlr) == 0) { |
| 150 | + mp_obj_t args[1]; |
| 151 | + args[0] = MP_OBJ_FROM_PTR(self); |
| 152 | + |
| 153 | + mp_call_function_n_kw(self->callback, 1, 0, args); |
| 154 | + nlr_pop(); |
| 155 | + } else { |
| 156 | + ets_printf("Uncaught exception in IRQ callback handler!\n"); |
| 157 | + mp_obj_print_exception(&mp_plat_print, MP_OBJ_FROM_PTR(nlr.ret_val)); |
| 158 | + } |
| 159 | + |
| 160 | + if (self->repeat) { |
| 161 | + self->period_time = esp_timer_get_time(); |
| 162 | + } |
| 163 | + |
| 164 | + gc_unlock(); |
| 165 | + mp_sched_unlock(); |
| 166 | + |
| 167 | + mp_thread_set_state(old_state); |
| 168 | + mp_hal_wake_main_task_from_isr(); |
| 169 | +} |
| 170 | + |
| 171 | + |
| 172 | +void machine_timer_enable(machine_timer_obj_t *self, void (*timer_isr)) |
| 173 | +{ |
| 174 | + esp_err_t err; |
| 175 | + |
| 176 | + if (self->handle == NULL) { |
| 177 | + esp_timer_create_args_t timer_args = { |
| 178 | + .callback=timer_isr, |
| 179 | + .arg=self, |
| 180 | + .dispatch_method=ESP_TIMER_ISR |
| 181 | + }; |
| 182 | + |
| 183 | + err = esp_timer_create(&timer_args, &self->handle); |
| 184 | + if (err != ESP_OK) { |
| 185 | + mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("Unable to create timer (%d)"), err); |
| 186 | + } |
| 187 | + |
| 188 | + if (self->repeat) err = esp_timer_start_periodic(self->handle, self->period); |
| 189 | + else err = esp_timer_start_once(self->handle, self->period); |
| 190 | + |
| 191 | + if (err != ESP_OK) { |
| 192 | + mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("Unable to start timer (%d)"), err); |
| 193 | + } else if (self->repeat) { |
| 194 | + self->period_time = esp_timer_get_time(); |
| 195 | + } |
| 196 | + } else if (self->change_type) { |
| 197 | + self->change_type = 0; |
| 198 | + if (self->repeat) err = esp_timer_start_periodic(self->handle, self->period); |
| 199 | + else err = esp_timer_start_once(self->handle, self->period); |
| 200 | + |
| 201 | + if (err != ESP_OK) { |
| 202 | + mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("Unable to start timer (%d)"), err); |
| 203 | + } |
| 204 | + } else { |
| 205 | + err = esp_timer_restart(self->handle, self->period); |
| 206 | + if (err != ESP_OK) { |
| 207 | + mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("Unable to restart timer (%d)"), err); |
| 208 | + } |
| 209 | + } |
| 210 | +} |
| 211 | + |
| 212 | + |
| 213 | +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) { |
| 214 | + enum { ARG_mode, ARG_callback, ARG_period }; |
| 215 | + static const mp_arg_t allowed_args[] = { |
| 216 | + { MP_QSTR_mode, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} }, |
| 217 | + { MP_QSTR_callback, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none } }, |
| 218 | + { MP_QSTR_period, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none } }, |
| 219 | + }; |
| 220 | + |
| 221 | + machine_timer_disable(self); |
| 222 | + |
| 223 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 224 | + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 225 | + |
| 226 | + int mode = (int)args[ARG_mode].u_int; |
| 227 | + |
| 228 | + if (mode == -1) { |
| 229 | + self->change_type = 0; |
| 230 | + } else if ((uint8_t)mode != self->repeat) { |
| 231 | + self->repeat = (uint8_t)mode; |
| 232 | + self->change_type = 1; |
| 233 | + } |
| 234 | + |
| 235 | + if (args[ARG_period].u_obj != mp_const_none) { |
| 236 | + if (mp_obj_is_float(args[ARG_period].u_obj)) { |
| 237 | + float f_period = (float)mp_obj_get_float(args[ARG_period].u_obj); |
| 238 | + self->period = (uint64_t)(f_period * 1000.0f); |
| 239 | + } else { |
| 240 | + self->period = (uint64_t)mp_obj_get_int_truncated(args[ARG_period].u_obj); |
| 241 | + } |
| 242 | + } |
| 243 | + if (args[ARG_callback].u_obj != mp_const_none) { |
| 244 | + self->callback = args[ARG_callback].u_obj; |
| 245 | + } |
| 246 | + |
| 247 | + machine_timer_enable(self, machine_timer_isr); |
| 248 | + |
| 249 | + return mp_const_none; |
| 250 | +} |
| 251 | + |
| 252 | + |
| 253 | +static mp_obj_t machine_timer__del__(mp_obj_t self_in) |
| 254 | +{ |
| 255 | + machine_timer_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 256 | + machine_timer_disable(self); |
| 257 | + |
| 258 | + if (self->handle != NULL) esp_timer_delete(self->handle); |
| 259 | + |
| 260 | + self->handle = NULL; |
| 261 | + return mp_const_none; |
| 262 | +} |
| 263 | + |
| 264 | +static MP_DEFINE_CONST_FUN_OBJ_1(machine_timer__del__obj, machine_timer__del__); |
| 265 | + |
| 266 | + |
| 267 | +static mp_obj_t machine_timer_deinit(mp_obj_t self_in) |
| 268 | +{ |
| 269 | + machine_timer_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 270 | + |
| 271 | + machine_timer_disable(self); |
| 272 | + return mp_const_none; |
| 273 | +} |
| 274 | + |
| 275 | +static MP_DEFINE_CONST_FUN_OBJ_1(machine_timer_deinit_obj, machine_timer_deinit); |
| 276 | + |
| 277 | + |
| 278 | +static mp_obj_t machine_timer_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) |
| 279 | +{ |
| 280 | + return machine_timer_init_helper(args[0], n_args - 1, args + 1, kw_args); |
| 281 | +} |
| 282 | + |
| 283 | +static MP_DEFINE_CONST_FUN_OBJ_KW(machine_timer_init_obj, 1, machine_timer_init); |
| 284 | + |
| 285 | + |
| 286 | +static mp_obj_t machine_timer_value(mp_obj_t self_in) |
| 287 | +{ |
| 288 | + machine_timer_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 289 | + float res; |
| 290 | + |
| 291 | + if (esp_timer_is_active(self->handle)) { |
| 292 | + uint64_t curr_time = esp_timer_get_time(); |
| 293 | + if (self->repeat) { |
| 294 | + uint64_t diff = curr_time - self->period_time; |
| 295 | + res = (float)(self->period - diff) / 1000.0f; |
| 296 | + } else { |
| 297 | + uint64_t expire_time; |
| 298 | + esp_timer_get_expiry_time(self->handle, &expire_time); |
| 299 | + res = (float)(expire_time - curr_time) / 1000.0f; |
| 300 | + } |
| 301 | + } else { |
| 302 | + res = 0.0f; |
| 303 | + } |
| 304 | + |
| 305 | + if (res < 0.0f) res = 0.0f; |
| 306 | + |
| 307 | + return mp_obj_new_float(res); |
| 308 | +} |
| 309 | + |
| 310 | +static MP_DEFINE_CONST_FUN_OBJ_1(machine_timer_value_obj, machine_timer_value); |
| 311 | + |
| 312 | + |
| 313 | + |
| 314 | +static const mp_rom_map_elem_t machine_timer_locals_dict_table[] = { |
| 315 | + { MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&machine_timer__del__obj) }, |
| 316 | + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&machine_timer_deinit_obj) }, |
| 317 | + { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_timer_init_obj) }, |
| 318 | + { MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&machine_timer_value_obj) }, |
| 319 | + { MP_ROM_QSTR(MP_QSTR_ONE_SHOT), MP_ROM_INT(false) }, |
| 320 | + { MP_ROM_QSTR(MP_QSTR_PERIODIC), MP_ROM_INT(true) }, |
| 321 | +}; |
| 322 | +static MP_DEFINE_CONST_DICT(machine_timer_locals_dict, machine_timer_locals_dict_table); |
| 323 | + |
| 324 | +MP_DEFINE_CONST_OBJ_TYPE( |
| 325 | + machine_timer_type, |
| 326 | + MP_QSTR_Timer, |
| 327 | + MP_TYPE_FLAG_NONE, |
| 328 | + make_new, machine_timer_make_new, |
| 329 | + print, machine_timer_print, |
| 330 | + locals_dict, &machine_timer_locals_dict |
| 331 | +); |
| 332 | + |
| 333 | +MP_REGISTER_ROOT_POINTER(struct _machine_timer_obj_t *machine_timer_obj_head); |
0 commit comments