Skip to content

nrf: Fix non-running LFCLK #13339

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed

Conversation

cwalther
Copy link
Contributor

@cwalther cwalther commented Jan 3, 2024

As discussed in #13062 (post): under some circumstances, after a hard reset (power-on or machine.reset()), the low-frequency clock would not be running. This caused time.ticks_ms() to return 0, time.sleep_ms() to get stuck, and other misbehavior. A soft reboot would return it to a working state.

The cause was a race condition that was hit when the bootloader would itself turn LFCLK on, but turn it off again shortly before launching the main application (this apparently happens with the Adafruit bootloader from https://github.com/fanoush/ds-d6/tree/master/micropython). Stopping the clock is an asynchronous operation and it continues running for a short time after the stop command is given. When MicroPython checked whether to start it by looking at the LFCLKSTAT register (nrf_clock_lf_is_running) during that time, it would mistakenly not be started again. What MicroPython should be looking at is not whether the clock is running at this time, but whether a start/stop command has been given, which is indicated by the LFCLKRUN register (nrf_clock_lf_start_task_status_get). It is not clearly documented, but empirically LFCLKRUN is not just set when the LFCLKSTART task is triggered, but also cleared when the LFCLKSTOP task is triggered, which is exactly what we need.

The matter is complicated by the fact that the nRF52832 has an anomaly (see errata) where starting the LFCLK will not work between 66µs and 138µs after it last stopped. Apply a workaround for that. See nrfx_clock_lfclk_start() in micropython/lib/nrfx/drivers/src/nrfx_clock.c for reference, but I am not using that because it also does other things and makes the code larger.

Tested on a DS-D6 with nRF52832 (where the issue would occur) and on an Adafruit ItsyBitsy nRF52840 (where it would not, but just to check that I didn’t break anything). (By the way, would a board definition for the ItsyBitsy nRF52840 be welcome? I had to make one because it’s the only nRF52840 board I have.)

Copy link

github-actions bot commented Jan 5, 2024

Code size report:

   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:    +0 +0.000% standard
      stm32:    +0 +0.000% PYBV10
     mimxrt:    +0 +0.000% TEENSY40
        rp2:    +0 +0.000% RPI_PICO
       samd:    +0 +0.000% ADAFRUIT_ITSYBITSY_M4_EXPRESS

Copy link

codecov bot commented Jan 5, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.39%. Comparing base (d712feb) to head (e7db610).

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #13339   +/-   ##
=======================================
  Coverage   98.39%   98.39%           
=======================================
  Files         161      161           
  Lines       21078    21078           
=======================================
  Hits        20739    20739           
  Misses        339      339           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

It destroys a few manual alignments, but these seem minor compared to
the benefit of automated code style consistency.

Signed-off-by: Christian Walther <cwalther@gmx.ch>
Under some circumstances, after a hard reset, the low-frequency clock
would not be running. This caused time.ticks_ms() to return 0,
time.sleep_ms() to get stuck, and other misbehavior. A soft reboot would
return it to a working state.

The cause was a race condition that was hit when the bootloader would
itself turn LFCLK on, but turn it off again shortly before launching the
main application (this apparently happens with the Adafruit bootloader
from https://github.com/fanoush/ds-d6/tree/master/micropython). Stopping
the clock is an asynchronous operation and it continues running for a
short time after the stop command is given. When MicroPython checked
whether to start it by looking at the LFCLKSTAT register
(nrf_clock_lf_is_running) during that time, it would mistakenly not be
started again. What MicroPython should be looking at is not whether the
clock is running at this time, but whether a start/stop command has been
given, which is indicated by the LFCLKRUN register
(nrf_clock_lf_start_task_status_get). It is not clearly documented, but
empirically LFCLKRUN is not just set when the LFCLKSTART task is
triggered, but also cleared when the LFCLKSTOP task is triggered, which
is exactly what we need.

The matter is complicated by the fact that the nRF52832 has an anomaly
(see [errata](https://infocenter.nordicsemi.com/topic/errata_nRF52832_Rev3/ERR/nRF52832/Rev3/latest/anomaly_832_132.html?cp=5_2_1_0_1_33))
where starting the LFCLK will not work between 66µs and 138µs after it
last stopped. Apply a workaround for that. See nrfx_clock_lfclk_start()
in micropython/lib/nrfx/drivers/src/nrfx_clock.c for reference, but I am
not using that because it also does other things and makes the code
larger.

Signed-off-by: Christian Walther <cwalther@gmx.ch>
@cwalther
Copy link
Contributor Author

Rebased on master to deal with the STATIC removal, no other changes required.

@dpgeorge
Copy link
Member

Thanks for the LFCLK fix, and the very detailed explanation for why it's needed. It looks good to me, although I did not test it.

Also the code formatting of the machine module code is a good step forward.

Rebased and merged in be89d43 and b10182b

@dpgeorge dpgeorge closed this Mar 26, 2024
@cwalther cwalther deleted the nrf-lfclk branch March 26, 2024 22:31
@cwalther
Copy link
Contributor Author

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants