Skip to content

Commit 3c0fb93

Browse files
authored
Merge pull request #5164 from c1728p9/ticker_specification
Ticker specification (branch feature-hal-spec-ticker)
2 parents 291dd1d + 8ac4a38 commit 3c0fb93

File tree

5 files changed

+489
-10
lines changed

5 files changed

+489
-10
lines changed

TESTS/mbed_hal/lp_ticker_api_tests.h

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2017-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+
/** \addtogroup hal_lp_ticker_tests
18+
* @{
19+
*/
20+
21+
#ifndef LP_TICKER_API_TESTS_H
22+
#define LP_TICKER_API_TESTS_H
23+
24+
#include "device.h"
25+
26+
27+
#ifdef __cplusplus
28+
extern "C" {
29+
#endif
30+
31+
32+
/** Test that the ticker has the right frequency and number of bits
33+
*
34+
*/
35+
void lp_ticker_info_test(void);
36+
37+
/** Test that the ticker operates in deep sleep mode
38+
*
39+
*/
40+
void lp_ticker_deepsleep_test(void);
41+
42+
43+
/**@}*/
44+
45+
#ifdef __cplusplus
46+
}
47+
#endif
48+
49+
#endif
50+
51+
/**@}*/

TESTS/mbed_hal/ticker_api_tests.h

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2017-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+
/** \addtogroup hal_ticker_tests */
18+
/** @{*/
19+
20+
#ifndef TICKER_API_TESTS_H
21+
#define TICKER_API_TESTS_H
22+
23+
#include "device.h"
24+
25+
26+
#ifdef __cplusplus
27+
extern "C" {
28+
#endif
29+
30+
/** Test that ticker_init can be called multiple times
31+
*
32+
*/
33+
void ticker_init_test(void);
34+
35+
/** Test that ticker has a valid frequency and bitwidth
36+
*
37+
*/
38+
void ticker_info_test(void);
39+
40+
/** Test that the ticker increments by one on each tick
41+
*
42+
*/
43+
void ticker_increment_test(void);
44+
45+
/** Test that the interrupt fires at the right time
46+
*
47+
*/
48+
void ticker_interrupt_test(void);
49+
50+
/** Test that an interrupt is not triggered when ticker_set_interrupt is called with a time from the past
51+
*
52+
*/
53+
void ticker_past_test(void);
54+
55+
/** Test that a ticker can be rescheduled repeatedly before the handler has been called
56+
*
57+
*/
58+
void ticker_repeat_reschedule_test(void);
59+
60+
/** Test that ticker_fire_interrupt causes and interrupt to get fired immediately
61+
*
62+
*/
63+
void ticker_fire_now_test(void);
64+
65+
/** Test that common ticker functions complete with the required amount of time
66+
*
67+
* Ensure that ticker_read, ticker_clear_interrupt, ticker_set_interrupt
68+
* and ticker_fire_interrupt take less than 20us to complete.
69+
*
70+
*/
71+
void ticker_speed_test(void);
72+
73+
/** Test that the ticker correctly handles overflows
74+
*
75+
* This test verifies that rollover is properly handled and
76+
* that scheduling for a time after overflow works.
77+
*
78+
*/
79+
void ticker_overflow_test(void);
80+
81+
/** Test that rescheduling does not cause drift
82+
*
83+
* This test verifies that repeated rescheduling does not cause a
84+
* drift.
85+
*
86+
*/
87+
void ticker_reschedule_test(void);
88+
89+
/** Test that the ticker is operating at the frequency it specifies
90+
*
91+
*/
92+
void ticker_frequency_test(void);
93+
94+
95+
/**@}*/
96+
97+
#ifdef __cplusplus
98+
}
99+
#endif
100+
101+
#endif
102+
103+
/**@}*/

TESTS/mbed_hal/us_ticker_api_tests.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2017-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+
/** \addtogroup hal_us_ticker_tests */
18+
/** @{*/
19+
20+
#ifndef US_TICKER_API_TESTS_H
21+
#define US_TICKER_API_TESTS_H
22+
23+
#include "device.h"
24+
25+
26+
#ifdef __cplusplus
27+
extern "C" {
28+
#endif
29+
30+
31+
/** Test that the ticker has the right frequency and number of bits
32+
*
33+
*/
34+
void us_ticker_info_test(void);
35+
36+
37+
/**@}*/
38+
39+
#ifdef __cplusplus
40+
}
41+
#endif
42+
43+
#endif
44+
45+
/**@}*/

hal/lp_ticker_api.h

Lines changed: 124 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,33 @@ extern "C" {
3030
#endif
3131

3232
/**
33-
* \defgroup hal_LpTicker Low Power Ticker Functions
33+
* \defgroup hal_lp_ticker Low Power Ticker
34+
* Low level interface to the low power ticker of a target
35+
*
36+
* # Defined behavior
37+
* * Has a reported frequency between 8KHz and 64KHz - verified by lp_ticker_info_test
38+
* * Has a counter that is at least 12 bits wide - verified by lp_ticker_info_test
39+
* * Continues operating in deep sleep mode - verified by lp_ticker_sleep_test
40+
* * All behavior defined by the @ref hal_ticker_shared "ticker specification"
41+
*
42+
* # Undefined behavior
43+
* * See the @ref hal_ticker_shared "ticker specification"
44+
*
45+
* @see hal_lp_ticker_tests
46+
*
3447
* @{
3548
*/
3649

50+
/**
51+
* \defgroup hal_lp_ticker_tests Low Power Ticker tests
52+
* Tests to validate the proper implementation of the low power ticker
53+
*
54+
* To run the low power ticker hal tests use the command:
55+
*
56+
* mbed test -t <toolchain> -m <target> -n tests-mbed_hal-us_lp_ticker*,tests-mbed_hal-lp_ticker*
57+
*
58+
*/
59+
3760
typedef void (*ticker_irq_handler_type)(const ticker_data_t *const);
3861

3962
/** Set low power ticker IRQ handler
@@ -62,39 +85,135 @@ void lp_ticker_irq_handler(void);
6285

6386
/** Initialize the low power ticker
6487
*
88+
* Initialize or re-initialize the ticker. This resets all the
89+
* clocking and prescaler registers, along with disabling
90+
* the compare interrupt.
91+
*
92+
* Pseudo Code:
93+
* @code
94+
* void lp_ticker_init()
95+
* {
96+
* // Enable clock gate so processor can read LPTMR registers
97+
* POWER_CTRL |= POWER_CTRL_LPTMR_Msk;
98+
*
99+
* // Disable the timer and ensure it is powered down
100+
* LPTMR_CTRL &= ~(LPTMR_CTRL_ENABLE_Msk | LPTMR_CTRL_COMPARE_ENABLE_Msk);
101+
*
102+
* // Configure divisors - no division necessary
103+
* LPTMR_PRESCALE = 0;
104+
* LPTMR_CTRL |= LPTMR_CTRL_ENABLE_Msk;
105+
*
106+
* // Install the interrupt handler
107+
* NVIC_SetVector(LPTMR_IRQn, (uint32_t)lp_ticker_irq_handler);
108+
* NVIC_EnableIRQ(LPTMR_IRQn);
109+
* }
110+
* @endcode
65111
*/
66112
void lp_ticker_init(void);
67113

68-
/** Read the current counter
114+
/** Read the current tick
115+
*
116+
* If no rollover has occurred, the seconds passed since ::lp_ticker_init
117+
* was called can be found by dividing the ticks returned by this function
118+
* by the frequency returned by ::lp_ticker_get_info.
119+
*
120+
* @return The current timer's counter value in ticks
69121
*
70-
* @return The current timer's counter value in microseconds
122+
* Pseudo Code:
123+
* @code
124+
* uint32_t lp_ticker_read()
125+
* {
126+
* uint16_t count;
127+
* uint16_t last_count;
128+
*
129+
* // Loop until the same tick is read twice since this
130+
* // is ripple counter on a different clock domain.
131+
* count = LPTMR_COUNT;
132+
* do {
133+
* last_count = count;
134+
* count = LPTMR_COUNT;
135+
* } while (last_count != count);
136+
*
137+
* return count;
138+
* }
139+
* @endcode
71140
*/
72141
uint32_t lp_ticker_read(void);
73142

74143
/** Set interrupt for specified timestamp
75144
*
76-
* @param timestamp The time in microseconds to be set
145+
* @param timestamp The time in ticks to be set
146+
*
147+
* @note no special handling needs to be done for times in the past
148+
* as the common timer code will detect this and call
149+
* ::lp_ticker_fire_interrupt if this is the case
150+
*
151+
* @note calling this function with timestamp of more than the supported
152+
* number of bits returned by ::lp_ticker_get_info results in undefined
153+
* behavior.
154+
*
155+
* Pseudo Code:
156+
* @code
157+
* void lp_ticker_set_interrupt(timestamp_t timestamp)
158+
* {
159+
* LPTMR_COMPARE = timestamp;
160+
* LPTMR_CTRL |= LPTMR_CTRL_COMPARE_ENABLE_Msk;
161+
* }
162+
* @endcode
77163
*/
78164
void lp_ticker_set_interrupt(timestamp_t timestamp);
79165

80166
/** Disable low power ticker interrupt
81167
*
168+
* Pseudo Code:
169+
* @code
170+
* void lp_ticker_disable_interrupt(void)
171+
* {
172+
* // Disable the compare interrupt
173+
* LPTMR_CTRL &= ~LPTMR_CTRL_COMPARE_ENABLE_Msk;
174+
* }
175+
* @endcode
82176
*/
83177
void lp_ticker_disable_interrupt(void);
84178

85179
/** Clear the low power ticker interrupt
86180
*
181+
* Pseudo Code:
182+
* @code
183+
* void lp_ticker_clear_interrupt(void)
184+
* {
185+
* // Write to the ICR (interrupt clear register) of the LPTMR
186+
* LPTMR_ICR = LPTMR_ICR_COMPARE_Msk;
187+
* }
188+
* @endcode
87189
*/
88190
void lp_ticker_clear_interrupt(void);
89191

90192
/** Set pending interrupt that should be fired right away.
91193
*
92-
* The ticker should be initialized prior calling this function.
194+
* Pseudo Code:
195+
* @code
196+
* void lp_ticker_fire_interrupt(void)
197+
* {
198+
* NVIC_SetPendingIRQ(LPTMR_IRQn);
199+
* }
200+
* @endcode
93201
*/
94202
void lp_ticker_fire_interrupt(void);
95203

96204
/** Get frequency and counter bits of this ticker.
97205
*
206+
* Pseudo Code:
207+
* @code
208+
* const ticker_info_t* lp_ticker_get_info()
209+
* {
210+
* static const ticker_info_t info = {
211+
* 32768, // 32KHz
212+
* 16 // 16 bit counter
213+
* };
214+
* return &info;
215+
* }
216+
* @endcode
98217
*/
99218
const ticker_info_t* lp_ticker_get_info(void);
100219

0 commit comments

Comments
 (0)