2015-03-04 12:52:39 +00:00
|
|
|
/*
|
2017-06-30 08:22:17 +01:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2015-03-04 12:52:39 +00:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2015 Daniel Campora
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2017-02-21 06:26:21 +00:00
|
|
|
#include <stdio.h>
|
2015-07-05 21:26:12 +01:00
|
|
|
|
2015-03-04 12:52:39 +00:00
|
|
|
#include "py/mpconfig.h"
|
|
|
|
#include "py/obj.h"
|
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "py/gc.h"
|
|
|
|
#include "inc/hw_types.h"
|
|
|
|
#include "interrupt.h"
|
|
|
|
#include "pybsleep.h"
|
|
|
|
#include "mperror.h"
|
2015-09-22 22:20:29 +01:00
|
|
|
#include "mpirq.h"
|
2015-03-04 12:52:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
2015-09-22 22:20:29 +01:00
|
|
|
DECLARE PUBLIC DATA
|
2015-03-04 12:52:39 +00:00
|
|
|
******************************************************************************/
|
2015-09-22 22:20:29 +01:00
|
|
|
const mp_arg_t mp_irq_init_args[] = {
|
|
|
|
{ MP_QSTR_trigger, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
|
|
|
|
{ MP_QSTR_priority, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1} }, // the lowest priority
|
2015-03-04 12:52:39 +00:00
|
|
|
{ MP_QSTR_handler, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
|
2015-09-22 22:20:29 +01:00
|
|
|
{ MP_QSTR_wake, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
|
2015-03-04 12:52:39 +00:00
|
|
|
};
|
|
|
|
|
2015-09-22 22:20:29 +01:00
|
|
|
/******************************************************************************
|
|
|
|
DECLARE PRIVATE DATA
|
|
|
|
******************************************************************************/
|
|
|
|
STATIC uint8_t mp_irq_priorities[] = { INT_PRIORITY_LVL_7, INT_PRIORITY_LVL_6, INT_PRIORITY_LVL_5, INT_PRIORITY_LVL_4,
|
|
|
|
INT_PRIORITY_LVL_3, INT_PRIORITY_LVL_2, INT_PRIORITY_LVL_1 };
|
|
|
|
|
2015-03-04 12:52:39 +00:00
|
|
|
/******************************************************************************
|
|
|
|
DEFINE PUBLIC FUNCTIONS
|
|
|
|
******************************************************************************/
|
2015-09-22 22:20:29 +01:00
|
|
|
void mp_irq_init0 (void) {
|
2015-03-04 12:52:39 +00:00
|
|
|
// initialize the callback objects list
|
2015-09-22 22:20:29 +01:00
|
|
|
mp_obj_list_init(&MP_STATE_PORT(mp_irq_obj_list), 0);
|
2015-03-04 12:52:39 +00:00
|
|
|
}
|
|
|
|
|
2015-09-22 22:20:29 +01:00
|
|
|
mp_obj_t mp_irq_new (mp_obj_t parent, mp_obj_t handler, const mp_irq_methods_t *methods) {
|
2022-04-22 08:09:15 +01:00
|
|
|
mp_irq_obj_t *self = mp_obj_malloc(mp_irq_obj_t, &mp_irq_type);
|
2015-03-04 12:52:39 +00:00
|
|
|
self->handler = handler;
|
|
|
|
self->parent = parent;
|
2015-09-22 22:20:29 +01:00
|
|
|
self->methods = (mp_irq_methods_t *)methods;
|
|
|
|
self->isenabled = true;
|
2015-08-11 10:18:02 +01:00
|
|
|
// remove it in case it was already registered
|
2015-09-22 22:20:29 +01:00
|
|
|
mp_irq_remove(parent);
|
|
|
|
mp_obj_list_append(&MP_STATE_PORT(mp_irq_obj_list), self);
|
2015-03-04 12:52:39 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2015-09-22 22:20:29 +01:00
|
|
|
mp_irq_obj_t *mp_irq_find (mp_obj_t parent) {
|
|
|
|
for (mp_uint_t i = 0; i < MP_STATE_PORT(mp_irq_obj_list).len; i++) {
|
|
|
|
mp_irq_obj_t *callback_obj = ((mp_irq_obj_t *)(MP_STATE_PORT(mp_irq_obj_list).items[i]));
|
2015-03-15 18:53:12 +00:00
|
|
|
if (callback_obj->parent == parent) {
|
|
|
|
return callback_obj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-09-22 22:20:29 +01:00
|
|
|
void mp_irq_wake_all (void) {
|
2015-03-26 12:51:37 +00:00
|
|
|
// re-enable all active callback objects one by one
|
2015-09-22 22:20:29 +01:00
|
|
|
for (mp_uint_t i = 0; i < MP_STATE_PORT(mp_irq_obj_list).len; i++) {
|
|
|
|
mp_irq_obj_t *callback_obj = ((mp_irq_obj_t *)(MP_STATE_PORT(mp_irq_obj_list).items[i]));
|
2015-03-26 12:51:37 +00:00
|
|
|
if (callback_obj->isenabled) {
|
|
|
|
callback_obj->methods->enable(callback_obj->parent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-22 22:20:29 +01:00
|
|
|
void mp_irq_disable_all (void) {
|
2015-09-07 08:23:46 +01:00
|
|
|
// re-enable all active callback objects one by one
|
2015-09-22 22:20:29 +01:00
|
|
|
for (mp_uint_t i = 0; i < MP_STATE_PORT(mp_irq_obj_list).len; i++) {
|
|
|
|
mp_irq_obj_t *callback_obj = ((mp_irq_obj_t *)(MP_STATE_PORT(mp_irq_obj_list).items[i]));
|
2015-09-07 08:23:46 +01:00
|
|
|
callback_obj->methods->disable(callback_obj->parent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-22 22:20:29 +01:00
|
|
|
void mp_irq_remove (const mp_obj_t parent) {
|
|
|
|
mp_irq_obj_t *callback_obj;
|
|
|
|
if ((callback_obj = mp_irq_find(parent))) {
|
|
|
|
mp_obj_list_remove(&MP_STATE_PORT(mp_irq_obj_list), callback_obj);
|
2015-03-04 12:52:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-22 22:20:29 +01:00
|
|
|
uint mp_irq_translate_priority (uint priority) {
|
|
|
|
if (priority < 1 || priority > MP_ARRAY_SIZE(mp_irq_priorities)) {
|
2020-03-02 11:35:22 +00:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("invalid argument(s) value"));
|
2015-03-04 12:52:39 +00:00
|
|
|
}
|
2015-09-22 22:20:29 +01:00
|
|
|
return mp_irq_priorities[priority - 1];
|
2015-03-04 12:52:39 +00:00
|
|
|
}
|
|
|
|
|
2015-09-22 22:20:29 +01:00
|
|
|
void mp_irq_handler (mp_obj_t self_in) {
|
|
|
|
mp_irq_obj_t *self = self_in;
|
2015-03-20 09:59:35 +00:00
|
|
|
if (self && self->handler != mp_const_none) {
|
2015-03-04 12:52:39 +00:00
|
|
|
// when executing code within a handler we must lock the GC to prevent
|
2015-07-05 21:26:12 +01:00
|
|
|
// any memory allocations.
|
2015-03-04 12:52:39 +00:00
|
|
|
gc_lock();
|
|
|
|
nlr_buf_t nlr;
|
|
|
|
if (nlr_push(&nlr) == 0) {
|
|
|
|
mp_call_function_1(self->handler, self->parent);
|
|
|
|
nlr_pop();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// uncaught exception; disable the callback so that it doesn't run again
|
|
|
|
self->methods->disable (self->parent);
|
|
|
|
self->handler = mp_const_none;
|
2015-08-11 10:18:02 +01:00
|
|
|
// signal the error using the heart beat led and
|
|
|
|
// by printing a message
|
2015-07-05 21:26:12 +01:00
|
|
|
printf("Uncaught exception in callback handler\n");
|
|
|
|
mp_obj_print_exception(&mp_plat_print, (mp_obj_t)nlr.ret_val);
|
2015-08-11 10:18:02 +01:00
|
|
|
mperror_signal_error();
|
2015-03-04 12:52:39 +00:00
|
|
|
}
|
|
|
|
gc_unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2017-06-30 08:22:17 +01:00
|
|
|
// MicroPython bindings
|
2015-03-04 12:52:39 +00:00
|
|
|
|
2017-08-30 01:59:58 +01:00
|
|
|
STATIC mp_obj_t mp_irq_init(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
2015-09-22 22:20:29 +01:00
|
|
|
mp_irq_obj_t *self = pos_args[0];
|
2015-03-04 12:52:39 +00:00
|
|
|
// this is a bit of a hack, but it let us reuse the callback_create method from our parent
|
|
|
|
((mp_obj_t *)pos_args)[0] = self->parent;
|
|
|
|
self->methods->init (n_args, pos_args, kw_args);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2015-09-22 22:20:29 +01:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_KW(mp_irq_init_obj, 1, mp_irq_init);
|
2015-03-04 12:52:39 +00:00
|
|
|
|
2015-09-22 22:20:29 +01:00
|
|
|
STATIC mp_obj_t mp_irq_enable (mp_obj_t self_in) {
|
|
|
|
mp_irq_obj_t *self = self_in;
|
2015-03-04 12:52:39 +00:00
|
|
|
self->methods->enable(self->parent);
|
2015-03-26 12:51:37 +00:00
|
|
|
self->isenabled = true;
|
2015-03-04 12:52:39 +00:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2015-09-22 22:20:29 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_irq_enable_obj, mp_irq_enable);
|
2015-03-04 12:52:39 +00:00
|
|
|
|
2015-09-22 22:20:29 +01:00
|
|
|
STATIC mp_obj_t mp_irq_disable (mp_obj_t self_in) {
|
|
|
|
mp_irq_obj_t *self = self_in;
|
2015-03-04 12:52:39 +00:00
|
|
|
self->methods->disable(self->parent);
|
2015-03-26 12:51:37 +00:00
|
|
|
self->isenabled = false;
|
2015-03-04 12:52:39 +00:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2015-09-22 22:20:29 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_irq_disable_obj, mp_irq_disable);
|
|
|
|
|
|
|
|
STATIC mp_obj_t mp_irq_flags (mp_obj_t self_in) {
|
|
|
|
mp_irq_obj_t *self = self_in;
|
|
|
|
return mp_obj_new_int(self->methods->flags(self->parent));
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_irq_flags_obj, mp_irq_flags);
|
2015-03-04 12:52:39 +00:00
|
|
|
|
2017-01-04 13:10:42 +00:00
|
|
|
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) {
|
2015-03-04 12:52:39 +00:00
|
|
|
mp_arg_check_num(n_args, n_kw, 0, 0, false);
|
2015-09-22 22:20:29 +01:00
|
|
|
mp_irq_handler (self_in);
|
2015-03-04 12:52:39 +00:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
2017-08-21 12:34:23 +01:00
|
|
|
STATIC const mp_rom_map_elem_t mp_irq_locals_dict_table[] = {
|
2015-03-04 12:52:39 +00:00
|
|
|
// instance methods
|
2017-08-21 12:34:23 +01:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&mp_irq_init_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_enable), MP_ROM_PTR(&mp_irq_enable_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_disable), MP_ROM_PTR(&mp_irq_disable_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_flags), MP_ROM_PTR(&mp_irq_flags_obj) },
|
2015-03-04 12:52:39 +00:00
|
|
|
};
|
|
|
|
|
2015-09-22 22:20:29 +01:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(mp_irq_locals_dict, mp_irq_locals_dict_table);
|
2015-03-04 12:52:39 +00:00
|
|
|
|
2015-09-22 22:20:29 +01:00
|
|
|
const mp_obj_type_t mp_irq_type = {
|
2015-03-04 12:52:39 +00:00
|
|
|
{ &mp_type_type },
|
2015-09-22 22:20:29 +01:00
|
|
|
.name = MP_QSTR_irq,
|
|
|
|
.call = mp_irq_call,
|
|
|
|
.locals_dict = (mp_obj_t)&mp_irq_locals_dict,
|
2015-03-04 12:52:39 +00:00
|
|
|
};
|
|
|
|
|