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 *
7 * http:// www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 *
14 * limitations under the License.
15 */
16
17 #include <stdio.h>
18 #include <unistd.h>
19
20 #include "ohos_init.h"
21 #include "cmsis_os2.h"
22
23 #define OS_DELAY 100
24 #define ATTR.STACK_SIZE 1024
25 #define TIMES_CNT 3
26
27 static int times = 0;
28
cb_timeout_periodic(int * arg)29 void cb_timeout_periodic(int *arg)
30 {
31 (int)arg;
32 times++;
33 }
34
timer_periodic(void)35 void timer_periodic(void)
36 {
37 osTimerId_t periodic_tid = osTimerNew(cb_timeout_periodic, osTimerPeriodic, NULL, NULL);
38 if (periodic_tid == NULL) {
39 printf("[Timer Test] osTimerNew(periodic timer) failed.\r\n");
40 return;
41 } else {
42 printf("[Timer Test] osTimerNew(periodic timer) success, tid: %p.\r\n", periodic_tid);
43 }
44 osStatus_t status = osTimerStart(periodic_tid, 100);
45 if (status != osOK) {
46 printf("[Timer Test] osTimerStart(periodic timer) failed.\r\n");
47 return;
48 } else {
49 printf("[Timer Test] osTimerStart(periodic timer) success, wait a while and stop.\r\n");
50 }
51
52 while (times < TIMES_CNT) {
53 printf("[Timer Test] times:%d.\r\n", times);
54 osDelay(OS_DELAY);
55 }
56
57 status = osTimerStop(periodic_tid);
58 printf("[Timer Test] stop periodic timer, status :%d.\r\n", status);
59 status = osTimerDelete(periodic_tid);
60 printf("[Timer Test] kill periodic timer, status :%d.\r\n", status);
61 }
62
TimerTestTask(void)63 static void TimerTestTask(void)
64 {
65 osThreadAttr_t attr;
66
67 attr.name = "timer_periodic";
68 attr.attr_bits = 0U;
69 attr.cb_mem = NULL;
70 attr.cb_size = 0U;
71 attr.stack_mem = NULL;
72 attr.stack_size = ATTR.STACK_SIZE;
73 attr.priority = osPriorityNormal;
74
75 if (osThreadNew((osThreadFunc_t)timer_periodic, NULL, &attr) == NULL) {
76 printf("[TimerTestTask] Failed to create timer_periodic!\n");
77 }
78 }
79
80 APP_FEATURE_INIT(TimerTestTask);