Mistake on this page?
Report an issue in GitHub or email us
LowPowerTickerWrapper.h
1 
2 /** \addtogroup hal */
3 /** @{*/
4 /* mbed Microcontroller Library
5  * Copyright (c) 2018-2019 ARM Limited
6  * SPDX-License-Identifier: Apache-2.0
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 #ifndef MBED_LOW_POWER_TICKER_WRAPPER_H
21 #define MBED_LOW_POWER_TICKER_WRAPPER_H
22 
23 #include "device.h"
24 
25 #include "hal/ticker_api.h"
26 #include "hal/us_ticker_api.h"
27 #include "drivers/Timeout.h"
28 
29 #include "platform/mbed_critical.h"
30 
31 
33 public:
34 
35 
36  /**
37  * Create a new wrapped low power ticker object
38  *
39  * @param data Low power ticker data to wrap
40  * @param interface new ticker interface functions
41  * @param min_cycles_between_writes The number of whole low power clock periods
42  * which must complete before subsequent calls to set_interrupt
43  * @param min_cycles_until_match The minimum number of whole low power clock periods
44  * from the current time for which the match timestamp passed to set_interrupt is
45  * guaranteed to fire.
46  *
47  * N = min_cycles_between_writes
48  *
49  * 0 1 N - 1 N N + 1 N + 2 N + 3
50  * |-------|------...------|-------|-------|-------|-------|
51  * ^ ^
52  * | |
53  * set_interrupt Next set_interrupt allowed
54  *
55  * N = min_cycles_until_match
56  *
57  * 0 1 N - 1 N N + 1 N + 2 N + 3
58  * |-------|------...------|-------|-------|-------|-------|
59  * ^ ^
60  * | |
61  * set_interrupt Earliest match timestamp allowed
62  *
63  *
64  */
65 
66  LowPowerTickerWrapper(const ticker_data_t *data, const ticker_interface_t *interface, uint32_t min_cycles_between_writes, uint32_t min_cycles_until_match);
67 
68  /**
69  * Interrupt handler called by the underlying driver/hardware
70  *
71  * @param handler The callback which would normally be called by the underlying driver/hardware
72  */
73  void irq_handler(ticker_irq_handler_type handler);
74 
75  /**
76  * Suspend wrapper operation and pass through interrupts.
77  *
78  * This stops to wrapper layer from using the microsecond ticker.
79  * This should be called before using the low power ticker APIs directly.
80  *
81  * @warning: Make sure to suspend the LP ticker first (call ticker_suspend()),
82  * otherwise the behavior is undefined.
83  */
84  void suspend();
85 
86  /**
87  * Resume wrapper operation and filter interrupts normally
88  */
89  void resume();
90 
91  /**
92  * Check if a Timeout object is being used
93  *
94  * @return true if Timeout is used for scheduling false otherwise
95  */
96  bool timeout_pending();
97 
98  /*
99  * Implementation of ticker_init
100  */
101  void init();
102 
103  /*
104  * Implementation of free
105  */
106  void free();
107 
108  /*
109  * Implementation of read
110  */
111  uint32_t read();
112 
113  /*
114  * Implementation of set_interrupt
115  */
116  void set_interrupt(timestamp_t timestamp);
117 
118  /*
119  * Implementation of disable_interrupt
120  */
121  void disable_interrupt();
122 
123  /*
124  * Implementation of clear_interrupt
125  */
126  void clear_interrupt();
127 
128  /*
129  * Implementation of fire_interrupt
130  */
131  void fire_interrupt();
132 
133  /*
134  * Implementation of get_info
135  */
136  const ticker_info_t *get_info();
137 
138  ticker_data_t data;
139 
140 private:
141  mbed::Timeout _timeout;
142  const ticker_interface_t *const _intf;
143 
144  /*
145  * The number of low power clock cycles which must pass between subsequent
146  * calls to intf->set_interrupt
147  */
148  const uint32_t _min_count_between_writes;
149 
150  /*
151  * The minimum number of low power clock cycles in the future that
152  * a match value can be set to and still fire
153  */
154  const uint32_t _min_count_until_match;
155 
156  /*
157  * Flag to indicate if the timer is suspended
158  */
159  bool _suspended;
160 
161  /*
162  * _cur_match_time is valid and Timeout is scheduled to fire
163  */
164  bool _pending_timeout;
165 
166  /*
167  * set_interrupt has been called and _cur_match_time is valid
168  */
169  bool _pending_match;
170 
171  /*
172  * The function LowPowerTickerWrapper::fire_interrupt has been called
173  * and an interrupt is expected.
174  */
175  bool _pending_fire_now;
176 
177  /*
178  * It is safe to call intf->set_interrupt
179  */
180  bool _set_interrupt_allowed;
181 
182  /*
183  * Last value written by LowPowerTickerWrapper::set_interrupt
184  */
185  timestamp_t _cur_match_time;
186 
187  /*
188  * Time of last call to LowPowerTickerWrapper::set_interrupt
189  */
190  uint32_t _last_set_interrupt;
191 
192  /*
193  * Time of last call to intf->set_interrupt
194  */
195  uint32_t _last_actual_set_interrupt;
196 
197  /*
198  * Mask of valid bits from intf->read()
199  */
200  uint32_t _mask;
201 
202  /*
203  * Microsecond per low power tick (rounded up)
204  */
205  uint32_t _us_per_tick;
206 
207 
208  void _reset();
209 
210  /**
211  * Set the low power ticker match time when hardware is ready
212  *
213  * This event is scheduled to set the lp timer after the previous write
214  * has taken effect and it is safe to write a new value without blocking.
215  * If the time has already passed then this function fires and interrupt
216  * immediately.
217  */
218  void _timeout_handler();
219 
220  /*
221  * Check match time has passed
222  */
223  bool _match_check(timestamp_t current);
224 
225  /*
226  * Convert low power ticks to approximate microseconds
227  *
228  * This value is always larger or equal to exact value.
229  */
230  uint32_t _lp_ticks_to_us(uint32_t);
231 
232  /*
233  * Schedule a match interrupt to fire at the correct time
234  *
235  * @param current The current low power ticker time
236  */
237  void _schedule_match(timestamp_t current);
238 
239 };
240 
241 #endif
242 
243 /** @}*/
244 
245 
void irq_handler(ticker_irq_handler_type handler)
Interrupt handler called by the underlying driver/hardware.
Information about the ticker implementation.
Definition: ticker_api.h:53
Ticker's interface structure - required API for a ticker.
Definition: ticker_api.h:61
Ticker's data structure.
Definition: ticker_api.h:93
LowPowerTickerWrapper(const ticker_data_t *data, const ticker_interface_t *interface, uint32_t min_cycles_between_writes, uint32_t min_cycles_until_match)
Create a new wrapped low power ticker object.
void suspend()
Suspend wrapper operation and pass through interrupts.
uint32_t timestamp_t
Legacy format representing a timestamp in us.
Definition: ticker_api.h:33
void resume()
Resume wrapper operation and filter interrupts normally.
A Timeout is used to call a function at a point in the future.
Definition: Timeout.h:60
bool timeout_pending()
Check if a Timeout object is being used.
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.