Skip to content

Commit b36978b

Browse files
committed
[STM32L4XX] Fix timer interrupt handler
1 parent 84e28df commit b36978b

File tree

2 files changed

+34
-24
lines changed
  • hal/targets/cmsis/TARGET_STM/TARGET_STM32L4

2 files changed

+34
-24
lines changed

hal/targets/cmsis/TARGET_STM/TARGET_STM32L4/TARGET_DISCO_L476VG/hal_tick.c

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,28 @@ void us_ticker_irq_handler(void);
4141

4242
void timer_irq_handler(void) {
4343
// Channel 1 for mbed timeout
44-
if (__HAL_TIM_GET_ITSTATUS(&TimMasterHandle, TIM_IT_CC1) == SET) {
45-
us_ticker_irq_handler();
44+
if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC1) == SET) {
45+
if (__HAL_TIM_GET_IT_SOURCE(&TimMasterHandle, TIM_IT_CC1) == SET) {
46+
__HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC1);
47+
us_ticker_irq_handler();
48+
}
4649
}
4750

4851
// Channel 2 for HAL tick
49-
if (__HAL_TIM_GET_ITSTATUS(&TimMasterHandle, TIM_IT_CC2) == SET) {
50-
__HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC2);
51-
uint32_t val = __HAL_TIM_GetCounter(&TimMasterHandle);
52-
if ((val - PreviousVal) >= HAL_TICK_DELAY) {
53-
// Increment HAL variable
54-
HAL_IncTick();
55-
// Prepare next interrupt
56-
__HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_2, val + HAL_TICK_DELAY);
57-
PreviousVal = val;
52+
if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC2) == SET) {
53+
if (__HAL_TIM_GET_IT_SOURCE(&TimMasterHandle, TIM_IT_CC2) == SET) {
54+
__HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC2);
55+
uint32_t val = __HAL_TIM_GetCounter(&TimMasterHandle);
56+
if ((val - PreviousVal) >= HAL_TICK_DELAY) {
57+
// Increment HAL variable
58+
HAL_IncTick();
59+
// Prepare next interrupt
60+
__HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_2, val + HAL_TICK_DELAY);
61+
PreviousVal = val;
5862
#if 0 // For DEBUG only
59-
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_6);
63+
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_6);
6064
#endif
65+
}
6166
}
6267
}
6368
}

hal/targets/cmsis/TARGET_STM/TARGET_STM32L4/TARGET_NUCLEO_L476RG/hal_tick.c

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,28 @@ void us_ticker_irq_handler(void);
4141

4242
void timer_irq_handler(void) {
4343
// Channel 1 for mbed timeout
44-
if (__HAL_TIM_GET_ITSTATUS(&TimMasterHandle, TIM_IT_CC1) == SET) {
45-
us_ticker_irq_handler();
44+
if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC1) == SET) {
45+
if (__HAL_TIM_GET_IT_SOURCE(&TimMasterHandle, TIM_IT_CC1) == SET) {
46+
__HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC1);
47+
us_ticker_irq_handler();
48+
}
4649
}
4750

4851
// Channel 2 for HAL tick
49-
if (__HAL_TIM_GET_ITSTATUS(&TimMasterHandle, TIM_IT_CC2) == SET) {
50-
__HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC2);
51-
uint32_t val = __HAL_TIM_GetCounter(&TimMasterHandle);
52-
if ((val - PreviousVal) >= HAL_TICK_DELAY) {
53-
// Increment HAL variable
54-
HAL_IncTick();
55-
// Prepare next interrupt
56-
__HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_2, val + HAL_TICK_DELAY);
57-
PreviousVal = val;
52+
if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC2) == SET) {
53+
if (__HAL_TIM_GET_IT_SOURCE(&TimMasterHandle, TIM_IT_CC2) == SET) {
54+
__HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC2);
55+
uint32_t val = __HAL_TIM_GetCounter(&TimMasterHandle);
56+
if ((val - PreviousVal) >= HAL_TICK_DELAY) {
57+
// Increment HAL variable
58+
HAL_IncTick();
59+
// Prepare next interrupt
60+
__HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_2, val + HAL_TICK_DELAY);
61+
PreviousVal = val;
5862
#if 0 // For DEBUG only
59-
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_6);
63+
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_6);
6064
#endif
65+
}
6166
}
6267
}
6368
}

0 commit comments

Comments
 (0)