|
|
View previous topic :: View next topic |
Author |
Message |
kgng97ccs
Joined: 02 Apr 2022 Posts: 97
|
Using the watchdog timer |
Posted: Sun Apr 16, 2023 4:51 am |
|
|
I am using the PIC18LF46K22 MCU and CCS C compiler v5.115 under MPLAB IDE v8.92.
I am trying to understand the various ways in which the watchdog timer for this MCU can be set up.
Here is what I gathered from the CCS compiler manual, CCSinfo forum, and my own tests:
Code: | /* Option 1: WDT can be turned ON and OFF in code, timeout set in fuses; if NOWDT
is not specified, the default is NOWDT */
#fuses NOWDT, WDT4096 /* timeout = 4*4096 = 16364 ms */
...
setup_wdt(wdt_on);
...
restart_wdt();
...
restart_wdt();
...
setup_wdt(wdt_off);
...
setup_wdt(wdt_on); |
Code: | /* Option 2: WDT always ON, timeout set in fuses */
#fuses WDT, WDT4096 /* timeout = 4*4096 = 16364 ms */
...
restart_wdt();
...
restart_wdt(); |
Code: | /* Option 3: everything can be set in code, WDT can be turned ON and OFF in code */
#fuses WDT_SW
...
setup_wdt(wdt_16); /* timeout = 16384 ms; wdt_16s did not work */
setup_wdt(wdt_on);
...
restart_wdt();
...
restart_wdt();
...
setup_wdt(wdt_off);
...
setup_wdt(wdt_on); |
Code: | /* Option 4: same as Option 3, except that NOWDT, instead of WDT_SW, is set in
the fuses */
#fuses NOWDT
...
setup_wdt(wdt_16s); /* timeout = 16383 ms */
setup_wdt(wdt_on);
...
restart_wdt();
...
restart_wdt();
...
setup_wdt(wdt_off);
...
setup_wdt(wdt_on) |
I will appreciate receiving feedback on whether my understanding is correct. Thank you. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Sun Apr 16, 2023 8:33 am |
|
|
First comment. In fact the timeout is _always_ set in the fuses.
When you set a time 'in code', the fuses are set to make this the
timeout. Look at the end of the listing file, where the fuses are shown
to see this. This is why you can only set a time once.
Your versions with NOWDT selected, should not work, However again
check the actual fuses. It is possible that the compiler is detecting that
you are trying to start the WDT, and automatically selecting WDT_SW.
Basically options that cannot work, the compiler _will_ often override.
No. The watchdog will not be being enabled in the first NOWDT version
at all. Or in the last version.
The actual fuse options are WDT off, software enable disabled,
WDT off software enable allowed, WDT on, WDT on except in sleep.
The names chosen for these reflect both CCS's own thoughts, and also
historical names from other chips.
If using the WDT, and wanting to make it really work well, do not
go scattering restart_wdt statements through the code. This makes the
watchdog resettable when the code is not running correctly. The right
procedure is to have one routine, that is flagged as separate, and have
this actually test the status of registers and operations, and only if these
are correct restart the watchdog. Call this when you want to reset the
watchdog. This way the restart can only actually happen, when everything
_is_ working correctly. |
|
|
kgng97ccs
Joined: 02 Apr 2022 Posts: 97
|
|
Posted: Sun Jun 25, 2023 4:35 am |
|
|
Thank you, Ttelmah, for your input.
From some testing, I observed the following:
1. If NOWDT is explicitly placed in the fuses, there seems to be no way to turn ON the watchdog timer.
2. Once WDT is in the fuses, there is no way to turn OFF the watchdog timer in the program (at least not by using setup_wdt(wdt_off)).
3. If only WDT4096 or any other period is specified in the fuses, either setup_wdt(wdt_on) or restart_wdt() will turn ON the watchdog timer.
4. If only WDT4096 or any other period is specified in the fuses, and there is no restart_wdt() in the program, NOWDT will show up in the fuses, but if there is a restart_wdt() in the program, WDT will show up in the fuses.
5. If WDT_SW is specified in the fuses, setup_wdt(wdt_on) will turn ON the watchdog, but restart_wdt() will not. However, after setup_wdt(wdt_on) is issued, the watchdog timer can be restarted by restart_wdt(), or by sequentially issuing setup_wdt(wdt_off) and setup_wdt(wdt_on). Also, if the watchdog timer is turned OFF by setup_wdt(wdt_off), it can be turned ON again only by setup_wdt(wdt_on), not by restart_wdt().
6. setup_wdt(wdt_off) will turn OFF the watchdog timer only if WDT_SW is specified in the fuses. This means that if WDT shows up in the fuses, setup_wdt(wdt_off) will not turn OFF the watchdog timer, and the only way to be able to turn the watchdog timer ON or OFF at will is to set the WDT_SW fuse. */
I hope my observations are correct.
Questions:
1. If WDT is set in the fuses, at which part of the program does the watchdog timer actually start?
2. If WDT_SW is set in the fuses, where is the earliest location in the program where I can issue setup_wdt(wdt_on) to start the watchdog timer? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Sun Jun 25, 2023 7:01 am |
|
|
That looks spot on.
The WDT starts after PUT at the start of the code. the start does reset the
time.
You can start it anywhere you want. The normal safe place would be
immediately after any initial settling delays needed by the hardware. |
|
|
kgng97ccs
Joined: 02 Apr 2022 Posts: 97
|
|
Posted: Wed Jun 28, 2023 1:56 am |
|
|
Thank you, Ttelmah.
1. Is there a CCS statement I can use to check, at any time, whether the watchdog timer is enabled (ON) or not (OFF)?
2. Is there a CCS statement I can use to check, at any time, whether the watchdog timer is running or not, running as, for example, when a restart_wdt() statement has been issued but the watchdog timeout has not yet occurred? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Wed Jun 28, 2023 2:59 am |
|
|
If the watchdog is enabled, then it is running. It is just a counter. On some
chips it can be read, but not on most. If it has expired, then the chip will
reset. It is not made accessible, since this potentially could allow it to
be corrupted. Normally if you want to know how far along it is, you just
use your own counter from the same clock. If the watchdog is not enabled,
you can test if it would have triggered by testing the WDTO bit.
You can test the fuse bits to see what mode the watchdog is in. The software
WDTE bit is available as a bit in a register. A simple #bit can be generated to
test this.
The actual bit names do change on some chips - data sheet. |
|
|
|
|
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
|
Powered by phpBB © 2001, 2005 phpBB Group
|