Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
timer.c
00001 /* 00002 * Copyright 2010-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved. 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"). 00005 * You may not use this file except in compliance with the License. 00006 * A copy of the License is located at 00007 * 00008 * http://aws.amazon.com/apache2.0 00009 * 00010 * or in the "license" file accompanying this file. This file is distributed 00011 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 00012 * express or implied. See the License for the specific language governing 00013 * permissions and limitations under the License. 00014 */ 00015 00016 /** 00017 * @file timer.c 00018 * @brief Linux implementation of the timer interface. 00019 */ 00020 00021 #ifdef __cplusplus 00022 extern "C" { 00023 #endif 00024 00025 #include <stddef.h> 00026 //#include <sys/types.h> 00027 #include <stdint.h> 00028 #include <stdbool.h> 00029 00030 #include <stdio.h> 00031 //#include <sys/time.h> 00032 #include <time.h> 00033 00034 00035 #include "timer_platform.h " 00036 00037 #define timerclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0) 00038 #define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec) 00039 #define timercmp(tvp, uvp, cmp) \ 00040 (((tvp)->tv_sec == (uvp)->tv_sec) ? \ 00041 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \ 00042 ((tvp)->tv_sec cmp (uvp)->tv_sec)) 00043 #define timeradd(tvp, uvp, vvp) \ 00044 do { \ 00045 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \ 00046 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \ 00047 if ((vvp)->tv_usec >= 1000000) { \ 00048 (vvp)->tv_sec++; \ 00049 (vvp)->tv_usec -= 1000000; \ 00050 } \ 00051 } while (0) 00052 #define timersub(tvp, uvp, vvp) \ 00053 do { \ 00054 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \ 00055 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \ 00056 if ((vvp)->tv_usec < (time_t)0) { \ 00057 (vvp)->tv_sec--; \ 00058 (vvp)->tv_usec += 1000000; \ 00059 } \ 00060 } while (0) 00061 00062 volatile uint32_t mscount = 0; 00063 00064 void onMillisecondTicker(void){ 00065 mscount++; 00066 } 00067 00068 void mbed_gettimeofday(struct timeval *now) 00069 { 00070 uint32_t cnt = mscount; 00071 now->tv_sec = time(NULL); 00072 now->tv_usec= cnt - ((long)cnt/1000)*1000; 00073 } 00074 00075 00076 bool has_timer_expired(awsTimer *timer) { 00077 struct timeval now, res; 00078 mbed_gettimeofday(&now); 00079 timersub(&timer->end_time, &now, &res); 00080 return res.tv_sec < 0 || (res.tv_sec == 0 && res.tv_usec <= 0); 00081 } 00082 00083 void countdown_ms(awsTimer *timer, uint32_t timeout) { 00084 struct timeval now; 00085 #ifdef __cplusplus 00086 struct timeval interval = {timeout / 1000, static_cast<int>((timeout % 1000) * 1000)}; 00087 #else 00088 struct timeval interval = {timeout / 1000, (int)((timeout % 1000) * 1000)}; 00089 #endif 00090 mbed_gettimeofday(&now); 00091 timeradd(&now, &interval, &timer->end_time); 00092 } 00093 00094 uint32_t left_ms(awsTimer *timer) { 00095 struct timeval now, res; 00096 uint32_t result_ms = 0; 00097 mbed_gettimeofday(&now); 00098 timersub(&timer->end_time, &now, &res); 00099 if(res.tv_sec >= 0) { 00100 result_ms = (uint32_t) (res.tv_sec * 1000 + res.tv_usec / 1000); 00101 } 00102 return result_ms; 00103 } 00104 00105 void countdown_sec(awsTimer *timer, uint32_t timeout) { 00106 struct timeval now; 00107 struct timeval interval = {timeout, 0}; 00108 mbed_gettimeofday(&now); 00109 timeradd(&now, &interval, &timer->end_time); 00110 } 00111 00112 void init_timer(awsTimer *timer) { 00113 timer->end_time = (struct timeval) {0, 0}; 00114 } 00115 00116 #ifdef __cplusplus 00117 } 00118 #endif 00119
Generated on Tue Jul 12 2022 19:02:38 by
1.7.2