t

Fork of mbed-dev by mbed official

Committer:
AnnaBridge
Date:
Wed Jun 21 17:46:44 2017 +0100
Revision:
167:e84263d55307
Parent:
149:156823d33999
Child:
174:b96e65c34a4d
This updates the lib to the mbed lib v 145

Who changed what in which revision?

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