/**
 * @file watchdog.h
 * @date 2016-02-09
 *
 * NOTE:
 * This file is generated by DAVE. Any manual modification done to this file will be lost when the code is regenerated.
 *
 * @cond
 ***********************************************************************************************************************
 * WATCHDOG v4.0.14 - Configures the watchdog peripheral instance.
 *
 * Copyright (c) 2016, Infineon Technologies AG
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,are permitted provided that the
 * following conditions are met:
 *
 *   Redistributions of source code must retain the above copyright notice, this list of conditions and the  following
 *   disclaimer.
 *
 *   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.
 *
 *   Neither the name of the copyright holders 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.
 *
 * To improve the quality of the software, users are encouraged to share modifications, enhancements or bug fixes
 * with Infineon Technologies AG (dave@infineon.com).
 ***********************************************************************************************************************
 *
 * Change History
 * --------------
 *
 * 2015-02-16:
 *     - Initial version<br>
 *
 * 2015-05-08:
 *     - C++ guard applied to only API section<br>
 *     - File guard updated according to the guidelines<br>
 *     - Enum items of "WATCHDOG_EVENT_MODE" are changed to follow coding guidelines<br>
 *
 * 2015-06-20:
 *     - "callback_func_ptr" in APP config structure is updated according to the GLOBAL_SCU APP.
 *
 * 2015-06-20:
 *     - macro WATCHDOG_EVENT_CONFIGURATION is replaced with WATCHDOG_EVENT_VIA_SCU.
 *
 * 2016-02-09:
 *     - Added a check for XMC4 devices, to verify that WATCHDOG clock is enabled or not in CLOCK_XMC4 APP.
 *
 * @endcond
 *
 */

#ifndef WATCHDOG_H
#define WATCHDOG_H
/***********************************************************************************************************************
 * HEADER FILES
 **********************************************************************************************************************/
#include <xmc_wdt.h>
#if(UC_FAMILY == XMC4)
#include <CLOCK_XMC4/clock_xmc4.h>
#else
#include <CLOCK_XMC1/clock_xmc1.h>
#endif
#include "watchdog_conf.h"
#include <DAVE_Common.h>

/**********************************************************************************************************************
 * MACROS
 **********************************************************************************************************************/
#if (!((XMC_LIB_MAJOR_VERSION == 2U) && \
       (XMC_LIB_MINOR_VERSION >= 0U) && \
       (XMC_LIB_PATCH_VERSION >= 0U)))
#error "WATCHDOG requires XMC Peripheral Library v2.0.0 or higher"
#endif

/**********************************************************************************************************************
 * ENUMS
 **********************************************************************************************************************/
/**
 * @ingroup WATCHDOG_enumerations
 * @{
 */

/**
 *  @brief In case of XMC4000 devices, WatchDog event can be generated as  Regular Interrupt or can be promoted as
 *         NMI Request. Where as in case of XMC1000 device, it can be generated as Regular Interrupt only.
 */
typedef enum WATCHDOG_EVENT_MODE
{
    WATCHDOG_EVENT_MODE_INTERRUPT   = 0U, /**< Regular Interrupt */
    WATCHDOG_EVENT_MODE_NMI_REQUEST = 1U /**< Promoted to NMI */
} WATCHDOG_EVENT_MODE_t;

/**
 *  @brief Initialization status.
 */
typedef enum WATCHDOG_STATUS
{
  WATCHDOG_STATUS_SUCCESS = 0U, /**< Status success */
  WATCHDOG_STATUS_FAILURE, /**< Status failure */
} WATCHDOG_STATUS_t;
/**
* @}
*/

/***********************************************************************************************************************
* DATA STRUCTURES
***********************************************************************************************************************/
/**
 * @ingroup WATCHDOG_datastructures
 * @{
 */

/**
 * @brief WATCHDOG_HandleType is  data structure for storing the GUI configured values of a WATCHDOG app Instance.
 */
typedef struct WATCHDOG
{
  const XMC_WDT_CONFIG_t* const config; /**< Configuration of WDT */
  #if ((WATCHDOG_PREWARNING_CHECK == 1U) && (WATCHDOG_EVENT_VIA_SCU == 1U))
  #if(UC_FAMILY == XMC4)
  GLOBAL_SCU_XMC4_t* const scu_global_handler; /**< pointer to SCU_GLOBAL APP instance*/
  GLOBAL_SCU_XMC4_EVENT_HANDLER_t callback_func_ptr; /**< Callback function pointer */
  #else
  GLOBAL_SCU_XMC1_t* const scu_global_handler; /**< pointer to SCU_GLOBAL APP instance*/
  GLOBAL_SCU_XMC1_EVENT_HANDLER_t callback_func_ptr; /**< Callback function pointer */
  #endif
  #endif
  bool  initialized; /**< Holds the initialization status */
} WATCHDOG_t;

/**
* @}
*/
/***********************************************************************************************************************
* API Prototypes
***********************************************************************************************************************/
#ifdef __cplusplus
extern "C" {
#endif
/**
 * @brief Get WATCHDOG APP version
 * @return DAVE_APP_VERSION_t APP version information (major, minor and patch number)
 * <BR>
 * \par<b>Description:</b><br>
 * The function can be used to check application software compatibility with a
 * specific version of the APP.
 *
 * Example Usage:
 *
 * @code
 * #include <DAVE.h>
 * int main(void)
 * {
 *   DAVE_STATUS_t status;
 *   DAVE_APP_VERSION_t app_version;
 *
 *   status = DAVE_Init();    // WATCHDOG_Init() is called from DAVE_Init()
 *
 *   app_version = WATCHDOG_GetAppVersion();
 *
 *   if (app_version.major != 4U)
 *   {
 *     // Probably, not the right version.
 *   }
 *
 *   while(1U)
 *   {
 *   }
 *   return 1;
 * }
 * @endcode<BR>
 */

DAVE_APP_VERSION_t WATCHDOG_GetAppVersion(void);

/**
 * @ingroup WATCHDOG_apidoc
 * @{
 */

/**
 * @brief Initializes a WATCHDOG with generated configuration.
 *
 * @param handle pointer to the WATCHDOG APP handle structure.
 * @return WATCHDOG_STATUS_t\n
 *         WATCHDOG_STATUS_SUCCESS : if initialization is successful\n
 *         WATCHDOG_STATUS_FAILURE : if initialization is failed
 * <BR>
 * \par<b>Description:</b><br>
 * <ul>
 * <li>Enable the WDT module.</li>
 * <li>Configure the service upper and lower bound settings</li>
 * <li>Configure the Pre-warning alarm event, if enabled</li>
 * </ul>
 *
 * Example Usage:
 *
 * @code
 * #include <DAVE.h>
 * int main(void)
 * {
 *  // ... Initializes Apps configurations ...
 *  DAVE_Init(); // WATCHDOG_Init(&WATCHDOG_0) will be called from DAVE_Init()
 *
 *  while(1U)
 *  {
 *  }
 *
 *  return 1;
 * }
 *
 * @endcode<BR> </p>
 */
WATCHDOG_STATUS_t WATCHDOG_Init(WATCHDOG_t *handle);

/**
 * @brief Starts the WATCHDOG timer.
 *
 * @return None
 * <BR>
 * 
 * \par<b>Description:</b><br>
 * After starting, WDT has to be serviced with in the specified service window boundary.
 * This can done by invoking WATCHDOG_Service(). If failed service, leads to system reset.
 *
 * Example Usage:
 *
 * @code
 * #include <DAVE.h>
 * int main(void)
 * {
 *   DAVE_STATUS_t status;
 *
 *   status = DAVE_Init();        // Initialization of DAVE Apps
 *
 *   if(status == DAVE_STATUS_SUCCESS)
 *   {
 *     WATCHDOG_Start(); // Start the watchdog timer
 *   }
 *
 *   while(1U)
 *   {
 *   }
 *
 *   return 1;
 * }
 *
 * void Watchdog_Handler(void)
 * {
 *   WATCHDOG_Service(); //service the watchdog timer.
 *   WATCHDOG_Stop();
 * }
 *
 * @endcode<BR> </p>
 */
__STATIC_INLINE void WATCHDOG_Start(void)
{
  XMC_WDT_Start();
}

/**
 * @brief Stops the WATCHDOG timer.
 *
 * @return None
 *            <BR>
 *
 * \par<b>Description:</b><br>
 * WATCHDOG_Stop() can be used to pause the watchog timer as needed. To resume invoke WATCHDOG_Start().
 * 
 * Example Usage:
 *
 * @code
 * #include <DAVE.h>
 * int main(void)
 * {
 *   DAVE_STATUS_t status;
 *
 *   status = DAVE_Init();        // Initialization of DAVE Apps
 *
 *   if(status == DAVE_STATUS_SUCCESS)
 *   {
 *     WATCHDOG_Start(); // Start the watchdog timer
 *   }
 *
 *   while(1U)
 *   {
 *   }
 *
 *   return 1;
 * }
 *
 * void Watchdog_Handler(void)
 * {
 *   WATCHDOG_ClearAlarm(); //clears the alarm flag
 *   WATCHDOG_Service(); //service the watchdog timer.
 *   WATCHDOG_Stop(); //Stop the watchdog timer
 * }
 *
 * @endcode<BR> </p>
 */
__STATIC_INLINE void WATCHDOG_Stop(void)
{
  XMC_WDT_Stop();
}

/**
 * @brief Service the WATCHDOG timer.
 *
 * @return    None                           
 *            <BR>
 *
 * \par<b>Description:</b><br>
 * WATCHDOG_Service() will reset the watchdog timer.
 *
 * Example Usage:
 * 
 * @code
 * #include <DAVE.h>
 * int main(void)
 * {
 *   DAVE_STATUS_t status;
 *
 *   status = DAVE_Init();        // Initialization of DAVE Apps
 *
 *   if(status == DAVE_STATUS_SUCCESS)
 *   {
 *     WATCHDOG_Start(); // Start the watchdog timer
 *   }
 *
 *   while(1U)
 *   {
 *   }
 *
 *   return 1;
 * }
 *
 * void Watchdog_Handler(void)
 * {
 *   WATCHDOG_ClearAlarm(); //clears the alarm flag
 *   WATCHDOG_Service(); //service the watchdog timer.
 *   WATCHDOG_Stop();
 * }
 *
 * @endcode<BR> </p>
 */
__STATIC_INLINE void WATCHDOG_Service(void)
{
  XMC_WDT_Service();
}

/**
 * @brief Clears the alarm flag.
 *
 * @return None
 * <BR>
 * \par<b>Description:</b><br>
 * After occurrence of Pre-warning alarm event, the timer has to be serviced with in second over flow by
 * clearing the alarm flag. Otherwise it will lead to system reset.
 *
 * Example Usage:
 *
 * @code
 * #include <DAVE.h>
 * int main(void)
 * {
 *   DAVE_STATUS_t status;
 *
 *   status = DAVE_Init();        // Initialization of DAVE Apps
 *
 *   if(status == DAVE_STATUS_SUCCESS)
 *   {
 *     WATCHDOG_Start(); // Start the watchdog timer
 *   }
 *
 *   while(1U)
 *   {
 *   }
 *
 *   return 1;
 * }
 *
 * void Watchdog_Handler(void)
 * {
 *   WATCHDOG_ClearAlarm(); //clears the alarm flag
 *   WATCHDOG_Service(); //service the watchdog timer.
 * }
 *
 * @endcode<BR> </p>
 */
__STATIC_INLINE void WATCHDOG_ClearAlarm(void)
{
  XMC_WDT_ClearAlarm();
}

/**
 * @brief Returns the present watchdog counter value.
 *
 * @return uint32_t watchdog counter value
 *            <BR>
 *
 * \par<b>Description:</b><br>
 * The counter value can be utilized where continuous monitoring is required.
 *
 * Example Usage:
 *
 * @code
 * #include <DAVE.h>
 * int main(void)
 * {
 *  uint32_t wdt_counter;
 *  DAVE_STATUS_t status;
 *
 *  status = DAVE_Init();        // Initialization of DAVE Apps
 *
 *  if(status == DAVE_STATUS_SUCCESS)
 *  {
 *    WATCHDOG_Start(); // Start the watchdog timer
 *  }
 *
 *  //Application Code
 * wdt_counter = WATCHDOG_GetCounter();
 *
 *
 *  while(1U)
 *  {
 *  }
 *
 *  return 1;
 * }
 *
 * @endcode<BR> </p>
 */
__STATIC_INLINE uint32_t WATCHDOG_GetCounter(void)
{
  return(XMC_WDT_GetCounter());
}

/**
 *@}
 */

#include "watchdog_extern.h"   /* Included to access the app Handles at Main.c */

#ifdef __cplusplus
}
#endif

#endif /* WATCHDOG_H */
