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

@ -23,12 +23,13 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <upm_platform.h>
#include <upm_utilities.h>
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
}

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