test

Dependencies:   mbed Watchdog

Dependents:   STM32-MC_node

Committer:
ommpy
Date:
Mon Jul 06 17:18:59 2020 +0530
Revision:
0:d383e2dee0f7
first commit

Who changed what in which revision?

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