/**
 * @file watchdog.c
 * @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:
 *     - Optimization of WATCHDOG_Init() function<br>
 *     - Added a macro "WATCHDOG_NORMAL_INTERRUPT" to avoid misra warning<br>
 *
 * 2015-06-20:
 *     - Pre-warn event registration is updated by using GLOBAL_SCU APP macros.
 *
 * 2015-11-30:
 *     - macro WATCHDOG_EVENT_CONFIGURATION is replaced with WATCHDOG_EVENT_VIA_SCU.
 *     - NMI support for XMC4000 devices is added.
 *
 * @endcond
 *
 */

/***********************************************************************************************************************
 * HEADER FILES
 **********************************************************************************************************************/
#include "watchdog.h"

/***********************************************************************************************************************
 * MACROS
 **********************************************************************************************************************/

/***********************************************************************************************************************
 * LOCAL DATA
 **********************************************************************************************************************/

/***********************************************************************************************************************
 * LOCAL ROUTINES
 **********************************************************************************************************************/
#if (WATCHDOG_PREWARNING_CHECK == 1U)
/*
 * Register the Pre-warning event with GLOBAL_SCU APP
 */
WATCHDOG_STATUS_t WATCHDOG_lPrewarning_Configure(WATCHDOG_t *handle);
#endif

/**********************************************************************************************************************
 * API IMPLEMENTATION
 **********************************************************************************************************************/
/* Returns the version of the WATCHDOG APP. */
DAVE_APP_VERSION_t WATCHDOG_GetAppVersion(void)
{
  DAVE_APP_VERSION_t version;

  version.major = (uint8_t)WATCHDOG_MAJOR_VERSION;
  version.minor = (uint8_t)WATCHDOG_MINOR_VERSION;
  version.patch = (uint8_t)WATCHDOG_PATCH_VERSION;

  return (version);
}

/* Initializes the watchdog timer with the generated configuration */
WATCHDOG_STATUS_t WATCHDOG_Init(WATCHDOG_t *handle)
{
  WATCHDOG_STATUS_t status;

  XMC_ASSERT("WATCHDOG_Init:handle NULL" , (handle != NULL));

  status = WATCHDOG_STATUS_SUCCESS;

  /* Check for app instance is initialized or not */
  if (false == handle->initialized)
  {
#if (WATCHDOG_PREWARNING_CHECK == 1U)
      /* Service Event Handling */
      status = WATCHDOG_lPrewarning_Configure(handle);
      if(WATCHDOG_STATUS_FAILURE != status)
      {
#endif
        /* Initialize the WDT peripheral */
        XMC_WDT_Init(handle->config);
        /* Update the initialization flag */
        handle->initialized = true;
#if (WATCHDOG_PREWARNING_CHECK == 1U)
      }
#endif
  }

  return (status);
}

/***********************************************************************************************************************
**                                        Private function definitions                                                **
***********************************************************************************************************************/
#if (WATCHDOG_PREWARNING_CHECK == 1U)
/*
 * Register the Pre-warning event with GLOBAL_SCU APP
 */
WATCHDOG_STATUS_t WATCHDOG_lPrewarning_Configure(WATCHDOG_t *handle)
{
  WATCHDOG_STATUS_t status;
  status = WATCHDOG_STATUS_SUCCESS;
  /* Normal interrupt generation */
#if (WATCHDOG_EVENT_VIA_SCU == 1U)
  #if(UC_FAMILY == XMC4)
    status = (WATCHDOG_STATUS_t)GLOBAL_SCU_XMC4_Init(handle->scu_global_handler);
  #else
    status = (WATCHDOG_STATUS_t)GLOBAL_SCU_XMC1_Init(handle->scu_global_handler);
  #endif
    if ((handle->callback_func_ptr != NULL) &&  (status != WATCHDOG_STATUS_FAILURE))
    {
      /* Enable the pre-warn event */
      XMC_SCU_INTERRUPT_EnableEvent((uint32_t)XMC_SCU_INTERRUPT_EVENT_WDT_WARN);
      /* Register User defined Event Handler function */
	  #if(UC_FAMILY == XMC4)
      status = (WATCHDOG_STATUS_t)GLOBAL_SCU_XMC4_RegisterCallback(GLOBAL_SCU_XMC4_EVENT_WDT_WARNING,
    		                                                       handle->callback_func_ptr);
	  #else
      status = (WATCHDOG_STATUS_t)GLOBAL_SCU_XMC1_RegisterCallback(GLOBAL_SCU_XMC1_EVENT_WDT_WARNING,
    		                                                       handle->callback_func_ptr);
	  #endif
    }
    else
    {
      status = WATCHDOG_STATUS_FAILURE;
    }
#endif

#if (WATCHDOG_EVENT_VIA_NMI == 1U)
    XMC_SCU_INTERRUPT_EnableEvent((uint32_t)XMC_SCU_INTERRUPT_EVENT_WDT_WARN);
    XMC_SCU_INTERRUPT_EnableNmiRequest((uint32_t)XMC_SCU_INTERRUPT_EVENT_WDT_WARN);
#endif

  return (status);
}
#endif
