mimxrt/machine_rtc: Enable rtc.alarm for RT1176 and fix deepsleep. by robert-hh · Pull Request #19641 · micropython/micropython · GitHub
Skip to content

mimxrt/machine_rtc: Enable rtc.alarm for RT1176 and fix deepsleep. - #19641

Open
robert-hh wants to merge 3 commits into
micropython:masterfrom
robert-hh:mimxrt_alarm
Open

robert-hh wants to merge 3 commits into
micropython:masterfrom
robert-hh:mimxrt_alarm

Conversation

@robert-hh

Copy link
Copy Markdown
Contributor

Summary

machine.rtc.alarm() did not work with the RT1176 MCU. The reason was, that the proper IRQ handler was not enabled. Since machine.deepsleep(timeout) uses mechanism of rtc alarm, it did not work either.

This PR also changes machine.deepsleep() without timeout for the RT1176 MCU. This option which should allow to wake up the MCU from deepsleep with a low pulse at the WAKEUP pin was not reliable. When used, it worked once, and then both deepsleep variants failed until the next power cycle. It seems to be a hardware issue. To avoid the unexpected behavior, the wakeup by the WAKEUP pin for machine.deepsleep() is disabled now for the RT1176. It is still possible to wakeup the device from deepsleep with a >1 sec long low pulse at the ONOFF pin. That works for MIMXRT10xx devices as well.

Testing

Tested with MIMXRT1020EVK, Seeed Arch Mix, Teensy 4.1, OpenMV RT1062, MIMXRT1170EVK.

Generative AI

I did not use generative AI tools when creating this PR.

@robert-hh

Copy link
Copy Markdown
Contributor Author

@github-actions

github-actions Bot commented Aug 20, 2026

Copy link
Copy Markdown

Code size report:

Reference:  rp2/boards: Extend SEEED_XIAO_RP2040 to support new Plus boards. [8cb7558]
Comparison: mimxrt/modmachine: Fix machine.deepsleep(ms) for the MIMXRT1176 port. [merge of 337dc46]
  mpy-cross:    +0 +0.000% 
   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:    +0 +0.000% standard
      stm32:    +0 +0.000% PYBV10
      esp32:    +0 +0.000% ESP32_GENERIC
     mimxrt:    +8 +0.002% TEENSY40
        rp2:    +0 +0.000% RPI_PICO_W
       samd:    +0 +0.000% ADAFRUIT_ITSYBITSY_M4_EXPRESS
  qemu rv32:    +0 +0.000% VIRT_RV32

Comment thread ports/mimxrt/machine_rtc.c Outdated
};

void SNVS_HP_WRAPPER_IRQHandler(void) {
#if defined(MIMXRT117x_SERIES)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest matching this with the IRQn macro setting that is configured similarly in 2 other places in this file, to make sure they stay in sync.

Eg at the top of this file do:

#ifdef MIMXRT117x_SERIES
#define RTC_IRQ_NUMBER SNVS_HP_NON_TZ_IRQn
#define RTC_IRQ_HANDLER SNVS_HP_NON_TZ_IRQHandler
#else
#define RTC_IRQ_NUMBER SNVS_HP_WRAPPER_IRQn
#define RTC_IRQ_HANDLER SNVS_HP_WRAPPER_IRQHandler
#endif

And then use those macros as appropriate.

Comment thread ports/mimxrt/modmachine.c
MP_NORETURN static void mp_machine_deepsleep(size_t n_args, const mp_obj_t *args) {

// Stop a potentially active alarm.
machine_rtc_alarm_off(true);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really necessary? I think it changes valid behaviour, eg if code manually sets an RTC alarm with the intention that it wakes the device from deepsleep, and then calls machine.deepsleep() (no args), that should continue to function (ie wake based on the manually-set alarm).

Maybe instead this line should go down inside the if (n_args != 0) { block? So that it resets the alarm before trying to set it.

Comment thread ports/mimxrt/modmachine.c Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that the pin is pull-up, should it be PIN_MODE_IT_FALLING?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed, even if it does not matter. By hardware it wakes up with a low level at the wakeup pin, no matter how it's set.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, right, so this argument is essentially unused for the wakeup pin.

Well, at least it's now matching the MIMXRT117x_SERIES case above.

Use the RTC irq handler to call the handler if required. Defining
the proper IRQ handler for the IRQ table was missing.

Signed-off-by: robert-hh <robert@hammelrath.com>
That makes it available for the code and it is used
by machine.deepsleep().

Signed-off-by: robert-hh <robert@hammelrath.com>
machine.deepsleep(ms) works now as intended by powering down the board
for ms/1000 seconds and then resetting it. That can be repeated several
times.

For MIMXRT10xx MCUs machine.deepsleep() works as intended. The WAKEUP
pin pull-up was enabled and set to 100k to ensure a high/low transition.

machine.deepsleep() does not work as intended for mimxrt1176. It can be
called once and recover from deepsleep by pulling the WAKEUP_DIG pin
low, but then attempts to call machine.deepsleep with or without a
timeout fail until a power cycle is done. So waking up from deepsleep
using the WAKEUP_DIG pin is not enabled. It is however possible to
wakeup the device with >1 sec low pulse at the ONOFF pin.

Signed-off-by: robert-hh <robert@hammelrath.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants