fsl_rtwdog.h 13.2 KB
Newer Older
T
tanek liang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
/*
 * Copyright (c) 2016, Freescale Semiconductor, Inc.
 * Copyright 2016-2017 NXP
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * o Redistributions of source code must retain the above copyright notice, this list
 *   of conditions and the following disclaimer.
 *
 * o Redistributions in binary form must reproduce the above copyright notice, this
 *   list of conditions and the following disclaimer in the documentation and/or
 *   other materials provided with the distribution.
 *
 * o Neither the name of the copyright holder nor the names of its
 *   contributors may be used to endorse or promote products derived from this
 *   software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#ifndef _FSL_RTWDOG_H_
#define _FSL_RTWDOG_H_

#include "fsl_common.h"

/*!
 * @addtogroup rtwdog
 * @{
 */


/*******************************************************************************
 * Definitions
 *******************************************************************************/
/*! @name Unlock sequence */
/*@{*/
#define WDOG_FIRST_WORD_OF_UNLOCK (RTWDOG_UPDATE_KEY & 0xFFFFU)  /*!< First word of unlock sequence */
#define WDOG_SECOND_WORD_OF_UNLOCK ((RTWDOG_UPDATE_KEY >> 16U)& 0xFFFFU) /*!< Second word of unlock sequence */
/*@}*/

/*! @name Refresh sequence */
/*@{*/
#define WDOG_FIRST_WORD_OF_REFRESH (RTWDOG_REFRESH_KEY & 0xFFFFU)  /*!< First word of refresh sequence */
#define WDOG_SECOND_WORD_OF_REFRESH ((RTWDOG_REFRESH_KEY >> 16U)& 0xFFFFU) /*!< Second word of refresh sequence */
/*@}*/
/*! @name Driver version */
/*@{*/
/*! @brief RTWDOG driver version 2.0.0. */
#define FSL_RTWDOG_DRIVER_VERSION (MAKE_VERSION(2, 0, 0))
/*@}*/

/*! @brief Describes RTWDOG clock source. */
typedef enum _rtwdog_clock_source
{
    kRTWDOG_ClockSource0 = 0U, /*!< Clock source 0 */
    kRTWDOG_ClockSource1 = 1U, /*!< Clock source 1 */
    kRTWDOG_ClockSource2 = 2U, /*!< Clock source 2 */
    kRTWDOG_ClockSource3 = 3U, /*!< Clock source 3 */
} rtwdog_clock_source_t;

/*! @brief Describes the selection of the clock prescaler. */
typedef enum _rtwdog_clock_prescaler
{
    kRTWDOG_ClockPrescalerDivide1 = 0x0U,   /*!< Divided by 1 */
    kRTWDOG_ClockPrescalerDivide256 = 0x1U, /*!< Divided by 256 */
} rtwdog_clock_prescaler_t;

/*! @brief Defines RTWDOG work mode. */
typedef struct _rtwdog_work_mode
{
    bool enableWait;  /*!< Enables or disables RTWDOG in wait mode */
    bool enableStop;  /*!< Enables or disables RTWDOG in stop mode */
    bool enableDebug; /*!< Enables or disables RTWDOG in debug mode */
} rtwdog_work_mode_t;

/*! @brief Describes RTWDOG test mode. */
typedef enum _rtwdog_test_mode
{
    kRTWDOG_TestModeDisabled = 0U, /*!< Test Mode disabled */
    kRTWDOG_UserModeEnabled = 1U,  /*!< User Mode enabled */
    kRTWDOG_LowByteTest = 2U,      /*!< Test Mode enabled, only low byte is used */
    kRTWDOG_HighByteTest = 3U,     /*!< Test Mode enabled, only high byte is used */
} rtwdog_test_mode_t;

/*! @brief Describes RTWDOG configuration structure. */
typedef struct _rtwdog_config
{
    bool enableRtwdog;                  /*!< Enables or disables RTWDOG */
    rtwdog_clock_source_t clockSource;  /*!< Clock source select */
    rtwdog_clock_prescaler_t prescaler; /*!< Clock prescaler value */
    rtwdog_work_mode_t workMode;        /*!< Configures RTWDOG work mode in debug stop and wait mode */
    rtwdog_test_mode_t testMode;        /*!< Configures RTWDOG test mode */
    bool enableUpdate;                  /*!< Update write-once register enable */
    bool enableInterrupt;               /*!< Enables or disables RTWDOG interrupt */
    bool enableWindowMode;              /*!< Enables or disables RTWDOG window mode */
    uint16_t windowValue;               /*!< Window value */
    uint16_t timeoutValue;              /*!< Timeout value */
} rtwdog_config_t;

/*!
 * @brief RTWDOG interrupt configuration structure.
 *
 * This structure contains the settings for all of the RTWDOG interrupt configurations.
 */
enum _rtwdog_interrupt_enable_t
{
    kRTWDOG_InterruptEnable = RTWDOG_CS_INT_MASK, /*!< Interrupt is generated before forcing a reset */
};

/*!
 * @brief RTWDOG status flags.
 *
 * This structure contains the RTWDOG status flags for use in the RTWDOG functions.
 */
enum _rtwdog_status_flags_t
{
    kRTWDOG_RunningFlag = RTWDOG_CS_EN_MASK,    /*!< Running flag, set when RTWDOG is enabled */
    kRTWDOG_InterruptFlag = RTWDOG_CS_FLG_MASK, /*!< Interrupt flag, set when interrupt occurs */
};

/*******************************************************************************
 * API
 *******************************************************************************/

#if defined(__cplusplus)
extern "C" {
#endif /* __cplusplus */

/*!
 * @name RTWDOG Initialization and De-initialization
 * @{
 */

/*!
 * @brief Initializes the RTWDOG configuration structure.
 *
 * This function initializes the RTWDOG configuration structure to default values. The default
 * values are:
 * @code
 *   rtwdogConfig->enableRtwdog = true;
 *   rtwdogConfig->clockSource = kRTWDOG_ClockSource1;
 *   rtwdogConfig->prescaler = kRTWDOG_ClockPrescalerDivide1;
 *   rtwdogConfig->workMode.enableWait = true;
 *   rtwdogConfig->workMode.enableStop = false;
 *   rtwdogConfig->workMode.enableDebug = false;
 *   rtwdogConfig->testMode = kRTWDOG_TestModeDisabled;
 *   rtwdogConfig->enableUpdate = true;
 *   rtwdogConfig->enableInterrupt = false;
 *   rtwdogConfig->enableWindowMode = false;
 *   rtwdogConfig->windowValue = 0U;
 *   rtwdogConfig->timeoutValue = 0xFFFFU;
 * @endcode
 *
 * @param config Pointer to the RTWDOG configuration structure.
 * @see rtwdog_config_t
 */
void RTWDOG_GetDefaultConfig(rtwdog_config_t *config);

/*!
 * @brief Initializes the RTWDOG module.
 *
 * This function initializes the RTWDOG.
 * To reconfigure the RTWDOG without forcing a reset first, enableUpdate must be set to true
 * in the configuration.
 *
 * Example:
 * @code
 *   rtwdog_config_t config;
 *   RTWDOG_GetDefaultConfig(&config);
 *   config.timeoutValue = 0x7ffU;
 *   config.enableUpdate = true;
 *   RTWDOG_Init(wdog_base,&config);
 * @endcode
 *
 * @param base   RTWDOG peripheral base address.
 * @param config The configuration of the RTWDOG.
 */
void RTWDOG_Init(RTWDOG_Type *base, const rtwdog_config_t *config);

/*!
 * @brief De-initializes the RTWDOG module.
 *
 * This function shuts down the RTWDOG.
 * Ensure that the WDOG_CS.UPDATE is 1, which means that the register update is enabled.
 *
 * @param base   RTWDOG peripheral base address.
 */
void RTWDOG_Deinit(RTWDOG_Type *base);

/* @} */

/*!
 * @name RTWDOG functional Operation
 * @{
 */

/*!
 * @brief Enables the RTWDOG module.
 *
 * This function writes a value into the WDOG_CS register to enable the RTWDOG.
 * The WDOG_CS register is a write-once register. Ensure that the WCT window is still open and
 * this register has not been written in this WCT while the function is called.
 *
 * @param base RTWDOG peripheral base address.
 */
static inline void RTWDOG_Enable(RTWDOG_Type *base)
{
    base->CS |= RTWDOG_CS_EN_MASK;
}

/*!
 * @brief Disables the RTWDOG module.
 *
 * This function writes a value into the WDOG_CS register to disable the RTWDOG.
 * The WDOG_CS register is a write-once register. Ensure that the WCT window is still open and
 * this register has not been written in this WCT while the function is called.
 *
 * @param base RTWDOG peripheral base address
 */
static inline void RTWDOG_Disable(RTWDOG_Type *base)
{
    base->CS &= ~RTWDOG_CS_EN_MASK;
}

/*!
 * @brief Enables the RTWDOG interrupt.
 *
 * This function writes a value into the WDOG_CS register to enable the RTWDOG interrupt.
 * The WDOG_CS register is a write-once register. Ensure that the WCT window is still open and
 * this register has not been written in this WCT while the function is called.
 *
 * @param base RTWDOG peripheral base address.
 * @param mask The interrupts to enable.
 *        The parameter can be a combination of the following source if defined:
 *        @arg kRTWDOG_InterruptEnable
 */
static inline void RTWDOG_EnableInterrupts(RTWDOG_Type *base, uint32_t mask)
{
    base->CS |= mask;
}

/*!
 * @brief Disables the RTWDOG interrupt.
 *
 * This function writes a value into the WDOG_CS register to disable the RTWDOG interrupt.
 * The WDOG_CS register is a write-once register. Ensure that the WCT window is still open and
 * this register has not been written in this WCT while the function is called.
 *
 * @param base RTWDOG peripheral base address.
 * @param mask The interrupts to disabled.
 *        The parameter can be a combination of the following source if defined:
 *        @arg kRTWDOG_InterruptEnable
 */
static inline void RTWDOG_DisableInterrupts(RTWDOG_Type *base, uint32_t mask)
{
    base->CS &= ~mask;
}

/*!
 * @brief Gets the RTWDOG all status flags.
 *
 * This function gets all status flags.
 *
 * Example to get the running flag:
 * @code
 *   uint32_t status;
 *   status = RTWDOG_GetStatusFlags(wdog_base) & kRTWDOG_RunningFlag;
 * @endcode
 * @param base        RTWDOG peripheral base address
 * @return            State of the status flag: asserted (true) or not-asserted (false). @see _rtwdog_status_flags_t
 *                    - true: related status flag has been set.
 *                    - false: related status flag is not set.
 */
static inline uint32_t RTWDOG_GetStatusFlags(RTWDOG_Type *base)
{
    return (base->CS & (RTWDOG_CS_EN_MASK | RTWDOG_CS_FLG_MASK));
}

/*!
 * @brief Clears the RTWDOG flag.
 *
 * This function clears the RTWDOG status flag.
 *
 * Example to clear an interrupt flag:
 * @code
 *   RTWDOG_ClearStatusFlags(wdog_base,kRTWDOG_InterruptFlag);
 * @endcode
 * @param base        RTWDOG peripheral base address.
 * @param mask        The status flags to clear.
 *                    The parameter can be any combination of the following values:
 *                    @arg kRTWDOG_InterruptFlag
 */
void RTWDOG_ClearStatusFlags(RTWDOG_Type *base, uint32_t mask);

/*!
 * @brief Sets the RTWDOG timeout value.
 *
 * This function writes a timeout value into the WDOG_TOVAL register.
 * The WDOG_TOVAL register is a write-once register. Ensure that the WCT window is still open and
 * this register has not been written in this WCT while the function is called.
 *
 * @param base RTWDOG peripheral base address
 * @param timeoutCount RTWDOG timeout value, count of RTWDOG clock ticks.
 */
static inline void RTWDOG_SetTimeoutValue(RTWDOG_Type *base, uint16_t timeoutCount)
{
    base->TOVAL = timeoutCount;
}

/*!
 * @brief Sets the RTWDOG window value.
 *
 * This function writes a window value into the WDOG_WIN register.
 * The WDOG_WIN register is a write-once register. Ensure that the WCT window is still open and
 * this register has not been written in this WCT while the function is called.
 *
 * @param base RTWDOG peripheral base address.
 * @param windowValue RTWDOG window value.
 */
static inline void RTWDOG_SetWindowValue(RTWDOG_Type *base, uint16_t windowValue)
{
    base->WIN = windowValue;
}

/*!
 * @brief Unlocks the RTWDOG register written.
 *
 * This function unlocks the RTWDOG register written.
 *
 * Before starting the unlock sequence and following the configuration, disable the global interrupts.
 * Otherwise, an interrupt could effectively invalidate the unlock sequence and the WCT may expire.
 * After the configuration finishes, re-enable the global interrupts.
 *
 * @param base RTWDOG peripheral base address
 */
static inline void RTWDOG_Unlock(RTWDOG_Type *base)
{
    if ((base->CS) & RTWDOG_CS_CMD32EN_MASK)
    {
        base->CNT = RTWDOG_UPDATE_KEY;
    }
    else
    {
        base->CNT = WDOG_FIRST_WORD_OF_UNLOCK;
        base->CNT = WDOG_SECOND_WORD_OF_UNLOCK;
    }
}

/*!
 * @brief Refreshes the RTWDOG timer.
 *
 * This function feeds the RTWDOG.
 * This function should be called before the Watchdog timer is in timeout. Otherwise, a reset is asserted.
 *
 * @param base RTWDOG peripheral base address
 */
static inline void RTWDOG_Refresh(RTWDOG_Type *base)
{
    if ((base->CS) & RTWDOG_CS_CMD32EN_MASK)
    {
        base->CNT = RTWDOG_REFRESH_KEY;
    }
    else
    {
        base->CNT = WDOG_FIRST_WORD_OF_REFRESH;
        base->CNT = WDOG_SECOND_WORD_OF_REFRESH;
    }
}

/*!
 * @brief Gets the RTWDOG counter value.
 *
 * This function gets the RTWDOG counter value.
 *
 * @param base RTWDOG peripheral base address.
 * @return     Current RTWDOG counter value.
 */
static inline uint16_t RTWDOG_GetCounterValue(RTWDOG_Type *base)
{
    return base->CNT;
}

/*@}*/

#if defined(__cplusplus)
}
#endif /* __cplusplus */

/*! @}*/

#endif /* _FSL_RTWDOG_H_ */