Skip to content

Commit def1ef2

Browse files
author
Cruz Monrreal
authored
Merge pull request #7494 from mprse/lp_ticker_deep_sleep_delay_fix
tests-mbed_hal-lp_ticker: change implementation of the delay before deep-sleep.
2 parents 602b0ce + 832e8b3 commit def1ef2

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

TESTS/mbed_hal/lp_ticker/main.cpp

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,38 @@ using namespace utest::v1;
2929

3030
volatile int intFlag = 0;
3131

32+
#define US_PER_MS 1000
33+
3234
#define TICKER_GLITCH_TEST_TICKS 1000
3335

3436
#define TICKER_INT_VAL 500
3537
#define TICKER_DELTA 10
3638

3739
#define LP_TICKER_OV_LIMIT 4000
3840

41+
/* Flush serial buffer before deep sleep
42+
*
43+
* Since deepsleep() may shut down the UART peripheral, we wait for some time
44+
* to allow for hardware serial buffers to completely flush.
45+
*
46+
* Take NUMAKER_PFM_NUC472 as an example:
47+
* Its UART peripheral has 16-byte Tx FIFO. With baud rate set to 9600, flush
48+
* Tx FIFO would take: 16 * 8 * 1000 / 9600 = 13.3 (ms). So set wait time to
49+
* 20ms here for safe.
50+
*
51+
* This should be replaced with a better function that checks if the
52+
* hardware buffers are empty. However, such an API does not exist now,
53+
* so we'll use the busy_wait_ms() function for now.
54+
*/
55+
#define SERIAL_FLUSH_TIME_MS 20
56+
57+
void busy_wait_ms(int ms)
58+
{
59+
const ticker_data_t *const ticker = get_us_ticker_data();
60+
uint32_t start = ticker_read(ticker);
61+
while ((ticker_read(ticker) - start) < (uint32_t)(ms * US_PER_MS));
62+
}
63+
3964
/* Since according to the ticker requirements min acceptable counter size is
4065
* - 12 bits for low power timer - max count = 4095,
4166
* then all test cases must be executed in this time windows.
@@ -72,11 +97,6 @@ void ticker_event_handler_stub(const ticker_data_t * const ticker)
7297
lp_ticker_disable_interrupt();
7398
}
7499

75-
void wait_cycles(volatile unsigned int cycles)
76-
{
77-
while (cycles--);
78-
}
79-
80100
/* Test that the ticker has the correct frequency and number of bits. */
81101
void lp_ticker_info_test()
82102
{
@@ -97,8 +117,10 @@ void lp_ticker_deepsleep_test()
97117

98118
lp_ticker_init();
99119

100-
/* Wait for green tea UART transmission before entering deep-sleep mode. */
101-
wait_cycles(400000);
120+
/* Give some time Green Tea to finish UART transmission before entering
121+
* deep-sleep mode.
122+
*/
123+
busy_wait_ms(SERIAL_FLUSH_TIME_MS);
102124

103125
overflow_protect();
104126

0 commit comments

Comments
 (0)