TIMER
更新时间:2019-01-25 14:41:42
接口列表
接口详情
int32_t hal_timer_init(timer_dev_t *tim)
描述 |
初始化指定TIMER |
参数 |
tim:TIMER设备描述,定义需要初始化的TIMER参数 |
返回值 |
返回成功或失败, 返回0表示TIMER初始化成功,非0表示失败 |
int32_t hal_timer_start(timer_dev_t *tim)
描述 |
启动指定的TIMER |
参数 |
tim:TIMER设备描述 |
返回值 |
返回成功或失败, 返回0表示TIMER启动成功,非0表示失败 |
void hal_timer_stop(timer_dev_t *tim)
描述 |
停止指定的TIMER |
参数 |
tim:TIMER设备描述 |
返回值 |
返回成功或失败, 返回0表示TIMER停止成功,非0表示失败 |
描述 |
改变指定TIMER的参数 |
参数 |
tim:TIMER设备描述 |
|
para:TIMER配置信息 |
返回值 |
返回成功或失败, 返回0表示TIMER参数改变成功,非0表示失败 |
int32_t hal_timer_finalize(timer_dev_t *tim)
描述 |
关闭指定TIMER |
参数 |
tim:TIMER设备描述 |
返回值 |
返回成功或失败, 返回0表示TIMER关闭成功,非0表示失败 |
相关宏定义
#define TIMER_RELOAD_AUTO 1
#define TIMER_RELOAD_MANU 2
相关结数据结构
timer_dev_t
typedef struct {
int8_t port;
timer_config_t config;
void *priv;
} timer_dev_t;
timer_config_t
typedef struct {
uint32_t period;
uint8_t reload_mode;
hal_timer_cb_t cb;
void *arg;
} timer_config_t;
hal_timer_cb_t
typedef void (*hal_timer_cb_t)(void *arg);
使用示例
#include <aos/hal/timer.h>
#define TIMER1_PORT_NUM 1
timer_dev_t timer1;
void timer_handler(void *arg)
{
static int timer_cnt = 0;
printf("timer_handler: %d times !\n", timer_cnt++);
}
int application_start(int argc, char *argv[])
{
int ret = -1;
timer_config_t timer_cfg;
static int count = 0;
timer1.port = TIMER1_PORT_NUM;
timer1.config.period = 1000000;
timer1.config.reload_mode = TIMER_RELOAD_AUTO;
timer1.config.cb = timer_handler;
ret = hal_timer_init(&timer1);
if (ret != 0) {
printf("timer1 init error !\n");
}
ret = hal_timer_start(&timer1);
if (ret != 0) {
printf("timer1 start error !\n");
}
while(1) {
if (count == 5) {
memset(&timer_cfg, 0, sizeof(timer_config_t));
timer_cfg.period = 2000000;
ret = hal_timer_para_chg(&timer1, timer_cfg);
if (ret != 0) {
printf("timer1 para change error !\n");
}
}
if (count == 20) {
hal_timer_stop(&timer1);
hal_timer_finalize(&timer1);
}
aos_msleep(1000);
count++;
};
}
注:port为逻辑端口号,其与物理端口号的对应关系见具体的对接实现
移植说明
新建hal_timer_xxmcu.c和hal_timer_xxmcu.h的文件,并将这两个文件放到platform/mcu/xxmcu/hal目录下。在hal_timer_xxmcu.c中实现所需要的hal函数,hal_timer_xxmcu.h中放置相关宏定义。<br /> 参考platform/mcu/stm32l4xx/src/STM32L496G-Discovery/hal/hal_timer_stm32l4.c