1 /**
2 * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
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 * Description: Timer Sample Source. \n
16 *
17 * History: \n
18 * 2023-07-18, Create file. \n
19 */
20 #include "timer.h"
21 #include "tcxo.h"
22 #include "chip_core_irq.h"
23 #include "common_def.h"
24 #include "soc_osal.h"
25 #include "app_init.h"
26
27 #define TIMER_TIMERS_NUM 4
28 #define TIMER_INDEX 1
29 #define TIMER_PRIO 1
30 #define TIMER_DELAY_INT 5
31 #define TIMER1_DELAY_1000US 1000
32 #define TIMER2_DELAY_2000US 2000
33 #define TIMER3_DELAY_3000US 3000
34 #define TIMER4_DELAY_4000US 4000
35 #define TIMER_MS_2_US 1000
36
37 #define TIMER_TASK_PRIO 24
38 #define TIMER_TASK_STACK_SIZE 0x1000
39
40 typedef struct timer_info {
41 uint32_t start_time;
42 uint32_t end_time;
43 uint32_t delay_time;
44 } timer_info_t;
45
46 static uint32_t g_timer_int_count = 0;
47 static timer_info_t g_timers_info[TIMER_TIMERS_NUM] = {
48 {0, 0, TIMER1_DELAY_1000US},
49 {0, 0, TIMER2_DELAY_2000US},
50 {0, 0, TIMER3_DELAY_3000US},
51 {0, 0, TIMER4_DELAY_4000US}
52 };
53
54 /* Timed task callback function list. */
timer_timeout_callback(uintptr_t data)55 static void timer_timeout_callback(uintptr_t data)
56 {
57 uint32_t timer_index = (uint32_t)data;
58 g_timers_info[timer_index].end_time = uapi_tcxo_get_ms();
59 g_timer_int_count++;
60 }
61
timer_task(const char * arg)62 static void *timer_task(const char *arg)
63 {
64 unused(arg);
65 timer_handle_t timer_index[TIMER_TIMERS_NUM] = { 0 };
66 uapi_timer_init();
67 uapi_timer_adapter(TIMER_INDEX, TIMER_1_IRQN, TIMER_PRIO);
68
69 for (uint32_t i = 0; i < TIMER_TIMERS_NUM; i++) {
70 uapi_timer_create(TIMER_INDEX, &timer_index[i]);
71 g_timers_info[i].start_time = uapi_tcxo_get_ms();
72 uapi_timer_start(timer_index[i], g_timers_info[i].delay_time, timer_timeout_callback, i);
73 osal_msleep(TIMER_DELAY_INT);
74 }
75
76 while (g_timer_int_count < TIMER_TIMERS_NUM) {
77 osal_msleep(TIMER_DELAY_INT);
78 }
79
80 for (uint32_t i = 0; i < TIMER_TIMERS_NUM; i++) {
81 uapi_timer_stop(timer_index[i]);
82 uapi_timer_delete(timer_index[i]);
83 osal_printk("real time[%d] = %dms ", i, (g_timers_info[i].end_time - g_timers_info[i].start_time));
84 osal_printk(" delay = %dms\r\n", g_timers_info[i].delay_time / TIMER_MS_2_US);
85 }
86 return NULL;
87 }
88
timer_entry(void)89 static void timer_entry(void)
90 {
91 osal_task *task_handle = NULL;
92 osal_kthread_lock();
93 task_handle = osal_kthread_create((osal_kthread_handler)timer_task, 0, "TimerTask", TIMER_TASK_STACK_SIZE);
94 if (task_handle != NULL) {
95 osal_kthread_set_priority(task_handle, TIMER_TASK_PRIO);
96 osal_kfree(task_handle);
97 }
98 osal_kthread_unlock();
99 }
100
101 /* Run the timer_entry. */
102 app_run(timer_entry);