inport from local

Dependents:   Hobbyking_Cheetah_0511

Committer:
NYX
Date:
Mon Mar 16 06:35:48 2020 +0000
Revision:
0:85b3fd62ea1a
reinport to mbed;

Who changed what in which revision?

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