WatchDog with generalization and specialization for Freescale FRDM K64F. Tested and working - also in multi-threaded environment.

Watchdog with generalization and specialization for Freescale K64F Tested on the FRDM K64F from Freescale Can be specialized for any type of the processor as long as one watchdog kicker exists only

Based on hints from https://os.mbed.com/users/manitou/code/k64f_wdog/ but reworked.

Copyright (c) 2019 Waldemar Dworakowski,

/*-

  • Copyright (c) 2019 Waldemar Dworakowski,
  • All Rights Reserved
  • Redistribution and use in source and binary forms, with or without
  • modification, are permitted provided that the following conditions are
  • met:
  • 1. Redistributions of source code must retain the above copyright notice,
  • this list of conditions and the following disclaimer.
  • 2. Redistributions in binary form must reproduce the above copyright
  • notice, this list of conditions and the following disclaimer in the
  • documentation and/or other materials provided with the distribution.
  • 3. Complete source form of any/all derivative/s and it's components must be
  • provided without any cost until last copy of the software (or derivative
  • of) exists.
  • 4. Any derivative must use same licence.
  • THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
  • IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  • WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  • DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
  • FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  • CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  • SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  • BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  • WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  • OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  • IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  • /

Usage:

  1. include "mbed.h"
  2. include "WatchDog.h"

WatchDog WatchDog wd;

int main(void) { wd.Start(1000); Watchdog 1000ms

printf("\r\nON - restart %d cause %4X\n\r", wd.RestartCounter(), (int)wd.RestartCause());

while(wd.RestartCounter()<10) { ThisThread::sleep_for(200); wait(200ms) wd.Kick(); we kick the watchdog to not die if not error. On Error (or when we are not getting time from other threads) then restart. }

wd.Stop();

printf("\r\nOFF! %d restarts \n\r", wd.RestartCounter());

return 0; }

Committer:
Pythia
Date:
Tue Oct 29 12:08:02 2019 +0000
Revision:
1:69a4a19ae933
Parent:
0:9363a65b371b
removal of remains from larger projects

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Pythia 0:9363a65b371b 1 #include "mbed.h"
Pythia 0:9363a65b371b 2 #include "system_def.h"
Pythia 0:9363a65b371b 3 #include "WatchDog.h"
Pythia 0:9363a65b371b 4
Pythia 0:9363a65b371b 5 /*
Pythia 0:9363a65b371b 6 * Implementation for FRDM-K64F
Pythia 0:9363a65b371b 7 */
Pythia 0:9363a65b371b 8
Pythia 0:9363a65b371b 9 /*-
Pythia 0:9363a65b371b 10 * Copyright (c) 2019 Waldemar Dworakowski,
Pythia 0:9363a65b371b 11 *
Pythia 0:9363a65b371b 12 * All Rights Reserved
Pythia 0:9363a65b371b 13 *
Pythia 0:9363a65b371b 14 * Redistribution and use in source and binary forms, with or without
Pythia 0:9363a65b371b 15 * modification, are permitted provided that the following conditions are
Pythia 0:9363a65b371b 16 * met:
Pythia 0:9363a65b371b 17 * 1. Redistributions of source code must retain the above copyright notice,
Pythia 0:9363a65b371b 18 * this list of conditions and the following disclaimer.
Pythia 0:9363a65b371b 19 * 2. Redistributions in binary form must reproduce the above copyright
Pythia 0:9363a65b371b 20 * notice, this list of conditions and the following disclaimer in the
Pythia 0:9363a65b371b 21 * documentation and/or other materials provided with the distribution.
Pythia 0:9363a65b371b 22 * 3. Complete source form of any/all derivative/s and it's components must be
Pythia 0:9363a65b371b 23 * provided without any cost.
Pythia 0:9363a65b371b 24 * 4. Any derivative must use same licence.
Pythia 0:9363a65b371b 25 *
Pythia 0:9363a65b371b 26 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
Pythia 0:9363a65b371b 27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
Pythia 0:9363a65b371b 28 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Pythia 0:9363a65b371b 29 * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
Pythia 0:9363a65b371b 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
Pythia 0:9363a65b371b 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
Pythia 0:9363a65b371b 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
Pythia 0:9363a65b371b 33 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
Pythia 0:9363a65b371b 34 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
Pythia 0:9363a65b371b 35 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
Pythia 0:9363a65b371b 36 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Pythia 0:9363a65b371b 37 */
Pythia 0:9363a65b371b 38
Pythia 0:9363a65b371b 39 WatchDog::WatchDog(void)
Pythia 0:9363a65b371b 40 {
Pythia 0:9363a65b371b 41 DoomsDayTime = 0;
Pythia 0:9363a65b371b 42 int fbus = SystemCoreClock / (((SIM->CLKDIV1 & SIM_CLKDIV1_OUTDIV2_MASK) >> SIM_CLKDIV1_OUTDIV2_SHIFT) + 1);
Pythia 0:9363a65b371b 43 int flashbus = SystemCoreClock / (((SIM->CLKDIV1 & SIM_CLKDIV1_OUTDIV4_MASK) >> SIM_CLKDIV1_OUTDIV4_SHIFT) + 1);
Pythia 0:9363a65b371b 44
Pythia 0:9363a65b371b 45 printf("\nSystemCoreClock %d fbus %d flash %d %s %s\n",SystemCoreClock,fbus,flashbus,__TIME__,__DATE__);
Pythia 0:9363a65b371b 46
Pythia 0:9363a65b371b 47 c_SRS0=RCM->SRS0; // reset reason
Pythia 0:9363a65b371b 48 c_SRS1=RCM->SRS1;
Pythia 0:9363a65b371b 49 c_STCTRLH=WDOG->STCTRLH;
Pythia 0:9363a65b371b 50 c_PRESC=WDOG->PRESC;
Pythia 0:9363a65b371b 51 c_TOVALL=WDOG->TOVALL;
Pythia 0:9363a65b371b 52 c_TOVALH=WDOG->TOVALH;
Pythia 0:9363a65b371b 53 c_RSTCNT=WDOG->RSTCNT;
Pythia 0:9363a65b371b 54 }
Pythia 0:9363a65b371b 55
Pythia 0:9363a65b371b 56 void WatchDog::Start(uint32_t ms)
Pythia 0:9363a65b371b 57 {
Pythia 0:9363a65b371b 58 DoomsDayTime = ms;
Pythia 0:9363a65b371b 59
Pythia 0:9363a65b371b 60 __disable_irq();
Pythia 0:9363a65b371b 61 WDOG->UNLOCK = WDOG_UNLOCK_WDOGUNLOCK(0xC520); /* Key 1 */
Pythia 0:9363a65b371b 62 WDOG->UNLOCK = WDOG_UNLOCK_WDOGUNLOCK(0xD928); /* Key 2 */
Pythia 0:9363a65b371b 63 __NOP();
Pythia 0:9363a65b371b 64 __NOP();
Pythia 0:9363a65b371b 65
Pythia 0:9363a65b371b 66 WDOG->TOVALL = (uint16_t)(ms & 0xffff); // sets the time-out value
Pythia 0:9363a65b371b 67 WDOG->TOVALH = (uint16_t)((ms>>16) & 0xffff);
Pythia 0:9363a65b371b 68 WDOG->STCTRLH = (WDOG_STCTRLH_ALLOWUPDATE_MASK | WDOG_STCTRLH_WDOGEN_MASK | WDOG_STCTRLH_WAITEN_MASK | WDOG_STCTRLH_STOPEN_MASK); // Enable WDG
Pythia 0:9363a65b371b 69 WDOG->PRESC = 0; // prescaler
Pythia 0:9363a65b371b 70 __enable_irq();
Pythia 0:9363a65b371b 71 }
Pythia 0:9363a65b371b 72
Pythia 0:9363a65b371b 73 void WatchDog::Stop(void)
Pythia 0:9363a65b371b 74 {
Pythia 0:9363a65b371b 75 __disable_irq();
Pythia 0:9363a65b371b 76 WDOG->UNLOCK = WDOG_UNLOCK_WDOGUNLOCK(0xC520); /* Key 1 */
Pythia 0:9363a65b371b 77 WDOG->UNLOCK = WDOG_UNLOCK_WDOGUNLOCK(0xD928); /* Key 2 */
Pythia 0:9363a65b371b 78 __NOP();
Pythia 0:9363a65b371b 79 __NOP();
Pythia 0:9363a65b371b 80 WDOG->STCTRLH = WDOG_STCTRLH_ALLOWUPDATE_MASK;
Pythia 0:9363a65b371b 81 __enable_irq();
Pythia 0:9363a65b371b 82
Pythia 0:9363a65b371b 83 DoomsDayTime = 0;
Pythia 0:9363a65b371b 84 }
Pythia 0:9363a65b371b 85
Pythia 0:9363a65b371b 86 void WatchDog::Kick(void)
Pythia 0:9363a65b371b 87 {
Pythia 0:9363a65b371b 88 if (0 == DoomsDayTime)
Pythia 0:9363a65b371b 89 return;
Pythia 0:9363a65b371b 90
Pythia 0:9363a65b371b 91 __disable_irq();
Pythia 0:9363a65b371b 92 WDOG->REFRESH = 0xA602;
Pythia 0:9363a65b371b 93 WDOG->REFRESH = 0xB480;
Pythia 0:9363a65b371b 94 __enable_irq();
Pythia 0:9363a65b371b 95 return;
Pythia 0:9363a65b371b 96 }
Pythia 0:9363a65b371b 97
Pythia 0:9363a65b371b 98 uint32_t WatchDog::RestartCounter(void)
Pythia 0:9363a65b371b 99 {
Pythia 0:9363a65b371b 100 return this->c_RSTCNT;
Pythia 0:9363a65b371b 101 }
Pythia 0:9363a65b371b 102
Pythia 0:9363a65b371b 103 enum WatchDog::RestartCauses WatchDog::RestartCause(void)
Pythia 0:9363a65b371b 104 {
Pythia 0:9363a65b371b 105 switch(this->c_SRS0)
Pythia 0:9363a65b371b 106 {
Pythia 0:9363a65b371b 107 case 0x40: return RESET;
Pythia 0:9363a65b371b 108 case 0x20: return WATCHDOG;
Pythia 0:9363a65b371b 109 default: return UNKNOWN;
Pythia 0:9363a65b371b 110 }
Pythia 0:9363a65b371b 111 }
Pythia 0:9363a65b371b 112
Pythia 0:9363a65b371b 113 WatchDog::~WatchDog()
Pythia 0:9363a65b371b 114 {
Pythia 0:9363a65b371b 115 if (0 != DoomsDayTime) this->Stop();
Pythia 0:9363a65b371b 116 return;
Pythia 0:9363a65b371b 117 }