2016-09-22 16:40:01 -06:00
|
|
|
/*
|
|
|
|
|
* Authors:
|
|
|
|
|
* Jon Trulson <jtrulson@ics.com>
|
|
|
|
|
* Copyright (c) 2016 Intel Corporation.
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
|
* the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
*/
|
2016-08-17 17:58:21 -07:00
|
|
|
|
2016-11-02 16:58:10 -06:00
|
|
|
#include <upm_platform.h>
|
2016-09-22 16:40:01 -06:00
|
|
|
#include <upm_utilities.h>
|
2016-08-17 17:58:21 -07:00
|
|
|
|
2016-12-05 15:04:19 -07:00
|
|
|
void upm_delay(int time)
|
|
|
|
|
{
|
|
|
|
|
if (time <= 0)
|
|
|
|
|
time = 1;
|
|
|
|
|
|
2016-11-02 16:58:10 -06:00
|
|
|
#if defined(UPM_PLATFORM_LINUX)
|
2016-12-05 15:04:19 -07:00
|
|
|
|
2016-08-17 17:58:21 -07:00
|
|
|
sleep(time);
|
2016-12-05 13:38:51 -07:00
|
|
|
|
2016-12-05 15:04:19 -07:00
|
|
|
#elif defined(UPM_PLATFORM_ZEPHYR)
|
|
|
|
|
# if KERNEL_VERSION_MAJOR == 1 && KERNEL_VERSION_MINOR >= 6
|
2016-12-02 16:40:48 -07:00
|
|
|
|
|
|
|
|
struct k_timer timer;
|
|
|
|
|
k_timer_init(&timer, NULL, NULL);
|
2016-12-05 13:38:51 -07:00
|
|
|
k_timer_start(&timer, time * 1000, 0);
|
2016-12-02 16:40:48 -07:00
|
|
|
k_timer_status_sync(&timer);
|
|
|
|
|
|
|
|
|
|
# else
|
|
|
|
|
|
2016-08-17 17:58:21 -07:00
|
|
|
struct nano_timer timer;
|
|
|
|
|
void *timer_data[1];
|
|
|
|
|
nano_timer_init(&timer, timer_data);
|
2016-09-30 17:05:15 -06:00
|
|
|
nano_timer_start(&timer, SECONDS(time) + 1);
|
2016-08-17 17:58:21 -07:00
|
|
|
nano_timer_test(&timer, TICKS_UNLIMITED);
|
2016-12-02 16:40:48 -07:00
|
|
|
|
|
|
|
|
# endif
|
|
|
|
|
|
2016-08-17 17:58:21 -07:00
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-05 15:04:19 -07:00
|
|
|
void upm_delay_ms(int time)
|
|
|
|
|
{
|
|
|
|
|
if (time <= 0)
|
|
|
|
|
time = 1;
|
|
|
|
|
|
2016-11-02 16:58:10 -06:00
|
|
|
#if defined(UPM_PLATFORM_LINUX)
|
2016-12-05 15:04:19 -07:00
|
|
|
|
2016-08-17 17:58:21 -07:00
|
|
|
usleep(1000 * time);
|
2016-12-05 13:38:51 -07:00
|
|
|
|
2016-12-05 15:04:19 -07:00
|
|
|
#elif defined(UPM_PLATFORM_ZEPHYR)
|
|
|
|
|
# if KERNEL_VERSION_MAJOR == 1 && KERNEL_VERSION_MINOR >= 6
|
2016-12-02 16:40:48 -07:00
|
|
|
|
|
|
|
|
struct k_timer timer;
|
|
|
|
|
k_timer_init(&timer, NULL, NULL);
|
2016-12-05 13:38:51 -07:00
|
|
|
k_timer_start(&timer, time, 0);
|
2016-12-02 16:40:48 -07:00
|
|
|
k_timer_status_sync(&timer);
|
|
|
|
|
|
|
|
|
|
# else
|
|
|
|
|
|
2016-08-17 17:58:21 -07:00
|
|
|
struct nano_timer timer;
|
|
|
|
|
void *timer_data[1];
|
|
|
|
|
nano_timer_init(&timer, timer_data);
|
2016-09-30 17:05:15 -06:00
|
|
|
nano_timer_start(&timer, MSEC(time) + 1);
|
2016-08-17 17:58:21 -07:00
|
|
|
nano_timer_test(&timer, TICKS_UNLIMITED);
|
2016-12-02 16:40:48 -07:00
|
|
|
|
|
|
|
|
# endif
|
2016-08-17 17:58:21 -07:00
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-05 15:04:19 -07:00
|
|
|
void upm_delay_us(int time)
|
|
|
|
|
{
|
|
|
|
|
if (time <= 0)
|
|
|
|
|
time = 1;
|
|
|
|
|
|
2016-11-02 16:58:10 -06:00
|
|
|
#if defined(UPM_PLATFORM_LINUX)
|
2016-12-05 15:04:19 -07:00
|
|
|
|
2016-08-17 17:58:21 -07:00
|
|
|
usleep(time);
|
2016-12-02 16:40:48 -07:00
|
|
|
|
2016-12-05 15:04:19 -07:00
|
|
|
#elif defined(UPM_PLATFORM_ZEPHYR)
|
|
|
|
|
# if KERNEL_VERSION_MAJOR == 1 && KERNEL_VERSION_MINOR >= 6
|
2016-12-05 13:38:51 -07:00
|
|
|
// we will use a upm_clock to do microsecond timings here as k_timer has
|
|
|
|
|
// only a millisecond resolution. So we init a clock and spin.
|
|
|
|
|
|
|
|
|
|
upm_clock_t timer;
|
|
|
|
|
upm_clock_init(&timer);
|
|
|
|
|
while (upm_elapsed_us(&timer) < time)
|
|
|
|
|
; // spin
|
2016-12-02 16:40:48 -07:00
|
|
|
|
|
|
|
|
# else
|
|
|
|
|
|
2016-08-17 17:58:21 -07:00
|
|
|
struct nano_timer timer;
|
|
|
|
|
void *timer_data[1];
|
|
|
|
|
nano_timer_init(&timer, timer_data);
|
2016-09-30 17:05:15 -06:00
|
|
|
nano_timer_start(&timer, USEC(time) + 1);
|
2016-08-17 17:58:21 -07:00
|
|
|
nano_timer_test(&timer, TICKS_UNLIMITED);
|
2016-12-02 16:40:48 -07:00
|
|
|
|
|
|
|
|
# endif
|
|
|
|
|
|
2016-08-17 17:58:21 -07:00
|
|
|
#endif
|
|
|
|
|
}
|
2016-11-02 16:58:10 -06:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|