Skip to content

Commit 5e6419c

Browse files
committed
Merge branch 'dhylands-add-timer-deinit'
2 parents 9cd96cf + e70b5db commit 5e6419c

3 files changed

Lines changed: 24 additions & 3 deletions

File tree

stmhal/main.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,7 @@ int main(void) {
549549
storage_flush();
550550

551551
printf("PYB: soft reboot\n");
552+
timer_deinit();
552553

553554
first_soft_reset = false;
554555
goto soft_reset;

stmhal/timer.c

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,26 @@ static uint32_t tim3_counter = 0;
107107
STATIC pyb_timer_obj_t *pyb_timer_obj_all[14];
108108
#define PYB_TIMER_OBJ_ALL_NUM MP_ARRAY_SIZE(pyb_timer_obj_all)
109109

110+
STATIC mp_obj_t pyb_timer_deinit(mp_obj_t self_in);
111+
STATIC mp_obj_t pyb_timer_callback(mp_obj_t self_in, mp_obj_t callback);
112+
110113
void timer_init0(void) {
111114
tim3_counter = 0;
112115
for (uint i = 0; i < PYB_TIMER_OBJ_ALL_NUM; i++) {
113116
pyb_timer_obj_all[i] = NULL;
114117
}
115118
}
116119

120+
// unregister all interrupt sources
121+
void timer_deinit(void) {
122+
for (uint i = 0; i < PYB_TIMER_OBJ_ALL_NUM; i++) {
123+
pyb_timer_obj_t *tim = pyb_timer_obj_all[i];
124+
if (tim != NULL) {
125+
pyb_timer_deinit(tim);
126+
}
127+
}
128+
}
129+
117130
// TIM3 is set-up for the USB CDC interface
118131
void timer_tim3_init(void) {
119132
// set up the timer for USBD CDC
@@ -369,10 +382,15 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_timer_init_obj, 1, pyb_timer_init);
369382
/// \method deinit()
370383
/// Deinitialises the timer.
371384
///
372-
/// *This function is not yet implemented.*
385+
/// Disables the callback (and the associated irq).
386+
/// Stops the timer, and disables the timer peripheral.
373387
STATIC mp_obj_t pyb_timer_deinit(mp_obj_t self_in) {
374-
//pyb_timer_obj_t *self = self_in;
375-
// TODO implement me
388+
pyb_timer_obj_t *self = self_in;
389+
390+
// Disable the interrupt
391+
pyb_timer_callback(self_in, mp_const_none);
392+
393+
HAL_TIM_Base_DeInit(&self->tim);
376394
return mp_const_none;
377395
}
378396
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_timer_deinit_obj, pyb_timer_deinit);

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)