Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Solution for Hard reset ATMEA328P

Status
Not open for further replies.

johnny78

Full Member level 4
Joined
Jun 28, 2017
Messages
206
Helped
0
Reputation
0
Reaction score
4
Trophy points
18
Activity points
1,701
hi Guys
i need to Disable my circuit in case of Emergerncy for some reasons
But how can i decide which its restarted after Hard reset or normal power on ?
i need to do some tasks in case the emergenct button was pressed & released
any idea please ?
is it possible to save a variable to Eeprom in the case of hard reset ?

thanks
Johnny
 

Hello!

is it possible to save a variable to Eeprom in the case of hard reset ?

In case of hard reset, assuming the system wasn't powered down exactly at the
same time, you are likely to have a constant power supply. One way to do it would
be a simple RC circuit. If it has been powered on for ages, the capacitor is fully
charged, and if you measure its voltage, you will find VCC.
If you just powered the board, then the capacitor is not fully charged. So if the first
thing you do in your program is to measure the cap voltage, then you can have
a hint whether it was powered or whether it has just been switched on.
But: you have to specify your timeout with great care. For example, what about if
you make a short power off, a few 100s milliseconds, the cap might not be discharged.
Or maybe find a trick to discharge it quickly with some FET just when the power goes off.

Dora.
 

    johnny78

    Points: 2
    Helpful Answer Positive Rating
Hi,

There are flags telling you why the microcontroller is restarted.
Power_on, external reset, watchdog .. and others.
I don't have the complete datasheet in mind, so I had to read the datasheet....

Less save method:
You may store a magic number in RAM. If it already exists at power up (be sure the compiler does not clear variables) then there was no power down. After power down memory content is lost.

Alternative methods:
* external circuits, like some kind of RS-FF that is cleard on power up, set by software.
* not using RESET at all, but Interrupt to force the MCU controlled in inactive state.

Klaus
 

    johnny78

    Points: 2
    Helpful Answer Positive Rating
Hello!



In case of hard reset, assuming the system wasn't powered down exactly at the
same time, you are likely to have a constant power supply. One way to do it would
be a simple RC circuit. If it has been powered on for ages, the capacitor is fully
charged, and if you measure its voltage, you will find VCC.
If you just powered the board, then the capacitor is not fully charged. So if the first
thing you do in your program is to measure the cap voltage, then you can have
a hint whether it was powered or whether it has just been switched on.
But: you have to specify your timeout with great care. For example, what about if
you make a short power off, a few 100s milliseconds, the cap might not be discharged.
Or maybe find a trick to discharge it quickly with some FET just when the power goes off.

Dora.
your solution is not useful in my case because maybe i have power loose at the same time of emergency pressed

but its a good idea for other projects

thanks
--- Updated ---

Hi,

There are flags telling you why the microcontroller is restarted.
Power_on, external reset, watchdog .. and others.
I don't have the complete datasheet in mind, so I had to read the datasheet....

Less save method:
You may store a magic number in RAM. If it already exists at power up (be sure the compiler does not clear variables) then there was no power down. After power down memory content is lost.

Alternative methods:
* external circuits, like some kind of RS-FF that is cleard on power up, set by software.
* not using RESET at all, but Interrupt to force the MCU controlled in inactive state.

Klaus
im reading the datasheet for this flags
im not sure if i still have power or not at the time of emergency
i want to exclude the Mcu at all at this case

the flag you have mentioned is the thing im looking for but i dont remember
this kind of flags when i read the Datasheet earlier

i will look for it right now

thanks
 

Hi,

I'd try a datasheet search for "reset source" or similar phrases.

Klaus
--- Updated ---

Added:
In case of power loss the AVR internal port protection diodes may get active.
This may result in:
* increase of AVR power supply voltage by reverse powering from ports ... in worst case above the "power off" detection of the AVR.
* pulling ports LOW instead of leaving them floating.

Klaus
 

how can i decide which its restarted after Hard reset or normal power on ?
There is no general answer, it's application specific. Context variables could be considered, but depend on their change rate, you could not save to EEPROM everytime but from time to time, tolerating some losses; counters for example are candidates to be restored, as well as user specific last configurations such as alarm on/off , etc...
 

    johnny78

    Points: 2
    Helpful Answer Positive Rating
You can do an EEPROM flag save at the same time you detect power loss if
your supply cap on part is large enough to cover the span of time needed
to EEPROM write. Basically you supply power to CPU thru a diode (Schottky
preferable) that has a large cap connected to CPU pin. Diode prevent cap
supplying other circuits, just CPU on power loss.

In fact you could also save A/D reading to provide further info on recovery.

Use Q = C x V, I = C x dV/dT or C = (I x dT) /dV to calculate C you will need.
Volts, Ohms, Farads units of measure.

Also set up A/D using internal Vref, not Vdd, which would be falling, to get
accurate brownout/power loss value. Do an error budget to worst case what
A/D measures that influences C calculation.

So when you detect power fail you enter EEPROM flag write, and Cap is big
enough to allow CPU to complete write.

Note in some parts the FLASH can be programmed much faster than EE, check
that out. You could set aside an area in FLASH for saves. That saves size of backup
cap, expands amount of data you could save. Note you give up R/W cycle lifetime
as EE typically much more resilient in R/W cycle limits.





Regards, Dana.
--- Updated ---

In some devices the save routine should shutdown all ancillary internal
hardware resources not needed to maximize time available for writes.

Just a thought.


Regards, Dana.
 
Last edited:

thanks Dana
Alot of useful ideas in the avrfreaks post
its something can be done But the main problem is that i dont have more free pin to detect power
so Klaus idea about a register of reset was great but i couldnt find it
i will try another external solution which save the situation of my sensors before doing hard reset to main Mcu

thanks
Johnny
 

Hi,

Klaus idea about a register of reset was great but i couldnt find it
I could no believe this. So I downloaded the datasheet, did a search for "reset source" (like recommended) and the 5th hit gave:
10.9.1 MCUSR – MCU Status Register
The MCU status register provides information on which
reset source caused an MCU reset.

--> You need to improve your datasheet search skills. It's really simple and straightforeward:
* open datasheet in PDF viewer
* start the "text search" feature
* type in "reset source" and start the search
* if the current highlighted is not what you are looking for --> press the "next search" button

Klaus
 

    johnny78

    Points: 2
    Helpful Answer Positive Rating
thanks Dana
Alot of useful ideas in the avrfreaks post
its something can be done But the main problem is that i dont have more free pin to detect power
so Klaus idea about a register of reset was great but i couldnt find it
i will try another external solution which save the situation of my sensors before doing hard reset to main Mcu

thanks
Johnny
The part has a configurable brown out detection interrupt where you can set the trigger V
in code. The source is internal Vcc for detection, so no pin needed. That should be more than
enough to accomplish EE or FLASH write. FLASH write of a page, more than you need, is max 4.5 mS.
EEPROM timing I did not see a number. I am sure there is one.

Regards, Dana.
 

Hi,
The part has a configurable brown out detection interrupt
are you sure?
Afaik, there is no brown Out Interrupt --> to run an ISR
It´s just a Brown Out Reset, to stop code from running.

So I think it is not able to perform an EEPROM write after Brown Out condition is detected.
(but it should be able to finish a previously started EEPROM write process. depending on how fats the VCC drops)

Klaus
 

Its listed in interrupt table -

1638188140856.png


And then shown/implied calling a handler to service it, its vector in address 0 -

1638188211325.png


It would be highly unusual in todays UP to have a brownout condition w/o an attached ISR vector.


Regards, Dana.
 
Last edited:

Hi,

I agree, but it´s the reset not an interrupt. They operate differently.

Means
* as long as brown_out is active: immediately no instruction is performed.
* After brown_out situation: the program starts at where the reset_pointer points to. Independent of RESET, Power_ON, brown_out, watchdog...

Klaus
 

As you can see the ISR vector table is nothing more than a set of
LJMPS to service routines, so when one jumps using the vector at
address 0 first thing is to use masks to determine what caused that
ISR, amongst which brownout is a bit in some register. I state this
w/o looking at programmers model for part, but suspect its accurate.

Regards, Dana.
 
Last edited:

Hi,
That should be more than
enough to accomplish EE or FLASH write.
While you can perform code while an interrupt is pending..
In oppostie to this: You can not perform code when a RESET is pending.

****

Any RESET (brown_out, watchdog, external) will imediately stop performing code. It will reset internal registers and periferals ... and stays stopped until the reset condition disappears. You can not initate an EEPROM_write.
After RESET it does not resume performing code at the place it was stopped, it starts code at the very beginning.
The RESET pointer points to where the main (loop) code is placed.

In oppostite to this:
An interrupt may be masked. MCU operation continues.. usually the main loop.
When the according interrupt is enabled: it performs the ISR code and then resumes at the main loop, where it was before the ISR was performed.
Since the interrupt signal does not stop the MCU from working, an ISR may perform EEPROM_writes, independent whether the according interrupt signal is still pending or not.

***
I just want to say: You can not perform an EEPROM_write initated by a brown_out condition in the same way as an interrupt can..

Klaus
 

i
Hi,


I could no believe this. So I downloaded the datasheet, did a search for "reset source" (like recommended) and the 5th hit gave:
10.9.1 MCUSR – MCU Status Register
The MCU status register provides information on which
reset source caused an MCU reset.

--> You need to improve your datasheet search skills. It's really simple and straightforeward:
* open datasheet in PDF viewer
* start the "text search" feature
* type in "reset source" and start the search
* if the current highlighted is not what you are looking for --> press the "next search" button

Klaus
i have downloaded the datasheet again & yes i didnt search the same way you have mentioned
& yes there is EXTRF External Reset Flag.
So all i have to do is save the flag to Eeprom when Reset occurs & then on Restart Read the Saved Flag & do whatever i want

thanks
--- Updated ---

i

i have downloaded the datasheet again & yes i didnt search the same way you have mentioned
& yes there is EXTRF External Reset Flag.
So all i have to do is save the flag to Eeprom when Reset occurs & then on Restart Read the Saved Flag & do whatever i want

thanks
i need to read more as i see i cant write to eeprom when reset occurs
So i have to read carefully your conversation & take a good look to the datasheet to understand exactly how can i do this Job

• Bit 1 – EXTRF: External Reset Flag
This bit is set if an external reset occurs. The bit is reset by a power-on reset, or by writing a logic zero to the flag.

this flag will keep set when the Reset occurs So i have to read it when the Mcu restarted
But when power fails this flag is lost?

Thanks
 
Last edited:

Hi,

no EEPROM is needed.

There is not only the EXTRF! There are three other flags that show you the reset source.

at the very beginning of your code:
* declare an 8 bit variable as myResetSource
* copy the MCUSR --> myResetSource
* clear MCUSR contents
... (do some code)
* then perform desried functions according the myResetSource contents.
.. (do some other code)

Read the datasheet carefully.

Klaus
 

There is a flag that gets set on brownout -

1638194461584.png


And there is a vector executed in the interrupt table....

Manual leaves a LOT to be desired.

There is a statement in manual that if brownout occurs , which cause an ISR, the EEPROM operation will be completed if Vcc stays above min level, not the brownout selected level.

Then there is disable ISRs and detect BOD flag and do the save.....

Section 6.7 seems to make no exception as to how interrupts are handled for internal reset....

I could be wrong, but todays processors allow code execution on detection of many
fault conditions, especially low Vcc, otherwise one cannot consider safety applications
w/o designing external brownout HW. Orderly startup AND shutdowns. Not just stop and die.

Saw this as well -

1638195989257.png



Regards, Dana.
 
Last edited:

There is a flag that gets set on brownout -
yes

And there is a vector executed in the interrupt table....
complete: RESET and Interrupt table...

Manual leaves a LOT to be desired.
What exactly?

There is a statement in manual that if brownout occurs , which cause an ISR
Where exactly?

the EEPROM operation will be completed if Vcc stays above min level, not the brownout selected level.
True.
It says: the EEPROM_write completes .. if started beforehead, but it can not be "started" during brown_out.
(as I already mentioned in post#11)
EEPROM is not needed at all

Then there is disable ISRs and detect BOD flag and do the save.....
Disabling ISRs has nothing to do with BOD.

You can detect BOD flag only after restart (reset) (also see FIGURE 10-1. Reset Logic)

****

the ATMEGA328PB (Mind the "B") datasheet, from 10/2015 tells:
15.9.1. MCU Status Register
To make use of the Reset Flags to identify a reset condition, the user should read and then Reset the
MCUSR as early as possible in the program. If the register is cleared before another reset occurs, the
source of the reset can be found by examining the Reset Flags.

I´m very sure the same applies to ATMEGA328P.

Also read ATMEGA328B datasheet:
* 10.5 Brown-out Detection... and the according Figure 10-5.

Klaus
 

are you sure?
Afaik, there is no brown Out Interrupt --> to run an ISR
It´s just a Brown Out Reset, to stop code from running.

I partially concur, seems its a specialized ISR, as it appears to act like an ISR, eg.
jumps to a reset handler, but non ISR behavior that affects other
HW. It stops code for a time delay and then waits for Vcc recovery. But the
BOD can trigger on low Vcc but still valid operating Vcc. So is timer still
invoked, more uncertainty. In any event an early BOD warning (user set
higher trip V than absolute min). In 6.7 Reset and Interrupt Handling manual
refers to RESET (not to be confused with RESET pin) has highest priority, another
funky reference to being an "ISR".

1638222327712.png


So in short if a full reset occurs the part loses cause of reset in registers ?
But if a BOD then code can be executed to program EEPROM....and test
status register for cause.

Cannot find spec for Tbod, and if BOD occurs with a jump to handler then CPU
stops running code.....huh, what a nice feature that is....especially when Vbod >
Vcc absolute min.....

Time to drill a hole in my head, drain it, and enjoy the collapse of the universe.


Regards, Dana.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top