Committer:
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4
Date:
Tue Jun 14 09:21:18 2022 +0000
Revision:
0:bdf663c61a82
lib

Who changed what in which revision?

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