View previous topic :: View next topic |
Author |
Message |
Pzx
Joined: 05 May 2015 Posts: 39
|
Posted: Thu Aug 29, 2019 8:54 am Post subject: SRAM preserve after reset ATMEGA2560 |
|
|
Is it possible to preserve SRAM data after watchdog reset in ATMEGA2560?
Some users claim that this is possible after watchdog reset and not possible after hardware reset.
see the link:
https://www.avrfreaks.net/forum/sram-after-reset
I tryed with a simple program:
Config Watchdog = 1024
Start Watchdog
dim x as word
do
x=x+1
Lcdat 1 , 1 , "X=" ; X ; " "
loop
end
Watchdog resets the micro every 1 second, but value of X is not preserved, is always starts from 0.
What is your experience with SRAM and watchdog reset?
(BASCOM-AVR version : 2.0.8.2 ) |
|
Back to top |
|
|
Evert :-)
Joined: 18 Feb 2005 Posts: 2156
|
|
Back to top |
|
|
laborratte
Joined: 27 Jul 2005 Posts: 299 Location: Berlin
|
Posted: Thu Aug 29, 2019 9:14 am Post subject: |
|
|
see the $NORAMCLEAR option in help.
But as mentioned in the avrfreaks thread: You can't be shure if the RAM is in a proper state after WDT. In your example you increment a word-var. if the WDT executes just in between the increment on a low byte overflow (i.e. 255 + 1) you will miss the highbyte increment (so your value is 0 instead of 256). |
|
Back to top |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
Posted: Thu Aug 29, 2019 9:43 am Post subject: |
|
|
If purpose of using WD is wake uC from Powersave mode then you can use WD with WDIE (Watchdog Interrupt Enable) option.
Then when WD occur and uC was sleep then it wake and goes into WD interrupt only (no Reset). uC will clear WDIE and WDF flag and after return from interrupt will start tasks that are coded "after Config powermode = Powersave"
If you want to uC sleep again you should reenable WDIE again and after that config powermode.
WDIE dont should be set in the WD interrupt because safety reasons.
Simply if WDIE will not be Set and program hangs then proper reset will be executed instead interrupt jump. |
|
Back to top |
|
|
Pzx
Joined: 05 May 2015 Posts: 39
|
Posted: Tue Sep 03, 2019 6:35 am Post subject: |
|
|
Thanks for all your advice.
$NORAMCLEAR works fine with a code programmed by a USBASP or STK500 programmer, but not work with a bootloader.
I added $NORAMCLEAR in the bootloader and also in the main program but SRAM is then not preserved.
Any ideas why? |
|
Back to top |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
Posted: Tue Sep 03, 2019 7:15 am Post subject: |
|
|
Do you have $NORAMCLEAR on both Bootloader and Application? |
|
Back to top |
|
|
Pzx
Joined: 05 May 2015 Posts: 39
|
Posted: Tue Sep 03, 2019 8:29 am Post subject: |
|
|
Yes, I have $NORAMCLEAR both in Bootloader and Application. |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Tue Sep 03, 2019 12:07 pm Post subject: |
|
|
Pzx wrote: | but not work with a bootloader. |
A bootloader may use some SRam itself, for that part SRam cells will be altered, but not for the rest of SRam.
The bootloader may still clear SRam itself.
Post a bootloader.hex and it's possible to look it up.
Last edited by MWS on Tue Sep 03, 2019 12:16 pm; edited 2 times in total |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Tue Sep 03, 2019 12:12 pm Post subject: |
|
|
Double post, Mod pls remove. |
|
Back to top |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
Posted: Wed Sep 04, 2019 10:43 am Post subject: |
|
|
Like MWS suggest SRAM for application and bootloader start from address zero (share same address).
Try collect and count SRAM bytes used in the bootloader and then in the application declare some dummy array as first Dimm variable.
Code: | Dim Dummy_arr(100) As byte
Dim MyFirstVar as Byte 'this will be placed at address 101
'so bootloader not alter it
|
or..from the other side.. place important for you variable at the begining of application SRAM and then reserve its addresses in the bootloader application.
My tip is that it will be good to validate content of the SRAM variables you want to keep.
Simply when I want to do some thing like that I declare Array of bytes and Overlay it with variables. Then from array I can get checksum and store it in the array end. If after reset readed checksum dont match calculated checksum data is corrupted and cant be valid so another steps must be taken.
Code: | $regfile = "m328pdef.dat"
$crystal = 16000000
$hwstack = 64
$swstack = 16
$framesize = 64
$noramclear
$sim
Dim Myarray(100) As Byte
Dim My1byte As Byte At Myarray(1) Overlay
Dim My1word As Word At Myarray(2) Overlay
Dim My2word As Word At Myarray(4) Overlay
Dim Mychecksum As Byte At Myarray(100) Overlay
Myarray(100) = Crc8(myarray(1) , 99)
'test after restart
If Myarray(100) <> Crc8(myarray(1) , 99) Then
Print "Checksum is invalid!"
Else
Print "So far so good"
End If
End
|
Because Aliases and Overlay code can be written like that and does the same
Code: | Mychecksum = Crc8(myarray(1) , 99)
If Mychecksum <> Crc8(myarray(1) , 99) Then
Print "Checksum is invalid!"
Else
Print "So far so good"
End If
|
but after any change of the important values checksum must be corrected/calculated again so this line must be "fired" everytime if something need keep in sync:
Code: | Mychecksum = Crc8(myarray(1) , 99) |
|
|
Back to top |
|
|
|