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