Niobe开发板OpenHarmony内核编程开发——定时器

本示例将演示如何在Niobe Wifi IoT开发板上使用cmsis 2.0 接口进行定时器开发

Timer API分析

osTimerNew()

c 复制代码
    /// Create and Initialize a timer.
    /// \param[in]     func          function pointer to callback function.
    /// \param[in]     type          \ref osTimerOnce for one-shot or \ref osTimerPeriodic for periodic behavior.
    /// \param[in]     argument      argument to the timer callback function.
    /// \param[in]     attr          timer attributes; NULL: default values.
    /// \return timer ID for reference by other functions or NULL in case of error.
   osTimerId_t osTimerNew	(osTimerFunc_t func,osTimerType_t type,void *argument,const osTimerAttr_t *attr)

描述:

函数osTimerNew创建一个一次性或周期性计时器,并将其与一个带参数的回调函数相关联。计时器在osTimerStart启动之前一直处于停止状态。可以在RTOS启动(调用 osKernelStart)之前安全地调用该函数,但不能在内核初始化 (调用 osKernelInitialize)之前调用该函数。

注意 :不能在中断服务调用该函数
参数:

|名字|描述|

|:--|:------|

| func | 函数指针指向回调函数. |

| type | 定时器类型,osTimerOnce表示单次定时器,ostimer周期表示周期性定时器. |

| argument |定时器回调函数的参数|

| attr |计时器属性|

osTimerStart()

c 复制代码
  /// Start or restart a timer.
  /// \param[in]     timer_id      timer ID obtained by \ref osTimerNew.
  /// \param[in]     ticks         \ref CMSIS_RTOS_TimeOutValue "time ticks" value of the timer.
  /// \return status code that indicates the execution status of the function.
  osStatus_t osTimerStart(osTimerId_t timer_id, uint32_t ticks);

描述:

函数osTimerStart启动或重新启动指定参数timer_id的计时器。参数ticks指定计时器的计数值。

注意 :不能在中断服务调用该函数

参数:

名字 描述
timer_id 由osTimerNew获得的计时器ID.
ticks 时间滴答计时器的值.

软件设计

软件设计

主要代码分析

在OS_Timer_example函数中,通过osTimerNew()函数创建了回调函数为OS_Timer1_Callback的定时器1,并通过osTimerStart()函数将该定时器设置为100个tick,因为hi3861默认10ms为一个tick,所以100个tick正好为1S钟,1S计时到后会触发OS_Timer1_Callback()函数并打印日志。定时器2也同理为3S触发OS_Timer2_Callback()函数并打印日志.

c 复制代码
  static void OS_Timer_example(void)
{
    osTimerId_t timerId1, timerId2;
    uint32_t delay;
    osStatus_t status;

    timer1Exec = 1U;
    /// Create and Initialize a timer.
    /// \param[in]     func          function pointer to callback function.
    /// \param[in]     type          \ref osTimerOnce for one-shot or \ref osTimerPeriodic for periodic behavior.
    /// \param[in]     argument      argument to the timer callback function.
    /// \param[in]     attr          timer attributes; NULL: default values.
    /// \return timer ID for reference by other functions or NULL in case of error.
    timerId1 = osTimerNew((osTimerFunc_t)OS_Timer1_Callback, osTimerPeriodic, &timer1Exec, NULL);
    if (timerId1 != NULL)
    {
        delay = 100U;
        /// Start or restart a timer.
        /// \param[in]     timer_id      timer ID obtained by \ref osTimerNew.
        /// \param[in]     ticks         \ref CMSIS_RTOS_TimeOutValue "time ticks" value of the timer.
        /// \return status code that indicates the execution status of the function.
        //osStatus_t osTimerStart(osTimerId_t timer_id, uint32_t ticks);
        status = osTimerStart(timerId1, delay);
        if (status != osOK)
        {
            printf("Falied to start timer1!\n");
        }
    }

    timer2Exec = 100U;
    timerId2 = osTimerNew((osTimerFunc_t)OS_Timer2_Callback, osTimerPeriodic, &timer2Exec, NULL);
    if (timerId2 != NULL)
    {
        delay = 300U;
        status = osTimerStart(timerId2, delay);
        if (status != osOK)
        {
            printf("Falied to start timer2!\n");
        }
    }
}

编译调试

修改 BUILD.gn 文件

修改 applications\app路径下 BUILD.gn 文件,指定 os_timer_example 参与编译。

r 复制代码
#"TW001_OS_helloworld:helloworld_example",
#"TW002_OS_thread:os_thread_example",
"TW003_OS_timer:os_timer_example",
#"TW004_OS_event:os_event_example",
#"TW005_OS_mutex:os_mutex_example",
#"TW006_OS_semp:os_semaphore_example",
#"TW007_OS_message:os_message_example",

运行结果

示例代码编译烧录代码后,按下开发板的RESET按键,通过串口助手查看日志,OS_Timer1_Callback会1S打印一次数据,OS_Timer2_Callback会3S打印一次数据。

复制代码
This is Niebo Timer1_Callback:1!
This is Niebo Timer1_Callback:1!
This is Niebo Timer1_Callback:1!
This is Niebo Timer2_Callback:100!
This is Niebo Timer1_Callback:1!
This is Niebo Timer1_Callback:1!
This is Niebo Timer1_Callback:1!
This is Niebo Timer2_Callback:100!

为了能让大家更好的学习鸿蒙(HarmonyOS NEXT)开发技术,这边特意整理了《鸿蒙开发学习手册》(共计890页),希望对大家有所帮助:https://qr21.cn/FV7h05

《鸿蒙开发学习手册》:

如何快速入门:https://qr21.cn/FV7h05

  1. 基本概念
  2. 构建第一个ArkTS应用
  3. ......

开发基础知识:https://qr21.cn/FV7h05

  1. 应用基础知识
  2. 配置文件
  3. 应用数据管理
  4. 应用安全管理
  5. 应用隐私保护
  6. 三方应用调用管控机制
  7. 资源分类与访问
  8. 学习ArkTS语言
  9. ......

基于ArkTS 开发:https://qr21.cn/FV7h05

  1. Ability开发
  2. UI开发
  3. 公共事件与通知
  4. 窗口管理
  5. 媒体
  6. 安全
  7. 网络与链接
  8. 电话服务
  9. 数据管理
  10. 后台任务(Background Task)管理
  11. 设备管理
  12. 设备使用信息统计
  13. DFX
  14. 国际化开发
  15. 折叠屏系列
  16. ......

鸿蒙开发面试真题(含参考答案):https://qr18.cn/F781PH

鸿蒙开发面试大盘集篇(共计319页):https://qr18.cn/F781PH

1.项目开发必备面试题

2.性能优化方向

3.架构方向

4.鸿蒙开发系统底层方向

5.鸿蒙音视频开发方向

6.鸿蒙车载开发方向

7.鸿蒙南向开发方向

相关推荐
ifeng09181 小时前
HarmonyOS实战项目:打造智能家居控制中心(设备发现与控制)
wpf·智能家居·harmonyos
ifeng09184 小时前
HarmonyOS分布式数据管理——跨设备数据同步实战
harmonyos
ifeng09184 小时前
HarmonyOS实战项目:开发一个分布式新闻阅读客户端
分布式·wpf·harmonyos
小范馆5 小时前
通过 useEventBus 和 useEventCallBack 实现与原生 Android、鸿蒙、iOS 的事件交互
android·ios·harmonyos
爱笑的眼睛116 小时前
HarmonyOS Text组件样式定制深度解析:从基础到高级实践
华为·harmonyos
ChinaDragon6 小时前
HarmonyOS:弹出框层级管理
harmonyos
爱笑的眼睛117 小时前
鸿蒙应用开发:华为静默登录解决方案
华为·harmonyos
用户498888174377 小时前
ArkTS 语言基础 第九节:接口与抽象
harmonyos
纯爱掌门人7 小时前
鸿蒙状态管理V2实战:从零构建MVVM架构的应用
前端·harmonyos
白鹿第一帅8 小时前
【案例实战】鸿蒙元服务开发实战:从云原生到移动端,包大小压缩 96% 启动提速 75% 的轻量化设计
harmonyos·白鹿第一帅·鸿蒙元服务·csdn成都站·鸿蒙开放能力·鸿蒙学习之路·鸿蒙元服务框架