/**
 * @file xmc_common.h
 * @date 2020-05-05
 *
 * @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
 * --------------
 *
 * 2015-06-20:
 *     - Initial
 *     - Brief section updated
 *     - Added XMC_LIB_VERSION macro
 *
 * 2016-02-26:
 *     - Updated XMC_LIB_VERSION macro to v2.1.6
 *
 * 2016-05-30:
 *     - Updated XMC_LIB_VERSION macro to v2.1.8
 *
 * 2016-11-18:
 *     - Updated XMC_LIB_VERSION macro to v2.1.10
 *     - Changed type of size in XMC_PRIOARRAY_t to fix compilation warnings
 *
 * 2017-04-04:
 *     - Updated XMC_LIB_VERSION macro to v2.1.12
 *
 * 2017-06-24:
 *     - Updated XMC_LIB_VERSION macro to v2.1.14
 *
 * 2017-08-03:
 *     - Updated XMC_LIB_VERSION macro to v2.1.16
 *     - Added __RAM_FUNC macro
 *
 * 2018-06-29:
 *     - Updated XMC_LIB_VERSION macro to v2.1.20
 *
 * 2019-05-29:
 *     - Updated XMC_LIB_VERSION macro to v2.1.22
 *
 * 2019-07-12:
 *     - Updated XMC_LIB_VERSION macro to v2.1.24
 *
 * 2019-11-19:
 *     - Updated XMC_LIB_VERSION macro to v2.2.0
 *
 * 2019-12-16:
 *     - Added include for inttypes.h
 *
 * 2020-05-05:
 *     - Added XMC_VERSION_NUMBER to build version number given major, minor and patch numbers
 *
 * @endcond
 *
 */

#ifndef XMC_COMMON_H
#define XMC_COMMON_H

#include <stddef.h>
#include <stdint.h>
#include <inttypes.h>
#include <stdbool.h>
#include <string.h>

#include "xmc_device.h"

/**
 * @addtogroup XMClib XMC Peripheral Library
 * @{
 */

/**
 * @addtogroup COMMON
 * @brief Common APIs to all peripherals for XMC microcontroller family
 * @{
 */

/**********************************************************************************************************************
 * MACROS
 *********************************************************************************************************************/
#define XMC_LIB_MAJOR_VERSION	(2U)
#define XMC_LIB_MINOR_VERSION	(2U)
#define XMC_LIB_PATCH_VERSION	(0U)

#define XMC_LIB_VERSION         ((XMC_LIB_MAJOR_VERSION << 16U) + (XMC_LIB_MINOR_VERSION << 8U) + XMC_LIB_PATCH_VERSION)

#define XMC_VERSION_NUMBER(major, minor, patch)         ((major << 16U) + (minor << 8U) + patch)

/* Define WEAK attribute */
#if !defined(__WEAK)
#if defined ( __CC_ARM )
#define __WEAK __attribute__ ((weak))
#elif defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
#define __WEAK __attribute__ ((weak))
#elif defined ( __ICCARM__ )
#define __WEAK __weak
#elif defined ( __GNUC__ )
#define __WEAK __attribute__ ((weak))
#elif defined ( __TASKING__ )
#define __WEAK __attribute__ ((weak))
#endif
#endif

#if !defined(__RAM_FUNC)
#if defined ( __CC_ARM )
// http://www.keil.com/support/docs/3723.htm
#define __RAM_FUNC __attribute__((section("RAMCODESECTION")))
#elif defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
#define __RAM_FUNC __attribute__((section("RAMCODESECTION")))
#elif defined ( __ICCARM__ )
// https://www.iar.com/support/tech-notes/linker/controlling-placement-of-the-section-where-__ramfunc-functions-reside-ewarm-5.x--6.x/
#define __RAM_FUNC __ramfunc
#elif defined ( __GNUC__ )
#define __RAM_FUNC __attribute__((section(".ram_code"), long_call))
#elif defined ( __TASKING__ )
#define __RAM_FUNC __attribute__((section(".ram_code")))
#endif
#endif

#ifdef XMC_ASSERT_ENABLE
#define XMC_ASSERT(msg, exp) { if(!(exp)) {XMC_AssertHandler(msg, __FILE__, __LINE__);} }
#else
#define XMC_ASSERT(msg, exp)
#endif

#ifdef XMC_DEBUG_ENABLE
#include <stdio.h>
#define XMC_DEBUG(...) { printf(__VA_ARGS__); }
#else
#define XMC_DEBUG(...)
#endif

#define XMC_UNUSED_ARG(x) (void)x

#define XMC_STRUCT_INIT(m) memset(&m, 0, sizeof(m))

/**********************************************************************************************************************
 * DATA STRUCTURES
 *********************************************************************************************************************/
/*
 *
 */
typedef struct XMC_DRIVER_VERSION
{
  uint8_t major;
  uint8_t minor;
  uint8_t patch;
} XMC_DRIVER_VERSION_t;

/**********************************************************************************************************************
 * API PROTOTYPES
 *********************************************************************************************************************/

#ifdef __cplusplus
extern "C" {
#endif

/*
 *
 */
void XMC_AssertHandler(const char *const msg, const char *const file, uint32_t line);

#ifdef __cplusplus
}
#endif

/**
 * @}
 */

/**
 * @}
 */

#endif /* XMC_COMMON_H */
