#include <stdint.h>
#include <stdbool.h>  
#include "SPIBUS.h"

#ifdef USE_SPI1
#include "mcc_generated_files/spi1.h"  
#endif
#ifdef USE_SPI2
#include "mcc_generated_files/spi2.h"  
#endif
#ifdef USE_SPI3
#include "mcc_generated_files/spi3.h"  
#endif
#ifdef USE_SPI4
#include "mcc_generated_files/spi4.h"  
#endif

extern volatile uint16_t tick;

uint8_t SPIWriteRead(SPIBUS_t * pSPIObj);

#ifdef USE_SPI1
static uint16_t dataSentCount_1;
static uint16_t dataReceivedCount_1;
static uint16_t MaxSize_1; //uint may byte, may word
#endif

#ifdef USE_SPI2
static uint16_t dataSentCount_2;
static uint16_t dataReceivedCount_2;
static uint16_t MaxSize_2; //uint may byte, may word
#endif


#ifdef USE_SPI3
static uint16_t dataSentCount_3;
static uint16_t dataReceivedCount_3;
static uint16_t MaxSize_3; //uint may byte, may word
#endif

#ifdef USE_SPI4
static uint16_t dataSentCount_4;
static uint16_t dataReceivedCount_4;
static uint16_t MaxSize_4; //uint may byte, may word
#endif


//SPI_Poll_t POLL_InMain = 0;
SPI_Poll_t POLL_InMain ={0,0,0,0};
#ifdef USE_SPI1 
uint8_t LowerLayer_spi_ReturnResult1 = 1; //means continue
#endif
#ifdef USE_SPI2 
uint8_t LowerLayer_spi_ReturnResult2 = 1; //means continue
#endif
#ifdef USE_SPI3 
uint8_t LowerLayer_spi_ReturnResult3 = 1; //means continue
#endif
#ifdef USE_SPI4 
uint8_t LowerLayer_spi_ReturnResult4 = 1; //means continue
#endif

uint8_t SPIWriteRead(SPIBUS_t * pSPIObj) {
    uint8_t retR;
    switch (pSPIObj->SPI_Peripheral_No) {
        case 1:
#ifdef USE_SPI1
            if ((1 == LowerLayer_spi_ReturnResult1)&&(0 == POLL_InMain.SPI1_Polling)) //let main loop call forever
            {
                POLL_InMain.SPI1_Polling = 1;
                LowerLayer_spi_ReturnResult1 = SPIWriteRead_lowLayer(pSPIObj);
                if ((0 == LowerLayer_spi_ReturnResult1) || (-1 == LowerLayer_spi_ReturnResult1)) {
                    //success or fail
                    POLL_InMain.SPI1_Polling = 0; //stop main loop call forever
                    retR = LowerLayer_spi_ReturnResult1;
                    LowerLayer_spi_ReturnResult1 = 1; //for next time call me : both LowerLayer_spi_ReturnResult1==1 and POLL_InMain==0
                    return retR;
                }
            }
            retR = LowerLayer_spi_ReturnResult1;
            if ((0 == LowerLayer_spi_ReturnResult1) || (-1 == LowerLayer_spi_ReturnResult1)) {
                //success or fail                
                LowerLayer_spi_ReturnResult1 = 1; //for next time call me : both LowerLayer_spi_ReturnResult1==1 and POLL_InMain==0
            }
            return retR;
#else
            return -1; //error
#endif
            break;
        case 2:
#ifdef USE_SPI2
            if ((1 == LowerLayer_spi_ReturnResult2)&&(0 == POLL_InMain.SPI2_Polling)) //let main loop call forever
            {
                POLL_InMain.SPI2_Polling = 1;
                LowerLayer_spi_ReturnResult2 = SPIWriteRead_lowLayer(pSPIObj);
                if ((0 == LowerLayer_spi_ReturnResult2) || (-1 == LowerLayer_spi_ReturnResult2)) {
                    //success or fail
                    POLL_InMain.SPI2_Polling = 0; //stop main loop call forever
                    retR = LowerLayer_spi_ReturnResult2;
                    LowerLayer_spi_ReturnResult2 = 1; //for next time call me : both LowerLayer_spi_ReturnResult2==1 and POLL_InMain==0
                    return retR;
                }
            }
            retR = LowerLayer_spi_ReturnResult2;
            if ((0 == LowerLayer_spi_ReturnResult2) || (-1 == LowerLayer_spi_ReturnResult2)) {
                //success or fail                
                LowerLayer_spi_ReturnResult2 = 1; //for next time call me : both LowerLayer_spi_ReturnResult2==1 and POLL_InMain==0
            }
            return retR;
#else
            return -1; //error
#endif
            break;
        case 3:
#ifdef USE_SPI3
            if ((1 == LowerLayer_spi_ReturnResult3)&&(0 == POLL_InMain.SPI3_Polling)) //let main loop call forever
            {
                POLL_InMain.SPI3_Polling = 1;
                LowerLayer_spi_ReturnResult3 = SPIWriteRead_lowLayer(pSPIObj);
                if ((0 == LowerLayer_spi_ReturnResult3) || (-1 == LowerLayer_spi_ReturnResult3)) {
                    //success or fail
                    POLL_InMain.SPI3_Polling = 0; //stop main loop call forever
                    retR = LowerLayer_spi_ReturnResult3;
                    LowerLayer_spi_ReturnResult3 = 1; //for next time call me : both LowerLayer_spi_ReturnResult3==1 and POLL_InMain==0
                    return retR;
                }
            }
            retR = LowerLayer_spi_ReturnResult3;
            if ((0 == LowerLayer_spi_ReturnResult3) || (-1 == LowerLayer_spi_ReturnResult3)) {
                //success or fail
                LowerLayer_spi_ReturnResult3 = 1; //for next time call me : both LowerLayer_spi_ReturnResult3==1 and POLL_InMain==0
            }
            return retR;
#else
            return -1; //error
#endif
            break;
        case 4:
#ifdef USE_SPI4
            if ((1 == LowerLayer_spi_ReturnResult4)&&(0 == POLL_InMain.SPI4_Polling)) //let main loop call forever
            {
                POLL_InMain.SPI4_Polling = 1;
                LowerLayer_spi_ReturnResult4 = SPIWriteRead_lowLayer(pSPIObj);
                if ((0 == LowerLayer_spi_ReturnResult4) || (-1 == LowerLayer_spi_ReturnResult4)) {
                    //success or fail
                    POLL_InMain.SPI4_Polling = 0; //stop main loop call forever
                    retR = LowerLayer_spi_ReturnResult4;
                    LowerLayer_spi_ReturnResult4 = 1; //for next time call me : both LowerLayer_spi_ReturnResult4==1 and POLL_InMain==0
                    return retR;
                }
            }
            retR = LowerLayer_spi_ReturnResult4;
            if ((0 == LowerLayer_spi_ReturnResult4) || (-1 == LowerLayer_spi_ReturnResult4)) {
                //success or fail
                LowerLayer_spi_ReturnResult4 = 1; //for next time call me : both LowerLayer_spi_ReturnResult4==1 and POLL_InMain==0
            }
            return retR;
#else
            return -1; //error
#endif            
    }
    return 1;
}

/**
 * SPI Write-only/ Read-only/ Write and read   Function
 * @param pI2CObj --- I2C Bus object
 * 
 * @return 
 * 0: Success
 * 1: Continue
 * negative: fail
 */
uint8_t SPIWriteRead_lowLayer(SPIBUS_t * pSPIObj) {
    int16_t i;
    switch (pSPIObj->state) {
        case 0:
            //prepare all spi ans transmit Data including dummy data, 
            //Actually Transmit and receiver Array is equal size.
            //So Actually we defined equal size array in pSPIObj
            if (1 == pSPIObj->SPI_Peripheral_No) {
#ifdef USE_SPI1      
                dataSentCount_1 = 0;
                dataReceivedCount_1 = 0;
                MaxSize_1 = (pSPIObj->QTY4Read[pSPIObj->PresentSeqNo] > pSPIObj->QTY4Write[pSPIObj->PresentSeqNo]) ? pSPIObj->QTY4Read[pSPIObj->PresentSeqNo] : pSPIObj->QTY4Write[pSPIObj->PresentSeqNo];
                if (pSPIObj->Use16bitsBus) {
                    //16 bits
                    if (pSPIObj->QTY4Write[pSPIObj->PresentSeqNo] < MaxSize_1) {
                        if (pSPIObj->DummyDataAllOne & (1U << pSPIObj->PresentSeqNo)) {
                            //All 1 for dummy data
                            for (i = pSPIObj->QTY4Write[pSPIObj->PresentSeqNo]; i < MaxSize_1; i++) {
                                ((uint16_t * *) pSPIObj->pWriteData)[pSPIObj->PresentSeqNo][i] = 0xFFFFU;
                            }
                        } else {
                            //All 0 for dummy data
                            for (i = pSPIObj->QTY4Write[pSPIObj->PresentSeqNo]; i < MaxSize_1; i++) {
                                ((uint16_t * *) pSPIObj->pWriteData)[pSPIObj->PresentSeqNo][i] = 0x0;
                            }
                        }
                        //Transmit---16 bits bus
                        pSPIObj->set_CS[pSPIObj->PresentSeqNo](true);
                        //(pSPIObj->set_CS + pSPIObj->PresentSeqNo)(true);
                        //We changed for better compatible----Jan 22 2025
                        //#if !defined(__dsPIC33C__)
                        #if defined(SPI1STAT)
                        while (SPI1STATbits.SPITBF != true) //because 8 level FIFO, so it has chance more data transmitting
                        {
                            if (dataSentCount_1 < MaxSize_1) {
                                SPI1BUF = ((uint16_t * *) pSPIObj->pWriteData)[pSPIObj->PresentSeqNo][dataSentCount_1++];
                            } else {
                                break;
                            }
                            if (SPI1STATbits.SRXMPT == false) {

                                ((uint16_t * *) pSPIObj->pReadData)[pSPIObj->PresentSeqNo][dataReceivedCount_1++] = SPI1BUF;
                            }
                        }
                        #else
                        while (SPI1STATLbits.SPITBF != true) //because 8 level FIFO, so it has chance more data transmitting
                        {
                            if (dataSentCount_1 < MaxSize_1) {
                                SPI1BUFL = ((uint16_t * *) pSPIObj->pWriteData)[pSPIObj->PresentSeqNo][dataSentCount_1++];
                            } else {
                                break;
                            }
                            if (SPI1STATLbits.SPIRBE == false) {

                                ((uint16_t * *) pSPIObj->pReadData)[pSPIObj->PresentSeqNo][dataReceivedCount_1++] = SPI1BUFL;
                            }
                        }
                        #endif

                    }
                } else {
                    //8 bits
                    if (pSPIObj->QTY4Write[pSPIObj->PresentSeqNo] < MaxSize_1) {
                        if (pSPIObj->DummyDataAllOne & (1U << pSPIObj->PresentSeqNo)) {
                            //All 1 for dummy data
                            for (i = pSPIObj->QTY4Write[pSPIObj->PresentSeqNo]; i < MaxSize_1; i++) {
                                pSPIObj->pWriteData[pSPIObj->PresentSeqNo][i] = 0xFF;
                            }
                        } else {
                            //All 0 for dummy data
                            for (i = pSPIObj->QTY4Write[pSPIObj->PresentSeqNo]; i < MaxSize_1; i++) {
                                pSPIObj->pWriteData[pSPIObj->PresentSeqNo][i] = 0x0;
                            }
                        }
                    }
                    //transmit ---8 bits bus
                    (pSPIObj->set_CS[pSPIObj->PresentSeqNo])(true);
                    //(pSPIObj->set_CS + pSPIObj->PresentSeqNo)(true);
                    //We changed for better compatible----Jan 22 2025
                    //#if !defined(__dsPIC33C__)
                    #if defined(SPI1STAT)
                    while (SPI1STATbits.SPITBF != true) //because 18 level FIFO, so it has chance more data transmitting
                    {
                        if (dataSentCount_1 < MaxSize_1) {
                            SPI1BUF = pSPIObj->pWriteData[pSPIObj->PresentSeqNo][dataSentCount_1++];
                        } else {
                            break;
                        }
                        if (SPI1STATbits.SRXMPT == false) {

                            pSPIObj->pReadData[pSPIObj->PresentSeqNo][dataReceivedCount_1++] = SPI1BUF;
                        }
                    }
                    #else
                    while (SPI1STATLbits.SPITBF != true) //because 18 level FIFO, so it has chance more data transmitting
                    {
                        if (dataSentCount_1 < MaxSize_1) {
                            SPI1BUFL = pSPIObj->pWriteData[pSPIObj->PresentSeqNo][dataSentCount_1++];
                        } else {
                            break;
                        }
                        if (SPI1STATLbits.SPIRBE == false) {

                            pSPIObj->pReadData[pSPIObj->PresentSeqNo][dataReceivedCount_1++] = SPI1BUFL;
                        }
                    }
                    #endif
                }
                if ((MaxSize_1 == dataSentCount_1) && (MaxSize_1 == dataReceivedCount_1)) {
                    //done all transmit/receive
                    pSPIObj->set_CS[pSPIObj->PresentSeqNo](false);
                    //(pSPIObj->set_CS + pSPIObj->PresentSeqNo)(false);
                    return 0; //success
                }
                pSPIObj->state++;
                return 1;
#else
                return -1;
#endif
            } else if (2 == pSPIObj->SPI_Peripheral_No) {
#ifdef USE_SPI2   
                dataSentCount_2 = 0;
                dataReceivedCount_2 = 0;
                MaxSize_2 = (pSPIObj->QTY4Read[pSPIObj->PresentSeqNo] > pSPIObj->QTY4Write[pSPIObj->PresentSeqNo]) ? pSPIObj->QTY4Read[pSPIObj->PresentSeqNo] : pSPIObj->QTY4Write[pSPIObj->PresentSeqNo];
                if (pSPIObj->Use16bitsBus) {
                    //16 bits
                    if (pSPIObj->QTY4Write[pSPIObj->PresentSeqNo] < MaxSize_2) {
                        if (pSPIObj->DummyDataAllOne & (1U << pSPIObj->PresentSeqNo)) {
                            //All 1 for dummy data
                            for (i = pSPIObj->QTY4Write[pSPIObj->PresentSeqNo]; i < MaxSize_2; i++) {
                                ((uint16_t * *) pSPIObj->pWriteData)[pSPIObj->PresentSeqNo][i] = 0xFFFFU;
                            }
                        } else {
                            //All 0 for dummy data
                            for (i = pSPIObj->QTY4Write[pSPIObj->PresentSeqNo]; i < MaxSize_2; i++) {
                                ((uint16_t * *) pSPIObj->pWriteData)[pSPIObj->PresentSeqNo][i] = 0x0;
                            }
                        }
                        //Transmit---16 bits bus
                        pSPIObj->set_CS[pSPIObj->PresentSeqNo](true);
                        //(pSPIObj->set_CS + pSPIObj->PresentSeqNo)(true);
                        //We changed for better compatible----Jan 22 2025
                        //#if !defined(__dsPIC33C__)
                        #if defined(SPI2STAT)
                        while (SPI2STATbits.SPITBF != true) //because 8 level FIFO, so it has chance more data transmitting
                        {
                            if (dataSentCount_2 < MaxSize_2) {
                                SPI2BUF = ((uint16_t * *) pSPIObj->pWriteData)[pSPIObj->PresentSeqNo][dataSentCount_2++];
                            } else {
                                break;
                            }
                            if (SPI2STATbits.SRXMPT == false) {

                                ((uint16_t * *) pSPIObj->pReadData)[pSPIObj->PresentSeqNo][dataReceivedCount_2++] = SPI2BUF;
                            }
                        }
                        #else
                        while (SPI2STATLbits.SPITBF != true) //because 8 level FIFO, so it has chance more data transmitting
                        {
                            if (dataSentCount_2 < MaxSize_2) {
                                SPI2BUFL = ((uint16_t * *) pSPIObj->pWriteData)[pSPIObj->PresentSeqNo][dataSentCount_2++];
                            } else {
                                break;
                            }
                            if (SPI2STATLbits.SPIRBE == false) {

                                ((uint16_t * *) pSPIObj->pReadData)[pSPIObj->PresentSeqNo][dataReceivedCount_2++] = SPI2BUFL;
                            }
                        }
                        #endif


                    }
                } else {
                    //8 bits
                    if (pSPIObj->QTY4Write[pSPIObj->PresentSeqNo] < MaxSize_2) {
                        if (pSPIObj->DummyDataAllOne & (1U << pSPIObj->PresentSeqNo)) {
                            //All 1 for dummy data
                            for (i = pSPIObj->QTY4Write[pSPIObj->PresentSeqNo]; i < MaxSize_2; i++) {
                                pSPIObj->pWriteData[pSPIObj->PresentSeqNo][i] = 0xFF;
                            }
                        } else {
                            //All 0 for dummy data
                            for (i = pSPIObj->QTY4Write[pSPIObj->PresentSeqNo]; i < MaxSize_2; i++) {
                                pSPIObj->pWriteData[pSPIObj->PresentSeqNo][i] = 0x0;
                            }
                        }
                    }
                    //transmit ---8 bits bus
                    pSPIObj->set_CS[pSPIObj->PresentSeqNo](true);
                    //(pSPIObj->set_CS + pSPIObj->PresentSeqNo)(true);
                    //We changed for better compatible----Jan 22 2025
                    //#if !defined(__dsPIC33C__)
                    #if defined(SPI2STAT)
                    while (SPI2STATbits.SPITBF != true) //because 18 level FIFO, so it has chance more data transmitting
                    {
                        if (dataSentCount_2 < MaxSize_2) {
                            SPI2BUF = pSPIObj->pWriteData[pSPIObj->PresentSeqNo][dataSentCount_2++];
                        } else {
                            break;
                        }
                        if (SPI2STATbits.SRXMPT == false) {

                            pSPIObj->pReadData[pSPIObj->PresentSeqNo][dataReceivedCount_2++] = SPI2BUF;
                        }
                    }
                    #else
                    while (SPI2STATLbits.SPITBF != true) //because 18 level FIFO, so it has chance more data transmitting
                    {
                        if (dataSentCount_2 < MaxSize_2) {
                            SPI2BUFL = pSPIObj->pWriteData[pSPIObj->PresentSeqNo][dataSentCount_2++];
                        } else {
                            break;
                        }
                        if (SPI2STATLbits.SPIRBE == false) {

                            pSPIObj->pReadData[pSPIObj->PresentSeqNo][dataReceivedCount_2++] = SPI2BUFL;
                        }
                    }
                    #endif
                }
                if ((MaxSize_2 == dataSentCount_2) && (MaxSize_2 == dataReceivedCount_2)) {
                    //done all transmit/receive
                    //pSPIObj->set_CS[pSPIObj->PresentSeqNo](false);
                    (pSPIObj->set_CS + pSPIObj->PresentSeqNo)(false);
                    return 0; //success
                }
                pSPIObj->state++;
                return 1;
#else
                return -1;
#endif
            } else if (3 == pSPIObj->SPI_Peripheral_No) {
#ifdef USE_SPI3   
                dataSentCount_3 = 0;
                dataReceivedCount_3 = 0;
                MaxSize_3 = (pSPIObj->QTY4Read[pSPIObj->PresentSeqNo] > pSPIObj->QTY4Write[pSPIObj->PresentSeqNo]) ? pSPIObj->QTY4Read[pSPIObj->PresentSeqNo] : pSPIObj->QTY4Write[pSPIObj->PresentSeqNo];
                if (pSPIObj->Use16bitsBus) {
                    //16 bits
                    if (pSPIObj->QTY4Write[pSPIObj->PresentSeqNo] < MaxSize_3) {
                        if (pSPIObj->DummyDataAllOne & (1U << pSPIObj->PresentSeqNo)) {
                            //All 1 for dummy data
                            for (i = pSPIObj->QTY4Write[pSPIObj->PresentSeqNo]; i < MaxSize_3; i++) {
                                ((uint16_t * *) pSPIObj->pWriteData)[pSPIObj->PresentSeqNo][i] = 0xFFFFU;
                            }
                        } else {
                            //All 0 for dummy data
                            for (i = pSPIObj->QTY4Write[pSPIObj->PresentSeqNo]; i < MaxSize_3; i++) {
                                ((uint16_t * *) pSPIObj->pWriteData)[pSPIObj->PresentSeqNo][i] = 0x0;
                            }
                        }
                        //Transmit---16 bits bus
                        pSPIObj->set_CS[pSPIObj->PresentSeqNo](true);
                        //(pSPIObj->set_CS + pSPIObj->PresentSeqNo)(true);
                        //We changed for better compatible----Jan 22 2025
                        //#if !defined(__dsPIC33C__)
                        #if defined(SPI3STAT)
                        while (SPI3STATbits.SPITBF != true) //because 8 level FIFO, so it has chance more data transmitting
                        {
                            if (dataSentCount_3 < MaxSize_3) {
                                SPI3BUF = ((uint16_t * *) pSPIObj->pWriteData)[pSPIObj->PresentSeqNo][dataSentCount_3++];
                            } else {
                                break;
                            }
                            if (SPI3STATbits.SRXMPT == false) {

                                ((uint16_t * *) pSPIObj->pReadData)[pSPIObj->PresentSeqNo][dataReceivedCount_3++] = SPI3BUF;
                            }
                        }
                        #else
                        while (SPI3STATLbits.SPITBF != true) //because 8 level FIFO, so it has chance more data transmitting
                        {
                            if (dataSentCount_3 < MaxSize_3) {
                                SPI3BUFL = ((uint16_t * *) pSPIObj->pWriteData)[pSPIObj->PresentSeqNo][dataSentCount_3++];
                            } else {
                                break;
                            }
                            if (SPI3STATLbits.SPIRBE == false) {

                                ((uint16_t * *) pSPIObj->pReadData)[pSPIObj->PresentSeqNo][dataReceivedCount_3++] = SPI3BUFL;
                            }
                        }
                        #endif


                    }
                } else {
                    //8 bits
                    if (pSPIObj->QTY4Write[pSPIObj->PresentSeqNo] < MaxSize_3) {
                        if (pSPIObj->DummyDataAllOne & (1U << pSPIObj->PresentSeqNo)) {
                            //All 1 for dummy data
                            for (i = pSPIObj->QTY4Write[pSPIObj->PresentSeqNo]; i < MaxSize_3; i++) {
                                pSPIObj->pWriteData[pSPIObj->PresentSeqNo][i] = 0xFF;
                            }
                        } else {
                            //All 0 for dummy data
                            for (i = pSPIObj->QTY4Write[pSPIObj->PresentSeqNo]; i < MaxSize_3; i++) {
                                pSPIObj->pWriteData[pSPIObj->PresentSeqNo][i] = 0x0;
                            }
                        }
                    }
                    //transmit ---8 bits bus
                    pSPIObj->set_CS[pSPIObj->PresentSeqNo](true);
                    //(pSPIObj->set_CS + pSPIObj->PresentSeqNo)(true);
                    //We changed for better compatible----Jan 22 2025
                    //#if !defined(__dsPIC33C__)
                    #if defined(SPI3STAT)
                    while (SPI3STATbits.SPITBF != true) //because 18 level FIFO, so it has chance more data transmitting
                    {
                        if (dataSentCount_3 < MaxSize_3) {
                            SPI3BUF = pSPIObj->pWriteData[pSPIObj->PresentSeqNo][dataSentCount_3++];
                        } else {
                            break;
                        }
                        if (SPI3STATbits.SRXMPT == false) {

                            pSPIObj->pReadData[pSPIObj->PresentSeqNo][dataReceivedCount_3++] = SPI3BUF;
                        }
                    }
                    #else
                    while (SPI3STATLbits.SPITBF != true) //because 18 level FIFO, so it has chance more data transmitting
                    {
                        if (dataSentCount_3 < MaxSize_3) {
                            SPI3BUFL = pSPIObj->pWriteData[pSPIObj->PresentSeqNo][dataSentCount_3++];
                        } else {
                            break;
                        }
                        if (SPI3STATLbits.SPIRBE == false) {

                            pSPIObj->pReadData[pSPIObj->PresentSeqNo][dataReceivedCount_3++] = SPI3BUFL;
                        }
                    }
                    #endif
                }
                if ((MaxSize_3 == dataSentCount_3) && (MaxSize_3 == dataReceivedCount_3)) {
                    //done all transmit/receive
                    //pSPIObj->set_CS[pSPIObj->PresentSeqNo](false);
                    (pSPIObj->set_CS + pSPIObj->PresentSeqNo)(false);
                    return 0; //success
                }
                pSPIObj->state++;
                return 1;
#else
                return -1;
#endif
            } else if (4 == pSPIObj->SPI_Peripheral_No) {
#ifdef USE_SPI4 
                dataSentCount_4 = 0;
                dataReceivedCount_4 = 0;
                MaxSize_4 = (pSPIObj->QTY4Read[pSPIObj->PresentSeqNo] > pSPIObj->QTY4Write[pSPIObj->PresentSeqNo]) ? pSPIObj->QTY4Read[pSPIObj->PresentSeqNo] : pSPIObj->QTY4Write[pSPIObj->PresentSeqNo];
                if (pSPIObj->Use16bitsBus) {
                    //16 bits
                    if (pSPIObj->QTY4Write[pSPIObj->PresentSeqNo] < MaxSize_4) {
                        if (pSPIObj->DummyDataAllOne & (1U << pSPIObj->PresentSeqNo)) {
                            //All 1 for dummy data
                            for (i = pSPIObj->QTY4Write[pSPIObj->PresentSeqNo]; i < MaxSize_4; i++) {
                                ((uint16_t * *) pSPIObj->pWriteData)[pSPIObj->PresentSeqNo][i] = 0xFFFFU;
                            }
                        } else {
                            //All 0 for dummy data
                            for (i = pSPIObj->QTY4Write[pSPIObj->PresentSeqNo]; i < MaxSize_4; i++) {
                                ((uint16_t * *) pSPIObj->pWriteData)[pSPIObj->PresentSeqNo][i] = 0x0;
                            }
                        }
                        //Transmit---16 bits bus
                        pSPIObj->set_CS[pSPIObj->PresentSeqNo](true);
                        //(pSPIObj->set_CS + pSPIObj->PresentSeqNo)(true);
                        //We changed for better compatible----Jan 22 2025
                        //#if !defined(__dsPIC33C__)
                        #if defined(SPI4STAT)
                        while (SPI4STATbits.SPITBF != true) //because 8 level FIFO, so it has chance more data transmitting
                        {
                            if (dataSentCount_4 < MaxSize_4) {
                                SPI4BUF = ((uint16_t * *) pSPIObj->pWriteData)[pSPIObj->PresentSeqNo][dataSentCount_4++];
                            } else {
                                break;
                            }
                            if (SPI4STATbits.SRXMPT == false) {

                                ((uint16_t * *) pSPIObj->pReadData)[pSPIObj->PresentSeqNo][dataReceivedCount_4++] = SPI4BUF;
                            }
                        }
                        #else
                        while (SPI4STATLbits.SPITBF != true) //because 8 level FIFO, so it has chance more data transmitting
                        {
                            if (dataSentCount_4 < MaxSize_4) {
                                SPI4BUFL = ((uint16_t * *) pSPIObj->pWriteData)[pSPIObj->PresentSeqNo][dataSentCount_4++];
                            } else {
                                break;
                            }
                            if (SPI4STATLbits.SPIRBE == false) {

                                ((uint16_t * *) pSPIObj->pReadData)[pSPIObj->PresentSeqNo][dataReceivedCount_4++] = SPI4BUFL;
                            }
                        }
                        #endif


                    }
                } else {
                    //8 bits
                    if (pSPIObj->QTY4Write[pSPIObj->PresentSeqNo] < MaxSize_4) {
                        if (pSPIObj->DummyDataAllOne & (1U << pSPIObj->PresentSeqNo)) {
                            //All 1 for dummy data
                            for (i = pSPIObj->QTY4Write[pSPIObj->PresentSeqNo]; i < MaxSize_4; i++) {
                                pSPIObj->pWriteData[pSPIObj->PresentSeqNo][i] = 0xFF;
                            }
                        } else {
                            //All 0 for dummy data
                            for (i = pSPIObj->QTY4Write[pSPIObj->PresentSeqNo]; i < MaxSize_4; i++) {
                                pSPIObj->pWriteData[pSPIObj->PresentSeqNo][i] = 0x0;
                            }
                        }
                    }
                    //transmit ---8 bits bus
                    pSPIObj->set_CS[pSPIObj->PresentSeqNo](true);
                    //(pSPIObj->set_CS + pSPIObj->PresentSeqNo)(true);
                    //We changed for better compatible----Jan 22 2025
                    //#if !defined(__dsPIC33C__)
                    #if defined(SPI4STAT)
                    while (SPI4STATbits.SPITBF != true) //because 18 level FIFO, so it has chance more data transmitting
                    {
                        if (dataSentCount_4 < MaxSize_4) {
                            SPI4BUF = pSPIObj->pWriteData[pSPIObj->PresentSeqNo][dataSentCount_4++];
                        } else {
                            break;
                        }
                        if (SPI4STATbits.SRXMPT == false) {

                            pSPIObj->pReadData[pSPIObj->PresentSeqNo][dataReceivedCount_4++] = SPI4BUF;
                        }
                    }
                    #else
                    while (SPI4STATLbits.SPITBF != true) //because 18 level FIFO, so it has chance more data transmitting
                    {
                        if (dataSentCount_4 < MaxSize_4) {
                            SPI4BUFL = pSPIObj->pWriteData[pSPIObj->PresentSeqNo][dataSentCount_4++];
                        } else {
                            break;
                        }
                        if (SPI4STATLbits.SPIRBE == false) {

                            pSPIObj->pReadData[pSPIObj->PresentSeqNo][dataReceivedCount_4++] = SPI4BUFL;
                        }
                    }
                    #endif
                }
                if ((MaxSize_4 == dataSentCount_4) && (MaxSize_4 == dataReceivedCount_4)) {
                    //done all transmit/receive
                    //pSPIObj->set_CS[pSPIObj->PresentSeqNo](false);
                    (pSPIObj->set_CS + pSPIObj->PresentSeqNo)(false);
                    return 0; //success
                }
                pSPIObj->state++;
                return 1;
#else                
                return -1;
#endif
            }
            break;
        case 1:
            //Transmit/receiving continuously
            if (1 == pSPIObj->SPI_Peripheral_No) {
#ifdef USE_SPI1                
                if (pSPIObj->Use16bitsBus) {
                    //16 bits
                    //Transmit---16 bits bus
                    //We changed for better compatible----Jan 22 2025
                    //#if !defined(__dsPIC33C__)
                    #if defined(SPI1STAT)
                    while (SPI1STATbits.SPITBF != true) //because 8 level FIFO, so it has chance more data transmitting
                    {
                        //First receive is better, in case overflow
                        while (SPI1STATbits.SRXMPT == false) {

                            ((uint16_t * *) pSPIObj->pReadData)[pSPIObj->PresentSeqNo][dataReceivedCount_1++] = SPI1BUF;
                        }
                        if (dataSentCount_1 < MaxSize_1) {
                            SPI1BUF = ((uint16_t * *) pSPIObj->pWriteData)[pSPIObj->PresentSeqNo][dataSentCount_1++];
                        } else {
                            break;
                        }
                        //                        if (SPI1STATbits.SRXMPT == false) {
                        //
                        //                            ((uint16_t * *) pSPIObj->pReadData)[pSPIObj->PresentSeqNo][dataReceivedCount_1++] = SPI1BUF;
                        //                        }
                    }                    
                    if (MaxSize_1 == dataSentCount_1) {
                        while (dataReceivedCount_1 < MaxSize_1) {
                            if (SPI1STATbits.SRXMPT == false) {
                                ((uint16_t * *) pSPIObj->pReadData)[pSPIObj->PresentSeqNo][dataReceivedCount_1++] = SPI1BUF;
                            }
                        }
                        pSPIObj->set_CS[pSPIObj->PresentSeqNo](false);
                        //(pSPIObj->set_CS + pSPIObj->PresentSeqNo)(false);
                        pSPIObj->state = 0;
                        return 0; //success
                    }
                    #else
                    while (SPI1STATLbits.SPITBF != true) //because 8 level FIFO, so it has chance more data transmitting
                    {
                        //First receive is better, in case overflow
                        while (SPI1STATLbits.SPIRBE == false) {

                            ((uint16_t * *) pSPIObj->pReadData)[pSPIObj->PresentSeqNo][dataReceivedCount_1++] = SPI1BUFL;
                        }
                        if (dataSentCount_1 < MaxSize_1) {
                            SPI1BUFL = ((uint16_t * *) pSPIObj->pWriteData)[pSPIObj->PresentSeqNo][dataSentCount_1++];
                        } else {
                            break;
                        }
                        //                        if (SPI1STATbits.SRXMPT == false) {
                        //
                        //                            ((uint16_t * *) pSPIObj->pReadData)[pSPIObj->PresentSeqNo][dataReceivedCount_1++] = SPI1BUF;
                        //                        }
                    }                    
                    if (MaxSize_1 == dataSentCount_1) {
                        while (dataReceivedCount_1 < MaxSize_1) {
                            if (SPI1STATLbits.SPIRBE == false) {
                                ((uint16_t * *) pSPIObj->pReadData)[pSPIObj->PresentSeqNo][dataReceivedCount_1++] = SPI1BUFL;
                            }
                        }
                        pSPIObj->set_CS[pSPIObj->PresentSeqNo](false);
                        //(pSPIObj->set_CS + pSPIObj->PresentSeqNo)(false);
                        pSPIObj->state = 0;
                        return 0; //success
                    }
                    #endif

                } else {
                    //8 bits
                    //transmit ---8 bits bus
                    //We changed for better compatible----Jan 22 2025
                    //#if !defined(__dsPIC33C__)
                    #if defined(SPI1STAT)
                    while (SPI1STATbits.SPITBF != true) //because 18 level FIFO, so it has chance more data transmitting
                    {
                        //First receive is better, in case overflow
                        while (SPI1STATbits.SRXMPT == false) {

                            pSPIObj->pReadData[pSPIObj->PresentSeqNo][dataReceivedCount_1++] = SPI1BUF;
                        }
                        if (dataSentCount_1 < MaxSize_1) {
                            SPI1BUF = pSPIObj->pWriteData[pSPIObj->PresentSeqNo][dataSentCount_1++];
                        } else {
                            break;
                        }
                        //                        if (SPI1STATbits.SRXMPT == false) {
                        //
                        //                            pSPIObj->pReadData[pSPIObj->PresentSeqNo][dataReceivedCount_1++] = SPI1BUF;
                        //                        }
                    }
                    if (MaxSize_1 == dataSentCount_1) {
                        while (dataReceivedCount_1 < MaxSize_1) {
                            if (SPI1STATbits.SRXMPT == false) {
                                pSPIObj->pReadData[pSPIObj->PresentSeqNo][dataReceivedCount_1++] = SPI1BUF;
                            }
                        }
                        pSPIObj->set_CS[pSPIObj->PresentSeqNo](false);
                        //(pSPIObj->set_CS + pSPIObj->PresentSeqNo)(false);
                        pSPIObj->state = 0;
                        return 0; //success
                    }
                    #else
                    while (SPI1STATLbits.SPITBF != true) //because 18 level FIFO, so it has chance more data transmitting
                    {
                        //First receive is better, in case overflow
                        while (SPI1STATLbits.SPIRBE == false) {

                            pSPIObj->pReadData[pSPIObj->PresentSeqNo][dataReceivedCount_1++] = SPI1BUFL;
                        }
                        if (dataSentCount_1 < MaxSize_1) {
                            SPI1BUFL = pSPIObj->pWriteData[pSPIObj->PresentSeqNo][dataSentCount_1++];
                        } else {
                            break;
                        }
                        //                        if (SPI1STATbits.SRXMPT == false) {
                        //
                        //                            pSPIObj->pReadData[pSPIObj->PresentSeqNo][dataReceivedCount_1++] = SPI1BUF;
                        //                        }
                    }
                    if (MaxSize_1 == dataSentCount_1) {
                        while (dataReceivedCount_1 < MaxSize_1) {
                            if (SPI1STATLbits.SPIRBE == false) {
                                pSPIObj->pReadData[pSPIObj->PresentSeqNo][dataReceivedCount_1++] = SPI1BUFL;
                            }
                        }
                        pSPIObj->set_CS[pSPIObj->PresentSeqNo](false);
                        //(pSPIObj->set_CS + pSPIObj->PresentSeqNo)(false);
                        pSPIObj->state = 0;
                        return 0; //success
                    }
                    #endif
                }
                return 1; //continue
#else
                pSPIObj->state = 0;
                return -1; //Fail
#endif
            } else if (2 == pSPIObj->SPI_Peripheral_No) {
#ifdef USE_SPI2
                if (pSPIObj->Use16bitsBus) {
                    //16 bits
                    //Transmit---16 bits bus
                    //We changed for better compatible----Jan 22 2025
                    //#if !defined(__dsPIC33C__)
                    #if defined(SPI2STAT)
                    while (SPI2STATbits.SPITBF != true) //because 8 level FIFO, so it has chance more data transmitting
                    {
                        //First receive is better, in case overflow
                        while (SPI2STATbits.SRXMPT == false) {

                            ((uint16_t * *) pSPIObj->pReadData)[pSPIObj->PresentSeqNo][dataReceivedCount_2++] = SPI2BUF;
                        }
                        if (dataSentCount_2 < MaxSize_2) {
                            SPI2BUF = ((uint16_t * *) pSPIObj->pWriteData)[pSPIObj->PresentSeqNo][dataSentCount_2++];
                        } else {
                            break;
                        }
                        //                        if (SPI2STATbits.SRXMPT == false) {
                        //
                        //                            ((uint16_t * *) pSPIObj->pReadData)[pSPIObj->PresentSeqNo][dataReceivedCount_2++] = SPI2BUF;
                        //                        }
                    }
                    if (MaxSize_2 == dataSentCount_2) {
                        while (dataReceivedCount_2 < MaxSize_2) {
                            if (SPI2STATbits.SRXMPT == false) {
                                ((uint16_t * *) pSPIObj->pReadData)[pSPIObj->PresentSeqNo][dataReceivedCount_2++] = SPI2BUF;
                            }
                        }
                        pSPIObj->set_CS[pSPIObj->PresentSeqNo](false);
                        //(pSPIObj->set_CS + pSPIObj->PresentSeqNo)(false);
                        pSPIObj->state = 0;
                        return 0; //success
                    }
                    #else
                    while (SPI2STATLbits.SPITBF != true) //because 8 level FIFO, so it has chance more data transmitting
                    {
                        //First receive is better, in case overflow
                        while (SPI2STATLbits.SPIRBE == false) {

                            ((uint16_t * *) pSPIObj->pReadData)[pSPIObj->PresentSeqNo][dataReceivedCount_2++] = SPI2BUFL;
                        }
                        if (dataSentCount_2 < MaxSize_2) {
                            SPI2BUFL = ((uint16_t * *) pSPIObj->pWriteData)[pSPIObj->PresentSeqNo][dataSentCount_2++];
                        } else {
                            break;
                        }
                        //                        if (SPI2STATbits.SRXMPT == false) {
                        //
                        //                            ((uint16_t * *) pSPIObj->pReadData)[pSPIObj->PresentSeqNo][dataReceivedCount_2++] = SPI2BUF;
                        //                        }
                    }
                    if (MaxSize_2 == dataSentCount_2) {
                        while (dataReceivedCount_2 < MaxSize_2) {
                            if (SPI2STATLbits.SPIRBE == false) {
                                ((uint16_t * *) pSPIObj->pReadData)[pSPIObj->PresentSeqNo][dataReceivedCount_2++] = SPI2BUFL;
                            }
                        }
                        pSPIObj->set_CS[pSPIObj->PresentSeqNo](false);
                        //(pSPIObj->set_CS + pSPIObj->PresentSeqNo)(false);
                        pSPIObj->state = 0;
                        return 0; //success
                    }
                    #endif

                } else {
                    //8 bits
                    //transmit ---8 bits bus
                    //We changed for better compatible----Jan 22 2025
                    //#if !defined(__dsPIC33C__)
                    #if defined(SPI2STAT)
                    while (SPI2STATbits.SPITBF != true) //because 18 level FIFO, so it has chance more data transmitting
                    {
                        //First receive is better, in case overflow
                        while (SPI2STATbits.SRXMPT == false) {

                            pSPIObj->pReadData[pSPIObj->PresentSeqNo][dataReceivedCount_2++] = SPI2BUF;
                        }
                        if (dataSentCount_2 < MaxSize_2) {
                            SPI2BUF = pSPIObj->pWriteData[pSPIObj->PresentSeqNo][dataSentCount_2++];
                        } else {
                            break;
                        }
                        //                        if (SPI2STATbits.SRXMPT == false) {
                        //
                        //                            pSPIObj->pReadData[pSPIObj->PresentSeqNo][dataReceivedCount_2++] = SPI2BUF;
                        //                        }
                    }
                    if (MaxSize_2 == dataSentCount_2) {
                        while (dataReceivedCount_2 < MaxSize_2) {
                            if (SPI2STATbits.SRXMPT == false) {
                                pSPIObj->pReadData[pSPIObj->PresentSeqNo][dataReceivedCount_2++] = SPI2BUF;
                            }
                        }
                        pSPIObj->set_CS[pSPIObj->PresentSeqNo](false);
                        //(pSPIObj->set_CS + pSPIObj->PresentSeqNo)(false);
                        pSPIObj->state = 0;
                        return 0; //success
                    }
                    #else
                    while (SPI2STATLbits.SPITBF != true) //because 18 level FIFO, so it has chance more data transmitting
                    {
                        //First receive is better, in case overflow
                        while (SPI2STATLbits.SPIRBE == false) {

                            pSPIObj->pReadData[pSPIObj->PresentSeqNo][dataReceivedCount_2++] = SPI2BUFL;
                        }
                        if (dataSentCount_2 < MaxSize_2) {
                            SPI2BUFL = pSPIObj->pWriteData[pSPIObj->PresentSeqNo][dataSentCount_2++];
                        } else {
                            break;
                        }
                        //                        if (SPI2STATbits.SRXMPT == false) {
                        //
                        //                            pSPIObj->pReadData[pSPIObj->PresentSeqNo][dataReceivedCount_2++] = SPI2BUF;
                        //                        }
                    }
                    if (MaxSize_2 == dataSentCount_2) {
                        while (dataReceivedCount_2 < MaxSize_2) {
                            if (SPI2STATLbits.SPIRBE == false) {
                                pSPIObj->pReadData[pSPIObj->PresentSeqNo][dataReceivedCount_2++] = SPI2BUFL;
                            }
                        }
                        pSPIObj->set_CS[pSPIObj->PresentSeqNo](false);
                        //(pSPIObj->set_CS + pSPIObj->PresentSeqNo)(false);
                        pSPIObj->state = 0;
                        return 0; //success
                    }
                    #endif
                }
                return 1; //continue
#else
                pSPIObj->state = 0;
                return -1; //Fail
#endif                
            } else if (3 == pSPIObj->SPI_Peripheral_No) {
#ifdef USE_SPI3
                if (pSPIObj->Use16bitsBus) {
                    //16 bits
                    //Transmit---16 bits bus
                    //We changed for better compatible----Jan 22 2025
                    //#if !defined(__dsPIC33C__)
                    #if defined(SPI3STAT)
                    while (SPI3STATbits.SPITBF != true) //because 8 level FIFO, so it has chance more data transmitting
                    {
                        //First receive is better, in case overflow
                        while (SPI3STATbits.SRXMPT == false) {

                            ((uint16_t * *) pSPIObj->pReadData)[pSPIObj->PresentSeqNo][dataReceivedCount_3++] = SPI3BUF;
                        }
                        if (dataSentCount_3 < MaxSize_3) {
                            SPI3BUF = ((uint16_t * *) pSPIObj->pWriteData)[pSPIObj->PresentSeqNo][dataSentCount_3++];
                        } else {
                            break;
                        }
                        //                        if (SPI3STATbits.SRXMPT == false) {
                        //
                        //                            ((uint16_t * *) pSPIObj->pReadData)[pSPIObj->PresentSeqNo][dataReceivedCount_3++] = SPI3BUF;
                        //                        }
                    }
                    if (MaxSize_3 == dataSentCount_3) {
                        while (dataReceivedCount_3 < MaxSize_3) {
                            if (SPI3STATbits.SRXMPT == false) {
                                ((uint16_t * *) pSPIObj->pReadData)[pSPIObj->PresentSeqNo][dataReceivedCount_3++] = SPI3BUF;
                            }
                        }
                        pSPIObj->set_CS[pSPIObj->PresentSeqNo](false);
                        //(pSPIObj->set_CS + pSPIObj->PresentSeqNo)(false);
                        pSPIObj->state = 0;
                        return 0; //success
                    }
                    #else
                    while (SPI3STATLbits.SPITBF != true) //because 8 level FIFO, so it has chance more data transmitting
                    {
                        //First receive is better, in case overflow
                        while (SPI3STATLbits.SPIRBE == false) {

                            ((uint16_t * *) pSPIObj->pReadData)[pSPIObj->PresentSeqNo][dataReceivedCount_3++] = SPI3BUFL;
                        }
                        if (dataSentCount_3 < MaxSize_3) {
                            SPI3BUFL = ((uint16_t * *) pSPIObj->pWriteData)[pSPIObj->PresentSeqNo][dataSentCount_3++];
                        } else {
                            break;
                        }
                        //                        if (SPI3STATbits.SRXMPT == false) {
                        //
                        //                            ((uint16_t * *) pSPIObj->pReadData)[pSPIObj->PresentSeqNo][dataReceivedCount_3++] = SPI3BUF;
                        //                        }
                    }
                    if (MaxSize_3 == dataSentCount_3) {
                        while (dataReceivedCount_3 < MaxSize_3) {
                            if (SPI3STATLbits.SPIRBE == false) {
                                ((uint16_t * *) pSPIObj->pReadData)[pSPIObj->PresentSeqNo][dataReceivedCount_3++] = SPI3BUFL;
                            }
                        }
                        pSPIObj->set_CS[pSPIObj->PresentSeqNo](false);
                        //(pSPIObj->set_CS + pSPIObj->PresentSeqNo)(false);
                        pSPIObj->state = 0;
                        return 0; //success
                    }
                    #endif

                } else {
                    //8 bits
                    //transmit ---8 bits bus
                    //We changed for better compatible----Jan 22 2025
                    //#if !defined(__dsPIC33C__)
                    #if defined(SPI3STAT)
                    while (SPI3STATbits.SPITBF != true) //because 18 level FIFO, so it has chance more data transmitting
                    {
                        //First receive is better, in case overflow
                        while (SPI3STATbits.SRXMPT == false) {

                            pSPIObj->pReadData[pSPIObj->PresentSeqNo][dataReceivedCount_3++] = SPI3BUF;
                        }
                        if (dataSentCount_3 < MaxSize_3) {
                            SPI3BUF = pSPIObj->pWriteData[pSPIObj->PresentSeqNo][dataSentCount_3++];
                        } else {
                            break;
                        }
                        //                        if (SPI3STATbits.SRXMPT == false) {
                        //
                        //                            pSPIObj->pReadData[pSPIObj->PresentSeqNo][dataReceivedCount_3++] = SPI3BUF;
                        //                        }
                    }
                    if (MaxSize_3 == dataSentCount_3) {
                        while (dataReceivedCount_3 < MaxSize_3) {
                            if (SPI3STATbits.SRXMPT == false) {
                                pSPIObj->pReadData[pSPIObj->PresentSeqNo][dataReceivedCount_3++] = SPI3BUF;
                            }
                        }
                        pSPIObj->set_CS[pSPIObj->PresentSeqNo](false);
                        //(pSPIObj->set_CS + pSPIObj->PresentSeqNo)(false);
                        pSPIObj->state = 0;
                        return 0; //success
                    }
                    #else
                    while (SPI3STATLbits.SPITBF != true) //because 18 level FIFO, so it has chance more data transmitting
                    {
                        //First receive is better, in case overflow
                        while (SPI3STATLbits.SPIRBE == false) {

                            pSPIObj->pReadData[pSPIObj->PresentSeqNo][dataReceivedCount_3++] = SPI3BUFL;
                        }
                        if (dataSentCount_3 < MaxSize_3) {
                            SPI3BUFL = pSPIObj->pWriteData[pSPIObj->PresentSeqNo][dataSentCount_3++];
                        } else {
                            break;
                        }
                        //                        if (SPI3STATbits.SRXMPT == false) {
                        //
                        //                            pSPIObj->pReadData[pSPIObj->PresentSeqNo][dataReceivedCount_3++] = SPI3BUF;
                        //                        }
                    }
                    if (MaxSize_3 == dataSentCount_3) {
                        while (dataReceivedCount_3 < MaxSize_3) {
                            if (SPI3STATLbits.SPIRBE == false) {
                                pSPIObj->pReadData[pSPIObj->PresentSeqNo][dataReceivedCount_3++] = SPI3BUFL;
                            }
                        }
                        pSPIObj->set_CS[pSPIObj->PresentSeqNo](false);
                        //(pSPIObj->set_CS + pSPIObj->PresentSeqNo)(false);
                        pSPIObj->state = 0;
                        return 0; //success
                    }
                    #endif
                }
                return 1; //continue
#else
                pSPIObj->state = 0;
                return -1; //Fail
#endif                
            } else if (4 == pSPIObj->SPI_Peripheral_No) {
#ifdef USE_SPI4
                if (pSPIObj->Use16bitsBus) {
                    //16 bits
                    //Transmit---16 bits bus
                    //We changed for better compatible----Jan 22 2025
                    //#if !defined(__dsPIC33C__)
                    #if defined(SPI4STAT)
                    while (SPI4STATbits.SPITBF != true) //because 8 level FIFO, so it has chance more data transmitting
                    {
                        //First receive is better, in case overflow
                        while (SPI4STATbits.SRXMPT == false) {

                            ((uint16_t * *) pSPIObj->pReadData)[pSPIObj->PresentSeqNo][dataReceivedCount_4++] = SPI4BUF;
                        }
                        if (dataSentCount_4 < MaxSize_4) {
                            SPI4BUF = ((uint16_t * *) pSPIObj->pWriteData)[pSPIObj->PresentSeqNo][dataSentCount_4++];
                        } else {
                            break;
                        }
                        //                        if (SPI4STATbits.SRXMPT == false) {
                        //
                        //                            ((uint16_t * *) pSPIObj->pReadData)[pSPIObj->PresentSeqNo][dataReceivedCount_4++] = SPI4BUF;
                        //                        }
                    }
                    if (MaxSize_4 == dataSentCount_4) {
                        while (dataReceivedCount_4 < MaxSize_4) {
                            if (SPI4STATbits.SRXMPT == false) {
                                ((uint16_t * *) pSPIObj->pReadData)[pSPIObj->PresentSeqNo][dataReceivedCount_4++] = SPI4BUF;
                            }
                        }
                        pSPIObj->set_CS[pSPIObj->PresentSeqNo](false);
                        //(pSPIObj->set_CS + pSPIObj->PresentSeqNo)(false);
                        pSPIObj->state = 0;
                        return 0; //success
                    }
                    #else
                    while (SPI4STATLbits.SPITBF != true) //because 8 level FIFO, so it has chance more data transmitting
                    {
                        //First receive is better, in case overflow
                        while (SPI4STATLbits.SPIRBE == false) {

                            ((uint16_t * *) pSPIObj->pReadData)[pSPIObj->PresentSeqNo][dataReceivedCount_4++] = SPI4BUFL;
                        }
                        if (dataSentCount_4 < MaxSize_4) {
                            SPI4BUFL = ((uint16_t * *) pSPIObj->pWriteData)[pSPIObj->PresentSeqNo][dataSentCount_4++];
                        } else {
                            break;
                        }
                        //                        if (SPI4STATbits.SRXMPT == false) {
                        //
                        //                            ((uint16_t * *) pSPIObj->pReadData)[pSPIObj->PresentSeqNo][dataReceivedCount_4++] = SPI4BUF;
                        //                        }
                    }
                    if (MaxSize_4 == dataSentCount_4) {
                        while (dataReceivedCount_4 < MaxSize_4) {
                            if (SPI4STATLbits.SPIRBE == false) {
                                ((uint16_t * *) pSPIObj->pReadData)[pSPIObj->PresentSeqNo][dataReceivedCount_4++] = SPI4BUFL;
                            }
                        }
                        pSPIObj->set_CS[pSPIObj->PresentSeqNo](false);
                        //(pSPIObj->set_CS + pSPIObj->PresentSeqNo)(false);
                        pSPIObj->state = 0;
                        return 0; //success
                    }
                    #endif

                } else {
                    //8 bits
                    //transmit ---8 bits bus
                    //We changed for better compatible----Jan 22 2025
                    //#if !defined(__dsPIC33C__)
                    #if defined(SPI4STAT)
                    while (SPI4STATbits.SPITBF != true) //because 18 level FIFO, so it has chance more data transmitting
                    {
                        //First receive is better, in case overflow
                        while (SPI4STATbits.SRXMPT == false) {

                            pSPIObj->pReadData[pSPIObj->PresentSeqNo][dataReceivedCount_4++] = SPI4BUF;
                        }
                        if (dataSentCount_4 < MaxSize_4) {
                            SPI4BUF = pSPIObj->pWriteData[pSPIObj->PresentSeqNo][dataSentCount_4++];
                        } else {
                            break;
                        }
                        //                        if (SPI4STATbits.SRXMPT == false) {
                        //
                        //                            pSPIObj->pReadData[pSPIObj->PresentSeqNo][dataReceivedCount_4++] = SPI4BUF;
                        //                        }
                    }
                    if (MaxSize_4 == dataSentCount_4) {
                        while (dataReceivedCount_4 < MaxSize_4) {
                            if (SPI4STATbits.SRXMPT == false) {
                                pSPIObj->pReadData[pSPIObj->PresentSeqNo][dataReceivedCount_4++] = SPI4BUF;
                            }
                        }
                        pSPIObj->set_CS[pSPIObj->PresentSeqNo](false);
                        //(pSPIObj->set_CS + pSPIObj->PresentSeqNo)(false);
                        pSPIObj->state = 0;
                        return 0; //success
                    }
                    #else
                    while (SPI4STATLbits.SPITBF != true) //because 18 level FIFO, so it has chance more data transmitting
                    {
                        //First receive is better, in case overflow
                        while (SPI4STATLbits.SPIRBE == false) {

                            pSPIObj->pReadData[pSPIObj->PresentSeqNo][dataReceivedCount_4++] = SPI4BUFL;
                        }
                        if (dataSentCount_4 < MaxSize_4) {
                            SPI4BUFL = pSPIObj->pWriteData[pSPIObj->PresentSeqNo][dataSentCount_4++];
                        } else {
                            break;
                        }
                        //                        if (SPI4STATbits.SRXMPT == false) {
                        //
                        //                            pSPIObj->pReadData[pSPIObj->PresentSeqNo][dataReceivedCount_4++] = SPI4BUF;
                        //                        }
                    }
                    if (MaxSize_4 == dataSentCount_4) {
                        while (dataReceivedCount_4 < MaxSize_4) {
                            if (SPI4STATLbits.SPIRBE == false) {
                                pSPIObj->pReadData[pSPIObj->PresentSeqNo][dataReceivedCount_4++] = SPI4BUFL;
                            }
                        }
                        pSPIObj->set_CS[pSPIObj->PresentSeqNo](false);
                        //(pSPIObj->set_CS + pSPIObj->PresentSeqNo)(false);
                        pSPIObj->state = 0;
                        return 0; //success
                    }
                    #endif
                }
                return 1; //continue
#else
                pSPIObj->state = 0;
                return -1; //Fail
#endif               
            }

    }
    return 1;
}




