added main and defined M_PI

Dependencies:   BNOWrapper

Committer:
JesiMiranda
Date:
Mon Aug 05 21:57:13 2019 +0000
Revision:
8:729ad465d6c9
Parent:
7:b548a684290d
Child:
10:14374b492f1d
up-to-date code

Who changed what in which revision?

UserRevisionLine numberNew 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 8:729ad465d6c9 5 /// it is possible to identify if the Watchdog was the cause of any
JesiMiranda 7:b548a684290d 6 /// system restart.
JesiMiranda 8:729ad465d6c9 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 8:729ad465d6c9 30 #include "BNO080.h"
JesiMiranda 7:b548a684290d 31
JesiMiranda 7:b548a684290d 32 /// Watchdog gets instantiated at the module level
JesiMiranda 8:729ad465d6c9 33 Watchdog::Watchdog(BNO080 *imu)
JesiMiranda 8:729ad465d6c9 34 {
JesiMiranda 8:729ad465d6c9 35 #ifdef LPC
JesiMiranda 7:b548a684290d 36 wdreset = (LPC_WDT->WDMOD >> 2) & 1; // capture the cause of the previous reset
JesiMiranda 7:b548a684290d 37 #endif
JesiMiranda 7:b548a684290d 38 #ifdef ST_NUCLEO
JesiMiranda 7:b548a684290d 39 // capture the cause of the previous reset
JesiMiranda 7:b548a684290d 40 /* Check if the system has resumed from IWDG reset */
JesiMiranda 8:729ad465d6c9 41 /*
JesiMiranda 8:729ad465d6c9 42 if (__HAL_RCC_GET_FLAG(RCC_FLAG_IWDGRST)) {
JesiMiranda 8:729ad465d6c9 43 wdreset = true;
JesiMiranda 8:729ad465d6c9 44 }
JesiMiranda 8:729ad465d6c9 45 else {
JesiMiranda 8:729ad465d6c9 46 wdreset = false;
JesiMiranda 8:729ad465d6c9 47 }
JesiMiranda 8:729ad465d6c9 48 */
JesiMiranda 8:729ad465d6c9 49 //printf("in constructor\r\n");
JesiMiranda 8:729ad465d6c9 50 imu1 = imu;
JesiMiranda 8:729ad465d6c9 51 wdreset = false;
JesiMiranda 7:b548a684290d 52 #endif
JesiMiranda 7:b548a684290d 53
JesiMiranda 7:b548a684290d 54 }
JesiMiranda 7:b548a684290d 55
JesiMiranda 7:b548a684290d 56 /// Load timeout value in watchdog timer and enable
JesiMiranda 7:b548a684290d 57 // Timeout is in units of seconds
JesiMiranda 8:729ad465d6c9 58 void Watchdog::Configure(float timeout)
JesiMiranda 8:729ad465d6c9 59 {
JesiMiranda 8:729ad465d6c9 60 #ifdef LPC
JesiMiranda 7:b548a684290d 61 LPC_WDT->WDCLKSEL = 0x1; // Set CLK src to PCLK
JesiMiranda 7:b548a684290d 62 uint32_t clk = SystemCoreClock / 16; // WD has a fixed /4 prescaler, PCLK default is /4
JesiMiranda 7:b548a684290d 63 LPC_WDT->WDTC = (uint32_t)(timeout * (float)clk);
JesiMiranda 7:b548a684290d 64 LPC_WDT->WDMOD = 0x3; // Enabled and Reset
JesiMiranda 8:729ad465d6c9 65 #endif
JesiMiranda 8:729ad465d6c9 66
JesiMiranda 7:b548a684290d 67 #ifdef ST_NUCLEO
JesiMiranda 8:729ad465d6c9 68 // see http://embedded-lab.com/blog/?p=9662
JesiMiranda 8:729ad465d6c9 69 #define LsiFreq (45000)
JesiMiranda 7:b548a684290d 70 uint16_t PrescalerCode;
JesiMiranda 7:b548a684290d 71 uint16_t Prescaler;
JesiMiranda 7:b548a684290d 72 uint16_t ReloadValue;
JesiMiranda 7:b548a684290d 73 float Calculated_timeout;
JesiMiranda 8:729ad465d6c9 74
JesiMiranda 7:b548a684290d 75 if ((timeout * (LsiFreq/4)) < 0x7FF) {
JesiMiranda 7:b548a684290d 76 PrescalerCode = IWDG_PRESCALER_4;
JesiMiranda 7:b548a684290d 77 Prescaler = 4;
JesiMiranda 8:729ad465d6c9 78 } else if ((timeout * (LsiFreq/8)) < 0xFF0) {
JesiMiranda 7:b548a684290d 79 PrescalerCode = IWDG_PRESCALER_8;
JesiMiranda 7:b548a684290d 80 Prescaler = 8;
JesiMiranda 8:729ad465d6c9 81 } else if ((timeout * (LsiFreq/16)) < 0xFF0) {
JesiMiranda 7:b548a684290d 82 PrescalerCode = IWDG_PRESCALER_16;
JesiMiranda 7:b548a684290d 83 Prescaler = 16;
JesiMiranda 8:729ad465d6c9 84 } else if ((timeout * (LsiFreq/32)) < 0xFF0) {
JesiMiranda 7:b548a684290d 85 PrescalerCode = IWDG_PRESCALER_32;
JesiMiranda 7:b548a684290d 86 Prescaler = 32;
JesiMiranda 8:729ad465d6c9 87 if(imu1->begin()) {
JesiMiranda 8:729ad465d6c9 88 IWDG->KR = 0xAAAA; //Reload IWDG
JesiMiranda 8:729ad465d6c9 89 IWDG->KR = 0xCCCC; //Start IWDG
JesiMiranda 8:729ad465d6c9 90 Service();
JesiMiranda 8:729ad465d6c9 91 }
JesiMiranda 8:729ad465d6c9 92 //wait(5);
JesiMiranda 8:729ad465d6c9 93
JesiMiranda 8:729ad465d6c9 94 } else if ((timeout * (LsiFreq/64)) < 0xFF0) {
JesiMiranda 7:b548a684290d 95 PrescalerCode = IWDG_PRESCALER_64;
JesiMiranda 7:b548a684290d 96 Prescaler = 64;
JesiMiranda 8:729ad465d6c9 97 } else if ((timeout * (LsiFreq/128)) < 0xFF0) {
JesiMiranda 7:b548a684290d 98 PrescalerCode = IWDG_PRESCALER_128;
JesiMiranda 7:b548a684290d 99 Prescaler = 128;
JesiMiranda 8:729ad465d6c9 100 } else {
JesiMiranda 7:b548a684290d 101 PrescalerCode = IWDG_PRESCALER_256;
JesiMiranda 7:b548a684290d 102 Prescaler = 256;
JesiMiranda 7:b548a684290d 103 }
JesiMiranda 8:729ad465d6c9 104
JesiMiranda 7:b548a684290d 105 // specifies the IWDG Reload value. This parameter must be a number between 0 and 0x0FFF.
JesiMiranda 7:b548a684290d 106 ReloadValue = (uint32_t)(timeout * (LsiFreq/Prescaler));
JesiMiranda 8:729ad465d6c9 107
JesiMiranda 7:b548a684290d 108 Calculated_timeout = ((float)(Prescaler * ReloadValue)) / LsiFreq;
JesiMiranda 7:b548a684290d 109 printf("WATCHDOG set with prescaler:%d reload value: 0x%X - timeout:%f\n",Prescaler, ReloadValue, Calculated_timeout);
JesiMiranda 8:729ad465d6c9 110
JesiMiranda 8:729ad465d6c9 111 IWDG->KR = 0x5555; //Disable write protection of IWDG registers
JesiMiranda 8:729ad465d6c9 112 IWDG->PR = PrescalerCode; //Set PR value
JesiMiranda 8:729ad465d6c9 113 IWDG->RLR = ReloadValue; //Set RLR value
JesiMiranda 8:729ad465d6c9 114 IWDG->KR = 0xAAAA; //Reload IWDG
JesiMiranda 8:729ad465d6c9 115 IWDG->KR = 0xCCCC; //Start IWDG - See more at: http://embedded-lab.com/blog/?p=9662#sthash.6VNxVSn0.dpuf
JesiMiranda 7:b548a684290d 116 #endif
JesiMiranda 8:729ad465d6c9 117
JesiMiranda 7:b548a684290d 118 Service();
JesiMiranda 8:729ad465d6c9 119
JesiMiranda 7:b548a684290d 120 }
JesiMiranda 7:b548a684290d 121
JesiMiranda 7:b548a684290d 122 /// "Service", "kick" or "feed" the dog - reset the watchdog timer
JesiMiranda 7:b548a684290d 123 /// by writing this required bit pattern
JesiMiranda 8:729ad465d6c9 124 void Watchdog::Service()
JesiMiranda 8:729ad465d6c9 125 {
JesiMiranda 8:729ad465d6c9 126 #ifdef LPC
JesiMiranda 7:b548a684290d 127 LPC_WDT->WDFEED = 0xAA;
JesiMiranda 7:b548a684290d 128 LPC_WDT->WDFEED = 0x55;
JesiMiranda 8:729ad465d6c9 129 #endif
JesiMiranda 7:b548a684290d 130 #ifdef ST_NUCLEO
JesiMiranda 7:b548a684290d 131 IWDG->KR = 0xAAAA; //Reload IWDG - See more at: http://embedded-lab.com/blog/?p=9662#sthash.6VNxVSn0.dpuf
JesiMiranda 7:b548a684290d 132 #endif
JesiMiranda 7:b548a684290d 133 }
JesiMiranda 7:b548a684290d 134
JesiMiranda 7:b548a684290d 135 /// get the flag to indicate if the watchdog causes the reset
JesiMiranda 8:729ad465d6c9 136 bool Watchdog::WatchdogCausedReset()
JesiMiranda 8:729ad465d6c9 137 {
JesiMiranda 7:b548a684290d 138 return wdreset;
JesiMiranda 7:b548a684290d 139 }