Skip to content

Commit 24136af

Browse files
committed
Merge pull request #1763 from BartSX/timer-fxxx-#816
Fix timer #816 issue for STM32F0 and STM32F1
2 parents 39fa25d + 01ff0b9 commit 24136af

File tree

5 files changed

+25
-38
lines changed

5 files changed

+25
-38
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ 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;
4647

4748
// Used to increment the slave counter
4849
void timer_update_irq_handler(void)
@@ -59,7 +60,7 @@ void timer_update_irq_handler(void)
5960
// Used for mbed timeout (channel 1) and HAL tick (channel 2)
6061
void timer_oc_irq_handler(void)
6162
{
62-
uint16_t cval = TIM_MST->CNT;
63+
cnt_val = TIM_MST->CNT;
6364
TimMasterHandle.Instance = TIM_MST;
6465

6566
// Channel 1 for mbed timeout
@@ -71,7 +72,7 @@ void timer_oc_irq_handler(void)
7172
} else {
7273
if (oc_int_part > 0) {
7374
set_compare(0xFFFF);
74-
oc_rem_part = cval; // To finish the counter loop the next time
75+
oc_rem_part = cnt_val; // To finish the counter loop the next time
7576
oc_int_part--;
7677
} else {
7778
us_ticker_irq_handler();

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ 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;
4647

4748
// Used to increment the slave counter
4849
void timer_update_irq_handler(void)
@@ -59,7 +60,7 @@ void timer_update_irq_handler(void)
5960
// Used for mbed timeout (channel 1) and HAL tick (channel 2)
6061
void timer_oc_irq_handler(void)
6162
{
62-
uint16_t cval = TIM_MST->CNT;
63+
cnt_val = TIM_MST->CNT;
6364
TimMasterHandle.Instance = TIM_MST;
6465

6566
// Channel 1 for mbed timeout
@@ -71,7 +72,7 @@ void timer_oc_irq_handler(void)
7172
} else {
7273
if (oc_int_part > 0) {
7374
set_compare(0xFFFF);
74-
oc_rem_part = cval; // To finish the counter loop the next time
75+
oc_rem_part = cnt_val; // To finish the counter loop the next time
7576
oc_int_part--;
7677
} else {
7778
us_ticker_irq_handler();

hal/targets/cmsis/TARGET_STM/TARGET_STM32F1/TARGET_DISCO_F100RB/hal_tick.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@ 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;
4647

4748
void timer_irq_handler(void) {
48-
uint16_t cval = TIM_MST->CNT;
49+
cnt_val= TIM_MST->CNT;
4950

5051
TimMasterHandle.Instance = TIM_MST;
5152

@@ -64,7 +65,7 @@ void timer_irq_handler(void) {
6465
} else {
6566
if (oc_int_part > 0) {
6667
set_compare(0xFFFF);
67-
oc_rem_part = cval; // To finish the counter loop the next time
68+
oc_rem_part = cnt_val; // To finish the counter loop the next time
6869
oc_int_part--;
6970
} else {
7071
us_ticker_irq_handler();

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

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ 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;
4445

4546
void set_compare(uint16_t count) {
4647
TimMasterHandle.Instance = TIM_MST;
@@ -58,24 +59,15 @@ void us_ticker_init(void) {
5859
}
5960

6061
uint32_t us_ticker_read() {
61-
uint32_t counter, counter2;
62+
uint32_t counter;
6263
if (!us_ticker_inited) us_ticker_init();
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.
64+
65+
//Current value of TIM_MST->CNT is stored in cnt_val and is
66+
//updated in interrupt context
6867
counter = (uint32_t)(SlaveCounter << 16);
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;
68+
counter += cnt_val;
69+
70+
return counter;
7971
}
8072

8173
void us_ticker_set_interrupt(timestamp_t timestamp) {

hal/targets/hal/TARGET_STM/TARGET_STM32F1/us_ticker.c

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ 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;
4142

4243
void set_compare(uint16_t count)
4344
{
@@ -58,24 +59,15 @@ void us_ticker_init(void)
5859

5960
uint32_t us_ticker_read()
6061
{
61-
uint32_t counter, counter2;
62+
uint32_t counter;
6263
if (!us_ticker_inited) us_ticker_init();
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.
64+
65+
//Current value of TIM_MST->CNT is stored in cnt_val and is
66+
//updated in interrupt context
6867
counter = (uint32_t)(SlaveCounter << 16);
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;
68+
counter += cnt_val;
69+
70+
return counter;
7971
}
8072

8173
void us_ticker_set_interrupt(timestamp_t timestamp)

0 commit comments

Comments
 (0)