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 /// 00002 /// @author Bernaérd Remond 00003 /// 00004 00005 //#define LPC 00006 #define ST_NUCLEO 00007 00008 00009 #include "mbed.h" 00010 #include "Watchdog.h" 00011 00012 00013 /// Watchdog gets instantiated at the module level 00014 Watchdog::Watchdog() { 00015 #ifdef LPC 00016 wdreset = (LPC_WDT->WDMOD >> 2) & 1; // capture the cause of the previous reset 00017 #endif 00018 #ifdef ST_NUCLEO 00019 // capture the cause of the previous reset 00020 /* Check if the system has resumed from IWDG reset */ 00021 /* 00022 if (__HAL_RCC_GET_FLAG(RCC_FLAG_IWDGRST)) { 00023 wdreset = true; 00024 } 00025 else { 00026 wdreset = false; 00027 } 00028 */ 00029 wdreset = false; 00030 #endif 00031 00032 } 00033 00034 /// Load timeout value in watchdog timer and enable 00035 void Watchdog::Configure(float timeout) { 00036 #ifdef LPC 00037 LPC_WDT->WDCLKSEL = 0x1; // Set CLK src to PCLK 00038 uint32_t clk = SystemCoreClock / 16; // WD has a fixed /4 prescaler, PCLK default is /4 00039 LPC_WDT->WDTC = (uint32_t)(timeout * (float)clk); 00040 LPC_WDT->WDMOD = 0x3; // Enabled and Reset 00041 #endif 00042 #ifdef ST_NUCLEO 00043 // see http://embedded-lab.com/blog/?p=9662 00044 #define LsiFreq (45000) 00045 00046 uint16_t PrescalerCode; 00047 uint16_t Prescaler; 00048 uint16_t ReloadValue; 00049 float Calculated_timeout; 00050 00051 if ((timeout * (LsiFreq/4)) < 0x7FF) { 00052 PrescalerCode = IWDG_PRESCALER_4; 00053 Prescaler = 4; 00054 } 00055 else if ((timeout * (LsiFreq/8)) < 0xFF0) { 00056 PrescalerCode = IWDG_PRESCALER_8; 00057 Prescaler = 8; 00058 } 00059 else if ((timeout * (LsiFreq/16)) < 0xFF0) { 00060 PrescalerCode = IWDG_PRESCALER_16; 00061 Prescaler = 16; 00062 } 00063 else if ((timeout * (LsiFreq/32)) < 0xFF0) { 00064 PrescalerCode = IWDG_PRESCALER_32; 00065 Prescaler = 32; 00066 } 00067 else if ((timeout * (LsiFreq/64)) < 0xFF0) { 00068 PrescalerCode = IWDG_PRESCALER_64; 00069 Prescaler = 64; 00070 } 00071 else if ((timeout * (LsiFreq/128)) < 0xFF0) { 00072 PrescalerCode = IWDG_PRESCALER_128; 00073 Prescaler = 128; 00074 } 00075 else { 00076 PrescalerCode = IWDG_PRESCALER_256; 00077 Prescaler = 256; 00078 } 00079 00080 // specifies the IWDG Reload value. This parameter must be a number between 0 and 0x0FFF. 00081 ReloadValue = (uint32_t)(timeout * (LsiFreq/Prescaler)); 00082 00083 Calculated_timeout = ((float)(Prescaler * ReloadValue)) / LsiFreq; 00084 printf("\r\n\r\nWATCHDOG set with prescaler:%d reload value: 0x%X - timeout:%f",Prescaler, ReloadValue, Calculated_timeout); 00085 00086 IWDG->KR = 0x5555; //Disable write protection of IWDG registers 00087 IWDG->PR = PrescalerCode; //Set PR value 00088 IWDG->RLR = ReloadValue; //Set RLR value 00089 IWDG->KR = 0xAAAA; //Reload IWDG 00090 IWDG->KR = 0xCCCC; //Start IWDG - See more at: http://embedded-lab.com/blog/?p=9662#sthash.6VNxVSn0.dpuf 00091 #endif 00092 00093 Service(); 00094 } 00095 00096 /// "Service", "kick" or "feed" the dog - reset the watchdog timer 00097 /// by writing this required bit pattern 00098 void Watchdog::Service() { 00099 #ifdef LPC 00100 LPC_WDT->WDFEED = 0xAA; 00101 LPC_WDT->WDFEED = 0x55; 00102 #endif 00103 #ifdef ST_NUCLEO 00104 IWDG->KR = 0xAAAA; //Reload IWDG - See more at: http://embedded-lab.com/blog/?p=9662#sthash.6VNxVSn0.dpuf 00105 #endif 00106 } 00107 00108 /// get the flag to indicate if the watchdog causes the reset 00109 bool Watchdog::WatchdogCausedReset() { 00110 return wdreset; 00111 }
Generated on Tue Jul 19 2022 00:58:42 by
1.7.2