machine_timer.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. #include <stdint.h>
  30. #include <stdio.h>
  31. #include "driver/timer.h"
  32. #include "py/obj.h"
  33. #include "py/runtime.h"
  34. #include "modmachine.h"
  35. #include "mphalport.h"
  36. #define TIMER_INTR_SEL TIMER_INTR_LEVEL
  37. #define TIMER_DIVIDER 8
  38. // TIMER_BASE_CLK is normally 80MHz. TIMER_DIVIDER ought to divide this exactly
  39. #define TIMER_SCALE (TIMER_BASE_CLK / TIMER_DIVIDER)
  40. #define TIMER_FLAGS 0
  41. typedef struct _machine_timer_obj_t {
  42. mp_obj_base_t base;
  43. mp_uint_t group;
  44. mp_uint_t index;
  45. mp_uint_t repeat;
  46. // ESP32 timers are 64-bit
  47. uint64_t period;
  48. mp_obj_t callback;
  49. intr_handle_t handle;
  50. } machine_timer_obj_t;
  51. const mp_obj_type_t machine_timer_type;
  52. STATIC esp_err_t check_esp_err(esp_err_t code) {
  53. if (code) {
  54. mp_raise_OSError(code);
  55. }
  56. return code;
  57. }
  58. STATIC void machine_timer_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
  59. machine_timer_obj_t *self = self_in;
  60. timer_config_t config;
  61. mp_printf(print, "Timer(%p; ", self);
  62. timer_get_config(self->group, self->index, &config);
  63. mp_printf(print, "alarm_en=%d, ", config.alarm_en);
  64. mp_printf(print, "auto_reload=%d, ", config.auto_reload);
  65. mp_printf(print, "counter_en=%d)", config.counter_en);
  66. }
  67. 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) {
  68. mp_arg_check_num(n_args, n_kw, 1, 1, false);
  69. machine_timer_obj_t *self = m_new_obj(machine_timer_obj_t);
  70. self->base.type = &machine_timer_type;
  71. self->group = (mp_obj_get_int(args[0]) >> 1) & 1;
  72. self->index = mp_obj_get_int(args[0]) & 1;
  73. return self;
  74. }
  75. STATIC void machine_timer_disable(machine_timer_obj_t *self) {
  76. if (self->handle) {
  77. timer_pause(self->group, self->index);
  78. esp_intr_free(self->handle);
  79. self->handle = NULL;
  80. }
  81. }
  82. STATIC void machine_timer_isr(void *self_in) {
  83. machine_timer_obj_t *self = self_in;
  84. timg_dev_t *device = self->group ? &(TIMERG1) : &(TIMERG0);
  85. device->hw_timer[self->index].update = 1;
  86. if (self->index) {
  87. device->int_clr_timers.t1 = 1;
  88. } else {
  89. device->int_clr_timers.t0 = 1;
  90. }
  91. device->hw_timer[self->index].config.alarm_en = self->repeat;
  92. mp_sched_schedule(self->callback, self);
  93. mp_hal_wake_main_task_from_isr();
  94. }
  95. STATIC void machine_timer_enable(machine_timer_obj_t *self) {
  96. timer_config_t config;
  97. config.alarm_en = TIMER_ALARM_EN;
  98. config.auto_reload = self->repeat;
  99. config.counter_dir = TIMER_COUNT_UP;
  100. config.divider = TIMER_DIVIDER;
  101. config.intr_type = TIMER_INTR_LEVEL;
  102. config.counter_en = TIMER_PAUSE;
  103. check_esp_err(timer_init(self->group, self->index, &config));
  104. check_esp_err(timer_set_counter_value(self->group, self->index, 0x00000000));
  105. check_esp_err(timer_set_alarm_value(self->group, self->index, self->period));
  106. check_esp_err(timer_enable_intr(self->group, self->index));
  107. check_esp_err(timer_isr_register(self->group, self->index, machine_timer_isr, (void*)self, TIMER_FLAGS, &self->handle));
  108. check_esp_err(timer_start(self->group, self->index));
  109. }
  110. 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) {
  111. enum {
  112. ARG_mode,
  113. ARG_callback,
  114. ARG_period,
  115. ARG_tick_hz,
  116. ARG_freq,
  117. };
  118. static const mp_arg_t allowed_args[] = {
  119. { MP_QSTR_mode, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1} },
  120. { MP_QSTR_callback, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
  121. { MP_QSTR_period, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0xffffffff} },
  122. { MP_QSTR_tick_hz, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1000} },
  123. #if MICROPY_PY_BUILTINS_FLOAT
  124. { MP_QSTR_freq, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
  125. #else
  126. { MP_QSTR_freq, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0xffffffff} },
  127. #endif
  128. };
  129. machine_timer_disable(self);
  130. mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
  131. mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
  132. #if MICROPY_PY_BUILTINS_FLOAT
  133. if (args[ARG_freq].u_obj != mp_const_none) {
  134. self->period = (uint64_t)(TIMER_SCALE / mp_obj_get_float(args[ARG_freq].u_obj));
  135. }
  136. #else
  137. if (args[ARG_freq].u_int != 0xffffffff) {
  138. self->period = TIMER_SCALE / ((uint64_t)args[ARG_freq].u_int);
  139. }
  140. #endif
  141. else {
  142. self->period = (((uint64_t)args[ARG_period].u_int) * TIMER_SCALE) / args[ARG_tick_hz].u_int;
  143. }
  144. self->repeat = args[ARG_mode].u_int;
  145. self->callback = args[ARG_callback].u_obj;
  146. self->handle = NULL;
  147. machine_timer_enable(self);
  148. return mp_const_none;
  149. }
  150. STATIC mp_obj_t machine_timer_deinit(mp_obj_t self_in) {
  151. machine_timer_disable(self_in);
  152. return mp_const_none;
  153. }
  154. STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_timer_deinit_obj, machine_timer_deinit);
  155. STATIC mp_obj_t machine_timer_init(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
  156. return machine_timer_init_helper(args[0], n_args - 1, args + 1, kw_args);
  157. }
  158. STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_timer_init_obj, 1, machine_timer_init);
  159. STATIC mp_obj_t machine_timer_value(mp_obj_t self_in) {
  160. machine_timer_obj_t *self = self_in;
  161. double result;
  162. timer_get_counter_time_sec(self->group, self->index, &result);
  163. return MP_OBJ_NEW_SMALL_INT((mp_uint_t)(result * 1000)); // value in ms
  164. }
  165. STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_timer_value_obj, machine_timer_value);
  166. STATIC const mp_rom_map_elem_t machine_timer_locals_dict_table[] = {
  167. { MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&machine_timer_deinit_obj) },
  168. { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&machine_timer_deinit_obj) },
  169. { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_timer_init_obj) },
  170. { MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&machine_timer_value_obj) },
  171. { MP_ROM_QSTR(MP_QSTR_ONE_SHOT), MP_ROM_INT(false) },
  172. { MP_ROM_QSTR(MP_QSTR_PERIODIC), MP_ROM_INT(true) },
  173. };
  174. STATIC MP_DEFINE_CONST_DICT(machine_timer_locals_dict, machine_timer_locals_dict_table);
  175. const mp_obj_type_t machine_timer_type = {
  176. { &mp_type_type },
  177. .name = MP_QSTR_Timer,
  178. .print = machine_timer_print,
  179. .make_new = machine_timer_make_new,
  180. .locals_dict = (mp_obj_t)&machine_timer_locals_dict,
  181. };