Skip to content

Commit 92a47b4

Browse files
committed
Merge branch 'add-timer-deinit' of github.com:dhylands/micropython into dhylands-add-timer-deinit
2 parents 9cd96cf + 0d81c13 commit 92a47b4

3 files changed

Lines changed: 24 additions & 3 deletions

File tree

stmhal/main.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,8 @@ int main(void) {
548548
printf("PYB: sync filesystems\n");
549549
storage_flush();
550550

551+
timer_deinit();
552+
551553
printf("PYB: soft reboot\n");
552554

553555
first_soft_reset = false;

stmhal/timer.c

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -366,13 +366,20 @@ STATIC mp_obj_t pyb_timer_init(uint n_args, const mp_obj_t *args, mp_map_t *kw_a
366366
}
367367
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_timer_init_obj, 1, pyb_timer_init);
368368

369+
STATIC mp_obj_t pyb_timer_callback(mp_obj_t self_in, mp_obj_t callback);
370+
369371
/// \method deinit()
370372
/// Deinitialises the timer.
371373
///
372-
/// *This function is not yet implemented.*
374+
/// Disables the callback (and the associated irq).
375+
/// Stops the timer, and disables the timer peripheral.
373376
STATIC mp_obj_t pyb_timer_deinit(mp_obj_t self_in) {
374-
//pyb_timer_obj_t *self = self_in;
375-
// TODO implement me
377+
pyb_timer_obj_t *self = self_in;
378+
379+
// Disable the interrupt
380+
pyb_timer_callback(self_in, mp_const_none);
381+
382+
HAL_TIM_Base_DeInit(&self->tim);
376383
return mp_const_none;
377384
}
378385
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_timer_deinit_obj, pyb_timer_deinit);
@@ -464,6 +471,16 @@ const mp_obj_type_t pyb_timer_type = {
464471
.locals_dict = (mp_obj_t)&pyb_timer_locals_dict,
465472
};
466473

474+
// Unregister all interrupt sources
475+
void timer_deinit(void) {
476+
for (uint i = 0; i < PYB_TIMER_OBJ_ALL_NUM; i++) {
477+
pyb_timer_obj_t *tim = pyb_timer_obj_all[i];
478+
if (tim != NULL) {
479+
pyb_timer_deinit(tim);
480+
}
481+
}
482+
}
483+
467484
void timer_irq_handler(uint tim_id) {
468485
if (tim_id - 1 < PYB_TIMER_OBJ_ALL_NUM) {
469486
// get the timer object

stmhal/timer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,6 @@ void timer_tim3_init(void);
4040
void timer_tim5_init(void);
4141
void timer_tim6_init(uint freq);
4242

43+
void timer_deinit(void);
44+
4345
void timer_irq_handler(uint tim_id);

0 commit comments

Comments
 (0)