8
8
#include <zephyr/kernel.h>
9
9
#include <zephyr/shell/shell.h>
10
10
#include <zephyr/drivers/rtc.h>
11
+ #include <zephyr/sys/realtime.h>
11
12
#include <time.h>
12
13
#include <stdlib.h>
13
14
@@ -227,6 +228,43 @@ static void device_name_get(size_t idx, struct shell_static_entry *entry)
227
228
entry -> subcmd = NULL ;
228
229
}
229
230
231
+ #if CONFIG_SYS_REALTIME
232
+ static int cmd_sync (const struct shell * sh , size_t argc , char * * argv )
233
+ {
234
+ const struct device * dev = device_get_binding (argv [1 ]);
235
+
236
+ if (!device_is_ready (dev )) {
237
+ shell_error (sh , "device %s not %s" , argv [1 ], "ready" );
238
+ return - ENODEV ;
239
+ }
240
+
241
+ ARG_UNUSED (argc );
242
+ ARG_UNUSED (argv );
243
+
244
+ struct rtc_time rtctime ;
245
+
246
+ int res = rtc_get_time (dev , & rtctime );
247
+
248
+ if (res == - ENODATA ) {
249
+ shell_error (sh , "device %s not %s" , argv [1 ], "set" );
250
+ return 0 ;
251
+ }
252
+ if (res < 0 ) {
253
+ return res ;
254
+ }
255
+
256
+ struct sys_datetime * datetime = (struct sys_datetime * )(& rtctime );
257
+
258
+ res = sys_realtime_set_datetime (datetime );
259
+
260
+ if (res < 0 ) {
261
+ shell_error (sh , "failed to set system realtime from %s" , argv [1 ]);
262
+ }
263
+
264
+ return res ;
265
+ }
266
+ #endif /* CONFIG_SYS_REALTIME */
267
+
230
268
#define RTC_GET_HELP \
231
269
("Get current time (UTC)\n" \
232
270
"Usage: rtc get <device>")
@@ -235,12 +273,22 @@ static void device_name_get(size_t idx, struct shell_static_entry *entry)
235
273
("Set UTC time\n" \
236
274
"Usage: rtc set <device> <YYYY-MM-DDThh:mm:ss> | <YYYY-MM-DD> | <hh:mm:ss>")
237
275
276
+ #if CONFIG_SYS_REALTIME
277
+ #define RTC_SYNC_HELP \
278
+ ("Set system realtime from RTC time\n" \
279
+ "Usage: rtc sync <device>")
280
+ #endif /* CONFIG_SYS_REALTIME */
281
+
238
282
SHELL_DYNAMIC_CMD_CREATE (dsub_device_name , device_name_get );
239
283
240
- SHELL_STATIC_SUBCMD_SET_CREATE (sub_rtc ,
241
- /* Alphabetically sorted */
242
- SHELL_CMD_ARG (set , & dsub_device_name , RTC_SET_HELP , cmd_set , 3 , 0 ),
243
- SHELL_CMD_ARG (get , & dsub_device_name , RTC_GET_HELP , cmd_get , 2 , 0 ),
244
- SHELL_SUBCMD_SET_END );
284
+ SHELL_STATIC_SUBCMD_SET_CREATE (
285
+ sub_rtc ,
286
+ SHELL_CMD_ARG (set , & dsub_device_name , RTC_SET_HELP , cmd_set , 3 , 0 ),
287
+ SHELL_CMD_ARG (get , & dsub_device_name , RTC_GET_HELP , cmd_get , 2 , 0 ),
288
+ #if CONFIG_SYS_REALTIME
289
+ SHELL_CMD_ARG (sync , & dsub_device_name , RTC_SYNC_HELP , cmd_sync , 2 , 0 ),
290
+ #endif /* CONFIG_SYS_REALTIME */
291
+ SHELL_SUBCMD_SET_END
292
+ );
245
293
246
294
SHELL_CMD_REGISTER (rtc , & sub_rtc , "RTC commands" , NULL );
0 commit comments