|
39 | 39 | typedef struct _machine_timer_obj_t {
|
40 | 40 | mp_obj_base_t base;
|
41 | 41 | nrfx_timer_t p_instance;
|
| 42 | + bool ishard; |
42 | 43 | } machine_timer_obj_t;
|
43 | 44 |
|
44 | 45 | static mp_obj_t machine_timer_callbacks[] = {
|
@@ -89,20 +90,25 @@ static void timer_event_handler(nrf_timer_event_t event_type, void *p_context) {
|
89 | 90 | machine_timer_obj_t *self = p_context;
|
90 | 91 | mp_obj_t callback = machine_timer_callbacks[self->p_instance.instance_id];
|
91 | 92 | if (callback != NULL) {
|
92 |
| - mp_call_function_1(callback, self); |
| 93 | + if (self->ishard) { |
| 94 | + mp_call_function_1(callback, self); |
| 95 | + } else { |
| 96 | + mp_sched_schedule(callback, self); |
| 97 | + } |
93 | 98 | }
|
94 | 99 | }
|
95 | 100 |
|
96 | 101 | /******************************************************************************/
|
97 | 102 | /* MicroPython bindings for machine API */
|
98 | 103 |
|
99 | 104 | static mp_obj_t machine_timer_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
|
100 |
| - enum { ARG_id, ARG_period, ARG_mode, ARG_callback }; |
| 105 | + enum { ARG_id, ARG_period, ARG_mode, ARG_callback, ARG_hard }; |
101 | 106 | static const mp_arg_t allowed_args[] = {
|
102 | 107 | { MP_QSTR_id, MP_ARG_OBJ, {.u_obj = MP_OBJ_NEW_SMALL_INT(-1)} },
|
103 | 108 | { MP_QSTR_period, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1000000} }, // 1 second
|
104 | 109 | { MP_QSTR_mode, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = TIMER_MODE_PERIODIC} },
|
105 | 110 | { MP_QSTR_callback, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
|
| 111 | + { MP_QSTR_hard, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = true} }, |
106 | 112 | };
|
107 | 113 |
|
108 | 114 | // parse args
|
@@ -133,6 +139,7 @@ static mp_obj_t machine_timer_make_new(const mp_obj_type_t *type, size_t n_args,
|
133 | 139 | } else {
|
134 | 140 | mp_raise_ValueError(MP_ERROR_TEXT("callback must be a function"));
|
135 | 141 | }
|
| 142 | + self->ishard = args[ARG_hard].u_bool; |
136 | 143 |
|
137 | 144 | // Timer peripheral usage:
|
138 | 145 | // Every timer instance has a number of capture/compare (CC) registers.
|
|
0 commit comments