|
| 1 | +/* |
| 2 | + * This file is part of the Micro Python 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 | + |
| 27 | +#include "py/mpconfig.h" |
| 28 | +#include MICROPY_HAL_H |
| 29 | +#include "py/obj.h" |
| 30 | +#include "py/runtime.h" |
| 31 | +#include "py/gc.h" |
| 32 | +#include "inc/hw_types.h" |
| 33 | +#include "interrupt.h" |
| 34 | +#include "pybsleep.h" |
| 35 | +#include "mpcallback.h" |
| 36 | +#include "mpexception.h" |
| 37 | +#include "mperror.h" |
| 38 | + |
| 39 | + |
| 40 | +/****************************************************************************** |
| 41 | + DECLARE PRIVATE FUNCTIONS |
| 42 | + ******************************************************************************/ |
| 43 | +STATIC mpcallback_obj_t *mpcallback_find (mp_obj_t parent); |
| 44 | + |
| 45 | +/****************************************************************************** |
| 46 | + DEFINE PUBLIC DATA |
| 47 | + ******************************************************************************/ |
| 48 | +const mp_arg_t mpcallback_init_args[] = { |
| 49 | + { MP_QSTR_intmode, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, |
| 50 | + { MP_QSTR_handler, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} }, |
| 51 | + { MP_QSTR_priority, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1} }, |
| 52 | + { MP_QSTR_value, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, |
| 53 | + { MP_QSTR_wake, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = PYB_PWR_MODE_ACTIVE_IDLE } }, |
| 54 | +}; |
| 55 | + |
| 56 | +/****************************************************************************** |
| 57 | + DEFINE PUBLIC FUNCTIONS |
| 58 | + ******************************************************************************/ |
| 59 | +void mpcallback_init0 (void) { |
| 60 | + // initialize the callback objects list |
| 61 | + mp_obj_list_init(&MP_STATE_PORT(mpcallback_obj_list), 0); |
| 62 | +} |
| 63 | + |
| 64 | +mp_obj_t mpcallback_new (mp_obj_t parent, mp_obj_t handler, const mp_cb_methods_t *methods) { |
| 65 | + mpcallback_obj_t *self = m_new_obj(mpcallback_obj_t); |
| 66 | + self->base.type = &pyb_callback_type; |
| 67 | + self->handler = handler; |
| 68 | + self->parent = parent; |
| 69 | + self->methods = (mp_cb_methods_t *)methods; |
| 70 | + // remove any old callback if present |
| 71 | + mpcallback_remove(self->parent); |
| 72 | + mp_obj_list_append(&MP_STATE_PORT(mpcallback_obj_list), self); |
| 73 | + return self; |
| 74 | +} |
| 75 | + |
| 76 | +void mpcallback_remove (const mp_obj_t parent) { |
| 77 | + mpcallback_obj_t *callback_obj; |
| 78 | + if ((callback_obj = mpcallback_find(parent))) { |
| 79 | + mp_obj_list_remove(&MP_STATE_PORT(mpcallback_obj_list), callback_obj); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +uint mpcallback_translate_priority (uint priority) { |
| 84 | + if (priority < 1 || priority > 7) { |
| 85 | + nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments)); |
| 86 | + } |
| 87 | + |
| 88 | + switch (priority) { |
| 89 | + case 1: |
| 90 | + return INT_PRIORITY_LVL_7; |
| 91 | + case 2: |
| 92 | + return INT_PRIORITY_LVL_6; |
| 93 | + case 3: |
| 94 | + return INT_PRIORITY_LVL_5; |
| 95 | + case 4: |
| 96 | + return INT_PRIORITY_LVL_4; |
| 97 | + case 5: |
| 98 | + return INT_PRIORITY_LVL_3; |
| 99 | + case 6: |
| 100 | + return INT_PRIORITY_LVL_2; |
| 101 | + case 7: |
| 102 | + return INT_PRIORITY_LVL_1; |
| 103 | + default: |
| 104 | + return INT_PRIORITY_LVL_7; |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +void mpcallback_handler (mp_obj_t self_in) { |
| 109 | + mpcallback_obj_t *self = self_in; |
| 110 | + if (self->handler != mp_const_none) { |
| 111 | + // disable interrupts to avoid nesting |
| 112 | + uint primsk = disable_irq(); |
| 113 | + // when executing code within a handler we must lock the GC to prevent |
| 114 | + // any memory allocations. We must also catch any exceptions. |
| 115 | + gc_lock(); |
| 116 | + nlr_buf_t nlr; |
| 117 | + if (nlr_push(&nlr) == 0) { |
| 118 | + mp_call_function_1(self->handler, self->parent); |
| 119 | + nlr_pop(); |
| 120 | + } |
| 121 | + else { |
| 122 | + // uncaught exception; disable the callback so that it doesn't run again |
| 123 | + self->methods->disable (self->parent); |
| 124 | + self->handler = mp_const_none; |
| 125 | + // printing an exception here will cause a stack overflow that will end up in |
| 126 | + // a hard fault, so is better to signal the uncaught (probably non-recoverable) |
| 127 | + // exception by blinking the system led instead. |
| 128 | + mperror_signal_error(); |
| 129 | + } |
| 130 | + gc_unlock(); |
| 131 | + enable_irq(primsk); |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +/****************************************************************************** |
| 136 | + DEFINE PRIVATE FUNCTIONS |
| 137 | + ******************************************************************************/ |
| 138 | +STATIC mpcallback_obj_t *mpcallback_find (mp_obj_t parent) { |
| 139 | + for (mp_uint_t i = 0; i < MP_STATE_PORT(mpcallback_obj_list).len; i++) { |
| 140 | + // search for the object and then remove it |
| 141 | + mpcallback_obj_t *callback_obj = ((mpcallback_obj_t *)(MP_STATE_PORT(mpcallback_obj_list).items[i])); |
| 142 | + if (callback_obj->parent == parent) { |
| 143 | + return callback_obj; |
| 144 | + } |
| 145 | + } |
| 146 | + return NULL; |
| 147 | +} |
| 148 | + |
| 149 | +/******************************************************************************/ |
| 150 | +// Micro Python bindings |
| 151 | + |
| 152 | +/// \method init() |
| 153 | +/// Initializes the interrupt callback. With no parameters passed, everything will default |
| 154 | +/// to the values assigned to mpcallback_init_args[]. |
| 155 | +STATIC mp_obj_t callback_init(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
| 156 | + mpcallback_obj_t *self = pos_args[0]; |
| 157 | + // this is a bit of a hack, but it let us reuse the callback_create method from our parent |
| 158 | + ((mp_obj_t *)pos_args)[0] = self->parent; |
| 159 | + self->methods->init (n_args, pos_args, kw_args); |
| 160 | + return mp_const_none; |
| 161 | +} |
| 162 | +MP_DEFINE_CONST_FUN_OBJ_KW(callback_init_obj, 1, callback_init); |
| 163 | + |
| 164 | +/// \method enable() |
| 165 | +/// Enables the interrupt callback |
| 166 | +STATIC mp_obj_t callback_enable (mp_obj_t self_in) { |
| 167 | + mpcallback_obj_t *self = self_in; |
| 168 | + self->methods->enable(self->parent); |
| 169 | + return mp_const_none; |
| 170 | +} |
| 171 | +STATIC MP_DEFINE_CONST_FUN_OBJ_1(callback_enable_obj, callback_enable); |
| 172 | + |
| 173 | +/// \method disable() |
| 174 | +/// Disables the interrupt callback |
| 175 | +STATIC mp_obj_t callback_disable (mp_obj_t self_in) { |
| 176 | + mpcallback_obj_t *self = self_in; |
| 177 | + self->methods->disable(self->parent); |
| 178 | + return mp_const_none; |
| 179 | +} |
| 180 | +STATIC MP_DEFINE_CONST_FUN_OBJ_1(callback_disable_obj, callback_disable); |
| 181 | + |
| 182 | +/// \method \call() |
| 183 | +/// Triggers the interrupt callback |
| 184 | +STATIC mp_obj_t callback_call(mp_obj_t self_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) { |
| 185 | + mp_arg_check_num(n_args, n_kw, 0, 0, false); |
| 186 | + mpcallback_handler (self_in); |
| 187 | + return mp_const_none; |
| 188 | +} |
| 189 | + |
| 190 | +STATIC const mp_map_elem_t callback_locals_dict_table[] = { |
| 191 | + // instance methods |
| 192 | + { MP_OBJ_NEW_QSTR(MP_QSTR_init), (mp_obj_t)&callback_init_obj }, |
| 193 | + { MP_OBJ_NEW_QSTR(MP_QSTR_enable), (mp_obj_t)&callback_enable_obj }, |
| 194 | + { MP_OBJ_NEW_QSTR(MP_QSTR_disable), (mp_obj_t)&callback_disable_obj }, |
| 195 | +}; |
| 196 | + |
| 197 | +STATIC MP_DEFINE_CONST_DICT(callback_locals_dict, callback_locals_dict_table); |
| 198 | + |
| 199 | +const mp_obj_type_t pyb_callback_type = { |
| 200 | + { &mp_type_type }, |
| 201 | + .name = MP_QSTR_callback, |
| 202 | + .call = callback_call, |
| 203 | + .locals_dict = (mp_obj_t)&callback_locals_dict, |
| 204 | +}; |
| 205 | + |
0 commit comments