1 /*
2 * Copyright (C) 2022 Beken Corporation
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
16
17 #include <os/os.h>
18 #include "cli.h"
19 #include <driver/aon_rtc.h>
20
21 static void alarm_auto_test_callback(aon_rtc_id_t id, uint8_t *name_p, void *param);
22
23
cli_aon_rtc_help(void)24 static void cli_aon_rtc_help(void)
25 {
26 CLI_LOGI("aon_rtc_driver init\r\n");
27 CLI_LOGI("aon_rtc_driver deinit\r\n");
28 CLI_LOGI("aon_rtc_register {id} {name} {period_tick} {period_cnt}, {callback}\r\n");
29 CLI_LOGI("aon_rtc_unregister {id} {name}\r\n");
30 CLI_LOGI("aon_rtc_timing_test {id} {rounds} {cycles} {set tick val}\r\n");
31 }
32
cli_aon_rtc_driver_cmd(char * pcWriteBuffer,int xWriteBufferLen,int argc,char ** argv)33 static void cli_aon_rtc_driver_cmd(char *pcWriteBuffer, int xWriteBufferLen, int argc, char **argv)
34 {
35 if (argc < 2) {
36 cli_aon_rtc_help();
37 return;
38 }
39
40 if (os_strcmp(argv[1], "init") == 0) {
41 BK_LOG_ON_ERR(bk_aon_rtc_driver_init());
42 CLI_LOGI("aon_rtc driver init\n");
43 } else if (os_strcmp(argv[1], "deinit") == 0) {
44 BK_LOG_ON_ERR(bk_aon_rtc_driver_deinit());
45 CLI_LOGI("aon_rtc driver deinit\n");
46 } else {
47 cli_aon_rtc_help();
48 return;
49 }
50 }
51 static uint8_t alarm_param[12] = {'a', 'l', 'a', 'r', 'm', 'p', 'r', 'm', 0, 0, 0, 0};
alarm_callback(aon_rtc_id_t id,uint8_t * name_p,void * param)52 static void alarm_callback(aon_rtc_id_t id, uint8_t *name_p, void *param)
53 {
54 CLI_LOGI("id=%d, name=%s %s\r\n", id, name_p, alarm_param);
55 }
56
cli_aon_rtc_get_time(char * pcWriteBuffer,int xWriteBufferLen,int argc,char ** argv)57 static void cli_aon_rtc_get_time(char *pcWriteBuffer, int xWriteBufferLen, int argc, char **argv)
58 {
59 uint32_t aon_rtc_id;
60 uint64_t tick = 0;
61
62 aon_rtc_id = os_strtoul(argv[1], NULL, 10);
63 tick = bk_aon_rtc_get_current_tick(aon_rtc_id);
64
65 //CLI_LOGI("id=%d, tick_h=%d tick_l=%d\r\n", aon_rtc_id, (uint32_t)(tick>>32), (uint32_t)tick);
66 CLI_LOGI("id=%d, tick_h=%d tick_l=%d ms\r\n", aon_rtc_id, (uint32_t)((tick/32)>>32), (uint32_t)(tick/32));
67 }
68
cli_aon_rtc_register_cmd(char * pcWriteBuffer,int xWriteBufferLen,int argc,char ** argv)69 static void cli_aon_rtc_register_cmd(char *pcWriteBuffer, int xWriteBufferLen, int argc, char **argv)
70 {
71 uint32_t aon_rtc_id;
72 alarm_info_t alarm_info;
73
74 if (argc != 5) {
75 cli_aon_rtc_help();
76 return;
77 }
78
79 aon_rtc_id = os_strtoul(argv[1], NULL, 10);
80 strncpy((char *)alarm_info.name, argv[2], ALARM_NAME_MAX_LEN);
81 alarm_info.period_tick = os_strtoul(argv[3], NULL, 10);
82 alarm_info.period_cnt = os_strtoul(argv[4], NULL, 10);
83 alarm_info.param_p = (void *)alarm_param;
84 alarm_info.callback = alarm_callback;
85 bk_alarm_register(aon_rtc_id, &alarm_info);
86 }
87
cli_aon_rtc_unregister_cmd(char * pcWriteBuffer,int xWriteBufferLen,int argc,char ** argv)88 static void cli_aon_rtc_unregister_cmd(char *pcWriteBuffer, int xWriteBufferLen, int argc, char **argv)
89 {
90 uint32_t aon_rtc_id;
91 uint8_t name[ALARM_NAME_MAX_LEN+1];
92
93 if (argc != 3) {
94 cli_aon_rtc_help();
95 return;
96 }
97
98 aon_rtc_id = os_strtoul(argv[1], NULL, 10);
99 strncpy((char *)name, argv[2], ALARM_NAME_MAX_LEN);
100
101 bk_alarm_unregister(aon_rtc_id, name);
102 }
103
104 static alarm_info_t s_cli_alarm_info[] =
105 {
106 {"alarm_1", (1000 * AON_RTC_MS_TICK_CNT), 0xFFFFFFFF, alarm_auto_test_callback, NULL},
107 {"alarm_2", (6000 * AON_RTC_MS_TICK_CNT), 0xFFFFFFFF, alarm_auto_test_callback, NULL},
108 {"alarm_3", (12000 * AON_RTC_MS_TICK_CNT), 0xFFFFFFFF, alarm_auto_test_callback, NULL},
109 {"alarm_4", (48000 * AON_RTC_MS_TICK_CNT), 0xFFFFFFFF, alarm_auto_test_callback, NULL},
110 {"alarm_5", (3000 * AON_RTC_MS_TICK_CNT), 0xFFFFFFFF, alarm_auto_test_callback, NULL},
111 {"alarm_6", (4000 * AON_RTC_MS_TICK_CNT), 0xFFFFFFFF, alarm_auto_test_callback, NULL},
112 };
113
alarm_auto_test_callback(aon_rtc_id_t id,uint8_t * name_p,void * param)114 static void alarm_auto_test_callback(aon_rtc_id_t id, uint8_t *name_p, void *param)
115 {
116 uint32_t i = 0;
117 uint32_t arr_size = sizeof(s_cli_alarm_info)/sizeof(alarm_info_t);
118
119 CLI_LOGI("id=%d, name=%s\r\n", id, name_p);
120 for(i = 0; i < arr_size; i++)
121 {
122 if(os_strcmp((const char*)s_cli_alarm_info[i].name, (const char*)name_p) == 0)
123 {
124 //forbid unregister self in the callback
125 //CLI_LOGI("Unregister name=%s\r\n", name_p);
126 //bk_alarm_unregister(id, s_cli_alarm_info[i].name);
127
128 CLI_LOGI("register name=%s\r\n", s_cli_alarm_info[(i+3)%arr_size].name);
129 bk_alarm_register(id, &s_cli_alarm_info[(i+3)%arr_size]);
130
131 break;
132 }
133 }
134 }
135
aon_rtc_autotest_start(aon_rtc_id_t id)136 static void aon_rtc_autotest_start(aon_rtc_id_t id)
137 {
138 uint32_t i = 0;
139
140 for(i = 0; i < sizeof(s_cli_alarm_info)/sizeof(alarm_info_t); i++)
141 {
142 bk_alarm_register(id, &s_cli_alarm_info[i]);
143 }
144 }
145
aon_rtc_autotest_stop(aon_rtc_id_t id)146 static void aon_rtc_autotest_stop(aon_rtc_id_t id)
147 {
148 uint32_t i = 0;
149 for(i = 0; i < sizeof(s_cli_alarm_info)/sizeof(alarm_info_t); i++)
150 {
151 bk_alarm_unregister(id, s_cli_alarm_info[i].name);
152 }
153 }
154
cli_aon_rtc_auto_test_cmd(char * pcWriteBuffer,int xWriteBufferLen,int argc,char ** argv)155 static void cli_aon_rtc_auto_test_cmd(char *pcWriteBuffer, int xWriteBufferLen, int argc, char **argv)
156 {
157 uint32_t aon_rtc_id;
158
159 if (argc != 3) {
160 cli_aon_rtc_help();
161 return;
162 }
163
164 aon_rtc_id = os_strtoul(argv[1], NULL, 10);
165 if (os_strcmp(argv[2], "start") == 0)
166 {
167 aon_rtc_autotest_start(aon_rtc_id);
168 }
169 else if (os_strcmp(argv[2], "stop") == 0)
170 {
171 aon_rtc_autotest_stop(aon_rtc_id);
172 }
173 else
174 cli_aon_rtc_help();
175 }
176
177 #if AON_RTC_DEBUG
cli_aon_rtc_timing_test_cmd(char * pcWriteBuffer,int xWriteBufferLen,int argc,char ** argv)178 static void cli_aon_rtc_timing_test_cmd(char *pcWriteBuffer, int xWriteBufferLen, int argc, char **argv)
179 {
180 uint32_t aon_rtc_id;
181 uint32_t round, cnts, set_tick_val = 0xfffffff0;
182
183 if (argc != 5) {
184 cli_aon_rtc_help();
185 return;
186 }
187
188 aon_rtc_id = os_strtoul(argv[1], NULL, 10);
189 round = os_strtoul(argv[2], NULL, 10);
190 cnts = os_strtoul(argv[3], NULL, 10);
191 set_tick_val = os_strtoul(argv[4], NULL, 10);
192
193 bk_aon_rtc_timing_test(aon_rtc_id, round, cnts, set_tick_val);
194 }
195 #endif
196
cli_aon_rtc_dump_cmd(char * pcWriteBuffer,int xWriteBufferLen,int argc,char ** argv)197 static void cli_aon_rtc_dump_cmd(char *pcWriteBuffer, int xWriteBufferLen, int argc, char **argv)
198 {
199 uint32_t aon_rtc_id;
200
201 if (argc != 2) {
202 cli_aon_rtc_help();
203 return;
204 }
205
206 aon_rtc_id = os_strtoul(argv[1], NULL, 10);
207
208 bk_aon_rtc_dump(aon_rtc_id);
209 }
210
211
212 #define AON_RTC_CMD_CNT (sizeof(s_aon_rtc_commands) / sizeof(struct cli_command))
213 static const struct cli_command s_aon_rtc_commands[] = {
214 {"aon_rtc_driver", "aon_rtc_driver {init|deinit}", cli_aon_rtc_driver_cmd},
215 {"aon_rtc_get_time", "aon_rtc_get_time {id}", cli_aon_rtc_get_time},
216 {"aon_rtc_register", "aon_rtc_register {id} {name} {period_tick} {period_cnt}, {callback}", cli_aon_rtc_register_cmd},
217 {"aon_rtc_unregister", "aon_rtc_unregister {id} {name}", cli_aon_rtc_unregister_cmd},
218 {"aon_rtc_auto_test", "{id} {start|stop}", cli_aon_rtc_auto_test_cmd},
219 #if AON_RTC_DEBUG
220 {"aon_rtc_timing_test", "{id} {rounds} {cycles} {set tick val}", cli_aon_rtc_timing_test_cmd},
221 #endif
222 {"aon_rtc_dump", "{id}", cli_aon_rtc_dump_cmd},
223 };
224
cli_aon_rtc_init(void)225 int cli_aon_rtc_init(void)
226 {
227 BK_LOG_ON_ERR(bk_aon_rtc_driver_init());
228 return cli_register_commands(s_aon_rtc_commands, AON_RTC_CMD_CNT);
229 }
230
231