/*==================================================================================================
*   Project              : RTD AUTOSAR 4.4
*   Platform             : CORTEXM
*   Peripheral           : LPUART
*   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 LPUART_LIN_IP_H
#define LPUART_LIN_IP_H

/**
*   @file
*   @defgroup lpuart_lin_ip Lpuart Lin IPL
*   @addtogroup  lpuart_lin_ip Lpuart Lin IPL
*   @{
*/

#ifdef __cplusplus
extern "C"{
#endif

/*==================================================================================================
*                                          INCLUDE FILES
* 1) system and project includes
* 2) needed interfaces from external units
* 3) internal and external interfaces from this unit
==================================================================================================*/

#include "Lpuart_Lin_Ip_Hw_Access.h"
#include "Lpuart_Lin_Ip_Cfg.h"
#include "Lin_Ip_Common.h"
#include "Lpuart_Lin_Ip_Types.h"

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

#define LPUART_LIN_IP_VENDOR_ID                      43
#define LPUART_LIN_IP_AR_RELEASE_MAJOR_VERSION       4
#define LPUART_LIN_IP_AR_RELEASE_MINOR_VERSION       4
#define LPUART_LIN_IP_AR_RELEASE_REVISION_VERSION    0
#define LPUART_LIN_IP_SW_MAJOR_VERSION               0
#define LPUART_LIN_IP_SW_MINOR_VERSION               9
#define LPUART_LIN_IP_SW_PATCH_VERSION               0

/*==================================================================================================
*                                       FILE VERSION CHECKS
==================================================================================================*/
/* Checks against Lpuart_Lin_Ip_Hw_Access.h */
#if (LPUART_LIN_IP_VENDOR_ID != LPUART_LIN_IP_HW_ACCESS_VENDOR_ID)
    #error "Lpuart_Lin_Ip.h and Lpuart_Lin_Ip_Hw_Access.h have different vendor ids"
#endif
/* Check if current file and Lpuart_Lin_Ip_Hw_Access header file are of the same Autosar version */
#if ((LPUART_LIN_IP_AR_RELEASE_MAJOR_VERSION    != LPUART_LIN_IP_HW_ACCESS_AR_RELEASE_MAJOR_VERSION) || \
     (LPUART_LIN_IP_AR_RELEASE_MINOR_VERSION    != LPUART_LIN_IP_HW_ACCESS_AR_RELEASE_MINOR_VERSION) || \
     (LPUART_LIN_IP_AR_RELEASE_REVISION_VERSION != LPUART_LIN_IP_HW_ACCESS_AR_RELEASE_REVISION_VERSION))
    #error "AUTOSAR Version Numbers of Lpuart_Lin_Ip.h and Lpuart_Lin_Ip_Hw_Access.h are different"
#endif
/* Check if current file and Lpuart_Lin_Ip_Hw_Access header file are of the same Software version */
#if ((LPUART_LIN_IP_SW_MAJOR_VERSION != LPUART_LIN_IP_HW_ACCESS_SW_MAJOR_VERSION) || \
     (LPUART_LIN_IP_SW_MINOR_VERSION != LPUART_LIN_IP_HW_ACCESS_SW_MINOR_VERSION) || \
     (LPUART_LIN_IP_SW_PATCH_VERSION != LPUART_LIN_IP_HW_ACCESS_SW_PATCH_VERSION) \
    )
    #error "Software Version Numbers of Lpuart_Lin_Ip.h and Lpuart_Lin_Ip_Hw_Access.h are different"
#endif

/* Checks against Lpuart_Lin_Ip_Cfg.h */
#if (LPUART_LIN_IP_VENDOR_ID != LPUART_LIN_IP_CFG_VENDOR_ID)
    #error "Lpuart_Lin_Ip.h and Lpuart_Lin_Ip_Cfg.h have different vendor ids"
#endif
/* Check if current file and Lpuart_Lin_Ip_Cfg header file are of the same Autosar version */
#if ((LPUART_LIN_IP_AR_RELEASE_MAJOR_VERSION    != LPUART_LIN_IP_CFG_AR_RELEASE_MAJOR_VERSION) || \
     (LPUART_LIN_IP_AR_RELEASE_MINOR_VERSION    != LPUART_LIN_IP_CFG_AR_RELEASE_MINOR_VERSION) || \
     (LPUART_LIN_IP_AR_RELEASE_REVISION_VERSION != LPUART_LIN_IP_CFG_AR_RELEASE_REVISION_VERSION))
    #error "AUTOSAR Version Numbers of Lpuart_Lin_Ip.h and Lpuart_Lin_Ip_Cfg.h are different"
#endif
/* Check if current file and Lpuart_Lin_Ip_Cfg header file are of the same Software version */
#if ((LPUART_LIN_IP_SW_MAJOR_VERSION != LPUART_LIN_IP_CFG_SW_MAJOR_VERSION) || \
     (LPUART_LIN_IP_SW_MINOR_VERSION != LPUART_LIN_IP_CFG_SW_MINOR_VERSION) || \
     (LPUART_LIN_IP_SW_PATCH_VERSION != LPUART_LIN_IP_CFG_SW_PATCH_VERSION) \
    )
    #error "Software Version Numbers of Lpuart_Lin_Ip.h and Lpuart_Lin_Ip_Cfg.h are different"
#endif

/* Checks against Lin_Ip_Common.h */
#if (LPUART_LIN_IP_VENDOR_ID != LIN_IP_COMMON_VENDOR_ID)
    #error "Lpuart_Lin_Ip.h and Lin_Ip_Common.h have different vendor ids"
#endif
/* Check if current file and Lin_Ip_Common header file are of the same Autosar version */
#if ((LPUART_LIN_IP_AR_RELEASE_MAJOR_VERSION    != LIN_IP_COMMON_AR_RELEASE_MAJOR_VERSION) || \
     (LPUART_LIN_IP_AR_RELEASE_MINOR_VERSION    != LIN_IP_COMMON_AR_RELEASE_MINOR_VERSION) || \
     (LPUART_LIN_IP_AR_RELEASE_REVISION_VERSION != LIN_IP_COMMON_AR_RELEASE_REVISION_VERSION))
    #error "AUTOSAR Version Numbers of Lpuart_Lin_Ip.h and Lin_Ip_Common.h are different"
#endif
/* Check if current file and Lin_Ip_Common header file are of the same Software version */
#if ((LPUART_LIN_IP_SW_MAJOR_VERSION != LIN_IP_COMMON_SW_MAJOR_VERSION) || \
     (LPUART_LIN_IP_SW_MINOR_VERSION != LIN_IP_COMMON_SW_MINOR_VERSION) || \
     (LPUART_LIN_IP_SW_PATCH_VERSION != LIN_IP_COMMON_SW_PATCH_VERSION) \
    )
    #error "Software Version Numbers of Lpuart_Lin_Ip.h and Lin_Ip_Common.h are different"
#endif

/* Checks against Lpuart_Lin_Ip_Types.h */
#if (LPUART_LIN_IP_VENDOR_ID != LPUART_LIN_IP_TYPES_VENDOR_ID)
    #error "Lpuart_Lin_Ip.h and Lpuart_Lin_Ip_Types.h have different vendor ids"
#endif
/* Check if current file and Lpuart_Lin_Ip_Types header file are of the same Autosar version */
#if ((LPUART_LIN_IP_AR_RELEASE_MAJOR_VERSION    != LPUART_LIN_IP_TYPES_AR_RELEASE_MAJOR_VERSION) || \
     (LPUART_LIN_IP_AR_RELEASE_MINOR_VERSION    != LPUART_LIN_IP_TYPES_AR_RELEASE_MINOR_VERSION) || \
     (LPUART_LIN_IP_AR_RELEASE_REVISION_VERSION != LPUART_LIN_IP_TYPES_AR_RELEASE_REVISION_VERSION))
    #error "AUTOSAR Version Numbers of Lpuart_Lin_Ip.h and Lpuart_Lin_Ip_Types.h are different"
#endif
/* Check if current file and Lpuart_Lin_Ip_Types header file are of the same Software version */
#if ((LPUART_LIN_IP_SW_MAJOR_VERSION != LPUART_LIN_IP_TYPES_SW_MAJOR_VERSION) || \
     (LPUART_LIN_IP_SW_MINOR_VERSION != LPUART_LIN_IP_TYPES_SW_MINOR_VERSION) || \
     (LPUART_LIN_IP_SW_PATCH_VERSION != LPUART_LIN_IP_TYPES_SW_PATCH_VERSION) \
    )
    #error "Software Version Numbers of Lpuart_Lin_Ip.h and Lpuart_Lin_Ip_Types.h are different"
#endif

/*==================================================================================================
*                                            CONSTANTS
==================================================================================================*/
#define LIN_START_SEC_CONFIG_DATA_UNSPECIFIED
#include "Lin_MemMap.h"

/* Calling the external Configuration symbols defined by Lpuart_Lin_Ip_Cfg.h */
LPUART_LIN_IP_CONFIG_EXT

#define LIN_STOP_SEC_CONFIG_DATA_UNSPECIFIED
#include "Lin_MemMap.h"

/*==================================================================================================
*                                       DEFINES AND MACROS
==================================================================================================*/

/*==================================================================================================
*                                             ENUMS
==================================================================================================*/

/*==================================================================================================
*                                  STRUCTURES AND OTHER TYPEDEFS
==================================================================================================*/

/*==================================================================================================
*                                 GLOBAL VARIABLE DECLARATIONS
==================================================================================================*/

/*==================================================================================================
*                                     FUNCTION PROTOTYPES
==================================================================================================*/
#define LIN_START_SEC_CODE
#include "Lin_MemMap.h"

/**
 * @brief Initializes an LIN_LPUART instance for LIN Network.
 *
 * @details The caller provides memory for the driver state structures during initialization.
 * The user must select the LIN_LPUART clock source in the application to initialize the LIN_LPUART.
 * This function initializes a LPUART instance for operation.
 * This function will initialize the run-time state structure to keep track of
 * the on-going transfers, initialize the module to user defined settings and
 * default settings, set break field length to be 13 bit times minimum, enable
 * the break detect interrupt, Rx complete interrupt, frame error detect interrupt,
 * and enable the LPUART module transmitter and receiver
 *
 * @param u32Instance LIN_LPUART instance number
 * @param linUserConfig user configuration structure of type #lin_user_config_t
 * 
 * @return void
 * 
 * 
 */
void Lpuart_Lin_Ip_Init(const uint32 u32Instance, const Lpuart_Lin_Ip_UserConfigType * pUserConfig);

/**
 * @brief Shuts down the LIN_LPUART by disabling interrupts and transmitter/receiver.
 *
 * @param u32Instance LIN_LPUART instance number
 * 
 * @return void
 * 
 * 
 */
void Lpuart_Lin_Ip_Deinit(const uint32 u32Instance);

/**
 * @brief Sends frame data out through the LIN_LPUART module using non-blocking method.
 * 
 * @details This enables an a-sync method for transmitting data.
 *  Non-blocking  means that the function returns immediately.
 *  The application has to get the transmit status to know when the transmit is complete.
 *  This function will calculate the checksum byte and send it with the frame data.
 *  If txSize is equal to 0 or greater than 8 or node's current state is in SLEEP mode
 *  then the function will return LPUART_LIN_IP_STATUS_ERROR. If isBusBusy is currently true then the
 *  function will return LPUART_LIN_IP_STATUS_BUSY.
 *
 * @param[in]  u32Instance LIN_LPUART instance number
 * @param[in]  pTxBuff  source buffer containing 8-bit data chars to send
 * @param[in]  u8TxSize  the number of bytes to send
 * 
 * @return operation status:
 * @retval LPUART_LIN_IP_STATUS_SUCCESS : The transmission is successful.
 * @retval LPUART_LIN_IP_STATUS_BUSY    : Operation failed due to isBusBusy is currently true.
 * @retval LPUART_LIN_IP_STATUS_ERROR   : Operation failed due to txSize is equal to 0 or greater than 8
 *                                 or node's current state is in SLEEP mode
 * 
 * 
 */
Lpuart_Lin_Ip_StatusType Lpuart_Lin_Ip_AsyncSendFrameData(const uint32 u32Instance, const uint8 * pTxBuff, const uint8 u8TxSize);

/**
 * @brief Sends frame data out through the LIN_LPUART module using blocking method.
 * 
 * @details This enables a sync method for transmitting data. The frame data will be
 *  transmitted to the last byte and at the end is checksum byte. If any error occurred,
 *  the transmission would be suspended and returned LPUART_LIN_IP_STATUS_ERROR.
 *  Blocking means that the function does not return until the transmission is completed.
 *  The application can get status of the transmission by return value.
 *
 *
 * @param[in]  u32Instance LIN_LPUART instance number
 * @param[out] pTxBuff  source buffer containing 8-bit data chars to send
 * @param[in]  u8TxSize  the number of bytes to send
 * 
 * @return operation status:
 * @retval LPUART_LIN_IP_STATUS_SUCCESS : The transmission is successful.
 * @retval LPUART_LIN_IP_STATUS_TIMEOUT : The transmission has timeout.
 * @retval LPUART_LIN_IP_STATUS_ERROR   : Operation failed due to txSize is equal to 0 or greater than 8
 *                                 or node's current state is in SLEEP mode or an error occurred in transmission
 * 
 * 
 */
Lpuart_Lin_Ip_StatusType Lpuart_Lin_Ip_SyncSendFrameData(const uint32 u32Instance, const uint8 * pTxBuff, const uint8 u8TxSize);

/**
 * @brief Get status of an on-going non-blocking transmission
 *  
 * @details While sending frame data using non-blocking method, users can
 *  use this function to get status of that transmission.
 *  This function return LPUART_LIN_IP_STATUS_BUSY while sending, or LPUART_LIN_IP_STATUS_TIMEOUT
 *  if timeout has occurred, or return LPUART_LIN_IP_STATUS_SUCCESS when the transmission is complete.
 *  The pBytesRemaining shows number of bytes that still needed to transmit including the CheckSum byte.
 *
 * @param u32Instance LIN_LPUART instance number
 * @param pBytesRemaining  Number of bytes still needed to transmit
 * @return operation status:
 * @retval  LPUART_LIN_IP_STATUS_SUCCESS : The transmission is successful.
 * @retval  LPUART_LIN_IP_STATUS_BUSY    : The transmission is sending
 * @retval  LPUART_LIN_IP_STATUS_TIMEOUT : Operation failed due to timeout has occurred.
 * 
 * 
 */

Lpuart_Lin_Ip_StatusType Lpuart_Lin_Ip_GetTransmitStatus(const uint32 u32Instance, uint8 * const pBytesRemaining);

/**
 * @brief Receives frame data through the LIN_LPUART module using non-blocking method.
 * 
 * @details This function returns immediately after initiating the receive function.
 *  The application has to get the receive status to see when the receive is complete.
 *  The interrupt handler Lpuart_Lin_Ip_IRQHandler will check the checksum byte.
 *  If the checksum is correct, it will receive the frame data.
 *  This function also check if rxSize is in range from 1 to 8.
 *  If not, it will return LPUART_LIN_IP_STATUS_ERROR. This function also returns LPUART_LIN_IP_STATUS_ERROR if
 *  node's current state is in SLEEP mode. This function checks if the isBusBusy is
 *  false, if not it will return LPUART_LIN_IP_STATUS_BUSY.
 *
 * @param[in]  u32Instance LIN_LPUART u32Instance number
 * @param[out] pRxBuff  buffer containing 8-bit received data
 * @param[in]  u8RxSize the number of bytes to receive
 * @return operation status:
 * @retval  LPUART_LIN_IP_STATUS_SUCCESS : The receives frame data is successful.
 * @retval  LPUART_LIN_IP_STATUS_BUSY    : Bus busy flag is true.
 * @retval  LPUART_LIN_IP_STATUS_ERROR   : Operation failed due is equal to 0 or greater than 8 or
 *                                  node's current state is in SLEEP mode
 * 
 * 
 */
Lpuart_Lin_Ip_StatusType Lpuart_Lin_Ip_AsyncReceiveFrameData(const uint32 u32Instance, uint8 * pRxBuff, const uint8 u8RxSize);

/**
 * @brief Sends frame data out through the LIN_LPUART module using blocking method.
 * 
 * @details This enables a sync method for receiving data. The frame data will be
 *  received to its the last byte and at the end is checksum byte. If any error occurred,
 *  the reception would be suspended and returned LPUART_LIN_IP_STATUS_ERROR.
 *  Blocking means that the function does not return until the reception is completed.
 *  The application can get status of the reception by return value.
 *
 *
 * @param[in]  u32Instance LIN_LPUART instance number
 * @param[out] pRxBuff  source buffer containing 8-bit data chars to receive
 * @param[in]  u8RxSize  the number of bytes to receive
 * 
 * @return operation status:
 * @retval LPUART_LIN_IP_STATUS_SUCCESS : The receives frame data is successful.
 * @retval LPUART_LIN_IP_STATUS_TIMEOUT : The transmission has timeout.
 * @retval LPUART_LIN_IP_STATUS_BUSY    : Bus busy flag is true.
 * @retval LPUART_LIN_IP_STATUS_ERROR   : Operation failed due to rxSize is equal to 0 or greater than 8
 *                                 or node's current state is in SLEEP mode or an error occurred in transmission
 * 
 * 
 */
Lpuart_Lin_Ip_StatusType Lpuart_Lin_Ip_SyncReceiveFrameData(const uint32 u32Instance, uint8 * pRxBuff, const uint8 u8RxSize);

/**
 * @brief Aborts an on-going non-blocking transmission/reception.
 * 
 * @details While performing a non-blocking transferring data, users can call this function
 *  to terminate immediately the transferring.
 *
 * @param u32Instance LIN_LPUART instance number
 * @return void
 *
 * 
 */
void Lpuart_Lin_Ip_AbortTransferData(const uint32 u32Instance);

/**
 * @brief Get status of an on-going non-blocking reception
 * @details While receiving frame data using non-blocking method, users can
 *  use this function to get status of that receiving.
 *  This function return the current event ID, LPUART_LIN_IP_STATUS_BUSY while receiving
 *  and return LPUART_LIN_IP_STATUS_SUCCESS, or timeout (LPUART_LIN_IP_STATUS_TIMEOUT) when the reception is complete.
 *  The pBytesRemaining shows number of bytes that still needed to receive including the CheckSum byte.
 *
 * @param u32Instance LIN_LPUART instance number
 * @param pBytesRemaining  Number of bytes still needed to receive
 * 
 * @return operation status:
 * @retval  LPUART_LIN_IP_STATUS_SUCCESS : The reception is complete.
 * @retval  LPUART_LIN_IP_STATUS_TIMEOUT : The reception isn't complete.
 * @retval  LPUART_LIN_IP_STATUS_BUSY    : The reception is on going
 * 
 * 
 */
Lpuart_Lin_Ip_StatusType Lpuart_Lin_Ip_GetReceiveStatus(const uint32 u32Instance, uint8 * const pBytesRemaining);

/**
 * @brief This function puts current node to sleep mode
 * @details This function changes current node state to LIN_NODE_STATE_SLEEP_MODE
 *
 * @param u32Instance LIN_LPUART instance number
 * @return function always return LPUART_LIN_IP_STATUS_SUCCESS
 * 
 * 
 */
void Lpuart_Lin_Ip_GoToSleepMode(const uint32 u32Instance);

/**
 * @brief Puts current LIN node to Idle state
 * This function changes current node state to LIN_NODE_STATE_IDLE
 *
 * @param u32Instance LIN_LPUART instance number
 * @return function always return LPUART_LIN_IP_STATUS_SUCCESS
 * 
 * 
 */
void Lpuart_Lin_Ip_GotoIdleState(const uint32 u32Instance);

/**
 * @brief Sends a wakeup signal through the LIN_LPUART interface
 *
 * @param u32Instance LIN_LPUART instance number
 * @return operation status:
 * @retval  LPUART_LIN_IP_STATUS_SUCCESS : Bus busy flag is false.
 * @retval  LPUART_LIN_IP_STATUS_BUSY    : Bus busy flag is true.
 * 
 * 
 */
Lpuart_Lin_Ip_StatusType Lpuart_Lin_Ip_SendWakeupSignal(const uint32 u32Instance);

/**
 * @brief Get the current LIN node state
 *
 * @param u32Instance LIN_LPUART instance number
 * @return current LIN node state
 * 
 * 
 */
Lpuart_Lin_Ip_NodeStateType Lpuart_Lin_Ip_GetCurrentNodeState(const uint32 u32Instance);

/**
 * @brief Callback function for Timer Interrupt Handler
 * Users shall initialize a timer (for example FTM) in Output compare mode
 * with period of 500 micro seconds. In timer IRQ handler, call this function.
 *
 * @param u32Instance LIN_LPUART instance number
 * @return void
 * 
 * 
 */
void Lpuart_Lin_Ip_TimeoutService(const uint32 u32Instance);
/**
 * @brief Set Value for Timeout Counter that is used in Lpuart_Lin_Ip_TimeoutService
 *
 * @param u32Instance LPUART instance number
 * @param u32TimeoutValue  Timeout Value to be set
 * @return void
 */

void Lpuart_Lin_Ip_SetTimeoutCounter(const uint32 u32Instance, const uint32 u32TimeoutValue);

/**
 * @brief Sends frame header out through the LIN_LPUART module using a non-blocking method.
 * @details This function sends LIN Break field, sync field then the ID with
 * correct parity. This function checks if the interface is Master, if not, it will
 * return LPUART_LIN_IP_STATUS_ERROR.This function checks if id is in range from 0 to 0x3F, if not
 * it will return LPUART_LIN_IP_STATUS_ERROR.
 *
 * @param u32Instance LIN_LPUART instance number.
 * @param u8Id  Frame Identifier.
 * @return operation status:
 * @retval  LPUART_LIN_IP_STATUS_SUCCESS : The transmission is successful.
 * @retval  LPUART_LIN_IP_STATUS_BUSY    : Bus busy flag is true.
 * @retval  LPUART_LIN_IP_STATUS_ERROR   : The interface isn't Master or id isn't in range from 0 to 0x3F
 *                            or node's current state is in SLEEP mode.
 * 
 * 
 */
Lpuart_Lin_Ip_StatusType Lpuart_Lin_Ip_MasterSendHeader(const uint32 u32Instance, const uint8 u8Id);

/**
 * @brief LIN_LPUART interrupt handler for RX_TX and Error interrupts.
 *
 * @param u32Instance LIN_LPUART instance number
 * @return void
 * 
 * 
 */
void Lpuart_Lin_Ip_IRQHandler(const uint32 u32Instance);

#define LIN_STOP_SEC_CODE
#include "Lin_MemMap.h"

#ifdef __cplusplus
}
#endif

/** @} */

#endif /* LPUART_LIN_IP_H */
