The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Dependents:   hello SerialTestv11 SerialTestv12 Sierpinski ... more

mbed 2

This is the mbed 2 library. If you'd like to learn about Mbed OS please see the mbed-os docs.

Committer:
AnnaBridge
Date:
Wed Feb 20 20:53:29 2019 +0000
Revision:
172:65be27845400
Parent:
171:3a7713b1edbc
mbed library release version 165

Who changed what in which revision?

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