Skip to content

Commit adc64cc

Browse files
committed
Update low power ticker wrapper
Update the low power ticker wrapper code so it does not violate any properties of the ticker specification. In specific this patch fixes the following: - Prevent spurious interrupts - Fire interrupt only when the ticker times increments to or past the value set by ticker_set_interrupt - Disable interrupts when ticker_init is called
1 parent 472abab commit adc64cc

File tree

5 files changed

+688
-107
lines changed

5 files changed

+688
-107
lines changed

hal/LowPowerTickerWrapper.cpp

Lines changed: 290 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,290 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2018 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+
#include "hal/LowPowerTickerWrapper.h"
17+
#include "platform/Callback.h"
18+
19+
LowPowerTickerWrapper::LowPowerTickerWrapper(const ticker_data_t *data, const ticker_interface_t *interface, uint32_t min_cycles_between_writes, uint32_t min_cycles_until_match)
20+
: _intf(data->interface), _min_count_between_writes(min_cycles_between_writes + 1), _min_count_until_match(min_cycles_until_match + 1), _suspended(false)
21+
{
22+
core_util_critical_section_enter();
23+
24+
this->data.interface = interface;
25+
this->data.queue = data->queue;
26+
_reset();
27+
28+
core_util_critical_section_exit();
29+
}
30+
31+
void LowPowerTickerWrapper::irq_handler(ticker_irq_handler_type handler)
32+
{
33+
core_util_critical_section_enter();
34+
35+
if (_suspended) {
36+
if (handler) {
37+
handler(&data);
38+
}
39+
core_util_critical_section_exit();
40+
return;
41+
}
42+
43+
if (_pending_fire_now || _match_check(_intf->read())) {
44+
_timeout.detach();
45+
_pending_timeout = false;
46+
_pending_match = false;
47+
_pending_fire_now = false;
48+
if (handler) {
49+
handler(&data);
50+
}
51+
} else {
52+
// Spurious interrupt
53+
_intf->clear_interrupt();
54+
}
55+
56+
core_util_critical_section_exit();
57+
}
58+
59+
void LowPowerTickerWrapper::suspend()
60+
{
61+
core_util_critical_section_enter();
62+
63+
// Wait until rescheduling is allowed
64+
while (!_set_interrupt_allowed) {
65+
timestamp_t current = _intf->read();
66+
if (((current - _last_actual_set_interrupt) & _mask) >= _min_count_between_writes) {
67+
_set_interrupt_allowed = true;
68+
}
69+
}
70+
71+
_reset();
72+
_suspended = true;
73+
74+
core_util_critical_section_exit();
75+
}
76+
77+
void LowPowerTickerWrapper::resume()
78+
{
79+
core_util_critical_section_enter();
80+
81+
_suspended = false;
82+
83+
core_util_critical_section_exit();
84+
}
85+
86+
bool LowPowerTickerWrapper::timeout_pending()
87+
{
88+
core_util_critical_section_enter();
89+
90+
bool pending = _pending_timeout;
91+
92+
core_util_critical_section_exit();
93+
return pending;
94+
}
95+
96+
void LowPowerTickerWrapper::init()
97+
{
98+
core_util_critical_section_enter();
99+
100+
_reset();
101+
_intf->init();
102+
103+
core_util_critical_section_exit();
104+
}
105+
106+
void LowPowerTickerWrapper::free()
107+
{
108+
core_util_critical_section_enter();
109+
110+
_reset();
111+
_intf->free();
112+
113+
core_util_critical_section_exit();
114+
}
115+
116+
uint32_t LowPowerTickerWrapper::read()
117+
{
118+
core_util_critical_section_enter();
119+
120+
timestamp_t current = _intf->read();
121+
if (_match_check(current)) {
122+
_intf->fire_interrupt();
123+
}
124+
125+
core_util_critical_section_exit();
126+
return current;
127+
}
128+
129+
void LowPowerTickerWrapper::set_interrupt(timestamp_t timestamp)
130+
{
131+
core_util_critical_section_enter();
132+
133+
_last_set_interrupt = _intf->read();
134+
_cur_match_time = timestamp;
135+
_pending_match = true;
136+
_schedule_match(_last_set_interrupt);
137+
138+
core_util_critical_section_exit();
139+
}
140+
141+
void LowPowerTickerWrapper::disable_interrupt()
142+
{
143+
core_util_critical_section_enter();
144+
145+
_intf->disable_interrupt();
146+
147+
core_util_critical_section_exit();
148+
}
149+
150+
void LowPowerTickerWrapper::clear_interrupt()
151+
{
152+
core_util_critical_section_enter();
153+
154+
_intf->clear_interrupt();
155+
156+
core_util_critical_section_exit();
157+
}
158+
159+
void LowPowerTickerWrapper::fire_interrupt()
160+
{
161+
core_util_critical_section_enter();
162+
163+
_pending_fire_now = 1;
164+
_intf->fire_interrupt();
165+
166+
core_util_critical_section_exit();
167+
}
168+
169+
const ticker_info_t *LowPowerTickerWrapper::get_info()
170+
{
171+
172+
core_util_critical_section_enter();
173+
174+
const ticker_info_t *info = _intf->get_info();
175+
176+
core_util_critical_section_exit();
177+
return info;
178+
}
179+
180+
void LowPowerTickerWrapper::_reset()
181+
{
182+
MBED_ASSERT(core_util_in_critical_section());
183+
184+
_timeout.detach();
185+
_pending_timeout = false;
186+
_pending_match = false;
187+
_pending_fire_now = false;
188+
_set_interrupt_allowed = true;
189+
_cur_match_time = 0;
190+
_last_set_interrupt = 0;
191+
_last_actual_set_interrupt = 0;
192+
193+
const ticker_info_t *info = _intf->get_info();
194+
if (info->bits >= 32) {
195+
_mask = 0xffffffff;
196+
} else {
197+
_mask = ((uint64_t)1 << info->bits) - 1;
198+
}
199+
200+
// Round us_per_tick up
201+
_us_per_tick = (1000000 + info->frequency - 1) / info->frequency;
202+
}
203+
204+
void LowPowerTickerWrapper::_timeout_handler()
205+
{
206+
core_util_critical_section_enter();
207+
_pending_timeout = false;
208+
209+
timestamp_t current = _intf->read();
210+
if (_ticker_match_interval_passed(_last_set_interrupt, current, _cur_match_time)) {
211+
_intf->fire_interrupt();
212+
} else {
213+
_schedule_match(current);
214+
}
215+
216+
core_util_critical_section_exit();
217+
}
218+
219+
bool LowPowerTickerWrapper::_match_check(timestamp_t current)
220+
{
221+
MBED_ASSERT(core_util_in_critical_section());
222+
223+
if (!_pending_match) {
224+
return false;
225+
}
226+
return _ticker_match_interval_passed(_last_set_interrupt, current, _cur_match_time);
227+
}
228+
229+
uint32_t LowPowerTickerWrapper::_lp_ticks_to_us(uint32_t ticks)
230+
{
231+
MBED_ASSERT(core_util_in_critical_section());
232+
233+
// Add 4 microseconds to round up the micro second ticker time (which has a frequency of at least 250KHz - 4us period)
234+
return _us_per_tick * ticks + 4;
235+
}
236+
237+
void LowPowerTickerWrapper::_schedule_match(timestamp_t current)
238+
{
239+
MBED_ASSERT(core_util_in_critical_section());
240+
241+
// Check if _intf->set_interrupt is allowed
242+
if (!_set_interrupt_allowed) {
243+
if (((current - _last_actual_set_interrupt) & _mask) >= _min_count_between_writes) {
244+
_set_interrupt_allowed = true;
245+
}
246+
}
247+
248+
uint32_t cycles_until_match = (_cur_match_time - _last_set_interrupt) & _mask;
249+
bool too_close = cycles_until_match < _min_count_until_match;
250+
251+
if (!_set_interrupt_allowed) {
252+
253+
// Can't use _intf->set_interrupt so use microsecond Timeout instead
254+
uint32_t ticks = cycles_until_match < _min_count_until_match ? cycles_until_match : _min_count_until_match;
255+
_timeout.attach_us(mbed::callback(this, &LowPowerTickerWrapper::_timeout_handler), _lp_ticks_to_us(ticks));
256+
_pending_timeout = true;
257+
return;
258+
}
259+
260+
if (!too_close) {
261+
262+
// Schedule LP ticker
263+
_intf->set_interrupt(_cur_match_time);
264+
current = _intf->read();
265+
_last_actual_set_interrupt = current;
266+
_set_interrupt_allowed = false;
267+
268+
// Check for overflow
269+
uint32_t new_cycles_until_match = (_cur_match_time - current) & _mask;
270+
if (new_cycles_until_match > cycles_until_match) {
271+
// Overflow so fire now
272+
_intf->fire_interrupt();
273+
return;
274+
}
275+
276+
// Update variables with new time
277+
cycles_until_match = new_cycles_until_match;
278+
too_close = cycles_until_match < _min_count_until_match;
279+
}
280+
281+
if (too_close) {
282+
283+
// Low power ticker incremented to less than _min_count_until_match
284+
// so low power ticker may not fire. Use Timeout to ensure it does fire.
285+
uint32_t ticks = cycles_until_match < _min_count_until_match ? cycles_until_match : _min_count_until_match;
286+
_timeout.attach_us(mbed::callback(this, &LowPowerTickerWrapper::_timeout_handler), _lp_ticks_to_us(ticks));
287+
_pending_timeout = true;
288+
return;
289+
}
290+
}

0 commit comments

Comments
 (0)