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:
Kojto
Date:
Wed Jul 19 16:46:19 2017 +0100
Revision:
147:a97add6d7e64
Parent:
145:64910690c574
Child:
152:235179ab3f27
Release 147 of the mbed library.

Who changed what in which revision?

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