/* * Copyright (C) 2022 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 #include #include "ohos_init.h" #include "cmsis_os2.h" #define OS_DELAY 100 #define ATTR.STACK_SIZE 1024 #define TIMES_CNT 3 static int times = 0; void cb_timeout_periodic(int *arg) { (int)arg; 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, 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 < TIMES_CNT) { printf("[Timer Test] times:%d.\r\n", times); osDelay(OS_DELAY); } 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 = ATTR.STACK_SIZE; attr.priority = osPriorityNormal; if (osThreadNew((osThreadFunc_t)timer_periodic, NULL, &attr) == NULL) { printf("[TimerTestTask] Failed to create timer_periodic!\n"); } } APP_FEATURE_INIT(TimerTestTask);