STM32F1: correct the read of timer master value

pull/3144/head
bcostm 2016-10-13 10:04:54 +02:00 committed by Anna Bridge
parent e763793a28
commit 0287eccf5b
3 changed files with 20 additions and 14 deletions

View File

@ -43,10 +43,9 @@ void set_compare(uint16_t count);
extern volatile uint32_t SlaveCounter; extern volatile uint32_t SlaveCounter;
extern volatile uint32_t oc_int_part; extern volatile uint32_t oc_int_part;
extern volatile uint16_t oc_rem_part; extern volatile uint16_t oc_rem_part;
extern volatile uint16_t cnt_val;
void timer_irq_handler(void) { void timer_irq_handler(void) {
cnt_val = TIM_MST->CNT; uint16_t cval = TIM_MST->CNT;
TimMasterHandle.Instance = TIM_MST; TimMasterHandle.Instance = TIM_MST;
@ -68,7 +67,7 @@ void timer_irq_handler(void) {
} else { } else {
if (oc_int_part > 0) { if (oc_int_part > 0) {
set_compare(0xFFFF); set_compare(0xFFFF);
oc_rem_part = cnt_val; // To finish the counter loop the next time oc_rem_part = cval; // To finish the counter loop the next time
oc_int_part--; oc_int_part--;
} else { } else {
us_ticker_irq_handler(); us_ticker_irq_handler();

View File

@ -43,10 +43,9 @@ void set_compare(uint16_t count);
extern volatile uint32_t SlaveCounter; extern volatile uint32_t SlaveCounter;
extern volatile uint32_t oc_int_part; extern volatile uint32_t oc_int_part;
extern volatile uint16_t oc_rem_part; extern volatile uint16_t oc_rem_part;
extern volatile uint16_t cnt_val;
void timer_irq_handler(void) { void timer_irq_handler(void) {
cnt_val = TIM_MST->CNT; uint16_t cval = TIM_MST->CNT;
TimMasterHandle.Instance = TIM_MST; TimMasterHandle.Instance = TIM_MST;
@ -68,7 +67,7 @@ void timer_irq_handler(void) {
} else { } else {
if (oc_int_part > 0) { if (oc_int_part > 0) {
set_compare(0xFFFF); set_compare(0xFFFF);
oc_rem_part = cnt_val; // To finish the counter loop the next time oc_rem_part = cval; // To finish the counter loop the next time
oc_int_part--; oc_int_part--;
} else { } else {
us_ticker_irq_handler(); us_ticker_irq_handler();

View File

@ -38,7 +38,6 @@ static int us_ticker_inited = 0;
volatile uint32_t SlaveCounter = 0; volatile uint32_t SlaveCounter = 0;
volatile uint32_t oc_int_part = 0; volatile uint32_t oc_int_part = 0;
volatile uint16_t oc_rem_part = 0; volatile uint16_t oc_rem_part = 0;
volatile uint16_t cnt_val = 0;
void set_compare(uint16_t count) void set_compare(uint16_t count)
{ {
@ -59,15 +58,24 @@ void us_ticker_init(void)
uint32_t us_ticker_read() uint32_t us_ticker_read()
{ {
uint32_t counter; uint32_t counter, counter2;
if (!us_ticker_inited) us_ticker_init(); if (!us_ticker_inited) us_ticker_init();
// A situation might appear when Master overflows right after Slave is read and before the
//Current value of TIM_MST->CNT is stored in cnt_val and is // new (overflowed) value of Master is read. Which would make the code below consider the
//updated in interrupt context // previous (incorrect) value of Slave and the new value of Master, which would return a
// value in the past. Avoid this by computing consecutive values of the timer until they
// are properly ordered.
counter = (uint32_t)(SlaveCounter << 16); counter = (uint32_t)(SlaveCounter << 16);
counter += cnt_val; counter += TIM_MST->CNT;
while (1) {
return counter; counter2 = (uint32_t)(SlaveCounter << 16);
counter2 += TIM_MST->CNT;
if (counter2 > counter) {
break;
}
counter = counter2;
}
return counter2;
} }
void us_ticker_set_interrupt(timestamp_t timestamp) void us_ticker_set_interrupt(timestamp_t timestamp)