|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2019 Nick Moore for Adafruit Industries |
| 7 | + * Copyright (c) 2019 Artur Pacholec |
| 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 <stdlib.h> |
| 29 | + |
| 30 | +#include "py/obj.h" |
| 31 | +#include "py/runtime.h" |
| 32 | +#include "shared/timeutils/timeutils.h" |
| 33 | +#include "supervisor/port.h" |
| 34 | + |
| 35 | +// This is the time in seconds since 2000 that the RTC was started. |
| 36 | +// TODO: Change the offset to ticks so that it can be a subsecond adjustment. |
| 37 | +static uint32_t rtc_offset = 0; |
| 38 | + |
| 39 | +void common_hal_rtc_get_time(timeutils_struct_time_t *tm) { |
| 40 | + uint64_t ticks_s = port_get_raw_ticks(NULL) / 1024; |
| 41 | + timeutils_seconds_since_2000_to_struct_time(rtc_offset + ticks_s, tm); |
| 42 | +} |
| 43 | + |
| 44 | +void common_hal_rtc_set_time(timeutils_struct_time_t *tm) { |
| 45 | + uint64_t ticks_s = port_get_raw_ticks(NULL) / 1024; |
| 46 | + uint32_t epoch_s = timeutils_seconds_since_2000( |
| 47 | + tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec |
| 48 | + ); |
| 49 | + rtc_offset = epoch_s - ticks_s; |
| 50 | +} |
| 51 | + |
| 52 | +int common_hal_rtc_get_calibration(void) { |
| 53 | + return 0; |
| 54 | +} |
| 55 | + |
| 56 | +void common_hal_rtc_set_calibration(int calibration) { |
| 57 | + mp_raise_NotImplementedError_varg(translate("%q"), MP_QSTR_calibration); |
| 58 | +} |
0 commit comments