Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

SRAM preserve after reset ATMEGA2560

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
Pzx

Bascom Member



Joined: 05 May 2015
Posts: 31

poland.gif
PostPosted: Thu Aug 29, 2019 8:54 am    Post subject: SRAM preserve after reset ATMEGA2560 Reply with quote

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
View user's profile
Evert :-)

Bascom Expert



Joined: 18 Feb 2005
Posts: 2092

netherlands.gif
PostPosted: Thu Aug 29, 2019 9:09 am    Post subject: Reply with quote

Try this https://avrhelp.mcselec.com/_noramclear.htm

Read the help file carefully and try to understand what this setting does.

_________________
www.evertdekker.com Bascom code vault
Back to top
View user's profile Visit poster's website
laborratte

Bascom Expert



Joined: 27 Jul 2005
Posts: 255
Location: Berlin

germany.gif
PostPosted: Thu Aug 29, 2019 9:14 am    Post subject: Reply with quote

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
View user's profile
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 607

poland.gif
PostPosted: Thu Aug 29, 2019 9:43 am    Post subject: Reply with quote

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
View user's profile Visit poster's website
Pzx

Bascom Member



Joined: 05 May 2015
Posts: 31

poland.gif
PostPosted: Tue Sep 03, 2019 6:35 am    Post subject: Reply with quote

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
View user's profile
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 607

poland.gif
PostPosted: Tue Sep 03, 2019 7:15 am    Post subject: Reply with quote

Do you have $NORAMCLEAR on both Bootloader and Application?
Back to top
View user's profile Visit poster's website
Pzx

Bascom Member



Joined: 05 May 2015
Posts: 31

poland.gif
PostPosted: Tue Sep 03, 2019 8:29 am    Post subject: Reply with quote

Yes, I have $NORAMCLEAR both in Bootloader and Application.
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 1722

blank.gif
PostPosted: Tue Sep 03, 2019 12:07 pm    Post subject: Reply with quote

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.
Quote:
Any ideas why?

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
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 1722

blank.gif
PostPosted: Tue Sep 03, 2019 12:12 pm    Post subject: Reply with quote

Double post, Mod pls remove.
Back to top
View user's profile
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 607

poland.gif
PostPosted: Wed Sep 04, 2019 10:43 am    Post subject: Reply with quote

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 Very Happy
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
View user's profile Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum