added prescaler for 16 bit pwm in LPC1347 target
Fork of mbed-dev by
targets/hal/TARGET_Silicon_Labs/TARGET_EFM32/emlib/src/em_wdog.c@0:9b334a45a8ff, 2015-10-01 (annotated)
- Committer:
- bogdanm
- Date:
- Thu Oct 01 15:25:22 2015 +0300
- Revision:
- 0:9b334a45a8ff
- Child:
- 50:a417edff4437
Initial commit on mbed-dev
Replaces mbed-src (now inactive)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bogdanm | 0:9b334a45a8ff | 1 | /***************************************************************************//** |
bogdanm | 0:9b334a45a8ff | 2 | * @file em_wdog.c |
bogdanm | 0:9b334a45a8ff | 3 | * @brief Watchdog (WDOG) peripheral API |
bogdanm | 0:9b334a45a8ff | 4 | * devices. |
bogdanm | 0:9b334a45a8ff | 5 | * @version 3.20.12 |
bogdanm | 0:9b334a45a8ff | 6 | ******************************************************************************* |
bogdanm | 0:9b334a45a8ff | 7 | * @section License |
bogdanm | 0:9b334a45a8ff | 8 | * <b>(C) Copyright 2014 Silicon Labs, http://www.silabs.com</b> |
bogdanm | 0:9b334a45a8ff | 9 | ******************************************************************************* |
bogdanm | 0:9b334a45a8ff | 10 | * |
bogdanm | 0:9b334a45a8ff | 11 | * Permission is granted to anyone to use this software for any purpose, |
bogdanm | 0:9b334a45a8ff | 12 | * including commercial applications, and to alter it and redistribute it |
bogdanm | 0:9b334a45a8ff | 13 | * freely, subject to the following restrictions: |
bogdanm | 0:9b334a45a8ff | 14 | * |
bogdanm | 0:9b334a45a8ff | 15 | * 1. The origin of this software must not be misrepresented; you must not |
bogdanm | 0:9b334a45a8ff | 16 | * claim that you wrote the original software. |
bogdanm | 0:9b334a45a8ff | 17 | * 2. Altered source versions must be plainly marked as such, and must not be |
bogdanm | 0:9b334a45a8ff | 18 | * misrepresented as being the original software. |
bogdanm | 0:9b334a45a8ff | 19 | * 3. This notice may not be removed or altered from any source distribution. |
bogdanm | 0:9b334a45a8ff | 20 | * |
bogdanm | 0:9b334a45a8ff | 21 | * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Silicon Labs has no |
bogdanm | 0:9b334a45a8ff | 22 | * obligation to support this Software. Silicon Labs is providing the |
bogdanm | 0:9b334a45a8ff | 23 | * Software "AS IS", with no express or implied warranties of any kind, |
bogdanm | 0:9b334a45a8ff | 24 | * including, but not limited to, any implied warranties of merchantability |
bogdanm | 0:9b334a45a8ff | 25 | * or fitness for any particular purpose or warranties against infringement |
bogdanm | 0:9b334a45a8ff | 26 | * of any proprietary rights of a third party. |
bogdanm | 0:9b334a45a8ff | 27 | * |
bogdanm | 0:9b334a45a8ff | 28 | * Silicon Labs will not be liable for any consequential, incidental, or |
bogdanm | 0:9b334a45a8ff | 29 | * special damages, or any other relief, or for any claim by any third party, |
bogdanm | 0:9b334a45a8ff | 30 | * arising from your use of this Software. |
bogdanm | 0:9b334a45a8ff | 31 | * |
bogdanm | 0:9b334a45a8ff | 32 | ******************************************************************************/ |
bogdanm | 0:9b334a45a8ff | 33 | |
bogdanm | 0:9b334a45a8ff | 34 | |
bogdanm | 0:9b334a45a8ff | 35 | #include "em_wdog.h" |
bogdanm | 0:9b334a45a8ff | 36 | #if defined(WDOG_COUNT) && (WDOG_COUNT > 0) |
bogdanm | 0:9b334a45a8ff | 37 | |
bogdanm | 0:9b334a45a8ff | 38 | #include "em_bitband.h" |
bogdanm | 0:9b334a45a8ff | 39 | |
bogdanm | 0:9b334a45a8ff | 40 | /***************************************************************************//** |
bogdanm | 0:9b334a45a8ff | 41 | * @addtogroup EM_Library |
bogdanm | 0:9b334a45a8ff | 42 | * @{ |
bogdanm | 0:9b334a45a8ff | 43 | ******************************************************************************/ |
bogdanm | 0:9b334a45a8ff | 44 | |
bogdanm | 0:9b334a45a8ff | 45 | /***************************************************************************//** |
bogdanm | 0:9b334a45a8ff | 46 | * @addtogroup WDOG |
bogdanm | 0:9b334a45a8ff | 47 | * @brief Watchdog (WDOG) Peripheral API |
bogdanm | 0:9b334a45a8ff | 48 | * @{ |
bogdanm | 0:9b334a45a8ff | 49 | ******************************************************************************/ |
bogdanm | 0:9b334a45a8ff | 50 | |
bogdanm | 0:9b334a45a8ff | 51 | /******************************************************************************* |
bogdanm | 0:9b334a45a8ff | 52 | ************************** GLOBAL FUNCTIONS ******************************* |
bogdanm | 0:9b334a45a8ff | 53 | ******************************************************************************/ |
bogdanm | 0:9b334a45a8ff | 54 | |
bogdanm | 0:9b334a45a8ff | 55 | /***************************************************************************//** |
bogdanm | 0:9b334a45a8ff | 56 | * @brief |
bogdanm | 0:9b334a45a8ff | 57 | * Enable/disable the watchdog timer. |
bogdanm | 0:9b334a45a8ff | 58 | * |
bogdanm | 0:9b334a45a8ff | 59 | * @note |
bogdanm | 0:9b334a45a8ff | 60 | * This function modifies the WDOG CTRL register which requires |
bogdanm | 0:9b334a45a8ff | 61 | * synchronization into the low frequency domain. If this register is modified |
bogdanm | 0:9b334a45a8ff | 62 | * before a previous update to the same register has completed, this function |
bogdanm | 0:9b334a45a8ff | 63 | * will stall until the previous synchronization has completed. |
bogdanm | 0:9b334a45a8ff | 64 | * |
bogdanm | 0:9b334a45a8ff | 65 | * @param[in] enable |
bogdanm | 0:9b334a45a8ff | 66 | * true to enable watchdog, false to disable. Watchdog cannot be disabled if |
bogdanm | 0:9b334a45a8ff | 67 | * watchdog has been locked. |
bogdanm | 0:9b334a45a8ff | 68 | ******************************************************************************/ |
bogdanm | 0:9b334a45a8ff | 69 | void WDOG_Enable(bool enable) |
bogdanm | 0:9b334a45a8ff | 70 | { |
bogdanm | 0:9b334a45a8ff | 71 | if (!enable) |
bogdanm | 0:9b334a45a8ff | 72 | { |
bogdanm | 0:9b334a45a8ff | 73 | /* Wait for any pending previous write operation to have been completed in */ |
bogdanm | 0:9b334a45a8ff | 74 | /* low frequency domain */ |
bogdanm | 0:9b334a45a8ff | 75 | while (WDOG->SYNCBUSY & WDOG_SYNCBUSY_CTRL) |
bogdanm | 0:9b334a45a8ff | 76 | ; |
bogdanm | 0:9b334a45a8ff | 77 | } |
bogdanm | 0:9b334a45a8ff | 78 | BITBAND_Peripheral(&(WDOG->CTRL), _WDOG_CTRL_EN_SHIFT, (unsigned int)enable); |
bogdanm | 0:9b334a45a8ff | 79 | } |
bogdanm | 0:9b334a45a8ff | 80 | |
bogdanm | 0:9b334a45a8ff | 81 | |
bogdanm | 0:9b334a45a8ff | 82 | /***************************************************************************//** |
bogdanm | 0:9b334a45a8ff | 83 | * @brief |
bogdanm | 0:9b334a45a8ff | 84 | * Feed the watchdog. |
bogdanm | 0:9b334a45a8ff | 85 | * |
bogdanm | 0:9b334a45a8ff | 86 | * @details |
bogdanm | 0:9b334a45a8ff | 87 | * When the watchdog is activated, it must be fed (ie clearing the counter) |
bogdanm | 0:9b334a45a8ff | 88 | * before it reaches the defined timeout period. Otherwise, the watchdog |
bogdanm | 0:9b334a45a8ff | 89 | * will generate a reset. |
bogdanm | 0:9b334a45a8ff | 90 | ******************************************************************************/ |
bogdanm | 0:9b334a45a8ff | 91 | void WDOG_Feed(void) |
bogdanm | 0:9b334a45a8ff | 92 | { |
bogdanm | 0:9b334a45a8ff | 93 | /* The watchdog should not be fed while it is disabled */ |
bogdanm | 0:9b334a45a8ff | 94 | if ( !(WDOG->CTRL & WDOG_CTRL_EN) ) |
bogdanm | 0:9b334a45a8ff | 95 | { |
bogdanm | 0:9b334a45a8ff | 96 | return; |
bogdanm | 0:9b334a45a8ff | 97 | } |
bogdanm | 0:9b334a45a8ff | 98 | |
bogdanm | 0:9b334a45a8ff | 99 | /* If a previous clearing is being synchronized to LF domain, then there */ |
bogdanm | 0:9b334a45a8ff | 100 | /* is no point in waiting for it to complete before clearing over again. */ |
bogdanm | 0:9b334a45a8ff | 101 | /* This avoids stalling the core in the typical use case where some idle loop */ |
bogdanm | 0:9b334a45a8ff | 102 | /* keeps clearing the watchdog. */ |
bogdanm | 0:9b334a45a8ff | 103 | if (WDOG->SYNCBUSY & WDOG_SYNCBUSY_CMD) |
bogdanm | 0:9b334a45a8ff | 104 | { |
bogdanm | 0:9b334a45a8ff | 105 | return; |
bogdanm | 0:9b334a45a8ff | 106 | } |
bogdanm | 0:9b334a45a8ff | 107 | /* Before writing to the WDOG_CMD register we also need to make sure that |
bogdanm | 0:9b334a45a8ff | 108 | * any previous write to WDOG_CTRL is complete. */ |
bogdanm | 0:9b334a45a8ff | 109 | while ( WDOG->SYNCBUSY & WDOG_SYNCBUSY_CTRL ); |
bogdanm | 0:9b334a45a8ff | 110 | |
bogdanm | 0:9b334a45a8ff | 111 | WDOG->CMD = WDOG_CMD_CLEAR; |
bogdanm | 0:9b334a45a8ff | 112 | } |
bogdanm | 0:9b334a45a8ff | 113 | |
bogdanm | 0:9b334a45a8ff | 114 | |
bogdanm | 0:9b334a45a8ff | 115 | /***************************************************************************//** |
bogdanm | 0:9b334a45a8ff | 116 | * @brief |
bogdanm | 0:9b334a45a8ff | 117 | * Initialize watchdog (assuming the watchdog configuration has not been |
bogdanm | 0:9b334a45a8ff | 118 | * locked). |
bogdanm | 0:9b334a45a8ff | 119 | * |
bogdanm | 0:9b334a45a8ff | 120 | * @note |
bogdanm | 0:9b334a45a8ff | 121 | * This function modifies the WDOG CTRL register which requires |
bogdanm | 0:9b334a45a8ff | 122 | * synchronization into the low frequency domain. If this register is modified |
bogdanm | 0:9b334a45a8ff | 123 | * before a previous update to the same register has completed, this function |
bogdanm | 0:9b334a45a8ff | 124 | * will stall until the previous synchronization has completed. |
bogdanm | 0:9b334a45a8ff | 125 | * |
bogdanm | 0:9b334a45a8ff | 126 | * @param[in] init |
bogdanm | 0:9b334a45a8ff | 127 | * Structure holding watchdog configuration. A default setting |
bogdanm | 0:9b334a45a8ff | 128 | * #WDOG_INIT_DEFAULT is available for init. |
bogdanm | 0:9b334a45a8ff | 129 | ******************************************************************************/ |
bogdanm | 0:9b334a45a8ff | 130 | void WDOG_Init(const WDOG_Init_TypeDef *init) |
bogdanm | 0:9b334a45a8ff | 131 | { |
bogdanm | 0:9b334a45a8ff | 132 | uint32_t setting; |
bogdanm | 0:9b334a45a8ff | 133 | |
bogdanm | 0:9b334a45a8ff | 134 | if (init->enable) |
bogdanm | 0:9b334a45a8ff | 135 | { |
bogdanm | 0:9b334a45a8ff | 136 | setting = WDOG_CTRL_EN; |
bogdanm | 0:9b334a45a8ff | 137 | } |
bogdanm | 0:9b334a45a8ff | 138 | else |
bogdanm | 0:9b334a45a8ff | 139 | { |
bogdanm | 0:9b334a45a8ff | 140 | setting = 0; |
bogdanm | 0:9b334a45a8ff | 141 | } |
bogdanm | 0:9b334a45a8ff | 142 | |
bogdanm | 0:9b334a45a8ff | 143 | if (init->debugRun) |
bogdanm | 0:9b334a45a8ff | 144 | { |
bogdanm | 0:9b334a45a8ff | 145 | setting |= WDOG_CTRL_DEBUGRUN; |
bogdanm | 0:9b334a45a8ff | 146 | } |
bogdanm | 0:9b334a45a8ff | 147 | |
bogdanm | 0:9b334a45a8ff | 148 | if (init->em2Run) |
bogdanm | 0:9b334a45a8ff | 149 | { |
bogdanm | 0:9b334a45a8ff | 150 | setting |= WDOG_CTRL_EM2RUN; |
bogdanm | 0:9b334a45a8ff | 151 | } |
bogdanm | 0:9b334a45a8ff | 152 | |
bogdanm | 0:9b334a45a8ff | 153 | if (init->em3Run) |
bogdanm | 0:9b334a45a8ff | 154 | { |
bogdanm | 0:9b334a45a8ff | 155 | setting |= WDOG_CTRL_EM3RUN; |
bogdanm | 0:9b334a45a8ff | 156 | } |
bogdanm | 0:9b334a45a8ff | 157 | |
bogdanm | 0:9b334a45a8ff | 158 | if (init->em4Block) |
bogdanm | 0:9b334a45a8ff | 159 | { |
bogdanm | 0:9b334a45a8ff | 160 | setting |= WDOG_CTRL_EM4BLOCK; |
bogdanm | 0:9b334a45a8ff | 161 | } |
bogdanm | 0:9b334a45a8ff | 162 | |
bogdanm | 0:9b334a45a8ff | 163 | if (init->swoscBlock) |
bogdanm | 0:9b334a45a8ff | 164 | { |
bogdanm | 0:9b334a45a8ff | 165 | setting |= WDOG_CTRL_SWOSCBLOCK; |
bogdanm | 0:9b334a45a8ff | 166 | } |
bogdanm | 0:9b334a45a8ff | 167 | |
bogdanm | 0:9b334a45a8ff | 168 | setting |= ((uint32_t)(init->clkSel) << _WDOG_CTRL_CLKSEL_SHIFT) | |
bogdanm | 0:9b334a45a8ff | 169 | ((uint32_t)(init->perSel) << _WDOG_CTRL_PERSEL_SHIFT); |
bogdanm | 0:9b334a45a8ff | 170 | |
bogdanm | 0:9b334a45a8ff | 171 | /* Wait for any pending previous write operation to have been completed in */ |
bogdanm | 0:9b334a45a8ff | 172 | /* low frequency domain */ |
bogdanm | 0:9b334a45a8ff | 173 | while (WDOG->SYNCBUSY & WDOG_SYNCBUSY_CTRL) |
bogdanm | 0:9b334a45a8ff | 174 | ; |
bogdanm | 0:9b334a45a8ff | 175 | |
bogdanm | 0:9b334a45a8ff | 176 | WDOG->CTRL = setting; |
bogdanm | 0:9b334a45a8ff | 177 | |
bogdanm | 0:9b334a45a8ff | 178 | /* Optional register locking */ |
bogdanm | 0:9b334a45a8ff | 179 | if (init->lock) |
bogdanm | 0:9b334a45a8ff | 180 | { |
bogdanm | 0:9b334a45a8ff | 181 | if (init->enable) |
bogdanm | 0:9b334a45a8ff | 182 | { |
bogdanm | 0:9b334a45a8ff | 183 | WDOG_Lock(); |
bogdanm | 0:9b334a45a8ff | 184 | } |
bogdanm | 0:9b334a45a8ff | 185 | else |
bogdanm | 0:9b334a45a8ff | 186 | { |
bogdanm | 0:9b334a45a8ff | 187 | BITBAND_Peripheral(&(WDOG->CTRL), _WDOG_CTRL_LOCK_SHIFT, 1); |
bogdanm | 0:9b334a45a8ff | 188 | } |
bogdanm | 0:9b334a45a8ff | 189 | } |
bogdanm | 0:9b334a45a8ff | 190 | } |
bogdanm | 0:9b334a45a8ff | 191 | |
bogdanm | 0:9b334a45a8ff | 192 | |
bogdanm | 0:9b334a45a8ff | 193 | /***************************************************************************//** |
bogdanm | 0:9b334a45a8ff | 194 | * @brief |
bogdanm | 0:9b334a45a8ff | 195 | * Lock the watchdog configuration. |
bogdanm | 0:9b334a45a8ff | 196 | * |
bogdanm | 0:9b334a45a8ff | 197 | * @details |
bogdanm | 0:9b334a45a8ff | 198 | * This prevents errors from overwriting the watchdog configuration, possibly |
bogdanm | 0:9b334a45a8ff | 199 | * disabling it. Only a reset can unlock the watchdog config, once locked. |
bogdanm | 0:9b334a45a8ff | 200 | * |
bogdanm | 0:9b334a45a8ff | 201 | * If the LFRCO or LFXO clocks are used to clock the watchdog, one should |
bogdanm | 0:9b334a45a8ff | 202 | * consider using the option of inhibiting those clocks to be disabled, |
bogdanm | 0:9b334a45a8ff | 203 | * please see the WDOG_Enable() init structure. |
bogdanm | 0:9b334a45a8ff | 204 | * |
bogdanm | 0:9b334a45a8ff | 205 | * @note |
bogdanm | 0:9b334a45a8ff | 206 | * This function modifies the WDOG CTRL register which requires |
bogdanm | 0:9b334a45a8ff | 207 | * synchronization into the low frequency domain. If this register is modified |
bogdanm | 0:9b334a45a8ff | 208 | * before a previous update to the same register has completed, this function |
bogdanm | 0:9b334a45a8ff | 209 | * will stall until the previous synchronization has completed. |
bogdanm | 0:9b334a45a8ff | 210 | ******************************************************************************/ |
bogdanm | 0:9b334a45a8ff | 211 | void WDOG_Lock(void) |
bogdanm | 0:9b334a45a8ff | 212 | { |
bogdanm | 0:9b334a45a8ff | 213 | /* Wait for any pending previous write operation to have been completed in */ |
bogdanm | 0:9b334a45a8ff | 214 | /* low frequency domain */ |
bogdanm | 0:9b334a45a8ff | 215 | while (WDOG->SYNCBUSY & WDOG_SYNCBUSY_CTRL) |
bogdanm | 0:9b334a45a8ff | 216 | ; |
bogdanm | 0:9b334a45a8ff | 217 | |
bogdanm | 0:9b334a45a8ff | 218 | /* Disable writing to the control register */ |
bogdanm | 0:9b334a45a8ff | 219 | BITBAND_Peripheral(&(WDOG->CTRL), _WDOG_CTRL_LOCK_SHIFT, 1); |
bogdanm | 0:9b334a45a8ff | 220 | } |
bogdanm | 0:9b334a45a8ff | 221 | |
bogdanm | 0:9b334a45a8ff | 222 | |
bogdanm | 0:9b334a45a8ff | 223 | /** @} (end addtogroup WDOG) */ |
bogdanm | 0:9b334a45a8ff | 224 | /** @} (end addtogroup EM_Library) */ |
bogdanm | 0:9b334a45a8ff | 225 | #endif /* defined(WDOG_COUNT) && (WDOG_COUNT > 0) */ |