Erste version der Software für der Prototyp

Committer:
borlanic
Date:
Thu Mar 29 07:02:09 2018 +0000
Revision:
0:380207fcb5c1
Encoder, IMU --> OK; Controller --> in bearbeitung

Who changed what in which revision?

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