Skip to content

Commit 3361ec3

Browse files
committed
Broadcom port: Add 'fake' RTC support
The Broadcom chips apparently don't have an RTC, so this PR uses the timer to add dummy RTC responses and allow resonable results from time.localtime() as well as setting the time and date while the board is powered.
1 parent 9c06682 commit 3361ec3

File tree

5 files changed

+102
-2
lines changed

5 files changed

+102
-2
lines changed

ports/broadcom/common-hal/rtc/RTC.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 "shared-bindings/rtc/__init__.h"
34+
#include "shared-bindings/rtc/RTC.h"
35+
#include "common-hal/rtc/RTC.h"
36+
#include "supervisor/port.h"
37+
#include "supervisor/shared/translate/translate.h"
38+
39+
void rtc_init(void) {
40+
}
41+
42+
// This is the time in seconds since 2000 that the RTC was started.
43+
// TODO: Change the offset to ticks so that it can be a subsecond adjustment.
44+
static uint32_t rtc_offset = 0;
45+
46+
void common_hal_rtc_get_time(timeutils_struct_time_t *tm) {
47+
uint64_t ticks_s = port_get_raw_ticks(NULL) / 1024;
48+
timeutils_seconds_since_2000_to_struct_time(rtc_offset + ticks_s, tm);
49+
}
50+
51+
void common_hal_rtc_set_time(timeutils_struct_time_t *tm) {
52+
uint64_t ticks_s = port_get_raw_ticks(NULL) / 1024;
53+
uint32_t epoch_s = timeutils_seconds_since_2000(
54+
tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec
55+
);
56+
rtc_offset = epoch_s - ticks_s;
57+
}
58+
59+
int common_hal_rtc_get_calibration(void) {
60+
return 0;
61+
}
62+
63+
void common_hal_rtc_set_calibration(int calibration) {
64+
// SNVS has HPCALB_VAL bits for calibration.
65+
mp_raise_NotImplementedError_varg(translate("%q"), MP_QSTR_calibration);
66+
}

ports/broadcom/common-hal/rtc/RTC.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
* 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+
#ifndef MICROPY_INCLUDED_MIMXRT10XX_COMMON_HAL_RTC_RTC_H
29+
#define MICROPY_INCLUDED_MIMXRT10XX_COMMON_HAL_RTC_RTC_H
30+
31+
extern void rtc_init(void);
32+
extern void rtc_reset(void);
33+
34+
#endif // MICROPY_INCLUDED_MIMXRT10XX_COMMON_HAL_RTC_RTC_H

ports/broadcom/common-hal/rtc/__init__.c

Whitespace-only changes.

ports/broadcom/mpconfigport.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ CIRCUITPY_PARALLELDISPLAY = 0
1515
CIRCUITPY_PULSEIO = 0
1616
CIRCUITPY_PWMIO = 0
1717
CIRCUITPY_ROTARYIO = 0
18-
CIRCUITPY_RTC = 0
18+
CIRCUITPY_RTC = 1
1919

2020
CIRCUITPY_SDIOIO = 1
2121
CIRCUITPY_VIDEOCORE = 1

ports/broadcom/supervisor/port.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
#include "genhdr/mpversion.h"
3434

35-
// #include "common-hal/rtc/RTC.h"
35+
#include "common-hal/rtc/RTC.h"
3636
#include "common-hal/busio/I2C.h"
3737
#include "common-hal/busio/SPI.h"
3838
#include "common-hal/busio/UART.h"

0 commit comments

Comments
 (0)