/*==================================================================================================
*   Project              : RTD AUTOSAR 4.4
*   Platform             : CORTEXM
*   Peripheral           : LPI2C
*   Dependencies         : none
*
*   Autosar Version      : 4.4.0
*   Autosar Revision     : ASR_REL_4_4_REV_0000
*   Autosar Conf.Variant :
*   SW Version           : 0.9.0
*   Build Version        : S32K3_RTD_0_9_0__ASR_REL_4_4_REV_0000_20210326
*
*   (c) Copyright 2020 - 2021 NXP Semiconductors
*   All Rights Reserved.
*
*   NXP Confidential. This software is owned or controlled by NXP and may only be
*   used strictly in accordance with the applicable license terms. By expressly
*   accepting such terms or by downloading, installing, activating and/or otherwise
*   using the software, you are agreeing that you have read, and that you agree to
*   comply with and are bound by, such license terms. If you do not agree to be
*   bound by the applicable license terms, then you may not retain, install,
*   activate or otherwise use the software.
==================================================================================================*/

#if !defined(LPI2C_IP_H)
#define LPI2C_IP_H

/**
*     @file
*
*     @addtogroup LPI2C_DRIVER Lpi2c Driver
*     @{
*/

 /*==================================================================================================
*                                        INCLUDE FILES
* 1) system and project includes
* 2) needed interfaces from external units
* 3) internal and external interfaces from this unit
==================================================================================================*/
#include "Lpi2c_Ip_Callbacks.h"
#include "S32K344_LPI2C.h"
#include "Lpi2c_Ip_Types.h"
#include "Lpi2c_Ip_Cfg.h"
#include "Mcal.h"

/*==================================================================================================
*                              SOURCE FILE VERSION INFORMATION
==================================================================================================*/
#define LPI2C_IP_VENDOR_ID                      43
#define LPI2C_IP_AR_RELEASE_MAJOR_VERSION       4
#define LPI2C_IP_AR_RELEASE_MINOR_VERSION       4
#define LPI2C_IP_AR_RELEASE_REVISION_VERSION    0
#define LPI2C_IP_SW_MAJOR_VERSION               0
#define LPI2C_IP_SW_MINOR_VERSION               9
#define LPI2C_IP_SW_PATCH_VERSION               0

/*==================================================================================================
*                                     FILE VERSION CHECKS
==================================================================================================*/
/* Checks against I2c_Ip_Callbacks.h */
#if (LPI2C_IP_VENDOR_ID != LPI2C_IP_CALLBACKS_VENDOR_ID)
    #error "LPI2C_Ip.h and Lpi2c_Ip_Callbacks.h have different vendor ids"
#endif
#if (( LPI2C_IP_AR_RELEASE_MAJOR_VERSION    != LPI2C_IP_CALLBACKS_AR_RELEASE_MAJOR_VERSION) || \
     ( LPI2C_IP_AR_RELEASE_MINOR_VERSION    != LPI2C_IP_CALLBACKS_AR_RELEASE_MINOR_VERSION) || \
     ( LPI2C_IP_AR_RELEASE_REVISION_VERSION != LPI2C_IP_CALLBACKS_AR_RELEASE_REVISION_VERSION))
     #error "AUTOSAR Version Numbers of LPI2C_Ip.h and Lpi2c_Ip_Callbacks.h are different"
#endif
#if (( LPI2C_IP_SW_MAJOR_VERSION != LPI2C_IP_CALLBACKS_SW_MAJOR_VERSION) || \
     ( LPI2C_IP_SW_MINOR_VERSION != LPI2C_IP_CALLBACKS_SW_MINOR_VERSION) || \
     ( LPI2C_IP_SW_PATCH_VERSION != LPI2C_IP_CALLBACKS_SW_PATCH_VERSION))
    #error "Software Version Numbers of LPI2C_Ip.h and Lpi2c_Ip_Callbacks.h are different"
#endif

/* Checks against LPI2C_Ip_Types.h */
#if (LPI2C_IP_VENDOR_ID != LPI2C_IP_TYPES_VENDOR_ID)
    #error "LPI2C_Ip.h and LPI2C_Ip_Types.h have different vendor ids"
#endif
#if (( LPI2C_IP_AR_RELEASE_MAJOR_VERSION    != LPI2C_IP_TYPES_AR_RELEASE_MAJOR_VERSION) || \
     ( LPI2C_IP_AR_RELEASE_MINOR_VERSION    != LPI2C_IP_TYPES_AR_RELEASE_MINOR_VERSION) || \
     ( LPI2C_IP_AR_RELEASE_REVISION_VERSION != LPI2C_IP_TYPES_AR_RELEASE_REVISION_VERSION))
     #error "AUTOSAR Version Numbers of LPI2C_Ip.h and LPI2C_Ip_Types.h are different"
#endif
#if (( LPI2C_IP_SW_MAJOR_VERSION != LPI2C_IP_TYPES_SW_MAJOR_VERSION) || \
     ( LPI2C_IP_SW_MINOR_VERSION != LPI2C_IP_TYPES_SW_MINOR_VERSION) || \
     ( LPI2C_IP_SW_PATCH_VERSION != LPI2C_IP_TYPES_SW_PATCH_VERSION))
    #error "Software Version Numbers of LPI2C_Ip.h and LPI2C_Ip_Types.h are different"
#endif

/* Checks against LPI2C_Ip_Cfg.h */
#if (LPI2C_IP_VENDOR_ID != LPI2C_IP_CFG_VENDOR_ID)
    #error "LPI2C_Ip.h and LPI2C_Ip_Cfg.h have different vendor ids"
#endif
#if (( LPI2C_IP_AR_RELEASE_MAJOR_VERSION    != LPI2C_IP_CFG_AR_RELEASE_MAJOR_VERSION) || \
     ( LPI2C_IP_AR_RELEASE_MINOR_VERSION    != LPI2C_IP_CFG_AR_RELEASE_MINOR_VERSION) || \
     ( LPI2C_IP_AR_RELEASE_REVISION_VERSION != LPI2C_IP_CFG_AR_RELEASE_REVISION_VERSION))
     #error "AUTOSAR Version Numbers of LPI2C_Ip.h and LPI2C_Ip_Cfg.h are different"
#endif
#if (( LPI2C_IP_SW_MAJOR_VERSION != LPI2C_IP_CFG_SW_MAJOR_VERSION) || \
     ( LPI2C_IP_SW_MINOR_VERSION != LPI2C_IP_CFG_SW_MINOR_VERSION) || \
     ( LPI2C_IP_SW_PATCH_VERSION != LPI2C_IP_CFG_SW_PATCH_VERSION))
    #error "Software Version Numbers of LPI2C_Ip.h and LPI2C_Ip_Cfg.h are different"
#endif

#ifndef DISABLE_MCAL_INTERMODULE_ASR_CHECK
    /* Check if current file and Mcal.h header file are of the same Autosar version */
    #if ( (LPI2C_IP_AR_RELEASE_MAJOR_VERSION != MCAL_AR_RELEASE_MAJOR_VERSION) || \
          (LPI2C_IP_AR_RELEASE_MINOR_VERSION != MCAL_AR_RELEASE_MINOR_VERSION)    \
        )
        #error "AutoSar Version Numbers of CDD_I2c.h and Mcal.h are different"
    #endif
#endif

/*******************************************************************************
 * Enumerations.
 ******************************************************************************/

/*******************************************************************************
 * API
 ******************************************************************************/
/**
 * @name LPI2C Driver
 * @{
 */

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

/**
 * @brief Master state array sructure
 *
 * @details Array with the master state structures for each instance used.
 */
#if(LPI2C_IP_NUMBER_OF_MASTER_INSTANCES != 0U)
	
#define I2C_START_SEC_VAR_NO_INIT_UNSPECIFIED
#include "I2c_MemMap.h"

extern Lpi2c_Ip_MasterStateType Lpi2c_Ip_MasterState[LPI2C_IP_NUMBER_OF_MASTER_INSTANCES];

#define I2C_STOP_SEC_VAR_NO_INIT_UNSPECIFIED
#include "I2c_MemMap.h"

#endif

/**
 * @brief Slave state array structure
 *
 * @details Array with the slave state structures for each instance used.
 */
#if(LPI2C_IP_NUMBER_OF_SLAVE_INSTANCES != 0U)
	
#define I2C_START_SEC_VAR_NO_INIT_UNSPECIFIED
#include "I2c_MemMap.h"

extern Lpi2c_Ip_SlaveStateType Lpi2c_Ip_SlaveState[LPI2C_IP_NUMBER_OF_SLAVE_INSTANCES];

#define I2C_STOP_SEC_VAR_NO_INIT_UNSPECIFIED
#include "I2c_MemMap.h"

#endif




/**
* @note put all I2C code into defined section
*/
#define I2C_START_SEC_CODE
#include "I2c_MemMap.h"

/**
 * @brief Initialize the LPI2C master mode driver
 *
 * @details This function initializes the LPI2C driver in master mode.
 *
 * @param[in] instance  LPI2C peripheral instance number
 * @param[in] userConfigPtr    Pointer to the LPI2C master user configuration structure. The function
 *                         reads configuration data from this structure and initializes the
 *                         driver accordingly. The application may free this structure after
 *                         the function returns.
 * @return    Error or success status returned by API
 * @implements Lpi2c_Ip_MasterInit_Activity
 */
Lpi2c_Ip_StatusType Lpi2c_Ip_MasterInit(uint32 instance,
                                    const Lpi2c_Ip_MasterConfigType * userConfigPtr);


/**
 * @brief De-initialize the LPI2C master mode driver
 *
 * @details This function de-initializes the LPI2C driver in master mode. The driver can't be used
 * again until reinitialized. The context structure is no longer needed by the driver and
 * can be freed after calling this function.
 *
 * @param[in] instance  LPI2C peripheral instance number
 * @return    Error or success status returned by API
 * @implements Lpi2c_Ip_MasterDeinit_Activity
 */
Lpi2c_Ip_StatusType Lpi2c_Ip_MasterDeinit(uint32 instance);


/**
 * @brief Get the currently configured baud rate
 *
 * @details This function returns the currently configured baud rate.
 *
 * @param[in] instance   LPI2C peripheral instance number
 * @param[in] inputClock input clock in Hz
 * @param[out] baudRate  structure that contains the current baud rate in hertz
 *                  and the baud rate in hertz for High-speed mode (unused
 *                  in other modes, can be NULL)
 * @implements Lpi2c_Ip_MasterGetBaudRate_Activity
 */
void Lpi2c_Ip_MasterGetBaudRate(uint32 instance, uint32 inputClock, uint32 *baudRate);


/**
 * @brief Set the baud rate for any subsequent I2C communication
 *
 * @details This function sets the baud rate (SCL frequency) for the I2C master. It can also
 * change the operating mode.
 * Note that due to module limitation not any baud rate can be achieved. The driver
 * will set a baud rate as close as possible to the requested baud rate, but there may
 * still be substantial differences, for example if requesting a high baud rate while
 * using a low-frequency protocol clock for the LPI2C module. The application should
 * call Lpi2c_Ip_MasterGetBaudRate() after Lpi2c_Ip_MasterSetBaudRate() to check
 * what baud rate was actually set.
 *
 * @param[in] instance  LPI2C peripheral instance number
 * @param[in] operatingMode  I2C operating mode
 * @param[in] baudRate  structure that contains the baud rate in hertz to use by current slave device
 *                  and also the baud rate in hertz for High-speed mode (unused in other modes)
 * @param[in] inputClock input clock in Hz
 * @return    Error or success status returned by API
 * @implements Lpi2c_Ip_MasterSetBaudRate_Activity
 */
Lpi2c_Ip_StatusType Lpi2c_Ip_MasterSetBaudRate(uint32 instance,
                                                Lpi2c_Ip_ModeType operatingMode,
                                                uint32 baudrate,
                                                uint32 inputClock);


/**
 * @brief Set the slave address for any subsequent I2C communication
 *
 * @details This function sets the slave address which will be used for any future
 * transfer initiated by the LPI2C master.
 *
 * @param[in] instance  LPI2C peripheral instance number
 * @param[in] address   slave address, 7-bit or 10-bit
 * @param[in] is10bitAddr   specifies if provided address is 10-bit
 * @implements Lpi2c_Ip_MasterSetSlaveAddr_Activity
 */
void Lpi2c_Ip_MasterSetSlaveAddr(uint32 instance, const uint16 address, const boolean is10bitAddr);


/**
 * @brief Perform a non-blocking send transaction on the I2C bus
 *
 * @details This function starts the transmission of a block of data to the currently
 * configured slave address and returns immediately.
 * The rest of the transmission is handled by the interrupt service routine.
 * Use Lpi2c_Ip_MasterGetSendStatus() to check the progress of the transmission.
 *
 * @param[in] instance  LPI2C peripheral instance number
 * @param[in] txBuff    pointer to the data to be transferred
 * @param[in] txSize    length in bytes of the data to be transferred
 * @param[in] sendStop    specifies whether or not to generate stop condition after the transmission
 * @return    Error or success status returned by API
 * @implements Lpi2c_Ip_MasterSendData_Activity
 */
Lpi2c_Ip_StatusType Lpi2c_Ip_MasterSendData(uint32 instance,
                                            uint8 * txBuff,
                                            uint32 txSize,
                                            boolean sendStop);


/**
 * @brief Perform a blocking send transaction on the I2C bus
 *
 * @details This function sends a block of data to the currently configured slave address, and
 * only returns when the transmission is complete.
 *
 * @param[in] instance  LPI2C peripheral instance number
 * @param[in] txBuff    pointer to the data to be transferred
 * @param[in] txSize    length in bytes of the data to be transferred
 * @param[in] sendStop    specifies whether or not to generate stop condition after the transmission
 * @param[in] timeout   timeout for the transfer in milliseconds
 * @return    Error or success status returned by API
 * @implements Lpi2c_Ip_MasterSendDataBlocking_Activity
 */
Lpi2c_Ip_StatusType Lpi2c_Ip_MasterSendDataBlocking(uint32 instance,
                                    uint8 * txBuff,
                                    uint32 txSize,
                                    boolean sendStop,
                                    uint32 timeout);


/**
 * @brief Perform a non-blocking receive transaction on the I2C bus
 *
 * @details This function starts the reception of a block of data from the currently
 * configured slave address and returns immediately.
 * The rest of the reception is handled by the interrupt service routine.
 * Use Lpi2c_Ip_MasterGetReceiveStatus() to check the progress of the reception.
 *
 * @param[in] instance  LPI2C peripheral instance number
 * @param[out] rxBuff    pointer to the buffer where to store received data
 * @param[in] rxSize    length in bytes of the data to be transferred
 * @param[in] sendStop    specifies whether or not to generate stop condition after the reception
 * @return    Error or success status returned by API
 * @implements Lpi2c_Ip_MasterReceiveData_Activity
 */
Lpi2c_Ip_StatusType Lpi2c_Ip_MasterReceiveData(uint32  instance,
                                               uint8 * rxBuff,
                                               uint32 rxSize,
                                               boolean sendStop);


/**
 * @brief Perform a blocking receive transaction on the I2C bus
 *
 * @details This function receives a block of data from the currently configured slave address,
 * and only returns when the transmission is complete.
 *
 * @param[in] instance  LPI2C peripheral instance number
 * @param[out] rxBuff    pointer to the buffer where to store received data
 * @param[in] rxSize    length in bytes of the data to be transferred
 * @param[in] sendStop    specifies whether or not to generate stop condition after the reception
 * @param[in] timeout   timeout for the transfer in milliseconds
 * @return    Error or success status returned by API
 * @implements Lpi2c_Ip_MasterReceiveDataBlocking_Activity
 */
Lpi2c_Ip_StatusType Lpi2c_Ip_MasterReceiveDataBlocking(uint32 instance,
                                       uint8 * rxBuff,
                                       uint32 rxSize,
                                       boolean sendStop,
                                       uint32 timeout);


/**
 * @brief Return the current status of the I2C master transfer
 *
 * @details This function can be called during a non-blocking transmission to check the
 * status of the transfer.
 *
 * @param[in] instance  LPI2C peripheral instance number
 * @param[out] bytesRemaining   the number of remaining bytes in the active I2C transfer
 * @return    Error or success status returned by API
 * @implements Lpi2c_Ip_MasterGetTransferStatus_Activity
 */
Lpi2c_Ip_StatusType Lpi2c_Ip_MasterGetTransferStatus(uint32 instance, uint32 *bytesRemaining);


/**
 * @brief Handle master operation when I2C interrupt occurs
 *
 * @details This is the interrupt service routine for the LPI2C master mode driver. It
 * handles the rest of the transfer started by one of the send/receive functions.
 *
 * @param[in] instance  LPI2C peripheral instance number
 */
void Lpi2c_Ip_MasterIRQHandler(uint32 instance);


/**
 * @brief Initialize the I2C slave mode driver
 *
 * @param[in] instance  LPI2C peripheral instance number
 * @param[in] userConfigPtr    Pointer to the LPI2C slave user configuration structure. The function
 *                         reads configuration data from this structure and initializes the
 *                         driver accordingly. The application may free this structure after
 *                         the function returns.
 * @return    Error or success status returned by API
 * @implements Lpi2c_Ip_SlaveInit_Activity
 */
Lpi2c_Ip_StatusType Lpi2c_Ip_SlaveInit(uint32 instance,
                               const Lpi2c_Ip_SlaveConfigType * userConfigPtr);


/**
 * @brief De-initialize the I2C slave mode driver
 *
 * @details This function de-initializes the LPI2C driver in slave mode. The driver can't be used
 * again until reinitialized. The context structure is no longer needed by the driver and
 * can be freed after calling this function.
 *
 * @param[in] instance  LPI2C peripheral instance number
 * @return    Error or success status returned by API
 * @implements Lpi2c_Ip_SlaveDeinit_Activity
 */
Lpi2c_Ip_StatusType Lpi2c_Ip_SlaveDeinit(uint32 instance);


/**
 * @brief Provide a buffer for transmitting data
 *
 * @details This function provides a buffer from which the LPI2C slave-mode driver can
 * transmit data. It can be called for example from the user callback provided at
 * initialization time, when the driver reports events LPI2C_SLAVE_EVENT_TX_REQ or
 * LPI2C_SLAVE_EVENT_TX_EMPTY.
 *
 * @param[in] instance  LPI2C peripheral instance number
 * @param[in] txBuff    pointer to the data to be transferred
 * @param[in] txSize    length in bytes of the data to be transferred
 * @return    Error or success status returned by API
 * @implements Lpi2c_Ip_SlaveSetBuffer_Activity
 */
Lpi2c_Ip_StatusType Lpi2c_Ip_SlaveSetBuffer(uint32 instance,
                                            uint8 * dataBuff,
                                            uint32 dataSize);


/**
 * @brief Return the current status of the I2C slave transfer
 *
 * @details This function can be called during a non-blocking transmission to check the
 * status of the transfer.
 *
 * @param[in] instance  LPI2C peripheral instance number
 * @param[in] bytesRemaining[out]   the number of remaining bytes in the active I2C transfer
 * @return    Error or success status returned by API
 * @implements Lpi2c_Ip_SlaveGetTransferStatus_Activity
 */
Lpi2c_Ip_StatusType Lpi2c_Ip_SlaveGetTransferStatus(uint32 instance,
                                            uint32 *bytesRemaining);


/**
 * @brief Handle slave operation when I2C interrupt occurs
 *
 * @details This is the interrupt service routine for the LPI2C slave mode driver. It
 * handles any transfer initiated by an I2C master and notifies the application
 * via the provided callback when relevant events occur.
 *
 * @param[in] instance  LPI2C peripheral instance number
 *
 * @return void
 */
void Lpi2c_Ip_SlaveIRQHandler(uint32 instance);

/**
 * @brief Handler for both slave and master operation when I2C interrupt occurs
 *
 * @details This is the interrupt service routine for the LPI2C slave and master mode driver. It
 * handles any transfer initiated by an I2C master and notifies the application
 * via the provided callback when relevant events occur.
 *
 * @param[in] instance  LPI2C peripheral instance number
 *
 * @return void
 */
void Lpi2c_Ip_ModuleIRQHandler(uint32 instance);

/**
 * @brief Sets the master callback
 *
 * @details This functions sets the master callback
 *
 * @param[in] u32Instance  LPI2C peripheral instance number
 * @param[in] masterCallback  master callback to be set 
 *
 * @return void
 */
/* @implements Lpi2c_Ip_SetMasterCallback_Activity */
void Lpi2c_Ip_SetMasterCallback(uint32 u32Instance, Lpi2c_Ip_MasterCallbackType masterCallback);

/**
 * @brief Sets the slave callback
 *
 * @details This functions sets the slave callback
 *
 * @param[in] u32Instance  LPI2C peripheral instance number
 * @param[in] slaveCallback  slave callback to be set 
 *
 * @return void
 */
/* @implements Lpi2c_Ip_SetSlaveCallback_Activity */
void Lpi2c_Ip_SetSlaveCallback(uint32 u32Instance, Lpi2c_Ip_SlaveCallbackType slaveCallback);

/**
 * @brief Start listening 
 *
 * @details This is used to enable slave events 
 *
 * @param[in] u32Instance  LPI2C peripheral instance number
 * @return void
 */
/* @implements Lpi2c_Ip_StartListening_Activity */
void Lpi2c_Ip_StartListening(uint32 u32Instance);

/**
 * @brief Set high speed mode for master 
 *
 * @details This function enables high speed mode for master
 *
 * @param[in] u32Instance  LPI2C peripheral instance number
 * @param[in] bHighSpeedEnabled  enables/disables master high speed mode 
 * @return void
 */
 /* @implements Lpi2c_Ip_SetMasterHighSpeedMode_Activity */
void Lpi2c_Ip_SetMasterHighSpeedMode(uint32 u32Instance, boolean bHighSpeedEnabled);

#if(STD_ON == LPI2C_IP_DMA_FEATURE_AVAILABLE)

#if (LPI2C_INSTANCE_COUNT >= 1U)
void LPI2C0_DmaCompleteNotification(void);
#endif /* LPI2C_INSTANCE_COUNT >= 1U */

#if (LPI2C_INSTANCE_COUNT >= 2U)
void LPI2C1_DmaCompleteNotification(void);
#endif /* LPI2C_INSTANCE_COUNT >= 2U */
#endif

#define I2C_STOP_SEC_CODE
#include "I2c_MemMap.h"

#if defined(__cplusplus)
}
#endif

/** @}*/

#endif /* LPI2C_IP_H */
/*! @}*/
/*******************************************************************************
 * EOF
 ******************************************************************************/
