mpirq.c 4.8 KB

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