Hi3861 OpenHarmony嵌入式应用入门--LiteOS Timer

news2024/9/8 23:04:26

LiteOS Timer(定时器)是LiteOS操作系统中的一个重要组件,它提供了一种基于软件模拟的定时器功能,用于满足在硬件定时器数量不足时的定时需求。
软件定时器:基于系统Tick时钟中断,由软件来模拟的定时器。当经过设定的Tick时钟计数值后,会触发用户定义的回调函数。
定时精度:与系统Tick时钟周期有关。
功能:包括静态裁剪、软件定时器创建、启动、停止、删除、剩余Tick数获取等。
资源使用:软件定时器使用了系统的一个队列和一个任务资源。
触发规则:遵循队列规则,先进先出。定时时间短的定时器总是比定时时间长的靠近队列头,满足优先被触发的准则。
基本计时单位:以Tick为基本计时单位。
当用户创建并启动一个软件定时器时,LiteOS会根据当前系统Tick时间及设置的定时时长确定该定时器的到期Tick时间,并将该定时器控制结构挂入计时全局链表。
当Tick中断到来时,在Tick中断处理函数中扫描软件定时器的计时全局链表,检查是否有定时器超时。
若有超时的定时器,则记录下来,并在Tick中断处理函数结束后唤醒软件定时器任务(优先级最高)。
在软件定时器任务中调用之前记录下来的定时器的超时回调函数。
支持的定时器模式
单次触发定时器:启动后只会触发一次定时器事件,然后定时器自动删除。
周期触发定时器:周期性地触发定时器事件,直到用户手动停止定时器。
单次触发但不自动删除定时器:超时触发后不会自动删除,需要调用定时器删除接口删除定时器。

Timer API

API名称

说明

osTimerNew

创建和初始化定时器

osTimerGetName

获取指定的定时器名字

osTimerStart

启动或者重启指定的定时器

osTimerStop

停止指定的定时器

osTimerIsRunning

检查一个定时器是否在运行

osTimerDelete

删除定时器

函数介绍

osTimerId_t osTimerNew (osTimerFunc_t func, osTimerType_t type, void *argument, const osTimerAttr_t *attr)

参数

名字

描述

func

定时器回调函数.

type

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

argument

定时器回调函数的参数

attr

定时器属性

代码编写

修改D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\BUILD.gn文件

# Copyright (c) 2023 Beijing HuaQing YuanJian Education Technology Co., Ltd
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#    http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License. 

import("//build/lite/config/component/lite_component.gni")

lite_component("demo") {
  features = [
    #"base_00_helloworld:base_helloworld_example",
    #"base_01_led:base_led_example",
    #"base_02_loopkey:base_loopkey_example",
    #"base_03_irqkey:base_irqkey_example",
    #"base_04_adc:base_adc_example",
    #"base_05_pwm:base_pwm_example",
    #"base_06_ssd1306:base_ssd1306_example",
    #"kernel_01_task:kernel_task_example",
    "kernel_02_timer:kernel_timer_example",
  ]
}

创建D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\kernel_02_timer文件夹

文件夹中创建D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\kernel_02_timer\kernel_timer_example.c文件D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\kernel_02_timer\BUILD.gn文件

# Copyright (c) 2023 Beijing HuaQing YuanJian Education Technology Co., Ltd
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License. 

static_library("kernel_timer_example") {
    sources = [
        "kernel_timer_example.c",
    ]

    include_dirs = [
        "//utils/native/lite/include",
        "//kernel/liteos_m/kal/cmsis",
        "//base/iot_hardware/peripheral/interfaces/kits",
        "//vendor/hqyj/fs_hi3861/common/bsp/include"
    ]
}
/*
 * Copyright (C) 2023 HiHope Open Source Organization .
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <unistd.h>

#include "ohos_init.h"
#include "cmsis_os2.h"

#define STACK_SIZE      (1024)
#define DELAY_TICKS_100 (100)
#define TEST_TIMES      (3)
static int times = 0;

void cb_timeout_periodic(void)
{
    times++;
}

void timer_periodic(void)
{
    osTimerId_t periodic_tid = osTimerNew(cb_timeout_periodic, osTimerPeriodic, NULL, NULL);
    if (periodic_tid == NULL) {
        printf("[Timer Test] osTimerNew(periodic timer) failed.\r\n");
        return;
    } else {
        printf("[Timer Test] osTimerNew(periodic timer) success, tid: %p.\r\n", periodic_tid);
    }
    osStatus_t status = osTimerStart(periodic_tid, DELAY_TICKS_100);
    if (status != osOK) {
        printf("[Timer Test] osTimerStart(periodic timer) failed.\r\n");
        return;
    } else {
        printf("[Timer Test] osTimerStart(periodic timer) success, wait a while and stop.\r\n");
    }

    while (times < TEST_TIMES) {
        printf("[Timer Test] times:%d.\r\n", times);
        osDelay(DELAY_TICKS_100);
    }

    status = osTimerStop(periodic_tid);
    printf("[Timer Test] stop periodic timer, status :%d.\r\n", status);
    status = osTimerDelete(periodic_tid);
    printf("[Timer Test] kill periodic timer, status :%d.\r\n", status);
}

static void TimerTestTask(void)
{
    osThreadAttr_t attr;

    attr.name = "timer_periodic";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = STACK_SIZE;
    attr.priority = osPriorityNormal;

    if (osThreadNew((osThreadFunc_t)timer_periodic, NULL, &attr) == NULL) {
        printf("[TimerTestTask] Falied to create timer_periodic!\n");
    }
}
APP_FEATURE_INIT(TimerTestTask);

代码分析

定时器的回调函数

void cb_timeout_periodic(void *arg) {
    (void)arg;
    times++;
}

使用osTimerNew创建一个100个时钟周期调用一次回调函数cb_timeout_periodic定时器,每隔100个时钟周期检查一下全局变量times是否小于3,若不小于3则停止时钟周期

void timer_periodic(void)
{
    // 创建一个周期性定时器
    osTimerId_t periodic_tid = osTimerNew(cb_timeout_periodic, osTimerPeriodic, NULL, NULL);
    // 如果创建失败,打印错误信息
    if (periodic_tid == NULL) {
        printf("[Timer Test] osTimerNew(periodic timer) failed.\r\n");
        return;
    // 如果创建成功,打印成功信息
    } else {
        printf("[Timer Test] osTimerNew(periodic timer) success, tid: %p.\r\n", periodic_tid);
    }
    // 启动周期性定时器,延时100个tick
    osStatus_t status = osTimerStart(periodic_tid, DELAY_TICKS_100);
    // 如果启动失败,打印错误信息
    if (status != osOK) {
        printf("[Timer Test] osTimerStart(periodic timer) failed.\r\n");
        return;
    // 如果启动成功,打印成功信息
    } else {
        printf("[Timer Test] osTimerStart(periodic timer) success, wait a while and stop.\r\n");
    }

    // 循环等待测试次数达到
    while (times < TEST_TIMES) {
        // 打印测试次数
        printf("[Timer Test] times:%d.\r\n", times);
        // 延时100个tick
        osDelay(DELAY_TICKS_100);
    }

    // 停止周期性定时器
    status = osTimerStop(periodic_tid);
    // 打印停止状态
    printf("[Timer Test] stop periodic timer, status :%d.\r\n", status);
    // 删除周期性定时器
    status = osTimerDelete(periodic_tid);
    // 打印删除状态
    printf("[Timer Test] kill periodic timer, status :%d.\r\n", status);
}

使用build,编译成功后,使用upload进行烧录。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1860224.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

表单(forms)

自学python如何成为大佬(目录):https://blog.csdn.net/weixin_67859959/article/details/139049996?spm1001.2014.3001.5501 在app1文件夹下创建一个forms.py文件&#xff0c;添加如下类代码&#xff1a; from django import forms class PersonForm(forms.Form): first_na…

GPT-5:AI新纪元的领航者,多维度的审视与准备

一、引言&#xff1a;GPT-5与AI的多维演进 GPT-5作为AI领域的里程碑式突破&#xff0c;不仅仅代表了技术的飞跃&#xff0c;更预示着社会、文化以及经济等多个层面的深刻变革。从技术的角度看&#xff0c;GPT-5代表着AI在自然语言处理领域的最新高度&#xff1b;而从更宽广的视…

中国高分辨率土壤侵蚀因子K

土壤可蚀性因子&#xff08;K&#xff09;数据&#xff0c;基于多种土壤属性数据计算&#xff0c;所用数据包括土壤黏粒含量&#xff08;%&#xff09;、粉粒含量&#xff08;%&#xff09;、砂粒含量&#xff08;%&#xff09;、土壤有机碳含量&#xff08;g/kg&#xff09;、…

我国季戊四醇市场规模逐渐扩大 出口量有所增长

我国季戊四醇市场规模逐渐扩大 出口量有所增长 季戊四醇&#xff08;PETP/THME&#xff09;又称为四羟甲基甲烷、2,2-双羟甲基-1,3-丙二醇等&#xff0c;是一种多元醇类有机化合物&#xff0c;多表现为一种白色结晶性粉末。季戊四醇可溶于水及乙醇等溶剂&#xff0c;但不溶于苯…

台式扫描电镜工作距离越远观察区越大?

台式扫描电镜&#xff08;Scanning Electron Microscope, SEM&#xff09;是一种高分辨率的显微镜&#xff0c;它利用电子束扫描样品表面&#xff0c;通过样品与电子束相互作用产生的信号来形成图像。这种显微镜广泛应用于材料科学、生物学和医学等领域&#xff0c;以观察样品的…

AI元宇宙

随着科技的迅猛发展&#xff0c;人工智能&#xff08;AI&#xff09;迎来了一个宇宙大爆发的时代。特别是以GPT为代表的生成式大模型的诞生和不断进步&#xff0c;彻底改变了人们的工作和生活方式。程序员与AI协同工作写代码已成为常态&#xff0c;大模型不仅提高了工作效率&am…

4418 HMI 更换logo 图片

逻辑说明&#xff1a; HMI 的 kernel 没有提供源码&#xff0c;只是提供了镜像&#xff0c;如果客户需要更换自己的logo 的话&#xff0c; 可以使用提供的工具&#xff0c;将内核logo 打包起来。 我觉得这里的打包的过程应该是参考了&#xff0c; 4418 build_android.sh 脚…

linux学习week1

linux学习 一.介绍 1.概述 linux的读法不下10种 linux是一个开源的操作系统&#xff0c;操作系统包括mac、windows、安卓等 linux的开发版&#xff1a;Ubuntu&#xff08;乌班图&#xff09;、RedHat&#xff08;红帽&#xff09;、CentOS linux的应用&#xff1a;linux在服…

百问网全志D1h开发板MIPI屏幕触摸功能适配

硬件了解 首先&#xff0c;还是从官方提供的资料&#xff0c;可以了解MIPI LCD对应的接口信息&#xff1a; [ 触摸功能涉及到DSI_SCL、DSI_SDA、TP_INT、TP_RESET。 从芯片的引脚图里面&#xff0c;可以了解到&#xff1a; [ 其中&#xff1a; DSI_SCL、DSI_SDA使用的是…

1954springboot VUE 天然气系统隐患管理系统开发mysql数据库web结构java编程计算机网页源码maven项目

一、源码特点 springboot VUE天然气系统隐患管理系统是一套完善的完整信息管理类型系统&#xff0c;结合springboot框架和VUE完成本系统&#xff0c;对理解JSP java编程开发语言有帮助系统采用springboot框架&#xff08;MVC 模式开发&#xff09;&#xff0c;系统具有完整的…

JNI详解

JNI简介 Java是跨平台的语言,但在有的时候仍需要调用本地代码(这些代码通常由C/C++编写的)。 Sun公司提供的JNI是Java平台的一个功能强大的接口,JNI接口提供了Java与操作系统本地代码互相调用的功能。 Java调C++ 1)使用javah命令生成native的头文件 javah com.studio.j…

优化|PyOptInterface:高效且灵活的Python优化建模语言

优化建模语言作为优化求解器与终端用户之间的桥梁&#xff0c;是构建、求解和分析优化模型的重要工具。建模语言的效率直接影响优化模型的构建和求解时间。PyOptInterface是一种基于Python编程语言的优化建模语言&#xff0c;相比现有建模语言兼具高效率和灵活性&#xff0c;在…

Kotlin设计模式:深入理解桥接模式

Kotlin设计模式&#xff1a;深入理解桥接模式 在软件开发中&#xff0c;随着系统需求的不断增长和变化&#xff0c;类的职责可能会变得越来越复杂&#xff0c;导致代码难以维护和扩展。桥接模式&#xff08;Bridge Pattern&#xff09;是一种结构型设计模式&#xff0c;它通过…

【MySQL】如果表被锁可以尝试看一下事务

今天在MySQL中删除表的时候&#xff0c;发现无法删除&#xff0c;一执行drop&#xff0c;navicat就卡死。 通过 SHOW PROCESSLIST显示被锁了 kill掉被锁的进程后依旧被锁 最后发现是由于存在为执行完的事务 SELECT * FROM INFORMATION_SCHEMA.INNODB_TRX; kill掉这些事务以…

九泰智库 | 医械周刊- Vol.36

⚖️ 法规动态 广东药监局 | 8家医疗器械公司体系不合规被停产 6月17日&#xff0c;广东省药品监督管理局组织开展医疗器械生产企业监督检查&#xff0c;发现8家企业质量管理体系存在严重缺陷&#xff0c;不符合《医疗器械生产质量管理规范》相关规定&#xff0c;广东省药品监督…

美食解压视频素材无水印无字幕的在哪找?海外美食解压网站分享

在如今快节奏的生活中&#xff0c;观看美食视频已成为许多人缓解压力的一种方式。这些视频不仅唤醒人们的味觉记忆&#xff0c;还能在繁忙中带来片刻的放松。然而&#xff0c;对于视频创作者来说&#xff0c;寻找高品质的美食视频素材&#xff0c;特别是那些无水印、无字幕、可…

HALCON-从入门到入门-提取小票上的斑点

测试效果 在一张超市小票上提取点阵数字 处理步骤解析 首先读取两张图&#xff0c;一张是小票的图片&#xff0c;一张是静脉的图片 为了让点阵数字提取更加困难&#xff0c;我们将两张图片合成到一起 read_image (ImageNoise, angio-part) crop_part (ImageNoise, ImagePart…

面试-java多线程与并发

1.如何实现处理线程的返回值 (1)主线程等待法 主线程等待法&#xff1a;程序执行时&#xff0c;没有等到value值赋予完成&#xff0c;就直接在主函数 中执行打印value的值。 缺点&#xff1a;需要自己去实现循环等待的逻辑。若需要等待的变量变多&#xff0c;需要等待的时间可能…

Python22 Pandas库

Pandas 是一个Python数据分析库&#xff0c;它提供了高性能、易于使用的数据结构和数据分析工具。这个库适用于处理和分析输入数据&#xff0c;常见于统计分析、金融分析、社会科学研究等领域。 1.Pandas的核心功能 Pandas 库的核心功能包括&#xff1a; 1.数据结构&#xff…

python flask使用flask_migrate管理数据库迁移

&#x1f308;所属专栏&#xff1a;【Flask】✨作者主页&#xff1a; Mr.Zwq✔️个人简介&#xff1a;一个正在努力学技术的Python领域创作者&#xff0c;擅长爬虫&#xff0c;逆向&#xff0c;全栈方向&#xff0c;专注基础和实战分享&#xff0c;欢迎咨询&#xff01; 您的点…