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