Ben Katz / mbed-dev_spine

Dependents:   SPIne CH_Communicatuin_Test CH_Communicatuin_Test2 MCP_SPIne ... more

Fork of mbed-dev-f303 by Ben Katz

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  * Maximum delta (in us) between too interrupts.
00028  */
00029 #define MBED_TICKER_INTERRUPT_TIMESTAMP_MAX_DELTA   0x70000000ULL
00030 
00031 /**
00032  * Legacy format representing a timestamp in us.
00033  * Given it is modeled as a 32 bit integer, this type can represent timestamp
00034  * up to 4294 seconds (71 minutes).
00035  * Prefer using us_timestamp_t which store timestamp as 64 bits integer.
00036  */
00037 typedef uint32_t timestamp_t;
00038 
00039 /**
00040  * A us timestamp stored in a 64 bit integer.
00041  * Can store timestamp up to 584810 years.
00042  */
00043 typedef uint64_t us_timestamp_t;
00044 
00045 /** Ticker's event structure
00046  */
00047 typedef struct ticker_event_s {
00048     us_timestamp_t         timestamp; /**< Event's timestamp */
00049     uint32_t               id;        /**< TimerEvent object */
00050     struct ticker_event_s *next;      /**< Next event in the queue */
00051 } ticker_event_t;
00052 
00053 typedef void (*ticker_event_handler)(uint32_t id);
00054 
00055 /** Ticker's interface structure - required API for a ticker
00056  */
00057 typedef struct {
00058     void (*init)(void);                           /**< Init function */
00059     uint32_t (*read)(void);                       /**< Read function */
00060     void (*disable_interrupt)(void);              /**< Disable interrupt function */
00061     void (*clear_interrupt)(void);                /**< Clear interrupt function */
00062     void (*set_interrupt)(timestamp_t timestamp); /**< Set interrupt function */
00063     void (*fire_interrupt)(void);                 /**< Fire interrupt right-away */
00064 } ticker_interface_t;
00065 
00066 /** Ticker's event queue structure
00067  */
00068 typedef struct {
00069     ticker_event_handler event_handler; /**< Event handler */
00070     ticker_event_t *head;               /**< A pointer to head */
00071     us_timestamp_t present_time;        /**< Store the timestamp used for present time */
00072     bool initialized;                   /**< Indicate if the instance is initialized */
00073 } ticker_event_queue_t;
00074 
00075 /** Ticker's data structure
00076  */
00077 typedef struct {
00078     const ticker_interface_t *interface; /**< Ticker's interface */
00079     ticker_event_queue_t *queue;         /**< Ticker's event queue */
00080 } ticker_data_t;
00081 
00082 #ifdef __cplusplus
00083 extern "C" {
00084 #endif
00085 
00086 /**
00087  * \defgroup hal_ticker Ticker HAL functions
00088  * @{
00089  */
00090 
00091 /** Initialize a ticker and set the event handler
00092  *
00093  * @param ticker The ticker object.
00094  * @param handler A handler to be set
00095  */
00096 void ticker_set_handler(const ticker_data_t *const ticker, ticker_event_handler handler);
00097 
00098 /** IRQ handler that goes through the events to trigger overdue events.
00099  *
00100  * @param ticker The ticker object.
00101  */
00102 void ticker_irq_handler(const ticker_data_t *const ticker);
00103 
00104 /** Remove an event from the queue
00105  *
00106  * @param ticker The ticker object.
00107  * @param obj  The event object to be removed from the queue
00108  */
00109 void ticker_remove_event(const ticker_data_t *const ticker, ticker_event_t *obj);
00110 
00111 /** Insert an event to the queue
00112  *
00113  * The event will be executed in timestamp - ticker_read().
00114  *
00115  * @warning This function does not consider timestamp in the past. If an event
00116  * is inserted with a timestamp less than the current timestamp then the event
00117  * will be executed in timestamp - ticker_read() us.
00118  * The internal counter wrap very quickly it is hard to decide weither an
00119  * event is in the past or in 1 hour.
00120  *
00121  * @note prefer the use of ticker_insert_event_us which allows registration of
00122  * absolute timestamp.
00123  *
00124  * @param ticker    The ticker object.
00125  * @param obj       The event object to be inserted to the queue
00126  * @param timestamp The event's timestamp
00127  * @param id        The event object
00128  */
00129 void ticker_insert_event(const ticker_data_t *const ticker, ticker_event_t *obj, timestamp_t timestamp, uint32_t id);
00130 
00131 /** Insert an event to the queue
00132  *
00133  * The event will be executed in timestamp - ticker_read_us() us.
00134  *
00135  * @warning If an event is inserted with a timestamp less than the current
00136  * timestamp then the event will **not** be inserted.
00137  *
00138  * @param ticker    The ticker object.
00139  * @param obj       The event object to be inserted to the queue
00140  * @param timestamp The event's timestamp
00141  * @param id        The event object
00142  */
00143 void ticker_insert_event_us(const ticker_data_t *const ticker, ticker_event_t *obj, us_timestamp_t timestamp, uint32_t id);
00144 
00145 /** Read the current (relative) ticker's timestamp
00146  *
00147  * @warning Return a relative timestamp because the counter wrap every 4294
00148  * seconds.
00149  *
00150  * @param ticker The ticker object.
00151  * @return The current timestamp
00152  */
00153 timestamp_t ticker_read(const ticker_data_t *const ticker);
00154 
00155 /** Read the current (absolute) ticker's timestamp
00156  *
00157  * @warning Return an absolute timestamp counting from the initialization of the
00158  * ticker.
00159  *
00160  * @param ticker The ticker object.
00161  * @return The current timestamp
00162  */
00163 us_timestamp_t ticker_read_us(const ticker_data_t *const ticker);
00164 
00165 /** Read the next event's timestamp
00166  *
00167  * @param ticker        The ticker object.
00168  * @param timestamp     The timestamp object.
00169  * @return 1 if timestamp is pending event, 0 if there's no event pending
00170  */
00171 int ticker_get_next_timestamp(const ticker_data_t *const ticker, timestamp_t *timestamp);
00172 
00173 /**@}*/
00174 
00175 #ifdef __cplusplus
00176 }
00177 #endif
00178 
00179 #endif
00180 
00181 /** @}*/