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