Solution for Hard reset ATMEA328P

Status
Not open for further replies.

johnny78

Full Member level 5
Joined
Jun 28, 2017
Messages
270
Helped
1
Reputation
2
Reaction score
5
Trophy points
1,298
Activity points
3,460
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
 

Hi,

I give up. I can not convince you.
Please tell the OP what to do.

Klaus
 

Morning Guys
to be honest im more confused because this war of informations

finally i will try the method Klaus was suggested to read the MCUSR at the startup of the code
then after solving my first issue (knowing the reset source)i will try the rest in case of power loose

thanks
Johnny
--- Updated ---

it was nice to check this when looking for reading the MCUSR register

 
Last edited:

Hi
im testing the simple method i have found to read the MCUSR now
Code:
Serial.Print(MCUSR);
then i have issues of the bootloader so the result was "0"
after reading about the bootloader corrupt this registers i have tried to upload the code generated by Arduino without bootloader using my Avrisp programmer
& tried to read the serial
the result was 7 & as i read in the datasheet 7 is the WDIF

what do u suggest to solve this?
thanks

Johnny
 

Sorry for the confusion.

Do a simple routine where you ramp down the Vcc and in a modfied bootloader (think
reset handler in this case, see comments below) do a EEPROM write, then examine
memory after event.
So MCUSR = 7, then the lower 3 bits set =





rest handler considerations -






Seems to indicate to service a BOD and do EEPROM save that would have to be
done in a modified bootloader....? In your case think of this as bootloader = reset handler


Regards, Dana.
 
Last edited:

    johnny78

    Points: 2
    Helpful Answer Positive Rating
Hi
i dont have any idea about modifying a Bootloader

i have imported the MCUSR read sketch into Atmel studio7
& the same result i have is 7
for External reset i should read 1
shouldnt it work to read the MCUSR without the Bootloader?

Johnny
 

This user seemed to get it working -





Regards, Dana.
 

    johnny78

    Points: 2
    Helpful Answer Positive Rating
thanks Dana i was reading the Avrfreaks post before check your reply
im working on this
 

seems like no one got this working
no one read the MCUSR without problems with or without bootloader

in the datasheet it seems to be easy to read this register
but in real world no one give a good answer of how to read it

hope someone could post a working code of reading this register
with Arduino or Atmel studio or anything works

thanks
Johnny
 
Last edited:

Hi,
seems like no one got this working

I did read the reset source on different AVRs successfully.
I did it like written in post#17.
Did you test it? What happened? How did you test it?
*****
* I don´t know what hardware you use. --> Show your schematic, show a photo of your test circuit.
* I don´t know what compiler/IDE you use. --> Tell us
* What upload tool and what setting did you use?
* What power supply and what voltage do you use?
*****
From my experience:
* Generally it does not matter whether the reset_source_reading_code is in APP section or BOOTLOADER section. But I recommend to do first tests in APP section.
* be sure you use the correct and exact device setup for compiler/IDE/programming tool. Mind that there are ATMEGA328, ATMEGA328P, ATMEGA328PB with (partly) different features. ==> In doubt send a photo.* disable watchdog fuse (at least for the first tests) => set WDTON = 1
* set the BODLEVEL fuses according your supply voltage. --> Tell us your setup
* use the APP section for first tests ==> be sure BOOTRST = 1
* enable the external RESET for first tests: ==> be sure RSTDISBL = 1
* disable debugWIRE interface for first tests: ==> be sure DWEN = 1
* what clock_source do you use ==> show us your Low Fuse Value
* be sure the fuses are programmend correctly (verify, read back)
* don´t use any Lock and Protection features ==> show us your Lock Bit Byte value
* don´t use any interrupt for first tests. ==> Be sure CLI

****

Please check item by item very carefully
Please give detailed feedback

Klaus
 

Hi,


I did read the reset source on different AVRs successfully.
I did it like written in post#17.
Did you test it? What happened? How did you test it?
*****
i have tested on Arduino IDE & i got 0 as a result for reading MCUSR
my sketch was simple
Serial.begin(9600);
Serial.Print(MCUSR);

later i got the ino file without bootloader & i used Atmel studio to program the Atmega with the file & i thought maybe without the bootloader i will get the result i want but it was the same

later i have imported the arduino sketch into Atmel studio7 & uploaded the code to the ATMEGA328p using Avrisp MKII programmer
the result this time is 7 which in the datasheet reserved & i think i must get 1 for the external reset
* I don´t know what hardware you use. --> Show your schematic, show a photo of your test circuit.
im testing on Arduino uno So im just interested to read the register if i press the Reset button
then i get result on the serial monitor
* I don´t know what compiler/IDE you use. --> Tell us
using Arduino ide & Atmel studio 7 later
* What upload tool and what setting did you use?
i have used the UNO bootloader first then used MKII but i didnt change any settings
* What power supply and what voltage do you use?
the uno Voltage

 

Hi,

Arduino:
* is a big tool. It adds a lot of code to your code. So you can´t be sure it modifies the MCUCSR before you PRINT it.
--> not a reliable test method.

Also my recommendation was to clear the MCUCSR contents after reading. You did not do this. So if if some bits are not cleared by code they will stay SET.

--> not a reliable test method.

If you want/need to stay with Arduino you need to read Arduino documentation about MCUCSR treatment.

*****
Compiled Arduino code, but uploaded via Atmel studio:
* without knowing about fuse setting this also is no reliable test method.
* even if you don´t use the (Arduino) Bootloader does not mean the bootloader does not exist and it does not mean that it is not (partly) executed.
--> thus I asked for fuse setting . You may also upload the used HEX file.

*****
Are you able to write a simple assembler program?
It´s the most focussed and reliable way to debug, because you control every single instruction that will be executed.

*****
the uno Voltage
I´m not familair with uno. It comes with it´s own power supply? And you don´t know the voltage?

*****
Although I´m not familiar with Atmel Studio, I´m rahter sure that one can see the fuse setup, modify fuse setup, program or not program fuses... fuses have big influence on behaviour.
--> if you don´t give fuse information, I / we are not able to know the the test conditions.

Thus, please answer every single question beginning with post#29 as detailed as possible.

It´s not my intention to keep you busy, I want to quickly find a solution.

Klaus
 

--- Updated ---

I guess the key to this is an IDE and programming not using a boot loader, or the modified
Optiboot bootloader -


I also saw this -




Regards, Dana.
 
Last edited:

yes i thought of this for next readings
--> not a reliable test method.

If you want/need to stay with Arduino you need to read Arduino documentation about MCUCSR treatment.
its not reliable method at all All forums talk about bootloaders & methods to try reading this but it fails
i used to write assmebly code for PIC mcu so i think its the best solution for this
Low level programming is the best to control registers im sure
*****

I´m not familair with uno. It comes with it´s own power supply? And you don´t know the voltage?
working with UNO is much easier & yes it have 5v Regulator inside it
sure i can set fuses im using the default fuses but i was reading about disabling boot section with fuses
what fuses u think i must think of ?
Thus, please answer every single question beginning with post#29 as detailed as possible.

It´s not my intention to keep you busy, I want to quickly find a solution.
you help alot but every person have its own way of understanding
--- Updated ---

yes its right
i will try to start with Assembler again

thanks
 

if you look at Optiboot webpage there are numerous other bootloaders folks
have done for boot loaders. Point I would make is "touching" the bootloader
not a big deal. Keep in mind Arduino not a well controlled development environment
like using a IDE is. So if you are doing product development not quite a big deal
to think about your own B/L version. In fact that would give you more control in a
sense.


Arduino is, by definition, open source so you are always exposed to the vagaries of
changes from the authors of the myriad of libraries offered for Arduino. So I do not
think of libraries and firmware inviolate anymore. Pick and choose and abuse as the
saying goes . At your own risk of course.

Regards, Dana.
 

Read post #29. I've written one line for every important fuse.

--> For now it's important to read them back and show us.

Klaus
Hi
here is the default fuses set
How did you read the MCUSR ?

Johnny
 

Attachments

  • Screenshot (6).png
    168.1 KB · Views: 114
  • Screenshot (7).png
    163 KB · Views: 111


Hi,

what does a checked box mean? 1 or 0?
Did you verify them against post#29?

******

as already written:
* read MCUCSR (there are several ways, with IN, LD, ...)
* then clear the MCUCSR by writng 0x00

assembler pseudo code:

Code:
clr R17
in R16, MCUCSR
out MCUCSR, R17

ldi R17, all outputs
out DDRx, R17
out PORTx, R16
loop:
rjmp loop

No guarantee. Have not used assembler for a long time...
Let´s see what happens.

Klaus
 

You can do that as in-line ASM in Arduino IDE -


Don't forget if doing this as mixed code, not a stand alone program, you need to do some
stack pushes / restore of accumulator, pointers, etcc....if doing a print of values read.....other
stuff.

You might also want to look at a bootloader to see what CPU initialization needs to be done
if not using a bootloader....


Regards, Dana.
 
Last edited:

im planning to use Avrisp & im reading about reuse Assembly
But i will read once more about easy way with bootloader

thanks
Johnny
--- Updated ---

Hi,

what does a checked box mean? 1 or 0?
i think it means Enabled or 0
Did you verify them against post#29?
i didnt yet
i didnt use Assembly for atmel before but im trying to reuse it
im reading about it now
thanks
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…