|
50 | 50 | const mp_obj_type_t machine_timer_type;
|
51 | 51 |
|
52 | 52 | static mp_obj_t machine_timer_init_helper(machine_timer_obj_t *self, mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
|
| 53 | +static mp_obj_t machine_timer_deinit(mp_obj_t self_in); |
53 | 54 |
|
54 | 55 | void machine_timer_deinit_all(void) {
|
55 | 56 | // Disable, deallocate and remove all timers from list
|
56 | 57 | machine_timer_obj_t **t = &MP_STATE_PORT(machine_timer_obj_head);
|
57 | 58 | while (*t != NULL) {
|
58 |
| - machine_timer_disable(*t); |
| 59 | + machine_timer_deinit(*t); |
59 | 60 | machine_timer_obj_t *next = (*t)->next;
|
60 | 61 | m_del_obj(machine_timer_obj_t, *t);
|
61 | 62 | *t = next;
|
@@ -96,6 +97,7 @@ machine_timer_obj_t *machine_timer_create(mp_uint_t timer) {
|
96 | 97 | self = mp_obj_malloc(machine_timer_obj_t, &machine_timer_type);
|
97 | 98 | self->group = group;
|
98 | 99 | self->index = index;
|
| 100 | + self->handle = NULL; |
99 | 101 |
|
100 | 102 | // Add the timer to the linked-list of timers
|
101 | 103 | self->next = MP_STATE_PORT(machine_timer_obj_head);
|
@@ -131,9 +133,8 @@ void machine_timer_disable(machine_timer_obj_t *self) {
|
131 | 133 | }
|
132 | 134 |
|
133 | 135 | if (self->handle) {
|
134 |
| - // Free the interrupt handler. |
135 |
| - esp_intr_free(self->handle); |
136 |
| - self->handle = NULL; |
| 136 | + // Disable the interrupt |
| 137 | + ESP_ERROR_CHECK(esp_intr_disable(self->handle)); |
137 | 138 | }
|
138 | 139 |
|
139 | 140 | // We let the disabled timer stay in the list, as it might be
|
@@ -167,6 +168,10 @@ void machine_timer_enable(machine_timer_obj_t *self, void (*timer_isr)) {
|
167 | 168 | // Allocate and enable the alarm interrupt.
|
168 | 169 | timer_ll_enable_intr(self->hal_context.dev, TIMER_LL_EVENT_ALARM(self->index), false);
|
169 | 170 | timer_ll_clear_intr_status(self->hal_context.dev, TIMER_LL_EVENT_ALARM(self->index));
|
| 171 | + if (self->handle) { |
| 172 | + ESP_ERROR_CHECK(esp_intr_free(self->handle)); |
| 173 | + self->handle = NULL; |
| 174 | + } |
170 | 175 | ESP_ERROR_CHECK(
|
171 | 176 | esp_intr_alloc(timer_group_periph_signals.groups[self->group].timer_irq_id[self->index],
|
172 | 177 | TIMER_FLAGS, timer_isr, self, &self->handle)
|
@@ -233,7 +238,13 @@ static mp_obj_t machine_timer_init_helper(machine_timer_obj_t *self, mp_uint_t n
|
233 | 238 | }
|
234 | 239 |
|
235 | 240 | static mp_obj_t machine_timer_deinit(mp_obj_t self_in) {
|
236 |
| - machine_timer_disable(self_in); |
| 241 | + machine_timer_obj_t *self = self_in; |
| 242 | + |
| 243 | + machine_timer_disable(self); |
| 244 | + if (self->handle) { |
| 245 | + ESP_ERROR_CHECK(esp_intr_free(self->handle)); |
| 246 | + self->handle = NULL; |
| 247 | + } |
237 | 248 |
|
238 | 249 | return mp_const_none;
|
239 | 250 | }
|
|
0 commit comments