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:
Anna Bridge
Date:
Fri Apr 20 11:08:29 2018 +0100
Revision:
166:5aab5a7997ee
Parent:
160:5571c4ff569f
Child:
169:a7c7b631e539
Updating mbed 2 version number

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