Backup 1

Committer:
borlanic
Date:
Tue Apr 24 11:45:18 2018 +0000
Revision:
0:02dd72d1d465
BaBoRo_test2 - backup 1

Who changed what in which revision?

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