3131#include "py/gc.h"
3232#include "lib/utils/mpirq.h"
3333
34+ #if MICROPY_ENABLE_SCHEDULER
35+
3436/******************************************************************************
3537 DECLARE PUBLIC DATA
3638 ******************************************************************************/
3739
3840const mp_arg_t mp_irq_init_args [] = {
39- { MP_QSTR_handler , MP_ARG_OBJ , {.u_rom_obj = MP_ROM_PTR ( & mp_const_none_obj ) } },
41+ { MP_QSTR_handler , MP_ARG_OBJ , {.u_rom_obj = MP_ROM_NONE } },
4042 { MP_QSTR_trigger , MP_ARG_INT , {.u_int = 0 } },
4143 { MP_QSTR_hard , MP_ARG_BOOL , {.u_bool = false} },
4244};
@@ -51,19 +53,25 @@ const mp_arg_t mp_irq_init_args[] = {
5153
5254mp_irq_obj_t * mp_irq_new (const mp_irq_methods_t * methods , mp_obj_t parent ) {
5355 mp_irq_obj_t * self = m_new0 (mp_irq_obj_t , 1 );
56+ mp_irq_init (self , methods , parent );
57+ return self ;
58+ }
59+
60+ void mp_irq_init (mp_irq_obj_t * self , const mp_irq_methods_t * methods , mp_obj_t parent ) {
5461 self -> base .type = & mp_irq_type ;
55- self -> methods = (mp_irq_methods_t * )methods ;
62+ self -> methods = (mp_irq_methods_t * )methods ;
5663 self -> parent = parent ;
5764 self -> handler = mp_const_none ;
5865 self -> ishard = false;
59- return self ;
6066}
6167
6268void mp_irq_handler (mp_irq_obj_t * self ) {
6369 if (self -> handler != mp_const_none ) {
6470 if (self -> ishard ) {
65- // When executing code within a handler we must lock the GC to prevent
66- // any memory allocations.
71+ // When executing code within a handler we must lock the scheduler to
72+ // prevent any scheduled callbacks from running, and lock the GC to
73+ // prevent any memory allocations.
74+ mp_sched_lock ();
6775 gc_lock ();
6876 nlr_buf_t nlr ;
6977 if (nlr_push (& nlr ) == 0 ) {
@@ -77,6 +85,7 @@ void mp_irq_handler(mp_irq_obj_t *self) {
7785 mp_obj_print_exception (& mp_plat_print , MP_OBJ_FROM_PTR (nlr .ret_val ));
7886 }
7987 gc_unlock ();
88+ mp_sched_unlock ();
8089 } else {
8190 // Schedule call to user function
8291 mp_sched_schedule (self -> handler , self -> parent );
@@ -120,5 +129,7 @@ const mp_obj_type_t mp_irq_type = {
120129 { & mp_type_type },
121130 .name = MP_QSTR_irq ,
122131 .call = mp_irq_call ,
123- .locals_dict = (mp_obj_dict_t * )& mp_irq_locals_dict ,
132+ .locals_dict = (mp_obj_dict_t * )& mp_irq_locals_dict ,
124133};
134+
135+ #endif // MICROPY_ENABLE_SCHEDULER
0 commit comments