@@ -29,13 +29,38 @@ using namespace utest::v1;
29
29
30
30
volatile int intFlag = 0 ;
31
31
32
+ #define US_PER_MS 1000
33
+
32
34
#define TICKER_GLITCH_TEST_TICKS 1000
33
35
34
36
#define TICKER_INT_VAL 500
35
37
#define TICKER_DELTA 10
36
38
37
39
#define LP_TICKER_OV_LIMIT 4000
38
40
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
+
39
64
/* Since according to the ticker requirements min acceptable counter size is
40
65
* - 12 bits for low power timer - max count = 4095,
41
66
* 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)
72
97
lp_ticker_disable_interrupt ();
73
98
}
74
99
75
- void wait_cycles (volatile unsigned int cycles)
76
- {
77
- while (cycles--);
78
- }
79
-
80
100
/* Test that the ticker has the correct frequency and number of bits. */
81
101
void lp_ticker_info_test ()
82
102
{
@@ -97,8 +117,10 @@ void lp_ticker_deepsleep_test()
97
117
98
118
lp_ticker_init ();
99
119
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);
102
124
103
125
overflow_protect ();
104
126
0 commit comments