/**
 * @file xmc_acmp.c
 * @date 2019-12-16
 *
 * @cond
 *****************************************************************************
 * XMClib v2.2.0 - XMC Peripheral Driver Library
 *
 * Copyright (c) 2015-2020, Infineon Technologies AG
 * All rights reserved.
 *
 * Boost Software License - Version 1.0 - August 17th, 2003
 *
 * Permission is hereby granted, free of charge, to any person or organization
 * obtaining a copy of the software and accompanying documentation covered by
 * this license (the "Software") to use, reproduce, display, distribute,
 * execute, and transmit the Software, and to prepare derivative works of the
 * Software, and to permit third-parties to whom the Software is furnished to
 * do so, all subject to the following:
 *
 * The copyright notices in the Software and this entire statement, including
 * the above license grant, this restriction and the following disclaimer,
 * must be included in all copies of the Software, in whole or in part, and
 * all derivative works of the Software, unless such copies or derivative
 * works are solely in the form of machine-executable object code generated by
 * a source language processor.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
 * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
 * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 * To improve the quality of the software, users are encouraged to share
 * modifications, enhancements or bug fixes with Infineon Technologies AG
 * at XMCSupport@infineon.com.
 *****************************************************************************
 *
 * Change History
 * --------------
 *
 * 2014-12-10:
 *     - Initial <br>
 * 2015-02-20:
 *     - Removed unused declarations<br>
 * 2015-05-08:
 *     - Fixed sequence problem of low power mode in XMC_ACMP_Init() API<br>
 *     - Fixed wrong register setting in XMC_ACMP_SetInput() API<br>
 *     - Removed return type variable and by default comparator enable from XMC_ACMP_Init() API. <br>
 *       Additional call to XMC_ACMP_EnableComparator() API needed to start Comparator after Init.<br>
 * 2015-06-04:
 *     - Removed return type variable and by default comparator enable from XMC_ACMP_Init() API. <br>
 *     - Divided XMC_ACMP_SetInput into two 3 APIs to reduce the code size and complexity as stated below<br>
 *       (a)XMC_ACMP_EnableReferenceDivider <br>
 *       (b)XMC_ACMP_DisableReferenceDivider <br>
 *       (c)XMC_ACMP_SetInput <br>
 *     - Optimized enable and disable API's and moved to header file as static inline APIs.
 *     - XMC_ACMP_t typedef changed to structure which overrides the standard header file structure.
 * 2015-06-20:
 *     - Removed definition of GetDriverVersion API
 * 2019-12-16:
 *     - Fix including files following the convention: angle brackets are used for standard includes and double quotes for everything else.
 *
 * @endcond
 *
 */


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

/* If ACMP is available*/
#if defined (COMPARATOR)

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

#define XMC_ACMP_INSTANCE_1    (1U) /* Instance number for Slice-1 */

/*********************************************************************************************************************
 * API IMPLEMENTATION
 ********************************************************************************************************************/

/* API to initialize an instance of ACMP module */
void XMC_ACMP_Init(XMC_ACMP_t *const peripheral, uint32_t instance, const XMC_ACMP_CONFIG_t *const config)
{

  XMC_ASSERT("XMC_ACMP_Init:NULL Configuration", (config != (XMC_ACMP_CONFIG_t *)NULL))
  XMC_ASSERT("XMC_ACMP_Init:Wrong module pointer", XMC_ACMP_CHECK_MODULE_PTR(peripheral))
  XMC_ASSERT("XMC_ACMP_Init:Wrong instance number", XMC_ACMP_CHECK_INSTANCE(instance) )

  /*
   * Initializes the comparator with configuration supplied. Low power node setting is retained during initialization.
   * All the instances passed are handled with low power setting, to avoid conditional check for ACMP0 instance.
   * This reduces the code size. No side effects, because this register bit field is empty for other instances.
   */
  peripheral->ANACMP[instance] = ((peripheral->ANACMP[instance] & (uint32_t)COMPARATOR_ANACMP0_CMP_LPWR_Msk)) |
                                 (uint32_t)config->anacmp;
}

/* API to select INP source */
void XMC_ACMP_SetInput(XMC_ACMP_t *const peripheral, uint32_t instance, XMC_ACMP_INP_SOURCE_t source)
{
  XMC_ASSERT("XMC_ACMP_SetInput:Wrong module pointer", XMC_ACMP_CHECK_MODULE_PTR(peripheral))
  XMC_ASSERT("XMC_ACMP_SetInput:Wrong instance number", ((instance != XMC_ACMP_INSTANCE_1) &&
             XMC_ACMP_CHECK_INSTANCE(instance)) )
  XMC_ASSERT("XMC_ACMP_SetInput:Wrong input source", ((source == XMC_ACMP_INP_SOURCE_STANDARD_PORT) ||
             (source == XMC_ACMP_INP_SOURCE_ACMP1_INP_PORT)) )

  /*
   * Three options of Input Setting are listed below
   * 1. The comparator inputs aren't connected to other comparator inputs
   * 2. Can program the comparators to connect ACMP0.INP to ACMP1.INP in XMC1200 AA or XMC1300 AA
   *    Can program the comparators to connect ACMP0.INN to ACMP1.INP in XMC1200 AB or XMC1300 AB or XMC1400 AA
   * 3. Can program the comparators to connect ACMP2.INP to ACMP1.INP
   * 4. Can program the comparators to connect ACMP3.INP to ACMP1.INP in XMC1400
   */
  peripheral->ANACMP[instance] = ((peripheral->ANACMP[instance] & (uint32_t)(~COMPARATOR_ANACMP0_ACMP0_SEL_Msk))) |
                                 (uint32_t)source;
}

#endif /* #ifdef ACMP_AVAILABLE */
