Skip to content

Commit

Permalink
Fix problem with low level lp_ticker STM wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
Tharazi97 committed Aug 30, 2019
1 parent 399b517 commit 4551399
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 32 deletions.
46 changes: 17 additions & 29 deletions TESTS/mbed_drivers/lp_ticker/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

using utest::v1::Case;

static const int test_timeout = 10;
static const int test_timeout = 1000;

#define TICKER_COUNT 16
#define MULTI_TICKER_TIME_MS 100
Expand Down Expand Up @@ -71,40 +71,28 @@ void increment_multi_counter(void)
*/
void test_multi_ticker(void)
{
LowPowerTicker ticker[TICKER_COUNT];
const uint32_t extra_wait = 10; // extra 10ms wait time
for(uint64_t k = 0; k<10000; k++)
{
//ThisThread::sleep_for(750);

multi_counter = 0;
for (int i = 0; i < TICKER_COUNT; i++) {
ticker[i].attach_us(callback(increment_multi_counter), MULTI_TICKER_TIME_MS * 1000);
}
LowPowerTicker ticker[TICKER_COUNT];
const uint32_t extra_wait = 2; // extra 2ms wait time

ThisThread::sleep_for(MULTI_TICKER_TIME_MS + extra_wait);
TEST_ASSERT_EQUAL(TICKER_COUNT, multi_counter);
multi_counter = 0;

for (int i = 0; i < TICKER_COUNT; i++) {
ticker[i].detach();
}
// Because detach calls schedule_interrupt in some circumstances
// (e.g. when head event is removed), it's good to check if
// no more callbacks were triggered during detaching.
TEST_ASSERT_EQUAL(TICKER_COUNT, multi_counter);

multi_counter = 0;
for (int i = 0; i < TICKER_COUNT; i++) {
ticker[i].attach_us(callback(increment_multi_counter), (MULTI_TICKER_TIME_MS + i) * 1000);
}
for (int i = 0; i < TICKER_COUNT; i++) {
ticker[i].attach_us(callback(increment_multi_counter), MULTI_TICKER_TIME_MS * 1000);
}

ThisThread::sleep_for(MULTI_TICKER_TIME_MS + extra_wait);

ThisThread::sleep_for(MULTI_TICKER_TIME_MS + TICKER_COUNT + extra_wait);
TEST_ASSERT_EQUAL(TICKER_COUNT, multi_counter);
TEST_ASSERT_EQUAL(TICKER_COUNT, multi_counter);

for (int i = 0; i < TICKER_COUNT; i++) {
ticker[i].detach();
for (int i = 0; i < TICKER_COUNT; i++) {
ticker[i].detach();
}
printf("%llu\n",k);
}
// Because detach calls schedule_interrupt in some circumstances
// (e.g. when head event is removed), it's good to check if
// no more callbacks were triggered during detaching.
TEST_ASSERT_EQUAL(TICKER_COUNT, multi_counter);
}

/** Test multi callback time
Expand Down
54 changes: 51 additions & 3 deletions targets/TARGET_STM/lp_ticker.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
* between is unreliable */
#define LP_TIMER_SAFE_GUARD 5


LPTIM_HandleTypeDef LptimHandle;

const ticker_info_t *lp_ticker_get_info()
Expand All @@ -73,6 +72,10 @@ const ticker_info_t *lp_ticker_get_info()
volatile uint8_t lp_Fired = 0;
/* Flag and stored counter to handle delayed programing at low level */
volatile bool lp_delayed_prog = false;

volatile bool future_event_flag = false;
volatile bool turn_flag = false;

volatile bool lp_cmpok = false;
volatile timestamp_t lp_delayed_counter = 0;
volatile bool sleep_manager_locked = false;
Expand Down Expand Up @@ -231,7 +234,31 @@ static void LPTIM1_IRQHandler(void)
sleep_manager_locked = false;
}
if(lp_delayed_prog) {
lp_ticker_set_interrupt(lp_delayed_counter);
if(turn_flag)
{
u_int32_t temp = (lp_ticker_read() + 6) & 0xFFFF;
if(lp_delayed_counter <= temp)
{
lp_ticker_fire_interrupt();
}
else
{
lp_ticker_set_interrupt((lp_delayed_counter - 6) & 0xFFFF);
}
}
else
{
if(future_event_flag && (lp_delayed_counter <= lp_ticker_read()))
{
lp_ticker_fire_interrupt();
future_event_flag = false;
}
else
{
lp_ticker_set_interrupt(lp_delayed_counter);
}
}

lp_delayed_prog = false;
}
}
Expand Down Expand Up @@ -260,6 +287,8 @@ uint32_t lp_ticker_read(void)
void lp_ticker_set_interrupt(timestamp_t timestamp)
{
core_util_critical_section_enter();

timestamp_t last_read_counter = lp_ticker_read();

/* Always store the last requested timestamp */
lp_delayed_counter = timestamp;
Expand All @@ -272,9 +301,27 @@ void lp_ticker_set_interrupt(timestamp_t timestamp)
if (lp_cmpok == false) {
/* if this is not safe to write, then delay the programing to the
* time when CMPOK interrupt will trigger */
if((0xFFFA < timestamp) && (timestamp < 5) && (0xFFFA < last_read_counter)&&(last_read_counter < 5))
{
turn_flag = true;
lp_delayed_counter = (timestamp + 6) & 0xFFFF;
}
else
{
turn_flag = false;
if(lp_delayed_counter >= last_read_counter)
{
future_event_flag = true;
}
else
{
future_event_flag = false;
}
}

lp_delayed_prog = true;
} else {
timestamp_t last_read_counter = lp_ticker_read();

lp_ticker_clear_interrupt();

/* HW is not able to trig a very short term interrupt, that is
Expand All @@ -285,6 +332,7 @@ void lp_ticker_set_interrupt(timestamp_t timestamp)
timestamp = LP_TIMER_WRAP((timestamp + LP_TIMER_SAFE_GUARD));
}
}

/* Then check if this target timestamp is not in the past, or close to wrap-around
* Let's assume last_read_counter = 0xFFFC, and we want to program timestamp = 0x100
* The interrupt will not fire before the CMPOK flag is OK, so there are 2 cases:
Expand Down

0 comments on commit 4551399

Please sign in to comment.