Skip to content

Commit c6983e3

Browse files
dhylandsdpgeorge
authored andcommitted
stmhal: Fix timer capture/compare interrupt handling for TIM1 and TIM8.
It turns out that TIM1 and TIM8 have their own Capture/Compare interrupt vector. For all of the other timers, the capture/compare interrupt vector is the same as the update vector. So we need to add handlers for these vectors and enable them when using capture/compare callbacks. During testing of this, I also found that passing a channel callback into the channel constructor would not enable interrupts properly. I tested using: ``` >>> pyb.Timer(1, freq=4).channel(1, pyb.Timer.OC_TOGGLE, callback=lambda t: print('.', end='')) ``` I tested the above with channels 1, 4, and 8
1 parent f2a21a2 commit c6983e3

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

stmhal/stm32_it.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,12 @@ void TIM1_TRG_COM_TIM17_IRQHandler(void) {
556556
}
557557
#endif
558558

559+
void TIM1_CC_IRQHandler(void) {
560+
IRQ_ENTER(TIM1_CC_IRQn);
561+
timer_irq_handler(1);
562+
IRQ_EXIT(TIM1_CC_IRQn);
563+
}
564+
559565
void TIM2_IRQHandler(void) {
560566
IRQ_ENTER(TIM2_IRQn);
561567
timer_irq_handler(2);
@@ -581,18 +587,23 @@ void TIM5_IRQHandler(void) {
581587
IRQ_EXIT(TIM5_IRQn);
582588
}
583589

590+
#if defined(TIM6) // STM32F401 doesn't have TIM6
584591
void TIM6_DAC_IRQHandler(void) {
585592
IRQ_ENTER(TIM6_DAC_IRQn);
586593
timer_irq_handler(6);
587594
IRQ_EXIT(TIM6_DAC_IRQn);
588595
}
596+
#endif
589597

598+
#if defined(TIM7) // STM32F401 doesn't have TIM7
590599
void TIM7_IRQHandler(void) {
591600
IRQ_ENTER(TIM7_IRQn);
592601
timer_irq_handler(7);
593602
IRQ_EXIT(TIM7_IRQn);
594603
}
604+
#endif
595605

606+
#if defined(TIM8) // STM32F401 doesn't have TIM8
596607
void TIM8_BRK_TIM12_IRQHandler(void) {
597608
IRQ_ENTER(TIM8_BRK_TIM12_IRQn);
598609
timer_irq_handler(12);
@@ -614,11 +625,18 @@ void TIM8_UP_IRQHandler(void) {
614625
}
615626
#endif
616627

628+
void TIM8_CC_IRQHandler(void) {
629+
IRQ_ENTER(TIM8_CC_IRQn);
630+
timer_irq_handler(8);
631+
IRQ_EXIT(TIM8_CC_IRQn);
632+
}
633+
617634
void TIM8_TRG_COM_TIM14_IRQHandler(void) {
618635
IRQ_ENTER(TIM8_TRG_COM_TIM14_IRQn);
619636
timer_irq_handler(14);
620637
IRQ_EXIT(TIM8_TRG_COM_TIM14_IRQn);
621638
}
639+
#endif
622640

623641
// UART/USART IRQ handlers
624642
void USART1_IRQHandler(void) {

stmhal/timer.c

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,13 @@ STATIC mp_obj_t pyb_timer_init_helper(pyb_timer_obj_t *self, mp_uint_t n_args, c
603603
// set IRQ priority (if not a special timer)
604604
if (self->tim_id != 3 && self->tim_id != 5) {
605605
HAL_NVIC_SetPriority(self->irqn, IRQ_PRI_TIMX, IRQ_SUBPRI_TIMX);
606+
if (self->tim_id == 1) {
607+
HAL_NVIC_SetPriority(TIM1_CC_IRQn, IRQ_PRI_TIMX, IRQ_SUBPRI_TIMX);
608+
#if defined(TIM8)
609+
} else if (self->tim_id == 8) {
610+
HAL_NVIC_SetPriority(TIM8_CC_IRQn, IRQ_PRI_TIMX, IRQ_SUBPRI_TIMX);
611+
#endif
612+
}
606613
}
607614

608615
// init TIM
@@ -932,7 +939,7 @@ STATIC mp_obj_t pyb_timer_channel(mp_uint_t n_args, const mp_obj_t *pos_args, mp
932939
if (chan->callback == mp_const_none) {
933940
HAL_TIM_PWM_Start(&self->tim, TIMER_CHANNEL(chan));
934941
} else {
935-
HAL_TIM_PWM_Start_IT(&self->tim, TIMER_CHANNEL(chan));
942+
pyb_timer_channel_callback(chan, chan->callback);
936943
}
937944
// Start the complimentary channel too (if its supported)
938945
if (IS_TIM_CCXN_INSTANCE(self->tim.Instance, TIMER_CHANNEL(chan))) {
@@ -970,7 +977,7 @@ STATIC mp_obj_t pyb_timer_channel(mp_uint_t n_args, const mp_obj_t *pos_args, mp
970977
if (chan->callback == mp_const_none) {
971978
HAL_TIM_OC_Start(&self->tim, TIMER_CHANNEL(chan));
972979
} else {
973-
HAL_TIM_OC_Start_IT(&self->tim, TIMER_CHANNEL(chan));
980+
pyb_timer_channel_callback(chan, chan->callback);
974981
}
975982
// Start the complimentary channel too (if its supported)
976983
if (IS_TIM_CCXN_INSTANCE(self->tim.Instance, TIMER_CHANNEL(chan))) {
@@ -997,7 +1004,7 @@ STATIC mp_obj_t pyb_timer_channel(mp_uint_t n_args, const mp_obj_t *pos_args, mp
9971004
if (chan->callback == mp_const_none) {
9981005
HAL_TIM_IC_Start(&self->tim, TIMER_CHANNEL(chan));
9991006
} else {
1000-
HAL_TIM_IC_Start_IT(&self->tim, TIMER_CHANNEL(chan));
1007+
pyb_timer_channel_callback(chan, chan->callback);
10011008
}
10021009
break;
10031010
}
@@ -1294,7 +1301,16 @@ STATIC mp_obj_t pyb_timer_channel_callback(mp_obj_t self_in, mp_obj_t callback)
12941301
self->callback = mp_const_none;
12951302
} else if (mp_obj_is_callable(callback)) {
12961303
self->callback = callback;
1297-
HAL_NVIC_EnableIRQ(self->timer->irqn);
1304+
uint8_t tim_id = self->timer->tim_id;
1305+
if (tim_id == 1) {
1306+
HAL_NVIC_EnableIRQ(TIM1_CC_IRQn);
1307+
#if defined(TIM8) // STM32F401 doesn't have a TIM8
1308+
} else if (tim_id == 8) {
1309+
HAL_NVIC_EnableIRQ(TIM8_CC_IRQn);
1310+
#endif
1311+
} else {
1312+
HAL_NVIC_EnableIRQ(self->timer->irqn);
1313+
}
12981314
// start timer, so that it interrupts on overflow
12991315
switch (self->mode) {
13001316
case CHANNEL_MODE_PWM_NORMAL:

0 commit comments

Comments
 (0)