Skip to content

Commit cb9ec11

Browse files
c1728p90xc0170
authored andcommitted
Add port tests for ticker and lp ticker
Add a test to make sure timer events scheduled for the past fire immediately, and add a test to ensure many events occurring at once do not cause a stack overflow.
1 parent cbfb234 commit cb9ec11

File tree

3 files changed

+157
-2
lines changed

3 files changed

+157
-2
lines changed

TESTS/mbed_hal/ticker_port/main.cpp

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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+
#include "utest/utest.h"
18+
#include "unity/unity.h"
19+
#include "greentea-client/test_env.h"
20+
21+
#include "mbed.h"
22+
#include "us_ticker_api.h"
23+
#include "lp_ticker_api.h"
24+
25+
using namespace utest::v1;
26+
27+
static const uint32_t QUICK_EVENT_COUNT = 100000;
28+
29+
static const ticker_interface_t* intf;
30+
static volatile uint32_t irq_count;
31+
static void (*irq_cb)(void);
32+
33+
static void ticker_irq_handler(void)
34+
{
35+
intf->clear_interrupt();
36+
irq_count++;
37+
if (irq_cb) {
38+
irq_cb();
39+
}
40+
}
41+
42+
void us_ticker_irq_handler(void)
43+
{
44+
ticker_irq_handler();
45+
}
46+
47+
#if DEVICE_LOWPOWERTIMER
48+
void lp_ticker_irq_handler(void)
49+
{
50+
ticker_irq_handler();
51+
}
52+
#endif
53+
54+
static void run_next_event(void)
55+
{
56+
if (irq_count < QUICK_EVENT_COUNT) {
57+
intf->set_interrupt(intf->read() - 1);
58+
}
59+
}
60+
61+
/**
62+
* Test that events schedule in the past still trigger an interrupt
63+
*
64+
* Events can be scheduled in the past if there is a delay between the
65+
* call to timer->read() and timer->set_interrupt(). This delay can be
66+
* caused by a task switch or interrupt, in addition to other things.
67+
* This test ensures that the hardware gracefully handles this case.
68+
*/
69+
void test_events_in_past(void)
70+
{
71+
static const uint32_t us_in_past[] = {
72+
0,
73+
1,
74+
10,
75+
1000000,
76+
0x70000000
77+
};
78+
irq_count = 0;
79+
irq_cb = 0;
80+
intf->init();
81+
82+
for (uint32_t i = 0; i < sizeof(us_in_past) / sizeof(us_in_past[0]); i++) {
83+
printf("Testing interrupt %lu us in the past\r\n", us_in_past[i]);
84+
intf->set_interrupt(intf->read() - us_in_past[i]);
85+
wait(0.01);
86+
TEST_ASSERT_EQUAL(i + 1, irq_count);
87+
}
88+
89+
}
90+
91+
/**
92+
* Test that the port can handle events being added non-stop
93+
*
94+
* In the past some ports processed new pending events recursively.
95+
* With this type of implementation, if too many events are pending at
96+
* the same time then the recursion will be deep enough to cause a stack
97+
* overflow. This test simulates that to ensure there will not be a stack
98+
* overflow.
99+
*/
100+
void test_events_quickly(void)
101+
{
102+
irq_count = 0;
103+
irq_cb = run_next_event;
104+
intf->init();
105+
106+
printf("Running %lu events now\r\n", QUICK_EVENT_COUNT);
107+
intf->set_interrupt(intf->read() - 1);
108+
while (irq_count < QUICK_EVENT_COUNT) {
109+
wait(0.01);
110+
}
111+
TEST_ASSERT_EQUAL(QUICK_EVENT_COUNT, irq_count);
112+
}
113+
114+
utest::v1::status_t us_ticker_setup(const Case *const source, const size_t index_of_case)
115+
{
116+
intf = get_us_ticker_data()->interface;
117+
irq_count = 0;
118+
irq_cb = 0;
119+
return greentea_case_setup_handler(source, index_of_case);
120+
}
121+
122+
#if DEVICE_LOWPOWERTIMER
123+
utest::v1::status_t lp_ticker_setup(const Case *const source, const size_t index_of_case)
124+
{
125+
intf = get_lp_ticker_data()->interface;
126+
irq_count = 0;
127+
irq_cb = 0;
128+
return greentea_case_setup_handler(source, index_of_case);
129+
}
130+
#endif
131+
132+
Case cases[] = {
133+
Case("Test us ticker events in the past", us_ticker_setup, test_events_in_past),
134+
Case("Test us ticker events occurring quickly", us_ticker_setup, test_events_quickly),
135+
#if DEVICE_LOWPOWERTIMER
136+
Case("Test lp ticker events in the past", lp_ticker_setup, test_events_in_past),
137+
Case("Test lp ticker events occurring quickly", lp_ticker_setup, test_events_quickly),
138+
#endif
139+
};
140+
141+
142+
utest::v1::status_t greentea_test_setup(const size_t number_of_cases) {
143+
GREENTEA_SETUP(1200, "default_auto");
144+
return greentea_test_setup_handler(number_of_cases);
145+
}
146+
147+
Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler);
148+
149+
int main() {
150+
return Harness::run(specification);
151+
}

hal/mbed_lp_ticker_api.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*/
1616
#include "hal/lp_ticker_api.h"
17+
#include "mbed_toolchain.h"
1718

1819
#if DEVICE_LOWPOWERTIMER
1920

@@ -37,7 +38,8 @@ const ticker_data_t* get_lp_ticker_data(void)
3738
return &lp_data;
3839
}
3940

40-
void lp_ticker_irq_handler(void)
41+
// MBED_WEAK for testing only
42+
MBED_WEAK void lp_ticker_irq_handler(void)
4143
{
4244
ticker_irq_handler(&lp_data);
4345
}

hal/mbed_us_ticker_api.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*/
1616
#include "hal/us_ticker_api.h"
17+
#include "mbed_toolchain.h"
1718

1819
static ticker_event_queue_t events = { 0 };
1920

@@ -35,7 +36,8 @@ const ticker_data_t* get_us_ticker_data(void)
3536
return &us_data;
3637
}
3738

38-
void us_ticker_irq_handler(void)
39+
// MBED_WEAK for testing only
40+
MBED_WEAK void us_ticker_irq_handler(void)
3941
{
4042
ticker_irq_handler(&us_data);
4143
}

0 commit comments

Comments
 (0)