Dependencies:   MMA7660 LM75B

Committer:
MACRUM
Date:
Sat Jun 30 01:40:30 2018 +0000
Revision:
0:119624335925
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MACRUM 0:119624335925 1 /*
MACRUM 0:119624335925 2 * Copyright (c) 2014-2015 ARM Limited. All rights reserved.
MACRUM 0:119624335925 3 * SPDX-License-Identifier: Apache-2.0
MACRUM 0:119624335925 4 * Licensed under the Apache License, Version 2.0 (the License); you may
MACRUM 0:119624335925 5 * not use this file except in compliance with the License.
MACRUM 0:119624335925 6 * You may obtain a copy of the License at
MACRUM 0:119624335925 7 *
MACRUM 0:119624335925 8 * http://www.apache.org/licenses/LICENSE-2.0
MACRUM 0:119624335925 9 *
MACRUM 0:119624335925 10 * Unless required by applicable law or agreed to in writing, software
MACRUM 0:119624335925 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
MACRUM 0:119624335925 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
MACRUM 0:119624335925 13 * See the License for the specific language governing permissions and
MACRUM 0:119624335925 14 * limitations under the License.
MACRUM 0:119624335925 15 */
MACRUM 0:119624335925 16 #ifndef EVENTOS_EVENT_TIMER_H_
MACRUM 0:119624335925 17 #define EVENTOS_EVENT_TIMER_H_
MACRUM 0:119624335925 18 #ifdef __cplusplus
MACRUM 0:119624335925 19 extern "C" {
MACRUM 0:119624335925 20 #endif
MACRUM 0:119624335925 21 #include "ns_types.h"
MACRUM 0:119624335925 22 #include "eventOS_event.h"
MACRUM 0:119624335925 23
MACRUM 0:119624335925 24 struct arm_event_s;
MACRUM 0:119624335925 25 typedef struct sys_timer_struct_s sys_timer_struct_t;
MACRUM 0:119624335925 26
MACRUM 0:119624335925 27 /* 100 Hz ticker, so 10 milliseconds per tick */
MACRUM 0:119624335925 28 #define EVENTOS_EVENT_TIMER_HZ 100
MACRUM 0:119624335925 29
MACRUM 0:119624335925 30 static inline uint32_t eventOS_event_timer_ticks_to_ms(uint32_t ticks)
MACRUM 0:119624335925 31 {
MACRUM 0:119624335925 32 NS_STATIC_ASSERT(1000 % EVENTOS_EVENT_TIMER_HZ == 0, "Assuming whole number of ms per tick")
MACRUM 0:119624335925 33 return ticks * (1000 / EVENTOS_EVENT_TIMER_HZ);
MACRUM 0:119624335925 34 }
MACRUM 0:119624335925 35
MACRUM 0:119624335925 36 /* Convert ms to ticks, rounding up (so 9ms = 1 tick, 10ms = 1 tick, 11ms = 2 ticks) */
MACRUM 0:119624335925 37 static inline uint32_t eventOS_event_timer_ms_to_ticks(uint32_t ms)
MACRUM 0:119624335925 38 {
MACRUM 0:119624335925 39 NS_STATIC_ASSERT(1000 % EVENTOS_EVENT_TIMER_HZ == 0, "Assuming whole number of ms per tick")
MACRUM 0:119624335925 40 return (ms + (1000 / EVENTOS_EVENT_TIMER_HZ) - 1) / (1000 / EVENTOS_EVENT_TIMER_HZ);
MACRUM 0:119624335925 41 }
MACRUM 0:119624335925 42
MACRUM 0:119624335925 43 /**
MACRUM 0:119624335925 44 * Read current timer tick count.
MACRUM 0:119624335925 45 *
MACRUM 0:119624335925 46 * Can be used as a monotonic time source, and to schedule events with
MACRUM 0:119624335925 47 * eventOS_event_timer_send.
MACRUM 0:119624335925 48 *
MACRUM 0:119624335925 49 * Note that the value will wrap, so take care on comparisons.
MACRUM 0:119624335925 50 *
MACRUM 0:119624335925 51 * \return tick count.
MACRUM 0:119624335925 52 */
MACRUM 0:119624335925 53 extern uint32_t eventOS_event_timer_ticks(void);
MACRUM 0:119624335925 54
MACRUM 0:119624335925 55 /* Comparison macros handling wrap efficiently (assuming a conventional compiler
MACRUM 0:119624335925 56 * which converts 0x80000000 to 0xFFFFFFFF to negative when casting to int32_t).
MACRUM 0:119624335925 57 */
MACRUM 0:119624335925 58 #define TICKS_AFTER(a, b) ((int32_t) ((a)-(b)) > 0)
MACRUM 0:119624335925 59 #define TICKS_BEFORE(a, b) ((int32_t) ((a)-(b)) < 0)
MACRUM 0:119624335925 60 #define TICKS_AFTER_OR_AT(a, b) ((int32_t) ((a)-(b)) >= 0)
MACRUM 0:119624335925 61 #define TICKS_BEFORE_OR_AT(a, b) ((int32_t) ((a)-(b)) <= 0)
MACRUM 0:119624335925 62
MACRUM 0:119624335925 63 /**
MACRUM 0:119624335925 64 * Send an event after time expired (in milliseconds)
MACRUM 0:119624335925 65 *
MACRUM 0:119624335925 66 * Note that the current implementation has the "feature" that rounding
MACRUM 0:119624335925 67 * varies depending on the precise timing requested:
MACRUM 0:119624335925 68 * 0-20 ms => 2 x 10ms tick
MACRUM 0:119624335925 69 * 21-29 ms => 3 x 10ms tick
MACRUM 0:119624335925 70 * 30-39 ms => 4 x 10ms tick
MACRUM 0:119624335925 71 * 40-49 ms => 5 x 10ms tick
MACRUM 0:119624335925 72 * ... etc
MACRUM 0:119624335925 73 *
MACRUM 0:119624335925 74 * For improved flexibility on the event, and for more control of time,
MACRUM 0:119624335925 75 * you should use eventOS_event_timer_request_at().
MACRUM 0:119624335925 76 *
MACRUM 0:119624335925 77 * \param event_id event_id for event
MACRUM 0:119624335925 78 * \param event_type event_type for event
MACRUM 0:119624335925 79 * \param tasklet_id receiver for event
MACRUM 0:119624335925 80 * \param time time to sleep in milliseconds
MACRUM 0:119624335925 81 *
MACRUM 0:119624335925 82 * \return 0 on success
MACRUM 0:119624335925 83 * \return -1 on error (invalid tasklet_id or allocation failure)
MACRUM 0:119624335925 84 *
MACRUM 0:119624335925 85 * */
MACRUM 0:119624335925 86 extern int8_t eventOS_event_timer_request(uint8_t event_id, uint8_t event_type, int8_t tasklet_id, uint32_t time);
MACRUM 0:119624335925 87
MACRUM 0:119624335925 88 /**
MACRUM 0:119624335925 89 * Send an event at specified time
MACRUM 0:119624335925 90 *
MACRUM 0:119624335925 91 * The event will be sent when eventOS_event_timer_ticks() reaches the
MACRUM 0:119624335925 92 * specified value.
MACRUM 0:119624335925 93 *
MACRUM 0:119624335925 94 * If the specified time is in the past (ie "at" is before or at the current
MACRUM 0:119624335925 95 * tick value), the event will be sent immediately.
MACRUM 0:119624335925 96 *
MACRUM 0:119624335925 97 * Can also be invoked using the eventOS_event_send_at() macro in eventOS_event.h
MACRUM 0:119624335925 98 *
MACRUM 0:119624335925 99 * \param event event to send
MACRUM 0:119624335925 100 * \param at absolute tick time to run event at
MACRUM 0:119624335925 101 *
MACRUM 0:119624335925 102 * \return pointer to timer structure on success
MACRUM 0:119624335925 103 * \return NULL on error (invalid tasklet_id or allocation failure)
MACRUM 0:119624335925 104 *
MACRUM 0:119624335925 105 */
MACRUM 0:119624335925 106 extern arm_event_storage_t *eventOS_event_timer_request_at(const struct arm_event_s *event, uint32_t at);
MACRUM 0:119624335925 107
MACRUM 0:119624335925 108 /**
MACRUM 0:119624335925 109 * Send an event in a specified time
MACRUM 0:119624335925 110 *
MACRUM 0:119624335925 111 * The event will be sent in the specified number of ticks - to
MACRUM 0:119624335925 112 * be precise, it is equivalent to requesting an event at
MACRUM 0:119624335925 113 *
MACRUM 0:119624335925 114 * eventOS_event_timer_ticks() + ticks
MACRUM 0:119624335925 115 *
MACRUM 0:119624335925 116 * Because of timer granularity, the elapsed time between issuing the request
MACRUM 0:119624335925 117 * and it running may be up to 1 tick less than the specified time.
MACRUM 0:119624335925 118 *
MACRUM 0:119624335925 119 * eg requesting 2 ticks will cause the event to be sent on the second tick from
MACRUM 0:119624335925 120 * now. If requested just after a tick, the delay will be nearly 2 ticks, but if
MACRUM 0:119624335925 121 * requested just before a tick, the delay will be just over 1 tick.
MACRUM 0:119624335925 122 *
MACRUM 0:119624335925 123 * If `in` is <= 0, the event will be sent immediately.
MACRUM 0:119624335925 124 *
MACRUM 0:119624335925 125 * Can also be invoked using the eventOS_event_send_in() macro in eventOS_event.h
MACRUM 0:119624335925 126 *
MACRUM 0:119624335925 127 * \param event event to send
MACRUM 0:119624335925 128 * \param in tick delay for event
MACRUM 0:119624335925 129 *
MACRUM 0:119624335925 130 * \return pointer to timer structure on success
MACRUM 0:119624335925 131 * \return NULL on error (invalid tasklet_id or allocation failure)
MACRUM 0:119624335925 132 *
MACRUM 0:119624335925 133 */
MACRUM 0:119624335925 134 extern arm_event_storage_t *eventOS_event_timer_request_in(const struct arm_event_s *event, int32_t in);
MACRUM 0:119624335925 135
MACRUM 0:119624335925 136 /**
MACRUM 0:119624335925 137 * Send an event after a specified time
MACRUM 0:119624335925 138 *
MACRUM 0:119624335925 139 * The event will be sent after the specified number of ticks - to
MACRUM 0:119624335925 140 * be precise, it is equivalent to requesting an event at
MACRUM 0:119624335925 141 *
MACRUM 0:119624335925 142 * eventOS_event_timer_ticks() + ticks + 1
MACRUM 0:119624335925 143 *
MACRUM 0:119624335925 144 * Because of timer granularity, the elapsed time between issuing the request
MACRUM 0:119624335925 145 * and it running may be up to 1 tick more than the specified time.
MACRUM 0:119624335925 146 *
MACRUM 0:119624335925 147 * eg requesting 2 ticks will cause the event to be sent on the third tick from
MACRUM 0:119624335925 148 * now. If requested just after a tick, the delay will be nearly 3 ticks, but if
MACRUM 0:119624335925 149 * requested just before a tick, the delay will be just over 2 ticks.
MACRUM 0:119624335925 150 *
MACRUM 0:119624335925 151 * If `after` is < 0, the event will be sent immediately. If it is 0, the event
MACRUM 0:119624335925 152 * is sent on the next tick.
MACRUM 0:119624335925 153 *
MACRUM 0:119624335925 154 * Can also be invoked using the eventOS_event_send_after() macro in eventOS_event.h
MACRUM 0:119624335925 155 *
MACRUM 0:119624335925 156 * \param event event to send
MACRUM 0:119624335925 157 * \param after tick delay for event
MACRUM 0:119624335925 158 *
MACRUM 0:119624335925 159 * \return pointer to timer structure on success
MACRUM 0:119624335925 160 * \return NULL on error (invalid tasklet_id or allocation failure)
MACRUM 0:119624335925 161 *
MACRUM 0:119624335925 162 */
MACRUM 0:119624335925 163 #define eventOS_event_timer_request_after(event, after) \
MACRUM 0:119624335925 164 eventOS_event_timer_request_in(event, (after) + 1)
MACRUM 0:119624335925 165
MACRUM 0:119624335925 166 /**
MACRUM 0:119624335925 167 * Send an event periodically
MACRUM 0:119624335925 168 *
MACRUM 0:119624335925 169 * The event will be sent repeatedly using the specified ticks period.
MACRUM 0:119624335925 170 *
MACRUM 0:119624335925 171 * The first call is sent at
MACRUM 0:119624335925 172 *
MACRUM 0:119624335925 173 * eventOS_event_timer_ticks() + ticks
MACRUM 0:119624335925 174 *
MACRUM 0:119624335925 175 * Subsequent events will be sent at N*ticks from the initial time.
MACRUM 0:119624335925 176 *
MACRUM 0:119624335925 177 * Period will be maintained while the device is awake, regardless of delays to
MACRUM 0:119624335925 178 * event scheduling. If an event has not been delivered and completed by the
MACRUM 0:119624335925 179 * next scheduled time, the next event will be sent immediately when it
MACRUM 0:119624335925 180 * finishes. This could cause a continuous stream of events if unable to keep
MACRUM 0:119624335925 181 * up with the period.
MACRUM 0:119624335925 182 *
MACRUM 0:119624335925 183 * Can also be invoked using the eventOS_event_send_every() macro in eventOS_event.h
MACRUM 0:119624335925 184 *
MACRUM 0:119624335925 185 * \param event event to send
MACRUM 0:119624335925 186 * \param period period for event
MACRUM 0:119624335925 187 *
MACRUM 0:119624335925 188 * \return pointer to timer structure on success
MACRUM 0:119624335925 189 * \return NULL on error (invalid tasklet_id or allocation failure)
MACRUM 0:119624335925 190 *
MACRUM 0:119624335925 191 */
MACRUM 0:119624335925 192 extern arm_event_storage_t *eventOS_event_timer_request_every(const struct arm_event_s *event, int32_t period);
MACRUM 0:119624335925 193
MACRUM 0:119624335925 194 /**
MACRUM 0:119624335925 195 * Cancel an event timer
MACRUM 0:119624335925 196 *
MACRUM 0:119624335925 197 * This cancels a pending timed event, matched by event_id and tasklet_id.
MACRUM 0:119624335925 198 *
MACRUM 0:119624335925 199 * \param event_id event_id for event
MACRUM 0:119624335925 200 * \param tasklet_id receiver for event
MACRUM 0:119624335925 201 *
MACRUM 0:119624335925 202 * \return 0 on success
MACRUM 0:119624335925 203 * \return -1 on error (event not found)
MACRUM 0:119624335925 204 *
MACRUM 0:119624335925 205 * */
MACRUM 0:119624335925 206 extern int8_t eventOS_event_timer_cancel(uint8_t event_id, int8_t tasklet_id);
MACRUM 0:119624335925 207
MACRUM 0:119624335925 208 /**
MACRUM 0:119624335925 209 * System Timer shortest time in milli seconds
MACRUM 0:119624335925 210 *
MACRUM 0:119624335925 211 * \param ticks Time in 10 ms resolution
MACRUM 0:119624335925 212 *
MACRUM 0:119624335925 213 * \return none
MACRUM 0:119624335925 214 *
MACRUM 0:119624335925 215 * */
MACRUM 0:119624335925 216 extern uint32_t eventOS_event_timer_shortest_active_timer(void);
MACRUM 0:119624335925 217
MACRUM 0:119624335925 218
MACRUM 0:119624335925 219 /** Timeout structure. Not to be modified by user */
MACRUM 0:119624335925 220 typedef struct timeout_entry_t timeout_t;
MACRUM 0:119624335925 221
MACRUM 0:119624335925 222 /** Request timeout callback.
MACRUM 0:119624335925 223 *
MACRUM 0:119624335925 224 * Create timeout request for specific callback.
MACRUM 0:119624335925 225 *
MACRUM 0:119624335925 226 * \param ms timeout in milliseconds. Maximum range is same as for eventOS_event_timer_request().
MACRUM 0:119624335925 227 * \param callback function to call after timeout
MACRUM 0:119624335925 228 * \param arg arquement to pass to callback
MACRUM 0:119624335925 229 * \return pointer to timeout structure or NULL on errors
MACRUM 0:119624335925 230 */
MACRUM 0:119624335925 231 timeout_t *eventOS_timeout_ms(void (*callback)(void *), uint32_t ms, void *arg);
MACRUM 0:119624335925 232
MACRUM 0:119624335925 233 /** Request periodic callback.
MACRUM 0:119624335925 234 *
MACRUM 0:119624335925 235 * Create timeout request for specific callback. Called periodically until eventOS_timeout_cancel() is called.
MACRUM 0:119624335925 236 *
MACRUM 0:119624335925 237 * \param every period in milliseconds. Maximum range is same as for eventOS_event_timer_request().
MACRUM 0:119624335925 238 * \param callback function to call after timeout
MACRUM 0:119624335925 239 * \param arg arquement to pass to callback
MACRUM 0:119624335925 240 * \return pointer to timeout structure or NULL on errors
MACRUM 0:119624335925 241 */
MACRUM 0:119624335925 242 timeout_t *eventOS_timeout_every_ms(void (*callback)(void *), uint32_t every, void *arg);
MACRUM 0:119624335925 243
MACRUM 0:119624335925 244 /** Cancell timeout request.
MACRUM 0:119624335925 245 *
MACRUM 0:119624335925 246 * \param t timeout request id.
MACRUM 0:119624335925 247 */
MACRUM 0:119624335925 248 void eventOS_timeout_cancel(timeout_t *t);
MACRUM 0:119624335925 249
MACRUM 0:119624335925 250
MACRUM 0:119624335925 251 #ifdef __cplusplus
MACRUM 0:119624335925 252 }
MACRUM 0:119624335925 253 #endif
MACRUM 0:119624335925 254
MACRUM 0:119624335925 255 #endif /* EVENTOS_EVENT_TIMER_H_ */