Skip to content

Commit 7499177

Browse files
authored
Merge pull request #3076 from bcostm/fix_issue-2910_nucleo-f103rb
STM32F1: Correct timer master value reading
2 parents 5c89e1f + 8a570cc commit 7499177

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

targets/TARGET_STM/TARGET_STM32F1/TARGET_BLUEPILL_F103C8/device/hal_tick.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,9 @@ 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
void timer_irq_handler(void) {
49-
cnt_val = TIM_MST->CNT;
48+
uint16_t cval = TIM_MST->CNT;
5049

5150
TimMasterHandle.Instance = TIM_MST;
5251

@@ -68,7 +67,7 @@ void timer_irq_handler(void) {
6867
} else {
6968
if (oc_int_part > 0) {
7069
set_compare(0xFFFF);
71-
oc_rem_part = cnt_val; // To finish the counter loop the next time
70+
oc_rem_part = cval; // To finish the counter loop the next time
7271
oc_int_part--;
7372
} else {
7473
us_ticker_irq_handler();

targets/TARGET_STM/TARGET_STM32F1/TARGET_NUCLEO_F103RB/device/hal_tick.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,9 @@ 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
void timer_irq_handler(void) {
49-
cnt_val = TIM_MST->CNT;
48+
uint16_t cval = TIM_MST->CNT;
5049

5150
TimMasterHandle.Instance = TIM_MST;
5251

@@ -68,7 +67,7 @@ void timer_irq_handler(void) {
6867
} else {
6968
if (oc_int_part > 0) {
7069
set_compare(0xFFFF);
71-
oc_rem_part = cnt_val; // To finish the counter loop the next time
70+
oc_rem_part = cval; // To finish the counter loop the next time
7271
oc_int_part--;
7372
} else {
7473
us_ticker_irq_handler();

targets/TARGET_STM/TARGET_STM32F1/us_ticker.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ static int us_ticker_inited = 0;
3838
volatile uint32_t SlaveCounter = 0;
3939
volatile uint32_t oc_int_part = 0;
4040
volatile uint16_t oc_rem_part = 0;
41-
volatile uint16_t cnt_val = 0;
4241

4342
void set_compare(uint16_t count)
4443
{
@@ -59,15 +58,24 @@ void us_ticker_init(void)
5958

6059
uint32_t us_ticker_read()
6160
{
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)