removed debug

Committer:
acihy
Date:
Mon Aug 07 07:48:29 2017 +0000
Revision:
7:76136c6dc64d
Parent:
6:40f189c7795b
Child:
8:c5a5935e59a4
Watchdog added

Who changed what in which revision?

UserRevisionLine numberNew contents of line
WiredHome 2:2873f068f325 1 /// @file Watchdog.cpp provides the interface to the Watchdog module
WiredHome 2:2873f068f325 2 ///
WiredHome 2:2873f068f325 3 /// This provides basic Watchdog service for the mbed. You can configure
WiredHome 2:2873f068f325 4 /// various timeout intervals that meet your system needs. Additionally,
WiredHome 2:2873f068f325 5 /// it is possible to identify if the Watchdog was the cause of any
WiredHome 2:2873f068f325 6 /// system restart.
WiredHome 2:2873f068f325 7 ///
WiredHome 2:2873f068f325 8 /// Adapted from Simon's Watchdog code from http://mbed.org/forum/mbed/topic/508/
WiredHome 2:2873f068f325 9 ///
WiredHome 2:2873f068f325 10 /// @note Copyright © 2011 by Smartware Computing, all rights reserved.
WiredHome 2:2873f068f325 11 /// This software may be used to derive new software, as long as
WiredHome 2:2873f068f325 12 /// this copyright statement remains in the source file.
WiredHome 2:2873f068f325 13 /// @author David Smart
WiredHome 2:2873f068f325 14 ///
nbremond 5:ea5595e2698b 15 /// @note Copyright © 2015 by NBRemond, all rights reserved.
nbremond 5:ea5595e2698b 16 /// This software may be used to derive new software, as long as
nbremond 5:ea5595e2698b 17 /// this copyright statement remains in the source file.
nbremond 5:ea5595e2698b 18 ///
nbremond 5:ea5595e2698b 19 /// Added support for STM32 Nucleo platforms
nbremond 5:ea5595e2698b 20 ///
nbremond 5:ea5595e2698b 21 /// @author Bernaérd Remond
nbremond 5:ea5595e2698b 22 ///
nbremond 5:ea5595e2698b 23
acihy 7:76136c6dc64d 24 //#define LPC
acihy 7:76136c6dc64d 25 #define ST_NUCLEO
nbremond 5:ea5595e2698b 26
nbremond 5:ea5595e2698b 27
WiredHome 0:7a316f14da9c 28 #include "mbed.h"
WiredHome 0:7a316f14da9c 29 #include "Watchdog.h"
WiredHome 0:7a316f14da9c 30
WiredHome 0:7a316f14da9c 31
WiredHome 0:7a316f14da9c 32 /// Watchdog gets instantiated at the module level
WiredHome 0:7a316f14da9c 33 Watchdog::Watchdog() {
nbremond 5:ea5595e2698b 34 #ifdef LPC
WiredHome 2:2873f068f325 35 wdreset = (LPC_WDT->WDMOD >> 2) & 1; // capture the cause of the previous reset
nbremond 5:ea5595e2698b 36 #endif
nbremond 5:ea5595e2698b 37 #ifdef ST_NUCLEO
nbremond 5:ea5595e2698b 38 // capture the cause of the previous reset
nbremond 5:ea5595e2698b 39 /* Check if the system has resumed from IWDG reset */
nbremond 5:ea5595e2698b 40 /*
nbremond 5:ea5595e2698b 41 if (__HAL_RCC_GET_FLAG(RCC_FLAG_IWDGRST)) {
nbremond 5:ea5595e2698b 42 wdreset = true;
nbremond 5:ea5595e2698b 43 }
nbremond 5:ea5595e2698b 44 else {
nbremond 5:ea5595e2698b 45 wdreset = false;
nbremond 5:ea5595e2698b 46 }
nbremond 5:ea5595e2698b 47 */
nbremond 5:ea5595e2698b 48 wdreset = false;
nbremond 5:ea5595e2698b 49 #endif
nbremond 5:ea5595e2698b 50
WiredHome 0:7a316f14da9c 51 }
WiredHome 0:7a316f14da9c 52
WiredHome 0:7a316f14da9c 53 /// Load timeout value in watchdog timer and enable
nbremond 5:ea5595e2698b 54 void Watchdog::Configure(float timeout) {
nbremond 5:ea5595e2698b 55 #ifdef LPC
WiredHome 0:7a316f14da9c 56 LPC_WDT->WDCLKSEL = 0x1; // Set CLK src to PCLK
WiredHome 0:7a316f14da9c 57 uint32_t clk = SystemCoreClock / 16; // WD has a fixed /4 prescaler, PCLK default is /4
nbremond 5:ea5595e2698b 58 LPC_WDT->WDTC = (uint32_t)(timeout * (float)clk);
WiredHome 0:7a316f14da9c 59 LPC_WDT->WDMOD = 0x3; // Enabled and Reset
nbremond 5:ea5595e2698b 60 #endif
nbremond 5:ea5595e2698b 61 #ifdef ST_NUCLEO
nbremond 5:ea5595e2698b 62 // see http://embedded-lab.com/blog/?p=9662
nbremond 5:ea5595e2698b 63 #define LsiFreq (45000)
nbremond 5:ea5595e2698b 64
nbremond 5:ea5595e2698b 65 uint16_t PrescalerCode;
nbremond 5:ea5595e2698b 66 uint16_t Prescaler;
nbremond 5:ea5595e2698b 67 uint16_t ReloadValue;
nbremond 5:ea5595e2698b 68 float Calculated_timeout;
nbremond 5:ea5595e2698b 69
nbremond 5:ea5595e2698b 70 if ((timeout * (LsiFreq/4)) < 0x7FF) {
nbremond 5:ea5595e2698b 71 PrescalerCode = IWDG_PRESCALER_4;
nbremond 5:ea5595e2698b 72 Prescaler = 4;
nbremond 5:ea5595e2698b 73 }
nbremond 5:ea5595e2698b 74 else if ((timeout * (LsiFreq/8)) < 0xFF0) {
nbremond 5:ea5595e2698b 75 PrescalerCode = IWDG_PRESCALER_8;
nbremond 5:ea5595e2698b 76 Prescaler = 8;
nbremond 5:ea5595e2698b 77 }
nbremond 5:ea5595e2698b 78 else if ((timeout * (LsiFreq/16)) < 0xFF0) {
nbremond 5:ea5595e2698b 79 PrescalerCode = IWDG_PRESCALER_16;
nbremond 5:ea5595e2698b 80 Prescaler = 16;
nbremond 5:ea5595e2698b 81 }
nbremond 5:ea5595e2698b 82 else if ((timeout * (LsiFreq/32)) < 0xFF0) {
nbremond 5:ea5595e2698b 83 PrescalerCode = IWDG_PRESCALER_32;
nbremond 5:ea5595e2698b 84 Prescaler = 32;
nbremond 5:ea5595e2698b 85 }
nbremond 5:ea5595e2698b 86 else if ((timeout * (LsiFreq/64)) < 0xFF0) {
nbremond 5:ea5595e2698b 87 PrescalerCode = IWDG_PRESCALER_64;
nbremond 5:ea5595e2698b 88 Prescaler = 64;
nbremond 5:ea5595e2698b 89 }
nbremond 5:ea5595e2698b 90 else if ((timeout * (LsiFreq/128)) < 0xFF0) {
nbremond 5:ea5595e2698b 91 PrescalerCode = IWDG_PRESCALER_128;
nbremond 5:ea5595e2698b 92 Prescaler = 128;
nbremond 5:ea5595e2698b 93 }
nbremond 5:ea5595e2698b 94 else {
nbremond 5:ea5595e2698b 95 PrescalerCode = IWDG_PRESCALER_256;
nbremond 5:ea5595e2698b 96 Prescaler = 256;
nbremond 5:ea5595e2698b 97 }
nbremond 5:ea5595e2698b 98
nbremond 5:ea5595e2698b 99 // specifies the IWDG Reload value. This parameter must be a number between 0 and 0x0FFF.
nbremond 5:ea5595e2698b 100 ReloadValue = (uint32_t)(timeout * (LsiFreq/Prescaler));
nbremond 5:ea5595e2698b 101
nbremond 5:ea5595e2698b 102 Calculated_timeout = ((float)(Prescaler * ReloadValue)) / LsiFreq;
nbremond 5:ea5595e2698b 103 printf("WATCHDOG set with prescaler:%d reload value: 0x%X - timeout:%f\n",Prescaler, ReloadValue, Calculated_timeout);
nbremond 5:ea5595e2698b 104
nbremond 5:ea5595e2698b 105 IWDG->KR = 0x5555; //Disable write protection of IWDG registers
nbremond 5:ea5595e2698b 106 IWDG->PR = PrescalerCode; //Set PR value
nbremond 5:ea5595e2698b 107 IWDG->RLR = ReloadValue; //Set RLR value
nbremond 5:ea5595e2698b 108 IWDG->KR = 0xAAAA; //Reload IWDG
nbremond 5:ea5595e2698b 109 IWDG->KR = 0xCCCC; //Start IWDG - See more at: http://embedded-lab.com/blog/?p=9662#sthash.6VNxVSn0.dpuf
nbremond 5:ea5595e2698b 110 #endif
nbremond 5:ea5595e2698b 111
WiredHome 0:7a316f14da9c 112 Service();
WiredHome 0:7a316f14da9c 113 }
WiredHome 0:7a316f14da9c 114
WiredHome 0:7a316f14da9c 115 /// "Service", "kick" or "feed" the dog - reset the watchdog timer
WiredHome 0:7a316f14da9c 116 /// by writing this required bit pattern
WiredHome 0:7a316f14da9c 117 void Watchdog::Service() {
nbremond 5:ea5595e2698b 118 #ifdef LPC
WiredHome 0:7a316f14da9c 119 LPC_WDT->WDFEED = 0xAA;
WiredHome 0:7a316f14da9c 120 LPC_WDT->WDFEED = 0x55;
nbremond 5:ea5595e2698b 121 #endif
nbremond 5:ea5595e2698b 122 #ifdef ST_NUCLEO
nbremond 5:ea5595e2698b 123 IWDG->KR = 0xAAAA; //Reload IWDG - See more at: http://embedded-lab.com/blog/?p=9662#sthash.6VNxVSn0.dpuf
nbremond 5:ea5595e2698b 124 #endif
WiredHome 0:7a316f14da9c 125 }
WiredHome 0:7a316f14da9c 126
WiredHome 0:7a316f14da9c 127 /// get the flag to indicate if the watchdog causes the reset
WiredHome 0:7a316f14da9c 128 bool Watchdog::WatchdogCausedReset() {
WiredHome 0:7a316f14da9c 129 return wdreset;
WiredHome 0:7a316f14da9c 130 }
WiredHome 0:7a316f14da9c 131
WiredHome 0:7a316f14da9c 132