HarmonyOS小熊派 | HarmonyOS内核编程开发—定时器

举报
Yuchuan 发表于 2021/06/25 23:49:06 2021/06/25
【摘要】 BearPi-HM_Nano开发板HarmonyOS内核编程开发——定时器本示例将演示如何在BearPi-HM_Nano开发板上使用cmsis 2.0 接口进行定时器开发Timer API分析osTimerNew()osTimerId_t osTimerNew (osTimerFunc_t func,osTimerType_t type,void *argument,const osTime...

BearPi-HM_Nano开发板HarmonyOS内核编程开发——定时器

本示例将演示如何在BearPi-HM_Nano开发板上使用cmsis 2.0 接口进行定时器开发

BearPi-HM_Nano

Timer API分析

osTimerNew()

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()

osStatus_t osTimerStart	(osTimerId_t timer_id,uint32_t ticks)

描述:

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

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

参数:

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

软件设计

软件设计

主要代码分析

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

timer_yuchuan_example.c

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

#include "ohos_init.h"
#include "cmsis_os2.h"
#include "iot_gpio.h"
#include "iot_gpio_ex.h"

#define GPIO_LED 2

uint32_t exec1,exec2;

void yuchuanTimer(void)
{
    printf("This is Yuchuan Create Timer!!!\n");
    
}

void huayingTimer(void)
{
    printf("This is Huaying Create Timer!!!\n");
    IoTGpioSetOutputVal(GPIO_LED,IOT_GPIO_VALUE1);
    osDelay(200U);
    IoTGpioSetOutputVal(GPIO_LED,IOT_GPIO_VALUE0);
}

static void timerExample(void)
{
    //初始化GPIO
    IoTGpioInit(GPIO_LED);

    //设置GPIO为输出方向
    IoTGpioSetDir(GPIO_LED,IOT_GPIO_DIR_OUT);

    osTimerId_t id1,id2;
    
    uint32_t timerDelay;
    osStatus_t status;

    // Create periodic timer
    exec1 = 1U;
    id1 = osTimerNew((osTimerFunc_t)yuchuanTimer,osTimerOnce,&exec1,NULL);
    if (id1 != NULL)
    {
        //1U = 10ms, 100U = 1s
        timerDelay = 100U;
        status = osTimerStart(id1,timerDelay);
        if (status != osOK)
        {
            /* code */
            printf("Timer cloud not be started");
        }
    }

    osDelay(50U);
    status = osTimerStop(id1);
    if (status != osOK)
    {
        //
        printf("Stop YuchuanTimer Fialed!!!\n");
    }else
    {
        printf("Stop YuchuanTimer Success!!!\n");
    }

    osDelay(200U);
    status = osTimerStart(id1,timerDelay);
    if (status != osOK)
    {
        //
        printf("Start YuchuanTimer Fialed!!!\n");
    }else
    {
        printf("Start YuchuanTimer Success!!!\n");
    }

    osDelay(200U);
    status = osTimerDelete(id1);
    if(status != osOK)
    {
        printf("Delete YuchuanTimer Fialed!!!\n");
    }else
    {
        printf("Delete YuchuanTimer Success!!!\n");
    }
     
    //Create Periodict timer
    exec2 = 1U;
    id2 = osTimerNew((osTimerFunc_t)huayingTimer,osTimerPeriodic,&exec2,NULL);
    if (id2 != NULL)
    {
        //1U = 10ms 300U = 3s
        timerDelay = 300U;
        status = osTimerStart(id2,timerDelay);
        if(status != osOK)
        {
            printf("Timer cloud not be started");
        }
    }

}

APP_FEATURE_INIT(timerExample);

applications/sample/BearPi/BearPi-HM_Nano/sample/A2_YuchuanTimer/BUILD.gn

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

    include_dirs = [
        "//utils/native/lite/include",
        "//device/bearpi/bearpi_hm_nano/hi3861_adapter/kal/cmsis",
        "//base/iot_hardware/peripheral/interfaces/kits",
    ]
}

编译调试

修改 BUILD.gn 文件

修改 applications\BearPi\BearPi-HM_Nano\sample路径下 BUILD.gn 文件,指定 yuchuanTimer 参与编译。

# Copyright (c) 2020 Nanjing Xiaoxiongpai Intelligent 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("sample") {
    features = [
        #"A1_kernal_thread:thread_example",
        #"A2_kernel_timer:timer_example",
        #"A3_kernel_event:event_example",
        #"A4_kernel_mutex:mutex_example",
        #"A5_kernel_semaphore:semaphore_example",
        #"A6_kernel_message:message_example",

        #"B1_basic_led_blink:led_example",
        #"B2_basic_button:button_example",
        #"B3_basic_pwm_led:pwm_example",
        #"B4_basic_adc:adc_example", 
        #"B5_basic_i2c_nfc:i2c_example",
        #"B6_basic_uart:uart_example",
        
        #"C1_e53_sf1_mq2:e53_sf1_example",
        #"C2_e53_ia1_temp_humi_pls:e53_ia1_example",
        #"C3_e53_sc1_pls:e53_sc1_example",
        #"C4_e53_sc2_axis:e53_sc2_example",
        #"C5_e53_is1_infrared:e53_is1_example",

        #"D1_iot_wifi_ap:wifi_ap",
        #"D2_iot_wifi_sta_connect:wifi_sta_connect",        
        #"D3_iot_udp_client:udp_client",
        #"D4_iot_tcp_server:tcp_server",
        #"D5_iot_mqtt:iot_mqtt",        
        #"D6_iot_cloud_oc:oc_mqtt",
        #"D7_iot_cloud_onenet:onenet_mqtt",
        #"D8_iot_cloud_oc_smoke:cloud_oc_smoke",
        #"D9_iot_cloud_oc_light:cloud_oc_light",
        #"D10_iot_cloud_oc_manhole_cover:cloud_oc_manhole_cover",
        #"D11_iot_cloud_oc_infrared:cloud_oc_infrared",
        #"D12_iot_cloud_oc_agriculture:cloud_oc_agriculture",
        #"D13_iot_cloud_oc_gps:cloud_oc_gps",
        #"A1_YuchuanThread:yuchuanThread",
        "A2_YuchuanTimer:yuchuanTimer",
    ]
}

运行结果

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

▒▒▒This is Huaying Create Timer!!!
ready to OS start
sdk ver:Hi3861V100R001C00SPC025 2020-09-03 18:10:00
FileSystem mount ok.
wifi init success!

hiview init success.00 00:00:00 0 196 D 0/HIVIEW: log limit init success.
00 00:00:00 0 196 I 1/SAMGR: Bootstrap core services(count:3).
00 00:00:00 0 196 I 1/SAMGR: Init service:0x4b0388 TaskPool:0xe4b7c
00 00:00:00 0 196 I 1/SAMGR: Init service:0x4b0394 TaskPool:0xe4b9c
00 00:00:00 0 196 I 1/SAMGR: Init service:0x4b0860 TaskPool:0xe4bbc
00 00:00:00 0 228 I 1/SAMGR: Init service 0x4b0394 <time: 0ms> success!
00 00:00:00 0 128 I 1/SAMGR: Init service 0x4b0388 <time: 0ms> success!
00 00:00:00 0 72 I 1/SAMGR: Init service 0x4b0860 <time: 10ms> success!
00 00:00:00 0 72 I 1/SAMGR: Initialized all core system services!
Stop YuchuanTimer Success!!!
Start YuchuanTimer Success!!!
This is Yuchuan Create Timer!!!
Delete YuchuanTimer Success!!!
This is Huaying Create Timer!!!
This is Huaying Create Timer!!!
This is Huaying Create Timer!!!
This is Huaying Create Timer!!!
This is Huaying Create Timer!!!
This is Huaying Create Timer!!!
This is Huaying Create Timer!!!
This is Huaying Create Timer!!!
This is Huaying Create Timer!!!
This is Huaying Create Timer!!!
This is Huaying Create Timer!!!
This is Huaying Create Timer!!!
This is Huaying Create Timer!!!
This is Huaying Create Timer!!!


【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。