blob: 594c34b1579cffb12e71ef469ac89b78597c0cd2 [file] [log] [blame]
Josef Gajdusek103d12a2015-05-11 21:11:37 +02001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2015 Josef Gajdusek
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
27#include <stdio.h>
28#include <string.h>
29
30#include "py/nlr.h"
31#include "py/obj.h"
32#include "py/runtime.h"
Josef Gajdusek103d12a2015-05-11 21:11:37 +020033#include "timeutils.h"
34#include "user_interface.h"
Damien Georgede8b5852015-06-22 23:03:17 +010035#include "modpyb.h"
Josef Gajdusek103d12a2015-05-11 21:11:37 +020036
37typedef struct _pyb_rtc_obj_t {
38 mp_obj_base_t base;
39} pyb_rtc_obj_t;
40
41#define MEM_MAGIC 0x75507921
42#define MEM_DELTA_ADDR 64
43#define MEM_CAL_ADDR (MEM_DELTA_ADDR + 2)
44#define MEM_USER_MAGIC_ADDR (MEM_CAL_ADDR + 1)
45#define MEM_USER_LEN_ADDR (MEM_USER_MAGIC_ADDR + 1)
46#define MEM_USER_DATA_ADDR (MEM_USER_LEN_ADDR + 1)
47#define MEM_USER_MAXLEN (512 - (MEM_USER_DATA_ADDR - MEM_DELTA_ADDR) * 4)
48
Damien Georgede8b5852015-06-22 23:03:17 +010049// singleton RTC object
50STATIC const pyb_rtc_obj_t pyb_rtc_obj = {{&pyb_rtc_type}};
51
Paul Sokolovskya4c8ef92016-02-08 21:43:37 +020052void mp_hal_rtc_init(void) {
53 uint32_t magic;
54
55 system_rtc_mem_read(MEM_USER_MAGIC_ADDR, &magic, sizeof(magic));
56 if (magic != MEM_MAGIC) {
57 magic = MEM_MAGIC;
58 system_rtc_mem_write(MEM_USER_MAGIC_ADDR, &magic, sizeof(magic));
59 uint32_t cal = system_rtc_clock_cali_proc();
60 int64_t delta = 0;
61 system_rtc_mem_write(MEM_CAL_ADDR, &cal, sizeof(cal));
62 system_rtc_mem_write(MEM_DELTA_ADDR, &delta, sizeof(delta));
63 }
64}
65
Damien George5b3f0b72016-01-03 15:55:55 +000066STATIC mp_obj_t pyb_rtc_make_new(const mp_obj_type_t *type, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
Damien Georgede8b5852015-06-22 23:03:17 +010067 // check arguments
68 mp_arg_check_num(n_args, n_kw, 0, 0, false);
69
70 // return constant object
71 return (mp_obj_t)&pyb_rtc_obj;
72}
73
Josef Gajdusek103d12a2015-05-11 21:11:37 +020074STATIC uint64_t pyb_rtc_raw_us(uint64_t cal) {
Paul Sokolovskya2e39a72016-02-12 23:20:52 +020075 return (system_get_rtc_time() * cal) >> 12;
Josef Gajdusek103d12a2015-05-11 21:11:37 +020076};
77
78void pyb_rtc_set_us_since_2000(uint64_t nowus) {
79 uint32_t cal = system_rtc_clock_cali_proc();
80 int64_t delta = nowus - pyb_rtc_raw_us(cal);
81
82 // As the calibration value jitters quite a bit, to make the
83 // clock at least somewhat practially usable, we need to store it
84 system_rtc_mem_write(MEM_CAL_ADDR, &cal, sizeof(cal));
85 system_rtc_mem_write(MEM_DELTA_ADDR, &delta, sizeof(delta));
86};
87
88uint64_t pyb_rtc_get_us_since_2000() {
89 uint32_t cal;
90 int64_t delta;
91
92 system_rtc_mem_read(MEM_CAL_ADDR, &cal, sizeof(cal));
93 system_rtc_mem_read(MEM_DELTA_ADDR, &delta, sizeof(delta));
94
95 return pyb_rtc_raw_us(cal) + delta;
96};
97
98STATIC mp_obj_t pyb_rtc_datetime(mp_uint_t n_args, const mp_obj_t *args) {
99 if (n_args == 1) {
100 // Get time
101 uint64_t msecs = pyb_rtc_get_us_since_2000() / 1000;
102
103 timeutils_struct_time_t tm;
104 timeutils_seconds_since_2000_to_struct_time(msecs / 1000, &tm);
105
106 mp_obj_t tuple[8] = {
107 mp_obj_new_int(tm.tm_year),
108 mp_obj_new_int(tm.tm_mon),
109 mp_obj_new_int(tm.tm_mday),
110 mp_obj_new_int(tm.tm_wday),
111 mp_obj_new_int(tm.tm_hour),
112 mp_obj_new_int(tm.tm_min),
113 mp_obj_new_int(tm.tm_sec),
114 mp_obj_new_int(msecs % 1000)
115 };
116
117 return mp_obj_new_tuple(8, tuple);
118 } else {
119 // Set time
120 mp_obj_t *items;
121 mp_obj_get_array_fixed_n(args[1], 8, &items);
122
123 pyb_rtc_set_us_since_2000(
124 ((uint64_t)timeutils_seconds_since_2000(
125 mp_obj_get_int(items[0]),
126 mp_obj_get_int(items[1]),
127 mp_obj_get_int(items[2]),
128 mp_obj_get_int(items[4]),
129 mp_obj_get_int(items[5]),
130 mp_obj_get_int(items[6])) * 1000 + mp_obj_get_int(items[7])) * 1000);
131
132 return mp_const_none;
133 }
134}
135STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_rtc_datetime_obj, 1, 2, pyb_rtc_datetime);
136
137STATIC mp_obj_t pyb_rtc_memory(mp_uint_t n_args, const mp_obj_t *args) {
138 uint8_t rtcram[MEM_USER_MAXLEN];
139 uint32_t len;
140 uint32_t magic;
141
142 if (n_args == 1) {
143
144 system_rtc_mem_read(MEM_USER_MAGIC_ADDR, &magic, sizeof(magic));
145 if (magic != MEM_MAGIC) {
146 return mp_const_none;
147 }
148
149 system_rtc_mem_read(MEM_USER_LEN_ADDR, &len, sizeof(len));
150 system_rtc_mem_read(MEM_USER_DATA_ADDR, rtcram, len + (4 - len % 4));
151
152 return mp_obj_new_bytes(rtcram, len);
153 } else {
154 mp_buffer_info_t bufinfo;
155 mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_READ);
156
157 if (bufinfo.len > MEM_USER_MAXLEN) {
158 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
159 "buffer too long"));
160 }
161
162 magic = MEM_MAGIC;
Paul Sokolovsky814b1ae2016-02-08 21:35:36 +0200163 system_rtc_mem_write(MEM_USER_MAGIC_ADDR, &magic, sizeof(magic));
Josef Gajdusek103d12a2015-05-11 21:11:37 +0200164 len = bufinfo.len;
165 system_rtc_mem_write(MEM_USER_LEN_ADDR, &len, sizeof(len));
166
167 int i = 0;
168 for (; i < bufinfo.len; i++) {
169 rtcram[i] = ((uint8_t *)bufinfo.buf)[i];
170 }
171
172 system_rtc_mem_write(MEM_USER_DATA_ADDR, rtcram, len + (4 - len % 4));
173
174 return mp_const_none;
175 }
176
177}
178STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_rtc_memory_obj, 1, 2, pyb_rtc_memory);
179
180STATIC const mp_map_elem_t pyb_rtc_locals_dict_table[] = {
181 { MP_OBJ_NEW_QSTR(MP_QSTR_datetime), (mp_obj_t)&pyb_rtc_datetime_obj },
182 { MP_OBJ_NEW_QSTR(MP_QSTR_memory), (mp_obj_t)&pyb_rtc_memory_obj },
183};
184STATIC MP_DEFINE_CONST_DICT(pyb_rtc_locals_dict, pyb_rtc_locals_dict_table);
185
Damien Georgede8b5852015-06-22 23:03:17 +0100186const mp_obj_type_t pyb_rtc_type = {
Josef Gajdusek103d12a2015-05-11 21:11:37 +0200187 { &mp_type_type },
188 .name = MP_QSTR_RTC,
Damien Georgede8b5852015-06-22 23:03:17 +0100189 .make_new = pyb_rtc_make_new,
Josef Gajdusek103d12a2015-05-11 21:11:37 +0200190 .locals_dict = (mp_obj_t)&pyb_rtc_locals_dict,
191};