Files
test/interface_btm.c
2025-07-05 20:11:15 +08:00

100 lines
2.7 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "regs_base.h"
#include "irq_num.h"
#include "sdrv_btm.h"
#include "interface_config.h"
//app包含
#include "app/app_config.h"
static sdrv_btm_t gstsdrv_btm_timer0;
static sdrv_btm_t gstsdrv_btm_timer1;
volatile uint8_t Btm10ms = 0;
#define DEVICE_BASE(dev) _DEVICE_BASE(dev)
#define _DEVICE_BASE(dev) APB_##dev##_BASE
#define DEVICE_INTR(dev) _DEVICE_INTR(dev)
#define _DEVICE_INTR(dev) dev##_INTR_NUM
/*SDRV_BTM_G0时钟来源于APB clock, APB clock等于150MHz*/
/*SDRV_BTM_G0分频之后clock等于1M*/
static sdrv_btm_cfg_t timer0_config = {
.base = DEVICE_BASE(BTM2),
.irq = DEVICE_INTR(BTM2_O_BTM),
.tmr_id = SDRV_BTM_G0,
.tmr_cfg = {
.si_val = 74, /*输入时钟每(si_val + 1)cycle, G0计数器增加inc_val个*/
.inc_val = 1,
.frc_rld_rst_cnt_en = true, /*默认是true表示初始化时是否对counter清零。*/
.term_use_mode = SDRV_BTM_DIRECT, /*设置overflow值立即生效而不是等到下一周期生效。*/
.cmp_use_mode = SDRV_BTM_DIRECT, /*设置compare值立即生效而不是等到下一周期生效。*/
.cnt_dir = SDRV_BTM_CNT_UP, /*设置计数器的值是向上增长*/
.cnt_mode = SDRV_BTM_CONTINOUS_MODE, /*设置计数器是连续模式*/
}
};
/*SDRV_BTM_G1时钟来源于外部晶振等于24MHz*/
/*SDRV_BTM_G1分频之后clock等于1M*/
static sdrv_btm_cfg_t timer1_config = {
.base = DEVICE_BASE(BTM2),
.irq = DEVICE_INTR(BTM2_O_BTM),
.tmr_id = SDRV_BTM_G1,
.tmr_cfg = {
.si_val = 23,
.inc_val = 1,
.frc_rld_rst_cnt_en = true,
.term_use_mode = SDRV_BTM_DIRECT,
.cmp_use_mode = SDRV_BTM_DIRECT,
.cnt_dir = SDRV_BTM_CNT_UP,
.cnt_mode = SDRV_BTM_CONTINOUS_MODE,
}
};
//回调处理10ms定时标志
static void timer1_handle(void *arg)
{
static int timer1_count = 0;
timer1_count++;
if(timer1_count >= 100)
{
timer1_count = 0;
Btm10ms = 1;
}
//app
timerUpdateAll(); // 调用定时器更新函数
}
/*启用g1计数器*/
int btm_init(void)
{
sdrv_btm_init(&gstsdrv_btm_timer1, &timer0_config);//定时器1初始化
sdrv_btm_set_callback(&gstsdrv_btm_timer1, timer1_handle, &gstsdrv_btm_timer1);//设定回调函数
sdrv_btm_start(&gstsdrv_btm_timer1, BTM_TYPE_PERIOD, BTM_TIME_US, 1000);//设定1000us定时
sdrv_btm_init(&gstsdrv_btm_timer0, &timer1_config);//定时器0初始化
sdrv_btm_start(&gstsdrv_btm_timer0, BTM_TYPE_PERIOD, BTM_TIME_MS, ~0);//设定1ms定时
return 0;
}
//获取当前计数器时间,lwip用
uint32_t current_time(void)
{
return sdrv_btm_get_current_time(&gstsdrv_btm_timer0) / 1000;
}
//获取当前计数器时间us
uint32_t getCurrentTime(void)
{
return sdrv_btm_get_current_time(&gstsdrv_btm_timer0);
}