static uint64_t timer_base = 0;
uint64_t hrt_micros()
{
static volatile uint64_t last_micros;
// 使用typedef为现有类型创建别名,定义易于记忆的类型名
typedef uint32_t syssts_t; /**< System status word. */
/*
use chSysGetStatusAndLockX() to prevent an interrupt while allowing this call from any context
*/
//使用chSysGetStatusAndLockX()来防止中断,同时允许来自任何上下文的调用
syssts_t sts = chSysGetStatusAndLockX();
uint64_t micros;
micros = timer_base + (uint64_t)chVTGetSystemTimeX();
// we are doing this to avoid an additional interupt routing
// since we are definitely going to get called atleast once in
// a full timer period
if (last_micros > micros) {
const uint64_t step = ST2US(1ULL<<CH_CFG_ST_RESOLUTION);
timer_base += step;
micros += step;
}
last_micros = micros;
chSysRestoreStatusX(sts);
return micros;
}