diff --git a/src/utilities/upm_utilities.c b/src/utilities/upm_utilities.c index 2ebca71b..926fcff8 100644 --- a/src/utilities/upm_utilities.c +++ b/src/utilities/upm_utilities.c @@ -23,12 +23,13 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +#include #include void upm_delay(int time){ -#if defined(linux) +#if defined(UPM_PLATFORM_LINUX) sleep(time); -#elif defined(CONFIG_BOARD_ARDUINO_101) || defined(CONFIG_BOARD_ARDUINO_101_SSS) || defined(CONFIG_BOARD_QUARK_D2000_CRB) +#elif defined(UPM_PLATFORM_ZEPHYR) struct nano_timer timer; void *timer_data[1]; nano_timer_init(&timer, timer_data); @@ -38,9 +39,9 @@ void upm_delay(int time){ } void upm_delay_ms(int time){ -#if defined(linux) +#if defined(UPM_PLATFORM_LINUX) usleep(1000 * time); -#elif defined(CONFIG_BOARD_ARDUINO_101) || defined(CONFIG_BOARD_ARDUINO_101_SSS) || defined(CONFIG_BOARD_QUARK_D2000_CRB) +#elif defined(UPM_PLATFORM_ZEPHYR) struct nano_timer timer; void *timer_data[1]; nano_timer_init(&timer, timer_data); @@ -50,9 +51,9 @@ void upm_delay_ms(int time){ } void upm_delay_us(int time){ -#if defined(linux) +#if defined(UPM_PLATFORM_LINUX) usleep(time); -#elif defined(CONFIG_BOARD_ARDUINO_101) || defined(CONFIG_BOARD_ARDUINO_101_SSS) || defined(CONFIG_BOARD_QUARK_D2000_CRB) +#elif defined(UPM_PLATFORM_ZEPHYR) struct nano_timer timer; void *timer_data[1]; nano_timer_init(&timer, timer_data); @@ -60,3 +61,103 @@ void upm_delay_us(int time){ nano_timer_test(&timer, TICKS_UNLIMITED); #endif } + +void upm_clock_init(upm_clock_t *clock) +{ +#if defined(UPM_PLATFORM_LINUX) + + gettimeofday(clock, NULL); + +#elif defined(UPM_PLATFORM_ZEPHYR) + *clock = sys_cycle_get_32(); +#endif +} + +uint32_t upm_elapsed_ms(upm_clock_t *clock) +{ +#if defined(UPM_PLATFORM_LINUX) + + struct timeval elapsed, now; + uint32_t elapse; + + // get current time + gettimeofday(&now, NULL); + + struct timeval startTime = *clock; + + // compute the delta since startTime + if( (elapsed.tv_usec = now.tv_usec - startTime.tv_usec) < 0 ) + { + elapsed.tv_usec += 1000000; + elapsed.tv_sec = now.tv_sec - startTime.tv_sec - 1; + } + else + { + elapsed.tv_sec = now.tv_sec - startTime.tv_sec; + } + + elapse = (uint32_t)((elapsed.tv_sec * 1000) + (elapsed.tv_usec / 1000)); + + // never return 0 + if (elapse == 0) + elapse = 1; + + return elapse; + +#elif defined(UPM_PLATFORM_ZEPHYR) + uint32_t now = sys_cycle_get_32(); + + uint32_t elapsed = + (uint32_t)(SYS_CLOCK_HW_CYCLES_TO_NS64(now - *clock)/(uint64_t)1000000); + + if (elapsed == 0) + elapsed = 1; + + return elapsed; +#endif +} + +uint32_t upm_elapsed_us(upm_clock_t *clock) +{ +#if defined(UPM_PLATFORM_LINUX) + + struct timeval elapsed, now; + uint32_t elapse; + + // get current time + gettimeofday(&now, NULL); + + struct timeval startTime = *clock; + + // compute the delta since startTime + if( (elapsed.tv_usec = now.tv_usec - startTime.tv_usec) < 0 ) + { + elapsed.tv_usec += 1000000; + elapsed.tv_sec = now.tv_sec - startTime.tv_sec - 1; + } + else + { + elapsed.tv_sec = now.tv_sec - startTime.tv_sec; + } + + elapse = (uint32_t)((elapsed.tv_sec * 1000000) + elapsed.tv_usec); + + // never return 0 + if (elapse == 0) + elapse = 1; + + return elapse; + +#elif defined(UPM_PLATFORM_ZEPHYR) + uint32_t now = sys_cycle_get_32(); + + uint32_t elapsed = + (uint32_t)(SYS_CLOCK_HW_CYCLES_TO_NS64(now - *clock)/(uint64_t)1000); + + // never return 0 + if (elapsed == 0) + elapsed = 1; + + return elapsed; +#endif +} diff --git a/src/utilities/upm_utilities.h b/src/utilities/upm_utilities.h index cd9d2b88..e20806b4 100644 --- a/src/utilities/upm_utilities.h +++ b/src/utilities/upm_utilities.h @@ -25,17 +25,23 @@ #ifndef UPM_UTILITIES_H_ #define UPM_UTILITIES_H_ +#include + #ifdef __cplusplus extern "C" { #endif -#if defined(linux) +#if defined(UPM_PLATFORM_LINUX) #include #include +#include #include -#endif /* linux */ +#include -#if defined(CONFIG_BOARD_ARDUINO_101) || defined(CONFIG_BOARD_ARDUINO_101_SSS) || defined(CONFIG_BOARD_QUARK_D2000_CRB) +typedef struct timeval upm_clock_t; +#endif /* UPM_PLATFORM_LINUX */ + +#if defined(UPM_PLATFORM_ZEPHYR) #include #include #include @@ -48,8 +54,10 @@ extern "C" { #define PRINT printk #endif -#endif /* CONFIG_BOARD_ARDUINO_101 || CONFIG_BOARD_ARDUINO_101_SSS || - CONFIG_BOARD_QUARK_D2000_CRB */ +typedef uint32_t upm_clock_t; + +#endif /* UPM_PLATFORM_ZEPHYR */ + /* Get filename w/o path */ #define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__) @@ -71,10 +79,38 @@ void upm_delay_ms(int time); /** * Delay for a number of microseconds * - * @param time The number of microsenconds to delay for + * @param time The number of microseconds to delay for */ void upm_delay_us(int time); +/** + * Initialize a clock. This can be used with upm_elapsed_ms() and + * upm_elapsed_us() for measuring a duration. + * + * @param clock The upm_clock_t to initialize to the current time + */ +void upm_clock_init(upm_clock_t *clock); + +/** + * Return the elapsed time in milliseconds since upm_init_clock() was + * last called. + * + * @param clock A upm_clock_t initialized by upm_init_clock() + * @return the number of milliseconds elapsed since upm_init_clock() + * was called on the clock parameter. + */ +uint32_t upm_elapsed_ms(upm_clock_t *clock); + +/** + * Return the elapsed time in microseconds since upm_init_clock() was + * last called. + * + * @param clock A upm_clock_t initialized by upm_init_clock() + * @return the number of microseconds elapsed since upm_init_clock() + * was called on the clock parameter. + */ +uint32_t upm_elapsed_us(upm_clock_t *clock); + #ifdef __cplusplus } #endif