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

boards/common/atmega: initialize watchdog #9141

Closed
wants to merge 1 commit into from

Conversation

ZetaR60
Copy link
Contributor

@ZetaR60 ZetaR60 commented May 15, 2018

This initializes the watchdog on ATmega boards if a watchdog reset occurred, preventing reboot loops when using the reboot command.

Adapted from #6837

Fixes #6836

@ZetaR60
Copy link
Contributor Author

ZetaR60 commented May 22, 2018

Note: @mali reports that this PR does not fix #6836 on some boards, for unknown reasons.

Copy link
Contributor

@mali mali left a comment

Choose a reason for hiding this comment

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

As per the datasheet, "the application software should always clear the WDRF and the WDE control bit in the initialization routine"and interrupt should be disabled while doing this. So I suggest 'WDTCSR |= 1 << WDCE | 1 << WDE;' and starting by irq_disable() and then irq_enable() before leaving

@ZetaR60
Copy link
Contributor Author

ZetaR60 commented May 23, 2018

If I am not mistaken, interrupts are enabled for the first time only after atmega_wdog_init is being run (at the end of board_init).

@mali
Copy link
Contributor

mali commented May 23, 2018

@ZetaR60 , you're right about interrupts. Any thoughts about the WDE bit ?

@ZetaR60
Copy link
Contributor Author

ZetaR60 commented May 23, 2018

@mali WDE should be cleared when all of WDTCSR is set to 0.

I have an idea: What if some versions of the Arduino bootloader are clearing MCUSR / WDRF, but not WDTCSR ? The atmega_wdog_init function would do nothing (because it does not know the board was watchdog reset), but it would still have the watchdog set to reboot again. Could you try #9141 again, but with the condition removed (so that atmega_wdog_init always clears the watchdog no matter what)?

@mali
Copy link
Contributor

mali commented May 23, 2018

Good idea but sorry, same result :-/
Honestly, I'd say ... nevermind the duemilanove, at least these PR doesn't break anything, so if it fix problems on other boards I'm ok with that.

@ZetaR60 ZetaR60 added Type: bug The issue reports a bug / The PR fixes a bug (including spelling errors) Platform: AVR Platform: This PR/issue effects AVR-based platforms labels Jun 14, 2018
@ZetaR60 ZetaR60 requested a review from kYc0o June 20, 2018 22:57
@ZetaR60 ZetaR60 added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Jun 20, 2018
@Josar
Copy link
Contributor

Josar commented Jun 22, 2018

As there is a problem with the watchdog beeing set to the shortest time after reset in some atmega MCU the WDT has to be reseted as fast as possible. Thus in the bootloader.

https://www.microchip.com/webdoc/AVRLibcReferenceManual/group__avr__watchdog.html

But the application is then not able to find out the reset cause. As for the atmegas the WDT is used for softreset this can not be distinguished. So we implemented a solution for that in the Jiminy bootloader.

https://github.com/Josar/arduino_stk500v2/blob/da56ca393e89ef59696b7de77a5d7c03c772af82/stk500boot.c#L560

And the atmega256rfr2
https://github.com/Josar/RIOT/blob/f1e5fccc728396c4d30a075a4a838af79f90b1c6/cpu/atmega256rfr2/cpu.c#L30

And the reboot command.
https://github.com/Josar/RIOT/blob/f1e5fccc728396c4d30a075a4a838af79f90b1c6/cpu/atmega_common/periph/pm.c#L33

My proposal would be to clear the WDT in __attribute__((section(".init0")));.
This could be adapted for all atmegas like follows, With no bootloader just save MCUSR and R3 to signlize softreset, With bootloader save R2 and R3. Use the values later to distinguish reset cause and softreset.

Related arduino bootloader code shows that MCUSR gets cleared

https://github.com/arduino/ArduinoCore-avr/blob/b7c607663fecc232e598f2c0acf419ceb0b7078c/bootloaders/atmega/ATmegaBOOT_168.c#L282

https://github.com/arduino/ArduinoCore-avr/blob/b7c607663fecc232e598f2c0acf419ceb0b7078c/bootloaders/stk500v2/stk500boot.c#L559

@ZetaR60
Copy link
Contributor Author

ZetaR60 commented Jun 22, 2018

@Josar I am not very familiar with the section attribute and how this is modifying the startup process. Could you submit an alternate PR so that we could test the method as you intend it to be written? I think that since there is no established API for getting the reset cause it should be a secondary priority (unless a specific board needs it, like Jiminy), and the main goal should be getting all CPUs to reset properly.

@ZetaR60
Copy link
Contributor Author

ZetaR60 commented Jul 2, 2018

ping @Josar

@Josar
Copy link
Contributor

Josar commented Jul 3, 2018

@ZetaR60 i will have a look into it. Maybe after #9130 was merged as this will place all CPU depended stuff in one place and then the init section can be in one place for all CPUs.

@kYc0o kYc0o added this to the Release 2018.10 milestone Jul 17, 2018
@kYc0o kYc0o self-assigned this Jul 17, 2018
@ZetaR60
Copy link
Contributor Author

ZetaR60 commented Nov 4, 2018

Fixed by #9866. Closing.

@ZetaR60 ZetaR60 closed this Nov 4, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR 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.

5 participants