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