mpirq.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. * 2018 Tobias Badertscher
  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 <stdio.h>
  28. #include "py/runtime.h"
  29. #include "py/gc.h"
  30. #include "lib/utils/mpirq.h"
  31. #if MICROPY_ENABLE_SCHEDULER
  32. /******************************************************************************
  33. DECLARE PUBLIC DATA
  34. ******************************************************************************/
  35. const mp_arg_t mp_irq_init_args[] = {
  36. { MP_QSTR_handler, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
  37. { MP_QSTR_trigger, MP_ARG_INT, {.u_int = 0} },
  38. { MP_QSTR_hard, MP_ARG_BOOL, {.u_bool = false} },
  39. };
  40. /******************************************************************************
  41. DECLARE PRIVATE DATA
  42. ******************************************************************************/
  43. /******************************************************************************
  44. DEFINE PUBLIC FUNCTIONS
  45. ******************************************************************************/
  46. mp_irq_obj_t *mp_irq_new(const mp_irq_methods_t *methods, mp_obj_t parent) {
  47. mp_irq_obj_t *self = m_new0(mp_irq_obj_t, 1);
  48. mp_irq_init(self, methods, parent);
  49. return self;
  50. }
  51. void mp_irq_init(mp_irq_obj_t *self, const mp_irq_methods_t *methods, mp_obj_t parent) {
  52. self->base.type = &mp_irq_type;
  53. self->methods = (mp_irq_methods_t *)methods;
  54. self->parent = parent;
  55. self->handler = mp_const_none;
  56. self->ishard = false;
  57. }
  58. void mp_irq_handler(mp_irq_obj_t *self) {
  59. if (self->handler != mp_const_none) {
  60. if (self->ishard) {
  61. // When executing code within a handler we must lock the scheduler to
  62. // prevent any scheduled callbacks from running, and lock the GC to
  63. // prevent any memory allocations.
  64. mp_sched_lock();
  65. gc_lock();
  66. nlr_buf_t nlr;
  67. if (nlr_push(&nlr) == 0) {
  68. mp_call_function_1(self->handler, self->parent);
  69. nlr_pop();
  70. } else {
  71. // Uncaught exception; disable the callback so that it doesn't run again
  72. self->methods->trigger(self->parent, 0);
  73. self->handler = mp_const_none;
  74. printf("Uncaught exception in IRQ callback handler\n");
  75. mp_obj_print_exception(&mp_plat_print, MP_OBJ_FROM_PTR(nlr.ret_val));
  76. }
  77. gc_unlock();
  78. mp_sched_unlock();
  79. } else {
  80. // Schedule call to user function
  81. mp_sched_schedule(self->handler, self->parent);
  82. }
  83. }
  84. }
  85. /******************************************************************************/
  86. // MicroPython bindings
  87. STATIC mp_obj_t mp_irq_flags(mp_obj_t self_in) {
  88. mp_irq_obj_t *self = MP_OBJ_TO_PTR(self_in);
  89. return mp_obj_new_int(self->methods->info(self->parent, MP_IRQ_INFO_FLAGS));
  90. }
  91. STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_irq_flags_obj, mp_irq_flags);
  92. STATIC mp_obj_t mp_irq_trigger(size_t n_args, const mp_obj_t *args) {
  93. mp_irq_obj_t *self = MP_OBJ_TO_PTR(args[0]);
  94. mp_obj_t ret_obj = mp_obj_new_int(self->methods->info(self->parent, MP_IRQ_INFO_TRIGGERS));
  95. if (n_args == 2) {
  96. // Set trigger
  97. self->methods->trigger(self->parent, mp_obj_get_int(args[1]));
  98. }
  99. return ret_obj;
  100. }
  101. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_irq_trigger_obj, 1, 2, mp_irq_trigger);
  102. 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) {
  103. mp_arg_check_num(n_args, n_kw, 0, 0, false);
  104. mp_irq_handler(MP_OBJ_TO_PTR(self_in));
  105. return mp_const_none;
  106. }
  107. STATIC const mp_rom_map_elem_t mp_irq_locals_dict_table[] = {
  108. { MP_ROM_QSTR(MP_QSTR_flags), MP_ROM_PTR(&mp_irq_flags_obj) },
  109. { MP_ROM_QSTR(MP_QSTR_trigger), MP_ROM_PTR(&mp_irq_trigger_obj) },
  110. };
  111. STATIC MP_DEFINE_CONST_DICT(mp_irq_locals_dict, mp_irq_locals_dict_table);
  112. const mp_obj_type_t mp_irq_type = {
  113. { &mp_type_type },
  114. .name = MP_QSTR_irq,
  115. .call = mp_irq_call,
  116. .locals_dict = (mp_obj_dict_t *)&mp_irq_locals_dict,
  117. };
  118. #endif // MICROPY_ENABLE_SCHEDULER