2018-01-05 02:19:51 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) Siddharth Bharat Purohit 2017
|
|
|
|
* This file is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This file is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
// High Resolution Timer
|
|
|
|
|
|
|
|
#include "ch.h"
|
|
|
|
#include "hal.h"
|
|
|
|
#include "hrt.h"
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2018-08-26 02:22:42 -03:00
|
|
|
static uint64_t timer_base_us64;
|
|
|
|
#if CH_CFG_ST_RESOLUTION == 16
|
|
|
|
static uint32_t timer_base_us32;
|
|
|
|
#endif
|
2018-08-08 03:57:05 -03:00
|
|
|
static uint32_t timer_base_ms;
|
|
|
|
static volatile systime_t last_systime;
|
2018-01-05 02:19:51 -04:00
|
|
|
|
2018-08-26 02:22:42 -03:00
|
|
|
#if CH_CFG_ST_RESOLUTION == 16
|
2018-08-08 03:57:05 -03:00
|
|
|
static uint32_t get_systime_us32(void)
|
2018-01-05 02:19:51 -04:00
|
|
|
{
|
2018-08-08 03:57:05 -03:00
|
|
|
systime_t now = chVTGetSystemTimeX();
|
2018-10-04 00:03:57 -03:00
|
|
|
#if CH_CFG_ST_FREQUENCY != 1000000
|
|
|
|
now *= (1000000UL / CH_CFG_ST_FREQUENCY);
|
|
|
|
#endif
|
2018-08-08 03:57:05 -03:00
|
|
|
if (now < last_systime) {
|
2018-08-26 02:22:42 -03:00
|
|
|
uint32_t last_u32 = timer_base_us32;
|
|
|
|
timer_base_us32 += (uint32_t)TIME_MAX_SYSTIME;
|
|
|
|
if (timer_base_us32 < last_u32) {
|
|
|
|
timer_base_us64 += ((uint32_t)-1);
|
|
|
|
timer_base_ms += ((uint32_t)-1)/1000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
last_systime = now;
|
|
|
|
return timer_base_us32 + (uint32_t)now;
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif CH_CFG_ST_RESOLUTION == 32
|
|
|
|
static uint32_t get_systime_us32(void)
|
|
|
|
{
|
|
|
|
systime_t now = chVTGetSystemTimeX();
|
2018-10-04 00:03:57 -03:00
|
|
|
#if CH_CFG_ST_FREQUENCY != 1000000
|
|
|
|
now *= (1000000UL / CH_CFG_ST_FREQUENCY);
|
|
|
|
#endif
|
2018-08-26 02:22:42 -03:00
|
|
|
if (now < last_systime) {
|
|
|
|
timer_base_us64 += TIME_MAX_SYSTIME;
|
2018-08-08 03:57:05 -03:00
|
|
|
timer_base_ms += TIME_MAX_SYSTIME/1000;
|
|
|
|
}
|
|
|
|
last_systime = now;
|
|
|
|
return now;
|
|
|
|
}
|
2018-08-26 02:22:42 -03:00
|
|
|
#else
|
|
|
|
#error "unsupported timer resolution"
|
|
|
|
#endif
|
2018-08-08 03:57:05 -03:00
|
|
|
|
|
|
|
/*
|
|
|
|
we use chSysGetStatusAndLockX() to prevent an interrupt while
|
|
|
|
allowing this call from any context
|
|
|
|
*/
|
|
|
|
|
|
|
|
uint64_t hrt_micros64()
|
|
|
|
{
|
|
|
|
syssts_t sts = chSysGetStatusAndLockX();
|
|
|
|
uint32_t now = get_systime_us32();
|
2018-08-26 02:22:42 -03:00
|
|
|
uint64_t ret = timer_base_us64 + now;
|
2018-08-08 03:57:05 -03:00
|
|
|
chSysRestoreStatusX(sts);
|
|
|
|
return ret;
|
|
|
|
}
|
2018-01-05 02:19:51 -04:00
|
|
|
|
2018-08-08 03:57:05 -03:00
|
|
|
uint32_t hrt_micros32()
|
|
|
|
{
|
|
|
|
syssts_t sts = chSysGetStatusAndLockX();
|
|
|
|
uint32_t ret = get_systime_us32();
|
|
|
|
chSysRestoreStatusX(sts);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t hrt_millis32()
|
|
|
|
{
|
|
|
|
syssts_t sts = chSysGetStatusAndLockX();
|
|
|
|
uint32_t now = get_systime_us32();
|
|
|
|
uint32_t ret = (now / 1000U) + timer_base_ms;
|
|
|
|
chSysRestoreStatusX(sts);
|
|
|
|
return ret;
|
2018-01-05 02:19:51 -04:00
|
|
|
}
|