Skip to content

Commit 832e8b3

Browse files
committed
tests-mbed_hal-lp_ticker: change implementation of the delay before deep-sleep.
Serial buffer must be flushed before entering deep sleep mode. In the test this is done by the additional delay which is implemented on the busy loop which decrements given value down to 0 (`void wait_cycles(volatile unsigned int cycles)`). This solution is not appropriate since it is very target specific and the cycles value has been already increased few times. Additionally very big number of loop cycles which is suitable for fast targets may take much longer on slower boards and results in test timeout. It has been verified that 20ms is sufficient delay for the green-tea transmission. In this test we cannot simply use `wait_ms(20)` since this potentially may put board to sleep and wake up using lp ticker. The test re-initialzies the lp ticker(disables ticker interrupt) and this operation may break the schedule and time tracing by the upper layer. But we can use us ticker which is not affected by this test. The solution is to add a delay routine based on busy loop and us ticker only. This way are able to wait exactly 20 ms.
1 parent bcec185 commit 832e8b3

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)