• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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: RTC Sample Source. \n
16  *
17  * History: \n
18  * 2023-07-18, Create file. \n
19  */
20 #include "rtc.h"
21 #include "tcxo.h"
22 #include "common_def.h"
23 #include "soc_osal.h"
24 #include "app_init.h"
25 
26 #define RTC_TIMERS_NUM            4
27 #define RTC_INDEX                 2
28 #define RTC_IRQN                  51
29 #define RTC_PRIO                  1
30 #define RTC_DELAY_INT             5
31 #define RTC1_DELAY_1000MS         1000
32 #define RTC2_DELAY_2000MS         2000
33 #define RTC3_DELAY_3000MS         3000
34 #define RTC4_DELAY_4000MS         4000
35 
36 #define RTC_TASK_PRIO             24
37 #define RTC_TASK_STACK_SIZE       0x1000
38 
39 typedef struct rtc_info {
40     uint32_t start_time;
41     uint32_t end_time;
42     uint32_t delay_time;
43 } rtc_info_t;
44 
45 static uint32_t g_rtc_int_count = 0;
46 static rtc_info_t g_rtcs_info[RTC_TIMERS_NUM] = {
47     {0, 0, RTC1_DELAY_1000MS},
48     {0, 0, RTC2_DELAY_2000MS},
49     {0, 0, RTC3_DELAY_3000MS},
50     {0, 0, RTC4_DELAY_4000MS}
51 };
52 
53 /* Timed task callback function list. */
rtc_timeout_callback(uintptr_t data)54 static void rtc_timeout_callback(uintptr_t data)
55 {
56     uint32_t rtc_index = (uint32_t)data;
57     g_rtcs_info[rtc_index].end_time = uapi_tcxo_get_ms();
58     g_rtc_int_count++;
59 }
60 
rtc_task(const char * arg)61 static void *rtc_task(const char *arg)
62 {
63     unused(arg);
64     rtc_handle_t rtc_index[RTC_TIMERS_NUM] = { 0 };
65     uapi_rtc_init();
66     uapi_rtc_adapter(RTC_INDEX, RTC_IRQN, RTC_PRIO);
67 
68     for (uint32_t i = 0; i < RTC_TIMERS_NUM; i++) {
69         uapi_rtc_create(RTC_INDEX, &rtc_index[i]);
70         g_rtcs_info[i].start_time = uapi_tcxo_get_ms();
71         uapi_rtc_start(rtc_index[i], g_rtcs_info[i].delay_time, rtc_timeout_callback, i);
72         osal_msleep(RTC_DELAY_INT);
73     }
74 
75     while (g_rtc_int_count < RTC_TIMERS_NUM) {
76         osal_msleep(RTC_DELAY_INT);
77     }
78 
79     for (uint32_t i = 0; i < RTC_TIMERS_NUM; i++) {
80         uapi_rtc_stop(rtc_index[i]);
81         uapi_rtc_delete(rtc_index[i]);
82         osal_printk("real time[%d] = %dms  ", i, (g_rtcs_info[i].end_time -  g_rtcs_info[i].start_time));
83         osal_printk("  delay = %dms\r\n", g_rtcs_info[i].delay_time);
84     }
85     return NULL;
86 }
87 
rtc_entry(void)88 static void rtc_entry(void)
89 {
90     osal_task *task_handle = NULL;
91     osal_kthread_lock();
92     task_handle = osal_kthread_create((osal_kthread_handler)rtc_task, 0, "RTCTask", RTC_TASK_STACK_SIZE);
93     if (task_handle != NULL) {
94         osal_kthread_set_priority(task_handle, RTC_TASK_PRIO);
95         osal_kfree(task_handle);
96     }
97     osal_kthread_unlock();
98 }
99 
100 /* Run the rtc_entry. */
101 app_run(rtc_entry);