sys_time_arch

/**
 * @file arch/stm32/mcu_periph/sys_time_arch.h
 * @ingroup stm32_arch
 *
 * STM32 timing functions.
 *
 */


#ifndef SYS_TIME_ARCH_H
#define SYS_TIME_ARCH_H


#include "sys_time.h"


#include <libopencm3/stm32/gpio.h>
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/cm3/systick.h>
#include "std.h"


#define AHB_CLK 72000000
/**
 * Get the time in microseconds since startup.
 * WARNING: overflows after 70min!
 * @return microseconds since startup as uint32_t
 */
static inline uint32_t get_sys_time_usec(void)
{


  return sys_time.nb_sec * 1000000 +
         usec_of_cpu_ticks(sys_time.nb_sec_rem) +
         usec_of_cpu_ticks(systick_get_reload() - systick_get_value());
}


/**
 * Get the time in milliseconds since startup.
 * @return milliseconds since startup as uint32_t
 */
static inline uint32_t get_sys_time_msec(void)
{
  return sys_time.nb_sec * 1000 +
         msec_of_cpu_ticks(sys_time.nb_sec_rem) +
         msec_of_cpu_ticks(systick_get_reload() - systick_get_value());
}




/** Busy wait in microseconds.
 *
 *  max value is limited by the max number of cycle
 *  i.e 2^32 * usec_of_cpu_ticks(systick_get_reload())
 */
static inline void sys_time_usleep(uint32_t us)
{
  // start time
  uint32_t start = systick_get_value();
  // max time of one full counter cycle (n + 1 ticks)
  uint32_t DT = usec_of_cpu_ticks(systick_get_reload() + 1);
  // number of cycles
  uint32_t n = us / DT;
  // remaining number of cpu ticks
  uint32_t rem = cpu_ticks_of_usec(us % DT);
  // end time depend on the current value of the counter
  uint32_t end;
  if (rem < start) {
    end = start - rem;
  } else {
    // one more count flag is required
    n++;
    end = systick_get_reload() - rem + start;
  }
  // count number of cycles (when counter reachs 0)
  while (n) {
    while (!systick_get_countflag());
    n--;
  }
  // wait remaining ticks
  while (systick_get_value() > end);
}


#endif /* SYS_TIME_ARCH_H */
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值