Skip to content

Commit 4191cec

Browse files
committed
alif/machine_rtc: Factor out RTC helper functions.
Signed-off-by: Damien George <damien@micropython.org>
1 parent 04bf835 commit 4191cec

3 files changed

Lines changed: 68 additions & 25 deletions

File tree

ports/alif/machine_rtc.c

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "py/mperrno.h"
3030
#include "extmod/modmachine.h"
3131
#include "shared/timeutils/timeutils.h"
32+
#include "modmachine.h"
3233
#include "rtc.h"
3334
#include "sys_ctrl_rtc.h"
3435

@@ -55,6 +56,27 @@ void LPRTC_IRQHandler(void) {
5556
lprtc_interrupt_disable(machine_rtc.rtc);
5657
}
5758

59+
void machine_rtc_init(void) {
60+
lprtc_interrupt_ack(machine_rtc.rtc);
61+
lprtc_interrupt_disable(machine_rtc.rtc);
62+
lprtc_interrupt_unmask(machine_rtc.rtc);
63+
64+
// Initialise the LPRTC if it's not already enabled.
65+
if (!((VBAT->RTC_CLK_EN & RTC_CLK_ENABLE)
66+
&& (machine_rtc.rtc->LPRTC_CCR & CCR_LPRTC_EN)
67+
&& (machine_rtc.rtc->LPRTC_CPSR == LPRTC_PRESCALER_SETTING))) {
68+
enable_lprtc_clk();
69+
machine_rtc.rtc->LPRTC_CCR = 0;
70+
lprtc_load_prescaler(machine_rtc.rtc, LPRTC_PRESCALER_SETTING);
71+
lprtc_load_count(machine_rtc.rtc, 0);
72+
machine_rtc.rtc->LPRTC_CCR = CCR_LPRTC_PSCLR_EN | CCR_LPRTC_EN;
73+
}
74+
75+
NVIC_SetPriority(LPRTC_IRQ_IRQn, IRQ_PRI_RTC);
76+
NVIC_ClearPendingIRQ(LPRTC_IRQ_IRQn);
77+
NVIC_EnableIRQ(LPRTC_IRQ_IRQn);
78+
}
79+
5880
// Returns the number of seconds and microseconds since the Epoch.
5981
uint32_t mp_hal_time_get(uint32_t *microseconds) {
6082
uint32_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION();
@@ -84,25 +106,18 @@ static mp_obj_t machine_rtc_make_new(const mp_obj_type_t *type, size_t n_args, s
84106
// Check arguments.
85107
mp_arg_check_num(n_args, n_kw, 0, 0, false);
86108

87-
lprtc_interrupt_disable(self->rtc);
88-
lprtc_interrupt_unmask(self->rtc);
109+
return MP_OBJ_FROM_PTR(self);
110+
}
89111

90-
// Initialise the LPRTC if it's not already enabled.
91-
if (!((VBAT->RTC_CLK_EN & RTC_CLK_ENABLE)
92-
&& (self->rtc->LPRTC_CCR & CCR_LPRTC_EN)
93-
&& (self->rtc->LPRTC_CPSR == LPRTC_PRESCALER_SETTING))) {
94-
enable_lprtc_clk();
95-
self->rtc->LPRTC_CCR = 0;
96-
lprtc_load_prescaler(self->rtc, LPRTC_PRESCALER_SETTING);
97-
lprtc_load_count(self->rtc, 0);
98-
self->rtc->LPRTC_CCR = CCR_LPRTC_PSCLR_EN | CCR_LPRTC_EN;
99-
}
112+
void machine_rtc_set_wakeup(uint32_t seconds) {
113+
LPRTC_Type *rtc = (LPRTC_Type *)LPRTC_BASE;
100114

101-
NVIC_SetPriority(LPRTC_IRQ_IRQn, IRQ_PRI_RTC);
102-
NVIC_ClearPendingIRQ(LPRTC_IRQ_IRQn);
103-
NVIC_EnableIRQ(LPRTC_IRQ_IRQn);
104-
105-
return MP_OBJ_FROM_PTR(self);
115+
// Configure the counter match as atomically as possible.
116+
uint32_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION();
117+
lprtc_interrupt_ack(rtc);
118+
lprtc_load_counter_match_register(rtc, lprtc_get_count(rtc) + seconds);
119+
lprtc_interrupt_enable(rtc);
120+
MICROPY_END_ATOMIC_SECTION(atomic_state);
106121
}
107122

108123
static mp_obj_t machine_rtc_datetime(mp_uint_t n_args, const mp_obj_t *args) {
@@ -155,8 +170,6 @@ static mp_obj_t machine_rtc_alarm(size_t n_args, const mp_obj_t *pos_args, mp_ma
155170
{ MP_QSTR_repeat, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
156171
};
157172

158-
machine_rtc_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
159-
160173
// Parse args.
161174
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
162175
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(args), allowed_args, args);
@@ -170,12 +183,7 @@ static mp_obj_t machine_rtc_alarm(size_t n_args, const mp_obj_t *pos_args, mp_ma
170183
// - if seconds >= 2 then it will always fire (when read/written close enough)
171184
seconds = MAX(2, seconds);
172185

173-
// Configure the counter match as atomically as possible.
174-
uint32_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION();
175-
lprtc_interrupt_ack(self->rtc);
176-
lprtc_load_counter_match_register(self->rtc, lprtc_get_count(self->rtc) + seconds);
177-
lprtc_interrupt_enable(self->rtc);
178-
MICROPY_END_ATOMIC_SECTION(atomic_state);
186+
machine_rtc_set_wakeup(seconds);
179187
} else {
180188
mp_raise_ValueError(MP_ERROR_TEXT("invalid argument(s)"));
181189
}

ports/alif/main.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "shared/runtime/softtimer.h"
3939
#include "shared/tinyusb/mp_usbd.h"
4040
#include "tusb.h"
41+
#include "modmachine.h"
4142
#include "mpbthciport.h"
4243
#include "mpuart.h"
4344
#include "ospi_flash.h"
@@ -77,6 +78,8 @@ int main(void) {
7778

7879
MICROPY_BOARD_EARLY_INIT();
7980

81+
machine_rtc_init();
82+
8083
#if MICROPY_HW_ENABLE_UART_REPL
8184
mp_uart_init_repl();
8285
#endif

ports/alif/modmachine.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2026 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+
#ifndef MICROPY_INCLUDED_ALIF_MODMACHINE_H
27+
#define MICROPY_INCLUDED_ALIF_MODMACHINE_H
28+
29+
void machine_rtc_init(void);
30+
void machine_rtc_set_wakeup(uint32_t seconds);
31+
32+
#endif // MICROPY_INCLUDED_ALIF_MODMACHINE_H

0 commit comments

Comments
 (0)