leo hendrickson / Mbed OS example-Ethernet-mbed-Cloud-connect
Committer:
leothedragon
Date:
Tue May 04 08:55:12 2021 +0000
Revision:
0:8f0bb79ddd48
nmn

Who changed what in which revision?

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