From Ben Katz mbed-dev library. Removed unnecessary target files to reduce the overall size by a factor of 10 to make it easier to import into the online IDE.

Dependents:   motor_driver motor_driver_screaming_fix

Committer:
saloutos
Date:
Thu Nov 26 04:08:56 2020 +0000
Revision:
0:083111ae2a11
first commit of leaned mbed dev lib

Who changed what in which revision?

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