Skip to content

Commit a686fc0

Browse files
committed
tests-mbed_hal-lp_ticker: Add overflow protection.
1 parent ccec53e commit a686fc0

File tree

1 file changed

+34
-5
lines changed

1 file changed

+34
-5
lines changed

TESTS/mbed_hal/lp_ticker/main.cpp

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,36 @@ using namespace utest::v1;
2929

3030
volatile int intFlag = 0;
3131

32-
#define TICKER_INT_VAL 5000
33-
#define TICKER_DELTA 50
32+
#define TICKER_GLITCH_TEST_TICKS 1000
33+
34+
#define TICKER_INT_VAL 500
35+
#define TICKER_DELTA 10
36+
37+
#define LP_TICKER_OV_LIMIT 4000
38+
39+
/* Since according to the ticker requirements min acceptable counter size is
40+
* - 12 bits for low power timer - max count = 4095,
41+
* then all test cases must be executed in this time windows.
42+
* HAL ticker layer handles counter overflow and it is not handled in the target
43+
* ticker drivers. Ensure we have enough time to execute test case without overflow.
44+
*/
45+
void overflow_protect()
46+
{
47+
uint32_t time_window;
48+
49+
time_window = LP_TICKER_OV_LIMIT;
50+
51+
const uint32_t ticks_now = lp_ticker_read();
52+
const ticker_info_t* p_ticker_info = lp_ticker_get_info();
53+
54+
const uint32_t max_count = ((1 << p_ticker_info->bits) - 1);
55+
56+
if ((max_count - ticks_now) > time_window) {
57+
return;
58+
}
59+
60+
while (lp_ticker_read() > ticks_now);
61+
}
3462

3563
void ticker_event_handler_stub(const ticker_data_t * const ticker)
3664
{
@@ -71,6 +99,8 @@ void lp_ticker_deepsleep_test()
7199
/* Wait for green tea UART transmission before entering deep-sleep mode. */
72100
wait_cycles(400000);
73101

102+
overflow_protect();
103+
74104
const uint32_t tick_count = lp_ticker_read();
75105

76106
/* Set interrupt. Interrupt should be fired when tick count is equal to:
@@ -91,13 +121,12 @@ void lp_ticker_glitch_test()
91121
{
92122
lp_ticker_init();
93123

94-
const ticker_info_t* p_ticker_info = lp_ticker_get_info();
124+
overflow_protect();
95125

96126
uint32_t last = lp_ticker_read();
97127
const uint32_t start = last;
98128

99-
/* Set test time to 2 sec. */
100-
while (last < (start + p_ticker_info->frequency * 2)) {
129+
while (last < (start + TICKER_GLITCH_TEST_TICKS)) {
101130
const uint32_t cur = lp_ticker_read();
102131
TEST_ASSERT(cur >= last);
103132
last = cur;

0 commit comments

Comments
 (0)