• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 HiHope Open Source Organization .
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *     http:// www.apache.org/licenses/LICENSE-2.0
7  *
8  * 版权所有 (C) 2022 HiHope 开源组织 。
9  * 根据 Apache 许可证 2.0 版(“许可证”)进行许可;
10  * 除非符合许可证,否则您不得使用此文件。
11  * 您可以在以下位置获取许可证副本:
12  *     http:// www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  *
19  * limitations under the License.
20  */
21  * 除非适用法律要求或书面同意,否则软件
22  * 根据许可证分发的按“原样”分发,
23  * 不提供任何明示或暗示的保证或条件。
24  * 请参阅许可证,了解管理权限和
25  *
26  * 许可证下的限制。
27 
28 #include <stdio.h>
29 #include <unistd.h>
30 
31 #include "ohos_init.h"
32 #include "cmsis_os2.h"
33 
34 #define OS_DELAY 100
35 #define ATTR.STACK_SIZE 1024
36 #define TIMES_CNT   3
37 
38 // 声明一个用于计数的全局变量
39 static int times = 0;
40 
41 // 声明定时器的回调函数
cb_timeout_periodic(int * arg)42 void cb_timeout_periodic(int *arg)
43 {
44     (int)arg;
45     times++;
46 }
47 
48 
timer_periodic(void)49 void timer_periodic(void)
50 {
51 // 用osTimerNew创建一个定时器
52     osTimerId_t periodic_tid = osTimerNew(cb_timeout_periodic, osTimerPeriodic, NULL, NULL);
53     if (periodic_tid == NULL) {
54         printf("[Timer Test] osTimerNew(periodic timer) failed.\r\n");
55         return;
56     } else {
57         printf("[Timer Test] osTimerNew(periodic timer) success, tid: %p.\r\n", periodic_tid);
58     }
59 // 一秒钟调用一次回调函数
60     osStatus_t status = osTimerStart(periodic_tid, 100);
61     if (status != osOK) {
62         printf("[Timer Test] osTimerStart(periodic timer) failed.\r\n");
63         return;
64     } else {
65         printf("[Timer Test] osTimerStart(periodic timer) success, wait a while and stop.\r\n");
66     }
67 
68 // 等待三秒
69     while (times < TIMES_CNT) {
70         printf("[Timer Test] times:%d.\r\n", times);
71         osDelay(OS_DELAY);
72 }
73 
74 // 停止定时器
75     status = osTimerStop(periodic_tid);
76     printf("[Timer Test] stop periodic timer, status :%d.\r\n", status);
77 // 删除定时器
78     status = osTimerDelete(periodic_tid);
79     printf("[Timer Test] kill periodic timer, status :%d.\r\n", status);
80 }
81 
TimerTestTask(void)82 static void TimerTestTask(void)
83 {
84     osThreadAttr_t attr;
85     // 创建新线程
86 
87     attr.name = "timer_periodic";
88     // 设置线程名
89     attr.attr_bits = 0U;
90     attr.cb_mem = NULL;
91     // 文档限定为NULL
92     attr.cb_size = 0U;
93     attr.stack_mem = NULL;
94     // 文档限定为NULL
95     attr.stack_size = ATTR.STACK_SIZE;
96     // 设置堆栈大小
97     attr.priority = osPriorityNormal;
98     // 设置线程优先级
99     // 创建失败提示:
100     if (osThreadNew((osThreadFunc_t)timer_periodic, NULL, &attr) == NULL) {
101         printf("[TimerTestTask] Failed to create timer_periodic!\n");
102     }
103 }
104 
105 APP_FEATURE_INIT(TimerTestTask);