mbed library sources. Supersedes mbed-src. Edited target satm32f446 for user USART3 pins

Dependents:   IGLOO_board

Fork of mbed-dev by mbed official

Committer:
Anna Bridge
Date:
Fri Jun 22 16:45:37 2018 +0100
Revision:
186:707f6e361f3e
mbed-dev library. Release version 162

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Anna Bridge 186:707f6e361f3e 1 /**
Anna Bridge 186:707f6e361f3e 2 * @file
Anna Bridge 186:707f6e361f3e 3 * @brief Watchdog Timer 2 Function Implementations.
Anna Bridge 186:707f6e361f3e 4 */
Anna Bridge 186:707f6e361f3e 5 /* *****************************************************************************
Anna Bridge 186:707f6e361f3e 6 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
Anna Bridge 186:707f6e361f3e 7 *
Anna Bridge 186:707f6e361f3e 8 * Permission is hereby granted, free of charge, to any person obtaining a
Anna Bridge 186:707f6e361f3e 9 * copy of this software and associated documentation files (the "Software"),
Anna Bridge 186:707f6e361f3e 10 * to deal in the Software without restriction, including without limitation
Anna Bridge 186:707f6e361f3e 11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
Anna Bridge 186:707f6e361f3e 12 * and/or sell copies of the Software, and to permit persons to whom the
Anna Bridge 186:707f6e361f3e 13 * Software is furnished to do so, subject to the following conditions:
Anna Bridge 186:707f6e361f3e 14 *
Anna Bridge 186:707f6e361f3e 15 * The above copyright notice and this permission notice shall be included
Anna Bridge 186:707f6e361f3e 16 * in all copies or substantial portions of the Software.
Anna Bridge 186:707f6e361f3e 17 *
Anna Bridge 186:707f6e361f3e 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
Anna Bridge 186:707f6e361f3e 19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Anna Bridge 186:707f6e361f3e 20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
Anna Bridge 186:707f6e361f3e 21 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
Anna Bridge 186:707f6e361f3e 22 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
Anna Bridge 186:707f6e361f3e 23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
Anna Bridge 186:707f6e361f3e 24 * OTHER DEALINGS IN THE SOFTWARE.
Anna Bridge 186:707f6e361f3e 25 *
Anna Bridge 186:707f6e361f3e 26 * Except as contained in this notice, the name of Maxim Integrated
Anna Bridge 186:707f6e361f3e 27 * Products, Inc. shall not be used except as stated in the Maxim Integrated
Anna Bridge 186:707f6e361f3e 28 * Products, Inc. Branding Policy.
Anna Bridge 186:707f6e361f3e 29 *
Anna Bridge 186:707f6e361f3e 30 * The mere transfer of this software does not imply any licenses
Anna Bridge 186:707f6e361f3e 31 * of trade secrets, proprietary technology, copyrights, patents,
Anna Bridge 186:707f6e361f3e 32 * trademarks, maskwork rights, or any other form of intellectual
Anna Bridge 186:707f6e361f3e 33 * property whatsoever. Maxim Integrated Products, Inc. retains all
Anna Bridge 186:707f6e361f3e 34 * ownership rights.
Anna Bridge 186:707f6e361f3e 35 *
Anna Bridge 186:707f6e361f3e 36 * $Date: 2017-02-16 12:02:20 -0600 (Thu, 16 Feb 2017) $
Anna Bridge 186:707f6e361f3e 37 * $Revision: 26462 $
Anna Bridge 186:707f6e361f3e 38 *
Anna Bridge 186:707f6e361f3e 39 **************************************************************************** */
Anna Bridge 186:707f6e361f3e 40
Anna Bridge 186:707f6e361f3e 41 /* **** Includes **** */
Anna Bridge 186:707f6e361f3e 42 #include <stddef.h>
Anna Bridge 186:707f6e361f3e 43 #include "wdt2.h"
Anna Bridge 186:707f6e361f3e 44 #include "pwrseq_regs.h"
Anna Bridge 186:707f6e361f3e 45
Anna Bridge 186:707f6e361f3e 46 /**
Anna Bridge 186:707f6e361f3e 47 * @addtogroup wdt2
Anna Bridge 186:707f6e361f3e 48 * @{
Anna Bridge 186:707f6e361f3e 49 */
Anna Bridge 186:707f6e361f3e 50 static uint32_t interruptEnable = 0; //keeps track to interrupts to enable in start function
Anna Bridge 186:707f6e361f3e 51
Anna Bridge 186:707f6e361f3e 52 /* ************************************************************************* */
Anna Bridge 186:707f6e361f3e 53 int WDT2_Init(uint8_t runInSleep, uint8_t unlock_key)
Anna Bridge 186:707f6e361f3e 54 {
Anna Bridge 186:707f6e361f3e 55 //enable nanoring in run and sleep mode
Anna Bridge 186:707f6e361f3e 56 MXC_PWRSEQ->reg0 |= (MXC_F_PWRSEQ_REG0_PWR_NREN_RUN);
Anna Bridge 186:707f6e361f3e 57
Anna Bridge 186:707f6e361f3e 58 //unlock ctrl to be writable
Anna Bridge 186:707f6e361f3e 59 MXC_WDT2->lock_ctrl = unlock_key;
Anna Bridge 186:707f6e361f3e 60
Anna Bridge 186:707f6e361f3e 61 //check to make sure it unlocked
Anna Bridge 186:707f6e361f3e 62 if (MXC_WDT2->lock_ctrl & 0x01)
Anna Bridge 186:707f6e361f3e 63 return E_BAD_STATE;
Anna Bridge 186:707f6e361f3e 64
Anna Bridge 186:707f6e361f3e 65 //disable all interrupts
Anna Bridge 186:707f6e361f3e 66 interruptEnable = 0;
Anna Bridge 186:707f6e361f3e 67 MXC_WDT2->enable = interruptEnable;
Anna Bridge 186:707f6e361f3e 68
Anna Bridge 186:707f6e361f3e 69 //enable the watchdog clock and clear all other settings
Anna Bridge 186:707f6e361f3e 70 MXC_WDT2->ctrl = (MXC_F_WDT2_CTRL_EN_CLOCK);
Anna Bridge 186:707f6e361f3e 71
Anna Bridge 186:707f6e361f3e 72 //clear all interrupt flags
Anna Bridge 186:707f6e361f3e 73 MXC_WDT2->flags = WDT2_FLAGS_CLEAR_ALL;
Anna Bridge 186:707f6e361f3e 74
Anna Bridge 186:707f6e361f3e 75 if(runInSleep) {
Anna Bridge 186:707f6e361f3e 76 // turn on nanoring during sleep
Anna Bridge 186:707f6e361f3e 77 MXC_PWRSEQ->reg0 |= (MXC_F_PWRSEQ_REG0_PWR_NREN_SLP);
Anna Bridge 186:707f6e361f3e 78 //turn on timer during sleep
Anna Bridge 186:707f6e361f3e 79 MXC_WDT2->ctrl |= MXC_F_WDT2_CTRL_EN_TIMER_SLP;
Anna Bridge 186:707f6e361f3e 80 } else {
Anna Bridge 186:707f6e361f3e 81 // turn off nanoring during sleep
Anna Bridge 186:707f6e361f3e 82 MXC_PWRSEQ->reg0 &= ~(MXC_F_PWRSEQ_REG0_PWR_NREN_SLP);
Anna Bridge 186:707f6e361f3e 83 //turn off timer during sleep
Anna Bridge 186:707f6e361f3e 84 MXC_WDT2->ctrl &= ~(MXC_F_WDT2_CTRL_EN_TIMER_SLP);
Anna Bridge 186:707f6e361f3e 85 }
Anna Bridge 186:707f6e361f3e 86
Anna Bridge 186:707f6e361f3e 87 //lock ctrl to read-only
Anna Bridge 186:707f6e361f3e 88 MXC_WDT2->lock_ctrl = MXC_V_WDT2_LOCK_KEY;
Anna Bridge 186:707f6e361f3e 89
Anna Bridge 186:707f6e361f3e 90 return E_NO_ERROR;
Anna Bridge 186:707f6e361f3e 91 }
Anna Bridge 186:707f6e361f3e 92
Anna Bridge 186:707f6e361f3e 93 /* ************************************************************************* */
Anna Bridge 186:707f6e361f3e 94 int WDT2_EnableWakeUp(wdt2_period_t int_period, uint8_t unlock_key)
Anna Bridge 186:707f6e361f3e 95 {
Anna Bridge 186:707f6e361f3e 96 // Make sure interrupt period is valid
Anna Bridge 186:707f6e361f3e 97 if (int_period >= WDT2_PERIOD_MAX)
Anna Bridge 186:707f6e361f3e 98 return E_INVALID;
Anna Bridge 186:707f6e361f3e 99
Anna Bridge 186:707f6e361f3e 100 //unlock ctrl to be writable
Anna Bridge 186:707f6e361f3e 101 MXC_WDT2->lock_ctrl = unlock_key;
Anna Bridge 186:707f6e361f3e 102
Anna Bridge 186:707f6e361f3e 103 //check to make sure it unlocked
Anna Bridge 186:707f6e361f3e 104 if (MXC_WDT2->lock_ctrl & 0x01)
Anna Bridge 186:707f6e361f3e 105 return E_BAD_STATE;
Anna Bridge 186:707f6e361f3e 106
Anna Bridge 186:707f6e361f3e 107 //stop timer and clear interval period
Anna Bridge 186:707f6e361f3e 108 MXC_WDT2->ctrl &= ~(MXC_F_WDT2_CTRL_INT_PERIOD | MXC_F_WDT2_CTRL_EN_TIMER);
Anna Bridge 186:707f6e361f3e 109
Anna Bridge 186:707f6e361f3e 110 //set interval period
Anna Bridge 186:707f6e361f3e 111 MXC_WDT2->ctrl |= (int_period << MXC_F_WDT2_CTRL_INT_PERIOD_POS);
Anna Bridge 186:707f6e361f3e 112
Anna Bridge 186:707f6e361f3e 113 //enable timeout wake-up
Anna Bridge 186:707f6e361f3e 114 interruptEnable |= MXC_F_WDT2_ENABLE_TIMEOUT;
Anna Bridge 186:707f6e361f3e 115
Anna Bridge 186:707f6e361f3e 116 //lock ctrl to read-only
Anna Bridge 186:707f6e361f3e 117 MXC_WDT2->lock_ctrl = MXC_V_WDT2_LOCK_KEY;
Anna Bridge 186:707f6e361f3e 118
Anna Bridge 186:707f6e361f3e 119 // Enable wake-up
Anna Bridge 186:707f6e361f3e 120 MXC_PWRSEQ->msk_flags |= MXC_F_PWRSEQ_MSK_FLAGS_PWR_NANORING_WAKEUP_FLAG;
Anna Bridge 186:707f6e361f3e 121
Anna Bridge 186:707f6e361f3e 122 return E_NO_ERROR;
Anna Bridge 186:707f6e361f3e 123 }
Anna Bridge 186:707f6e361f3e 124
Anna Bridge 186:707f6e361f3e 125 /* ************************************************************************* */
Anna Bridge 186:707f6e361f3e 126 int WDT2_DisableWakeUp(uint8_t unlock_key)
Anna Bridge 186:707f6e361f3e 127 {
Anna Bridge 186:707f6e361f3e 128 //unlock register to be writable
Anna Bridge 186:707f6e361f3e 129 MXC_WDT2->lock_ctrl = unlock_key;
Anna Bridge 186:707f6e361f3e 130
Anna Bridge 186:707f6e361f3e 131 //check to make sure it unlocked
Anna Bridge 186:707f6e361f3e 132 if (MXC_WDT2->lock_ctrl & 0x01)
Anna Bridge 186:707f6e361f3e 133 return E_BAD_STATE;
Anna Bridge 186:707f6e361f3e 134
Anna Bridge 186:707f6e361f3e 135 //disable timeout wake-up
Anna Bridge 186:707f6e361f3e 136 interruptEnable &= ~MXC_F_WDT2_ENABLE_TIMEOUT;
Anna Bridge 186:707f6e361f3e 137 MXC_WDT2->enable = interruptEnable;
Anna Bridge 186:707f6e361f3e 138
Anna Bridge 186:707f6e361f3e 139 //lock register to read-only
Anna Bridge 186:707f6e361f3e 140 MXC_WDT2->lock_ctrl = MXC_V_WDT2_LOCK_KEY;
Anna Bridge 186:707f6e361f3e 141
Anna Bridge 186:707f6e361f3e 142 // disable wake-up
Anna Bridge 186:707f6e361f3e 143 MXC_PWRSEQ->msk_flags &= ~MXC_F_PWRSEQ_MSK_FLAGS_PWR_NANORING_WAKEUP_FLAG;
Anna Bridge 186:707f6e361f3e 144
Anna Bridge 186:707f6e361f3e 145 return E_NO_ERROR;
Anna Bridge 186:707f6e361f3e 146 }
Anna Bridge 186:707f6e361f3e 147
Anna Bridge 186:707f6e361f3e 148 /* ************************************************************************* */
Anna Bridge 186:707f6e361f3e 149 int WDT2_EnableReset(wdt2_period_t rst_period, uint8_t unlock_key)
Anna Bridge 186:707f6e361f3e 150 {
Anna Bridge 186:707f6e361f3e 151 // Make sure reset period is valid
Anna Bridge 186:707f6e361f3e 152 if (rst_period >= WDT2_PERIOD_MAX)
Anna Bridge 186:707f6e361f3e 153 return E_INVALID;
Anna Bridge 186:707f6e361f3e 154
Anna Bridge 186:707f6e361f3e 155 //unlock ctrl to be writable
Anna Bridge 186:707f6e361f3e 156 MXC_WDT2->lock_ctrl = unlock_key;
Anna Bridge 186:707f6e361f3e 157
Anna Bridge 186:707f6e361f3e 158 //check to make sure it unlocked
Anna Bridge 186:707f6e361f3e 159 if (MXC_WDT2->lock_ctrl & 0x01)
Anna Bridge 186:707f6e361f3e 160 return E_BAD_STATE;
Anna Bridge 186:707f6e361f3e 161
Anna Bridge 186:707f6e361f3e 162 //stop timer and clear reset period
Anna Bridge 186:707f6e361f3e 163 MXC_WDT2->ctrl &= ~(MXC_F_WDT2_CTRL_RST_PERIOD | MXC_F_WDT2_CTRL_EN_TIMER);
Anna Bridge 186:707f6e361f3e 164
Anna Bridge 186:707f6e361f3e 165 //set reset period
Anna Bridge 186:707f6e361f3e 166 MXC_WDT2->ctrl |= (rst_period << MXC_F_WDT2_CTRL_RST_PERIOD_POS);
Anna Bridge 186:707f6e361f3e 167
Anna Bridge 186:707f6e361f3e 168 //int flag has to be clear before interrupt enable can be written
Anna Bridge 186:707f6e361f3e 169 MXC_WDT2->flags = MXC_F_WDT2_FLAGS_RESET_OUT;
Anna Bridge 186:707f6e361f3e 170
Anna Bridge 186:707f6e361f3e 171 //enable reset0
Anna Bridge 186:707f6e361f3e 172 interruptEnable |= MXC_F_WDT2_ENABLE_RESET_OUT;
Anna Bridge 186:707f6e361f3e 173
Anna Bridge 186:707f6e361f3e 174 //lock ctrl to read-only
Anna Bridge 186:707f6e361f3e 175 MXC_WDT2->lock_ctrl = MXC_V_WDT2_LOCK_KEY;
Anna Bridge 186:707f6e361f3e 176
Anna Bridge 186:707f6e361f3e 177 //enable RSTN on WDT2 reset
Anna Bridge 186:707f6e361f3e 178 MXC_PWRSEQ->msk_flags |= MXC_F_PWRSEQ_MSK_FLAGS_PWR_WATCHDOG_RSTN_FLAG;
Anna Bridge 186:707f6e361f3e 179
Anna Bridge 186:707f6e361f3e 180 return E_NO_ERROR;
Anna Bridge 186:707f6e361f3e 181 }
Anna Bridge 186:707f6e361f3e 182
Anna Bridge 186:707f6e361f3e 183 /* ************************************************************************* */
Anna Bridge 186:707f6e361f3e 184 int WDT2_DisableReset(uint8_t unlock_key)
Anna Bridge 186:707f6e361f3e 185 {
Anna Bridge 186:707f6e361f3e 186 //unlock register to be writable
Anna Bridge 186:707f6e361f3e 187 MXC_WDT2->lock_ctrl = unlock_key;
Anna Bridge 186:707f6e361f3e 188
Anna Bridge 186:707f6e361f3e 189 //check to make sure it unlocked
Anna Bridge 186:707f6e361f3e 190 if (MXC_WDT2->lock_ctrl & 0x01)
Anna Bridge 186:707f6e361f3e 191 return E_BAD_STATE;
Anna Bridge 186:707f6e361f3e 192
Anna Bridge 186:707f6e361f3e 193 //disable reset
Anna Bridge 186:707f6e361f3e 194 interruptEnable &= ~MXC_F_WDT2_ENABLE_RESET_OUT;
Anna Bridge 186:707f6e361f3e 195 MXC_WDT2->enable = interruptEnable;
Anna Bridge 186:707f6e361f3e 196
Anna Bridge 186:707f6e361f3e 197 //lock register to read-only
Anna Bridge 186:707f6e361f3e 198 MXC_WDT2->lock_ctrl = MXC_V_WDT2_LOCK_KEY;
Anna Bridge 186:707f6e361f3e 199
Anna Bridge 186:707f6e361f3e 200 //disable RSTN on WDT2 reset
Anna Bridge 186:707f6e361f3e 201 MXC_PWRSEQ->msk_flags &= ~MXC_F_PWRSEQ_MSK_FLAGS_PWR_WATCHDOG_RSTN_FLAG;
Anna Bridge 186:707f6e361f3e 202
Anna Bridge 186:707f6e361f3e 203 return E_NO_ERROR;
Anna Bridge 186:707f6e361f3e 204 }
Anna Bridge 186:707f6e361f3e 205
Anna Bridge 186:707f6e361f3e 206 /* ************************************************************************* */
Anna Bridge 186:707f6e361f3e 207 int WDT2_Start(uint8_t unlock_key)
Anna Bridge 186:707f6e361f3e 208 {
Anna Bridge 186:707f6e361f3e 209 //check if watchdog is already running
Anna Bridge 186:707f6e361f3e 210 if(WDT2_IsActive())
Anna Bridge 186:707f6e361f3e 211 return E_BAD_STATE;
Anna Bridge 186:707f6e361f3e 212
Anna Bridge 186:707f6e361f3e 213 //unlock ctrl to be writable
Anna Bridge 186:707f6e361f3e 214 MXC_WDT2->lock_ctrl = unlock_key;
Anna Bridge 186:707f6e361f3e 215
Anna Bridge 186:707f6e361f3e 216 //check to make sure it unlocked
Anna Bridge 186:707f6e361f3e 217 if (MXC_WDT2->lock_ctrl & 0x01)
Anna Bridge 186:707f6e361f3e 218 return E_BAD_STATE;
Anna Bridge 186:707f6e361f3e 219
Anna Bridge 186:707f6e361f3e 220 WDT2_Reset();
Anna Bridge 186:707f6e361f3e 221
Anna Bridge 186:707f6e361f3e 222 //enable interrupts
Anna Bridge 186:707f6e361f3e 223 MXC_WDT2->enable = interruptEnable;
Anna Bridge 186:707f6e361f3e 224
Anna Bridge 186:707f6e361f3e 225 //start timer
Anna Bridge 186:707f6e361f3e 226 MXC_WDT2->ctrl |= (MXC_F_WDT2_CTRL_EN_TIMER);
Anna Bridge 186:707f6e361f3e 227
Anna Bridge 186:707f6e361f3e 228 //lock ctrl to read-only
Anna Bridge 186:707f6e361f3e 229 MXC_WDT2->lock_ctrl = MXC_V_WDT2_LOCK_KEY;
Anna Bridge 186:707f6e361f3e 230
Anna Bridge 186:707f6e361f3e 231 return E_NO_ERROR;
Anna Bridge 186:707f6e361f3e 232 }
Anna Bridge 186:707f6e361f3e 233
Anna Bridge 186:707f6e361f3e 234 /* ************************************************************************* */
Anna Bridge 186:707f6e361f3e 235 void WDT2_Reset(void)
Anna Bridge 186:707f6e361f3e 236 {
Anna Bridge 186:707f6e361f3e 237 //reset the watchdog counter
Anna Bridge 186:707f6e361f3e 238 MXC_WDT2->clear = MXC_V_WDT2_RESET_KEY_0;
Anna Bridge 186:707f6e361f3e 239 MXC_WDT2->clear = MXC_V_WDT2_RESET_KEY_1;
Anna Bridge 186:707f6e361f3e 240
Anna Bridge 186:707f6e361f3e 241 //clear all interrupt flags
Anna Bridge 186:707f6e361f3e 242 MXC_WDT2->flags = WDT2_FLAGS_CLEAR_ALL;
Anna Bridge 186:707f6e361f3e 243
Anna Bridge 186:707f6e361f3e 244 //wait for all interrupts to clear
Anna Bridge 186:707f6e361f3e 245 while(MXC_WDT2->flags != 0) {
Anna Bridge 186:707f6e361f3e 246 MXC_WDT2->flags = WDT2_FLAGS_CLEAR_ALL;
Anna Bridge 186:707f6e361f3e 247 }
Anna Bridge 186:707f6e361f3e 248
Anna Bridge 186:707f6e361f3e 249 return;
Anna Bridge 186:707f6e361f3e 250 }
Anna Bridge 186:707f6e361f3e 251
Anna Bridge 186:707f6e361f3e 252 /* ************************************************************************* */
Anna Bridge 186:707f6e361f3e 253 int WDT2_Stop(uint8_t unlock_key)
Anna Bridge 186:707f6e361f3e 254 {
Anna Bridge 186:707f6e361f3e 255 //check if watchdog is not running
Anna Bridge 186:707f6e361f3e 256 if(!WDT2_IsActive())
Anna Bridge 186:707f6e361f3e 257 return E_BAD_STATE;
Anna Bridge 186:707f6e361f3e 258
Anna Bridge 186:707f6e361f3e 259 //unlock ctrl to be writable
Anna Bridge 186:707f6e361f3e 260 MXC_WDT2->lock_ctrl = unlock_key;
Anna Bridge 186:707f6e361f3e 261
Anna Bridge 186:707f6e361f3e 262 //check to make sure it unlocked
Anna Bridge 186:707f6e361f3e 263 if (MXC_WDT2->lock_ctrl & 0x01)
Anna Bridge 186:707f6e361f3e 264 return E_BAD_STATE;
Anna Bridge 186:707f6e361f3e 265
Anna Bridge 186:707f6e361f3e 266 //disabled the timer and interrupts
Anna Bridge 186:707f6e361f3e 267 MXC_WDT2->enable = 0;
Anna Bridge 186:707f6e361f3e 268 MXC_WDT2->ctrl &= ~(MXC_F_WDT2_CTRL_EN_TIMER);
Anna Bridge 186:707f6e361f3e 269
Anna Bridge 186:707f6e361f3e 270 //lock ctrl to read-only
Anna Bridge 186:707f6e361f3e 271 MXC_WDT2->lock_ctrl = MXC_V_WDT2_LOCK_KEY;
Anna Bridge 186:707f6e361f3e 272
Anna Bridge 186:707f6e361f3e 273 return E_NO_ERROR;
Anna Bridge 186:707f6e361f3e 274 }
Anna Bridge 186:707f6e361f3e 275 /**@} end of group wdt2*/