Skip to content

Commit 07b841b

Browse files
kulaarafBartSX
authored andcommitted
[STM32Fxxx] Fix issue #816
Both STM32F0xx and STM32F1xx are using a 16-bit timer as a internal ticker but the mBed ticker needs a 32-bit timer implementation, so the upper part of that 32-bit timer is being calculated in software. Software bug has been fixed where continous HIGH/LOW voltage levels could be observerd for 65ms due to 16-bit timer overflow. Now current value of TIM_MST->CNT is stored in cnt_val and is updated in interrupt context only. This avoids master timer overflow without SlaveCounter update. This fix is only for platforms which already implements a 16-bit timer: F103RB, F070RB, F030R8 Change-Id: I205c70ce155b373c6593ead93ade9ec38993f7f9
1 parent 821c492 commit 07b841b

File tree

2 files changed

+16
-32
lines changed

2 files changed

+16
-32
lines changed

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)