• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1  /*
2  Copyright (C) 2024 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  limitations under the License.
14  */
15 #include <stdio.h>
16 #include <unistd.h>
17 
18 #include "ohos_init.h"
19 #include "cmsis_os2.h"
20 
21 static int times = 0;
22 
cb_timeout_periodic(void * arg)23 void cb_timeout_periodic(void *arg)
24 {
25     (void)arg;
26     times++;
27 }
28 
timer_periodic(void)29 void timer_periodic(void)
30 {
31     osTimerId_t periodic_tid = osTimerNew(cb_timeout_periodic, osTimerPeriodic, NULL, NULL);
32     const uint32_t DelayTimeMs = 100;
33     const uint32_t DelayTime = 3;
34 
35     if (periodic_tid == NULL) {
36         printf("[Timer Test] osTimerNew(periodic timer) failed.\r\n");
37         return;
38     } else {
39         printf("[Timer Test] osTimerNew(periodic timer) success, tid: %p.\r\n", periodic_tid);
40     }
41     osStatus_t status = osTimerStart(periodic_tid, 100);
42     if (status != osOK) {
43         printf("[Timer Test] osTimerStart(periodic timer) failed.\r\n");
44         return;
45     } else {
46         printf("[Timer Test] osTimerStart(periodic timer) success, wait a while and stop.\r\n");
47     }
48 
49     while (times < DelayTime) {
50         printf("[Timer Test] times:%d.\r\n", times);
51         osDelay(DelayTimeMs);
52     }
53 
54     status = osTimerStop(periodic_tid);
55     printf("[Timer Test] stop periodic timer, status :%d.\r\n", status);
56     status = osTimerDelete(periodic_tid);
57     printf("[Timer Test] kill periodic timer, status :%d.\r\n", status);
58 }
59 
TimerTestTask(void)60 static void TimerTestTask(void)
61 {
62     osThreadAttr_t attr;
63 
64     attr.name = "timer_periodic";
65     attr.attr_bits = 0U;
66     attr.cb_mem = NULL;
67     attr.cb_size = 0U;
68     attr.stack_mem = NULL;
69     attr.stack_size = 0x1000;
70     attr.priority = osPriorityNormal;
71 
72     if (osThreadNew((osThreadFunc_t)timer_periodic, NULL, &attr) == NULL) {
73         printf("[TimerTestTask] Falied to create timer_periodic!\n");
74     }
75 }
76 
77 APP_FEATURE_INIT(TimerTestTask);