Eduardo Vergara
/
BNO080_program
added main and defined M_PI
Diff: Watchdog/Watchdog.cpp
- Revision:
- 8:729ad465d6c9
- Parent:
- 7:b548a684290d
- Child:
- 10:14374b492f1d
diff -r b548a684290d -r 729ad465d6c9 Watchdog/Watchdog.cpp --- a/Watchdog/Watchdog.cpp Tue Jul 30 21:06:04 2019 +0000 +++ b/Watchdog/Watchdog.cpp Mon Aug 05 21:57:13 2019 +0000 @@ -2,9 +2,9 @@ /// /// This provides basic Watchdog service for the mbed. You can configure /// various timeout intervals that meet your system needs. Additionally, -/// it is possible to identify if the Watchdog was the cause of any +/// it is possible to identify if the Watchdog was the cause of any /// system restart. -/// +/// /// Adapted from Simon's Watchdog code from http://mbed.org/forum/mbed/topic/508/ /// /// @note Copyright © 2011 by Smartware Computing, all rights reserved. @@ -27,106 +27,113 @@ #include "mbed.h" #include "Watchdog.h" - +#include "BNO080.h" /// Watchdog gets instantiated at the module level -Watchdog::Watchdog() { -#ifdef LPC +Watchdog::Watchdog(BNO080 *imu) +{ +#ifdef LPC wdreset = (LPC_WDT->WDMOD >> 2) & 1; // capture the cause of the previous reset #endif #ifdef ST_NUCLEO // capture the cause of the previous reset /* Check if the system has resumed from IWDG reset */ -/* - if (__HAL_RCC_GET_FLAG(RCC_FLAG_IWDGRST)) { - wdreset = true; - } - else { - wdreset = false; - } -*/ - //printf("in constructor\r\n"); - wdreset = false; + /* + if (__HAL_RCC_GET_FLAG(RCC_FLAG_IWDGRST)) { + wdreset = true; + } + else { + wdreset = false; + } + */ + //printf("in constructor\r\n"); + imu1 = imu; + wdreset = false; #endif } /// Load timeout value in watchdog timer and enable // Timeout is in units of seconds -void Watchdog::Configure(float timeout) { -#ifdef LPC +void Watchdog::Configure(float timeout) +{ +#ifdef LPC LPC_WDT->WDCLKSEL = 0x1; // Set CLK src to PCLK uint32_t clk = SystemCoreClock / 16; // WD has a fixed /4 prescaler, PCLK default is /4 LPC_WDT->WDTC = (uint32_t)(timeout * (float)clk); LPC_WDT->WDMOD = 0x3; // Enabled and Reset -#endif - +#endif + #ifdef ST_NUCLEO - // see http://embedded-lab.com/blog/?p=9662 - #define LsiFreq (45000) + // see http://embedded-lab.com/blog/?p=9662 +#define LsiFreq (45000) uint16_t PrescalerCode; uint16_t Prescaler; uint16_t ReloadValue; float Calculated_timeout; - + if ((timeout * (LsiFreq/4)) < 0x7FF) { PrescalerCode = IWDG_PRESCALER_4; Prescaler = 4; - } - else if ((timeout * (LsiFreq/8)) < 0xFF0) { + } else if ((timeout * (LsiFreq/8)) < 0xFF0) { PrescalerCode = IWDG_PRESCALER_8; Prescaler = 8; - } - else if ((timeout * (LsiFreq/16)) < 0xFF0) { + } else if ((timeout * (LsiFreq/16)) < 0xFF0) { PrescalerCode = IWDG_PRESCALER_16; Prescaler = 16; - } - else if ((timeout * (LsiFreq/32)) < 0xFF0) { + } else if ((timeout * (LsiFreq/32)) < 0xFF0) { PrescalerCode = IWDG_PRESCALER_32; Prescaler = 32; - } - else if ((timeout * (LsiFreq/64)) < 0xFF0) { + if(imu1->begin()) { + IWDG->KR = 0xAAAA; //Reload IWDG + IWDG->KR = 0xCCCC; //Start IWDG + Service(); + } + //wait(5); + + } else if ((timeout * (LsiFreq/64)) < 0xFF0) { PrescalerCode = IWDG_PRESCALER_64; Prescaler = 64; - } - else if ((timeout * (LsiFreq/128)) < 0xFF0) { + } else if ((timeout * (LsiFreq/128)) < 0xFF0) { PrescalerCode = IWDG_PRESCALER_128; Prescaler = 128; - } - else { + } else { PrescalerCode = IWDG_PRESCALER_256; Prescaler = 256; } - + // specifies the IWDG Reload value. This parameter must be a number between 0 and 0x0FFF. ReloadValue = (uint32_t)(timeout * (LsiFreq/Prescaler)); - + Calculated_timeout = ((float)(Prescaler * ReloadValue)) / LsiFreq; printf("WATCHDOG set with prescaler:%d reload value: 0x%X - timeout:%f\n",Prescaler, ReloadValue, Calculated_timeout); - - IWDG->KR = 0x5555; //Disable write protection of IWDG registers - IWDG->PR = PrescalerCode; //Set PR value - IWDG->RLR = ReloadValue; //Set RLR value - IWDG->KR = 0xAAAA; //Reload IWDG - IWDG->KR = 0xCCCC; //Start IWDG - See more at: http://embedded-lab.com/blog/?p=9662#sthash.6VNxVSn0.dpuf + + IWDG->KR = 0x5555; //Disable write protection of IWDG registers + IWDG->PR = PrescalerCode; //Set PR value + IWDG->RLR = ReloadValue; //Set RLR value + IWDG->KR = 0xAAAA; //Reload IWDG + IWDG->KR = 0xCCCC; //Start IWDG - See more at: http://embedded-lab.com/blog/?p=9662#sthash.6VNxVSn0.dpuf #endif - + Service(); + } /// "Service", "kick" or "feed" the dog - reset the watchdog timer /// by writing this required bit pattern -void Watchdog::Service() { -#ifdef LPC +void Watchdog::Service() +{ +#ifdef LPC LPC_WDT->WDFEED = 0xAA; LPC_WDT->WDFEED = 0x55; -#endif +#endif #ifdef ST_NUCLEO IWDG->KR = 0xAAAA; //Reload IWDG - See more at: http://embedded-lab.com/blog/?p=9662#sthash.6VNxVSn0.dpuf #endif } /// get the flag to indicate if the watchdog causes the reset -bool Watchdog::WatchdogCausedReset() { +bool Watchdog::WatchdogCausedReset() +{ return wdreset; }