Skip to content

Commit 97069a8

Browse files
committed
lp_us_tickers test - provide counter overflow protection
Since according to the ticker requirements min acceptable counter size is 12 bits (low power timer) for which max count is 4095, then all test cases must be executed in this time window. HAL ticker layer handles overflow and it is not handled in the target ticker drivers.
1 parent 43605d8 commit 97069a8

File tree

1 file changed

+29
-19
lines changed

1 file changed

+29
-19
lines changed

TESTS/mbed_hal/lp_us_tickers/main.cpp

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#endif
2727

2828
#define FORCE_OVERFLOW_TEST (false)
29-
#define TICKER_INT_VAL 2000
29+
#define TICKER_INT_VAL 500
3030
#define TICKER_DELTA 50
3131

3232
#define LP_TICKER_OVERFLOW_DELTA 0 // this will allow to detect that ticker counter rollovers to 0
@@ -51,9 +51,6 @@ unsigned int ticker_overflow_delta;
5151
* Parameter <step> is used to disable compiler optimisation. */
5252
uint32_t count_ticks(volatile uint32_t cycles, uint32_t step)
5353
{
54-
/* Init the ticker. */
55-
intf->init();
56-
5754
core_util_critical_section_enter();
5855

5956
const uint32_t start = intf->read();
@@ -133,9 +130,7 @@ void ticker_info_test(void)
133130
/* Test that ticker interrupt fires only when the ticker counter increments to the value set by ticker_set_interrupt. */
134131
void ticker_interrupt_test(void)
135132
{
136-
uint32_t ticker_timeout[] = { 100, 500, 1000, 2000 };
137-
138-
intf->init();
133+
uint32_t ticker_timeout[] = { 100, 200, 300, 500 };
139134

140135
for (uint32_t i = 0; i < (sizeof(ticker_timeout) / sizeof(uint32_t)); i++) {
141136
intFlag = 0;
@@ -175,8 +170,6 @@ void ticker_past_test(void)
175170
{
176171
intFlag = 0;
177172

178-
intf->init();
179-
180173
const uint32_t tick_count = intf->read();
181174

182175
/* Set interrupt tick count to value in the past.
@@ -193,8 +186,6 @@ void ticker_repeat_reschedule_test(void)
193186
{
194187
intFlag = 0;
195188

196-
intf->init();
197-
198189
const uint32_t tick_count = intf->read();
199190

200191
/* Set interrupt. Interrupt should be fired when tick count is equal to:
@@ -227,8 +218,6 @@ void ticker_fire_now_test(void)
227218
{
228219
intFlag = 0;
229220

230-
intf->init();
231-
232221
intf->fire_interrupt();
233222

234223
/* On some platforms set_interrupt function sets interrupt in the nearest feature. */
@@ -256,8 +245,6 @@ void ticker_overflow_test(void)
256245

257246
intFlag = 0;
258247

259-
intf->init();
260-
261248
/* Wait for max count. */
262249
while (intf->read() != (max_count - ticker_overflow_delta)) {
263250
/* Just wait. */
@@ -296,8 +283,6 @@ void ticker_overflow_test(void)
296283
/* Test that the ticker increments by one on each tick. */
297284
void ticker_increment_test(void)
298285
{
299-
intf->init();
300-
301286
const ticker_info_t* p_ticker_info = intf->get_info();
302287

303288
/* Perform test based on ticker speed. */
@@ -334,8 +319,6 @@ void ticker_speed_test(void)
334319
Timer timer;
335320
int counter = NUM_OF_CALLS;
336321

337-
intf->init();
338-
339322
/* ---- Test ticker_read function. ---- */
340323
timer.reset();
341324
timer.start();
@@ -391,15 +374,38 @@ void ticker_speed_test(void)
391374
TEST_ASSERT(timer.read_us() < (NUM_OF_CALLS * (MAX_FUNC_EXEC_TIME_US + DELTA_FUNC_EXEC_TIME_US)));
392375
}
393376

377+
/* Since according to the ticker requirements min acceptable counter size is
378+
* 12 bits (low power timer) for which max count is
379+
* 4095, then all test cases must be executed in this time window.
380+
* HAL ticker layer handles overflow and it is not handled in the target
381+
* ticker drivers.
382+
*/
383+
void overflow_protect()
384+
{
385+
const uint32_t ticks_now = intf->read();
386+
const ticker_info_t* p_ticker_info = intf->get_info();
387+
388+
const uint32_t max_count = (1 << p_ticker_info->bits - 1);
389+
390+
if ((max_count - ticks_now) > 4000) {
391+
return;
392+
}
393+
394+
while (intf->read() > ticks_now);
395+
}
394396

395397
utest::v1::status_t hf_ticker_setup(const Case *const source, const size_t index_of_case)
396398
{
397399
intf = get_us_ticker_data()->interface;
398400

401+
intf->init();
402+
399403
set_us_ticker_irq_handler(ticker_event_handler_stub);
400404

401405
ticker_overflow_delta = HF_TICKER_OVERFLOW_DELTA;
402406

407+
overflow_protect();
408+
403409
return greentea_case_setup_handler(source, index_of_case);
404410
}
405411

@@ -408,10 +414,14 @@ utest::v1::status_t lp_ticker_setup(const Case *const source, const size_t index
408414
{
409415
intf = get_lp_ticker_data()->interface;
410416

417+
intf->init();
418+
411419
set_lp_ticker_irq_handler(ticker_event_handler_stub);
412420

413421
ticker_overflow_delta = LP_TICKER_OVERFLOW_DELTA;
414422

423+
overflow_protect();
424+
415425
return greentea_case_setup_handler(source, index_of_case);
416426
}
417427
#endif

0 commit comments

Comments
 (0)