timer.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2016 Glenn Ruben Bakke
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include "py/nlr.h"
  27. #include "py/runtime.h"
  28. #include "timer.h"
  29. #include "nrfx_timer.h"
  30. #if MICROPY_PY_MACHINE_TIMER
  31. enum {
  32. TIMER_MODE_ONESHOT,
  33. TIMER_MODE_PERIODIC,
  34. };
  35. typedef struct _machine_timer_obj_t {
  36. mp_obj_base_t base;
  37. nrfx_timer_t p_instance;
  38. } machine_timer_obj_t;
  39. STATIC mp_obj_t machine_timer_callbacks[] = {
  40. NULL,
  41. NULL,
  42. NULL,
  43. #if NRF52
  44. NULL,
  45. NULL,
  46. #endif
  47. };
  48. STATIC const machine_timer_obj_t machine_timer_obj[] = {
  49. {{&machine_timer_type}, NRFX_TIMER_INSTANCE(0)},
  50. #if !defined(MICROPY_PY_MACHINE_SOFT_PWM) || (MICROPY_PY_MACHINE_SOFT_PWM == 0)
  51. {{&machine_timer_type}, NRFX_TIMER_INSTANCE(1)},
  52. #endif
  53. {{&machine_timer_type}, NRFX_TIMER_INSTANCE(2)},
  54. #if NRF52
  55. {{&machine_timer_type}, NRFX_TIMER_INSTANCE(3)},
  56. {{&machine_timer_type}, NRFX_TIMER_INSTANCE(4)},
  57. #endif
  58. };
  59. void timer_init0(void) {
  60. for (int i = 0; i < MP_ARRAY_SIZE(machine_timer_obj); i++) {
  61. nrfx_timer_uninit(&machine_timer_obj[i].p_instance);
  62. }
  63. }
  64. STATIC int timer_find(mp_obj_t id) {
  65. // given an integer id
  66. int timer_id = mp_obj_get_int(id);
  67. if (timer_id >= 0 && timer_id < MP_ARRAY_SIZE(machine_timer_obj)) {
  68. return timer_id;
  69. }
  70. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
  71. "Timer(%d) does not exist", timer_id));
  72. }
  73. STATIC void timer_print(const mp_print_t *print, mp_obj_t o, mp_print_kind_t kind) {
  74. machine_timer_obj_t *self = o;
  75. mp_printf(print, "Timer(%u)", self->p_instance.instance_id);
  76. }
  77. STATIC void timer_event_handler(nrf_timer_event_t event_type, void *p_context) {
  78. machine_timer_obj_t *self = p_context;
  79. mp_obj_t callback = machine_timer_callbacks[self->p_instance.instance_id];
  80. if (callback != NULL) {
  81. mp_call_function_1(callback, self);
  82. }
  83. }
  84. /******************************************************************************/
  85. /* MicroPython bindings for machine API */
  86. 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 *all_args) {
  87. enum { ARG_id, ARG_period, ARG_mode, ARG_callback };
  88. static const mp_arg_t allowed_args[] = {
  89. { MP_QSTR_id, MP_ARG_OBJ, {.u_obj = MP_OBJ_NEW_SMALL_INT(-1)} },
  90. { MP_QSTR_period, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1000000} }, // 1 second
  91. { MP_QSTR_mode, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = TIMER_MODE_PERIODIC} },
  92. { MP_QSTR_callback, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
  93. };
  94. // parse args
  95. mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
  96. mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
  97. // get static peripheral object
  98. int timer_id = timer_find(args[ARG_id].u_obj);
  99. #if BLUETOOTH_SD
  100. if (timer_id == 0) {
  101. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
  102. "Timer(%d) reserved by Bluetooth LE stack.", timer_id));
  103. }
  104. #endif
  105. #if MICROPY_PY_MACHINE_SOFT_PWM
  106. if (timer_id == 1) {
  107. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
  108. "Timer(%d) reserved by ticker driver.", timer_id));
  109. }
  110. #endif
  111. machine_timer_obj_t *self = (machine_timer_obj_t*)&machine_timer_obj[timer_id];
  112. if (MP_OBJ_IS_FUN(args[ARG_callback].u_obj)) {
  113. machine_timer_callbacks[timer_id] = args[ARG_callback].u_obj;
  114. } else if (args[ARG_callback].u_obj == mp_const_none) {
  115. machine_timer_callbacks[timer_id] = NULL;
  116. } else {
  117. mp_raise_ValueError("callback must be a function");
  118. }
  119. // Timer peripheral usage:
  120. // Every timer instance has a numer of capture/compare (CC) registers.
  121. // These can store either the value to compare against (to trigger an
  122. // interrupt or a shortcut) or store a value returned from a
  123. // capture/compare event.
  124. // We use channel 0 for comparing (to trigger the callback and clear
  125. // shortcut) and channel 1 for capturing the current time.
  126. const nrfx_timer_config_t config = {
  127. .frequency = NRF_TIMER_FREQ_1MHz,
  128. .mode = NRF_TIMER_MODE_TIMER,
  129. .bit_width = NRF_TIMER_BIT_WIDTH_24,
  130. #ifdef NRF51
  131. .interrupt_priority = 3,
  132. #else
  133. .interrupt_priority = 6,
  134. #endif
  135. .p_context = self,
  136. };
  137. // Initialize the drive.
  138. // When it is already initialized, this is a no-op.
  139. nrfx_timer_init(&self->p_instance, &config, timer_event_handler);
  140. // Configure channel 0.
  141. nrf_timer_short_mask_t short_mask = NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK |
  142. ((args[ARG_mode].u_int == TIMER_MODE_ONESHOT) ? NRF_TIMER_SHORT_COMPARE0_STOP_MASK : 0);
  143. bool enable_interrupts = true;
  144. nrfx_timer_extended_compare(
  145. &self->p_instance,
  146. NRF_TIMER_CC_CHANNEL0,
  147. args[ARG_period].u_int,
  148. short_mask,
  149. enable_interrupts);
  150. return MP_OBJ_FROM_PTR(self);
  151. }
  152. /// \method period()
  153. /// Return counter value, which is currently in us.
  154. ///
  155. STATIC mp_obj_t machine_timer_period(mp_obj_t self_in) {
  156. machine_timer_obj_t * self = MP_OBJ_TO_PTR(self_in);
  157. uint32_t period = nrfx_timer_capture(&self->p_instance, NRF_TIMER_CC_CHANNEL1);
  158. return MP_OBJ_NEW_SMALL_INT(period);
  159. }
  160. STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_timer_period_obj, machine_timer_period);
  161. /// \method start()
  162. /// Start the timer.
  163. ///
  164. STATIC mp_obj_t machine_timer_start(mp_obj_t self_in) {
  165. machine_timer_obj_t * self = MP_OBJ_TO_PTR(self_in);
  166. nrfx_timer_enable(&self->p_instance);
  167. return mp_const_none;
  168. }
  169. STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_timer_start_obj, machine_timer_start);
  170. /// \method stop()
  171. /// Stop the timer.
  172. ///
  173. STATIC mp_obj_t machine_timer_stop(mp_obj_t self_in) {
  174. machine_timer_obj_t * self = MP_OBJ_TO_PTR(self_in);
  175. nrfx_timer_disable(&self->p_instance);
  176. return mp_const_none;
  177. }
  178. STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_timer_stop_obj, machine_timer_stop);
  179. /// \method deinit()
  180. /// Free resources associated with the timer.
  181. ///
  182. STATIC mp_obj_t machine_timer_deinit(mp_obj_t self_in) {
  183. machine_timer_obj_t * self = MP_OBJ_TO_PTR(self_in);
  184. nrfx_timer_uninit(&self->p_instance);
  185. return mp_const_none;
  186. }
  187. STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_timer_deinit_obj, machine_timer_deinit);
  188. STATIC const mp_rom_map_elem_t machine_timer_locals_dict_table[] = {
  189. { MP_ROM_QSTR(MP_QSTR_time), MP_ROM_PTR(&machine_timer_period_obj) }, // alias
  190. { MP_ROM_QSTR(MP_QSTR_period), MP_ROM_PTR(&machine_timer_period_obj) },
  191. { MP_ROM_QSTR(MP_QSTR_start), MP_ROM_PTR(&machine_timer_start_obj) },
  192. { MP_ROM_QSTR(MP_QSTR_stop), MP_ROM_PTR(&machine_timer_stop_obj) },
  193. { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&machine_timer_deinit_obj) },
  194. // constants
  195. { MP_ROM_QSTR(MP_QSTR_ONESHOT), MP_ROM_INT(TIMER_MODE_ONESHOT) },
  196. { MP_ROM_QSTR(MP_QSTR_PERIODIC), MP_ROM_INT(TIMER_MODE_PERIODIC) },
  197. };
  198. STATIC MP_DEFINE_CONST_DICT(machine_timer_locals_dict, machine_timer_locals_dict_table);
  199. const mp_obj_type_t machine_timer_type = {
  200. { &mp_type_type },
  201. .name = MP_QSTR_Timer,
  202. .print = timer_print,
  203. .make_new = machine_timer_make_new,
  204. .locals_dict = (mp_obj_dict_t*)&machine_timer_locals_dict
  205. };
  206. #endif // MICROPY_PY_MACHINE_TIMER