The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Dependents:   hello SerialTestv11 SerialTestv12 Sierpinski ... more

mbed 2

This is the mbed 2 library. If you'd like to learn about Mbed OS please see the mbed-os docs.

Committer:
AnnaBridge
Date:
Thu Nov 09 11:14:10 2017 +0000
Revision:
157:e7ca05fa8600
Parent:
156:ff21514d8981
Child:
160:5571c4ff569f
Release 155 of the mbed library.

Who changed what in which revision?

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