|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2018 Noralf Trønnes |
| 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 <stdlib.h> |
| 28 | + |
| 29 | +#include <peripheral_clk_config.h> |
| 30 | +#include <hal_init.h> |
| 31 | +#include <hpl_gclk_base.h> |
| 32 | +#include <hpl_pm_base.h> |
| 33 | +#include <hal_calendar.h> |
| 34 | + |
| 35 | +#include "py/obj.h" |
| 36 | +#include "py/runtime.h" |
| 37 | +#include "lib/timeutils/timeutils.h" |
| 38 | +#include "shared-bindings/rtc/__init__.h" |
| 39 | + |
| 40 | +static struct calendar_descriptor calendar; |
| 41 | + |
| 42 | +void rtc_init(void) { |
| 43 | +#ifdef SAMD21 |
| 44 | + _gclk_enable_channel(RTC_GCLK_ID, CONF_GCLK_RTC_SRC); |
| 45 | +#endif |
| 46 | +#ifdef SAMD51 |
| 47 | + hri_mclk_set_APBAMASK_RTC_bit(MCLK); |
| 48 | +#endif |
| 49 | + calendar_init(&calendar, RTC); |
| 50 | + calendar_set_baseyear(&calendar, 2000); |
| 51 | + calendar_enable(&calendar); |
| 52 | +} |
| 53 | + |
| 54 | +void common_hal_rtc_get_time(timeutils_struct_time_t *tm) { |
| 55 | + struct calendar_date_time datetime; |
| 56 | + calendar_get_date_time(&calendar, &datetime); |
| 57 | + |
| 58 | + tm->tm_year = datetime.date.year; |
| 59 | + tm->tm_mon = datetime.date.month; |
| 60 | + tm->tm_mday = datetime.date.day; |
| 61 | + tm->tm_hour = datetime.time.hour; |
| 62 | + tm->tm_min = datetime.time.min; |
| 63 | + tm->tm_sec = datetime.time.sec; |
| 64 | +} |
| 65 | + |
| 66 | +void common_hal_rtc_set_time(timeutils_struct_time_t *tm) { |
| 67 | + struct calendar_date date = { |
| 68 | + .year = tm->tm_year, |
| 69 | + .month = tm->tm_mon, |
| 70 | + .day = tm->tm_mday, |
| 71 | + }; |
| 72 | + calendar_set_date(&calendar, &date); |
| 73 | + |
| 74 | + struct calendar_time time = { |
| 75 | + .hour = tm->tm_hour, |
| 76 | + .min = tm->tm_min, |
| 77 | + .sec = tm->tm_sec, |
| 78 | + }; |
| 79 | + calendar_set_time(&calendar, &time); |
| 80 | +} |
| 81 | + |
| 82 | +// A positive value speeds up the clock by removing clock cycles. |
| 83 | +int common_hal_rtc_get_calibration(void) { |
| 84 | + int calibration = hri_rtcmode0_read_FREQCORR_VALUE_bf(calendar.device.hw); |
| 85 | + |
| 86 | + if (!hri_rtcmode0_get_FREQCORR_SIGN_bit(calendar.device.hw)) |
| 87 | + calibration = -calibration; |
| 88 | + |
| 89 | + return calibration; |
| 90 | +} |
| 91 | + |
| 92 | +void common_hal_rtc_set_calibration(int calibration) { |
| 93 | + if (calibration > 127 || calibration < -127) |
| 94 | + mp_raise_ValueError("calibration value out of range +/-127"); |
| 95 | + |
| 96 | + hri_rtcmode0_write_FREQCORR_SIGN_bit(calendar.device.hw, calibration < 0 ? 0 : 1); |
| 97 | + hri_rtcmode0_write_FREQCORR_VALUE_bf(calendar.device.hw, abs(calibration)); |
| 98 | +} |
0 commit comments