Skip to content
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

Clock disabling code looks wrong #30

Open
denisbaylor opened this issue May 1, 2020 · 6 comments · May be fixed by #36
Open

Clock disabling code looks wrong #30

denisbaylor opened this issue May 1, 2020 · 6 comments · May be fixed by #36
Labels
topic: code Related to content of the project itself type: imperfection Perceived defect in any part of project

Comments

@denisbaylor
Copy link

GCLK->CLKCTRL.bit.CLKEN = 0; //disable GCLK module

The comment at this line says "disable GCLK module". But my reading of the somewhat ambiguous documentation of the CLKCTRL register is different.

I believe each generic clock id has its own enable (although I don't see this spelled out clearly in the documentation). The documentation for CLKCTRL says:

This register allows the user to configure one of the generic clocks, as specified in the CLKCTRL.ID bit group. To write to the CLKCTRL register, do a 16-bit write with all configurations and the ID.

To read the CLKCTRL register, first do an 8-bit write to the CLKCTRL.ID bit group with the ID of the generic clock whose configuration is to be read, and then read the CLKCTRL register.

I believe what line 9 is actually doing (via a forbidden 8-bit write), is setting the enable to 0, setting the GEN field to 0 (which means select generic clock generator 0) and then applying these changes to the ID field that last happened to be set. So it's essentially disabling and corrupting the clock source for the most recently configured clock generator ID. Things will work ok if you don't intermingle this low power code with other libraries that configure clocks. In my case, trying to add an interrupt between calls to other library code (Adafruit_SSD1306) caused that other code to hang. I believe the reason that code hangs is that the clocks to SERCOM2 had been disabled by this bug.

As I understand things, this line should be:

  GCLK->CLKCTRL.reg =
      (uint16_t)(GCLK_CLKCTRL_ID(GCM_EIC));  // Disable EIC clock (also sets
                                             // GCLK_CTRL_CLKEN bit to 0)
@PhilGoody
Copy link

I am having a similar problem using this library with a FEATHER M0/RFM69 BOARD ADAFRUIT PID:3176 for RTC and pin wakeups. The radio tries to send one packet and fails then the command for sleep(2000) is executed and the the board never wakes up again. Have you tried your suggested change to the library?

@denisbaylor
Copy link
Author

Yes. My change does fix the problems I was having.

@PhilGoody
Copy link

Thanks, it fixed my RFM69 problem too although Serial still stops working which, I guess, is a separate issue.

@aalbinati
Copy link

aalbinati commented Jan 3, 2021

You saved me! Thanks!!! Tha arduino MKR 1310 can't work with an ADCInterrupt and a timed sleep at the sime time. But this fixes it. I'll make a pull request

aalbinati added a commit to aalbinati/ArduinoLowPower that referenced this issue Jan 3, 2021
@aalbinati
Copy link

aalbinati commented Jan 3, 2021

@denisbaylor @PhilGoody
I made a pull #request. Please be kind to test it and support it for it to be included in the master branch!

@per1234 per1234 linked a pull request Jun 14, 2022 that will close this issue
@per1234 per1234 added type: imperfection Perceived defect in any part of project topic: code Related to content of the project itself labels Jun 14, 2022
@jremington
Copy link

jremington commented Jun 8, 2024

Fixed my problem with the Feather M0 LoRa radio, too. Now sleep modes can be implemented for the SAMD21 in the RadioHead library! For the details see https://forums.adafruit.com/viewtopic.php?t=211308

It is very sad that the library maintainer(s) can't be bothered commit this simple fix to a serious error in the library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: code Related to content of the project itself type: imperfection Perceived defect in any part of project
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants