sys_time.c

/*
 *
 * Copyright (C) 2009-2011 The Paparazzi Team
 *
 * This file is part of paparazzi.
 *
 * paparazzi 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 2, or (at your option)
 * any later version.
 *
 * paparazzi 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 paparazzi; see the file COPYING.  If not, write to
 * the Free Software Foundation, 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 */


/**
 * @file mcu_periph/sys_time.c
 * @brief Architecture independent timing functions.
 *
 */


#include "sys_time.h"
//#include "mcu.h"


struct sys_time sys_time;


int sys_time_register_timer(float duration, sys_time_cb cb)
{


  uint32_t start_time = sys_time.nb_tick;
  for (int i = 0; i < SYS_TIME_NB_TIMER; i++) {
    if (!sys_time.timer[i].in_use) {
      sys_time.timer[i].cb         = cb;
      sys_time.timer[i].elapsed    = FALSE;
      sys_time.timer[i].end_time   = start_time + sys_time_ticks_of_sec(duration);
      sys_time.timer[i].duration   = sys_time_ticks_of_sec(duration);
      sys_time.timer[i].in_use     = TRUE;
      return i;
    }
  }
  return -1;
}




void sys_time_cancel_timer(tid_t id)
{
  sys_time.timer[id].in_use     = FALSE;
  sys_time.timer[id].cb         = NULL;
  sys_time.timer[id].elapsed    = FALSE;
  sys_time.timer[id].end_time   = 0;
  sys_time.timer[id].duration   = 0;
}


// FIXME: race condition ??
void sys_time_update_timer(tid_t id, float duration)
{
  mcu_int_disable();
  sys_time.timer[id].end_time -= (sys_time.timer[id].duration - sys_time_ticks_of_sec(duration));
  sys_time.timer[id].duration = sys_time_ticks_of_sec(duration);
  mcu_int_enable();
}


void sys_time_init(void)
{
  sys_time.nb_sec     = 0;
  sys_time.nb_sec_rem = 0;
  sys_time.nb_tick    = 0;


  sys_time.ticks_per_sec = SYS_TIME_FREQUENCY;       //#define SYS_TIME_FREQUENCY 2000         //系统“心跳”
  sys_time.resolution = 1.0 / sys_time.ticks_per_sec;


  for (unsigned int i = 0; i < SYS_TIME_NB_TIMER; i++) {
    sys_time.timer[i].in_use     = FALSE;
    sys_time.timer[i].cb         = NULL;
    sys_time.timer[i].elapsed    = FALSE;
    sys_time.timer[i].end_time   = 0;
    sys_time.timer[i].duration   = 0;
  }


  sys_time_arch_init();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值