8000 rp2/machine_rtc: Add initial support for RTC. · micropython/micropython@37d01d4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 37d01d4

Browse files
kadamskidpgeorge
authored andcommitted
rp2/machine_rtc: Add initial support for RTC.
Initial support for machine.RTC on rp2 port. It only supports datetime() method and nothing else. The method gets/returns a tuple of 8 items, just like esp32 port, for example, but the usec parameter is ignored as the RP2 RTC only works up to seconds precision. The Pico RTC isn't very useful as the time is lost during reset and there seems to be no way to easily power up just the RTC clock with a low current voltage, but still there seems to be use-cases for that, see issues #6831, and a Thonny issue #1592. It was also requested for inclusion on v1.15 roadmap on #6832. Signed-off-by: Krzysztof Adamski <k@japko.eu>
1 parent c0499bc commit 37d01d4

File tree

4 files changed

+125
-0
lines change 10000 d

4 files changed

+125
-0
lines changed

ports/rp2/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ set(MICROPY_SOURCE_PORT
8686
machine_i2c.c
8787
machine_pin.c
8888
machine_pwm.c
89+
machine_rtc.c
8990
machine_spi.c
9091
machine_timer.c
9192
machine_uart.c
@@ -113,6 +114,7 @@ set(MICROPY_SOURCE_QSTR
113114
${PROJECT_SOURCE_DIR}/machine_i2c.c
114115
${PROJECT_SOURCE_DIR}/machine_pin.c
115116
${PROJECT_SOURCE_DIR}/machine_pwm.c
117+
${PROJECT_SOURCE_DIR}/machine_rtc.c
116118
${PROJECT_SOURCE_DIR}/machine_spi.c
117119
${PROJECT_SOURCE_DIR}/machine_timer.c
118120
${PROJECT_SOURCE_DIR}/machine_uart.c
@@ -154,6 +156,7 @@ set(PICO_SDK_COMPONENTS
154156
pico_sync
155157
pico_time
156158
pico_unique_id
159+
pico_util
157160
tinyusb_common
158161
tinyusb_device
159162
)

ports/rp2/machine_rtc.c

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2021 "Krzysztof Adamski" <k@japko.eu>
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 <time.h>
31+
#include <sys/time.h>
32+
33+
#include "py/nlr.h"
34+
#include "py/obj.h"
35+
#include "py/runtime.h"
36+
#include "py/mphal.h"
37+
#include "py/mperrno.h"
38+
#include "lib/timeutils/timeutils.h"
39+
#include "hardware/rtc.h"
40+
#include "pico/util/datetime.h"
41+
#include "modmachine.h"
42+
43+
typedef struct _machine_rtc_obj_t {
44+
mp_obj_base_t base;
45+
} machine_rtc_obj_t;
46+
47+
// singleton RTC object
48+
STATIC const machine_rtc_obj_t machine_rtc_obj = {{&machine_rtc_type}};
49+
50+
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) {
51+
// check arguments
52+
mp_arg_check_num(n_args, n_kw, 0, 0, false);
53+
bool r = rtc_running();
54+
55+
if (!r) {
56+
// This shouldn't happen as rtc_init() is already called in main so
57+
// it's here just in case
58+
rtc_init();
59+
datetime_t t = { .month = 1, .day = 1 };
60+
rtc_set_datetime(&t);
61+
}
62+
// return constant object
63+
return (mp_obj_t)&machine_rtc_obj;
64+
}
65+
66+
STATIC mp_obj_t machine_rtc_datetime(mp_uint_t n_args, const mp_obj_t *args) {
67+
if (n_args == 1) {
68+
bool ret;
69+
datetime_t t;
70+
71+
ret = rtc_get_datetime(&t);
72+
if (!ret) {
73+
mp_raise_OSError(MP_EIO);
74+
}
75+
76+
mp_obj_t tuple[8] = {
77+
mp_obj_new_int(t.year),
78+
mp_obj_new_int(t.month),
79+
mp_obj_new_int(t.day),
80+
mp_obj_new_int(t.dotw),
81+
mp_obj_new_int(t.hour),
82+
mp_obj_new_int(t.min),
83+
mp_obj_new_int(t.sec),
84+
mp_obj_new_int(0)
85+
};
86+
87+
return mp_obj_new_tuple(8, tuple);
88+
} else {
89+
mp_obj_t *items;
90+
91+
mp_obj_get_array_fixed_n(args[1], 8, &items);
92+
93+
datetime_t t = {
94+
.year = mp_obj_get_int(items[0]),
95+
.month = mp_obj_get_int(items[1]),
96+
.day = mp_obj_get_int(items[2]),
97+
.dotw = mp_obj_get_int(items[3]),
98+
.hour = mp_obj_get_int(items[4]),
99+
.min = mp_obj_get_int(items[5]),
100+
.sec = mp_obj_get_int(items[6]),
101+
};
102+
103+
rtc_set_datetime(&t);
104+
105+
}
106+
return mp_const_none;
107+
}
108+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_rtc_datetime_obj, 1, 2, machine_rtc_datetime);
109+
110+
STATIC const mp_rom_map_elem_t machine_rtc_locals_dict_table[] = {
111+
{ MP_ROM_QSTR(MP_QSTR_datetime), MP_ROM_PTR(&machine_rtc_datetime_obj) },
112+
};
113+
STATIC MP_DEFINE_CONST_DICT(machine_rtc_locals_dict, machine_rtc_locals_dict_table);
114+
115+
const mp_obj_type_t machine_rtc_type = {
116+
{ &mp_type_type },
117+
.name = MP_QSTR_RTC,
118+
.make_new = machine_rtc_make_new,
119+
.locals_dict = (mp_obj_t)&machine_rtc_locals_dict,
120+
};

ports/rp2/modmachine.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
164164
{ MP_ROM_QSTR(MP_QSTR_SoftI2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) },
165165
{ MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&machine_pin_type) },
166166
{ MP_ROM_QSTR(MP_QSTR_PWM), MP_ROM_PTR(&machine_pwm_type) },
167+
{ MP_ROM_QSTR(MP_QSTR_RTC), MP_ROM_PTR(&machine_rtc_type) },
167168
{ MP_ROM_QSTR(MP_QSTR_Signal), MP_ROM_PTR(&machine_signal_type) },
168169
{ MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&machine_spi_type) },
169170
{ MP_ROM_QSTR(MP_QSTR_SoftSPI), MP_ROM_PTR(&mp_machine_soft_spi_type) },

ports/rp2/modmachine.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ extern const mp_obj_type_t machine_adc_type;
77
extern const mp_obj_type_t machine_hw_i2c_type;
88
extern const mp_obj_type_t machine_pin_type;
99
extern const mp_obj_type_t machine_pwm_type;
10+
extern const mp_obj_type_t machine_rtc_type;
1011
extern const mp_obj_type_t machine_spi_type;
1112
extern const mp_obj_type_t machine_timer_type;
1213
extern const mp_obj_type_t machine_uart_type;

0 commit comments

Comments
 (0)
0