Pedro Correia / mbed-dev

Fork of mbed-dev by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ticker_api.h Source File

ticker_api.h

00001 
00002 /** \addtogroup hal */
00003 /** @{*/
00004 /* mbed Microcontroller Library
00005  * Copyright (c) 2015 ARM Limited
00006  *
00007  * Licensed under the Apache License, Version 2.0 (the "License");
00008  * you may not use this file except in compliance with the License.
00009  * You may obtain a copy of the License at
00010  *
00011  *     http://www.apache.org/licenses/LICENSE-2.0
00012  *
00013  * Unless required by applicable law or agreed to in writing, software
00014  * distributed under the License is distributed on an "AS IS" BASIS,
00015  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016  * See the License for the specific language governing permissions and
00017  * limitations under the License.
00018  */
00019 #ifndef MBED_TICKER_API_H
00020 #define MBED_TICKER_API_H
00021 
00022 #include <stdint.h>
00023 #include <stdbool.h>
00024 #include "device.h"
00025 
00026 /**
00027  * Legacy format representing a timestamp in us.
00028  * Given it is modeled as a 32 bit integer, this type can represent timestamp
00029  * up to 4294 seconds (71 minutes).
00030  * Prefer using us_timestamp_t which store timestamp as 64 bits integer.
00031  */
00032 typedef uint32_t timestamp_t;
00033 
00034 /**
00035  * A us timestamp stored in a 64 bit integer.
00036  * Can store timestamp up to 584810 years.
00037  */
00038 typedef uint64_t us_timestamp_t;
00039 
00040 /** Ticker's event structure
00041  */
00042 typedef struct ticker_event_s {
00043     us_timestamp_t         timestamp; /**< Event's timestamp */
00044     uint32_t               id;        /**< TimerEvent object */
00045     struct ticker_event_s *next;      /**< Next event in the queue */
00046 } ticker_event_t;
00047 
00048 typedef void (*ticker_event_handler)(uint32_t id);
00049 
00050 /** Information about the ticker implementation
00051  */
00052 typedef struct {
00053     uint32_t frequency;                           /**< Frequency in Hz this ticker runs at */
00054     uint32_t bits;                                /**< Number of bits this ticker supports */
00055 } ticker_info_t;
00056 
00057 
00058 /** Ticker's interface structure - required API for a ticker
00059  */
00060 typedef struct {
00061     void (*init)(void);                           /**< Init function */
00062     uint32_t (*read)(void);                       /**< Read function */
00063     void (*disable_interrupt)(void);              /**< Disable interrupt function */
00064     void (*clear_interrupt)(void);                /**< Clear interrupt function */
00065     void (*set_interrupt)(timestamp_t timestamp); /**< Set interrupt function */
00066     void (*fire_interrupt)(void);                 /**< Fire interrupt right-away */
00067     const ticker_info_t *(*get_info)(void);       /**< Return info about this ticker's implementation */
00068 } ticker_interface_t;
00069 
00070 /** Ticker's event queue structure
00071  */
00072 typedef struct {
00073     ticker_event_handler event_handler; /**< Event handler */
00074     ticker_event_t *head;               /**< A pointer to head */
00075     uint32_t frequency;                 /**< Frequency of the timer in Hz */
00076     uint32_t bitmask;                   /**< Mask to be applied to time values read */
00077     uint32_t max_delta;                 /**< Largest delta in ticks that can be used when scheduling */
00078     uint64_t max_delta_us;              /**< Largest delta in us that can be used when scheduling */
00079     uint32_t tick_last_read;            /**< Last tick read */
00080     uint64_t tick_remainder;            /**< Ticks that have not been added to base_time */
00081     us_timestamp_t present_time;        /**< Store the timestamp used for present time */
00082     bool initialized;                   /**< Indicate if the instance is initialized */
00083     uint8_t frequency_shifts;           /**< If frequency is a value of 2^n, this is n, otherwise 0 */ 
00084 } ticker_event_queue_t;
00085 
00086 /** Ticker's data structure
00087  */
00088 typedef struct {
00089     const ticker_interface_t *interface; /**< Ticker's interface */
00090     ticker_event_queue_t *queue;         /**< Ticker's event queue */
00091 } ticker_data_t;
00092 
00093 #ifdef __cplusplus
00094 extern "C" {
00095 #endif
00096 
00097 /**
00098  * \defgroup hal_ticker Ticker HAL functions
00099  * @{
00100  */
00101 
00102 /** Initialize a ticker and set the event handler
00103  *
00104  * @param ticker The ticker object.
00105  * @param handler A handler to be set
00106  */
00107 void ticker_set_handler(const ticker_data_t *const ticker, ticker_event_handler handler);
00108 
00109 /** IRQ handler that goes through the events to trigger overdue events.
00110  *
00111  * @param ticker The ticker object.
00112  */
00113 void ticker_irq_handler(const ticker_data_t *const ticker);
00114 
00115 /** Remove an event from the queue
00116  *
00117  * @param ticker The ticker object.
00118  * @param obj  The event object to be removed from the queue
00119  */
00120 void ticker_remove_event(const ticker_data_t *const ticker, ticker_event_t *obj);
00121 
00122 /** Insert an event to the queue
00123  *
00124  * The event will be executed in timestamp - ticker_read().
00125  *
00126  * @warning This function does not consider timestamp in the past. If an event
00127  * is inserted with a timestamp less than the current timestamp then the event
00128  * will be executed in timestamp - ticker_read() us.
00129  * The internal counter wrap very quickly it is hard to decide weither an
00130  * event is in the past or in 1 hour.
00131  *
00132  * @note prefer the use of ticker_insert_event_us which allows registration of
00133  * absolute timestamp.
00134  *
00135  * @param ticker    The ticker object.
00136  * @param obj       The event object to be inserted to the queue
00137  * @param timestamp The event's timestamp
00138  * @param id        The event object
00139  */
00140 void ticker_insert_event(const ticker_data_t *const ticker, ticker_event_t *obj, timestamp_t timestamp, uint32_t id);
00141 
00142 /** Insert an event to the queue
00143  *
00144  * The event will be executed in timestamp - ticker_read_us() us.
00145  *
00146  * @note If an event is inserted with a timestamp less than the current
00147  * timestamp then the event will be scheduled immediately resulting in
00148  * an instant call to event handler.
00149  *
00150  * @param ticker    The ticker object.
00151  * @param obj       The event object to be inserted to the queue
00152  * @param timestamp The event's timestamp
00153  * @param id        The event object
00154  */
00155 void ticker_insert_event_us(const ticker_data_t *const ticker, ticker_event_t *obj, us_timestamp_t timestamp, uint32_t id);
00156 
00157 /** Read the current (relative) ticker's timestamp
00158  *
00159  * @warning Return a relative timestamp because the counter wrap every 4294
00160  * seconds.
00161  *
00162  * @param ticker The ticker object.
00163  * @return The current timestamp
00164  */
00165 timestamp_t ticker_read(const ticker_data_t *const ticker);
00166 
00167 /** Read the current (absolute) ticker's timestamp
00168  *
00169  * @warning Return an absolute timestamp counting from the initialization of the
00170  * ticker.
00171  *
00172  * @param ticker The ticker object.
00173  * @return The current timestamp
00174  */
00175 us_timestamp_t ticker_read_us(const ticker_data_t *const ticker);
00176 
00177 /** Read the next event's timestamp
00178  *
00179  * @param ticker        The ticker object.
00180  * @param timestamp     The timestamp object.
00181  * @return 1 if timestamp is pending event, 0 if there's no event pending
00182  */
00183 int ticker_get_next_timestamp(const ticker_data_t *const ticker, timestamp_t *timestamp);
00184 
00185 /* Private functions
00186  *
00187  * @cond PRIVATE
00188  *
00189  */
00190 
00191 int _ticker_match_interval_passed(timestamp_t prev_tick, timestamp_t cur_tick, timestamp_t match_tick);
00192 
00193 /*
00194  * @endcond PRIVATE
00195  *
00196  */
00197 
00198 /**@}*/
00199 
00200 #ifdef __cplusplus
00201 }
00202 #endif
00203 
00204 #endif
00205 
00206 /** @}*/