Files
micropython/ports/mimxrt/modmachine.h
Damien George 1b2f6e3d1f mimxrt: Increase resolution of RTC to 1/32768 seconds.
Currently the mimxrt port has a resolution of only 1 second for
`machine.RTC().datetime()` and `time.time_ns()`.  This means (among other
things) that it fails the `tests/extmod/time_time_ns.py` test, which
requires requires at least 5ms of resolution.

The underlying RTC hardware is just a 64-bit counter, and the HAL functions
`SNVS_LP_SRTC_GetDatetime()` and `SNVS_LP_SRTC_SetDatetime()` do
conversions between y/m/d/h/m/s and this 64-bit value, which counts at a
rate of 32kHz.

This commit changes the RTC code to access the 64-bit counter directly and
therefore improve resolution of all RTC functions to 1/32768 seconds.

That makes things much simpler because it a lot of places the code wants to
know the number of seconds since the Epoch.  Currently it uses a
combination of `SNVS_LP_SRTC_GetDatetime()` and
`timeutils_seconds_since_epoch()` which converts the 64-bit counter to
date-time and then back to seconds.  Those operations are computationally
expensive.

With this commit, getting the number of seconds since the Epoch is as
simple as reading the 64-bit counter and dividing by 32768.  We can
leverage a lot of the timeutils functions to simplify everything, and make
it similar to other ports like rp2.

Benefits of this change:
- simpler, more efficient code to get/set RTC
- `machine.RTC().datetime()` now has a non-zero value in the last slot,
  being the number of microseconds, and has a resolution of 1/32768 seconds
- `time.time_ns()` now has a resolution of 1/32768 seconds
- the `test/extmod/time_time_ns.py` test now passes

Signed-off-by: Damien George <damien@micropython.org>
2026-01-27 13:35:23 +11:00

51 lines
2.0 KiB
C

/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2021 Damien P. George
* Copyright (c) 2021 Robert Hammelrath
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef MICROPY_INCLUDED_MIMXRT_MODMACHINE_H
#define MICROPY_INCLUDED_MIMXRT_MODMACHINE_H
#include "py/obj.h"
extern const mp_obj_type_t machine_sdcard_type;
void machine_adc_init(void);
void machine_pin_irq_deinit(void);
void machine_rtc_irq_deinit(void);
void machine_pwm_deinit_all(void);
void machine_uart_deinit_all(void);
void machine_timer_init_PIT(void);
void machine_sdcard_init0(void);
void mimxrt_sdram_init(void);
void machine_i2s_init0();
void machine_i2s_deinit_all(void);
void machine_rtc_start(void);
uint64_t machine_rtc_get_ticks(void);
uint64_t machine_rtc_get_seconds(void);
void machine_rtc_alarm_helper(int seconds, bool repeat);
void machine_uart_set_baudrate(mp_obj_t uart, uint32_t baudrate);
#endif // MICROPY_INCLUDED_MIMXRT_MODMACHINE_H