1 /*
2 * Copyright (C) 2015-2021 Alibaba Group Holding Limited
3 */
4
5 #include <stdio.h>
6 #include <string.h>
7 #include <sys/time.h>
8 #include <time.h>
9
10 #include "aos_hal_rtc.h"
11 #include "shared/timeutils/timeutils.h"
12 #include "modmachine.h"
13 #include "py/mphal.h"
14 #include "py/nlr.h"
15 #include "py/obj.h"
16 #include "py/runtime.h"
17 #include "ulog/ulog.h"
18
19 #define LOG_TAG "machine_rtc"
20
21 const mp_obj_type_t machine_rtc_type;
22
23 typedef struct _machine_rtc_obj_t {
24 mp_obj_base_t base;
25 rtc_dev_t dev;
26 } machine_rtc_obj_t;
27
28 // singleton RTC object
29 STATIC const machine_rtc_obj_t machine_rtc_obj = { { &machine_rtc_type } };
30
machine_rtc_make_new(const mp_obj_type_t * type,size_t n_args,size_t n_kw,const mp_obj_t * args)31 STATIC mp_obj_t machine_rtc_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args)
32 {
33 // check arguments
34 mp_arg_check_num(n_args, n_kw, 0, 0, false);
35
36 rtc_dev_t *dev = &(machine_rtc_obj.dev);
37 dev->port = 0;
38 dev->config.format = HAL_RTC_FORMAT_DEC;
39
40 // return constant object
41 return (mp_obj_t)&machine_rtc_obj;
42 }
43
machine_rtc_datetime_helper(mp_uint_t n_args,const mp_obj_t * args)44 STATIC mp_obj_t machine_rtc_datetime_helper(mp_uint_t n_args, const mp_obj_t *args)
45 {
46 machine_rtc_obj_t *self = (machine_rtc_obj_t *)MP_OBJ_TO_PTR(args[0]);
47
48 if (n_args == 1) {
49 // Get time
50 rtc_time_t tm = { 0 };
51
52 aos_hal_rtc_get_time(&self->dev, &tm);
53
54 mp_obj_t tuple[7] = {
55 mp_obj_new_int(tm.year), mp_obj_new_int(tm.month), mp_obj_new_int(tm.date), mp_obj_new_int(tm.weekday),
56 mp_obj_new_int(tm.hr), mp_obj_new_int(tm.min), mp_obj_new_int(tm.sec),
57 };
58
59 return mp_obj_new_tuple(7, tuple);
60 } else {
61 // Set time
62
63 mp_obj_t *items = NULL;
64 mp_obj_get_array_fixed_n(args[1], 8, &items);
65
66 rtc_time_t tm = {
67 .year = mp_obj_get_int(items[0]),
68 .month = mp_obj_get_int(items[1]),
69 .date = mp_obj_get_int(items[2]),
70 .weekday = mp_obj_get_int(items[3]),
71 .hr = mp_obj_get_int(items[4]),
72 .min = mp_obj_get_int(items[5]),
73 .sec = mp_obj_get_int(items[6]),
74 };
75
76 aos_hal_rtc_set_time(&self->dev, &tm);
77
78 return mp_const_none;
79 }
80 }
machine_rtc_datetime(mp_uint_t n_args,const mp_obj_t * args)81 STATIC mp_obj_t machine_rtc_datetime(mp_uint_t n_args, const mp_obj_t *args)
82 {
83 return machine_rtc_datetime_helper(n_args, args);
84 }
85 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_rtc_datetime_obj, 1, 2, machine_rtc_datetime);
86
machine_rtc_init(mp_obj_t self_in,mp_obj_t date)87 STATIC mp_obj_t machine_rtc_init(mp_obj_t self_in, mp_obj_t date)
88 {
89 mp_obj_t args[2] = { self_in, date };
90 machine_rtc_datetime_helper(2, args);
91 return mp_const_none;
92 }
93 STATIC MP_DEFINE_CONST_FUN_OBJ_2(machine_rtc_init_obj, machine_rtc_init);
94
95 STATIC const mp_rom_map_elem_t machine_rtc_locals_dict_table[] = {
96 { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_rtc_init_obj) },
97 { MP_ROM_QSTR(MP_QSTR_datetime), MP_ROM_PTR(&machine_rtc_datetime_obj) },
98 };
99 STATIC MP_DEFINE_CONST_DICT(machine_rtc_locals_dict, machine_rtc_locals_dict_table);
100
101 const mp_obj_type_t machine_rtc_type = {
102 { &mp_type_type },
103 .name = MP_QSTR_RTC,
104 .make_new = machine_rtc_make_new,
105 .locals_dict = (mp_obj_t)&machine_rtc_locals_dict,
106 };