/*==================================================================================================
*   Project              : RTD AUTOSAR 4.4
*   Platform             : CORTEXM
*   Peripheral           : FLEXIO
*   Dependencies         : 
*
*   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_UART_IP_HWACCESS_H__
#define LPUART_UART_IP_HWACCESS_H__

/**
*   @file
*   @internal
*   @defgroup lpuart_ip Lpuart IPL
*   @addtogroup  lpuart_ip Lpuart  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 "StandardTypes.h"
#include "S32K344_LPUART.h"
#include "OsIf.h"
#include "Lpuart_Uart_Ip_Defines.h"
#include "SchM_Uart.h"

/*==================================================================================================
*                                 SOURCE FILE VERSION INFORMATION
==================================================================================================*/
#define LPUART_UART_IP_HWACCESS_VENDOR_ID                    43
#define LPUART_UART_IP_HWACCESS_AR_RELEASE_MAJOR_VERSION     4
#define LPUART_UART_IP_HWACCESS_AR_RELEASE_MINOR_VERSION     4
#define LPUART_UART_IP_HWACCESS_AR_RELEASE_REVISION_VERSION  0
#define LPUART_UART_IP_HWACCESS_SW_MAJOR_VERSION             0
#define LPUART_UART_IP_HWACCESS_SW_MINOR_VERSION             9
#define LPUART_UART_IP_HWACCESS_SW_PATCH_VERSION             0

/*==================================================================================================
*                                       FILE VERSION CHECKS
==================================================================================================*/
/* Checks against Lpuart_Uart_Ip_Defines.h */
#if (LPUART_UART_IP_HWACCESS_VENDOR_ID != LPUART_UART_IP_DEFINES_VENDOR_ID)
    #error "Lpuart_Uart_Ip_HwAccess.h and Lpuart_Uart_Ip_Defines.h have different vendor ids"
#endif
#if ((LPUART_UART_IP_HWACCESS_AR_RELEASE_MAJOR_VERSION    != LPUART_UART_IP_DEFINES_AR_RELEASE_MAJOR_VERSION) || \
     (LPUART_UART_IP_HWACCESS_AR_RELEASE_MINOR_VERSION    != LPUART_UART_IP_DEFINES_AR_RELEASE_MINOR_VERSION) || \
     (LPUART_UART_IP_HWACCESS_AR_RELEASE_REVISION_VERSION != LPUART_UART_IP_DEFINES_AR_RELEASE_REVISION_VERSION))
     #error "AUTOSAR Version Numbers of Lpuart_Uart_Ip_HwAccess.h and Lpuart_Uart_Ip_Defines.h are different"
#endif
#if ((LPUART_UART_IP_HWACCESS_SW_MAJOR_VERSION != LPUART_UART_IP_DEFINES_SW_MAJOR_VERSION) || \
     (LPUART_UART_IP_HWACCESS_SW_MINOR_VERSION != LPUART_UART_IP_DEFINES_SW_MINOR_VERSION) || \
     (LPUART_UART_IP_HWACCESS_SW_PATCH_VERSION != LPUART_UART_IP_DEFINES_SW_PATCH_VERSION))
    #error "Software Version Numbers of Lpuart_Uart_Ip_HwAccess.h and Lpuart_Uart_Ip_Defines.h are different"
#endif

#ifndef DISABLE_MCAL_INTERMODULE_ASR_CHECK
/* Check if current file and StandardTypes.h header file are of the same Autosar version */
    #if ((LPUART_UART_IP_HWACCESS_AR_RELEASE_MAJOR_VERSION != STD_AR_RELEASE_MAJOR_VERSION) || \
            (LPUART_UART_IP_HWACCESS_AR_RELEASE_MINOR_VERSION != STD_AR_RELEASE_MINOR_VERSION))
        #error "Lpuart_Uart_Ip_HwAccess.h and StandardTypes.h are different"
    #endif
#endif
/*==================================================================================================
*                                            CONSTANTS
==================================================================================================*/

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

#define LPUART_FEATURE_DEFAULT_OSR                  (0xFU)
#define LPUART_FEATURE_DEFAULT_SBR                  (0x4U)
#define LPUART_FEATURE_STAT_REG_FLAGS_MASK          (0xC01FC000U)

/* Wrap-around value for timeout */
#define TIMEOUT_WRAP     (0xFFFFFFU)


/*! @brief LPUART number of bits in a character
 *
 *
 */
/* implements     Lpuart_Uart_Ip_BitCountPerCharType_Enumeration */
typedef enum
{
    LPUART_UART_IP_7_BITS_PER_CHAR  = 0x0U, /*!< 7-bit data characters */
    LPUART_UART_IP_8_BITS_PER_CHAR  = 0x1U, /*!< 8-bit data characters */
    LPUART_UART_IP_9_BITS_PER_CHAR  = 0x2U, /*!< 9-bit data characters */
    LPUART_UART_IP_10_BITS_PER_CHAR = 0x3U  /*!< 10-bit data characters */
} Lpuart_Uart_Ip_BitCountPerCharType;

/*! @brief LPUART parity mode
 *
 *
 */
/* implements     Lpuart_Uart_Ip_ParityModeType_Enumeration */
typedef enum
{
    LPUART_UART_IP_PARITY_DISABLED = 0x0U, /*!< parity disabled */
    LPUART_UART_IP_PARITY_EVEN     = 0x2U, /*!< parity enabled, type even, bit setting: PE|PT = 10 */
    LPUART_UART_IP_PARITY_ODD      = 0x3U  /*!< parity enabled, type odd,  bit setting: PE|PT = 11 */
} Lpuart_Uart_Ip_ParityModeType;

typedef enum
{
    LPUART_UART_IP_ONE_STOP_BIT = 0x0U, /*!< one stop bit */
    LPUART_UART_IP_TWO_STOP_BIT = 0x1U  /*!< two stop bits */
} Lpuart_Uart_Ip_StopBitCountType;

/*!
 * @brief LPUART status flags.
 *
 * This provides constants for the LPUART status flags for use in the UART functions.
 */
typedef enum
{
    LPUART_UART_IP_TX_DATA_REG_EMPTY          = (uint32)LPUART_STAT_TDRE_SHIFT,
                                                /*!< Tx data register empty flag, sets when Tx buffer is empty */
    LPUART_UART_IP_TX_COMPLETE                = (uint32)LPUART_STAT_TC_SHIFT,
                                                /*!< Transmission complete flag, sets when transmission activity complete */
    LPUART_UART_IP_DATA_REG_FULL              = (uint32)LPUART_STAT_RDRF_SHIFT,
                                                /*!< Rx data register full flag, sets when the receive data buffer is full */
    LPUART_UART_IP_RX_OVERRUN                 = (uint32)LPUART_STAT_OR_SHIFT,
                                                /*!< Rx Overrun sets if new data is received before data is read */
    LPUART_UART_IP_NOISE_DETECT               = (uint32)LPUART_STAT_NF_SHIFT,
                                                /*!< Rx takes 3 samples of each received bit. If these differ, the flag sets */
    LPUART_UART_IP_FRAME_ERR                  = (uint32)LPUART_STAT_FE_SHIFT,
                                                /*!< Frame error flag, sets if logic 0 was detected where stop bit expected */
    LPUART_UART_IP_PARITY_ERR                 = (uint32)LPUART_STAT_PF_SHIFT,
} Lpuart_Uart_Ip_StatusFlagType;

/*! @brief LPUART interrupt configuration structure, default settings are 0 (disabled) */
typedef enum
{
    LPUART_UART_IP_INT_TX_DATA_REG_EMPTY = (uint32)LPUART_CTRL_TIE_SHIFT,     /*!< Transmit data register empty. */
    LPUART_UART_IP_INT_TX_COMPLETE       = (uint32)LPUART_CTRL_TCIE_SHIFT,    /*!< Transmission complete. */
    LPUART_UART_IP_INT_RX_DATA_REG_FULL  = (uint32)LPUART_CTRL_RIE_SHIFT,     /*!< Receiver data register full. */
    LPUART_UART_IP_INT_RX_OVERRUN        = (uint32)LPUART_CTRL_ORIE_SHIFT,    /*!< Receiver Overrun. */
    LPUART_UART_IP_INT_NOISE_ERR_FLAG    = (uint32)LPUART_CTRL_NEIE_SHIFT,    /*!< Noise error flag. */
    LPUART_UART_IP_INT_FRAME_ERR_FLAG    = (uint32)LPUART_CTRL_FEIE_SHIFT,    /*!< Framing error flag. */
    LPUART_UART_IP_INT_PARITY_ERR_FLAG   = (uint32)LPUART_CTRL_PEIE_SHIFT,    /*!< Parity error flag. */

} Lpuart_Uart_Ip_InterruptType;

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

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

/*==================================================================================================
*                                       FUNCTION PROTOTYPES
==================================================================================================*/

#define UART_START_SEC_CODE
/* @violates @ref Uart_c_REF_1 This violation is not fixed since the inclusion of Uart_MemMap.h is as per AUTOSAR requirement*/
#include "Uart_MemMap.h"

/*!
 * @brief Initializes the LPUART controller.
 *
 * This function Initializes the LPUART controller to known state.
 *
 *
 * @param pBase LPUART base pointer.
 */
static inline void LPUART_Uart_Init(LPUART_Type * pBase)
{
    /* Set the default oversampling ratio (16) and baud-rate divider (4) */
    pBase->BAUD = ((uint32)(((uint32)LPUART_FEATURE_DEFAULT_OSR << LPUART_BAUD_OSR_SHIFT) | \
                 (LPUART_FEATURE_DEFAULT_SBR << LPUART_BAUD_SBR_SHIFT)));
    /* Clear the error/interrupt flags */
    pBase->STAT = LPUART_FEATURE_STAT_REG_FLAGS_MASK;
    /* Reset all features/interrupts by default */
    pBase->CTRL = 0x00000000;
}

/*!
 * @brief Enable/Disable the LPUART transmitter.
 *
 * This function enables or disables the LPUART transmitter, based on the
 * parameter received.
 *
 *
 * @param pBase LPUART base pointer.
 * @param enable Enable(true) or disable(false) transmitter.
 */
static inline void LPUART_Uart_SetTransmitterCmd(LPUART_Type * pBase, boolean enable)
{
    pBase->CTRL = (pBase->CTRL & ~LPUART_CTRL_TE_MASK) | ((enable ? 1UL : 0UL) << LPUART_CTRL_TE_SHIFT);
}

/*!
 * @brief Enable/Disable the LPUART receiver.
 *
 * This function enables or disables the LPUART receiver, based on the
 * parameter received.
 *
 *
 * @param pBase LPUART base pointer
 * @param enable Enable(true) or disable(false) receiver.
 */
static inline void LPUART_Uart_SetReceiverCmd(LPUART_Type * pBase, boolean enable)
{
    pBase->CTRL = (pBase->CTRL & ~LPUART_CTRL_RE_MASK) | ((enable ? 1UL : 0UL) << LPUART_CTRL_RE_SHIFT);
}

/*!
 * @brief Sets the LPUART baud rate modulo divisor.
 *
 * This function sets the LPUART baud rate modulo divisor.
 *
 *
 * @param pBase LPUART base pointer.
 * @param baudRateDivisor The baud rate modulo division "SBR"
 */
static inline void LPUART_Uart_SetBaudRateDivisor(LPUART_Type * pBase, uint32 baudRateDivisor)
{
    pBase->BAUD = (pBase->BAUD & ~LPUART_BAUD_SBR_MASK) | (baudRateDivisor & LPUART_BAUD_SBR_MASK);
}

/*!
 * @brief Gets the LPUART baud rate modulo divisor.
 *
 * This function gets the LPUART baud rate modulo divisor.
 *
 *
 * @param pBase LPUART base pointer.
 * @return The baud rate modulo division "SBR"
 */
static inline uint16 LPUART_Uart_GetBaudRateDivisor(const LPUART_Type * pBase)
{
    return ((uint16)((pBase->BAUD & LPUART_BAUD_SBR_MASK) >> LPUART_BAUD_SBR_SHIFT));
}

/*!
 * @brief Sets the LPUART baud rate oversampling ratio
 *
 * This function sets the LPUART baud rate oversampling ratio.
 * (Note: Feature available on select LPUART instances used together with baud rate programming)
 * The oversampling ratio should be set between 4x (00011) and 32x (11111). Writing
 * an invalid oversampling ratio results in an error and is set to a default
 * 16x (01111) oversampling ratio.
 * Disable the transmitter/receiver before calling this function.
 *
 *
 * @param pBase LPUART base pointer.
 * @param overSamplingRatio The oversampling ratio "OSR"
 */
static inline void LPUART_Uart_SetOversamplingRatio(LPUART_Type * pBase, uint32 overSamplingRatio)
{
    pBase->BAUD = (pBase->BAUD & ~LPUART_BAUD_OSR_MASK) | LPUART_BAUD_OSR(overSamplingRatio);
}

/*!
 * @brief Gets the LPUART baud rate oversampling ratio
 *
 * This function gets the LPUART baud rate oversampling ratio.
 * (Note: Feature available on select LPUART instances used together with baud rate programming)
 *
 *
 * @param pBase LPUART base pointer.
 * @return The oversampling ratio "OSR"
 */
static inline uint8 LPUART_Uart_GetOversamplingRatio(const LPUART_Type * pBase)
{
    return ((uint8)((pBase->BAUD & LPUART_BAUD_OSR_MASK) >> LPUART_BAUD_OSR_SHIFT));
}


/*!
 * @brief Configures the LPUART baud rate both edge sampling
 *
 * This function configures the LPUART baud rate both edge sampling.
 * (Note: Feature available on select LPUART instances used with baud rate programming)
 * When enabled, the received data is sampled on both edges of the baud rate clock.
 * This must be set when the oversampling ratio is between 4x and 7x.
 * This function should only be called when the receiver is disabled.
 *
 *
 * @param pBase LPUART base pointer.
 * @param enable   Enable (1) or Disable (0) Both Edge Sampling
 */
static inline void LPUART_Uart_EnableBothEdgeSamplingCmd(LPUART_Type * pBase)
{
    pBase->BAUD |= LPUART_BAUD_BOTHEDGE_MASK;
}

/*!
 * @brief Configures the number of bits per character in the LPUART controller.
 *
 * This function configures the number of bits per character in the LPUART controller.
 * In some LPUART instances, the user should disable the transmitter/receiver
 * before calling this function.
 * Generally, this may be applied to all LPUARTs to ensure safe operation.
 *
 * @param pBase LPUART base pointer.
 * @param bitCountPerChar  Number of bits per char (7, 8, 9, or 10, depending on the LPUART instance)
 * @param parity  Specifies whether parity bit is enabled
 */
static inline void LPUART_Uart_SetBitCountPerChar(LPUART_Type * pBase,
                                             Lpuart_Uart_Ip_BitCountPerCharType bitCountPerChar,
                                             boolean parity)
{
    uint32 tmpBitCountPerChar = (uint32)bitCountPerChar;
    if (parity)
    {
        tmpBitCountPerChar += 1U;
    }

    if (tmpBitCountPerChar == (uint32)LPUART_UART_IP_10_BITS_PER_CHAR)
    {
        pBase->BAUD = (pBase->BAUD & ~LPUART_BAUD_M10_MASK) | ((uint32)1U << LPUART_BAUD_M10_SHIFT);
    }
    else
    {
        if(LPUART_UART_IP_7_BITS_PER_CHAR == bitCountPerChar)
        {
            /* Check if parity is enabled or not*/
            if(parity)
            {
                /* clear M7 to make sure not 7-bit mode (M7 = 0) and config 8 bits (M = 0) */
                pBase->CTRL &= ~(LPUART_CTRL_M7_MASK | LPUART_CTRL_M_MASK);
            }
            else
            {
                /* config 7-bits (M7 = 1)*/
                pBase->CTRL = (pBase->CTRL & ~LPUART_CTRL_M7_MASK) | ((uint32)1U << LPUART_CTRL_M7_SHIFT);
            }
        }
        else
        {
            tmpBitCountPerChar -= 1U;
            /* config 8-bit (M=0) or 9-bits (M=1) */
            pBase->CTRL = (pBase->CTRL & ~LPUART_CTRL_M_MASK) | (tmpBitCountPerChar << LPUART_CTRL_M_SHIFT);
        }
        /* clear M10 to make sure not 10-bit mode */
        pBase->BAUD &= ~LPUART_BAUD_M10_MASK;
    }
}


/*!
 * @brief Configures parity mode in the LPUART controller.
 *
 * This function configures parity mode in the LPUART controller.
 * In some LPUART instances, the user should disable the transmitter/receiver
 * before calling this function.
 * Generally, this may be applied to all LPUARTs to ensure safe operation.
 *
 * @param pBase LPUART base pointer.
 * @param parityModeType  Parity mode (enabled, disable, odd, even - see parity_mode_t struct)
 */
static inline void LPUART_Uart_SetParityMode(LPUART_Type * pBase, Lpuart_Uart_Ip_ParityModeType parityModeType)
{
    pBase->CTRL = (pBase->CTRL & ~LPUART_CTRL_PE_MASK) | (((uint32)parityModeType >> 1U) << LPUART_CTRL_PE_SHIFT);
    pBase->CTRL = (pBase->CTRL & ~LPUART_CTRL_PT_MASK) | (((uint32)parityModeType & 1U) << LPUART_CTRL_PT_SHIFT);
}

/*!
 * @brief Configures the number of stop bits in the LPUART controller.
 *
 * This function configures the number of stop bits in the LPUART controller.
 * In some LPUART instances, the user should disable the transmitter/receiver
 * before calling this function.
 * Generally, this may be applied to all LPUARTs to ensure safe operation.
 *
 * @param pBase LPUART base pointer.
 * @param stopBitCount Number of stop bits (1 or 2 - see Lpuart_Uart_Ip_StopBitCountType struct)
 */
static inline void LPUART_Uart_SetStopBitCount(LPUART_Type * pBase, Lpuart_Uart_Ip_StopBitCountType stopBitCount)
{
    pBase->BAUD = (pBase->BAUD & ~LPUART_BAUD_SBNS_MASK) | ((uint32)stopBitCount << LPUART_BAUD_SBNS_SHIFT);
}


/*!
 * @brief Configures the LPUART module interrupts.
 *
 * This function configures the LPUART module interrupts to enable/disable various interrupt sources.
 *
 *
 * @param   pBase LPUART module base pointer.
 * @param   intSrc LPUART interrupt configuration data.
 * @param   enable   true: enable, false: disable.
 */
static inline void LPUART_Uart_SetIntMode(LPUART_Type * pBase, Lpuart_Uart_Ip_InterruptType intSrc, boolean enable)
{
    pBase->CTRL = (pBase->CTRL & ~(1UL << (uint32)intSrc)) | ((enable ? 1U : 0U) << (uint32)intSrc);
}

/*!
 * @brief Returns LPUART module interrupts state.
 *
 * This function returns whether a certain LPUART module interrupt is enabled or disabled.
 *
 *
 * @param   pBase LPUART module base pointer.
 * @param   intSrc LPUART interrupt configuration data.
 * @return  true: enable, false: disable.
 */
static inline boolean LPUART_Uart_GetIntMode(const LPUART_Type * pBase, Lpuart_Uart_Ip_InterruptType intSrc)
{
    boolean retVal = FALSE;
    retVal = (((pBase->CTRL >> (uint32)(intSrc)) & 1U) > 0U);
    return retVal;
}

#if (LPUART_UART_IP_HAS_DMA_ENABLED == STD_ON)
/*!
 * @brief Configures DMA requests.
 *
 * This function configures DMA requests for LPUART Transmitter.
 *
 *
 * @param pBase LPUART base pointer
 * @param enable Transmit DMA request configuration (enable:1 /disable: 0)
 */
static inline void LPUART_Uart_SetTxDmaCmd(LPUART_Type * pBase, boolean enable)
{
    pBase->BAUD = (pBase->BAUD & ~LPUART_BAUD_TDMAE_MASK) | ((enable ? 1UL : 0UL) << LPUART_BAUD_TDMAE_SHIFT);
}

/*!
 * @brief Configures DMA requests.
 *
 * This function configures DMA requests for LPUART Receiver.
 *
 *
 * @param pBase LPUART base pointer
 * @param enable Receive DMA request configuration (enable: 1/disable: 0)
 */
static inline void LPUART_Uart_SetRxDmaCmd(LPUART_Type * pBase, boolean enable)
{
    pBase->BAUD = (pBase->BAUD & ~LPUART_BAUD_RDMAE_MASK) | ((enable ? 1UL : 0UL) << LPUART_BAUD_RDMAE_SHIFT);
}
#endif

/*!
 * @brief Sends the LPUART 8-bit character.
 *
 * This functions sends an 8-bit character.
 *
 *
 * @param pBase LPUART Instance
 * @param data     data to send (8-bit)
 */
static inline void LPUART_Uart_Putchar(LPUART_Type * pBase, uint8 data)
{
    volatile uint8 * dataRegBytes = (volatile uint8 *)(&(pBase->DATA));
    dataRegBytes[0] = data;
}

/*!
 * @brief Sends the LPUART 9-bit character.
 *
 * This functions sends a 9-bit character.
 *
 *
 * @param pBase LPUART Instance
 * @param data     data to send (9-bit)
 */
static inline void LPUART_Uart_Putchar9(LPUART_Type * pBase, uint16 data)
{
    uint8 ninthDataBit;
    volatile uint8 * dataRegBytes = (volatile uint8 *)(&(pBase->DATA));


    ninthDataBit = (uint8)((data >> 8U) & 0x1U);

    /* write to ninth data bit T8(where T[0:7]=8-bits, T8=9th bit) */
    pBase->CTRL = (pBase->CTRL & ~LPUART_CTRL_R9T8_MASK) | ((uint32)(ninthDataBit) << LPUART_CTRL_R9T8_SHIFT);

    /* write 8-bits to the data register*/
    dataRegBytes[0] = (uint8)data;
}

/*!
 * @brief Sends the LPUART 10-bit character (Note: Feature available on select LPUART instances).
 *
 * This functions sends a 10-bit character.
 *
 *
 * @param pBase LPUART Instance
 * @param data   data to send (10-bit)
 */
static inline void LPUART_Uart_Putchar10(LPUART_Type * pBase, uint16 data)
{
    uint8 ninthDataBit, tenthDataBit;
    uint32 ctrlRegVal;
    volatile uint8 * dataRegBytes = (volatile uint8 *)(&(pBase->DATA));

    ninthDataBit = (uint8)((data >> 8U) & 0x1U);
    tenthDataBit = (uint8)((data >> 9U) & 0x1U);

    /* write to ninth/tenth data bit (T[0:7]=8-bits, T8=9th bit, T9=10th bit) */
    ctrlRegVal = pBase->CTRL;
    ctrlRegVal = (ctrlRegVal & ~LPUART_CTRL_R9T8_MASK) | ((uint32)ninthDataBit << LPUART_CTRL_R9T8_SHIFT);
    ctrlRegVal = (ctrlRegVal & ~LPUART_CTRL_R8T9_MASK) | ((uint32)tenthDataBit << LPUART_CTRL_R8T9_SHIFT);
    /*Note: T8(9th bit) and T9(10th bit) should be written at same time. */
    pBase->CTRL = ctrlRegVal;

    /* write to 8-bits to the data register */
    dataRegBytes[0] = (uint8)data;
}

/*!
 * @brief Gets the LPUART 8-bit character.
 *
 * This functions receives an 8-bit character.
 *
 *
 * @param pBase LPUART base pointer
 * @param readData Data read from receive (8-bit)
 */
static inline uint8 LPUART_Uart_Getchar(const LPUART_Type * pBase)
{
    return (uint8)pBase->DATA;
}

/*!
 * @brief Gets the LPUART 9-bit character.
 *
 * This functions receives a 9-bit character.
 *
 *
 * @param pBase LPUART base pointer
 * @param readData Data read from receive (9-bit)
 */
static inline uint16 LPUART_Uart_Getchar9(const LPUART_Type * pBase)
{
    uint16 readData;

    /* get ninth bit from lpuart data register */
    readData = (uint16)(((pBase->CTRL >> LPUART_CTRL_R8T9_SHIFT) & 1U) << 8);

    /* get 8-bit data from the lpuart data register */
    readData |= (uint8)pBase->DATA;
    return readData;
}

/*!
 * @brief Gets the LPUART 10-bit character.
 *
 * This functions receives a 10-bit character.
 *
 *
 * @param pBase LPUART pBase pointer
 * @param readData Data read from receive (10-bit)
 */
static inline uint16 LPUART_Uart_Getchar10(const LPUART_Type * pBase)
{
    uint16 readData;

    /* read tenth data bit */
    readData = (uint16)(((pBase->CTRL >> LPUART_CTRL_R9T8_SHIFT) & 1U) << 9);
    /* read ninth data bit */
    readData |= (uint16)(((pBase->CTRL >> LPUART_CTRL_R8T9_SHIFT) & 1U) << 8);

    /* get 8-bit data */
    readData |= (uint8)pBase->DATA;
    return readData;
}

/*!
 * @brief  LPUART get status flag
 *
 * This function returns the state of a status flag.
 *
 *
 * @param pBase LPUART base pointer
 * @param eStatusFlag  The status flag to query
 * @return Whether the current status flag is set(true) or not(false).
 */
static inline boolean LPUART_Uart_GetStatusFlag(const LPUART_Type * pBase, Lpuart_Uart_Ip_StatusFlagType eStatusFlag)
{
    boolean retVal = FALSE;
    retVal = (((pBase->STAT >> (uint32)(eStatusFlag)) & 1U) > 0U);
    return retVal;
}
/*!
 * @brief LPUART clears an individual status flag.
 *
 * This function clears an individual status flag (see Lpuart_Uart_Ip_StatusFlagType for list of status bits).
 *
 *
 * @param pBase LPUART base pointer
 * @param statusFlag  Desired LPUART status flag to clear
 * @return LPUART_UART_IP_STATUS_SUCCESS if successful or STATUS_ERROR if an error occured
 */
static inline void LPUART_Uart_ClearStatusFlag(LPUART_Type * pBase, Lpuart_Uart_Ip_StatusFlagType statusFlag)
{
    switch(statusFlag)
    {
        case LPUART_UART_IP_RX_OVERRUN:
            pBase->STAT = (pBase->STAT & (~LPUART_FEATURE_STAT_REG_FLAGS_MASK)) | LPUART_STAT_OR_MASK;
            break;

        case LPUART_UART_IP_NOISE_DETECT:
            pBase->STAT = (pBase->STAT & (~LPUART_FEATURE_STAT_REG_FLAGS_MASK)) | LPUART_STAT_NF_MASK;
            break;

        case LPUART_UART_IP_FRAME_ERR:
            pBase->STAT = (pBase->STAT & (~LPUART_FEATURE_STAT_REG_FLAGS_MASK)) | LPUART_STAT_FE_MASK;
            break;

        case LPUART_UART_IP_PARITY_ERR:
            pBase->STAT = (pBase->STAT & (~LPUART_FEATURE_STAT_REG_FLAGS_MASK)) | LPUART_STAT_PF_MASK;
            break;
        default:
            /* Dummy code */
            break;
    }
}

/*!
 * @brief  Clears the error flags treated by the driver
 *
 * This function clears the error flags treated by the driver.
 * *
 * @param pBase LPUART pBase pointer
 */
static inline void LPUART_Uart_ClearErrorFlags(LPUART_Type * pBase)
{
    uint32 mask = LPUART_STAT_OR_MASK | \
                    LPUART_STAT_NF_MASK | \
                    LPUART_STAT_FE_MASK | \
                    LPUART_STAT_PF_MASK;

    pBase->STAT = (pBase->STAT & (~LPUART_FEATURE_STAT_REG_FLAGS_MASK)) | mask;
}

/**
 * @brief   : Checks for timeout condition
 * @internal
 * @return  TRUE     Timeout occurs
 *          FALSE    Timeout does not occur
 */
static inline boolean LPUART_Uart_CheckTimeout(uint32 startTime, uint32 timeoutUs)
{
    uint32 timeoutTicks;
    uint32 currentTime;
    boolean retVal;
    timeoutTicks = OsIf_MicrosToTicks(timeoutUs,LPUART_UART_IP_TIMEOUT_TYPE);
    currentTime = OsIf_GetCounter(LPUART_UART_IP_TIMEOUT_TYPE);

    if (currentTime <= startTime)
    {
        retVal = ((startTime - currentTime) > timeoutTicks) ? TRUE : FALSE;
    }
    else
    {
        /* wrap around */
        retVal = ((TIMEOUT_WRAP + startTime - currentTime) > timeoutTicks) ? TRUE : FALSE;
    }
    return retVal;
}

/**
 * @brief   : Flush Tx Buffer
 *
 * This function causes all data that is stored in the transmit FIFO/buffer to be flushed.
 * *
 * @param pBase LPUART pBase pointer
 */
static inline void LPUART_Uart_FlushTxBuffer(LPUART_Type * pBase)
{
    pBase->FIFO |= LPUART_FIFO_TXFLUSH_MASK;
}

/**
 * @brief   : Flush Rx Buffer
 *
 * This function causes all data that is stored in the receive FIFO/buffer to be flushed.
 * *
 * @param pBase LPUART pBase pointer
 */
static inline void LPUART_Uart_FlushRxBuffer(LPUART_Type * pBase)
{
    pBase->FIFO |= LPUART_FIFO_RXFLUSH_MASK;
}

#define UART_STOP_SEC_CODE
/* @violates @ref Uart_c_REF_1 This violation is not fixed since the inclusion of Uart_MemMap.h is as per AUTOSAR requirement*/
#include "Uart_MemMap.h"

#if defined(__cplusplus)
}
#endif

/*@}*/

#endif /* LPUART_HW_ACCESS_H__ */
