Skip to content

Commit 9e34f76

Browse files
author
Filip Jagodzinski
committed
Tests: Sleep: Extract test utility functions
Move a few of utility functions so other tests can use them.
1 parent f415af8 commit 9e34f76

File tree

2 files changed

+115
-83
lines changed

2 files changed

+115
-83
lines changed

TESTS/mbed_hal/sleep/main.cpp

Lines changed: 1 addition & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,9 @@
2525
#include "greentea-client/test_env.h"
2626
#include "mbed_lp_ticker_wrapper.h"
2727

28+
#include "sleep_test_utils.h"
2829
#include "sleep_api_tests.h"
2930

30-
#define US_PER_S 1000000
31-
32-
/* Flush serial buffer before deep sleep
33-
*
34-
* Since deepsleep() may shut down the UART peripheral, we wait for some time
35-
* to allow for hardware serial buffers to completely flush.
36-
*
37-
* Take NUMAKER_PFM_NUC472 as an example:
38-
* Its UART peripheral has 16-byte Tx FIFO. With baud rate set to 9600, flush
39-
* Tx FIFO would take: 16 * 8 * 1000 / 9600 = 13.3 (ms). So set wait time to
40-
* 20ms here for safe.
41-
*
42-
* This should be replaced with a better function that checks if the
43-
* hardware buffers are empty. However, such an API does not exist now,
44-
* so we'll use the busy_wait_ms() function for now.
45-
*/
46-
#define SERIAL_FLUSH_TIME_MS 20
47-
4831
using namespace utest::v1;
4932

5033
static char info[512] = {0};
@@ -67,71 +50,6 @@ static const uint32_t sleep_mode_delta_us = (10 + 4 + 5);
6750
* delta = default 10 ms + worst ticker resolution + extra time for code execution */
6851
static const uint32_t deepsleep_mode_delta_us = (10000 + 125 + 5);
6952

70-
unsigned int ticks_to_us(unsigned int ticks, unsigned int freq)
71-
{
72-
return (unsigned int)((unsigned long long) ticks * US_PER_S / freq);
73-
}
74-
75-
unsigned int us_to_ticks(unsigned int us, unsigned int freq)
76-
{
77-
return (unsigned int)((unsigned long long) us * freq / US_PER_S);
78-
}
79-
80-
unsigned int overflow_protect(unsigned int timestamp, unsigned int ticker_width)
81-
{
82-
unsigned int counter_mask = ((1 << ticker_width) - 1);
83-
84-
return (timestamp & counter_mask);
85-
}
86-
87-
bool compare_timestamps(unsigned int delta_ticks, unsigned int ticker_width, unsigned int expected, unsigned int actual)
88-
{
89-
const unsigned int counter_mask = ((1 << ticker_width) - 1);
90-
91-
const unsigned int lower_bound = ((expected - delta_ticks) & counter_mask);
92-
const unsigned int upper_bound = ((expected + delta_ticks) & counter_mask);
93-
94-
if (lower_bound < upper_bound) {
95-
if (actual >= lower_bound && actual <= upper_bound) {
96-
return true;
97-
} else {
98-
return false;
99-
}
100-
} else {
101-
if ((actual >= lower_bound && actual <= counter_mask) || (actual >= 0 && actual <= upper_bound)) {
102-
return true;
103-
} else {
104-
return false;
105-
}
106-
}
107-
}
108-
109-
void busy_wait_ms(int ms)
110-
{
111-
const ticker_info_t *info = us_ticker_get_info();
112-
uint32_t mask = (1 << info->bits) - 1;
113-
int delay = (int)((uint64_t)ms * info->frequency / 1000);
114-
115-
uint32_t prev = us_ticker_read();
116-
while (delay > 0) {
117-
uint32_t next = us_ticker_read();
118-
delay -= (next - prev) & mask;
119-
prev = next;
120-
}
121-
}
122-
123-
void us_ticker_isr(const ticker_data_t *const ticker_data)
124-
{
125-
us_ticker_clear_interrupt();
126-
}
127-
128-
#ifdef DEVICE_LPTICKER
129-
void lp_ticker_isr(const ticker_data_t *const ticker_data)
130-
{
131-
lp_ticker_clear_interrupt();
132-
}
133-
#endif
134-
13553
/* Test that wake-up time from sleep should be less than 10 us and
13654
* high frequency ticker interrupt can wake-up target from sleep. */
13755
void sleep_usticker_test()
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2017 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* @addtogroup hal_sleep_test_utils
19+
* @{
20+
*/
21+
22+
#ifndef MBED_SLEEP_TEST_UTILS_H
23+
#define MBED_SLEEP_TEST_UTILS_H
24+
25+
#include "hal/ticker_api.h"
26+
#include "hal/us_ticker_api.h"
27+
#include "hal/lp_ticker_api.h"
28+
29+
/* Flush serial buffer before deep sleep
30+
*
31+
* Since deepsleep() may shut down the UART peripheral, we wait for some time
32+
* to allow for hardware serial buffers to completely flush.
33+
*
34+
* Take NUMAKER_PFM_NUC472 as an example:
35+
* Its UART peripheral has 16-byte Tx FIFO. With baud rate set to 9600, flush
36+
* Tx FIFO would take: 16 * 8 * 1000 / 9600 = 13.3 (ms). So set wait time to
37+
* 20ms here for safe.
38+
*
39+
* This should be replaced with a better function that checks if the
40+
* hardware buffers are empty. However, such an API does not exist now,
41+
* so we'll use the busy_wait_ms() function for now.
42+
*/
43+
#define SERIAL_FLUSH_TIME_MS 20
44+
45+
#define US_PER_S 1000000
46+
47+
unsigned int ticks_to_us(unsigned int ticks, unsigned int freq)
48+
{
49+
return (unsigned int) ((unsigned long long) ticks * US_PER_S / freq);
50+
}
51+
52+
unsigned int us_to_ticks(unsigned int us, unsigned int freq)
53+
{
54+
return (unsigned int) ((unsigned long long) us * freq / US_PER_S);
55+
}
56+
57+
unsigned int overflow_protect(unsigned int timestamp, unsigned int ticker_width)
58+
{
59+
unsigned int counter_mask = ((1 << ticker_width) - 1);
60+
61+
return (timestamp & counter_mask);
62+
}
63+
64+
bool compare_timestamps(unsigned int delta_ticks, unsigned int ticker_width, unsigned int expected, unsigned int actual)
65+
{
66+
const unsigned int counter_mask = ((1 << ticker_width) - 1);
67+
68+
const unsigned int lower_bound = ((expected - delta_ticks) & counter_mask);
69+
const unsigned int upper_bound = ((expected + delta_ticks) & counter_mask);
70+
71+
if (lower_bound < upper_bound) {
72+
if (actual >= lower_bound && actual <= upper_bound) {
73+
return true;
74+
} else {
75+
return false;
76+
}
77+
} else {
78+
if ((actual >= lower_bound && actual <= counter_mask) || (actual >= 0 && actual <= upper_bound)) {
79+
return true;
80+
} else {
81+
return false;
82+
}
83+
}
84+
}
85+
86+
void busy_wait_ms(int ms)
87+
{
88+
const ticker_info_t *info = us_ticker_get_info();
89+
uint32_t mask = (1 << info->bits) - 1;
90+
int delay = (int) ((uint64_t) ms * info->frequency / 1000);
91+
92+
uint32_t prev = us_ticker_read();
93+
while (delay > 0) {
94+
uint32_t next = us_ticker_read();
95+
delay -= (next - prev) & mask;
96+
prev = next;
97+
}
98+
}
99+
100+
void us_ticker_isr(const ticker_data_t * const ticker_data)
101+
{
102+
us_ticker_clear_interrupt();
103+
}
104+
105+
#ifdef DEVICE_LPTICKER
106+
void lp_ticker_isr(const ticker_data_t * const ticker_data)
107+
{
108+
lp_ticker_clear_interrupt();
109+
}
110+
#endif
111+
112+
#endif
113+
114+
/** @}*/

0 commit comments

Comments
 (0)