/******************************************************************************/
/* Williams Advanced Engineering Limited                     Copyright(C) 2021*/
/* Grove                                                  All rights reserved */
/*                                                                            */
/******************************************************************************/

/*
 * Fls_Spi.c
 *
 *  Created on: 22 Jun 2022
 *      Author: Sebastien.Chapeau
 */
#include "Fls_Spi.h"
#include "Det.h"
#include <string.h>

#define FLS_START_SEC_VAR_FAST_CLEARED_ASIL_C_UNSPECIFIED
#include "Fls_MemMap.h"

static volatile struct g_spiPacket
{
    spiDAT1_t g_spiDataFormat;
    uint32  tx_length;
    uint32  rx_length;
    uint16  * txdata_ptr;
    uint16  * rxdata_ptr;
    SpiDataStatus_t tx_data_status;
    SpiDataStatus_t rx_data_status;
} g_spiPacket_t[1U];

#define FLS_STOP_SEC_VAR_FAST_CLEARED_ASIL_C_UNSPECIFIED
#include "Fls_MemMap.h"

#define FLS_START_SEC_VAR_FAST_CLEARED_ASIL_C_UNSPECIFIED
#include "Fls_MemMap.h"

static VAR(Fls_QspiMsgType, FLS_VAR_FAST_CLEARED) FlashMsg[3];

#define FLS_STOP_SEC_VAR_FAST_CLEARED_ASIL_C_UNSPECIFIED
#include "Fls_MemMap.h"

#define FLS_START_SEC_VAR_FAST_CLEARED_ASIL_C_UNSPECIFIED
#include "Fls_MemMap.h"

static VAR(uint8, AUTOMATIC) TxDataValue[10];

#define FLS_STOP_SEC_VAR_FAST_CLEARED_ASIL_C_UNSPECIFIED
#include "Fls_MemMap.h"

#define FLS_START_SEC_VAR_FAST_CLEARED_ASIL_C_UNSPECIFIED
#include "Fls_MemMap.h"

static VAR(uint8, AUTOMATIC) RxDataValue[10];

#define FLS_STOP_SEC_VAR_FAST_CLEARED_ASIL_C_UNSPECIFIED
#include "Fls_MemMap.h"

#define FLS_START_SEC_VAR_FAST_CLEARED_ASIL_C_UNSPECIFIED
#include "Fls_MemMap.h"

static VAR(QSPI_RWDevType, FLS_VAR_FAST_CLEARED) QSPI_RWDev =
{
  QSPI_CMD_NONE,
};
#define FLS_STOP_SEC_VAR_FAST_CLEARED_ASIL_C_UNSPECIFIED
#include "Fls_MemMap.h"

#define FLS_START_SEC_VAR_FAST_CLEARED_ASIL_C_UNSPECIFIED
#include "Fls_MemMap.h"

static P2VAR(XQspiConfigType, FLS_VAR_FAST_CLEARED, FLS_APPL_DATA) pQspiInstancePtr;

#define FLS_STOP_SEC_VAR_FAST_CLEARED_ASIL_C_UNSPECIFIED
#include "Fls_MemMap.h"

/* Add more flags as required */
#define FLS_XQSPI_MSG_FLAG_STRIPE              0x1U
#define FLS_XQSPI_MSG_FLAG_RX                  0x2U
#define FLS_XQSPI_MSG_FLAG_TX                  0x4U
#define FLS_XQSPI_MSG_FLAG_POLL                0x8U


#define FLS_XQSPI_SELECT_MODE_SPI               0x1U
#define FLS_XQSPI_SELECT_MODE_DUALSPI           0x2U
#define FLS_XQSPI_SELECT_MODE_QUADSPI           0x3U

#define FLS_START_SEC_CODE_SLOW_ASIL_C
#include "Fls_MemMap.h"
/*******************************************************************************
** Syntax            : Fls_QspiHwInit                                         **
** [/cover]                                                                   **
**                                                                            **
** Service ID        : NA                                                     **
**                                                                            **
** Sync/Async        : NA                                                     **
**                                                                            **
** Reentrancy        : Reentrant                                              **
**                                                                            **
** Parameters (in)   : none                                                   **
**                                                                            **
** Parameters (out)  : ulStatus                                               **
**                                                                            **
** Return value      : uint32                                                 **
**                                                                            **
** Description       : This service initialising QSPI hardware                **
*******************************************************************************/
static FUNC(void, FLS_CODE_SLOW) Fls_QspiHwInit(P2CONST(QSPI_ConfigType, AUTOMATIC, FLS_APPL_DATA) pConfig)
{
    /** @b initialize @b SPI5 */


    /** bring SPI out of reset */
    spiREG5->GCR0 = 0U;
    spiREG5->GCR0 = 1U;

    /** SPI5 master mode and clock configuration */
    spiREG5->GCR1 = (spiREG5->GCR1 & 0xFFFFFFFCU) | ((uint32)((uint32)1U << 1U)  /* CLOKMOD */
                  | 1U);  /* MASTER */

    /** SPI5 enable pin configuration */
    spiREG5->INT0 = (spiREG5->INT0 & 0xFEFFFFFFU) | (uint32)((uint32)1U << 24U);  /* ENABLE HIGHZ */

    /** - Delays */
    spiREG5->DELAY = (uint32)((uint32)4U << 24U)  /* C2TDELAY */
                   | (uint32)((uint32)0U << 16U)  /* T2CDELAY */
                   | (uint32)((uint32)0U << 8U)  /* T2EDELAY */
                   | (uint32)((uint32)0U << 0U);  /* C2EDELAY */

    /** - Data Format 0 */
    spiREG5->FMT0 = (uint32)((uint32)0U << 24U)  /* wdelay */
                  | (uint32)((uint32)0U << 23U)  /* parity Polarity */
                  | (uint32)((uint32)0U << 22U)  /* parity enable */
                  | (uint32)((uint32)0U << 21U)  /* wait on enable */
                  | (uint32)((uint32)0U << 20U)  /* shift direction */
                  | (uint32)((uint32)0U << 17U)  /* clock polarity */
                  | (uint32)((uint32)1U << 16U)  /* clock phase */
                  | (uint32)((uint32)pConfig->ulFlsQspiBaudRateDiv << 8U) /* baudrate prescale */
                  | (uint32)((uint32)8U << 0U);  /* data word length */

    /** - Data Format 1 */
    spiREG5->FMT1 = (uint32)((uint32)0U << 24U)  /* wdelay */
                  | (uint32)((uint32)0U << 23U)  /* parity Polarity */
                  | (uint32)((uint32)0U << 22U)  /* parity enable */
                  | (uint32)((uint32)0U << 21U)  /* wait on enable */
                  | (uint32)((uint32)0U << 20U)  /* shift direction */
                  | (uint32)((uint32)0U << 17U)  /* clock polarity */
                  | (uint32)((uint32)0U << 16U)  /* clock phase */
                  | (uint32)((uint32)pConfig->ulFlsQspiBaudRateDiv << 8U) /* baudrate prescale */
                  | (uint32)((uint32)8U << 0U);  /* data word length */

    /** - Data Format 2 */
    spiREG5->FMT2 = (uint32)((uint32)0U << 24U)  /* wdelay */
                  | (uint32)((uint32)0U << 23U)  /* parity Polarity */
                  | (uint32)((uint32)0U << 22U)  /* parity enable */
                  | (uint32)((uint32)0U << 21U)  /* wait on enable */
                  | (uint32)((uint32)0U << 20U)  /* shift direction */
                  | (uint32)((uint32)0U << 17U)  /* clock polarity */
                  | (uint32)((uint32)0U << 16U)  /* clock phase */
                  | (uint32)((uint32)pConfig->ulFlsQspiBaudRateDiv << 8U) /* baudrate prescale */
                  | (uint32)((uint32)8U << 0U);  /* data word length */

    /** - Data Format 3 */
    spiREG5->FMT3 = (uint32)((uint32)0U << 24U)  /* wdelay */
                  | (uint32)((uint32)0U << 23U)  /* parity Polarity */
                  | (uint32)((uint32)0U << 22U)  /* parity enable */
                  | (uint32)((uint32)0U << 21U)  /* wait on enable */
                  | (uint32)((uint32)0U << 20U)  /* shift direction */
                  | (uint32)((uint32)0U << 17U)  /* clock polarity */
                  | (uint32)((uint32)0U << 16U)  /* clock phase */
                  | (uint32)((uint32)pConfig->ulFlsQspiBaudRateDiv << 8U) /* baudrate prescale */
                  | (uint32)((uint32)8U << 0U);  /* data word length */

    /** - set interrupt levels */
    spiREG5->LVL = (uint32)((uint32)0U << 9U)  /* TXINT */
                 | (uint32)((uint32)0U << 8U)  /* RXINT */
                 | (uint32)((uint32)0U << 6U)  /* OVRNINT */
                 | (uint32)((uint32)0U << 4U)  /* BITERR */
                 | (uint32)((uint32)0U << 3U)  /* DESYNC */
                 | (uint32)((uint32)0U << 2U)  /* PARERR */
                 | (uint32)((uint32)0U << 1U) /* TIMEOUT */
                 | (uint32)((uint32)0U << 0U);  /* DLENERR */

    /** - clear any pending interrupts */
    spiREG5->FLG |= 0xFFFFU;

    /** - enable interrupts */
    spiREG5->INT0 = (spiREG5->INT0 & 0xFFFF0000U)
                  | (uint32)((uint32)0U << 9U)  /* TXINT */
                  | (uint32)((uint32)0U << 8U)  /* RXINT */
                  | (uint32)((uint32)0U << 6U)  /* OVRNINT */
                  | (uint32)((uint32)0U << 4U)  /* BITERR */
                  | (uint32)((uint32)0U << 3U)  /* DESYNC */
                  | (uint32)((uint32)0U << 2U)  /* PARERR */
                  | (uint32)((uint32)0U << 1U) /* TIMEOUT */
                  | (uint32)((uint32)0U << 0U);  /* DLENERR */

    /** @b initialize @b SPI5 @b Port */

    /** - SPI5 Port output values */
    spiREG5->PC3 =    (uint32)((uint32)1U << 0U)  /* SCS[0] */
                    | (uint32)((uint32)1U << 1U)  /* SCS[1] */
                    | (uint32)((uint32)1U << 2U)  /* SCS[2] */
                    | (uint32)((uint32)1U << 3U)  /* SCS[3] */
                    | (uint32)((uint32)0U << 8U)  /* ENA */
                    | (uint32)((uint32)0U << 9U)  /* CLK */
                    | (uint32)((uint32)0U << 10U)  /* SIMO[0] */
                    | (uint32)((uint32)0U << 11U)  /* SOMI[0] */
                    | (uint32)((uint32)0U << 17U)  /* SIMO[1] */
                    | (uint32)((uint32)0U << 18U)  /* SIMO[2] */
                    | (uint32)((uint32)0U << 19U)  /* SIMO[3] */
                    | (uint32)((uint32)0U << 25U)  /* SOMI[1] */
                    | (uint32)((uint32)0U << 26U)  /* SOMI[2] */
                    | (uint32)((uint32)0U << 27U); /* SOMI[3] */

    /** - SPI5 Port direction */
    spiREG5->PC1  =   (uint32)((uint32)1U << 0U)  /* SCS[0] */
                    | (uint32)((uint32)1U << 1U)  /* SCS[1] */
                    | (uint32)((uint32)1U << 2U)  /* SCS[2] */
                    | (uint32)((uint32)1U << 3U)  /* SCS[3] */
                    | (uint32)((uint32)0U << 8U)  /* ENA */
                    | (uint32)((uint32)1U << 9U)  /* CLK */
                    | (uint32)((uint32)1U << 10U)  /* SIMO[0] */
                    | (uint32)((uint32)0U << 11U)  /* SOMI[0] */
                    | (uint32)((uint32)0U << 17U)  /* SIMO[1] */
                    | (uint32)((uint32)0U << 18U)  /* SIMO[2] */
                    | (uint32)((uint32)0U << 19U)  /* SIMO[3] */
                    | (uint32)((uint32)0U << 25U)  /* SOMI[1] */
                    | (uint32)((uint32)0U << 26U)  /* SOMI[2] */
                    | (uint32)((uint32)0U << 27U); /* SOMI[3] */


    /** - SPI5 Port open drain enable */
    spiREG5->PC6  =   (uint32)((uint32)0U << 0U)  /* SCS[0] */
                    | (uint32)((uint32)0U << 1U)  /* SCS[1] */
                    | (uint32)((uint32)0U << 2U)  /* SCS[2] */
                    | (uint32)((uint32)0U << 3U)  /* SCS[3] */
                    | (uint32)((uint32)0U << 8U)  /* ENA */
                    | (uint32)((uint32)0U << 9U)  /* CLK */
                    | (uint32)((uint32)0U << 10U)  /* SIMO[0] */
                    | (uint32)((uint32)0U << 11U)  /* SOMI[0] */
                    | (uint32)((uint32)0U << 17U)  /* SIMO[1] */
                    | (uint32)((uint32)0U << 18U)  /* SIMO[2] */
                    | (uint32)((uint32)0U << 19U)  /* SIMO[3] */
                    | (uint32)((uint32)0U << 25U)  /* SOMI[1] */
                    | (uint32)((uint32)0U << 26U)  /* SOMI[2] */
                    | (uint32)((uint32)0U << 27U); /* SOMI[3] */

    /** - SPI5 Port pullup / pulldown selection */
    spiREG5->PC8  =   (uint32)((uint32)1U << 0U)  /* SCS[0] */
                    | (uint32)((uint32)1U << 1U)  /* SCS[1] */
                    | (uint32)((uint32)1U << 2U)  /* SCS[2] */
                    | (uint32)((uint32)1U << 3U)  /* SCS[3] */
                    | (uint32)((uint32)1U << 8U)  /* ENA */
                    | (uint32)((uint32)1U << 9U)  /* CLK */
                    | (uint32)((uint32)1U << 10U)  /* SIMO[0] */
                    | (uint32)((uint32)1U << 11U)  /* SOMI[0] */
                    | (uint32)((uint32)1U << 17U)  /* SIMO[1] */
                    | (uint32)((uint32)1U << 18U)  /* SIMO[2] */
                    | (uint32)((uint32)1U << 19U)  /* SIMO[3] */
                    | (uint32)((uint32)1U << 25U)  /* SOMI[1] */
                    | (uint32)((uint32)1U << 26U)  /* SOMI[2] */
                    | (uint32)((uint32)1U << 27U); /* SOMI[3] */

    /** - SPI5 Port pullup / pulldown enable*/
    spiREG5->PC7  =   (uint32)((uint32)0U << 0U)  /* SCS[0] */
                    | (uint32)((uint32)0U << 1U)  /* SCS[1] */
                    | (uint32)((uint32)0U << 2U)  /* SCS[2] */
                    | (uint32)((uint32)0U << 3U)  /* SCS[3] */
                    | (uint32)((uint32)0U << 8U)  /* ENA */
                    | (uint32)((uint32)0U << 9U)  /* CLK */
                    | (uint32)((uint32)0U << 10U)  /* SIMO[0] */
                    | (uint32)((uint32)0U << 11U)  /* SOMI[0] */
                    | (uint32)((uint32)0U << 17U)  /* SIMO[1] */
                    | (uint32)((uint32)0U << 18U)  /* SIMO[2] */
                    | (uint32)((uint32)0U << 19U)  /* SIMO[3] */
                    | (uint32)((uint32)0U << 25U)  /* SOMI[1] */
                    | (uint32)((uint32)0U << 26U)  /* SOMI[2] */
                    | (uint32)((uint32)0U << 27U); /* SOMI[3] */

    /* SPI5 set all pins to functional */
    spiREG5->PC0  =   (uint32)((uint32)1U << 0U)  /* SCS[0] */
                    | (uint32)((uint32)0U << 1U)  /* SCS[1] */
                    | (uint32)((uint32)0U << 2U)  /* SCS[2] */
                    | (uint32)((uint32)0U << 3U)  /* SCS[3] */
                    | (uint32)((uint32)0U << 8U)  /* ENA */
                    | (uint32)((uint32)1U << 9U)  /* CLK */
                    | (uint32)((uint32)1U << 10U)  /* SIMO[0] */
                    | (uint32)((uint32)1U << 11U)  /* SOMI[0] */
                    | (uint32)((uint32)0U << 17U)  /* SIMO[1] */
                    | (uint32)((uint32)0U << 18U)  /* SIMO[2] */
                    | (uint32)((uint32)0U << 19U)  /* SIMO[3] */
                    | (uint32)((uint32)0U << 25U)  /* SOMI[1] */
                    | (uint32)((uint32)0U << 26U)  /* SOMI[2] */
                    | (uint32)((uint32)0U << 27U); /* SOMI[3] */

    /** - Initialize TX and RX data buffer Status */
    g_spiPacket_t[0U].tx_data_status  = SPI_READY;
    g_spiPacket_t[0U].rx_data_status  = SPI_READY;

    /** - Finally start SPI5 */
    spiREG5->GCR1 = (spiREG5->GCR1 & 0xFEFFFFFFU) | 0x01000000U;

/* USER CODE BEGIN (3) */
/* USER CODE END */
}
#define FLS_STOP_SEC_CODE_SLOW_ASIL_C
#include "Fls_MemMap.h"


#if (FLS_4BYTE_ADDRESS_MODE == STD_ON)
#define FLS_START_SEC_CODE_SLOW_ASIL_C
#include "Fls_MemMap.h"

/*******************************************************************************
** Syntax             : Fls_Enable4ByteAddressing                             **
** [/cover]                                                                   **
**                                                                            **
** Service ID         : NA                                                    **
**                                                                            **
** Sync/Async         : NA                                                    **
**                                                                            **
** Reentrancy         : Non Reentrant                                         **
**                                                                            **
** Parameters (in)    : void                                                  **
**                                                                            **
** Parameters (out)   : void                                                  **
**                                                                            **
** Return value       : None                                                  **
**                                                                            **
** Description        : To enable Flash memory for 4 byte address mode        **
*******************************************************************************/
static FUNC(void, FLS_CODE_SLOW) Fls_Enable4ByteAddressing(void)
{

    VAR(uint32, AUTOMATIC) retVal = 0u;
    VAR(spiDAT1_t, AUTOMATIC) dataconfig1_t;

    dataconfig1_t.CS_HOLD = TRUE;
    dataconfig1_t.WDEL    = TRUE;
    dataconfig1_t.DFSEL   = SPI_FMT_0;
    dataconfig1_t.CSNR    = 0xFE; // SPI5_CS0

    /*Write Enable Cmd before Entering 4 Byte Add Mode */
    memset(&TxDataValue[0], 0xFF, sizeof(TxDataValue)/sizeof(TxDataValue[0]));
    TxDataValue[0] = FLS_CMD_WREN;
    retVal = Fls_SPI_SendCmdFlsDevice(&(TxDataValue[0]),1);

    /* Wait for device to be ready */
    memset(&TxDataValue[0], 0xFF, sizeof(TxDataValue)/sizeof(TxDataValue[0]));
    memset(&RxDataValue[0], 0xFF, sizeof(RxDataValue)/sizeof(RxDataValue[0]));
    TxDataValue[0] = FLS_CMD_RDSR; /*Read Satus register cmd */
    FlashMsg[0].pTxBfrPtr = TxDataValue;
    FlashMsg[0].pRxBfrPtr = RxDataValue;
    FlashMsg[0].ulByteCount = 2;
    FlashMsg[0].ulBusWidth = FLS_XQSPI_SELECT_MODE_SPI;
    FlashMsg[0].ulFlags = FLS_XQSPI_MSG_FLAG_TX;
    do
    {
        retVal = spiTransmitAndReceiveDataByByte(spiREG5, &dataconfig1_t, FlashMsg[0].ulByteCount,  (uint8 *)FlashMsg[0].pTxBfrPtr,  (uint8 *)FlashMsg[0].pRxBfrPtr,1);
    }while((FlashMsg[0].pRxBfrPtr[0] & 0x01)!= 0x00);


    /* ENTER 4-BYTE ADDRESS MODE */
    memset(&TxDataValue[0], 0xFF, sizeof(TxDataValue)/sizeof(TxDataValue[0]));
    TxDataValue[0] = FLS_CMD_4BEN;
    retVal = Fls_SPI_SendCmdFlsDevice(&(TxDataValue[0]),1);

    /* Wait for device to be ready */
    memset(&TxDataValue[0], 0xFF, sizeof(TxDataValue)/sizeof(TxDataValue[0]));
    memset(&RxDataValue[0], 0xFF, sizeof(RxDataValue)/sizeof(RxDataValue[0]));
    TxDataValue[0] = FLS_CMD_RDSR; /*Read Satus register cmd */
    FlashMsg[0].pTxBfrPtr = TxDataValue;
    FlashMsg[0].pRxBfrPtr = RxDataValue;
    FlashMsg[0].ulByteCount = 2;
    FlashMsg[0].ulBusWidth = FLS_XQSPI_SELECT_MODE_SPI;
    FlashMsg[0].ulFlags = FLS_XQSPI_MSG_FLAG_TX;
    do
    {
        retVal = spiTransmitAndReceiveDataByByte(spiREG5, &dataconfig1_t, FlashMsg[0].ulByteCount,  (uint8 *)FlashMsg[0].pTxBfrPtr,  (uint8 *)FlashMsg[0].pRxBfrPtr,1);
    }while((FlashMsg[0].pRxBfrPtr[0] & 0x01)!= 0x00);

}

#define FLS_STOP_SEC_CODE_SLOW_ASIL_C
#include "Fls_MemMap.h"
#endif /*#endif (FLS_4BYTE_ADDRESS_MODE == STD_ON)*/

#define FLS_START_SEC_CODE_SLOW_ASIL_C
#include "Fls_MemMap.h"
/*******************************************************************************
** Syntax             : Fls_QSPI_Init                                         **
** [/cover]                                                                   **
**                                                                            **
** Service ID         : NA                                                    **
**                                                                            **
** Sync/Async         : NA                                                    **
**                                                                            **
** Reentrancy         : Non Reentrant                                         **
**                                                                            **
** Parameters (in)    : pConfig                                               **
**                                                                            **
** Parameters (out)   : none                                                  **
**                                                                            **
** Return value       : void                                                  **
**                                                                            **
** Description        : QSPI Hardware initialization                          **
*******************************************************************************/
FUNC(Std_ReturnType, FLS_CODE_SLOW) Fls_QSPI_Init(P2CONST(QSPI_ConfigType, AUTOMATIC, FLS_APPL_DATA) pConfig)
{
    VAR(Std_ReturnType, AUTOMATIC) ReturnValue = E_OK;

    // Initialize SPI5 controller
    Fls_QspiHwInit(pConfig);

#if (FLS_4BYTE_ADDRESS_MODE == STD_ON)

    Fls_Enable4ByteAddressing();

#endif

    Fls_setFlashModeGeneric();

    return ReturnValue;
}

#define FLS_STOP_SEC_CODE_SLOW_ASIL_C
#include "Fls_MemMap.h"

#define FLS_START_SEC_CODE_SLOW_ASIL_C
#include "Fls_MemMap.h"
/*******************************************************************************
** Syntax             : Fls_setFlashModeGeneric                               **
** [/cover]                                                                   **
**                                                                            **
** Service ID         : NA                                                    **
**                                                                            **
** Sync/Async         : NA                                                    **
**                                                                            **
** Reentrancy         : Non Reentrant                                         **
**                                                                            **
** Parameters (in)    : void                                                  **
**                                                                            **
** Parameters (out)   : void                                                  **
**                                                                            **
** Return value       : None                                                  **
**                                                                            **
** Description        : To Flash memory Selection Generic Mode                **
*******************************************************************************/
static FUNC(void, FLS_CODE_SLOW) Fls_setFlashModeGeneric(void)
{
    VAR(uint32, AUTOMATIC) retVal = 0U;
    VAR(spiDAT1_t, AUTOMATIC) dataconfig1_t;

    dataconfig1_t.CS_HOLD = TRUE;
    dataconfig1_t.WDEL    = TRUE;
    dataconfig1_t.DFSEL   = SPI_FMT_0;
    dataconfig1_t.CSNR    = 0xFE; // SPI5_CS0

    /*Write enable cmd */
    memset(&TxDataValue[0], 0xFF, sizeof(TxDataValue)/sizeof(TxDataValue[0]));
    TxDataValue[0] = FLS_CMD_WRENV; /* Write command to target only the volatile registers */
    FlashMsg[0].pTxBfrPtr = TxDataValue;
    FlashMsg[0].pRxBfrPtr = NULL_PTR;
    FlashMsg[0].ulByteCount = 1;
    FlashMsg[0].ulBusWidth = FLS_XQSPI_SELECT_MODE_SPI;
    FlashMsg[0].ulFlags = FLS_XQSPI_MSG_FLAG_TX;

    retVal = spiTransmitDataByByte(spiREG5, &dataconfig1_t, FlashMsg[0].ulByteCount,  (uint8 *)FlashMsg[0].pTxBfrPtr);

    /* Wait for device to be ready */
    memset(&TxDataValue[0], 0xFF, sizeof(TxDataValue)/sizeof(TxDataValue[0]));
    memset(&RxDataValue[0], 0xFF, sizeof(RxDataValue)/sizeof(RxDataValue[0]));
    TxDataValue[0] = FLS_CMD_RDSR; /*Read Satus register cmd */
    FlashMsg[0].pTxBfrPtr = TxDataValue;
    FlashMsg[0].pRxBfrPtr = RxDataValue;
    FlashMsg[0].ulByteCount = 2;
    FlashMsg[0].ulBusWidth = FLS_XQSPI_SELECT_MODE_SPI;
    FlashMsg[0].ulFlags = FLS_XQSPI_MSG_FLAG_TX;
    do
    {
        retVal = spiTransmitAndReceiveDataByByte(spiREG5, &dataconfig1_t, FlashMsg[0].ulByteCount,  (uint8 *)FlashMsg[0].pTxBfrPtr,  (uint8 *)FlashMsg[0].pRxBfrPtr,1);
    }while((FlashMsg[0].pRxBfrPtr[0] & 0x01)!= 0x00);


    /*Write device in SPI mode */
    memset(&TxDataValue[0], 0xFF, sizeof(TxDataValue)/sizeof(TxDataValue[0]));
    TxDataValue[0] = FLS_CMD_WRR; /*Write Satus register cmd */
    TxDataValue[1] = 0x00; /* Status register #1 = 0    */
    TxDataValue[2] = 0x00; /* Configuration Reg 1- QE Bit - Keep in SPI mode - No protection */
    TxDataValue[3] = 0x60; /* Configuration Reg 2- Keep line impedance to factory default */
    TxDataValue[4] = 0x78; /* Configuration Reg 3- Keep wrap disabled and latency to 8 dummy cycles when using RDAR command */

    /* Byte 0 */
    FlashMsg[0].pTxBfrPtr = TxDataValue;
    FlashMsg[0].pRxBfrPtr = NULL_PTR;
    FlashMsg[0].ulByteCount = 5;
    FlashMsg[0].ulBusWidth = FLS_XQSPI_SELECT_MODE_SPI;
    FlashMsg[0].ulFlags = FLS_XQSPI_MSG_FLAG_TX;

    retVal = spiTransmitDataByByte(spiREG5, &dataconfig1_t, FlashMsg[0].ulByteCount,  (uint8 *)FlashMsg[0].pTxBfrPtr);


    /* Wait for device to be ready */
    memset(&TxDataValue[0], 0xFF, sizeof(TxDataValue)/sizeof(TxDataValue[0]));
    memset(&RxDataValue[0], 0xFF, sizeof(RxDataValue)/sizeof(RxDataValue[0]));
    TxDataValue[0] = FLS_CMD_RDSR; /*Read Satus register cmd */
    FlashMsg[0].pTxBfrPtr = TxDataValue;
    FlashMsg[0].pRxBfrPtr = RxDataValue;
    FlashMsg[0].ulByteCount = 2;
    FlashMsg[0].ulBusWidth = FLS_XQSPI_SELECT_MODE_SPI;
    FlashMsg[0].ulFlags = FLS_XQSPI_MSG_FLAG_TX;
    do
    {
        retVal = spiTransmitAndReceiveDataByByte(spiREG5, &dataconfig1_t, FlashMsg[0].ulByteCount,  (uint8 *)FlashMsg[0].pTxBfrPtr,  (uint8 *)FlashMsg[0].pRxBfrPtr,1);
    }while((FlashMsg[0].pRxBfrPtr[0] & 0x01)!= 0x00);

}
#define FLS_STOP_SEC_CODE_SLOW_ASIL_C
#include "Fls_MemMap.h"

#define FLS_START_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"
/*******************************************************************************
** Syntax            : Fls_DMA_IsBusy                                         **
** [/cover]                                                                   **
**                                                                            **
** Service ID        : NA                                                     **
**                                                                            **
** Sync/Async        : NA                                                     **
**                                                                            **
** Reentrancy        : Non reentrant                                          **
**                                                                            **
** Parameters (in)   : None                                                   **
**                                                                            **
** Parameters (out)  : retVal                                                 **
**                                                                            **
** Return value      : uint32                                                 **
**                                                                            **
** Description       : To check DMA status                                    **
*******************************************************************************/
FUNC(uint32, FLS_CODE_FAST) Fls_DMA_IsBusy(void)
{
  volatile VAR(uint32, AUTOMATIC) retVal = FALSE;

//  if (pQspiInstancePtr->ulReadMode == FLS_XQSPI_READMODE_DMA)
//  {
//    if(((QSPI_DMA_REGS[0]->QSPIDMA_DST_STS) &
//                       FLS_XQSPI_QSPIDMA_DST_STS_BUSY_MASK) != FLS_ONE) {
//      retVal = FALSE;
//    }
//    else {
//      retVal = TRUE;
//    }
//  }
//  else
//  {

  /* No DMA - checking directly RXEMPTY bit which is == 1 when no data has been received since last read */
    if((spiREG5->BUF & 0x80000000) != FLS_ZERO)
    {
      retVal = FALSE;
    }
    else
    {
      retVal = TRUE;
    }
//  }

  return retVal;
}
#define FLS_STOP_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"

#define FLS_START_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"

/*******************************************************************************
** Syntax             : Fls_QSPI_IsBusy                                       **
** [/cover]                                                                   **
**                                                                            **
** Service ID         : NA                                                    **
**                                                                            **
** Sync/Async         : NA                                                    **
**                                                                            **
** Reentrancy         : Non reentrant                                         **
**                                                                            **
** Parameters (in)    : none                                                  **
**                                                                            **
** Parameters (out)   : retVal                                                **
**                                                                            **
** Return value       : uint32                                                **
**                                                                            **
** Description        : To check busy status                                  **
*******************************************************************************/
FUNC(uint32, FLS_CODE_FAST) Fls_QSPI_IsBusy(void)
{
    VAR(uint32, AUTOMATIC) retVal;

    if (QSPI_CMD_NONE == QSPI_RWDev.ulCurrCmd)
    {
        retVal = FALSE;
    }
    else
    {
        retVal = TRUE;
    }

    return retVal;
}
#define FLS_STOP_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"

#define FLS_START_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"
/*******************************************************************************
** Syntax             : Fls_QSPI_CmdHandler                                   **
** [/cover]                                                                   **
**                                                                            **
** Service ID         : NA                                                    **
**                                                                            **
** Sync/Async         : NA                                                    **
**                                                                            **
** Reentrancy         : Non Reentrant                                         **
**                                                                            **
** Parameters (in)    : ulIsErr                                               **
**                                                                            **
** Parameters (out)   : retVal                                                **
**                                                                            **
** Return value       : uint32                                                *
**                                                                            **
** Description        : Flash Command States handler function                 **
*******************************************************************************/
//FUNC(uint32, FLS_CODE_FAST) Fls_QSPI_CmdHandler(VAR(uint32, AUTOMATIC) ulIsErr)
//{
//    VAR(uint32, AUTOMATIC) retVal;
//    VAR(uint32, AUTOMATIC) ulIsTrigAgain;
//    VAR(QSPI_CmdType, AUTOMATIC) ulCurrCmd;
//
//    if (TRUE == ulIsErr)
//    {
//        ulCurrCmd = QSPI_CMD_STOP;
//    }
//    else
//    {
//        ulCurrCmd = QSPI_RWDev.ulCurrCmd;
//    }
//
//    switch(ulCurrCmd)
//    {
//
//    case QSPI_CMD_READ:
//        do
//        {
//            retVal = Fls_QSPI_ReadStateHandler(&ulIsTrigAgain);
//        } while ((TRUE == ulIsTrigAgain) && (ERR_OK == retVal));
//        break;
//
//    case QSPI_CMD_XFER:
//        do
//        {
//            retVal = Fls_QSPI_XferStateHandler(&ulIsTrigAgain);
//        } while ((TRUE == ulIsTrigAgain) && (ERR_OK == retVal));
//        break;
//
//    case QSPI_CMD_STOP:
//        if(QSPI_CMD_READ == QSPI_RWDev.ulCurrCmd)
//        {
//            retVal = Fls_QSPI_ReadStop();
//        }
//        else if (QSPI_CMD_XFER == QSPI_RWDev.ulCurrCmd)
//        {
//            Fls_QSPI_XferStop();
//            retVal = ERR_OK;
//        }
//        else
//        {
//#if (FLS_DEV_ERROR_DETECT == STD_ON)
//            /* triggered with err & wrong cmd */
//            (void) Det_ReportError(FLS_MODULE_ID, FLS_INSTANCE_ID, Fls_ApiId, FLS_E_PARAM_CMD);
//#endif /* FLS_DEV_ERROR_DETECT == STD_ON */
//            retVal = ERR_UNKNOWN;
//        }
//        QSPI_RWDev.ulCurrCmd = QSPI_CMD_NONE;
//        break;
//
//    default:
//#if (FLS_DEV_ERROR_DETECT == STD_ON)
//        /* triggered with err & wrong cmd */
//        (void) Det_ReportError(FLS_MODULE_ID, FLS_INSTANCE_ID, Fls_ApiId, FLS_E_PARAM_CMD);
//#endif /* FLS_DEV_ERROR_DETECT == STD_ON */
//        retVal = ERR_UNKNOWN;
//        break;
//    }
//
//    return retVal;
//}

#define FLS_STOP_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"


#define FLS_START_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"
/*******************************************************************************
** Syntax             : Fls_QSPI_ReadStateHandler                             **
** [/cover]                                                                   **
**                                                                            **
** Service ID         : NA                                                    **
**                                                                            **
** Sync/Async         : NA                                                    **
**                                                                            **
** Reentrancy         : Non Reentrant                                         **
**                                                                            **
** Parameters (in)    : pIsTrigAgain                                          **
**                                                                            **
** Parameters (out)   : retVal                                                **
**                                                                            **
** Return value       : uint32                                                **
**                                                                            **
** Description        : This service handle read operation related states     **
*******************************************************************************/
//static FUNC(uint32, FLS_CODE_FAST) Fls_QSPI_ReadStateHandler(CONSTP2VAR(uint32, AUTOMATIC, FLS_APPL_DATA) pIsTrigAgain)
//{
//    VAR(uint32, AUTOMATIC) retVal;
//
//    *pIsTrigAgain = FALSE;
//
//    switch(QSPI_RWDev.ulCurrCmdState)
//    {
//
//    case QSPI_CMD_READ_STATE_READ_START:
//        QSPI_RWDev.ulCurrCmdState = QSPI_CMD_READ_STATE_READ_STOP;
//        retVal = Fls_QSPI_ReadStart();
//        *pIsTrigAgain = FALSE;
//        break;
//
//    case QSPI_CMD_READ_STATE_READ_DATA:
//        QSPI_RWDev.ulCurrCmdState = QSPI_CMD_READ_STATE_READ_STOP;
//        *pIsTrigAgain = FALSE;
//        retVal = ERR_OK;
//        break;
//
//    case QSPI_CMD_READ_STATE_READ_STOP:
//        retVal = Fls_QSPI_ReadStop();
//        //retVal = ERR_OK;
//        QSPI_RWDev.ulCurrCmd = QSPI_CMD_NONE;
//        *pIsTrigAgain = FALSE;
//        break;
//
//    default:
//        retVal = ERR_INVAL_STATE;
//        break;
//    }
//
//    return retVal;
//}

#define FLS_STOP_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"

#define FLS_START_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"
/*******************************************************************************
** Syntax          : Fls_QSPI_ReadStart                                       **
** [/cover]                                                                   **
**                                                                            **
** Service ID      : NA                                                       **
**                                                                            **
** Sync/Async      : NA                                                       **
**                                                                            **
** Reentrancy      : Non Reentrant                                            **
**                                                                            **
** Parameters (in) : none                                                     **
**                                                                            **
** Parameters (out): retVal                                                   **
**                                                                            **
** Return value    : uint32                                                   **
**                                                                            **
** Description     : To execute read operation                                **
*******************************************************************************/
//static FUNC(uint32, FLS_CODE_FAST) Fls_QSPI_ReadStart(void)
//{
//  VAR(uint32, AUTOMATIC) retVal;
//
//  retVal = Fls_Qspi_PolledTransfer(pQspiInstancePtr, FlashMsg, 1);
//
//  if (retVal == (uint32) FLS_XST_SUCCESS)
//  {
//    retVal =(uint32) ERR_OK;;
//  }
//  else {
//    retVal = (uint32) ERR_TIME_OUT;
//  }
//
//  return retVal;
//}
#define FLS_STOP_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"

#define FLS_START_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"
/*******************************************************************************
** Syntax           : Fls_QSPI_ReadStop                                       **
** [/cover]                                                                   **
**                                                                            **
** Service ID       : NA                                                      **
**                                                                            **
** Sync/Async       : NA                                                      **
**                                                                            **
** Reentrancy       : Non Reentrant                                           **
**                                                                            **
** Parameters (in)  : none                                                    **
**                                                                            **
** Parameters (out) : retVal                                                  **
**                                                                            **
** Return value     : uint32                                                  **
**                                                                            **
** Description      : To stop read operation                                  **
*******************************************************************************/
//static FUNC(uint32, FLS_CODE_FAST) Fls_QSPI_ReadStop(void)
//{
//  VAR(uint32, AUTOMATIC) retVal = E_OK;
//
//  Fls_QSPI_XferStop();
//  return retVal;
//}
#define FLS_STOP_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"


#define FLS_START_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"

/*******************************************************************************
** Syntax            : Fls_Qspi_PolledTransfer                                **
** [/cover]                                                                   **
**                                                                            **
** Service ID        : NA                                                     **
**                                                                            **
** Sync/Async        : NA                                                     **
**                                                                            **
** Reentrancy        : Reentrant                                              **
**                                                                            **
** Parameters (in)   : pInstancePtr,pMsg,ulNumMsg                             **
**                                                                            **
** Parameters (out)  : XST_SUCCESS                                            **
**                                                                            **
** Return value      : uint32                                                 **
**                                                                            **
** Description       : This service actually transfer command and data        **
**                     on QSPI bus                                            **
*******************************************************************************/
//static FUNC(uint32, FLS_CODE_FAST) Fls_Qspi_PolledTransfer(
//        P2VAR(XQspiConfigType, AUTOMATIC, FLS_APPL_DATA) pInstancePtr,
//        P2VAR(Fls_QspiMsgType, AUTOMATIC, FLS_APPL_DATA) pMsg,
//        VAR(uint32, AUTOMATIC) ulNumMsg)
//{
//    VAR(uint32, AUTOMATIC) ulIndex;
//    VAR(uint32, AUTOMATIC) retVal = 0u;
//    VAR(spiDAT1_t, AUTOMATIC) dataconfig1_t;
//
//    dataconfig1_t.CS_HOLD = TRUE;
//    dataconfig1_t.WDEL    = TRUE;
//    dataconfig1_t.DFSEL   = SPI_FMT_0;
//    dataconfig1_t.CSNR    = 0xFE; // SPI5_CS0
//
//    /* * Set the busy flag, which will be cleared when the transfer is entirely
//   done.*/
//    pInstancePtr->pMsg = pMsg;
//    /* Enable */
//    //QSPI_REGS[0]->GQSPI_En_REG |= FLS_XQSPI_EN_MASK;
//    /* Select slave */
//    //Fls_QSPI_GenFifoEntryCSAssert(pInstancePtr);
//    /* list */
//    ulIndex = 0;
//    while (ulIndex < (uint32)ulNumMsg)
//    {
//        /* Transmit */
//        if (((pMsg->ulFlags & FLS_XQSPI_MSG_FLAG_TX) != FALSE) && ((pMsg->ulFlags & FLS_XQSPI_MSG_FLAG_RX) == FALSE))
//        {
//            /* check if pure command or data are expected*/
////            if(NULL_PTR != pMsg->pRxBfrPtr)
////            {
////                retVal = spiTransmitAndReceiveDataByByte(spiREG5, &dataconfig1_t, pMsg->ulByteCount,  (uint8 *)pMsg->pTxBfrPtr, (uint8 *)pMsg->pRxBfrPtr);
////            }
////            else
//            {
//                retVal = spiTransmitDataByByte(spiREG5, &dataconfig1_t, pMsg->ulByteCount,  (uint8 *)pMsg->pTxBfrPtr);
//            }
//        }
//        /*Receive */
//        else if(((pMsg->ulFlags & FLS_XQSPI_MSG_FLAG_RX) != FALSE) && ((pMsg->ulFlags & FLS_XQSPI_MSG_FLAG_TX) == FALSE))
//        {
//            retVal = spiReceiveDataByByte(spiREG5, &dataconfig1_t, pMsg->ulByteCount,  (uint8 *)pMsg->pRxBfrPtr);
//        }
//        /* Dummy and cmd sent by upper layer to received data */
//        else if(((pMsg->ulFlags & FLS_XQSPI_MSG_FLAG_RX) != FALSE) && ((pMsg->ulFlags & FLS_XQSPI_MSG_FLAG_TX) != FALSE))
//        {
//            retVal = spiSendDummyDataByByte(spiREG5, &dataconfig1_t, pMsg->ulByteCount);
//        }
//        else
//        {
//            pInstancePtr->ulTxBytes = 0;
//            pInstancePtr->ulRxBytes = 0;
//            pInstancePtr->pSendBufferPtr = NULL_PTR;
//            pInstancePtr->pRecvBufferPtr = NULL_PTR;
//        }
//        ulIndex++;
//
//    }
//    return FLS_XST_SUCCESS;
//}
#define FLS_STOP_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"


#define FLS_START_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"
/*******************************************************************************
** Syntax             : Fls_QSPI_XferStop                                     **
** [/cover]                                                                   **
**                                                                            **
** Service ID         : NA                                                    **
**                                                                            **
** Sync/Async         : NA                                                    **
**                                                                            **
** Reentrancy         : Non Reentrant                                         **
**                                                                            **
** Parameters (in)    : none                                                  **
**                                                                            **
** Parameters (out)   : none                                                  **
**                                                                            **
** Return value       : void                                                  **
**                                                                            **
** Description        : To stop Transfer operation                            **
*******************************************************************************/

//static FUNC(void, FLS_CODE_FAST) Fls_QSPI_XferStop(void)
//{
//   /* Disable the device. */
//  spiREG5->GCR1 &= ~(QSPI_GCR1_DEVICE_ENABLE);
//}
#define FLS_STOP_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"


#define FLS_START_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"
/*******************************************************************************
** Syntax             :Fls_QSPI_XferStart                                     **
** [/cover]                                                                   **
**                                                                            **
** Service ID         :  NA                                                   **
**                                                                            **
** Sync/Async         :  NA                                                   **
**                                                                            **
** Reentrancy         :  Non Reentrant                                        **
**                                                                            **
** Parameters (in)    : none                                                  **
**                                                                            **
** Parameters (out)   : none                                                  **
**                                                                            **
** Return value       : void                                                  **
**                                                                            **
** Description        : To transfer start command                             **
*******************************************************************************/

//static FUNC(void, FLS_CODE_FAST) Fls_QSPI_XferStart(void)
//{
//
//  VAR(uint32, AUTOMATIC) retVal;
//
//  retVal = Fls_Qspi_PolledTransfer(pQspiInstancePtr, FlashMsg, 1);
//
//  if (retVal == (uint32) FLS_XST_SUCCESS)
//  {
//    retVal = (uint32) ERR_OK;;
//  }
//  else
//  {
//    retVal = (uint32) ERR_TIME_OUT;
//  }
//  FlashMsg[0].ulByteCount = pQspiInstancePtr->ulTxBytes;
//
//}
#define FLS_STOP_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"


#define FLS_START_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"
/*******************************************************************************
* @fn uint32 spiTransmitDataByByte(spiBASE_t *spi, spiDAT1_t *dataconfig_t, uint32 blocksize, uint8 * srcbuff)
*   @brief Transmits Data using polling method
*   @param[in] spi           - Spi module base address
*   @param[in] dataconfig_t    - Spi DAT1 register configuration
*   @param[in] blocksize    - number of data
*   @param[in] srcbuff        - Pointer to the source data ( 8 bit).
*
*   @return flag register value.
*
*   This function transmits blocksize number of data from source buffer using polling method.
*******************************************************************************/
FUNC(uint32, FLS_CODE_FAST) spiTransmitDataByByte(P2VAR(spiBASE_t,AUTOMATIC,FLS_APPL_DATA) spi,
                                                  P2VAR(spiDAT1_t,AUTOMATIC,FLS_APPL_DATA) dataconfig_t,
                                                  VAR(uint32,FLS_APPL_DATA) blocksize,
                                                  P2VAR(uint8,AUTOMATIC,FLS_APPL_DATA) srcbuff)
{
    volatile  VAR(uint32, AUTOMATIC) SpiBuf;
    VAR(uint8, AUTOMATIC) Tx_Data;
    VAR(uint32, AUTOMATIC) Chip_Select_Hold = (dataconfig_t->CS_HOLD) ? 0x10000000U : 0U;
    VAR(uint32, AUTOMATIC) WDelay = (dataconfig_t->WDEL) ? 0x04000000U : 0U;
    VAR(SPIDATAFMT_t, AUTOMATIC) DataFormat = dataconfig_t->DFSEL;
    VAR(uint8, AUTOMATIC) ChipSelect = dataconfig_t->CSNR;

/* USER CODE BEGIN (10) */
/* USER CODE END */

    /* Make sure Data format is 8 bits */
    spi->FMT0 &= ~((uint32)0x1F);
    spi->FMT1 &= ~((uint32)0x1F);
    spi->FMT2 &= ~((uint32)0x1F);
    spi->FMT3 &= ~((uint32)0x1F);

    spi->FMT0 |= ((uint32)0x08);
    spi->FMT1 |= ((uint32)0x08);
    spi->FMT2 |= ((uint32)0x08);
    spi->FMT3 |= ((uint32)0x08);

    /* Make sure device is enabled before use */
    spi->GCR1 |= (QSPI_GCR1_DEVICE_ENABLE);

    while(blocksize != 0U)
    {
        if((spi->FLG & 0x000000FFU) !=0U)
        {
           break;
        }

        if(blocksize == 1U)
        {
           Chip_Select_Hold = 0U;
        }
        /*SAFETYMCUSW 45 D MR:21.1 <APPROVED> "Valid non NULL input parameters are only allowed in this driver" */
        Tx_Data = *srcbuff;

        spi->DAT1 =  ((uint32)DataFormat << 24U) |
                     ((uint32)ChipSelect << 16U) |
                     (WDelay)           |
                     (Chip_Select_Hold) |
                     (uint32)Tx_Data;
        /*SAFETYMCUSW 567 S MR:17.1,17.4 <APPROVED> "Pointer increment needed" */
        srcbuff++;
        /*SAFETYMCUSW 28 D MR:NA <APPROVED> "Hardware status bit read check" */
        while((spi->FLG & 0x00000100U) != 0x00000100U)
        {
        } /* Wait */
        SpiBuf = spi->BUF;

        blocksize--;
    }

/* USER CODE BEGIN (11) */
/* USER CODE END */

    return (spi->FLG & 0xFFU);
}
#define FLS_STOP_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"


#define FLS_START_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"

/*******************************************************************************

* @fn uint32 spiSendDummyDataByByte(spiBASE_t *spi, spiDAT1_t *dataconfig_t, uint32 blocksize)
*   @brief Receives Data using polling method
*   @param[in] spi           - Spi module base address
*   @param[in] dataconfig_t    - Spi DAT1 register configuration
*   @param[in] blocksize    - number of data
*   @param[in] destbuff        - Pointer to the destination data (8 bit).
*
*   @return flag register value.
*
*   This function transmits blocksize number of data from source buffer using polling method.
*******************************************************************************/

FUNC(uint32, FLS_CODE_FAST) spiSendDummyDataByByte(P2VAR(spiBASE_t,AUTOMATIC,FLS_APPL_DATA) spi,
                                                 P2VAR(spiDAT1_t,AUTOMATIC,FLS_APPL_DATA) dataconfig_t,
                                                 VAR(uint32,FLS_APPL_DATA) blocksize)
{
/* USER CODE BEGIN (6) */
/* USER CODE END */
    VAR(uint32, AUTOMATIC) Chip_Select_Hold = (dataconfig_t->CS_HOLD) ? 0x10000000U : 0U;
    VAR(uint32, AUTOMATIC) WDelay = (dataconfig_t->WDEL) ? 0x04000000U : 0U;
    VAR(SPIDATAFMT_t, AUTOMATIC) DataFormat = dataconfig_t->DFSEL;
    VAR(uint8, AUTOMATIC) ChipSelect = dataconfig_t->CSNR;
    VAR(uint8, AUTOMATIC) Dummy;

    /* Make sure Data format is 8 bits */
    spi->FMT0 &= ~((uint32)0x1F);
    spi->FMT1 &= ~((uint32)0x1F);
    spi->FMT2 &= ~((uint32)0x1F);
    spi->FMT3 &= ~((uint32)0x1F);

    spi->FMT0 |= ((uint32)0x08);
    spi->FMT1 |= ((uint32)0x08);
    spi->FMT2 |= ((uint32)0x08);
    spi->FMT3 |= ((uint32)0x08);

    /* Make sure device is enabled before use */
    spi->GCR1 |= (QSPI_GCR1_DEVICE_ENABLE);

    while(blocksize != 0U)
    {
        if((spi->FLG & 0x000000FFU) !=0U)
        {
          break;
        }
        if(blocksize == 1U)
        {
           Chip_Select_Hold = 0U;
        }

        /*SAFETYMCUSW 51 S MR:12.3 <APPROVED> "Needs shifting for 32-bit value" */
        spi->DAT1 = ((uint32)DataFormat << 24U) |
                    ((uint32)ChipSelect << 16U) |
                    (WDelay)            |
                    (Chip_Select_Hold)  |
                    (0x00000000U);
        /*SAFETYMCUSW 28 D MR:NA <APPROVED> "Hardware status bit read check" */
        while((spi->FLG & 0x00000100U) != 0x00000100U)
        {
        } /* Wait */
        /*SAFETYMCUSW 45 D MR:21.1 <APPROVED> "Valid non NULL input parameters are only allowed in this driver" */
        Dummy = (uint8)spi->BUF;

        blocksize--;
    }

/* USER CODE BEGIN (7) */
/* USER CODE END */

    return (spi->FLG & 0xFFU);
}

#define FLS_STOP_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"

#define FLS_START_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"

/*******************************************************************************

* @fn uint32 spiReceiveDataByByte(spiBASE_t *spi, spiDAT1_t *dataconfig_t, uint32 blocksize, uint8 * destbuff)
*   @brief Receives Data using polling method
*   @param[in] spi           - Spi module base address
*   @param[in] dataconfig_t    - Spi DAT1 register configuration
*   @param[in] blocksize    - number of data
*   @param[in] destbuff        - Pointer to the destination data (8 bit).
*
*   @return flag register value.
*
*   This function transmits blocksize number of data from source buffer using polling method.
*******************************************************************************/

FUNC(uint32, FLS_CODE_FAST) spiReceiveDataByByte(P2VAR(spiBASE_t,AUTOMATIC,FLS_APPL_DATA) spi,
                                                 P2VAR(spiDAT1_t,AUTOMATIC,FLS_APPL_DATA) dataconfig_t,
                                                 VAR(uint32,FLS_APPL_DATA) blocksize,
                                                 P2VAR(uint8,AUTOMATIC,FLS_APPL_DATA) destbuff)
{
/* USER CODE BEGIN (6) */
/* USER CODE END */
    VAR(uint32, AUTOMATIC) Chip_Select_Hold = (dataconfig_t->CS_HOLD) ? 0x10000000U : 0U;
    VAR(uint32, AUTOMATIC) WDelay = (dataconfig_t->WDEL) ? 0x04000000U : 0U;
    VAR(SPIDATAFMT_t, AUTOMATIC) DataFormat = dataconfig_t->DFSEL;
    VAR(uint8, AUTOMATIC) ChipSelect = dataconfig_t->CSNR;

    /* Make sure Data format is 8 bits */
    spi->FMT0 &= ~((uint32)0x1F);
    spi->FMT1 &= ~((uint32)0x1F);
    spi->FMT2 &= ~((uint32)0x1F);
    spi->FMT3 &= ~((uint32)0x1F);

    spi->FMT0 |= ((uint32)0x08);
    spi->FMT1 |= ((uint32)0x08);
    spi->FMT2 |= ((uint32)0x08);
    spi->FMT3 |= ((uint32)0x08);

    /* Make sure device is enabled before use */
    spi->GCR1 |= (QSPI_GCR1_DEVICE_ENABLE);

    while(blocksize != 0U)
    {
        if((spi->FLG & 0x000000FFU) !=0U)
        {
          break;
        }
        if(blocksize == 1U)
        {
           Chip_Select_Hold = 0U;
        }

        /*SAFETYMCUSW 51 S MR:12.3 <APPROVED> "Needs shifting for 32-bit value" */
        spi->DAT1 = ((uint32)DataFormat << 24U) |
                    ((uint32)ChipSelect << 16U) |
                    (WDelay)            |
                    (Chip_Select_Hold)  |
                    (0x00000000U);
        /*SAFETYMCUSW 28 D MR:NA <APPROVED> "Hardware status bit read check" */
        while((spi->FLG & 0x00000100U) != 0x00000100U)
        {
        } /* Wait */
        /*SAFETYMCUSW 45 D MR:21.1 <APPROVED> "Valid non NULL input parameters are only allowed in this driver" */
        *destbuff = (uint8)spi->BUF;
        /*SAFETYMCUSW 567 S MR:17.1,17.4 <APPROVED> "Pointer increment needed" */
        destbuff++;
        blocksize--;
    }

/* USER CODE BEGIN (7) */
/* USER CODE END */

    return (spi->FLG & 0xFFU);
}

#define FLS_STOP_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"

#define FLS_START_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"
/** @fn uint32 spiTransmitAndReceiveDataByByte(spiBASE_t *spi, spiDAT1_t *dataconfig_t, uint32 blocksize, uint8 * srcbuff, uint8 * destbuff)
*   @brief Transmits and Receive Data using polling method
*   @param[in] spi           - Spi module base address
*   @param[in] dataconfig_t    - Spi DAT1 register configuration
*   @param[in] blocksize    - number of data
*   @param[in] srcbuff        - Pointer to the source data ( 16 bit).
*   @param[in] destbuff        - Pointer to the destination data ( 16 bit).
*
*   @return flag register value.
*
*   This function transmits and receives blocksize number of data from source buffer using polling method.
*/
/* SourceId : SPI_SourceId_007 */
/* DesignId : SPI_DesignId_009 */
/* Requirements : HL_SR135 */

FUNC(uint32, FLS_CODE_FAST )spiTransmitAndReceiveDataByByte(P2VAR(spiBASE_t,AUTOMATIC,FLS_APPL_DATA) spi,
                                                            P2VAR(spiDAT1_t,AUTOMATIC,FLS_APPL_DATA) dataconfig_t,
                                                            VAR(uint32,FLS_APPL_DATA) blocksize,
                                                            P2VAR(uint8,AUTOMATIC,FLS_APPL_DATA) srcbuff,
                                                            P2VAR(uint8,AUTOMATIC,FLS_APPL_DATA) destbuff,
                                                            VAR(uint32,FLS_APPL_DATA) numRxBytesToDiscardBeforeRealRxData)
{
    VAR(uint8, AUTOMATIC) Tx_Data;
    VAR(uint32, AUTOMATIC) Chip_Select_Hold = (dataconfig_t->CS_HOLD) ? 0x10000000U : 0U;
    VAR(uint32, AUTOMATIC) WDelay = (dataconfig_t->WDEL) ? 0x04000000U : 0U;
    VAR(SPIDATAFMT_t, AUTOMATIC) DataFormat = dataconfig_t->DFSEL;
    VAR(uint8, AUTOMATIC) ChipSelect = dataconfig_t->CSNR;
    VAR(uint32, AUTOMATIC) ByteDiscardCntr = 0;
    VAR(uint8, AUTOMATIC) ByteDiscarded = 0;

    /* USER CODE BEGIN (14) */
    /* Make sure Data format is 8 bits */
    spi->FMT0 &= ~((uint32)0x1F);
    spi->FMT1 &= ~((uint32)0x1F);
    spi->FMT2 &= ~((uint32)0x1F);
    spi->FMT3 &= ~((uint32)0x1F);

    spi->FMT0 |= ((uint32)0x08);
    spi->FMT1 |= ((uint32)0x08);
    spi->FMT2 |= ((uint32)0x08);
    spi->FMT3 |= ((uint32)0x08);

    /* Make sure device is enabled before use */
    spi->GCR1 |= (QSPI_GCR1_DEVICE_ENABLE);

    /* USER CODE END */
    while(blocksize != 0U)
    {
        if((spi->FLG & 0x000000FFU) != 0U)
        {
            break;
        }

        if(blocksize == 1U)
        {
            Chip_Select_Hold = 0U;
        }
        /*SAFETYMCUSW 45 D MR:21.1 <APPROVED> "Valid non NULL input parameters are only allowed in this driver" */
        Tx_Data = *srcbuff;

        spi->DAT1 =((uint32)DataFormat << 24U) |
                ((uint32)ChipSelect << 16U) |
                (WDelay)           |
                (Chip_Select_Hold) |
                (uint32)Tx_Data;
        /*SAFETYMCUSW 567 S MR:17.1,17.4 <APPROVED> "Pointer increment needed" */
        srcbuff++;
        /*SAFETYMCUSW 28 D MR:NA <APPROVED> "Hardware status bit read check" */
        while((spi->FLG & 0x00000100U) != 0x00000100U);/* Wait */

        if(ByteDiscardCntr >= numRxBytesToDiscardBeforeRealRxData)
        {
            /*SAFETYMCUSW 45 D MR:21.1 <APPROVED> "Valid non NULL input parameters are only allowed in this driver" */
            *destbuff = (uint8)spi->BUF;
            /*SAFETYMCUSW 567 S MR:17.1,17.4 <APPROVED> "Pointer increment needed" */
            destbuff++;
        }
        else
        {
            //Need to read the byte in the receive buffer.
            ByteDiscarded = (uint8)spi->BUF;
            ByteDiscardCntr++;
        }

        blocksize--;
    }

    /* USER CODE BEGIN (15) */
    /* USER CODE END */

    return (spi->FLG & 0xFFU);
}
#define FLS_STOP_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"

#define FLS_START_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"
/*******************************************************************************
** Syntax            : Fls_QSPI_Read                                          **
** [/cover]                                                                   **
**                                                                            **
** Service ID        : NA                                                     **
**                                                                            **
** Sync/Async        : NA                                                     **
**                                                                            **
** Reentrancy        : Non reentrant                                          **
**                                                                            **
** Parameters (in)   : ulAddr,pData,ulLen                                     **
**                                                                            **
** Parameters (out)  : retVal                                                 **
**                                                                            **
** Return value      : uint32                                                 **
**                                                                            **
** Description       : To start read operation                                **
*******************************************************************************/
//FUNC(uint32, FLS_CODE_FAST) Fls_QSPI_Read(VAR(uint32, AUTOMATIC) ulAddr, P2VAR(uint8, AUTOMATIC, FLS_APPL_DATA) pData, VAR(uint32, AUTOMATIC) ulLen)
//{
//  VAR(uint32, AUTOMATIC) retVal;
//  VAR(uint32, AUTOMATIC) ulIsTrigAgain;
//
//  /* Set command info */
//  (void)memset(&(QSPI_RWDev.cmdInfo), 0x0,(uint32)sizeof(QSPI_CmdInfoType));
//
//  QSPI_RWDev.cmdInfo.pCmdRxData = pData;
//  QSPI_RWDev.cmdInfo.ulCmdAddr = ulAddr;
//  QSPI_RWDev.cmdInfo.ulCmdLen = ulLen;
//
//  /* Set command & command states */
//  QSPI_RWDev.ulCurrCmdState = QSPI_CMD_READ_STATE_READ_START;
//  QSPI_RWDev.ulCurrCmd = QSPI_CMD_READ;
//
//  FlashMsg[0].pTxBfrPtr = NULL_PTR;
//  FlashMsg[0].pRxBfrPtr = pData;
//  FlashMsg[0].ulByteCount = ulLen;
//  FlashMsg[0].ulBusWidth = pQspiInstancePtr->Config.ucBusWidth;
//  FlashMsg[0].ulFlags = FLS_XQSPI_MSG_FLAG_RX;
//
//  if(pQspiInstancePtr->ulBusConnectionMode == FLS_XQSPI_CONNECTION_MODE_PARALLEL)
//  {
//    FlashMsg[0].ulFlags |= FLS_XQSPI_MSG_FLAG_STRIPE;
//  }
//
//  do
//  {
//    retVal = Fls_QSPI_ReadStateHandler(&ulIsTrigAgain);
//  } while ((TRUE == ulIsTrigAgain) && (retVal == ERR_OK));
//
//  return retVal;
//}
#define FLS_STOP_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"


#define FLS_START_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"
/*******************************************************************************
** Syntax             : Fls_SPI_ReadFlsDeviceReg                              **
** [/cover]                                                                   **
**                                                                            **
** Service ID         : NA                                                    **
**                                                                            **
** Sync/Async         : NA                                                    **
**                                                                            **
** Reentrancy         : Non Reentrant                                         **
**                                                                            **
** Parameters (in)    : pTxData,pRxData,ulLen                                 **
**                                                                            **
** Parameters (out)   : retVal                                                **
**                                                                            **
** Return value       : uint32                                                **
**                                                                            **
** Description        : To transfer Data on bus                               **
*******************************************************************************/
FUNC(uint32, FLS_CODE_FAST) Fls_SPI_ReadFlsDeviceReg(
                           P2CONST(uint8, AUTOMATIC, FLS_APPL_DATA) pTxData,
                           P2VAR(uint8, AUTOMATIC, FLS_APPL_DATA) pRxData,
                           VAR(uint32, AUTOMATIC) ulLen)
{
    VAR(uint32, AUTOMATIC) retVal;
    VAR(spiDAT1_t, AUTOMATIC) dataconfig1_t;

    dataconfig1_t.CS_HOLD = TRUE;
    dataconfig1_t.WDEL    = TRUE;
    dataconfig1_t.DFSEL   = SPI_FMT_0;
    dataconfig1_t.CSNR    = 0xFE; // SPI5_CS0

    retVal = spiTransmitAndReceiveDataByByte(spiREG5, &dataconfig1_t, ulLen,  (uint8 *)pTxData, (uint8 *)pRxData,1);

    return retVal;
}
#define FLS_STOP_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"

#define FLS_START_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"
/*******************************************************************************
** Syntax             : Fls_SPI_SendCmdFlsDevice                              **
** [/cover]                                                                   **
**                                                                            **
** Service ID         : NA                                                    **
**                                                                            **
** Sync/Async         : NA                                                    **
**                                                                            **
** Reentrancy         : Non Reentrant                                         **
**                                                                            **
** Parameters (in)    : pTxData,ulLen                                         **
**                                                                            **
** Parameters (out)   : retVal                                                **
**                                                                            **
** Return value       : uint32                                                **
**                                                                            **
** Description        : To transfer Data on bus                               **
*******************************************************************************/
FUNC(uint32, FLS_CODE_FAST) Fls_SPI_SendCmdFlsDevice(
                           P2CONST(uint8, AUTOMATIC, FLS_APPL_DATA) pTxData,
                           VAR(uint32, AUTOMATIC) ulLen)
{
    VAR(uint32, AUTOMATIC) retVal;
    VAR(spiDAT1_t, AUTOMATIC) dataconfig1_t;

    dataconfig1_t.CS_HOLD = TRUE;
    dataconfig1_t.WDEL    = TRUE;
    dataconfig1_t.DFSEL   = SPI_FMT_0;
    dataconfig1_t.CSNR    = 0xFE; // SPI5_CS0

    retVal = spiTransmitDataByByte(spiREG5, &dataconfig1_t, ulLen,  (uint8 *)pTxData);

    return retVal;
}
#define FLS_STOP_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"

#define FLS_START_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"
FUNC(uint32, FLS_CODE_FAST)FLS_SPI_ProcessFastReadCmd(P2CONST(uint8, AUTOMATIC, FLS_APPL_DATA) pTxData,
                                                      P2VAR(uint8, AUTOMATIC, FLS_APPL_DATA) pRxData,
                                                      VAR(uint32, AUTOMATIC) ulLen)
{
    VAR(uint32, AUTOMATIC) retVal;
    VAR(uint8, AUTOMATIC) idx;
    VAR(uint8, AUTOMATIC) TxBuff[FLS_XQSPI_TXD_RXD_DEPTH];
    VAR(spiDAT1_t, AUTOMATIC) dataconfig1_t;

    dataconfig1_t.CS_HOLD = TRUE;
    dataconfig1_t.WDEL    = TRUE;
    dataconfig1_t.DFSEL   = SPI_FMT_0;
    dataconfig1_t.CSNR    = 0xFE; // SPI5_CS0

    (void)memset(&TxBuff[0],0xFF,sizeof(TxBuff));
    /* Copy command to Tx buffer */
    for(idx = 0; idx < 6; idx++)
    {
        TxBuff[idx] = (uint8)*pTxData;
        pTxData++;
    }

    retVal = spiTransmitAndReceiveDataByByte(spiREG5, &dataconfig1_t, ulLen+6,  TxBuff, pRxData, 6);

    return(retVal);

}
#define FLS_STOP_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"


#define FLS_START_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"
FUNC(uint32, FLS_CODE_FAST)FLS_SPI_ProcessWriteCmd(P2CONST(uint8, AUTOMATIC, FLS_APPL_DATA) pTxCmdBuff,
                                                   P2CONST(uint8, AUTOMATIC, FLS_APPL_DATA) pTxData,
                                                      VAR(uint32, AUTOMATIC) ulLenDataToWrite)
{
    VAR(uint32, AUTOMATIC) retVal;
    VAR(uint8, AUTOMATIC) TxBuff[FLS_XQSPI_TXD_RXD_DEPTH];
    VAR(spiDAT1_t, AUTOMATIC) dataconfig1_t;

    dataconfig1_t.CS_HOLD = TRUE;
    dataconfig1_t.WDEL    = TRUE;
    dataconfig1_t.DFSEL   = SPI_FMT_0;
    dataconfig1_t.CSNR    = 0xFE; // SPI5_CS0

    if(pTxCmdBuff != NULL)
    {
        /* Copy command into tx buffer */
        (void)memcpy(&TxBuff[0],pTxCmdBuff,5);

        /* Copy data into tx buffer */
        /* If NULL simple command - No data to transfer */
        if(pTxData != NULL)
        {
            (void)memcpy(&TxBuff[5],pTxData,ulLenDataToWrite);
            retVal = spiTransmitDataByByte(spiREG5, &dataconfig1_t, ulLenDataToWrite+5,  TxBuff);
        }
        else
        {
            retVal = spiTransmitDataByByte(spiREG5, &dataconfig1_t, 5,  TxBuff);
        }
    }

    return(retVal);

}
#define FLS_STOP_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"


#define FLS_START_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"
/*******************************************************************************
** Syntax             : Fls_QSPI_Xfer                                         **
** [/cover]                                                                   **
**                                                                            **
** Service ID         : NA                                                    **
**                                                                            **
** Sync/Async         : NA                                                    **
**                                                                            **
** Reentrancy         : Non Reentrant                                         **
**                                                                            **
** Parameters (in)    : pTxData,pRxData,ulLen,ulFlag                          **
**                                                                            **
** Parameters (out)   : retVal                                                **
**                                                                            **
** Return value       : uint32                                                **
**                                                                            **
** Description        : To transfer Data on bus                               **
*******************************************************************************/
//FUNC(uint32, FLS_CODE_FAST) Fls_QSPI_Xfer(
//                           P2CONST(uint8, AUTOMATIC, FLS_APPL_DATA) pTxData,
//                           CONSTP2VAR(uint8, AUTOMATIC, FLS_APPL_DATA) pRxData,
//                           VAR(uint32, AUTOMATIC) ulLen,
//                           VAR(QSPI_XferFlagType, AUTOMATIC) ulFlag,
//                           VAR(uint8, AUTOMATIC) isCmd)
//{
//  VAR(uint32, AUTOMATIC) retVal;
//  VAR(uint32, AUTOMATIC) ulIsTrigAgain = FALSE;
//  P2CONST(uint8, AUTOMATIC, FLS_APPL_DATA) ucTxDataValue;
//
//  /* Set command info */
//
//  (void)memset(&(QSPI_RWDev.cmdInfo), 0x0,(uint32)sizeof(QSPI_CmdInfoType));
//
//  QSPI_RWDev.cmdInfo.pCmdTxData = pTxData;
//  QSPI_RWDev.cmdInfo.pCmdRxData = pRxData;
//  QSPI_RWDev.cmdInfo.ulCmdLen = ulLen;
//  QSPI_RWDev.cmdInfo.ulCmdFlag = ulFlag;
//  QSPI_RWDev.cmdInfo.ulTmpTxLen = ulLen;
//  QSPI_RWDev.cmdInfo.ulTmpTxLenRem = ulLen;
//  QSPI_RWDev.cmdInfo.ulTmpRxLen = ulLen;
//  QSPI_RWDev.cmdInfo.ulTmpRxLenRem = ulLen;
//  QSPI_RWDev.cmdInfo.pTmpTxData = pTxData;
//  QSPI_RWDev.cmdInfo.pTmpRxData = pRxData;
//
//  /* Set command & command states */
//  QSPI_RWDev.ulCurrCmdState = QSPI_CMD_XFER_STATE_XFER_START;
//  QSPI_RWDev.ulCurrCmd = QSPI_CMD_XFER;
//
//  ucTxDataValue = pTxData;
//  FlashMsg[0].pTxBfrPtr = ucTxDataValue;
//  FlashMsg[0].pRxBfrPtr = NULL_PTR;
//  FlashMsg[0].ulByteCount = QSPI_RWDev.cmdInfo.ulTmpTxLen;
//
//   /* For Macronix send commands in SPI Mode */
//  if (Fls_MemConfig.flsID == MACRONIX_FLASH)
//  {
//    FlashMsg[0].ulBusWidth = FLS_XQSPI_SELECT_MODE_SPI;
//  }
//  else
//  {
//    FlashMsg[0].ulBusWidth = pQspiInstancePtr->Config.ucBusWidth;
//  }
//  if(pTxData == NULL_PTR)
//  {
//    FlashMsg[0].ulFlags = 0;
//    /* Dummy bytes should be sent in same mode as the read */
//    FlashMsg[0].ulBusWidth = pQspiInstancePtr->Config.ucBusWidth;
//  }
//  else
//  {
//    FlashMsg[0].ulFlags = FLS_XQSPI_MSG_FLAG_TX;
//  }
//  if(pQspiInstancePtr->ulBusConnectionMode == FLS_XQSPI_CONNECTION_MODE_PARALLEL)
//  {
//    /* Use Stripe mode if not sending a command */
//    if (!isCmd)
//    {
//      FlashMsg[0].ulFlags |= FLS_XQSPI_MSG_FLAG_STRIPE;
//    }
//  }
//  do {
//    retVal = Fls_QSPI_XferStateHandler(&ulIsTrigAgain);
//  } while((TRUE == ulIsTrigAgain) && (ERR_OK == retVal));
//
//  return retVal;
//}
#define FLS_STOP_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"

#define FLS_START_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"
/*******************************************************************************
** Syntax             : Fls_QSPI_XferStateHandler                             **
** [/cover]                                                                   **
**                                                                            **
** Service ID         : NA                                                    **
**                                                                            **
** Sync/Async         : NA                                                    **
**                                                                            **
** Reentrancy         : Non Reentrant                                         **
**                                                                            **
** Parameters (in)    : pIsTrigAgain                                          **
**                                                                            **
** Parameters (out)   : retVal                                                **
**                                                                            **
** Return value       : uint32                                                **
**                                                                            **
** Description        : This service handle different data Transfer states    **
*******************************************************************************/
//static FUNC(uint32, FLS_CODE_FAST) Fls_QSPI_XferStateHandler(CONSTP2VAR(uint32, AUTOMATIC, FLS_APPL_DATA) pIsTrigAgain)
//{
//    VAR(uint32, AUTOMATIC) retVal;
//
//    *pIsTrigAgain = FALSE;
//
//    switch(QSPI_RWDev.ulCurrCmdState) {
//
//    case QSPI_CMD_XFER_STATE_XFER_START:
//        /* Identify xfer length */
////        if (FLS_XQSPI_TXD_DEPTH >= QSPI_RWDev.cmdInfo.ulTmpTxLenRem)
////        {
////            QSPI_RWDev.cmdInfo.ulTmpTxLen = QSPI_RWDev.cmdInfo.ulTmpTxLenRem;
////            QSPI_RWDev.cmdInfo.ulTmpTxLenRem = 0UL;
////        }
////        else
////        {
////            QSPI_RWDev.cmdInfo.ulTmpTxLen = FLS_XQSPI_TXD_DEPTH;
////            QSPI_RWDev.cmdInfo.ulTmpTxLenRem -= FLS_XQSPI_TXD_DEPTH;
////        }
//
//        // No Fifo in TI controller so transfer whatever length is required
//
//        FlashMsg[0].ulByteCount = QSPI_RWDev.cmdInfo.ulTmpTxLen;
//        QSPI_RWDev.cmdInfo.ulTmpTxLenRem = 0UL;
//
//        /* Set xfer flag */
//        if (QSPI_XFER_CONTINUE == QSPI_RWDev.cmdInfo.ulCmdFlag)
//        {
//            QSPI_RWDev.cmdInfo.ulTmpFlag = QSPI_XFER_CONTINUE;
//        }
//        else
//        {
//            if (0UL == QSPI_RWDev.cmdInfo.ulTmpTxLenRem)
//            {
//                /* Last Xfer */
//                QSPI_RWDev.cmdInfo.ulTmpFlag = QSPI_XFER_STOP;
//            }
//            else
//            {
//                /* Xfer to continue */
//                QSPI_RWDev.cmdInfo.ulTmpFlag = QSPI_XFER_CONTINUE;
//            }
//        }
//        /* Update state to next */
//        QSPI_RWDev.ulCurrCmdState = QSPI_CMD_XFER_STATE_READ_DATA;
//        Fls_QSPI_XferStart();
//        retVal = ERR_OK;
//        break;
//
//    case QSPI_CMD_XFER_STATE_READ_DATA: /* case 11: */
//        if (0UL == QSPI_RWDev.cmdInfo.ulTmpTxLenRem)
//        {
//            /* Data Transfer complete */
//            QSPI_RWDev.ulCurrCmdState = QSPI_CMD_XFER_STATE_XFER_STOP;
//            if(QSPI_RWDev.cmdInfo.pCmdRxData != NULL_PTR)
//            {
//                FlashMsg[0].pTxBfrPtr = NULL_PTR;
//                FlashMsg[0].pRxBfrPtr = QSPI_RWDev.cmdInfo.pCmdRxData;
//                FlashMsg[0].ulByteCount = QSPI_RWDev.cmdInfo.ulTmpTxLen;
//
////                if (Fls_MemConfig.flsID == MACRONIX_FLASH)
////                {
////                    FlashMsg[0].ulBusWidth = FLS_XQSPI_GENFIFO_MODE_SPI;
////                }
////                else
////                {
////                    FlashMsg[0].ulBusWidth = pQspiInstancePtr->Config.ucBusWidth;
////                }
//                FlashMsg[0].ulBusWidth = pQspiInstancePtr->Config.ucBusWidth;
//
//                FlashMsg[0].ulFlags = FLS_XQSPI_MSG_FLAG_RX;
//                if(pQspiInstancePtr->ulBusConnectionMode == FLS_XQSPI_CONNECTION_MODE_PARALLEL)
//                {
//                    FlashMsg[0].ulFlags |= FLS_XQSPI_MSG_FLAG_STRIPE;
//                }
//                retVal = Fls_Qspi_PolledTransfer(pQspiInstancePtr,FlashMsg, 1);
//                if(retVal == (uint32) FLS_XST_SUCCESS)
//                {
//                    retVal = (uint32) ERR_OK;
//                }
//                else
//                {
//                    retVal = (uint32) ERR_TIME_OUT;
//                }
//                *pIsTrigAgain = FALSE;
//            }
//            else
//            {
//                *pIsTrigAgain = TRUE;
//                retVal = ERR_OK;
//            }
//        }
//        else
//        {
//            /* Transfer more data */
//            *pIsTrigAgain = TRUE;
//            retVal = ERR_OK;
//            QSPI_RWDev.ulCurrCmdState = QSPI_CMD_XFER_STATE_XFER_START;
//        }
//
//        break;
//    case QSPI_CMD_XFER_STATE_XFER_STOP:
//        if (QSPI_XFER_CONTINUE != QSPI_RWDev.cmdInfo.ulCmdFlag)
//        {
//            Fls_QSPI_XferStop();
//        }
//        QSPI_RWDev.ulCurrCmd = QSPI_CMD_NONE;
//        retVal = ERR_OK;
//        break;
//    default:
//        /* Shall never reach here */
//        retVal = ERR_INVAL_STATE;
//        break;
//    }
//
//    return retVal;
//}
#define FLS_STOP_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"

