Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Watchdog fuse bit
Goto page 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
Pzx

Bascom Member



Joined: 05 May 2015
Posts: 39

poland.gif
PostPosted: Wed Aug 17, 2016 6:55 pm    Post subject: Watchdog fuse bit Reply with quote

Hello, I would like to use Watchdog always on fusebit to be more safe and independent from unexpected software crash, but it seems not work properly.
My test program is very simple:

Config Watchdog = 1024
Start Watchdog

Do
Reset Watchdog
Toggle LED
Waitms 1
Loop
End

With Watchdog controlled by software fuse bit selected everything is ok, but when I select Watchdog always on fuse bit the program doesnt work. Why? What is missing in the code then?

(BASCOM-AVR version : 2.0.7.8 )
Back to top
View user's profile
Evert :-)

Bascom Expert



Joined: 18 Feb 2005
Posts: 2156

netherlands.gif
PostPosted: Wed Aug 17, 2016 9:19 pm    Post subject: Reply with quote

What processor are you using?
_________________
www.evertdekker.com Bascom code vault
Back to top
View user's profile Visit poster's website
Pzx

Bascom Member



Joined: 05 May 2015
Posts: 39

poland.gif
PostPosted: Wed Aug 17, 2016 9:23 pm    Post subject: Reply with quote

ATMEGA 2560
Back to top
View user's profile
i.dobson

Bascom Expert



Joined: 05 Jan 2006
Posts: 1570
Location: Basel, Switzerland

switzerland.gif
PostPosted: Thu Aug 18, 2016 7:32 am    Post subject: Reply with quote

Hi,

Could it be that the avr is taking too long to initialise and going into watchdog reset?

Maybe try using the $INITMICRO macro to reset the watchdog during init. Something like:

Code:

$INITMICRO
Config Watchdog = 1024
Start Watchdog

Do
 Reset Watchdog
 Toggle LED
 Waitms 1
Loop
End

_init_micro:
Reset Watchdog
Return
 


Maybe the AVR is taking too long to clear the ram, so also try $NORAMCLEAR (You'll need to handle setting all your variables to a defined value yourself)

Regards
Ian Dobson

_________________
Walking on water and writing software to specification is easy if they're frozen.
Back to top
View user's profile
Pzx

Bascom Member



Joined: 05 May 2015
Posts: 39

poland.gif
PostPosted: Thu Aug 18, 2016 8:59 am    Post subject: Reply with quote

Hi Ian, you are right Atmega seems to be in permanent reset state,
unfortunately your code doesnt help much.
Any other ideas?
Back to top
View user's profile
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 971

poland.gif
PostPosted: Thu Aug 18, 2016 9:11 am    Post subject: Reply with quote

This micro have even 8s Watchdog(look into datasheet page 66). Try another higher value.
Wait 1 + Toggle can be more than 1024 WD timer
You can also try Waitms 500->Reset WD ->Waitms 500 -> Reset WD + Toggle LED ..and so on if value 1024 is the best for you.

EDIT. Look attached picture.
I often open DAT files and search for interested me values by typing CTRL + F
Here I type WD and voila Wink
Back to top
View user's profile Visit poster's website
i.dobson

Bascom Expert



Joined: 05 Jan 2006
Posts: 1570
Location: Basel, Switzerland

switzerland.gif
PostPosted: Thu Aug 18, 2016 9:30 am    Post subject: Reply with quote

Hi,

Looking through the data sheet, I couldn't find anything on what value will be used for the watchdog reset timer on power up (before the user code starts).

Regards
Ian Dobson

_________________
Walking on water and writing software to specification is easy if they're frozen.
Back to top
View user's profile
Evert :-)

Bascom Expert



Joined: 18 Feb 2005
Posts: 2156

netherlands.gif
PostPosted: Thu Aug 18, 2016 9:48 am    Post subject: Reply with quote

If you are using the watchdog with the WDTON fuse you can not change the watchdog registers anymore easy. (Has to be done with in 4 clock cycles)
This is done to prevent that the software accidentally alter the registers.
It's not clear how Bascom handles this and if Bascom takes the setting of the fuse bit in consideration.

Are you using a bootloader?

_________________
www.evertdekker.com Bascom code vault
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: Thu Aug 18, 2016 10:20 am    Post subject: Reply with quote

This is $initmicro instruction after the execution, would initialize the Watchdog to a minimum of 16ms, I think that it is a bug in the BASCOM.
Only get fixed in the mark, there is not means.
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
 

If there is no need to clear all of the SRAM, or initialize it yourself, please put the $noramclear instruction.
Code:

   $noramclear
   Config Watchdog = 1024
 
Back to top
View user's profile Visit poster's website
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Thu Aug 18, 2016 1:23 pm    Post subject: Reply with quote

i.dobson wrote:
Looking through the data sheet, I couldn't find anything on what value will be used for the watchdog reset timer on power up (before the user code starts)

Default value in WDTCSR for WDP3..0 is 0000, this results in a timeout of 16ms.
O-Family wrote:
Only get fixed in the mark, there is not means.

Would you mind to write in English?

@TO:
There are two issues imho:

1) the clear-ram and startup code takes about 50000 processor cycles. If the processor is running at 8 MHz, it is done in a bit more than 6ms, fast enough to reset the watchdog within 16ms.
In case the processor runs slower, it may run into a endless watchdog reset loop while clearing ram.

2) looking at the Config Watchdog routine, it is messed up somehow, it does weird things and looks unusable to me.
A correct version according the data sheet would look like:
Code:
!IN   R22, SREG                                             ' save status of global interrupts
!CLI                                                        ' disable interrupts
!IN   R23, MCUSR                                            ' get processor status register
!CBR  R23, 2^WDRF                                           ' clear watchdog reset flag
!OUT  MCUSR,R23                                             ' save it to status register
!LDI  R25, 2^WDE+2^WDP3+2^WDP0                              ' have new prescaler ready
!LDS  R24, WDTCSR                                           ' get watchdog control register
!ORI  R24, 2^WDCE+2^WDE                                     ' enable change
!STS  WDTCSR, R24                                           ' save
!STS  WDTCSR, R25                                           ' write new prescaler well within 4 cycles
!OUT  SREG, R22                                             ' restore global interrupt status

You can try and paste this code instead of Config Watchdog = 1024

My version 2.0.7.8
Back to top
View user's profile
Pzx

Bascom Member



Joined: 05 May 2015
Posts: 39

poland.gif
PostPosted: Thu Aug 18, 2016 7:21 pm    Post subject: Reply with quote

Hello, thanks for all your replays Smile

Same more claryfication regarding my hardware:
no Bootloader
external 16MHz cristal oscilator

Also I find interesting info in the Atmel ATMEGA2560 pdf in the Watchdog Timer section:
(There is also timming issue about 4 clock cycles like Evert wrote before)

The Watchdog always on (WDTON) fuse, if programmed, will force the Watchdog Timer to System Reset mode.
With the fuse programmed the System Reset mode bit (WDE) and Interrupt mode bit (WDIE) are locked to 1 and 0
respectively. To further ensure program security, alterations to the Watchdog set-up must follow timed sequences.
The sequence for clearing WDE and changing time-out configuration is as follows:
1. In the same operation, write a logic one to the Watchdog change enable bit (WDCE) and WDE. A logic one
must be written to WDE regardless of the previous value of the WDE bit.
2. Within the next four clock cycles, write the WDE and Watchdog prescaler bits (WDP) as desired, but with
the WDCE bit cleared. This must be done in one operation.

There is also some asm code example:

WDT_off:
; Turn off global interrupt
cli
; Reset Watchdog Timer
wdr
; Clear WDRF in MCUSR
in r16, MCUSR
andi r16, (0xff & (0<<WDRF))
out MCUSR, r16
; Write logical one to WDCE and WDE
; Keep old prescaler setting to prevent unintentional time-out
ldi r16, WDTCSR
ori r16, (1<<WDCE) | (1<<WDE)
sts WDTCSR, r16
; Turn off WDT
ldi r16, (0<<WDE)
sts WDTCSR, r16
; Turn on global interrupt
sei
ret

Im not sure what to do with this code. Is it watchdog configure code only or is it reset Watchdog procedure that I should run periodicly? Can I then use normal Reset Watchdog Bascom function? Unfortunately I am not familiar with asm (thats why I am using Bascom Smile ) so how to implemant this asm code into my Bascom test code?.
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Thu Aug 18, 2016 7:58 pm    Post subject: Reply with quote

Pzx wrote:
Im not sure what to do with this code.

For example learn from it?
Quote:
Is it watchdog configure code only or is it reset Watchdog procedure that I should run periodicly?

What would the name of the routine WDT_off: tell you about it's purpose?
Quote:
Can I then use normal Reset Watchdog Bascom function?

Sure.
Quote:
Unfortunately I am not familiar with asm

You also have some more problems to open your eyes, LOL.
Quote:
so how to implemant this asm code into my Bascom test code?.

If you'd actually succeed to implement this asm code, you would end trying to switch off a watchdog, which can't be forced off.
Why do you think the fuse is called watchdog always on fuse and why can you read about WDE locked in the part of the datasheet which you've quoted?
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 1:36 am    Post subject: Reply with quote

If your operation clock is 16MHz, that normally work by my Arduino MEGA250 board.
When Wait was put at the beginning of the program, I didn't normally move, so the whole clearance time of SRAM was doubted.

$Initmicro after the instruction, because the Watchdog is initialized to the minimum of 16ms, it makes no sense to write the ASM code.
Code:

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

'  Waitms 20                                                'Normal operation.
   Waitms 20                                                'Abnormal movement.

   Config Portb.7 = Output

   Config Watchdog = 1024
   Start Watchdog

   Do
      Reset Watchdog
      Toggle Portb.7
      Wait 1
   Loop

   End
 


To MWS.
Even your English post, meaning do not know to perform the automatic translation.
Back to top
View user's profile Visit poster's website
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Fri Aug 19, 2016 2:16 am    Post subject: Reply with quote

O-Family wrote:
Even your English post, meaning do not know to perform the automatic translation.

What does this mean, do you translate yourself?
You're writing in riddles Very Happy
Back to top
View user's profile
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 971

poland.gif
PostPosted: Fri Aug 19, 2016 10:33 am    Post subject: Reply with quote

I test this with m2560 with WD FuseBit on.

EDIT: I found funny issue. BOOTRST FuseBit must be disabled while programming. I don`t have time to investigation this Very Happy

This code work perfectly Wink
Code:
$regfile = "m2560def.dat"
$crystal = 16000000
$hwstack = 64
$swstack = 32
$framesize = 128

Config Watchdog = 1024
Reset Watchdog


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

Do

 Toggle Led
  Wait 1
   Reset Watchdog

Loop
End
 


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

$asm
!IN   R22, SREG                                             ' save status of global interrupts
!CLI                                                        ' disable interrupts
!IN   R23, MCUSR                                            ' get processor status register
!CBR  R23, 2^WDRF                                           ' clear watchdog reset flag
!OUT  MCUSR,R23                                             ' save it to status register
!LDI  R25, 2^WDE+2^WDP3+2^WDP0                              ' have new prescaler ready
!LDS  R24, WDTCSR                                           ' get watchdog control register
!ORI  R24, 2^WDCE+2^WDE                                     ' enable change
!STS  WDTCSR, R24                                           ' save
!STS  WDTCSR, R25                                           ' write new prescaler well within 4 cycles
!OUT  SREG, R22                                             ' restore global interrupt status
$end Asm

Reset Watchdog

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

Do

 Toggle Led
  Wait 1
   Reset Watchdog

Loop
End



..but interesting is that this code don`t work Very Happy
Code:
$regfile = "m2560def.dat"
$crystal = 16000000
$hwstack = 64
$swstack = 32
$framesize = 128

$initmicro

Reset Watchdog

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

Do

 Toggle Led
  Wait 1
   Reset Watchdog

Loop
End

_init_micro:
$asm
!IN   R22, SREG                                             ' save status of global interrupts
!CLI                                                        ' disable interrupts
!IN   R23, MCUSR                                            ' get processor status register
!CBR  R23, 2^WDRF                                           ' clear watchdog reset flag
!OUT  MCUSR,R23                                             ' save it to status register
!LDI  R25, 2^WDE+2^WDP3+2^WDP0                              ' have new prescaler ready
!LDS  R24, WDTCSR                                           ' get watchdog control register
!ORI  R24, 2^WDCE+2^WDE                                     ' enable change
!STS  WDTCSR, R24                                           ' save
!STS  WDTCSR, R25                                           ' write new prescaler well within 4 cycles
!OUT  SREG, R22                                             ' restore global interrupt status
$end Asm
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 1, 2, 3  Next
Page 1 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