Skip to content

Commit 84e28df

Browse files
committed
[STM32L1XX] Fix timer interrupt handler
1 parent 2780436 commit 84e28df

File tree

3 files changed

+51
-36
lines changed

3 files changed

+51
-36
lines changed

hal/targets/cmsis/TARGET_STM/TARGET_STM32L1/TARGET_MOTE_L152RC/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_STM32L1/TARGET_NUCLEO_L152RE/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_STM32L1/TARGET_NZ32_SC151/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)