Skip to content

Commit fe699c3

Browse files
committed
【添加】引脚中断功能
1 parent cb2280b commit fe699c3

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

port/genhdr/qstrdefs.generated.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,4 +765,5 @@ QDEF(MP_QSTR_set_color, (const byte*)"\x25\x09" "set_color")
765765
QDEF(MP_QSTR_file_crc32, (const byte*)"\x6f\x0a" "file_crc32")
766766
QDEF(MP_QSTR_list_device, (const byte*)"\x20\x0b" "list_device")
767767
QDEF(MP_QSTR_show_image, (const byte*)"\xde\x0a" "show_image")
768+
QDEF(MP_QSTR_IRQ, (const byte*)"\xaf\x03" "IRQ")
768769
// This file was automatically generated by makeqstrdata.py

port/modules/machine/machine_pin.c

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,12 +224,43 @@ STATIC mp_uint_t machine_pin_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_
224224
return MP_STREAM_ERROR;
225225
}
226226

227+
STATIC void machine_pin_isr_handler(void *arg) {
228+
machine_pin_obj_t *self = arg;
229+
mp_sched_schedule(self->pin_isr_cb, MP_OBJ_FROM_PTR(self));
230+
}
231+
232+
// pin.irq(handler=None, trigger=IRQ_FALLING|IRQ_RISING)
233+
STATIC mp_obj_t machine_pin_irq(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
234+
enum { ARG_handler, ARG_trigger, ARG_wake };
235+
static const mp_arg_t allowed_args[] = {
236+
{ MP_QSTR_handler, MP_ARG_OBJ, {.u_obj = mp_const_none} },
237+
{ MP_QSTR_trigger, MP_ARG_INT, {.u_int = PIN_IRQ_MODE_RISING} },
238+
};
239+
machine_pin_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
240+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
241+
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
242+
243+
if (n_args > 1 || kw_args->used != 0) {
244+
// configure irq
245+
self->pin_isr_cb = args[ARG_handler].u_obj;
246+
uint32_t trigger = args[ARG_trigger].u_int;
247+
248+
rt_pin_mode(self->pin, PIN_MODE_INPUT_PULLUP);
249+
rt_pin_attach_irq(self->pin, trigger, machine_pin_isr_handler, (void*)self);
250+
rt_pin_irq_enable(self->pin, PIN_IRQ_ENABLE);
251+
}
252+
253+
return mp_const_none;
254+
}
255+
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_pin_irq_obj, 1, machine_pin_irq);
256+
227257
STATIC const mp_rom_map_elem_t machine_pin_locals_dict_table[] = {
228258
// instance methods
229259
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_pin_init_obj) },
230260
{ MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&machine_pin_value_obj) },
231261
{ MP_ROM_QSTR(MP_QSTR_name), MP_ROM_PTR(&machine_pin_name_obj) },
232262
{ MP_ROM_QSTR(MP_QSTR_pin), MP_ROM_PTR(&machine_pin_pin_obj) },
263+
{ MP_ROM_QSTR(MP_QSTR_irq), MP_ROM_PTR(&machine_pin_irq_obj) },
233264

234265
// class constants
235266
{ MP_ROM_QSTR(MP_QSTR_ALT_OD), MP_ROM_INT(GPIO_MODE_AF_OD) },
@@ -241,9 +272,9 @@ STATIC const mp_rom_map_elem_t machine_pin_locals_dict_table[] = {
241272
{ MP_ROM_QSTR(MP_QSTR_PULL_DOWN), MP_ROM_INT(GPIO_PULLDOWN) },
242273
{ MP_ROM_QSTR(MP_QSTR_PULL_NONE), MP_ROM_INT(GPIO_NOPULL) },
243274
{ MP_ROM_QSTR(MP_QSTR_PULL_UP), MP_ROM_INT(GPIO_PULLUP) },
244-
{ MP_ROM_QSTR(MP_QSTR_IRQ_RISING), MP_ROM_INT(GPIO_MODE_IT_RISING) },
245-
{ MP_ROM_QSTR(MP_QSTR_IRQ_FALLING), MP_ROM_INT(GPIO_MODE_IT_FALLING) },
246-
{ MP_ROM_QSTR(MP_QSTR_IRQ_RISING_FALLING), MP_ROM_INT(GPIO_MODE_IT_RISING_FALLING) },
275+
{ MP_ROM_QSTR(MP_QSTR_IRQ_RISING), MP_ROM_INT(PIN_IRQ_MODE_RISING) },
276+
{ MP_ROM_QSTR(MP_QSTR_IRQ_FALLING), MP_ROM_INT(PIN_IRQ_MODE_FALLING) },
277+
{ MP_ROM_QSTR(MP_QSTR_IRQ_RISING_FALLING), MP_ROM_INT(PIN_IRQ_MODE_RISING_FALLING) },
247278
};
248279

249280
STATIC MP_DEFINE_CONST_DICT(machine_pin_locals_dict, machine_pin_locals_dict_table);

port/modules/machine/modmachine.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ typedef struct _machine_pin_obj_t {
4747
mp_obj_base_t base;
4848
char name[RT_NAME_MAX];
4949
uint32_t pin;
50+
mp_obj_t pin_isr_cb;
5051
} machine_pin_obj_t;
5152

5253
#endif // _MODMACHINE_H

0 commit comments

Comments
 (0)