当前位置: 首页 > 文档资料 > YoC 编程基础 >

定时器

优质
小牛编辑
114浏览
2023-12-01

概述

tick一般是作为任务延迟调度的内部机制,其接口主要是系统内部使用。对于使用os的应用软件,也需要定时触发相关功能的接口,包括单次定时器和周期定时器。从用户层面来讲,不关注底层cpu的定时机制以及tick的调度,用户希望的定时器接口是,可以创建和使能一个软件接口定时器,时间到了之后,用户的钩子函数能被执行。而对于操作系统的定时器本身来讲,其也需要屏蔽底层定时模块的差异。因此,在软件层次上,对于定时器硬件相关的操作由tick模块完成,定时器(timer)模块基于tick作为最基本的时间调度单元,即最小时间周期,来推动自己时间轴的运行。 YoC提供基本的软件定时器功能,包括定时器的创建、删除、运行,以及单次和周期定时器。

接口定义

动态创建软件定时器

int aos_timer_new(aos_timer_t *timer, void (*fn)(void *, void *), void *arg, int ms, int repeat);

创建一个定时器,并自动运行

  • 参数:

    • timer:timer描述结构体指针;需要用户定义一个timer结构体
    • fn: 定时器处理函数
    • arg:定时器处理参数
    • ms:定时器超时时间(单位ms),即间隔多少时间执行fn
    • repeat:周期或单次(1:周期,0:单次)
  • 返回值:

    • 类型:int 返回成功或失败, 返回0表示timer创建成功,非0表示失败。

动态创建软件定时器

int aos_timer_new_ext(aos_timer_t *timer, void (*fn)(void *, void *),
                      void *arg, int ms, int repeat, unsigned char auto_run);

创建一个定时器,根据auto_run参数选择是否自动运行

  • 参数:

    • timer:timer描述结构体指针;需要用户定义一个timer结构体
    • fn: 定时器处理函数
    • arg:定时器处理参数
    • ms:定时器超时时间(单位ms),即间隔多少时间执行fn
    • repeat:周期或单次(1:周期,0:单次)
    • auto_run:1表示自动运行,0表示不自动运行,需要手动调用aos_timer_start才能启动
  • 返回值:

    • 类型:int 返回成功或失败, 返回0表示timer创建成功,非0表示失败。

软件定时器停止

int aos_timer_stop(aos_timer_t *timer)

停止一个定时器

  • 参数:

    • timer:timer描述结构体指针
  • 返回值:

    • 类型:int 返回成功或失败, 返回0表示timer创建成功,非0表示失败。

删除软件定时器

void aos_timer_free(aos_timer_t *timer)

释放一个定时器

  • 参数:

    • timer:timer描述结构体指针
  • 返回值:

软件定时器启动

int aos_timer_start(aos_timer_t *timer)

启动一个定时器

  • 参数:

    • timer:timer描述结构体指针
  • 返回值:

    • 类型:int 返回成功或失败, 返回0表示timer创建成功,非0表示失败。

改变软件定时器的周期

int aos_timer_change(aos_timer_t *timer, int ms)

修改一个定时器周期

  • 参数:

    • timer:timer描述结构体指针
    • ms:新的定时器周期,单位ms
  • 返回值:

    • 类型:int 返回成功或失败, 返回0表示timer创建成功,非0表示失败。

示例代码

创建自动运行的周期执行定时器

aos_timer_t test_timer;

static void timer_handler(void *arg1, void* arg2)
{
    printf("timer handler\r\n");
}

void test_timer_periodic(void)
{
    int ret = -1;

    printf("test timer periodic\n");

    /*创建定时周期为200ms的周期执行的定时器,并自动运行*/
    ret = aos_timer_new(&test_timer, timer_handler, NULL, 200, 1);
    if (ret != 0) {
        printf("timer create failed\r\n");
    }

    aos_msleep(1000);

    /*停止定时器*/
    aos_timer_stop(&test_timer);

    /*释放定时器*/
    aos_timer_free(&test_timer);
}

创建不自动运行的周期执行定时器,并在使用中改变定时周期。

aos_timer_t test_timer;

static void timer_handler(void *arg1, void* arg2)
{
    printf("timer handler\r\n");
}
void test_timer_change(void)
{
    int ret = -1;

    printf("test timer change time\n");

    /*创建定时周期为200ms的周期执行的定时器,不自动运行*/
    ret = aos_timer_new_ext(&test_timer, timer_handler, NULL, 200, 0, 0);
    if (ret != 0) {
        printf("timer create failed\r\n");
    }

    /*需要手动启动定时器*/
    aos_timer_start(&test_timer);

    aos_msleep(1000);

    /*停止定时器*/
    aos_timer_stop(&test_timer);

    /*改变定时周期为300ms, 注意:需要在定时器未启动状态是才能修改*/
    aos_timer_change(&test_timer, 300);

    /*启动定时器*/
    aos_timer_start(&test_timer);

    /*停止定时器*/
    aos_timer_stop(&test_timer);

    /*释放定时器*/
    aos_timer_free(&test_timer);
}