utilities: add new platform checks, implement new clock functions

The new clock functions provide a mechanism for measuring durations in
the ms (millisecond) or us (microsecond) range, in a platform
independant manner.

Signed-off-by: Jon Trulson <jtrulson@ics.com>
This commit is contained in:
Jon Trulson
2016-11-02 16:58:10 -06:00
parent f2ad2c5679
commit 8f6442e9c7
2 changed files with 149 additions and 12 deletions

View File

@ -25,17 +25,23 @@
#ifndef UPM_UTILITIES_H_
#define UPM_UTILITIES_H_
#include <upm_platform.h>
#ifdef __cplusplus
extern "C" {
#endif
#if defined(linux)
#if defined(UPM_PLATFORM_LINUX)
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#endif /* linux */
#include <sys/time.h>
#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 <zephyr.h>
#include <device.h>
#include <sys_clock.h>
@ -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