added main and defined M_PI

Dependencies:   BNOWrapper

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