Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Watchdog fuse bit
Goto page Previous  1, 2, 3  Next
 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
O-Family

Bascom Expert



Joined: 23 May 2010
Posts: 320
Location: Japan

japan.gif
PostPosted: Fri Aug 19, 2016 12:47 pm    Post subject: Reply with quote

Hi EDC

Yes, the code does not work.
As I have pointed out the mistake of BASCOM, because Watchdog is initialized after the $initmicro instruction.
So the ASM code has no effect.
Let's wait for the correction of the MCS-Mark.
Back to top
View user's profile Visit poster's website
laborratte

Bascom Expert



Joined: 27 Jul 2005
Posts: 299
Location: Berlin

germany.gif
PostPosted: Fri Aug 19, 2016 2:50 pm    Post subject: Reply with quote

This is not a bug.

$initmicro only adds a call to a subroutine _init_micro at the earliest possible stage (after setting up the stacks) to allow port configuration (i.e. setting low and high states where needed in hardware). After returning from _init_micro all normal init is done: saving and clearing mcusr, setting watchdog register and clearing memory.

So any watchdog settings in _init_micro are overwritten.

According to the datasheet the initial setting of watchdog register in mandatory, even the watchdog isn't used at all. That's why BASCOM is doing that.

If you are setting the watchdog fuse and you are angry about the time needed for clearing the memory, use the $noramclear option. So you can setup your watchdogtime and, when needed, clear afterwards the memory by yourself.
Back to top
View user's profile
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 971

poland.gif
PostPosted: Fri Aug 19, 2016 3:02 pm    Post subject: Reply with quote

As I said if BOOTRST is disabled this code work with m2560 .
Led flashes.
Code:
$regfile = "m2560def.dat"
$crystal = 16000000
$hwstack = 64
$swstack = 32
$framesize = 128

Config Watchdog = 1024

Config Portb.7 = Output : Led Alias Portb.7                 'Arduino Mega LED

Do

 Toggle Led
  Wait 1
   Waitms 50                                                'value 60 causes Reset
    Reset Watchdog

Loop
End
Back to top
View user's profile Visit poster's website
laborratte

Bascom Expert



Joined: 27 Jul 2005
Posts: 299
Location: Berlin

germany.gif
PostPosted: Fri Aug 19, 2016 3:20 pm    Post subject: Reply with quote

BOOTRST has nothing to do with watchdog. It points programm counter to the bootloader section after reset, when programmed.
Back to top
View user's profile
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 971

poland.gif
PostPosted: Fri Aug 19, 2016 3:27 pm    Post subject: Reply with quote

I mean Arduino boards. If one use them for own project then bootloader also should take care to set WD time if WDTON is set.
If Arduino bootloader is cleared by flashing micro via ISP then BOOTRST should be disabled I think Very Happy
..and I test this..are you too?
If BOOTRST is set then led is lit, no flash Very Happy
Back to top
View user's profile Visit poster's website
O-Family

Bascom Expert



Joined: 23 May 2010
Posts: 320
Location: Japan

japan.gif
PostPosted: Fri Aug 19, 2016 3:34 pm    Post subject: Reply with quote

Hi laborratte

When the reset is performed, after setting the stack, user code of $initmicro is called.
However, followed by the initialization WDTCSR register of Watchdog.
If the AVR clock is low, this causes a problem.

BASCOM is after the initialization of WDTCSR register, it should call the $initmicro routine.
$noramclear in first aid, not a solution.

This is the assembler code output of BASCOM.
Code:

00072 : EF8F    RESET:          LDI     R24,$FF
00073 : BF8D                    OUT     SPL,R24
00074 : EEC0                    LDI     YL,$E0
00075 : EEE8                    LDI     ZL,$E8
00076 : 2E4E                    MOV     R4,ZL
00077 : E281                    LDI     R24,$21
00078 : BF8E                    OUT     SPH,R24
00079 : E2D1                    LDI     YH,$21
0007A : E2F1                    LDI     ZH,$21
0007B : 2E5F                    MOV     R5,ZH
0007C : 940E 00AA               CALL    L000AA              ;$initmicro
0007E : 95A8                    WDR                         ;Watchdog of Reset
0007F : B784                    IN      R24,MCUSR
00080 : 2E08                    MOV     R0,R24
00081 : 7F87                    ANDI    R24,$F7
00082 : BF84                    OUT     MCUSR,R24
00083 : E188                    LDI     R24,$18
00084 : 2799                    CLR     R25
00085 : 9380 0060               STS     WDTCSR,R24
00087 : 9390 0060               STS     WDTCSR,R25          ;### Watchdog => 16ms ###
00089 : EFEE                    LDI     ZL,LOW(S1FFE)
0008A : E1FF                    LDI     ZH,HIGH(S1FFE)
0008B : E0A0                    LDI     XL,LOW(S0200)
0008C : E0B2                    LDI     XH,HIGH(S0200)
0008D : 2788                    CLR     R24                 ;All clear of SRAM
0008E : 938D            L0008E: ST      X+,R24
0008F : 9731                    SBIW    ZH:ZL,1
00090 : F7E9                    BRNE    L0008E
 
Back to top
View user's profile Visit poster's website
laborratte

Bascom Expert



Joined: 27 Jul 2005
Posts: 299
Location: Berlin

germany.gif
PostPosted: Fri Aug 19, 2016 3:48 pm    Post subject: Reply with quote

The purpose of $initmicro is port setting as soon as possible, not watchdog setting for CPU with huge memory and low clock and fused "always wachdog".

IMO the port setting is more important than the rare second situation.

Edit: The assembler output shows "Watchdog off", not "Watchdog 16ms", if the fuse WDTON is not programmed. IMO this is OK.
Back to top
View user's profile
O-Family

Bascom Expert



Joined: 23 May 2010
Posts: 320
Location: Japan

japan.gif
PostPosted: Fri Aug 19, 2016 4:14 pm    Post subject: Reply with quote

Yes, to control the Watchdog in the fuse bit [WDTON] is a special example.
Therefore, I think this problem has not been found so far.

By performing the call of $initmicro after initialization of WDTCSR, the user can set the Watchdog freely.
Back to top
View user's profile Visit poster's website
Pzx

Bascom Member



Joined: 05 May 2015
Posts: 39

poland.gif
PostPosted: Sun Aug 21, 2016 11:36 pm    Post subject: Reply with quote

Thanks Bartek, you are very helpful, as usual Smile
You are right, your first code works, I tested it, and it was a surprise for me, but in my code I had one small additional thing that I didint mentioned before. I thought it was not important, but it is.
I want to use LCD in my program so I put CLS before the main loop.
My program look actually like this (ful version):

$regfile = "m2560def.dat"
$crystal = 16000000
$hwstack = 64
$swstack = 32
$framesize = 128

Config Watchdog = 1024
Reset Watchdog

Config Lcd = 16 * 4
Config Lcdpin = Port , Port = Portc , E = Porta.5 , Rs = Porta.7

Led Alias Portk.0
Config Led = Output

Cls 'This instruction is a problem in my code !!!!!!!!!
Lcd "Hello World" 'This instruction is also a problem, even without Cls before !!!!!!!!!

Do

Toggle Led
Waitms 1
Reset Watchdog

Loop
End

Without Cls instruction the code works perfectly, but with Cls or other LCD instruction like Lcd "Hello Word" The code doesnt work. This strange behaviour is only when Watchdog always on fusebit is activated.
If I activate Watchdog controlled by software fusebit all LCD instructions works ok.
Does anyone know how to use LCD with Watchdog always on fusebit activated?
Back to top
View user's profile
O-Family

Bascom Expert



Joined: 23 May 2010
Posts: 320
Location: Japan

japan.gif
PostPosted: Mon Aug 22, 2016 12:25 am    Post subject: Reply with quote

You showed only a part of the program source.
If you are using the LCD, initialization routine of the LCD after the clearing of the SRAM are put forcibly.
The initialization of this LCD takes time.
Thus, the $noramclear instruction is ineffective, too.

There are no ways until you correct BASCOM by MCS-Mark.
Do not use a fuse bit [WDTON] until the modification is carried out, and please be available on Config Watchdog instruction.
Back to top
View user's profile Visit poster's website
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 971

poland.gif
PostPosted: Mon Aug 22, 2016 8:31 am    Post subject: Reply with quote

Please don`t start coding in monday without cup of coffe Very Happy Hahaha
If you insert Initlcd statement then LCD will be initialized at this point Wink
Code below work fine Wink

Code:
$regfile = "m2560def.dat"
$crystal = 16000000
$hwstack = 64
$swstack = 32
$framesize = 128

Config Watchdog = 1024

Config Portb.7 = Output : Led Alias Portb.7                 'Arduino Mega LED

Config Lcdpin = Port , Port = Portc , E = Porta.5 , Rs = Porta.7

Initlcd   'the LCD will be initialized here, after Watchdog config
 Cls
  Reset Watchdog

Do

 Toggle Led                                                 'LED flash like a charm Wink
  Wait 1
   Reset Watchdog

Loop
End


Have a nice day Very Happy
Back to top
View user's profile Visit poster's website
Pzx

Bascom Member



Joined: 05 May 2015
Posts: 39

poland.gif
PostPosted: Mon Aug 22, 2016 2:54 pm    Post subject: Reply with quote

EDC's code solved the problem.
The issue was to properly initialize LCD in my program.
Many thanks, Bartek Smile
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5921
Location: Holland

blank.gif
PostPosted: Mon Sep 05, 2016 9:01 pm    Post subject: Reply with quote

While i can change the sequence so initmicro is called after the WD is turned off. (it is indeed turned off, not set to 16 ms), this will have effect on the timing.
I think a few cycles extra till the user can set the port levels will not matter much? on the other hand, it might be simpler to add an option to skip the reset of the WD.
Something like $NOWD that will remove the init code for the WD. That way the user can make use of the WD always on option.
Please comment.

_________________
Mark
Back to top
View user's profile Visit poster's website
Arera

Bascom Member



Joined: 23 Sep 2007
Posts: 386
Location: Wuppertal, Germany

germany.gif
PostPosted: Fri Sep 09, 2016 12:10 pm    Post subject: Reply with quote

It seems to me, we are running into an dead end here.
Actually it is only about a watchdog, it is not THAT tricky, I think.
I always use the WD, and I never needed to worry about ASM-code and stuff.

First question:
What are you aiming for?
Answer:
In case the main-routine "hangs" for any reason, it cannot reset the WD anymore, resulting in a WD-triggered reset.

That brings us to the crutial point in any programming:
The main routine shold be looped fast and regulary.
FAST means: Do not WAIT for to long. A 1000ms WAIT to flash a led is not a good idea...
Do not loop inside the main loop, unless you are shure the loop will be left within an acceptable time.
REGULARY means: Do not get stuck in subroutines, that themselfs take unexpected (or expected) time.

The WD-timeout should have surplus time to the main-loop execution time. This is to prevent the WD from accidently triggering a reset.
If your main loop takes 3s-5s (witch is looooooong), set the WD to 8s. There is no closer timeout avaliable and nessecary in most cases.

My conclusion:
Setting the WD to one second, and then WAIT for one second in the main-loop, can lead to problems. Obviously it did in in the TO's case.

Creating code, that takes all my hints into account, requires a well structured algorythm. I know this is difficult and often needs more code.
It took me a while to learn how to do this. But in the end it will be faster and more universal.
On the other hand, if your code is simpler (blink a led every 12s using a WAIT in an loop), it can work and be just fine for you, no problem.
But in that case the use of a WD will not succeed.

BTW: resetting the WD in every loop you have will kill the idea of the WD. The WD should be resetted only once in the code, right at the beginning of the main loop.
Back to top
View user's profile
O-Family

Bascom Expert



Joined: 23 May 2010
Posts: 320
Location: Japan

japan.gif
PostPosted: Fri Sep 09, 2016 2:44 pm    Post subject: Reply with quote

Hi Mark

Like the setting of the stack pointer, the initialization of Watchdog is possible by the assembler order of a short step.
Therefore when initializing Watchdog before $initmicro processing, setting of Watchdog is the user's concerned street.

Since this problem is a special case with the configuration of the fuse, additional new instruction ($NOWD) I think that there is no need.


_init_micro:
Config Watchdog = 1024
Return
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
Goto page Previous  1, 2, 3  Next
Page 2 of 3

 
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