Watchdog Timer - a WDT.bas System.bas and sample that works

Coding and general discussion relating to user created compiler modules

Moderators: David Barker, Jerry Messina

Post Reply
TonyR
Posts: 75
Joined: Fri Jan 14, 2011 11:49 pm
Location: Sydney
Contact:

Watchdog Timer - a WDT.bas System.bas and sample that works

Post by TonyR » Tue Aug 11, 2015 12:08 am

Hi Guys,

I need a watchdog working for my 18F87J93. In the past I tried various combinations of the wdt.bas, system.bas and sample code from the forum and wiki but couldnt find any that compiled without errors.

Im trying not to reverse engineer through the code to get it to work since it takes such a lot of time! I gave up back then and put a 4060 RC oscillator watchdog on my PCB because I knew it would be guaranteed to work quickly and it did.

I just tried again and don't seem to be able to find a known combination of those modules that works. (Without hours of reverse engineering). If anyone can point me to which modules I should put together to get a working start that would be much appreciated!

Jerry Messina
Swordfish Developer
Posts: 1235
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: Watchdog Timer - a WDT.bas System.bas and sample that wo

Post by Jerry Messina » Tue Aug 11, 2015 11:46 am

Things have gotten a bit tricky with the WDT over time... devices use different config settings and names.
Depending on how you want things to work, setting the config settings ON and OFF isn't so simple anymore,
You can thank Microchip for that. See my post hints for using '#const _wdt_type', option WDT, and system for more details... the info here is a rehash of that.

The device files generated by SystemConvert v1.2 try to help with this. I'd recommend rebuilding the device include files with it,
but I've included a converted file for the 18F87J93 in the zip file.

The attached zip file also has replacements for System.bas and wdt.bas. You'll want these as well. It corrects
some of the following limitations:

- There's a hard-coded check in the SF system library System.bas that needs to change
Right now, it only looks for one device

Code: Select all

#if WDT
   #if _device in (18F66J16)
   Config WDTEN = on
   #else
   Config WDT = on
   #endif
#endif

- The wdt.bas module from http://www.sfcompiler.co.uk/wiki/pmwiki ... oftwareWDT
needs some adjusting for different devices, so see the attached. Be VERY careful if you want
to change the timeout value in code... you'll have to match the module code/settings to your device.


Here's a test program that you can use to verify the modules with
main_wdt.bas:

Code: Select all

// watchdog test for module wdt.bas
device = 18F87J93

{
// _wdt_type was introduced in device files generated by SystemConvert v1.2
// to help set proper config settings for the WDT, which can vary by device

'#const _wdt_type' can be used to identify the watchdog config type
    0   WDT(WDT) = [OFF, ON]
    1   WDTEN(WDTEN) = [OFF, ON]
    2   WDTEN(WDTEN) = [OFF, NOSLP, SWON, ON]
    3   WDTEN(WDTEN) = [OFF, NOSLP, ON, SWDTDIS]

for _wdt_type = 0  (18F4520 SF default device):
    config WDT: Watchdog Timer Enable bit
    1 = WDT enabled
    0 = WDT disabled (control is placed on the SWDTEN bit)

for _wdt_type = 1  (18F14K50):
    config WDTEN: Watchdog Timer Enable bit
    1 = WDT is always enabled. SWDTEN bit has no effect
    0 = WDT is controlled by SWDTEN bit of the WDTCON register

for _wdt_type = 2 (18F2xK22):
    config WDTEN<1:0>: Watchdog Timer Enable bits
    11 (ON)    = WDT enabled in hardware (SWDTEN ignored)
    10 (SWON)  = WDT controlled by firmware (SWDTEN enabled)
    01 (NOSLP) = WDT enabled in hardware, disabled in Sleep mode (SWDTEN ignored)
    00 (OFF)   = WDT disabled in hardware (SWDTEN ignored)

for _wdt_type = 3 (18F2xK80):
    WDTEN<1:0>: Watchdog Timer Enable bits
    11 (SWDTDIS)= WDT is enabled in hardware; SWDTEN bit is disabled
    10 (ON)     = WDT is controlled by the SWDTEN bit setting
    01 (NOSLP)  = WDT is enabled only while the device is active and is disabled in Sleep mode; SWDTEN bit is disabled
    00 (OFF)    = WDT is disabled in hardware; SWDTEN bit is disabled

most devices have a register bit SWDTEN somewhere that allows for software control of the wdt... 
usually found in WDTCON.bits(0) except for the J94/J99 family where it's RCON2.bits(5)

_wdt_types 0 and 1 are similar, except that the config bit changes names from WDT to WDTEN.
likewise, _wdt_types 2 and 3 are similar except the settings change meanings slightly, esp ON
}

include "wdt.bas"		// automatically sets '#option WDT = true for you

// check to see if ClrWDT() produces a 'clrwdt' instruction
// after compiling press F2 to open an asm code window. scroll down to where the code begins
// (or use menu 'Edit | Find' MAIN). Verify that there's a 'clrwdt' instruction generated
//  MAIN
//  ?I000020_F000_000063_M000000 ; L#MK CLRWDT()
//      CLRWDT
ClrWDT()

// check system SBDLYUS16 for clrwdt
// in the asm code window use menu 'Edit | Find' SBDLYUS16
// verify that there's a 'clrwdt' instruction a few instructions before the return
//  SBDLYUS16
//      <snip>...
//      ADDWF 0,1,0
//      CLRWDT          << verify this is here
//      INCFSZ 1,1,0
//      BRA $ - 16
//      RETURN 0
delayus(100)

// this should call SBDLYUS16 (so it'll include a clrwdt)
delayms(100)

// set register bit SWDTEN wdt enable
// this may not have any effect depending on the config setting
watchdog.enabled = true

// tickle the watchdog
watchdog.reset

// this routine is no longer required... you can use normal DelayMS() calls
watchdog.delay(100)

// change wdog config timeout value
// **IMPORTANT**  see the notes in wdt.bas before using this... 
// it likely needs changing to match your device
watchdog.postscale(ps1024)
Attachments
wdt_routines.zip
(6.16 KiB) Downloaded 102 times

TonyR
Posts: 75
Joined: Fri Jan 14, 2011 11:49 pm
Location: Sydney
Contact:

Re: Watchdog Timer - a WDT.bas System.bas and sample that wo

Post by TonyR » Wed Aug 12, 2015 12:08 am

Thanks a lot for taking time to reply Jerry. I found a quick easy way to get what I needed by setting the WDT with the config statement and tickling it with the ClrWDT routine. It works just like my 4060 watchdog.

Code: Select all

Device = 18F87J93                    ' select our device
Clock = 12

Config OSC = HS, WDTPS = 2048, WDTEN = ON 

Public Inline Sub ClrWDT()
   Asm-
      ClrWDT
   End Asm  
End Sub

Include "usart2.bas"

USART2.SetBaudrate(br9600)
USART2.Write ("Module boot up...now 20 sec delay to see what wdt does ", 13, 10)



DelayMS(5000)
USART2.Write ("5", 13, 10)
ClrWDT
DelayMS(5000)
USART2.Write ("10", 13, 10)
ClrWDT
DelayMS(5000)
USART2.Write ("15", 13, 10)
ClrWDT
DelayMS(5000)
USART2.Write ("20", 13, 10)
ClrWDT


USART2.Write ("Got through 20 seconds without rebooting ...", 13, 10)


While true
Wend

Jerry Messina
Swordfish Developer
Posts: 1235
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: Watchdog Timer - a WDT.bas System.bas and sample that wo

Post by Jerry Messina » Wed Aug 12, 2015 10:09 am

There's two problems with doing it that way -

1) Any library calls that use ClearWDT won't tickle the WDT, so the DelayMS(), USART2.ReadByte(), and USART2.WriteByte() routines won't call it.

2) Likewise, any files that you include in your main program won't be able to see your ClearWDT() routine either, so they won't be able to use it.

The way to fix #1 is to set '#option WDT = true' in your main program before any include files, but once you do that you'll be back to getting a compile error.

If you want to hack system.bas to add in your processor you could change it to:

Code: Select all

{
****************************************************************************
* Name    : ClrWDT                                                         *
* Purpose : Clear the watch dog timer                                      *
****************************************************************************
}
#option WDT = false
#if IsOption(WDT) And Not (WDT in (true, false))
   #error WDT, "Invalid option. WDT must be true or false."
#endif
#if WDT
   #if _device in (18F66J16, 18F87J93)         // add support for 18F87J93
   Config WDTEN = on
   #else
   Config WDT = on
   #endif
#endif
Public Inline Sub ClrWDT()
   #if WDT
   Asm-
      ClrWDT
   End Asm
   #endif   
End Sub
That way when you add '#option WDT = true' to your main code it'll fix both issues #1 and #2.

You won't be able to use the Watchdog module from the wiki (wdt.bas) without some changes to it.

Post Reply