Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Watchdog.cpp
00001 /// @file Watchdog.cpp provides the interface to the Watchdog module 00002 /// 00003 /// This provides basic Watchdog service for the mbed. You can configure 00004 /// various timeout intervals that meet your system needs. Additionally, 00005 /// it is possible to identify if the Watchdog was the cause of any 00006 /// system restart. 00007 /// 00008 /// Adapted from Simon's Watchdog code from http://mbed.org/forum/mbed/topic/508/ 00009 /// 00010 /// @note Copyright © 2011 by Smartware Computing, all rights reserved. 00011 /// This software may be used to derive new software, as long as 00012 /// this copyright statement remains in the source file. 00013 /// @author David Smart 00014 /// 00015 /// @note Copyright © 2015 by NBRemond, all rights reserved. 00016 /// This software may be used to derive new software, as long as 00017 /// this copyright statement remains in the source file. 00018 /// 00019 /// Added support for STM32 Nucleo platforms 00020 /// 00021 /// @author Bernaérd Remond 00022 /// 00023 00024 //#define LPC 00025 #define ST_NUCLEO 00026 00027 00028 #include "mbed.h" 00029 #include "Watchdog.h" 00030 00031 00032 /// Watchdog gets instantiated at the module level 00033 Watchdog::Watchdog() { 00034 #ifdef LPC 00035 wdreset = (LPC_WDT->WDMOD >> 2) & 1; // capture the cause of the previous reset 00036 #endif 00037 #ifdef ST_NUCLEO 00038 // capture the cause of the previous reset 00039 /* Check if the system has resumed from IWDG reset */ 00040 /* 00041 if (__HAL_RCC_GET_FLAG(RCC_FLAG_IWDGRST)) { 00042 wdreset = true; 00043 } 00044 else { 00045 wdreset = false; 00046 } 00047 */ 00048 wdreset = false; 00049 #endif 00050 00051 } 00052 00053 /// Load timeout value in watchdog timer and enable 00054 void Watchdog::Configure(float timeout) { 00055 #ifdef LPC 00056 LPC_WDT->WDCLKSEL = 0x1; // Set CLK src to PCLK 00057 uint32_t clk = SystemCoreClock / 16; // WD has a fixed /4 prescaler, PCLK default is /4 00058 LPC_WDT->WDTC = (uint32_t)(timeout * (float)clk); 00059 LPC_WDT->WDMOD = 0x3; // Enabled and Reset 00060 #endif 00061 #ifdef ST_NUCLEO 00062 // see http://embedded-lab.com/blog/?p=9662 00063 #define LsiFreq (45000) 00064 00065 uint16_t PrescalerCode; 00066 uint16_t Prescaler; 00067 uint16_t ReloadValue; 00068 float Calculated_timeout; 00069 00070 if ((timeout * (LsiFreq/4)) < 0x7FF) { 00071 PrescalerCode = IWDG_PRESCALER_4; 00072 Prescaler = 4; 00073 } 00074 else if ((timeout * (LsiFreq/8)) < 0xFF0) { 00075 PrescalerCode = IWDG_PRESCALER_8; 00076 Prescaler = 8; 00077 } 00078 else if ((timeout * (LsiFreq/16)) < 0xFF0) { 00079 PrescalerCode = IWDG_PRESCALER_16; 00080 Prescaler = 16; 00081 } 00082 else if ((timeout * (LsiFreq/32)) < 0xFF0) { 00083 PrescalerCode = IWDG_PRESCALER_32; 00084 Prescaler = 32; 00085 } 00086 else if ((timeout * (LsiFreq/64)) < 0xFF0) { 00087 PrescalerCode = IWDG_PRESCALER_64; 00088 Prescaler = 64; 00089 } 00090 else if ((timeout * (LsiFreq/128)) < 0xFF0) { 00091 PrescalerCode = IWDG_PRESCALER_128; 00092 Prescaler = 128; 00093 } 00094 else { 00095 PrescalerCode = IWDG_PRESCALER_256; 00096 Prescaler = 256; 00097 } 00098 00099 // specifies the IWDG Reload value. This parameter must be a number between 0 and 0x0FFF. 00100 ReloadValue = (uint32_t)(timeout * (LsiFreq/Prescaler)); 00101 00102 Calculated_timeout = ((float)(Prescaler * ReloadValue)) / LsiFreq; 00103 //printf("WATCHDOG set with prescaler:%d reload value: 0x%X - timeout:%f\n",Prescaler, ReloadValue, Calculated_timeout); 00104 00105 IWDG->KR = 0x5555; //Disable write protection of IWDG registers 00106 IWDG->PR = PrescalerCode; //Set PR value 00107 IWDG->RLR = ReloadValue; //Set RLR value 00108 IWDG->KR = 0xAAAA; //Reload IWDG 00109 IWDG->KR = 0xCCCC; //Start IWDG - See more at: http://embedded-lab.com/blog/?p=9662#sthash.6VNxVSn0.dpuf 00110 #endif 00111 00112 Service(); 00113 } 00114 00115 /// "Service", "kick" or "feed" the dog - reset the watchdog timer 00116 /// by writing this required bit pattern 00117 void Watchdog::Service() { 00118 #ifdef LPC 00119 LPC_WDT->WDFEED = 0xAA; 00120 LPC_WDT->WDFEED = 0x55; 00121 #endif 00122 #ifdef ST_NUCLEO 00123 IWDG->KR = 0xAAAA; //Reload IWDG - See more at: http://embedded-lab.com/blog/?p=9662#sthash.6VNxVSn0.dpuf 00124 #endif 00125 } 00126 00127 /// get the flag to indicate if the watchdog causes the reset 00128 bool Watchdog::WatchdogCausedReset() { 00129 return wdreset; 00130 } 00131 00132
Generated on Sun Jul 24 2022 23:00:25 by
1.7.2