/*==================================================================================================
*   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.
==================================================================================================*/


#ifndef LPI2C_IP_TYPES_H_
#define LPI2C_IP_TYPES_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 "Lpi2c_Ip_Features.h"

/*==================================================================================================
*                               SOURCE FILE VERSION INFORMATION
==================================================================================================*/

#define LPI2C_IP_TYPES_VENDOR_ID                      43   
#define LPI2C_IP_TYPES_AR_RELEASE_MAJOR_VERSION       4
#define LPI2C_IP_TYPES_AR_RELEASE_MINOR_VERSION       4
#define LPI2C_IP_TYPES_AR_RELEASE_REVISION_VERSION    0
#define LPI2C_IP_TYPES_SW_MAJOR_VERSION               0
#define LPI2C_IP_TYPES_SW_MINOR_VERSION               9
#define LPI2C_IP_TYPES_SW_PATCH_VERSION               0

/*==================================================================================================
*                                     FILE VERSION CHECKS
==================================================================================================*/
/* Checks against I2c_Ip_Callbacks.h */
#if (LPI2C_IP_TYPES_VENDOR_ID != LPI2C_IP_CALLBACKS_VENDOR_ID)
    #error "LPI2C_Ip.h and Lpi2c_Ip_Callbacks.h have different vendor ids"
#endif
#if (( LPI2C_IP_TYPES_AR_RELEASE_MAJOR_VERSION    != LPI2C_IP_CALLBACKS_AR_RELEASE_MAJOR_VERSION) || \
     ( LPI2C_IP_TYPES_AR_RELEASE_MINOR_VERSION    != LPI2C_IP_CALLBACKS_AR_RELEASE_MINOR_VERSION) || \
     ( LPI2C_IP_TYPES_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_TYPES_SW_MAJOR_VERSION != LPI2C_IP_CALLBACKS_SW_MAJOR_VERSION) || \
     ( LPI2C_IP_TYPES_SW_MINOR_VERSION != LPI2C_IP_CALLBACKS_SW_MINOR_VERSION) || \
     ( LPI2C_IP_TYPES_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_Features.h */
#if (LPI2C_IP_TYPES_VENDOR_ID !=  LPI2C_IP_FEATURES_VENDOR_ID)
    #error "Lpi2c_Ip.c and Lpi2c_Ip_Features.h have different vendor ids"
#endif
#if (( LPI2C_IP_TYPES_AR_RELEASE_MAJOR_VERSION    !=  LPI2C_IP_FEATURES_AR_RELEASE_MAJOR_VERSION) || \
     ( LPI2C_IP_TYPES_AR_RELEASE_MINOR_VERSION    !=  LPI2C_IP_FEATURES_AR_RELEASE_MINOR_VERSION) || \
     ( LPI2C_IP_TYPES_AR_RELEASE_REVISION_VERSION !=  LPI2C_IP_FEATURES_AR_RELEASE_REVISION_VERSION))
     #error "AUTOSAR Version Numbers of Lpi2c_Ip.c and Lpi2c_Ip_Features.h are different"
#endif
#if (( LPI2C_IP_TYPES_SW_MAJOR_VERSION !=  LPI2C_IP_FEATURES_SW_MAJOR_VERSION) || \
     ( LPI2C_IP_TYPES_SW_MINOR_VERSION !=  LPI2C_IP_FEATURES_SW_MINOR_VERSION) || \
     ( LPI2C_IP_TYPES_SW_PATCH_VERSION !=  LPI2C_IP_FEATURES_SW_PATCH_VERSION))
    #error "Software Version Numbers of LPI2C_Ip.c and Lpi2c_Ip_Features.h are different"
#endif

/** @cond DRIVER_INTERNAL_USE_ONLY */
/* Size of the master command queue. Worst case: 5 commands in High-Speed receive with 10-bit address:
   START + master code, REP START + addr_1 + tx, addr_2, REP START + addr_1 + rx, receive command */
#define LPI2C_MASTER_CMD_QUEUE_SIZE   5U
/** @endcond */

/** @brief I2C operating modes
 * @implements Lpi2c_Ip_ModeType_enum
 */
typedef enum
{
    LPI2C_STANDARD_MODE      = 0x0U,   /**< Standard-mode (Sm), bidirectional data transfers up to 100 kbit/s */
    LPI2C_FAST_MODE          = 0x1U,   /**< Fast-mode (Fm), bidirectional data transfers up to 400 kbit/s */
    LPI2C_FASTPLUS_MODE      = 0x2U,   /**< Fast-mode Plus (Fm+), bidirectional data transfers up to 1 Mbit/s */
    LPI2C_HIGHSPEED_MODE     = 0x3U,   /**< High-speed Mode (Hs-mode), bidirectional data transfers up to 3.4 Mbit/s */
#if(LPI2C_HAS_ULTRA_FAST_MODE)
    LPI2C_ULTRAFAST_MODE     = 0x4U    /**< Ultra Fast Mode (UFm), unidirectional data transfers up to 5 Mbit/s */
#endif
} Lpi2c_Ip_ModeType;

/** @brief Type of LPI2C transfer (based on interrupts or DMA).
 * @implements Lpi2c_Ip_AsyncTransferType_enum
 */
typedef enum
{
   LPI2C_USING_DMA         = 0,    /**< The driver will use DMA to perform I2C transfer */
   LPI2C_USING_INTERRUPTS  = 1,    /**< The driver will use interrupts to perform I2C transfer */
} Lpi2c_Ip_AsyncTransferType;

/** @brief Type of LPI2C transfer (based on interrupts or DMA).
 */
typedef enum
{
    /* I2C specific error codes */
    STATUS_LPI2C_IP_SUCCESS                     = 0x000U,
    STATUS_LPI2C_IP_ERROR                       = 0x001U,
    STATUS_LPI2C_IP_BUSY                        = 0x002U,
    STATUS_LPI2C_IP_RECEIVED_NACK               = 0x003U,  /**< NACK signal received  */
    STATUS_LPI2C_IP_TX_UNDERRUN                 = 0x004U,  /**< TX underrun error */
    STATUS_LPI2C_IP_RX_OVERRUN                  = 0x005U,  /**< RX overrun error */
    STATUS_LPI2C_IP_ARBITRATION_LOST            = 0x006U,  /**< Arbitration lost */
    STATUS_LPI2C_IP_ABORTED                     = 0x007U,  /**< A transfer was aborted */
    STATUS_LPI2C_IP_BUS_BUSY                    = 0x008U,  /**< I2C bus is busy, cannot start transfer */
    STATUS_LPI2C_IP_TIMEOUT                     = 0x009U,
    STATUS_LPI2C_IP_UNSUPPORTED                 = 0x00AU
}Lpi2c_Ip_StatusType;

/*******************************************************************************
* Definitions
******************************************************************************/

/**
 * @brief Defines the example structure
 *
 * This structure is used as an example.
 */

 /** @brief LPI2C master prescaler options
 */
typedef enum
{
    LPI2C_MASTER_PRESC_DIV_1    = 0U,  /**< Divide by 1   */
    LPI2C_MASTER_PRESC_DIV_2    = 1U,  /**< Divide by 2   */
    LPI2C_MASTER_PRESC_DIV_4    = 2U,  /**< Divide by 4   */
    LPI2C_MASTER_PRESC_DIV_8    = 3U,  /**< Divide by 8   */
    LPI2C_MASTER_PRESC_DIV_16   = 4U,  /**< Divide by 16  */
    LPI2C_MASTER_PRESC_DIV_32   = 5U,  /**< Divide by 32  */
    LPI2C_MASTER_PRESC_DIV_64   = 6U,  /**< Divide by 64  */
    LPI2C_MASTER_PRESC_DIV_128  = 7U,  /**< Divide by 128 */
} Lpi2c_Ip_MasterPrescalerType;
 
 
/**
 * @brief Baud rate structure
 *
 * This structure is used for setting or getting the baud rate.
 * @implements Lpi2c_Ip_BaudRateType_struct
 */
typedef struct
{
    Lpi2c_Ip_MasterPrescalerType prescaler;
    uint32 clkHI;
    uint32 clkLO;
    uint32 setHold;
    uint32 dataValid;
    uint32 clkHIHS;
    uint32 clkLOHS;
    uint32 setHoldHS;
    uint32 dataValidHS;
} Lpi2c_Ip_BaudRateType;

/** @cond DRIVER_INTERNAL_USE_ONLY */
/* LPI2C master commands */
typedef enum
{
    LPI2C_MASTER_COMMAND_TRANSMIT         = 0U,  /**< Transmit DATA[7:0] */
    LPI2C_MASTER_COMMAND_RECEIVE          = 1U,  /**< Receive (DATA[7:0] + 1) bytes */
    LPI2C_MASTER_COMMAND_STOP             = 2U,  /**< Generate STOP condition */
    LPI2C_MASTER_COMMAND_RECEIVE_DISCARD  = 3U,  /**< Receive and discard (DATA[7:0] + 1) bytes */
    LPI2C_MASTER_COMMAND_START            = 4U,  /**< Generate START and transmit address in DATA[7:0] */
    LPI2C_MASTER_COMMAND_START_NACK       = 5U,  /**< Generate START and transmit address in DATA[7:0], expect a NACK to be returned */
    LPI2C_MASTER_COMMAND_START_HS         = 6U,  /**< Generate START and transmit address in DATA[7:0] in high speed mode */
    LPI2C_MASTER_COMMAND_START_NACK_HS    = 7U,  /**< Generate START and transmit address in DATA[7:0] in high speed mode, expect a NACK to be returned */
} Lpi2c_Ip_MasterCommandType;
/** @endcond */

/** @cond DRIVER_INTERNAL_USE_ONLY */
/* Master software command queue */
typedef struct
{
    Lpi2c_Ip_MasterCommandType cmd[LPI2C_MASTER_CMD_QUEUE_SIZE];
    uint8 data[LPI2C_MASTER_CMD_QUEUE_SIZE];
    uint8 writeIdx;
    uint8 readIdx;
}Lpi2c_Ip_MasterCmdQueueType;
/** @endcond */

typedef enum
{
    LPI2C_IP_SEND = 0x00U,    /* Send operation */
    LPI2C_IP_RECEIVE = 0x01U  /* Receive operation */
}Lpi2c_Ip_DirectionType;


/**
 * @brief Master internal context structure
 *
 * This structure is used by the master-mode driver for its internal logic. It must
 * be provided by the application through the LPI2C_DRV_MasterInit() function, then
 * it cannot be freed until the driver is de-initialized using LPI2C_DRV_MasterDeinit().
 * The application should make no assumptions about the content of this structure.
 */
typedef struct
{
/** @cond DRIVER_INTERNAL_USE_ONLY */
    Lpi2c_Ip_MasterCmdQueueType cmdQueue;            /* Software queue for commands, when LPI2C FIFO is not big enough */
    uint32 bufferSize;                               /* Size of tx/rx buffer */
    uint8 * dataBuffer;                              /* Pointer to data buffer */
    Lpi2c_Ip_DirectionType direction;                /* Specifies the direction of the transfer, send or receive */
    volatile Lpi2c_Ip_StatusType status;             /* Status of last driver operation */
    Lpi2c_Ip_ModeType operatingMode;                 /* I2C Operating mode */
    uint16 slaveAddress;                             /* Slave address */
    volatile boolean i2cIdle;                        /* Idle/busy state of the driver */                      
    uint8 masterCode;                                /* Master code for High-speed mode */
    boolean highSpeedInProgress;                     /* High-speed communication is in progress */                                             
    boolean sendStop;                                /* Specifies if STOP condition must be generated after current transfer */
    boolean is10bitAddr;                             /* Selects 7-bit or 10-bit slave address */
    Lpi2c_Ip_AsyncTransferType transferType;         /* Type of LPI2C transfer */
    uint32 dmaTxChannel;                              /* Channel number for DMA Tx channel */
    uint32 dmaRxChannel;                              /* Channel number for DMA Rx channel */
    Lpi2c_Ip_MasterCallbackType masterCallback;        /* Master callback function */
    uint8 callbackParam;                             /* Parameter for the master callback function */
    const Lpi2c_Ip_BaudRateType * baudrateParams;  /* Baud rate in Hz*/
/** @endcond */
} Lpi2c_Ip_MasterStateType;


 /**
 * @brief Master configuration structure
 *
 * This structure is used to provide configuration parameters for the LPI2C master at initialization time.
 * @implements Lpi2c_Ip_MasterConfigType_struct
 */
typedef struct
{
    uint16 slaveAddress;                             /**< Slave address, 7-bit or 10-bit */
    boolean is10bitAddr;                             /**< Selects 7-bit or 10-bit slave address */
    Lpi2c_Ip_ModeType operatingMode;                 /**< I2C Operating mode */
    const Lpi2c_Ip_BaudRateType * baudrateParams;  /**< Baud rate in Hz*/
    uint32 u32PinLowTimeout;                         /**< Pin Low Timeout */
    uint32 u32BusIdleTimeout;                        /**< Bus Idle Timeout */
    uint32 u32GlitchFilterSDA;                       /**< SDA glitch filter */
    uint32 u32GlitchFilterSCL;                       /**< SCL glitch filter */                      
    uint8 masterCode;                                /**< Master code for High-speed mode. Valid range: 0-7. Unused in other operating modes */
    Lpi2c_Ip_AsyncTransferType transferType;         /**< Type of LPI2C transfer */
    uint32 dmaTxChannel;                              /**< Channel number for DMA Tx channel. If DMA mode isn't used this field will be ignored. */
    uint32 dmaRxChannel;                              /**< Channel number for DMA Rx channel. If DMA mode isn't used this field will be ignored. */
    Lpi2c_Ip_MasterCallbackType masterCallback;        /**< Master callback function. Note that this function will be
                                                     called from the interrupt service routine at the end of a transfer,
                                                     so its execution time should be as small as possible. It can be
                                                     NULL if you want to check manually the status of the transfer. */
    uint8 callbackParam;                             /**< Parameter for the master callback function */

    Lpi2c_Ip_MasterStateType * masterState;          /**< Pointer to master state */
} Lpi2c_Ip_MasterConfigType;

/**
 * @brief Slave internal context structure
 *
 * This structure is used by the slave-mode driver for its internal logic. It must
 * be provided by the application through the LPI2C_DRV_SlaveInit() function, then
 * it cannot be freed until the driver is de-initialized using LPI2C_DRV_SlaveDeinit().
 * The application should make no assumptions about the content of this structure.
 */
typedef struct
{
/** @cond DRIVER_INTERNAL_USE_ONLY */
    Lpi2c_Ip_StatusType status;                  /* The I2C slave status */
    volatile boolean isTransferInProgress;     /* Slave is busy because of an ongoing transfer */
    uint32 bufferSize;                      /* Size of tx/rx buffer */
    uint8 * dataBuffer;                     /* Pointer to data buffer */
    Lpi2c_Ip_ModeType operatingMode;             /* I2C Operating mode */
    boolean is10bitAddress;                    /* Specifies if 10-bit or 7-bit address */
    uint8 repeatedStarts;                 /* Specifies the number of repeated starts */
    boolean slaveListening;                 /* Specifies if slave is in listening mode */
    boolean txUnderrunWarning;                 /* Possible slave tx underrun */
    Lpi2c_Ip_AsyncTransferType transferType;     /* Type of LPI2C transfer */
    uint32 dmaTxChannel;                   /* Channel number for TX DMA channel */                  /* Channel number for DMA tx channel */
    uint32 dmaRxChannel;                   /* Channel number for RX DMA channel */                  /* Channel number for DMA tx channel */
    Lpi2c_Ip_SlaveCallbackType slaveCallback;   /* Slave callback function */
    uint8 callbackParam;                    /* Parameter for the slave callback function */
    Lpi2c_Ip_DirectionType direction;                         /* Specifies the direction of the transfer, send or receive */
/** @endcond */
} Lpi2c_Ip_SlaveStateType;


/**
 * @brief Slave configuration structure
 *
 * This structure is used to provide configuration parameters for the LPI2C slave at initialization time.
 * @implements Lpi2c_Ip_SlaveConfigType_struct
 */
typedef struct
{
    uint16 slaveAddress;                      /**< Slave address, 7-bit or 10-bit */
    boolean is10bitAddr;                      /**< Selects 7-bit or 10-bit slave address */
    boolean slaveListening;                   /**<Specifies if slave is in listening mode */
    Lpi2c_Ip_ModeType operatingMode;          /**< I2C Operating mode */
    Lpi2c_Ip_AsyncTransferType transferType;  /**< Type of LPI2C transfer */
    uint32 u32GlitchFilterSDA;                /**< SDA glitch filter */
    uint32 u32GlitchFilterSCL;                /**< SCL glitch filter */
    uint32 dmaTxChannel;                         /**< Channel number for DMA tx channel. If DMA mode isn't used this field will be ignored. */
    uint32 dmaRxChannel;                         /**< Channel number for DMA rx channel. If DMA mode isn't used this field will be ignored. */
    Lpi2c_Ip_SlaveCallbackType slaveCallback;       /**< Slave callback function. Note that this function will be
                                                  called from the interrupt service routine, so its
                                                  execution time should be as small as possible. It can be
                                                  NULL if the slave is not in listening mode
                                                  (slaveListening = false) */
    uint8 callbackParam;                      /**< Parameter for the slave callback function */
    Lpi2c_Ip_SlaveStateType * slaveState;     /**< Pointer slave state */
} Lpi2c_Ip_SlaveConfigType;


#endif /* LPI2C_IP_TYPES_H_ */

/** @}*/

/*******************************************************************************
 * EOF
 ******************************************************************************/
