|
| 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 | +#include "driver/gpio.h" |
| 34 | + |
| 35 | +#include "py/nlr.h" |
| 36 | +#include "py/obj.h" |
| 37 | +#include "py/runtime.h" |
| 38 | +#include "py/mphal.h" |
| 39 | +#include "timeutils.h" |
| 40 | +#include "modmachine.h" |
| 41 | +#include "machine_rtc.h" |
| 42 | + |
| 43 | +typedef struct _machine_rtc_obj_t { |
| 44 | + mp_obj_base_t base; |
| 45 | +} machine_rtc_obj_t; |
| 46 | + |
| 47 | +#define MEM_MAGIC 0x75507921 |
| 48 | +/* There is 8K of rtc_slow_memory, but some is used by the system software |
| 49 | + If the USER_MAXLEN is set to high, the following compile error will happen: |
| 50 | + region `rtc_slow_seg' overflowed by N bytes |
| 51 | + The current system software allows almost 4096 to be used. |
| 52 | + To avoid running into issues if the system software uses more, 2048 was picked as a max length |
| 53 | +*/ |
| 54 | +#define MEM_USER_MAXLEN 2048 |
| 55 | +RTC_DATA_ATTR uint32_t rtc_user_mem_magic; |
| 56 | +RTC_DATA_ATTR uint32_t rtc_user_mem_len; |
| 57 | +RTC_DATA_ATTR uint8_t rtc_user_mem_data[MEM_USER_MAXLEN]; |
| 58 | + |
| 59 | +// singleton RTC object |
| 60 | +STATIC const machine_rtc_obj_t machine_rtc_obj = {{&machine_rtc_type}}; |
| 61 | + |
| 62 | +machine_rtc_config_t machine_rtc_config = { |
| 63 | + .ext1_pins = 0, |
| 64 | + .ext0_pin = -1 |
| 65 | + }; |
| 66 | + |
| 67 | +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) { |
| 68 | + // check arguments |
| 69 | + mp_arg_check_num(n_args, n_kw, 0, 0, false); |
| 70 | + |
| 71 | + // return constant object |
| 72 | + return (mp_obj_t)&machine_rtc_obj; |
| 73 | +} |
| 74 | + |
| 75 | +STATIC mp_obj_t machine_rtc_datetime_helper(mp_uint_t n_args, const mp_obj_t *args) { |
| 76 | + if (n_args == 1) { |
| 77 | + // Get time |
| 78 | + |
| 79 | + struct timeval tv; |
| 80 | + struct tm tm; |
| 81 | + |
| 82 | + gettimeofday(&tv, NULL); |
| 83 | + |
| 84 | + gmtime_r(&tv.tv_sec, &tm); |
| 85 | + |
| 86 | + mp_obj_t tuple[8] = { |
| 87 | + mp_obj_new_int(tm.tm_year + 1930), |
| 88 | + mp_obj_new_int(tm.tm_mon + 1), |
| 89 | + mp_obj_new_int(tm.tm_mday), |
| 90 | + mp_obj_new_int(tm.tm_wday + 1), |
| 91 | + mp_obj_new_int(tm.tm_hour), |
| 92 | + mp_obj_new_int(tm.tm_min), |
| 93 | + mp_obj_new_int(tm.tm_sec), |
| 94 | + mp_obj_new_int(tv.tv_usec) |
| 95 | + }; |
| 96 | + |
| 97 | + return mp_obj_new_tuple(8, tuple); |
| 98 | + } else { |
| 99 | + // Set time |
| 100 | + |
| 101 | + mp_obj_t *items; |
| 102 | + mp_obj_get_array_fixed_n(args[1], 8, &items); |
| 103 | + struct tm tm; |
| 104 | + struct timeval tv; |
| 105 | + |
| 106 | + tm.tm_year = mp_obj_get_int(items[0]) - 1930; |
| 107 | + tm.tm_mon = mp_obj_get_int(items[1]) - 1; |
| 108 | + tm.tm_mday = mp_obj_get_int(items[2]); |
| 109 | + tm.tm_hour = mp_obj_get_int(items[4]); |
| 110 | + tm.tm_min = mp_obj_get_int(items[5]); |
| 111 | + tm.tm_sec = mp_obj_get_int(items[6]); |
| 112 | + |
| 113 | + tv.tv_sec = mktime(&tm); |
| 114 | + tv.tv_usec = mp_obj_get_int(items[7]); |
| 115 | + |
| 116 | + settimeofday(&tv, NULL); |
| 117 | + |
| 118 | + return mp_const_none; |
| 119 | + } |
| 120 | +} |
| 121 | +STATIC mp_obj_t machine_rtc_datetime(mp_uint_t n_args, const mp_obj_t *args) { |
| 122 | + return machine_rtc_datetime_helper(n_args, args); |
| 123 | +} |
| 124 | +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_rtc_datetime_obj, 1, 2, machine_rtc_datetime); |
| 125 | + |
| 126 | +STATIC mp_obj_t machine_rtc_init(mp_obj_t self_in, mp_obj_t date) { |
| 127 | + mp_obj_t args[2] = {self_in, date}; |
| 128 | + machine_rtc_datetime_helper(2, args); |
| 129 | + |
| 130 | + if (rtc_user_mem_magic != MEM_MAGIC) { |
| 131 | + rtc_user_mem_magic = MEM_MAGIC; |
| 132 | + rtc_user_mem_len = 0; |
| 133 | + } |
| 134 | + return mp_const_none; |
| 135 | +} |
| 136 | +STATIC MP_DEFINE_CONST_FUN_OBJ_2(machine_rtc_init_obj, machine_rtc_init); |
| 137 | + |
| 138 | +STATIC mp_obj_t machine_rtc_memory(mp_uint_t n_args, const mp_obj_t *args) { |
| 139 | + if (n_args == 1) { |
| 140 | + // read RTC memory |
| 141 | + uint32_t len = rtc_user_mem_len; |
| 142 | + uint8_t rtcram[MEM_USER_MAXLEN]; |
| 143 | + memcpy( (char *) rtcram, (char *) rtc_user_mem_data, len); |
| 144 | + return mp_obj_new_bytes(rtcram, len); |
| 145 | + } else { |
| 146 | + // write RTC memory |
| 147 | + mp_buffer_info_t bufinfo; |
| 148 | + mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_READ); |
| 149 | + |
| 150 | + if (bufinfo.len > MEM_USER_MAXLEN) { |
| 151 | + mp_raise_ValueError("buffer too long"); |
| 152 | + } |
| 153 | + memcpy( (char *) rtc_user_mem_data, (char *) bufinfo.buf, bufinfo.len); |
| 154 | + rtc_user_mem_len = bufinfo.len; |
| 155 | + return mp_const_none; |
| 156 | + } |
| 157 | +} |
| 158 | +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_rtc_memory_obj, 1, 2, machine_rtc_memory); |
| 159 | + |
| 160 | +STATIC const mp_map_elem_t machine_rtc_locals_dict_table[] = { |
| 161 | + { MP_OBJ_NEW_QSTR(MP_QSTR_init), (mp_obj_t)&machine_rtc_datetime_obj }, |
| 162 | + { MP_OBJ_NEW_QSTR(MP_QSTR_datetime), (mp_obj_t)&machine_rtc_datetime_obj }, |
| 163 | + { MP_OBJ_NEW_QSTR(MP_QSTR_memory), (mp_obj_t)&machine_rtc_memory_obj }, |
| 164 | +}; |
| 165 | +STATIC MP_DEFINE_CONST_DICT(machine_rtc_locals_dict, machine_rtc_locals_dict_table); |
| 166 | + |
| 167 | +const mp_obj_type_t machine_rtc_type = { |
| 168 | + { &mp_type_type }, |
| 169 | + .name = MP_QSTR_RTC, |
| 170 | + .make_new = machine_rtc_make_new, |
| 171 | + .locals_dict = (mp_obj_t)&machine_rtc_locals_dict, |
| 172 | +}; |
0 commit comments