mpirq.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2015 Daniel Campora
  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 <stdio.h>
  27. #include "py/mpconfig.h"
  28. #include "py/obj.h"
  29. #include "py/runtime.h"
  30. #include "py/gc.h"
  31. #include "inc/hw_types.h"
  32. #include "interrupt.h"
  33. #include "pybsleep.h"
  34. #include "mpexception.h"
  35. #include "mperror.h"
  36. #include "mpirq.h"
  37. /******************************************************************************
  38. DECLARE PUBLIC DATA
  39. ******************************************************************************/
  40. const mp_arg_t mp_irq_init_args[] = {
  41. { MP_QSTR_trigger, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
  42. { MP_QSTR_priority, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1} }, // the lowest priority
  43. { MP_QSTR_handler, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
  44. { MP_QSTR_wake, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
  45. };
  46. /******************************************************************************
  47. DECLARE PRIVATE DATA
  48. ******************************************************************************/
  49. STATIC uint8_t mp_irq_priorities[] = { INT_PRIORITY_LVL_7, INT_PRIORITY_LVL_6, INT_PRIORITY_LVL_5, INT_PRIORITY_LVL_4,
  50. INT_PRIORITY_LVL_3, INT_PRIORITY_LVL_2, INT_PRIORITY_LVL_1 };
  51. /******************************************************************************
  52. DEFINE PUBLIC FUNCTIONS
  53. ******************************************************************************/
  54. void mp_irq_init0 (void) {
  55. // initialize the callback objects list
  56. mp_obj_list_init(&MP_STATE_PORT(mp_irq_obj_list), 0);
  57. }
  58. mp_obj_t mp_irq_new (mp_obj_t parent, mp_obj_t handler, const mp_irq_methods_t *methods) {
  59. mp_irq_obj_t *self = m_new_obj(mp_irq_obj_t);
  60. self->base.type = &mp_irq_type;
  61. self->handler = handler;
  62. self->parent = parent;
  63. self->methods = (mp_irq_methods_t *)methods;
  64. self->isenabled = true;
  65. // remove it in case it was already registered
  66. mp_irq_remove(parent);
  67. mp_obj_list_append(&MP_STATE_PORT(mp_irq_obj_list), self);
  68. return self;
  69. }
  70. mp_irq_obj_t *mp_irq_find (mp_obj_t parent) {
  71. for (mp_uint_t i = 0; i < MP_STATE_PORT(mp_irq_obj_list).len; i++) {
  72. mp_irq_obj_t *callback_obj = ((mp_irq_obj_t *)(MP_STATE_PORT(mp_irq_obj_list).items[i]));
  73. if (callback_obj->parent == parent) {
  74. return callback_obj;
  75. }
  76. }
  77. return NULL;
  78. }
  79. void mp_irq_wake_all (void) {
  80. // re-enable all active callback objects one by one
  81. for (mp_uint_t i = 0; i < MP_STATE_PORT(mp_irq_obj_list).len; i++) {
  82. mp_irq_obj_t *callback_obj = ((mp_irq_obj_t *)(MP_STATE_PORT(mp_irq_obj_list).items[i]));
  83. if (callback_obj->isenabled) {
  84. callback_obj->methods->enable(callback_obj->parent);
  85. }
  86. }
  87. }
  88. void mp_irq_disable_all (void) {
  89. // re-enable all active callback objects one by one
  90. for (mp_uint_t i = 0; i < MP_STATE_PORT(mp_irq_obj_list).len; i++) {
  91. mp_irq_obj_t *callback_obj = ((mp_irq_obj_t *)(MP_STATE_PORT(mp_irq_obj_list).items[i]));
  92. callback_obj->methods->disable(callback_obj->parent);
  93. }
  94. }
  95. void mp_irq_remove (const mp_obj_t parent) {
  96. mp_irq_obj_t *callback_obj;
  97. if ((callback_obj = mp_irq_find(parent))) {
  98. mp_obj_list_remove(&MP_STATE_PORT(mp_irq_obj_list), callback_obj);
  99. }
  100. }
  101. uint mp_irq_translate_priority (uint priority) {
  102. if (priority < 1 || priority > MP_ARRAY_SIZE(mp_irq_priorities)) {
  103. mp_raise_ValueError(mpexception_value_invalid_arguments);
  104. }
  105. return mp_irq_priorities[priority - 1];
  106. }
  107. void mp_irq_handler (mp_obj_t self_in) {
  108. mp_irq_obj_t *self = self_in;
  109. if (self && self->handler != mp_const_none) {
  110. // when executing code within a handler we must lock the GC to prevent
  111. // any memory allocations.
  112. gc_lock();
  113. nlr_buf_t nlr;
  114. if (nlr_push(&nlr) == 0) {
  115. mp_call_function_1(self->handler, self->parent);
  116. nlr_pop();
  117. }
  118. else {
  119. // uncaught exception; disable the callback so that it doesn't run again
  120. self->methods->disable (self->parent);
  121. self->handler = mp_const_none;
  122. // signal the error using the heart beat led and
  123. // by printing a message
  124. printf("Uncaught exception in callback handler\n");
  125. mp_obj_print_exception(&mp_plat_print, (mp_obj_t)nlr.ret_val);
  126. mperror_signal_error();
  127. }
  128. gc_unlock();
  129. }
  130. }
  131. /******************************************************************************/
  132. // MicroPython bindings
  133. STATIC mp_obj_t mp_irq_init(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
  134. mp_irq_obj_t *self = pos_args[0];
  135. // this is a bit of a hack, but it let us reuse the callback_create method from our parent
  136. ((mp_obj_t *)pos_args)[0] = self->parent;
  137. self->methods->init (n_args, pos_args, kw_args);
  138. return mp_const_none;
  139. }
  140. MP_DEFINE_CONST_FUN_OBJ_KW(mp_irq_init_obj, 1, mp_irq_init);
  141. STATIC mp_obj_t mp_irq_enable (mp_obj_t self_in) {
  142. mp_irq_obj_t *self = self_in;
  143. self->methods->enable(self->parent);
  144. self->isenabled = true;
  145. return mp_const_none;
  146. }
  147. STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_irq_enable_obj, mp_irq_enable);
  148. STATIC mp_obj_t mp_irq_disable (mp_obj_t self_in) {
  149. mp_irq_obj_t *self = self_in;
  150. self->methods->disable(self->parent);
  151. self->isenabled = false;
  152. return mp_const_none;
  153. }
  154. STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_irq_disable_obj, mp_irq_disable);
  155. STATIC mp_obj_t mp_irq_flags (mp_obj_t self_in) {
  156. mp_irq_obj_t *self = self_in;
  157. return mp_obj_new_int(self->methods->flags(self->parent));
  158. }
  159. STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_irq_flags_obj, mp_irq_flags);
  160. STATIC mp_obj_t mp_irq_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  161. mp_arg_check_num(n_args, n_kw, 0, 0, false);
  162. mp_irq_handler (self_in);
  163. return mp_const_none;
  164. }
  165. STATIC const mp_rom_map_elem_t mp_irq_locals_dict_table[] = {
  166. // instance methods
  167. { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&mp_irq_init_obj) },
  168. { MP_ROM_QSTR(MP_QSTR_enable), MP_ROM_PTR(&mp_irq_enable_obj) },
  169. { MP_ROM_QSTR(MP_QSTR_disable), MP_ROM_PTR(&mp_irq_disable_obj) },
  170. { MP_ROM_QSTR(MP_QSTR_flags), MP_ROM_PTR(&mp_irq_flags_obj) },
  171. };
  172. STATIC MP_DEFINE_CONST_DICT(mp_irq_locals_dict, mp_irq_locals_dict_table);
  173. const mp_obj_type_t mp_irq_type = {
  174. { &mp_type_type },
  175. .name = MP_QSTR_irq,
  176. .call = mp_irq_call,
  177. .locals_dict = (mp_obj_t)&mp_irq_locals_dict,
  178. };