interrupt.c 2.5 KB
Newer Older
1
2
3
4
5
6
7
8
#include "py/obj.h"
#include "py/runtime.h"
#include "py/builtin.h"
#include "epicardium.h"
#include "api/common.h"
#include "mphalport.h"

// TODO: these should be intialized as mp_const_none
9
mp_obj_t callbacks[EPIC_INT_NUM] = {
10
11
12
	0,
};

13
void epic_isr_default_handler(api_int_id_t id)
14
15
16
{
	// TODO: check if id is out of rante
	// TOOD: check against mp_const_none
17
	if (id < EPIC_INT_NUM) {
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
		if (callbacks[id]) {
			mp_sched_schedule(
				callbacks[id], MP_OBJ_NEW_SMALL_INT(id)
			);
		}
	}
}

STATIC mp_obj_t mp_interrupt_set_callback(mp_obj_t id_in, mp_obj_t callback_obj)
{
	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
35
	if (id < EPIC_INT_NUM) {
36
37
38
39
40
41
42
43
44
45
46
		callbacks[id] = callback_obj;
	}

	return mp_const_none;
}

STATIC mp_obj_t mp_interrupt_enable_callback(mp_obj_t id_in)
{
	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
47
48
	if (epic_interrupt_enable(id) < 0) {
	}
49
50
51
52

	return mp_const_none;
}

53
54
55
56
57
58
59
60
61
62
63
64
STATIC mp_obj_t mp_interrupt_disable_callback(mp_obj_t id_in)
{
	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;
}


65
66
67
68
69
70
STATIC MP_DEFINE_CONST_FUN_OBJ_2(
	interrupt_set_callback_obj, mp_interrupt_set_callback
);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(
	interrupt_enable_callback_obj, mp_interrupt_enable_callback
);
71
72
73
STATIC MP_DEFINE_CONST_FUN_OBJ_1(
	interrupt_disable_callback_obj, mp_interrupt_disable_callback
);
74
75
76
77
78
79
80

STATIC const mp_rom_map_elem_t interrupt_module_globals_table[] = {
	{ 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) },
81
82
	{ MP_ROM_QSTR(MP_QSTR_disable_callback),
	  MP_ROM_PTR(&interrupt_disable_callback_obj) },
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
	{ MP_ROM_QSTR(MP_QSTR_BHI160), MP_OBJ_NEW_SMALL_INT(2) },
};
STATIC MP_DEFINE_CONST_DICT(
	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);