Skip to content

Commit 4dcefa6

Browse files
authored
Merge pull request #1913 from BartSX/816f0xx
[STM32F0xx] Fix for #816 issue
2 parents 1f9d283 + bb65961 commit 4dcefa6

File tree

3 files changed

+24
-14
lines changed

3 files changed

+24
-14
lines changed

hal/targets/cmsis/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F030R8/hal_tick.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ void set_compare(uint16_t count);
4343
extern volatile uint32_t SlaveCounter;
4444
extern volatile uint32_t oc_int_part;
4545
extern volatile uint16_t oc_rem_part;
46-
extern volatile uint16_t cnt_val;
4746

4847
// Used to increment the slave counter
4948
void timer_update_irq_handler(void)
@@ -60,7 +59,7 @@ void timer_update_irq_handler(void)
6059
// Used for mbed timeout (channel 1) and HAL tick (channel 2)
6160
void timer_oc_irq_handler(void)
6261
{
63-
cnt_val = TIM_MST->CNT;
62+
uint16_t cval = TIM_MST->CNT;
6463
TimMasterHandle.Instance = TIM_MST;
6564

6665
// Channel 1 for mbed timeout
@@ -72,7 +71,7 @@ void timer_oc_irq_handler(void)
7271
} else {
7372
if (oc_int_part > 0) {
7473
set_compare(0xFFFF);
75-
oc_rem_part = cnt_val; // To finish the counter loop the next time
74+
oc_rem_part = cval; // To finish the counter loop the next time
7675
oc_int_part--;
7776
} else {
7877
us_ticker_irq_handler();
@@ -129,8 +128,10 @@ HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority) {
129128
// Output compare channel 2 interrupt for HAL tick
130129
NVIC_SetVector(TIM_MST_UP_IRQ, (uint32_t)timer_update_irq_handler);
131130
NVIC_EnableIRQ(TIM_MST_UP_IRQ);
131+
NVIC_SetPriority(TIM_MST_UP_IRQ, 0);
132132
NVIC_SetVector(TIM_MST_OC_IRQ, (uint32_t)timer_oc_irq_handler);
133133
NVIC_EnableIRQ(TIM_MST_OC_IRQ);
134+
NVIC_SetPriority(TIM_MST_OC_IRQ, 1);
134135

135136
// Enable interrupts
136137
__HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_UPDATE); // For 32-bit counter

hal/targets/cmsis/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F070RB/hal_tick.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ void set_compare(uint16_t count);
4343
extern volatile uint32_t SlaveCounter;
4444
extern volatile uint32_t oc_int_part;
4545
extern volatile uint16_t oc_rem_part;
46-
extern volatile uint16_t cnt_val;
4746

4847
// Used to increment the slave counter
4948
void timer_update_irq_handler(void)
@@ -60,7 +59,7 @@ void timer_update_irq_handler(void)
6059
// Used for mbed timeout (channel 1) and HAL tick (channel 2)
6160
void timer_oc_irq_handler(void)
6261
{
63-
cnt_val = TIM_MST->CNT;
62+
uint16_t cval = TIM_MST->CNT;
6463
TimMasterHandle.Instance = TIM_MST;
6564

6665
// Channel 1 for mbed timeout
@@ -72,7 +71,7 @@ void timer_oc_irq_handler(void)
7271
} else {
7372
if (oc_int_part > 0) {
7473
set_compare(0xFFFF);
75-
oc_rem_part = cnt_val; // To finish the counter loop the next time
74+
oc_rem_part = cval; // To finish the counter loop the next time
7675
oc_int_part--;
7776
} else {
7877
us_ticker_irq_handler();
@@ -131,8 +130,10 @@ HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority) {
131130
// Output compare channel 2 interrupt for HAL tick
132131
NVIC_SetVector(TIM_MST_UP_IRQ, (uint32_t)timer_update_irq_handler);
133132
NVIC_EnableIRQ(TIM_MST_UP_IRQ);
133+
NVIC_SetPriority(TIM_MST_UP_IRQ, 0);
134134
NVIC_SetVector(TIM_MST_OC_IRQ, (uint32_t)timer_oc_irq_handler);
135135
NVIC_EnableIRQ(TIM_MST_OC_IRQ);
136+
NVIC_SetPriority(TIM_MST_OC_IRQ, 1);
136137

137138
// Enable interrupts
138139
__HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_UPDATE); // For 32-bit counter

hal/targets/hal/TARGET_STM/TARGET_STM32F0/us_ticker.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ static int us_ticker_inited = 0;
4141
volatile uint32_t SlaveCounter = 0;
4242
volatile uint32_t oc_int_part = 0;
4343
volatile uint16_t oc_rem_part = 0;
44-
volatile uint16_t cnt_val = 0;
4544

4645
void set_compare(uint16_t count) {
4746
TimMasterHandle.Instance = TIM_MST;
@@ -59,15 +58,24 @@ void us_ticker_init(void) {
5958
}
6059

6160
uint32_t us_ticker_read() {
62-
uint32_t counter;
61+
uint32_t counter, counter2;
6362
if (!us_ticker_inited) us_ticker_init();
64-
65-
//Current value of TIM_MST->CNT is stored in cnt_val and is
66-
//updated in interrupt context
63+
// A situation might appear when Master overflows right after Slave is read and before the
64+
// new (overflowed) value of Master is read. Which would make the code below consider the
65+
// previous (incorrect) value of Slave and the new value of Master, which would return a
66+
// value in the past. Avoid this by computing consecutive values of the timer until they
67+
// are properly ordered.
6768
counter = (uint32_t)(SlaveCounter << 16);
68-
counter += cnt_val;
69-
70-
return counter;
69+
counter += TIM_MST->CNT;
70+
while (1) {
71+
counter2 = (uint32_t)(SlaveCounter << 16);
72+
counter2 += TIM_MST->CNT;
73+
if (counter2 > counter) {
74+
break;
75+
}
76+
counter = counter2;
77+
}
78+
return counter2;
7179
}
7280

7381
void us_ticker_set_interrupt(timestamp_t timestamp) {

0 commit comments

Comments
 (0)