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

atmega328p/startup.c: Clear watchdog flag #6837

Closed
wants to merge 1 commit into from

Conversation

zqad
Copy link

@zqad zqad commented Mar 31, 2017

When the atmega328p is restarted by the watchdog, the WDRF bit in
the MCUSR register will also set the WDE bit, enabling the
watchdog. Since we use the watchdog for reboot (see
cpu/atmega_common/periph/pm.c), this means that the CPU will just
loop-reboot when a reboot is issued.

From the Atmel documentation about WDE in WDTCSR:
"WDE is overridden by WDRF in MCUSR. This means that WDE is
always set when WDRF is set. To clear WDE, WDRF must be cleared
first. This feature ensures multiple resets during conditions
causing failure, and a safe start-up after the failure."

When the atmega328p is restarted by the watchdog, the WDRF bit in
the MCUSR register will also set the WDE bit, enabling the
watchdog. Since we use the watchdog for reboot (see
cpu/atmega_common/periph/pm.c), this means that the CPU will just
loop-reboot when a reboot is issued.

From the Atmel documentation about WDE in WDTCSR:
  "WDE is overridden by WDRF in MCUSR. This means that WDE is
  always set when WDRF is set. To clear WDE, WDRF must be cleared
  first. This feature ensures multiple resets during conditions
  causing failure, and a safe start-up after the failure."
@mali
Copy link
Contributor

mali commented Apr 10, 2017

I need to investigate more, because I can't reproduce the issue described in #6836.

@zqad
Copy link
Author

zqad commented Apr 12, 2017

That is strange, I'm still seeing this. And the documentation seems to indicate that the patch is needed. Might it be a revision thing?
For full disclosure: I'm using a 5V Arduino Mini Pro which is as similar to the Uno as they come, at least processor-wise.

@zqad
Copy link
Author

zqad commented Apr 13, 2017

..one thing just hit me. Are you running it with or without the Arduino bootloader? I'm running without it, so it might be that it clears the bit if the board was reset using the watchdog.
I'm not sure how RIOT works when running in "Arduino-mode", if it uses the bootloader or not..

@mali
Copy link
Contributor

mali commented Apr 14, 2017

@zqad : Yes I'm using arduino bootloader, indeed, that should be related.
thank's for mentionning this.

@mali
Copy link
Contributor

mali commented Jun 8, 2017

For now I'm unable to build Riot due to obsolete gcc toolchain. I'm waiting for the new debian stable which should be there soon.

@aabadie aabadie modified the milestone: Release 2017.07 Jun 26, 2017
@mali
Copy link
Contributor

mali commented Jun 27, 2017

Ok, so here are the results of my tests.

  • arduino uno : no problem found, with or without the patch.
  • arduino duemilanove : problem is here but is not corrected by this patch, the board is bricked in the two cases and go in infernal loop-reboot. Using another board as ISP is needed to recover.

@mali
Copy link
Contributor

mali commented Jun 28, 2017

Got something to track down, I've found this piece of code in arduino bootloader which can explain why the board go in loop-reboot

#ifdef WATCHDOG_MODS
ch = MCUSR;
MCUSR = 0;

WDTCSR |= _BV(WDCE) | _BV(WDE);
WDTCSR = 0;

// Check if the WDT was used to reset, in which case we dont bootload and skip
straight to the code. woot.

if (! (ch & _BV(EXTRF))) // if its a not an external reset...
app_start(); // skip bootloader
#else
asm volatile("nop\n\t");
#endif

But this said, I should have had hand on board while pressing reset button which was not the case ...

@miri64 miri64 added Platform: AVR Platform: This PR/issue effects AVR-based platforms Type: bug The issue reports a bug / The PR fixes a bug (including spelling errors) labels Oct 28, 2017
@kaspar030
Copy link
Contributor

any news on this?

@ZetaR60
Copy link
Contributor

ZetaR60 commented Apr 15, 2018

The ATmega1284P has this reboot-loop issue as well. Tested on BOARD=mega-xplained. Placing the watchdog reset code at the beginning of board_init in boards/common/atmega/board.c fixes the reboot-loops.

@kaspar030 kaspar030 removed this from the Release 2018.04 milestone Apr 22, 2018
Copy link
Contributor

@ZetaR60 ZetaR60 left a comment

Choose a reason for hiding this comment

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

Other ATmegas have the reboot-loop issue. I suggest moving this to boards/common/atmega/board.c

@ZetaR60
Copy link
Contributor

ZetaR60 commented May 7, 2018

Ping @zqad

@ZetaR60
Copy link
Contributor

ZetaR60 commented May 15, 2018

Updated PR that fixes all ATmega boards: #9141

@kYc0o kYc0o added this to the Release 2018.10 milestone Jul 17, 2018
@PeterKietzmann
Copy link
Member

@ZetaR60 can this one be closed in favor of #9141?

@ZetaR60
Copy link
Contributor

ZetaR60 commented Jul 31, 2018

@PeterKietzmann It should be okay to close. #9141 is not perfect (does not work on the Duemilanove, for unknown reasons), but it is a more comprehensive fix than this PR. Closing.

@ZetaR60 ZetaR60 closed this Jul 31, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Platform: AVR Platform: This PR/issue effects AVR-based platforms Type: bug The issue reports a bug / The PR fixes a bug (including spelling errors)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants