rtcounter.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. * Copyright (c) 2018 Ayke van Laethem
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. #include "py/nlr.h"
  28. #include "py/runtime.h"
  29. #include "rtcounter.h"
  30. #include "nrfx_rtc.h"
  31. #include "nrf_clock.h"
  32. #if MICROPY_PY_MACHINE_RTCOUNTER
  33. // Count every 125ms (~maximum prescaler setting)
  34. #define RTC_FREQUENCY (8UL)
  35. enum {
  36. RTC_MODE_ONESHOT,
  37. RTC_MODE_PERIODIC,
  38. };
  39. // Volatile part of the RTCounter object.
  40. typedef struct {
  41. mp_obj_t callback;
  42. uint32_t period;
  43. } machine_rtc_config_t;
  44. // Non-volatile part of the RTCounter object.
  45. typedef struct _machine_rtc_obj_t {
  46. mp_obj_base_t base;
  47. const nrfx_rtc_t * p_rtc; // Driver instance
  48. nrfx_rtc_handler_t handler; // interrupt callback
  49. machine_rtc_config_t * config; // pointer to volatile part
  50. } machine_rtc_obj_t;
  51. STATIC const nrfx_rtc_t machine_rtc_instances[] = {
  52. NRFX_RTC_INSTANCE(0),
  53. NRFX_RTC_INSTANCE(1),
  54. #if NRF52
  55. NRFX_RTC_INSTANCE(2),
  56. #endif
  57. };
  58. STATIC machine_rtc_config_t configs[MP_ARRAY_SIZE(machine_rtc_instances)];
  59. STATIC void interrupt_handler0(nrfx_rtc_int_type_t int_type);
  60. STATIC void interrupt_handler1(nrfx_rtc_int_type_t int_type);
  61. #if NRF52
  62. STATIC void interrupt_handler2(nrfx_rtc_int_type_t int_type);
  63. #endif
  64. STATIC const machine_rtc_obj_t machine_rtc_obj[] = {
  65. {{&machine_rtcounter_type}, .p_rtc = &machine_rtc_instances[0], .handler=interrupt_handler0, .config=&configs[0]},
  66. {{&machine_rtcounter_type}, .p_rtc = &machine_rtc_instances[1], .handler=interrupt_handler1, .config=&configs[1]},
  67. #if NRF52
  68. {{&machine_rtcounter_type}, .p_rtc = &machine_rtc_instances[2], .handler=interrupt_handler2, .config=&configs[2]},
  69. #endif
  70. };
  71. STATIC void interrupt_handler(size_t instance_id) {
  72. const machine_rtc_obj_t * self = &machine_rtc_obj[instance_id];
  73. machine_rtc_config_t *config = self->config;
  74. if (config->callback != NULL) {
  75. mp_call_function_1((mp_obj_t)config->callback, (mp_obj_t)self);
  76. }
  77. if (config->period == 0) {
  78. nrfx_rtc_cc_disable(self->p_rtc, 0);
  79. } else { // periodic
  80. uint32_t val = nrfx_rtc_counter_get(self->p_rtc) + config->period;
  81. nrfx_rtc_cc_set(self->p_rtc, 0, val, true);
  82. }
  83. }
  84. STATIC void interrupt_handler0(nrfx_rtc_int_type_t int_type) {
  85. interrupt_handler(0);
  86. }
  87. STATIC void interrupt_handler1(nrfx_rtc_int_type_t int_type) {
  88. interrupt_handler(1);
  89. }
  90. #if NRF52
  91. STATIC void interrupt_handler2(nrfx_rtc_int_type_t int_type) {
  92. interrupt_handler(2);
  93. }
  94. #endif
  95. void rtc_init0(void) {
  96. }
  97. STATIC int rtc_find(mp_obj_t id) {
  98. // given an integer id
  99. int rtc_id = mp_obj_get_int(id);
  100. if (rtc_id >= 0 && rtc_id <= MP_ARRAY_SIZE(machine_rtc_obj)) {
  101. return rtc_id;
  102. }
  103. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
  104. "RTCounter(%d) does not exist", rtc_id));
  105. }
  106. STATIC void rtc_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
  107. machine_rtc_obj_t *self = self_in;
  108. mp_printf(print, "RTCounter(%u)", self->p_rtc->instance_id);
  109. }
  110. /******************************************************************************/
  111. /* MicroPython bindings for machine API */
  112. const nrfx_rtc_config_t machine_rtc_config = {
  113. .prescaler = RTC_FREQ_TO_PRESCALER(RTC_FREQUENCY),
  114. .reliable = 0,
  115. .tick_latency = 0, // ignored when reliable == 0
  116. #ifdef NRF51
  117. .interrupt_priority = 3,
  118. #else
  119. .interrupt_priority = 6,
  120. #endif
  121. };
  122. STATIC mp_obj_t machine_rtc_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
  123. enum { ARG_id, ARG_period, ARG_mode, ARG_callback };
  124. static const mp_arg_t allowed_args[] = {
  125. { MP_QSTR_id, MP_ARG_OBJ, {.u_obj = MP_OBJ_NEW_SMALL_INT(-1)} },
  126. { MP_QSTR_period, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = RTC_FREQUENCY} }, // 1 second
  127. { MP_QSTR_mode, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = RTC_MODE_PERIODIC} },
  128. { MP_QSTR_callback, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
  129. };
  130. // parse args
  131. mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
  132. mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
  133. int rtc_id = rtc_find(args[ARG_id].u_obj);
  134. // const and non-const part of the RTC object.
  135. const machine_rtc_obj_t * self = &machine_rtc_obj[rtc_id];
  136. machine_rtc_config_t *config = self->config;
  137. if (args[ARG_callback].u_obj == mp_const_none) {
  138. config->callback = NULL;
  139. } else if (MP_OBJ_IS_FUN(args[ARG_callback].u_obj)) {
  140. config->callback = args[ARG_callback].u_obj;
  141. } else {
  142. mp_raise_ValueError("callback must be a function");
  143. }
  144. // Periodic or one-shot
  145. if (args[ARG_mode].u_int == RTC_MODE_ONESHOT) {
  146. // One-shot
  147. config->period = 0;
  148. } else {
  149. // Period between the intervals
  150. config->period = args[ARG_period].u_int;
  151. }
  152. // Start the low-frequency clock (if it hasn't been started already)
  153. if (!nrf_clock_lf_is_running()) {
  154. nrf_clock_task_trigger(NRF_CLOCK_TASK_LFCLKSTART);
  155. }
  156. // Make sure it's uninitialized.
  157. nrfx_rtc_uninit(self->p_rtc);
  158. nrfx_rtc_counter_clear(self->p_rtc);
  159. // Initialize and set the correct IRQ.
  160. nrfx_rtc_init(self->p_rtc, &machine_rtc_config, self->handler);
  161. nrfx_rtc_cc_set(self->p_rtc, 0 /*channel*/, args[ARG_period].u_int, true /*enable irq*/);
  162. return MP_OBJ_FROM_PTR(self);
  163. }
  164. /// \method start()
  165. /// Start the RTCounter. Timeout occurs after number of periods
  166. /// in the configured frequency has been reached.
  167. ///
  168. STATIC mp_obj_t machine_rtc_start(mp_obj_t self_in) {
  169. machine_rtc_obj_t * self = MP_OBJ_TO_PTR(self_in);
  170. nrfx_rtc_enable(self->p_rtc);
  171. return mp_const_none;
  172. }
  173. STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_rtc_start_obj, machine_rtc_start);
  174. /// \method stop()
  175. /// Stop the RTCounter.
  176. ///
  177. STATIC mp_obj_t machine_rtc_stop(mp_obj_t self_in) {
  178. machine_rtc_obj_t * self = MP_OBJ_TO_PTR(self_in);
  179. nrfx_rtc_disable(self->p_rtc);
  180. return mp_const_none;
  181. }
  182. STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_rtc_stop_obj, machine_rtc_stop);
  183. /// \method counter()
  184. /// Return the current counter value. Wraps around after about 24 days
  185. /// with the current prescaler (2^24 / 8 = 2097152 seconds).
  186. ///
  187. STATIC mp_obj_t machine_rtc_counter(mp_obj_t self_in) {
  188. machine_rtc_obj_t * self = MP_OBJ_TO_PTR(self_in);
  189. uint32_t counter = nrfx_rtc_counter_get(self->p_rtc);
  190. return MP_OBJ_NEW_SMALL_INT(counter);
  191. }
  192. STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_rtc_counter_obj, machine_rtc_counter);
  193. /// \method deinit()
  194. /// Free resources associated with this RTC.
  195. ///
  196. STATIC mp_obj_t machine_rtc_deinit(mp_obj_t self_in) {
  197. machine_rtc_obj_t * self = MP_OBJ_TO_PTR(self_in);
  198. nrfx_rtc_uninit(self->p_rtc);
  199. return mp_const_none;
  200. }
  201. STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_rtc_deinit_obj, machine_rtc_deinit);
  202. STATIC const mp_rom_map_elem_t machine_rtc_locals_dict_table[] = {
  203. { MP_ROM_QSTR(MP_QSTR_start), MP_ROM_PTR(&machine_rtc_start_obj) },
  204. { MP_ROM_QSTR(MP_QSTR_stop), MP_ROM_PTR(&machine_rtc_stop_obj) },
  205. { MP_ROM_QSTR(MP_QSTR_counter), MP_ROM_PTR(&machine_rtc_counter_obj) },
  206. { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&machine_rtc_deinit_obj) },
  207. // constants
  208. { MP_ROM_QSTR(MP_QSTR_ONESHOT), MP_ROM_INT(RTC_MODE_ONESHOT) },
  209. { MP_ROM_QSTR(MP_QSTR_PERIODIC), MP_ROM_INT(RTC_MODE_PERIODIC) },
  210. { MP_ROM_QSTR(MP_QSTR_FREQUENCY), MP_ROM_INT(RTC_FREQUENCY) },
  211. };
  212. STATIC MP_DEFINE_CONST_DICT(machine_rtc_locals_dict, machine_rtc_locals_dict_table);
  213. const mp_obj_type_t machine_rtcounter_type = {
  214. { &mp_type_type },
  215. .name = MP_QSTR_RTCounter,
  216. .print = rtc_print,
  217. .make_new = machine_rtc_make_new,
  218. .locals_dict = (mp_obj_dict_t*)&machine_rtc_locals_dict
  219. };
  220. #endif // MICROPY_PY_MACHINE_RTCOUNTER