• AVR Freaks

AnsweredHot!Initializing a persistent variable on boot

Author
luk
New Member
  • Total Posts : 21
  • Reward points : 0
  • Joined: 2015/10/08 07:12:34
  • Location: 0
  • Status: offline
2019/11/16 11:14:38 (permalink)
0

Initializing a persistent variable on boot

Hello, 
Following my previous post, I have an other question related to persistent variables.
 
The program sets a persistent variable to 3149642683 (random). It's its value after powering up the PIC.
The variable behaves correctly: when the value is changed, the PIC keeps the new value after a soft reset but not after a power off. It's okay for me. However, How can I change the random value the variable is set to on power up ?
 
uint32_t var __attribute__((persistent)) = 0xFFFFFFFF;

doesn't work:
main.c:56:1: warning: Persistent variable 'var' will not be initialized [enabled by default]

 
Actually I need the variable to be set to 0xFFFFFFFF on boot. Then the program changes its value to other values but never to 0xFFFFFFFF. After a soft reset, the program knows that the PIC was not powered off because the variable is not 0xFFFFFFFF.
 
#1
Jim Nickerson
User 452
  • Total Posts : 6376
  • Reward points : 0
  • Joined: 2003/11/07 12:35:10
  • Location: San Diego, CA
  • Status: offline
Re: Initializing a persistent variable on boot 2019/11/16 11:22:28 (permalink)
+1 (1)
I use
_on_reset()

 
#2
Jim Nickerson
User 452
  • Total Posts : 6376
  • Reward points : 0
  • Joined: 2003/11/07 12:35:10
  • Location: San Diego, CA
  • Status: offline
Re: Initializing a persistent variable on boot 2019/11/16 11:25:59 (permalink)
+1 (1)
This will be called on both a power on and soft reset.
There is a register to look at to tell the reset conditions when the PIC was started
#3
Jim Nickerson
User 452
  • Total Posts : 6376
  • Reward points : 0
  • Joined: 2003/11/07 12:35:10
  • Location: San Diego, CA
  • Status: offline
Re: Initializing a persistent variable on boot 2019/11/16 11:29:23 (permalink) ☼ Best Answerby luk 2019/11/16 12:23:28
+3 (3)
In the PIC32MX795F512 I look at the "RCON" register to determine how/why the chip reset.
#4
NKurzman
A Guy on the Net
  • Total Posts : 18061
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: offline
Re: Initializing a persistent variable on boot 2019/11/16 12:08:00 (permalink) ☄ Helpfulby luk 2019/11/16 12:23:22
+3 (3)
The compiler does not set it to a random value. It is the not quite random value that the pic hardware puts at that RAM location based on difference in biased caused by the silicon.

As pointed out the RCON register will tell that there was a power on reset. You can then initialize the variables as you choose.
#5
luk
New Member
  • Total Posts : 21
  • Reward points : 0
  • Joined: 2015/10/08 07:12:34
  • Location: 0
  • Status: offline
Re: Initializing a persistent variable on boot 2019/11/16 12:12:22 (permalink)
0
Indeed, the RCON register is a nice workaround in this case. I forgot about this one.
Something like this :

// next, check the cause of the Reset
if(RCON & 0x0003)
{
// execute a Power-on Reset handler
resetReason = POR;
}
else if(RCON & 0x0002)
{
// execute a Brown-out Reset handler
resetReason = BOR;
}
else if(RCON & 0x0080)
{
// execute a Master Clear Reset handler
resetReason = MasterClear;
}
else if(RCON & 0x0040)
{
// execute a Software Reset handler
resetReason = soft;
}
else if (RCON & 0x0200)
{
// execute a Configuration Mismatch Reset handler
resetReason = ConfMismatch;
}
else if (RCON & 0x0010)
{
// execute Watchdog Time-out Reset handler
resetReason = WTD;
}
RCON = 0;

 
#6
luk
New Member
  • Total Posts : 21
  • Reward points : 0
  • Joined: 2015/10/08 07:12:34
  • Location: 0
  • Status: offline
Re: Initializing a persistent variable on boot 2019/11/16 12:23:08 (permalink)
0
It is the not quite random value that the pic hardware puts at that RAM location based on difference in biased caused by the silicon.

 
Ok, now it's clear, thanks.
I get the same value for this variable after every POR but this value will be different with every chip I program.
 
#7
NKurzman
A Guy on the Net
  • Total Posts : 18061
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: offline
Re: Initializing a persistent variable on boot 2019/11/16 13:16:39 (permalink)
0
Read the data sheet about if you need to Clear RCON.
And clearing the WDT may affect the flag in RCON.
Something to remember if you application needs RCON too.
#8
luk
New Member
  • Total Posts : 21
  • Reward points : 0
  • Joined: 2015/10/08 07:12:34
  • Location: 0
  • Status: offline
Re: Initializing a persistent variable on boot 2019/11/16 14:06:04 (permalink)
0

Read the data sheet about if you need to Clear RCON.

Will check that!

Do you confirm that the variable value will be lost ONLY after a POR and will keep its value after a reset for any other reason (BOR, WDT, softreset, etc...) ?
#9
ric
Super Member
  • Total Posts : 24639
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: Initializing a persistent variable on boot 2019/11/16 14:35:33 (permalink)
+1 (1)
luk
Do you confirm that the variable value will be lost ONLY after a POR and will keep its value after a reset for any other reason (BOR, WDT, softreset, etc...) ?

Yes.
Once you declare it as "persistent", only losing physical power will cause the value to change (assuming none of your software writes to it).
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#10
moser
Super Member
  • Total Posts : 520
  • Reward points : 0
  • Joined: 2015/06/16 02:53:47
  • Location: Germany
  • Status: offline
Re: Initializing a persistent variable on boot 2019/11/19 05:05:49 (permalink)
+2 (2)
Is it sure that the RAM survives a "BOR without POR"? I would have expected, that a persistent variable might already be corrupted when there was a brown-out reset.  
 
For example the PIC32 has:
VDR, RAM Data Retention Voltage (Note 2), Min: 2.0 V
Note 2: This is the limit to which VDD can be lowered without losing RAM data
[...]
VPOR, VDD Start Voltage to Ensure Internal Power-on Reset Signal (Note 3), Min: 1.75 V
Note 3: This is the limit to which VDD must be lowered to ensure Power-on Reset.
[...]
VBOR, BOR Event on VDD transition high-to-low (Note 2), Min: 1.88 V, Max: 2.02 V
Note 2: Overall functional device operation at VBORMIN < VDD < VDDMIN is tested, but not characterized. All device Analog modules, such as ADC, etc., will function, but with degraded performance below VDDMIN.

#11
Jump to:
© 2019 APG vNext Commercial Version 4.5