Skip to content

Commit ec92bcf

Browse files
iabdalkaderdpgeorge
authored andcommitted
alif/machine_rtc: Add basic machine.RTC support.
Signed-off-by: iabdalkader <[email protected]>
1 parent 280e6e2 commit ec92bcf

File tree

4 files changed

+112
-0
lines changed

4 files changed

+112
-0
lines changed

ports/alif/alif.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ SRC_C = \
119119
machine_pin.c \
120120
machine_i2c.c \
121121
machine_spi.c \
122+
machine_rtc.c \
122123
main.c \
123124
modalif.c \
124125
mphalport.c \

ports/alif/irq.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#define IRQ_PRI_USB NVIC_EncodePriority(NVIC_PRIORITYGROUP_7, 7, 0)
4949
#define IRQ_PRI_HWSEM NVIC_EncodePriority(NVIC_PRIORITYGROUP_7, 8, 0)
5050
#define IRQ_PRI_GPU NVIC_EncodePriority(NVIC_PRIORITYGROUP_7, 10, 0)
51+
#define IRQ_PRI_RTC NVIC_EncodePriority(NVIC_PRIORITYGROUP_7, 100, 0)
5152
#define IRQ_PRI_PENDSV NVIC_EncodePriority(NVIC_PRIORITYGROUP_7, 127, 0)
5253

5354
// these states correspond to values from query_irq, enable_irq and disable_irq

ports/alif/machine_rtc.c

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2024-2025 OpenMV LLC.
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 "py/runtime.h"
28+
#include "py/mphal.h"
29+
#include "py/mperrno.h"
30+
#include "extmod/modmachine.h"
31+
#include "rtc.h"
32+
#include "sys_ctrl_rtc.h"
33+
34+
typedef struct _machine_rtc_obj_t {
35+
mp_obj_base_t base;
36+
LPRTC_Type *rtc;
37+
} machine_rtc_obj_t;
38+
39+
// Singleton RTC object.
40+
static const machine_rtc_obj_t machine_rtc = {{&machine_rtc_type}, (LPRTC_Type *)LPRTC_BASE};
41+
42+
void LPRTC_IRQHandler(void) {
43+
lprtc_interrupt_ack(machine_rtc.rtc);
44+
lprtc_interrupt_disable(machine_rtc.rtc);
45+
}
46+
47+
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) {
48+
const machine_rtc_obj_t *self = &machine_rtc;
49+
50+
// Check arguments.
51+
mp_arg_check_num(n_args, n_kw, 0, 0, false);
52+
53+
enable_lprtc_clk();
54+
lprtc_prescaler_disable(self->rtc);
55+
lprtc_counter_wrap_disable(self->rtc);
56+
lprtc_interrupt_disable(self->rtc);
57+
lprtc_interrupt_unmask(self->rtc);
58+
59+
NVIC_SetPriority(LPRTC_IRQ_IRQn, IRQ_PRI_RTC);
60+
NVIC_ClearPendingIRQ(LPRTC_IRQ_IRQn);
61+
NVIC_EnableIRQ(LPRTC_IRQ_IRQn);
62+
return MP_OBJ_FROM_PTR(self);
63+
}
64+
65+
static mp_obj_t machine_rtc_alarm(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
66+
enum { ARG_id, ARG_time, ARG_repeat };
67+
68+
static const mp_arg_t allowed_args[] = {
69+
{ MP_QSTR_id, MP_ARG_INT, {.u_int = 0} },
70+
{ MP_QSTR_time, MP_ARG_OBJ, {.u_obj = mp_const_none} },
71+
{ MP_QSTR_repeat, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
72+
};
73+
74+
machine_rtc_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
75+
76+
// Parse args.
77+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
78+
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(args), allowed_args, args);
79+
80+
if (mp_obj_is_int(args[ARG_time].u_obj)) {
81+
uint32_t seconds = mp_obj_get_int(args[1].u_obj) / 1000;
82+
83+
lprtc_counter_disable(self->rtc);
84+
lprtc_load_count(self->rtc, 1);
85+
lprtc_load_counter_match_register(self->rtc, seconds * 32768);
86+
87+
lprtc_interrupt_ack(self->rtc);
88+
lprtc_interrupt_enable(self->rtc);
89+
lprtc_counter_enable(self->rtc);
90+
} else {
91+
mp_raise_ValueError(MP_ERROR_TEXT("invalid argument(s)"));
92+
}
93+
94+
return mp_const_none;
95+
}
96+
static MP_DEFINE_CONST_FUN_OBJ_KW(machine_rtc_alarm_obj, 1, machine_rtc_alarm);
97+
98+
static const mp_rom_map_elem_t machine_rtc_locals_dict_table[] = {
99+
{ MP_ROM_QSTR(MP_QSTR_alarm), MP_ROM_PTR(&machine_rtc_alarm_obj) },
100+
};
101+
static MP_DEFINE_CONST_DICT(machine_rtc_locals_dict, machine_rtc_locals_dict_table);
102+
103+
MP_DEFINE_CONST_OBJ_TYPE(
104+
machine_rtc_type,
105+
MP_QSTR_RTC,
106+
MP_TYPE_FLAG_NONE,
107+
make_new, machine_rtc_make_new,
108+
locals_dict, &machine_rtc_locals_dict
109+
);

ports/alif/modmachine.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ extern void dcd_uninit(void);
3535
#define MICROPY_PY_MACHINE_EXTRA_GLOBALS \
3636
{ MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&machine_pin_type) }, \
3737
{ MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&machine_timer_type) }, \
38+
{ MP_ROM_QSTR(MP_QSTR_RTC), MP_ROM_PTR(&machine_rtc_type) }, \
3839

3940
static void mp_machine_idle(void) {
4041
mp_event_wait_indefinite();

0 commit comments

Comments
 (0)