mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

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?

UserRevisionLine numberNew 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) 2015 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_TICKER_API_H
AnnaBridge 189:f392fc9709a3 21 #define MBED_TICKER_API_H
AnnaBridge 189:f392fc9709a3 22
AnnaBridge 189:f392fc9709a3 23 #include <stdint.h>
AnnaBridge 189:f392fc9709a3 24 #include <stdbool.h>
AnnaBridge 189:f392fc9709a3 25 #include "device.h"
AnnaBridge 189:f392fc9709a3 26
AnnaBridge 189:f392fc9709a3 27 /**
AnnaBridge 189:f392fc9709a3 28 * Legacy format representing a timestamp in us.
AnnaBridge 189:f392fc9709a3 29 * Given it is modeled as a 32 bit integer, this type can represent timestamp
AnnaBridge 189:f392fc9709a3 30 * up to 4294 seconds (71 minutes).
AnnaBridge 189:f392fc9709a3 31 * Prefer using us_timestamp_t which store timestamp as 64 bits integer.
AnnaBridge 189:f392fc9709a3 32 */
AnnaBridge 189:f392fc9709a3 33 typedef uint32_t timestamp_t;
AnnaBridge 189:f392fc9709a3 34
AnnaBridge 189:f392fc9709a3 35 /**
AnnaBridge 189:f392fc9709a3 36 * A us timestamp stored in a 64 bit integer.
AnnaBridge 189:f392fc9709a3 37 * Can store timestamp up to 584810 years.
AnnaBridge 189:f392fc9709a3 38 */
AnnaBridge 189:f392fc9709a3 39 typedef uint64_t us_timestamp_t;
AnnaBridge 189:f392fc9709a3 40
AnnaBridge 189:f392fc9709a3 41 /** Ticker's event structure
AnnaBridge 189:f392fc9709a3 42 */
AnnaBridge 189:f392fc9709a3 43 typedef struct ticker_event_s {
AnnaBridge 189:f392fc9709a3 44 us_timestamp_t timestamp; /**< Event's timestamp */
AnnaBridge 189:f392fc9709a3 45 uint32_t id; /**< TimerEvent object */
AnnaBridge 189:f392fc9709a3 46 struct ticker_event_s *next; /**< Next event in the queue */
AnnaBridge 189:f392fc9709a3 47 } ticker_event_t;
AnnaBridge 189:f392fc9709a3 48
AnnaBridge 189:f392fc9709a3 49 typedef void (*ticker_event_handler)(uint32_t id);
AnnaBridge 189:f392fc9709a3 50
AnnaBridge 189:f392fc9709a3 51 /** Information about the ticker implementation
AnnaBridge 189:f392fc9709a3 52 */
AnnaBridge 189:f392fc9709a3 53 typedef struct {
AnnaBridge 189:f392fc9709a3 54 uint32_t frequency; /**< Frequency in Hz this ticker runs at */
AnnaBridge 189:f392fc9709a3 55 uint32_t bits; /**< Number of bits this ticker supports */
AnnaBridge 189:f392fc9709a3 56 } ticker_info_t;
AnnaBridge 189:f392fc9709a3 57
AnnaBridge 189:f392fc9709a3 58
AnnaBridge 189:f392fc9709a3 59 /** Ticker's interface structure - required API for a ticker
AnnaBridge 189:f392fc9709a3 60 */
AnnaBridge 189:f392fc9709a3 61 typedef struct {
AnnaBridge 189:f392fc9709a3 62 void (*init)(void); /**< Init function */
AnnaBridge 189:f392fc9709a3 63 uint32_t (*read)(void); /**< Read function */
AnnaBridge 189:f392fc9709a3 64 void (*disable_interrupt)(void); /**< Disable interrupt function */
AnnaBridge 189:f392fc9709a3 65 void (*clear_interrupt)(void); /**< Clear interrupt function */
AnnaBridge 189:f392fc9709a3 66 void (*set_interrupt)(timestamp_t timestamp); /**< Set interrupt function */
AnnaBridge 189:f392fc9709a3 67 void (*fire_interrupt)(void); /**< Fire interrupt right-away */
AnnaBridge 189:f392fc9709a3 68 void (*free)(void); /**< Disable function */
AnnaBridge 189:f392fc9709a3 69 const ticker_info_t *(*get_info)(void); /**< Return info about this ticker's implementation */
AnnaBridge 189:f392fc9709a3 70 } ticker_interface_t;
AnnaBridge 189:f392fc9709a3 71
AnnaBridge 189:f392fc9709a3 72 /** Ticker's event queue structure
AnnaBridge 189:f392fc9709a3 73 */
AnnaBridge 189:f392fc9709a3 74 typedef struct {
AnnaBridge 189:f392fc9709a3 75 ticker_event_handler event_handler; /**< Event handler */
AnnaBridge 189:f392fc9709a3 76 ticker_event_t *head; /**< A pointer to head */
AnnaBridge 189:f392fc9709a3 77 uint32_t frequency; /**< Frequency of the timer in Hz */
AnnaBridge 189:f392fc9709a3 78 uint32_t bitmask; /**< Mask to be applied to time values read */
AnnaBridge 189:f392fc9709a3 79 uint32_t max_delta; /**< Largest delta in ticks that can be used when scheduling */
AnnaBridge 189:f392fc9709a3 80 uint64_t max_delta_us; /**< Largest delta in us that can be used when scheduling */
AnnaBridge 189:f392fc9709a3 81 uint32_t tick_last_read; /**< Last tick read */
AnnaBridge 189:f392fc9709a3 82 uint64_t tick_remainder; /**< Ticks that have not been added to base_time */
AnnaBridge 189:f392fc9709a3 83 us_timestamp_t present_time; /**< Store the timestamp used for present time */
AnnaBridge 189:f392fc9709a3 84 bool initialized; /**< Indicate if the instance is initialized */
AnnaBridge 189:f392fc9709a3 85 bool dispatching; /**< The function ticker_irq_handler is dispatching */
AnnaBridge 189:f392fc9709a3 86 bool suspended; /**< Indicate if the instance is suspended */
AnnaBridge 189:f392fc9709a3 87 uint8_t frequency_shifts; /**< If frequency is a value of 2^n, this is n, otherwise 0 */
AnnaBridge 189:f392fc9709a3 88 } ticker_event_queue_t;
AnnaBridge 189:f392fc9709a3 89
AnnaBridge 189:f392fc9709a3 90 /** Ticker's data structure
AnnaBridge 189:f392fc9709a3 91 */
AnnaBridge 189:f392fc9709a3 92 typedef struct {
AnnaBridge 189:f392fc9709a3 93 const ticker_interface_t *interface; /**< Ticker's interface */
AnnaBridge 189:f392fc9709a3 94 ticker_event_queue_t *queue; /**< Ticker's event queue */
AnnaBridge 189:f392fc9709a3 95 } ticker_data_t;
AnnaBridge 189:f392fc9709a3 96
AnnaBridge 189:f392fc9709a3 97 #ifdef __cplusplus
AnnaBridge 189:f392fc9709a3 98 extern "C" {
AnnaBridge 189:f392fc9709a3 99 #endif
AnnaBridge 189:f392fc9709a3 100
AnnaBridge 189:f392fc9709a3 101 /**
AnnaBridge 189:f392fc9709a3 102 * \defgroup hal_ticker Ticker HAL functions
AnnaBridge 189:f392fc9709a3 103 * @{
AnnaBridge 189:f392fc9709a3 104 */
AnnaBridge 189:f392fc9709a3 105
AnnaBridge 189:f392fc9709a3 106 /** Initialize a ticker and set the event handler
AnnaBridge 189:f392fc9709a3 107 *
AnnaBridge 189:f392fc9709a3 108 * @param ticker The ticker object.
AnnaBridge 189:f392fc9709a3 109 * @param handler A handler to be set
AnnaBridge 189:f392fc9709a3 110 */
AnnaBridge 189:f392fc9709a3 111 void ticker_set_handler(const ticker_data_t *const ticker, ticker_event_handler handler);
AnnaBridge 189:f392fc9709a3 112
AnnaBridge 189:f392fc9709a3 113 /** IRQ handler that goes through the events to trigger overdue events.
AnnaBridge 189:f392fc9709a3 114 *
AnnaBridge 189:f392fc9709a3 115 * @param ticker The ticker object.
AnnaBridge 189:f392fc9709a3 116 */
AnnaBridge 189:f392fc9709a3 117 void ticker_irq_handler(const ticker_data_t *const ticker);
AnnaBridge 189:f392fc9709a3 118
AnnaBridge 189:f392fc9709a3 119 /** Remove an event from the queue
AnnaBridge 189:f392fc9709a3 120 *
AnnaBridge 189:f392fc9709a3 121 * @param ticker The ticker object.
AnnaBridge 189:f392fc9709a3 122 * @param obj The event object to be removed from the queue
AnnaBridge 189:f392fc9709a3 123 */
AnnaBridge 189:f392fc9709a3 124 void ticker_remove_event(const ticker_data_t *const ticker, ticker_event_t *obj);
AnnaBridge 189:f392fc9709a3 125
AnnaBridge 189:f392fc9709a3 126 /** Insert an event to the queue
AnnaBridge 189:f392fc9709a3 127 *
AnnaBridge 189:f392fc9709a3 128 * The event will be executed in timestamp - ticker_read().
AnnaBridge 189:f392fc9709a3 129 *
AnnaBridge 189:f392fc9709a3 130 * @warning This function does not consider timestamp in the past. If an event
AnnaBridge 189:f392fc9709a3 131 * is inserted with a timestamp less than the current timestamp then the event
AnnaBridge 189:f392fc9709a3 132 * will be executed in timestamp - ticker_read() us.
AnnaBridge 189:f392fc9709a3 133 * The internal counter wrap very quickly it is hard to decide weither an
AnnaBridge 189:f392fc9709a3 134 * event is in the past or in 1 hour.
AnnaBridge 189:f392fc9709a3 135 *
AnnaBridge 189:f392fc9709a3 136 * @note prefer the use of ticker_insert_event_us which allows registration of
AnnaBridge 189:f392fc9709a3 137 * absolute timestamp.
AnnaBridge 189:f392fc9709a3 138 *
AnnaBridge 189:f392fc9709a3 139 * @param ticker The ticker object.
AnnaBridge 189:f392fc9709a3 140 * @param obj The event object to be inserted to the queue
AnnaBridge 189:f392fc9709a3 141 * @param timestamp The event's timestamp
AnnaBridge 189:f392fc9709a3 142 * @param id The event object
AnnaBridge 189:f392fc9709a3 143 */
AnnaBridge 189:f392fc9709a3 144 void ticker_insert_event(const ticker_data_t *const ticker, ticker_event_t *obj, timestamp_t timestamp, uint32_t id);
AnnaBridge 189:f392fc9709a3 145
AnnaBridge 189:f392fc9709a3 146 /** Insert an event to the queue
AnnaBridge 189:f392fc9709a3 147 *
AnnaBridge 189:f392fc9709a3 148 * The event will be executed in timestamp - ticker_read_us() us.
AnnaBridge 189:f392fc9709a3 149 *
AnnaBridge 189:f392fc9709a3 150 * @note If an event is inserted with a timestamp less than the current
AnnaBridge 189:f392fc9709a3 151 * timestamp then the event will be scheduled immediately resulting in
AnnaBridge 189:f392fc9709a3 152 * an instant call to event handler.
AnnaBridge 189:f392fc9709a3 153 *
AnnaBridge 189:f392fc9709a3 154 * @param ticker The ticker object.
AnnaBridge 189:f392fc9709a3 155 * @param obj The event object to be inserted to the queue
AnnaBridge 189:f392fc9709a3 156 * @param timestamp The event's timestamp
AnnaBridge 189:f392fc9709a3 157 * @param id The event object
AnnaBridge 189:f392fc9709a3 158 */
AnnaBridge 189:f392fc9709a3 159 void ticker_insert_event_us(const ticker_data_t *const ticker, ticker_event_t *obj, us_timestamp_t timestamp, uint32_t id);
AnnaBridge 189:f392fc9709a3 160
AnnaBridge 189:f392fc9709a3 161 /** Read the current (relative) ticker's timestamp
AnnaBridge 189:f392fc9709a3 162 *
AnnaBridge 189:f392fc9709a3 163 * @warning Return a relative timestamp because the counter wrap every 4294
AnnaBridge 189:f392fc9709a3 164 * seconds.
AnnaBridge 189:f392fc9709a3 165 *
AnnaBridge 189:f392fc9709a3 166 * @param ticker The ticker object.
AnnaBridge 189:f392fc9709a3 167 * @return The current timestamp
AnnaBridge 189:f392fc9709a3 168 */
AnnaBridge 189:f392fc9709a3 169 timestamp_t ticker_read(const ticker_data_t *const ticker);
AnnaBridge 189:f392fc9709a3 170
AnnaBridge 189:f392fc9709a3 171 /** Read the current (absolute) ticker's timestamp
AnnaBridge 189:f392fc9709a3 172 *
AnnaBridge 189:f392fc9709a3 173 * @warning Return an absolute timestamp counting from the initialization of the
AnnaBridge 189:f392fc9709a3 174 * ticker.
AnnaBridge 189:f392fc9709a3 175 *
AnnaBridge 189:f392fc9709a3 176 * @param ticker The ticker object.
AnnaBridge 189:f392fc9709a3 177 * @return The current timestamp
AnnaBridge 189:f392fc9709a3 178 */
AnnaBridge 189:f392fc9709a3 179 us_timestamp_t ticker_read_us(const ticker_data_t *const ticker);
AnnaBridge 189:f392fc9709a3 180
AnnaBridge 189:f392fc9709a3 181 /** Read the next event's timestamp
AnnaBridge 189:f392fc9709a3 182 *
AnnaBridge 189:f392fc9709a3 183 * @param ticker The ticker object.
AnnaBridge 189:f392fc9709a3 184 * @param timestamp The timestamp object.
AnnaBridge 189:f392fc9709a3 185 * @return 1 if timestamp is pending event, 0 if there's no event pending
AnnaBridge 189:f392fc9709a3 186 */
AnnaBridge 189:f392fc9709a3 187 int ticker_get_next_timestamp(const ticker_data_t *const ticker, timestamp_t *timestamp);
AnnaBridge 189:f392fc9709a3 188
AnnaBridge 189:f392fc9709a3 189 /** Suspend this ticker
AnnaBridge 189:f392fc9709a3 190 *
AnnaBridge 189:f392fc9709a3 191 * When suspended reads will always return the same time and no
AnnaBridge 189:f392fc9709a3 192 * events will be dispatched. When suspended the common layer
AnnaBridge 189:f392fc9709a3 193 * will only ever call the interface function clear_interrupt()
AnnaBridge 189:f392fc9709a3 194 * and that is only if ticker_irq_handler is called.
AnnaBridge 189:f392fc9709a3 195 *
AnnaBridge 189:f392fc9709a3 196 *
AnnaBridge 189:f392fc9709a3 197 * @param ticker The ticker object.
AnnaBridge 189:f392fc9709a3 198 */
AnnaBridge 189:f392fc9709a3 199 void ticker_suspend(const ticker_data_t *const ticker);
AnnaBridge 189:f392fc9709a3 200
AnnaBridge 189:f392fc9709a3 201 /** Resume this ticker
AnnaBridge 189:f392fc9709a3 202 *
AnnaBridge 189:f392fc9709a3 203 * When resumed the ticker will ignore any time that has passed
AnnaBridge 189:f392fc9709a3 204 * and continue counting up where it left off.
AnnaBridge 189:f392fc9709a3 205 *
AnnaBridge 189:f392fc9709a3 206 * @param ticker The ticker object.
AnnaBridge 189:f392fc9709a3 207 */
AnnaBridge 189:f392fc9709a3 208 void ticker_resume(const ticker_data_t *const ticker);
AnnaBridge 189:f392fc9709a3 209
AnnaBridge 189:f392fc9709a3 210 /* Private functions
AnnaBridge 189:f392fc9709a3 211 *
AnnaBridge 189:f392fc9709a3 212 * @cond PRIVATE
AnnaBridge 189:f392fc9709a3 213 *
AnnaBridge 189:f392fc9709a3 214 */
AnnaBridge 189:f392fc9709a3 215
AnnaBridge 189:f392fc9709a3 216 int _ticker_match_interval_passed(timestamp_t prev_tick, timestamp_t cur_tick, timestamp_t match_tick);
AnnaBridge 189:f392fc9709a3 217
AnnaBridge 189:f392fc9709a3 218 /*
AnnaBridge 189:f392fc9709a3 219 * @endcond PRIVATE
AnnaBridge 189:f392fc9709a3 220 *
AnnaBridge 189:f392fc9709a3 221 */
AnnaBridge 189:f392fc9709a3 222
AnnaBridge 189:f392fc9709a3 223 /**@}*/
AnnaBridge 189:f392fc9709a3 224
AnnaBridge 189:f392fc9709a3 225 #ifdef __cplusplus
AnnaBridge 189:f392fc9709a3 226 }
AnnaBridge 189:f392fc9709a3 227 #endif
AnnaBridge 189:f392fc9709a3 228
AnnaBridge 189:f392fc9709a3 229 #endif
AnnaBridge 189:f392fc9709a3 230
AnnaBridge 189:f392fc9709a3 231 /** @}*/