|
| 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/lp_ticker_api.h" |
| 17 | + |
| 18 | +#if DEVICE_LOWPOWERTIMER && defined(LOWPOWERTIMER_DELAY_TICKS) && (LOWPOWERTIMER_DELAY_TICKS > 0) |
| 19 | + |
| 20 | +#include "Timeout.h" |
| 21 | +#include "mbed_critical.h" |
| 22 | + |
| 23 | +static bool init = false; |
| 24 | +static bool pending = false; |
| 25 | +static bool timeout_pending = false; |
| 26 | +static timestamp_t last_set_interrupt = 0; |
| 27 | +static timestamp_t last_request = 0; |
| 28 | +static timestamp_t next = 0; |
| 29 | + |
| 30 | +static timestamp_t mask; |
| 31 | +static timestamp_t min_delta; |
| 32 | +static timestamp_t reschedule_us; |
| 33 | + |
| 34 | +// Do not use SingletonPtr since this must be initialized in a critical section |
| 35 | +static mbed::Timeout *timeout; |
| 36 | +static uint64_t timeout_data[sizeof(mbed::Timeout) / 8]; |
| 37 | + |
| 38 | +/** |
| 39 | + * Initialize variables |
| 40 | + */ |
| 41 | +static void init_local() |
| 42 | +{ |
| 43 | + MBED_ASSERT(core_util_in_critical_section()); |
| 44 | + |
| 45 | + const ticker_info_t* info = lp_ticker_get_info(); |
| 46 | + if (info->bits >= 32) { |
| 47 | + mask = 0xffffffff; |
| 48 | + } else { |
| 49 | + mask = ((uint64_t)1 << info->bits) - 1; |
| 50 | + } |
| 51 | + |
| 52 | + // Round us_per_tick up |
| 53 | + timestamp_t us_per_tick = (1000000 + info->frequency - 1) / info->frequency; |
| 54 | + |
| 55 | + min_delta = LOWPOWERTIMER_DELAY_TICKS; |
| 56 | + |
| 57 | + // Add 1 tick to the min delta for the case where the clock transitions after you read it |
| 58 | + // Add 4 microseconds to round up the micro second ticker time (which has a frequency of at least 250KHz - 4us period) |
| 59 | + reschedule_us = (min_delta + 1) * us_per_tick + 4; |
| 60 | + |
| 61 | + timeout = new (timeout_data) mbed::Timeout(); |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * Call lp_ticker_set_interrupt with a value that is guaranteed to fire |
| 66 | + * |
| 67 | + * Assumptions |
| 68 | + * -Only one low power clock tick can pass from the last read (last_read) |
| 69 | + * -The closest an interrupt can fire is max_delta + 1 |
| 70 | + * |
| 71 | + * @param last_read The last value read from lp_ticker_read |
| 72 | + * @param timestamp The timestamp to trigger the interrupt at |
| 73 | + */ |
| 74 | +static void set_interrupt_safe(timestamp_t last_read, timestamp_t timestamp) |
| 75 | +{ |
| 76 | + MBED_ASSERT(core_util_in_critical_section()); |
| 77 | + uint32_t delta = (timestamp - last_read) & mask; |
| 78 | + if (delta < min_delta + 2) { |
| 79 | + timestamp = (last_read + min_delta + 2) & mask; |
| 80 | + } |
| 81 | + lp_ticker_set_interrupt(timestamp); |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * Set the low power ticker match time when hardware is ready |
| 86 | + * |
| 87 | + * This event is scheduled to set the lp timer after the previous write |
| 88 | + * has taken effect and it is safe to write a new value without blocking. |
| 89 | + * If the time has already passed then this function fires and interrupt |
| 90 | + * immediately. |
| 91 | + */ |
| 92 | +static void set_interrupt_later() |
| 93 | +{ |
| 94 | + core_util_critical_section_enter(); |
| 95 | + |
| 96 | + timestamp_t current = lp_ticker_read(); |
| 97 | + if (_ticker_match_interval_passed(last_request, current, next)) { |
| 98 | + lp_ticker_fire_interrupt(); |
| 99 | + } else { |
| 100 | + set_interrupt_safe(current, next); |
| 101 | + last_set_interrupt = lp_ticker_read(); |
| 102 | + } |
| 103 | + timeout_pending = false; |
| 104 | + |
| 105 | + core_util_critical_section_exit(); |
| 106 | +} |
| 107 | + |
| 108 | +void lp_ticker_set_interrupt_wrapper(timestamp_t timestamp) |
| 109 | +{ |
| 110 | + core_util_critical_section_enter(); |
| 111 | + |
| 112 | + if (!init) { |
| 113 | + init_local(); |
| 114 | + init = true; |
| 115 | + } |
| 116 | + |
| 117 | + timestamp_t current = lp_ticker_read(); |
| 118 | + if (pending) { |
| 119 | + // Check if pending should be cleared |
| 120 | + if (((current - last_set_interrupt) & mask) >= LOWPOWERTIMER_DELAY_TICKS) { |
| 121 | + pending = false; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + if (pending) { |
| 126 | + next = timestamp; |
| 127 | + last_request = current; |
| 128 | + if (!timeout_pending) { |
| 129 | + timeout->attach_us(set_interrupt_later, reschedule_us); |
| 130 | + timeout_pending = true; |
| 131 | + } |
| 132 | + } else { |
| 133 | + // Schedule immediately if nothing is pending |
| 134 | + set_interrupt_safe(current, timestamp); |
| 135 | + last_set_interrupt = lp_ticker_read(); |
| 136 | + pending = true; |
| 137 | + } |
| 138 | + |
| 139 | + core_util_critical_section_exit(); |
| 140 | +} |
| 141 | + |
| 142 | +#endif |
0 commit comments