1 /*
2  * This file is part of the MicroPython project, http://micropython.org/
3  *
4  * The MIT License (MIT)
5  *
6  * Copyright (c) 2017 "Eric Poulsen" <eric@zyxod.com>
7  * Copyright (c) 2017 "Tom Manning" <tom@manningetal.com>
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a copy
10  * of this software and associated documentation files (the "Software"), to deal
11  * in the Software without restriction, including without limitation the rights
12  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13  * copies of the Software, and to permit persons to whom the Software is
14  * furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be included in
17  * all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25  * THE SOFTWARE.
26  */
27 
28 #include <stdio.h>
29 #include <string.h>
30 
31 #include <time.h>
32 #include <sys/time.h>
33 
34 #include "py/nlr.h"
35 #include "py/obj.h"
36 #include "py/runtime.h"
37 #include "py/mphal.h"
38 #include "shared/timeutils/timeutils.h"
39 #include "modmachine.h"
40 
41 #include "ulog/ulog.h"
42 #include "aos_hal_rtc.h"
43 
44 #define LOG_TAG "machine_rtc"
45 
46 const mp_obj_type_t machine_rtc_type;
47 
48 typedef struct _machine_rtc_obj_t {
49     mp_obj_base_t base;
50     rtc_dev_t dev;
51 } machine_rtc_obj_t;
52 
53 // singleton RTC object
54 STATIC const machine_rtc_obj_t machine_rtc_obj = {{&machine_rtc_type}};
55 
machine_rtc_make_new(const mp_obj_type_t * type,size_t n_args,size_t n_kw,const mp_obj_t * args)56 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) {
57     // check arguments
58     mp_arg_check_num(n_args, n_kw, 0, 0, false);
59 
60     rtc_dev_t * dev = &(machine_rtc_obj.dev);
61     dev->port = 0;
62     dev->config.format = HAL_RTC_FORMAT_DEC;
63 
64     // return constant object
65     return (mp_obj_t)&machine_rtc_obj;
66 }
67 
machine_rtc_datetime_helper(mp_uint_t n_args,const mp_obj_t * args)68 STATIC mp_obj_t machine_rtc_datetime_helper(mp_uint_t n_args, const mp_obj_t *args) {
69     machine_rtc_obj_t *self = (machine_rtc_obj_t*)MP_OBJ_TO_PTR(args[0]);
70 
71     if (n_args == 1) {
72         // Get time
73         rtc_time_t tm = {0};
74 
75         aos_hal_rtc_get_time(&self->dev, &tm);
76 
77         mp_obj_t tuple[7] = {
78             mp_obj_new_int(tm.year),
79             mp_obj_new_int(tm.month),
80             mp_obj_new_int(tm.date),
81             mp_obj_new_int(tm.weekday),
82             mp_obj_new_int(tm.hr),
83             mp_obj_new_int(tm.min),
84             mp_obj_new_int(tm.sec),
85         };
86 
87         return mp_obj_new_tuple(7, tuple);
88     } else {
89         // Set time
90 
91         mp_obj_t *items = NULL;
92         mp_obj_get_array_fixed_n(args[1], 8, &items);
93 
94         rtc_time_t tm = {
95             .year= mp_obj_get_int(items[0]),
96             .month= mp_obj_get_int(items[1]),
97             .date = mp_obj_get_int(items[2]),
98             .weekday = mp_obj_get_int(items[3]),
99             .hr = mp_obj_get_int(items[4]),
100             .min = mp_obj_get_int(items[5]),
101             .sec = mp_obj_get_int(items[6]),
102         };
103 
104         aos_hal_rtc_set_time(&self->dev, &tm);
105 
106         return mp_const_none;
107     }
108 }
machine_rtc_datetime(mp_uint_t n_args,const mp_obj_t * args)109 STATIC mp_obj_t machine_rtc_datetime(mp_uint_t n_args, const mp_obj_t *args) {
110     return machine_rtc_datetime_helper(n_args, args);
111 }
112 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_rtc_datetime_obj, 1, 2, machine_rtc_datetime);
113 
machine_rtc_init(mp_obj_t self_in,mp_obj_t date)114 STATIC mp_obj_t machine_rtc_init(mp_obj_t self_in, mp_obj_t date) {
115     mp_obj_t args[2] = {self_in, date};
116     machine_rtc_datetime_helper(2, args);
117     return mp_const_none;
118 }
119 STATIC MP_DEFINE_CONST_FUN_OBJ_2(machine_rtc_init_obj, machine_rtc_init);
120 
121 STATIC const mp_rom_map_elem_t machine_rtc_locals_dict_table[] = {
122     { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_rtc_init_obj) },
123     { MP_ROM_QSTR(MP_QSTR_datetime), MP_ROM_PTR(&machine_rtc_datetime_obj) },
124 };
125 STATIC MP_DEFINE_CONST_DICT(machine_rtc_locals_dict, machine_rtc_locals_dict_table);
126 
127 const mp_obj_type_t machine_rtc_type = {
128     { &mp_type_type },
129     .name = MP_QSTR_RTC,
130     .make_new = machine_rtc_make_new,
131     .locals_dict = (mp_obj_t)&machine_rtc_locals_dict,
132 };