Previous

Next

Bottom

Contents

Glossary

Index

 

Persistor CF1 User's Manual

WatchDog Timer Example

October 1998

Revision 1.00

 Persistor Instruments Inc.
© 1998 All rights reserved.

Description

This examples demonstrates how to use the 68338 WatchDog Timer (WDT). The basic function of a WDT is to bring a wildly errant program back into some kind of deterministic state by resetting the CPU if the WDT doesn't receive timely notification that the system is operating correctly. You do not need to know about the WDT operation if your program does not need this functionality. If you do nothing to explicitly activate WDT, PicoDOS will automatically disable its operation. The CodeWarrior project and source code for this example are in the examples directory at:

C:\Program Files\CodeWarrior Pro ?\Metrowerks CodeWarrior\MotoCross Support\CF1\Examples\CF1 Watchdog\

Understanding the example

Keep in mind the following:

  1. WDT is a seldom used feature.
  2. WDT is enabled in hardware by default at reset.
  3. WDT must be disabled by PBM or PicoDOS when in command mode.
  4. WDT control registers can only be written once after a reset.

Given that, WDT is only enabled when applications burned into flash are setup to automatically take over at startup, and the onus is on developers who need WDT to take steps to keep from having it disabled by PicoDOS. The key to preventing WDT disabling is hidden in the file CF1.PicoDOS.MSLC.c which is in the CF1 support group of CF1 example projects and stationery. The function _pre_main_() in this module gets control when any CF1 first starts running to perform appropriate initializations before entering your applications main() function.

You should not make changes to the CF1.PicoDOS.MSLC.c module! If it appears that you have to, clone it, rename it, save it in your project folder (so it doesn't get wiped away on a CodeWarrior update) and make changes to what is now a different C source module. You should also let us know why you had to make changes so we can fine tune our strategy to help prevent this from being necessary.

Near the top of the _pre_main_() function, there's a small block of conditionally compiled code that looks something like:

#ifdef CUSTOM_SYPCR
    SYPCRInit(CustomSYPCR);   // whatever is appropriate for you
    TickleSWSR();             // make it take effect
#endif

Since CUSTOM_SYPCR is by default undefined, this code never gets executed, so SYPCRInit() never gets invoked. Regardless of that, applications running from flash will shortly make the PICOInit() call that ultimately loads the WDT with defaults to shut down WDT. Since WDT is a write once register, if you do it first, it stays what you set it to. So the key is to define CUSTOM_SYPCR, but to do so without modifying the source module. The only way to do that is to make sure that CF1.PicoDOS.MSLC.c #includes another file that defines CUSTOM_SYPCR. The best way to that (with the CodeWarrior compiler) is to make a change to the default Prefix File that is defined by the project settings.

To refresh, the prefix file is a single source file that gets included by every other source file in the project. Since the prefix file can contain other includes as well as definitions and compiler control statements, it's a very powerful tool for controlling compilation. You get to the project settings either from the Edit Menu or clicking on the Targets tab in the project window. In the Language Settings group is panel called C/C++ Language where the Prefix File is defined. The standard definition for CF1 examples and stationery is <cf1MxStd.h> and it can be found in the CF1\Headers folder. Also in that folder is a file called <cf1MxStd+WDT.h> that exists solely to let you keep the WDT activated by defining CUSTOM_SYPCR and declaring an external short named CustomSYPCR that you'll supply in one of your source modules. Beyond that <cf1MxStd+WDT.h> simply includes <cf1MxStd.h> to define standard CF1 behaviors.

In the Prefix File text box, replace cf1MxStd.h with cf1MxStd+WDT.h and click save.

If you did that and nothing else to a previously working project, attempting to make the project would evoke a linker error message because it couldn't find a referenced global variable named CustomSYPCR. If you look at CF1 Watchdog.c, you'll see something the following line:

// This definition is the key to using the WDT
short  CustomSYPCR = WDT6_5s | HaltMonEnable | BusMonEnable | BMT32;

This assigns a compile time value to CustomSYPCR which will be used by the _pre_main_() function the next time your application is run automatically from startup. That particular line ORs definitions contained in the BIOS section of <cf1bios.h> to select a watchdog timeout of 6.5 seconds (from a range of 13ms to 419 seconds), along with other standard bus monitor protection features that you will also probably want to adopt (read the MC68CK338 and 6833x SIM reference if you want to know more).

The first part of the example identifies the program and build. The next bit of code uses a BIOS inline assembly function named RunningFromFlashAsm () to safely test if this program is running from flash memory. It prints an appropriate message and exits if the program is running in flash, because if so, PicoDOS has already disabled WDT and nothing interesting can happen.

The final bit of code implements a dead-man timeout. If you don't keep hitting a key, and continue to do so within 6.5 seconds of each other, the system will get reset by the WDT.

Adding WDT protection to your application

  1. Choose an appropriate timeout from list in <cf1bios.h>.
  2. Copy the CustomSYPCR definintion from the CF1 Watchdog.c and modify the timeout as appropriate.
  3. Mofify your projects Language Settings/C/C++ Language/Prefix File to use <cf1MxStd+WDT.h>
  4. Sprinkle your program with TickleSWSR() calls (inline macros) as necessary to hold off a WDT reset.
  5. Conclude your program with a BIOSResetToPicoDOS() if you want to enter the PicoDOS command shell.

 

 

Previous

Next

Top

Contents

Glossary

Index

Tel: 508-759-6434

Fax: 508-759-6436

Copyright (C) 1998 Persistor Instruments Inc. - All Rights Reserved