interrupt.c 3.26 KB
Newer Older
1
#include "interrupt.h"
Rahix's avatar
Rahix committed
2 3 4 5 6

#include "api/common.h"
#include "epicardium.h"

#include "py/builtin.h"
7 8 9
#include "py/obj.h"
#include "py/runtime.h"

10 11 12 13 14 15 16
void interrupt_init0(void)
{
	int id;
	for (id = 0; id < EPIC_INT_NUM; id++) {
		MP_STATE_PORT(interrupt_callbacks)[id] = NULL;
	}
}
17

18
void epic_isr_default_handler(api_int_id_t id)
19 20 21
{
	// TODO: check if id is out of rante
	// TOOD: check against mp_const_none
22
	mp_obj_t callback = MP_STATE_PORT(interrupt_callbacks)[id];
23
	if (id < EPIC_INT_NUM) {
24 25
		if (callback) {
			mp_sched_schedule(callback, MP_OBJ_NEW_SMALL_INT(id));
26 27 28 29
		}
	}
}

30
mp_obj_t mp_interrupt_set_callback(mp_obj_t id_in, mp_obj_t callback_obj)
31 32 33 34 35 36 37 38
{
	api_int_id_t id = mp_obj_get_int(id_in);
	if (callback_obj != mp_const_none &&
	    !mp_obj_is_callable(callback_obj)) {
		mp_raise_ValueError("handler must be None or callable");
	}

	// TODO: throw error if id is out of range
39
	if (id < EPIC_INT_NUM) {
40
		MP_STATE_PORT(interrupt_callbacks)[id] = callback_obj;
41 42 43 44 45
	}

	return mp_const_none;
}

46
mp_obj_t mp_interrupt_enable_callback(mp_obj_t id_in)
47 48 49 50
{
	api_int_id_t id = mp_obj_get_int(id_in);

	// TODO: throw error if id is out of range or epic_interrupt_enable failed
51 52
	if (epic_interrupt_enable(id) < 0) {
	}
53 54 55 56

	return mp_const_none;
}

57
mp_obj_t mp_interrupt_disable_callback(mp_obj_t id_in)
58 59 60 61 62 63 64 65 66 67
{
	api_int_id_t id = mp_obj_get_int(id_in);

	// TODO: throw error if id is out of range or epic_interrupt_enable failed
	if (epic_interrupt_disable(id) < 0) {
	}

	return mp_const_none;
}

68
static MP_DEFINE_CONST_FUN_OBJ_2(
69 70
	interrupt_set_callback_obj, mp_interrupt_set_callback
);
71
static MP_DEFINE_CONST_FUN_OBJ_1(
72 73
	interrupt_enable_callback_obj, mp_interrupt_enable_callback
);
74
static MP_DEFINE_CONST_FUN_OBJ_1(
75 76
	interrupt_disable_callback_obj, mp_interrupt_disable_callback
);
77

78
static const mp_rom_map_elem_t interrupt_module_globals_table[] = {
79 80 81 82 83
	{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_interrupt) },
	{ MP_ROM_QSTR(MP_QSTR_set_callback),
	  MP_ROM_PTR(&interrupt_set_callback_obj) },
	{ MP_ROM_QSTR(MP_QSTR_enable_callback),
	  MP_ROM_PTR(&interrupt_enable_callback_obj) },
84 85
	{ MP_ROM_QSTR(MP_QSTR_disable_callback),
	  MP_ROM_PTR(&interrupt_disable_callback_obj) },
86 87 88 89

	/* Interrupt Numbers */
	{ MP_ROM_QSTR(MP_QSTR_RTC_ALARM),
	  MP_OBJ_NEW_SMALL_INT(EPIC_INT_RTC_ALARM) },
90 91
	{ MP_ROM_QSTR(MP_QSTR_BHI160_ACCELEROMETER),
	  MP_OBJ_NEW_SMALL_INT(EPIC_INT_BHI160_ACCELEROMETER) },
92 93
	{ MP_ROM_QSTR(MP_QSTR_BHI160_MAGNETOMETER),
	  MP_OBJ_NEW_SMALL_INT(EPIC_INT_BHI160_MAGNETOMETER) },
koalo's avatar
koalo committed
94 95
	{ MP_ROM_QSTR(MP_QSTR_BHI160_ORIENTATION),
	  MP_OBJ_NEW_SMALL_INT(EPIC_INT_BHI160_ORIENTATION) },
koalo's avatar
koalo committed
96 97
	{ MP_ROM_QSTR(MP_QSTR_BHI160_GYROSCOPE),
	  MP_OBJ_NEW_SMALL_INT(EPIC_INT_BHI160_GYROSCOPE) },
schneider's avatar
schneider committed
98 99
	{ MP_ROM_QSTR(MP_QSTR_MAX30001_ECG),
	  MP_OBJ_NEW_SMALL_INT(EPIC_INT_MAX30001_ECG) },
Arist's avatar
Arist committed
100 101
	{ MP_ROM_QSTR(MP_QSTR_MAX86150),
	  MP_OBJ_NEW_SMALL_INT(EPIC_INT_MAX86150) },
102
	{ MP_ROM_QSTR(MP_QSTR_BLE), MP_OBJ_NEW_SMALL_INT(EPIC_INT_BLE) },
schneider's avatar
schneider committed
103

104
};
105
static MP_DEFINE_CONST_DICT(
106 107 108 109 110 111 112 113 114 115 116 117
	interrupt_module_globals, interrupt_module_globals_table
);

// Define module object.
const mp_obj_module_t interrupt_module = {
	.base    = { &mp_type_module },
	.globals = (mp_obj_dict_t *)&interrupt_module_globals,
};

/* clang-format off */
// Register the module to make it available in Python
MP_REGISTER_MODULE(MP_QSTR_interrupt, interrupt_module, MODULE_INTERRUPT_ENABLED);