-
-
Notifications
You must be signed in to change notification settings - Fork 15
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
Store MCUSR into R2 on bootup #2
Conversation
this makes it possible to find the MCU reset caused in run code. because the MCUSR is being erased by the bootloader.
Thank you for this PR. Good idea. The register is only saved on controllers with the MCUSR register. I think it should handle MCUCSR as well. Maybe there should be an option in |
If you can wait a little longer I'll make some more adjustments.
ETA: I think for Friday (I'm still busy with other things). |
Sounds good. Take all the time you need. :-) |
ok:
Niet: Todo: option in config.h |
default: do not store
is there an alternative to the GPIOR0 ?
should we use the RAM? |
I've took a look at how Optiboot (the Arduino bootloader) handles this. Maybe we should adopt this and store the original value of MCUSR/MCUCSR in R2 too? |
This sketch demonstrates retrival of the Reset Cause register: |
I think we should use the R2 register instead of GPIOR0 to store the information and maybe add some description in the readme (after the "CAN bus communication" description). |
yes it would, I can't test the bootloader code for the moment. And a test.=> my problem. |
Unfortunately, I don't have much time for this at the moment . Maybe next week or so. __asm__ __volatile__ (" mov r2, %0\n" :: "r" (MCUSR)); and for controllers with MCUCSR: __asm__ __volatile__ (" mov r2, %0\n" :: "r" (MCUCSR)); |
code bootloader
code main prog.
|
looks good let's give it a try |
for de test : |
first test |
when updating firmware=> overwrites the R2 register |
What do you mean with "when updating firmware"? |
yes that works, but if there is a firmware update the R2 register will be overwritten
MCUSR == random |
Sorry for the late response... Maybe we need to store MCUSR into a variable first and then set R2 in the startApp function right before the gotoApp call? |
Funny thing, I tried that but I couldn't get it right. my asm is probably wrong. |
and i also tried this: |
what would help: if i could see the compiler asm (*.S file's) but i can't do it for platformio :/ |
You can add the following to the
|
thanks got it working. |
Nice, but why are you moving the registers so often? #if MCUSR_TO_R2
uint8_t mcusr = 0;
#endif
// [...]
void get_mcusr(void) {
// [...]
#if MCUSR_TO_R2
mcusr = MCUSR;
#endif
MCUSR = 0;
// [...] and add the move to R2 in void startApp () {
// [...]
#if MCUSR_TO_R2
__asm__ __volatile__(" mov r2, %0\n" ::"r"(mcusr));
#endif
// jump to main application
gotoApp();
}
|
yes i tried that too see: when bootloading 3: mov r2, reset_caused_by 2 times when starting normal => 2 *1 Clocks = 2 note: http://ww1.microchip.com/downloads/en/Appnotes/doc1497.pdf
so if you read and write a Global variable you are at 10 Cycles min. |
Thank you for your explanation. Now it makes sense to me. :-) One problem... the part #ifndef MCUSR // Backward compatability with old AVRs
#define MCUSR MCUCSR
#endif won't work for ATmega128 and throw an error:
From /* MCU Status Register */
#define MCUSR _SFR_IO8(0x34)
#define MCUCSR _SFR_IO8(0x34) /* new name in datasheet (2467E-AVR-05/02) */
// [...]
#pragma GCC poison MCUSR I've updated the GitHub workflow to run the check action on pull requests too. So if you push something new, all check should be done automatically. |
there is one more thing I want to test, this is the code in the normal program
wat als we call test Watchdog, ok got Watchdog reset from MCU Status |
Looks like this feature is ready to be merged, right? |
yes, it works stable here, note: Brown-out reset from MCU Status works too. (my breadboard will start to fail) :/ |
👍 Thank you for your contribution! I'll merge this PR.
This may be done by adding a simple config option. I'll have a look at it tomorrow. |
this makes it possible to find the MCU reset caused in run code.
because the MCUSR is being erased by the bootloader.