Skip to content

Commit

Permalink
Fix issue ARMmbed#816
Browse files Browse the repository at this point in the history
NUCLEO_F103RB is 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 - 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.

Change-Id: I09cc262083b66e16affea14c4d2126287519b477
  • Loading branch information
kulaaraf authored and erwango committed Apr 29, 2016
1 parent bad536c commit 8a193c0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ void set_compare(uint16_t count);
extern volatile uint32_t SlaveCounter;
extern volatile uint32_t oc_int_part;
extern volatile uint16_t oc_rem_part;
extern volatile uint16_t cnt_val;

void timer_irq_handler(void) {
uint16_t cval = TIM_MST->CNT;
cnt_val= TIM_MST->CNT;

TimMasterHandle.Instance = TIM_MST;

Expand All @@ -64,7 +65,7 @@ void timer_irq_handler(void) {
} else {
if (oc_int_part > 0) {
set_compare(0xFFFF);
oc_rem_part = cval; // To finish the counter loop the next time
oc_rem_part = cnt_val; // To finish the counter loop the next time
oc_int_part--;
} else {
us_ticker_irq_handler();
Expand Down
24 changes: 8 additions & 16 deletions libraries/mbed/targets/hal/TARGET_STM/TARGET_STM32F1/us_ticker.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ static int us_ticker_inited = 0;
volatile uint32_t SlaveCounter = 0;
volatile uint32_t oc_int_part = 0;
volatile uint16_t oc_rem_part = 0;
volatile uint16_t cnt_val = 0;

void set_compare(uint16_t count)
{
Expand All @@ -58,24 +59,15 @@ void us_ticker_init(void)

uint32_t us_ticker_read()
{
uint32_t counter, counter2;
uint32_t counter;
if (!us_ticker_inited) us_ticker_init();
// A situation might appear when Master overflows right after Slave is read and before the
// new (overflowed) value of Master is read. Which would make the code below consider the
// 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.

//Current value of TIM_MST->CNT is stored in cnt_val and is't
//updated in interrupt context
counter = (uint32_t)(SlaveCounter << 16);
counter += TIM_MST->CNT;
while (1) {
counter2 = (uint32_t)(SlaveCounter << 16);
counter2 += TIM_MST->CNT;
if (counter2 > counter) {
break;
}
counter = counter2;
}
return counter2;
counter += cnt_val;

return counter;
}

void us_ticker_set_interrupt(timestamp_t timestamp)
Expand Down

0 comments on commit 8a193c0

Please sign in to comment.