#include <stdint.h>
#include <stdbool.h>  
#include <stddef.h>
#include <xc.h>
#include "CANBUS.h"


#ifdef USE_CAN1
CANBUS_t CAN_myObj1;
extern volatile uint16_t RxStdCANID_1[]; //Rx Std short frame CANID for CAN1
extern uint16_t sizeof_RxStdCANID_1; // sizeof(RxStdCANID_1)/sizeof(RxStdCANID_1[0])
extern volatile uint8_t RxStdDLC_1[]; //CAN1 short Std rx: Bit7 :RTR (1 is RTR, after reading, clear to 0), Bit6-0: DLC (0 to 8)
extern volatile uint8_t RxStdData_1[][8]; //CAN1 short Std rx Data:  1st index  -> No of packet, 2nd index -> Data No

extern volatile uint32_t RxExtCANID_1[]; //Rx Ext short frame for CAN1
extern uint16_t sizeof_RxExtCANID_1; //sizeof(RxExtCANID_1)/sizeof(RxExtCANID_1[0])
extern volatile uint8_t RxExtDLC_1[]; //CAN1 short Ext rx: Bit7 :RTR (1 is RTR, after reading, clear to 0), Bit6-0: DLC (0 to 8)
extern volatile uint8_t RxExtData_1[][8]; //CAN1 short Ext rx Data:  1st index  -> No of packet, 2nd index -> Data No
//Rx CAN1 long for standard 11 bits CAN 
//Bit15: for data integrity: busy to receive. when Receiving First Frame, Set BIT15=1. When receiving last frame, clear B15, So you can get data when Bit15=0
extern volatile uint16_t RxStdCANID_Long_1[]; //Rx Std long frame CANID for CAN1, index is CAN ID index
extern uint16_t sizeof_RxStdCANID_Long_1; // sizeof(RxStdCANID_Long_1)/sizeof(RxStdCANID_Long_1[0])
//Bit15: for data integrity: busy to receive. when Receiving First Frame, Set BIT15=1. When receiving last frame, clear B15, So you can get data                                                
extern volatile uint8_t RxStdDLC_Long_1[][CAN1_RX_MAX_STD_FRAME_QTY4LONG]; //CAN1 long Std rx: 1st index  -> CAN ID index, 2nd Index -> Frame No
//Bit7 :reserved, Bit6-0: DLC (0 to 8)
extern volatile uint8_t RxStdData_Long_1[][CAN1_RX_MAX_STD_FRAME_QTY4LONG][8]; //CAN1 long Std rx: 1st index  -> CAN ID index, 2nd Index -> Frame No, 3rd index -> Data index,  Array value is Data
extern volatile uint8_t LastStdMAXFrmNo_1[]; //CAN1 long Std rx: index  -> CAN ID index.  This array value is the last frame number for this CAN ID from simulink (It is Fram No, not index)

//Rx CAN1 long for ext 29 bits CAN 
//Bit31: for data integrity: busy to receive. when Receiving First Frame, Set BIT31=1. When receiving last frame, clear B31, So you can get data when Bit31=0
extern volatile uint32_t RxExtCANID_Long_1[]; //Rx Ext long frame CANID for CAN1, index is CAN ID index
extern uint16_t sizeof_RxExtCANID_Long_1;  // sizeof(RxExtCANID_Long_1)/sizeof(RxExtCANID_Long_1[0])
//Bit31: for data integrity: busy to receive. when Receiving First Frame, Set BIT31=1. When receiving last frame, clear B31, So you can get data                                                
extern volatile uint8_t RxExtDLC_Long_1[][CAN1_RX_MAX_EXT_FRAME_QTY4LONG]; //CAN1 long Ext rx: 1st index  -> CAN ID index, 2nd Index -> Frame No
//Bit7 :reserved, Bit6-0: DLC (0 to 8)
extern volatile uint8_t RxExtData_Long_1[][CAN1_RX_MAX_EXT_FRAME_QTY4LONG][8]; //CAN1 long Ext rx: 1st index  -> CAN ID index, 2nd Index -> Frame No, 3rd index -> Data index,  Array value is Data
extern volatile uint8_t LastExtMAXFrmNo_1[]; //CAN1 long Ext rx: index  -> CAN ID index.  This array value is the last frame number for this CAN ID


extern uint16_t TxResponseCANID_Index_A_1[]; //A Channel Response CAN ID Index,  Bit15-Bit14:  Type of CANID  ---- 00: Std short Frame, 01: Ext short Frame 
// 10: Std long Frame, 11: Ext long Frame.  Bit13-Bit0: truly Response CAN ID index
//This Array's index is Seq No. Max Index=3.
extern uint8_t ResponseMaxFrameNo_A_1[]; //Respnse Max frame Number for this time transmit response when long frame response, Index is Seq No. Max Index=3. 

#ifdef USE_CAN1_CHANNELB
extern uint16_t TxResponseCANID_Index_B_1[]; //B Channel Response CAN ID Index,  Bit15-Bit14:  Type of CANID  ---- 00: Std short Frame, 01: Ext short Frame 
// 10: Std long Frame, 11: Ext long Frame.  Bit13-Bit0: truly Response CAN ID index
//This Array's index is Seq No. Max Index=3.
//extern uint16_t sizeof_TxResponseCANID_Index_B_1; //sizeof(TxResponseCANID_Index_B_1)/sizeof(TxResponseCANID_Index_B_1[0])
extern uint8_t ResponseMaxFrameNo_B_1[]; //Respnse Max frame Number for this time transmit response when long frame response, Index is Seq No. Max Index=3. 

#endif
#ifdef USE_CAN1_CHANNELC
extern uint16_t TxResponseCANID_Index_C_1[]; //C Channel Response CAN ID Index,  Bit15-Bit14:  Type of CANID  ---- 00: Std short Frame, 01: Ext short Frame 
// 10: Std long Frame, 11: Ext long Frame.  Bit13-Bit0: truly Response CAN ID index
//This Array's index is Seq No. Max Index=3.
//extern uint16_t sizeof_TxResponseCANID_Index_C_1; //sizeof(TxResponseCANID_Index_C_1)/sizeof(TxResponseCANID_Index_C_1[0])
extern uint8_t ResponseMaxFrameNo_C_1[]; //Respnse Max frame Number for this time transmit response when long frame response, Index is Seq No. Max Index=3. 

#endif
extern void CAN1_fill_Rx_Data();
#endif
extern int8_t CAN1_A_Channel_Transmit4HL(uint8_t Seq);
extern int8_t CAN1_B_Channel_Transmit4HL(uint8_t Seq);
extern int8_t CAN1_C_Channel_Transmit4HL(uint8_t Seq);




#ifdef USE_CAN2
CANBUS_t CAN_myObj2;
extern volatile uint16_t RxStdCANID_2[]; //Rx Std short frame CANID for CAN2
extern uint16_t sizeof_RxStdCANID_2; // sizeof(RxStdCANID_2)/sizeof(RxStdCANID_2[0])
extern volatile uint8_t RxStdDLC_2[]; //CAN2 short Std rx: Bit7 :RTR (1 is RTR, after reading, clear to 0), Bit6-0: DLC (0 to 8)
extern volatile uint8_t RxStdData_2[][8]; //CAN2 short Std rx Data:  1st index  -> No of packet, 2nd index -> Data No

extern volatile uint32_t RxExtCANID_2[]; //Rx Ext short frame for CAN2
extern uint16_t sizeof_RxExtCANID_2;
extern volatile uint8_t RxExtDLC_2[]; //CAN2 short Ext rx: Bit7 :RTR (1 is RTR, after reading, clear to 0), Bit6-0: DLC (0 to 8)
extern volatile uint8_t RxExtData_2[][8]; //CAN2 short Ext rx Data:  1st index  -> No of packet, 2nd index -> Data No

//Rx CAN2 long for standard 11 bits CAN 
//Bit15: for data integrity: busy to receive. when Receiving First Frame, Set BIT15=1. When receiving last frame, clear B15, So you can get data when Bit15=0
extern volatile uint16_t RxStdCANID_Long_2[]; //Rx Std long frame CANID for CAN2, index is CAN ID index
extern uint16_t sizeof_RxStdCANID_Long_2; //sizeof(RxStdCANID_Long_2)/sizeof(RxStdCANID_Long_2[0])
//Bit15: for data integrity: busy to receive. when Receiving First Frame, Set BIT15=1. When receiving last frame, clear B15, So you can get data                                                
extern volatile uint8_t RxStdDLC_Long_2[][CAN2_RX_MAX_STD_FRAME_QTY4LONG]; //CAN2 long Std rx: 1st index  -> CAN ID index, 2nd Index -> Frame No
//Bit7 :reserved, Bit6-0: DLC (0 to 8)
extern volatile uint8_t RxStdData_Long_2[][CAN2_RX_MAX_STD_FRAME_QTY4LONG][8]; //CAN2 long Std rx: 1st index  -> CAN ID index, 2nd Index -> Frame No, 3rd index -> Data index,  Array value is Data
extern volatile uint8_t LastStdMAXFrmNo_2[]; //CAN2 long Std rx: index  -> CAN ID index.  This array value is the last frame number for this CAN ID

//Rx CAN2 long for ext 29 bits CAN 
//Bit31: for data integrity: busy to receive. when Receiving First Frame, Set BIT31=1. When receiving last frame, clear B31, So you can get data when Bit31=0
extern volatile uint32_t RxExtCANID_Long_2[]; //Rx Ext long frame CANID for CAN2, index is CAN ID index
//Bit31: for data integrity: busy to receive. when Receiving First Frame, Set BIT31=1. When receiving last frame, clear B31, So you can get data   
extern uint16_t sizeof_RxExtCANID_Long_2;
extern volatile uint8_t RxExtDLC_Long_2[][CAN2_RX_MAX_EXT_FRAME_QTY4LONG]; //CAN2 long Ext rx: 1st index  -> CAN ID index, 2nd Index -> Frame No
//Bit7 :reserved, Bit6-0: DLC (0 to 8)
extern volatile uint8_t RxExtData_Long_2[][CAN2_RX_MAX_EXT_FRAME_QTY4LONG][8]; //CAN2 long Ext rx: 1st index  -> CAN ID index, 2nd Index -> Frame No, 3rd index -> Data index,  Array value is Data
extern volatile uint8_t LastExtMAXFrmNo_2[]; //CAN2 long Ext rx: index  -> CAN ID index.  This array value is the last frame number for this CAN ID


extern uint16_t TxResponseCANID_Index_A_2[]; //A Channel Response CAN ID Index,  Bit15-Bit14:  Type of CANID  ---- 00: Std short Frame, 01: Ext short Frame 
// 10: Std long Frame, 11: Ext long Frame.  Bit13-Bit0: truly Response CAN ID index
//This Array's index is Seq No. Max Index=3.
//extern uint16_t sizeof_TxResponseCANID_Index_A_2;
extern uint8_t ResponseMaxFrameNo_A_2[]; //Respnse Max frame Number for this time transmit response when long frame response, Index is Seq No. Max Index=3. 

#ifdef USE_CAN2_CHANNELB
extern uint16_t TxResponseCANID_Index_B_2[]; //B Channel Response CAN ID Index,  Bit15-Bit14:  Type of CANID  ---- 00: Std short Frame, 01: Ext short Frame 
// 10: Std long Frame, 11: Ext long Frame.  Bit13-Bit0: truly Response CAN ID index
//This Array's index is Seq No. Max Index=3.
//extern uint16_t sizeof_TxResponseCANID_Index_B_2;
extern uint8_t ResponseMaxFrameNo_B_2[]; //Respnse Max frame Number for this time transmit response when long frame response, Index is Seq No. Max Index=3. 

#endif
#ifdef USE_CAN2_CHANNELC
extern uint16_t TxResponseCANID_Index_C_2[]; //C Channel Response CAN ID Index,  Bit15-Bit14:  Type of CANID  ---- 00: Std short Frame, 01: Ext short Frame 
// 10: Std long Frame, 11: Ext long Frame.  Bit13-Bit0: truly Response CAN ID index
//This Array's index is Seq No. Max Index=3.
//extern uint16_t sizeof_TxResponseCANID_Index_C_2;
extern uint8_t ResponseMaxFrameNo_C_2[]; //Respnse Max frame Number for this time transmit response when long frame response, Index is Seq No. Max Index=3. 

#endif
extern void CAN2_fill_Rx_Data();
#endif
extern int8_t CAN2_A_Channel_Transmit4HL(uint8_t Seq);
extern int8_t CAN2_B_Channel_Transmit4HL(uint8_t Seq);
extern int8_t CAN2_C_Channel_Transmit4HL(uint8_t Seq);



void CAN_set_Mask(CANBUS_t * pCANxObj, bool MASK0IsSTD, bool MASK1IsSTD, bool MASK2IsSTD, uint32_t Mask0, uint32_t Mask1, uint32_t Mask2) {
    pCANxObj->MASK0IsSTD = MASK0IsSTD;
    pCANxObj->MASK1IsSTD = MASK1IsSTD;
    pCANxObj->MASK2IsSTD = MASK2IsSTD;

    pCANxObj->Mask0 = Mask0;
    pCANxObj->Mask1 = Mask1;
    pCANxObj->Mask2 = Mask2;

}

/**
 * Pre-condition for calling:
 *     Must call CAN_set_Mask firdt for PIC
 * @param pCANxObj
 * @param CAN_Peripheral_No
 * @param enSync
 * @param SyncUseStdID
 * @param SyncCANID
 * @param FrameNoIs0_based
 * @param pLogicRuleA
 * @param pLogicRuleB
 * @param pLogicRuleC
 * @param MaxSeqQTY4ChannelA
 * @param MaxSeqQTY4ChannelB
 * @param MaxSeqQTY4ChannelC
 * @param RxStdFrameQTY4short
 * @param RxExtFrameQTY4short
 * @param RxStdFrameQTY4long
 * @param RxExtFrameQTY4long
 */
void CAN_init(CANBUS_t * pCANxObj, uint8_t CAN_Peripheral_No, bool enSync, bool SyncUseStdID, uint32_t SyncCANID, 
        bool FrameNoIs0_based, uint32_t * pLogicRuleA, uint32_t * pLogicRuleB, uint32_t * pLogicRuleC, uint8_t MaxSeqQTY4ChannelA, 
        uint8_t MaxSeqQTY4ChannelB, uint8_t MaxSeqQTY4ChannelC, uint16_t COMFaultDetectTimeInms) {
    pCANxObj->CAN_Peripheral_No = CAN_Peripheral_No;
    pCANxObj->EnableSyn = enSync;
    pCANxObj->SynUseStdCANID = SyncUseStdID;
    pCANxObj->SyncCANID = SyncCANID;

    pCANxObj->FrameNoIs0_based = FrameNoIs0_based;
     //Apr08 2025 modify    , always received
    //pCANxObj->RxCANReceived = 0; //Received Data
    pCANxObj->RxCANReceived = 1;
    if (pLogicRuleA != NULL) {
        *pLogicRuleA = 0; //idle State
    }
    if (pLogicRuleB != NULL) {
        *pLogicRuleB = 0; //idle State
    }
    if (pLogicRuleC != NULL) {
        *pLogicRuleC = 0; //idle State
    }
    pCANxObj->pLogicRuleA = pLogicRuleA;
    pCANxObj->pLogicRuleB = pLogicRuleB;
    pCANxObj->pLogicRuleC = pLogicRuleC;
    pCANxObj->MaxSeqQTY4ChannelA = MaxSeqQTY4ChannelA;
    pCANxObj->MaxSeqQTY4ChannelB = MaxSeqQTY4ChannelB;
    pCANxObj->MaxSeqQTY4ChannelC = MaxSeqQTY4ChannelC;

    //pCANxObj->RxStdFrameQTY4short = RxStdFrameQTY4short;
    //pCANxObj->RxExtFrameQTY4short = RxExtFrameQTY4short;
    //pCANxObj->RxStdFrameQTY4long = RxStdFrameQTY4long;
    //pCANxObj->RxExtFrameQTY4long = RxExtFrameQTY4long;
    pCANxObj->COMFaultDetectTimeInms = COMFaultDetectTimeInms;
    pCANxObj->COMFaultCounter = 0;
    pCANxObj->CANBusCOMFault = 0;

    

}

void CANBus_Poll(CANBUS_t * pCANxObj) {
    uint32_t temp4state;
    int8_t TxResult;
    static uint8_t PresentSeqNo4A_1 = 0; //CAN1 channel A , current SeqNo
    static uint8_t PresentSeqNo4B_1 = 0; //CAN1 channel B , current SeqNo
    static uint8_t PresentSeqNo4C_1 = 0; //CAN1 channel C , current SeqNo

    static uint8_t PresentSeqNo4A_2 = 0; //CAN2 channel A , current SeqNo
    static uint8_t PresentSeqNo4B_2 = 0; //CAN2 channel B , current SeqNo
    static uint8_t PresentSeqNo4C_2 = 0; //CAN2 channel C , current SeqNo
   

    //Receive CAN Data
    #if defined(__dsPIC33C__)
    if (pCANxObj->CAN_Peripheral_No == 1) {
      #ifdef USE_CAN1
        if (CAN1_ReceivedMessageCountGet() > 0) {
            pCANxObj->RxCANReceived=1;
        }
      #endif
    }
    else {
      #ifdef USE_CAN2
        if (CAN2_ReceivedMessageCountGet() > 0) {
            pCANxObj->RxCANReceived=1;
        }
      #endif  
    }              
    #endif
    if (pCANxObj->RxCANReceived) {
        //pCANxObj->RxCANReceived = 0;  //Apr08 2025 modify    , always received
        //pCANxObj->COMFaultCounter = 0;  //must remove, otherwise cannot detect CAN Communication fault
        if (pCANxObj->CAN_Peripheral_No == 1) {
#ifdef USE_CAN1
            CAN1_fill_Rx_Data();   //Apr 07 2025 modify            
#endif
        }
        if (pCANxObj->CAN_Peripheral_No == 2) {
#ifdef USE_CAN2
            CAN2_fill_Rx_Data();
#endif
        }
    }
    if (pCANxObj->COMFaultCounter > pCANxObj->COMFaultDetectTimeInms)
        pCANxObj->CANBusCOMFault = 1;
    else
        pCANxObj->CANBusCOMFault = 0;

    //Transmit Data  (Actual transmit handle in low layer : CAN1_A_Channel_Transmit4HL, ...)
    //Channel A
    if ((pCANxObj->pLogicRuleA != NULL) && ((*pCANxObj->pLogicRuleA) & 0x80000000UL)) {
        temp4state = *pCANxObj->pLogicRuleA;
        if (pCANxObj->CAN_Peripheral_No == 1) {
            //CAN1
            if (temp4state & (1UL << PresentSeqNo4A_1)) {
                //Transmit Seq: PresentSeqNo4A_1
                TxResult = CAN1_A_Channel_Transmit4HL(PresentSeqNo4A_1);
                if (0 == TxResult) {
                    //Transmit Success
                    pCANxObj->TxChannelA_Success = pCANxObj->TxChannelA_Success | (1UL << PresentSeqNo4A_1);
                    temp4state = temp4state & ~(1UL << PresentSeqNo4A_1);
                    PresentSeqNo4A_1++;
                    if (PresentSeqNo4A_1 >= pCANxObj->MaxSeqQTY4ChannelA) {
                        PresentSeqNo4A_1 = 0; 
                        //Clear "Sync RxCANID arrive " flag if Channel B and C Don't be used and if Sync Enable
                        if ((pCANxObj->pLogicRuleB == NULL) && ((pCANxObj->pLogicRuleC == NULL))) {
                            pCANxObj->SyncCANID &= 0x7FFFffff;  //actually we don't care if Sync Enable  
                        }
                    }
                                           
                } else if (TxResult < 0) {
                    //transmit fail
                    pCANxObj->TxChannelA_Success = pCANxObj->TxChannelA_Success & ~(1UL << PresentSeqNo4A_1);
                    temp4state = temp4state & ~(1UL << PresentSeqNo4A_1);
                    PresentSeqNo4A_1++;
                    if (PresentSeqNo4A_1 >= pCANxObj->MaxSeqQTY4ChannelA) {
                        PresentSeqNo4A_1 = 0;
                        //Clear "Sync RxCANID arrive " flag if Channel B and C Don't be used and if Sync Enable
                        if ((pCANxObj->pLogicRuleB == NULL) && ((pCANxObj->pLogicRuleC == NULL))) {
                            pCANxObj->SyncCANID &= 0x7FFFffff;  //actually we don't care if Sync Enable  
                        }
                    }                        
                } else {
                    //Run again next time (continue), so do nothing
                }                        
            }
            else
            {
                PresentSeqNo4A_1++;
                if (0x80000000==(*pCANxObj->pLogicRuleA))  //Keep Seq0 is the highest Priority
                {
                    PresentSeqNo4A_1=0;
                    //Clear "Sync RxCANID arrive " flag if Channel B and C Don't be used and if Sync Enable
                    if ((pCANxObj->pLogicRuleB == NULL) && ((pCANxObj->pLogicRuleC == NULL))) {
                        pCANxObj->SyncCANID &= 0x7FFFffff;  //actually we don't care if Sync Enable  
                    }                    
                } else {
                //if (32==PresentSeqNo4A_1) PresentSeqNo4A_1=0;
                if (pCANxObj->MaxSeqQTY4ChannelA==PresentSeqNo4A_1) PresentSeqNo4A_1=0; }
                
            }
            
        } else {
            //CAN2
            if (temp4state & (1UL << PresentSeqNo4A_2)) {
                //Transmit Seq: PresentSeqNo4A_2
                TxResult = CAN2_A_Channel_Transmit4HL(PresentSeqNo4A_2);
                if (0 == TxResult) {
                    //Transmit Success
                    pCANxObj->TxChannelA_Success = pCANxObj->TxChannelA_Success | (1UL << PresentSeqNo4A_2);
                    temp4state = temp4state & ~(1UL << PresentSeqNo4A_2);
                    PresentSeqNo4A_2++;
                    if (PresentSeqNo4A_2 >= pCANxObj->MaxSeqQTY4ChannelA) {
                        PresentSeqNo4A_2 = 0;
                        //Clear "Sync RxCANID arrive " flag if Channel B and C Don't be used and if Sync Enable
                        if ((pCANxObj->pLogicRuleB == NULL) && ((pCANxObj->pLogicRuleC == NULL))) {
                            pCANxObj->SyncCANID &= 0x7FFFffff;  //actually we don't care if Sync Enable  
                        }
                    }                        
                } else if (TxResult < 0) {
                    //transmit fail
                    pCANxObj->TxChannelA_Success = pCANxObj->TxChannelA_Success & ~(1UL << PresentSeqNo4A_2);
                    temp4state = temp4state & ~(1UL << PresentSeqNo4A_2);
                    PresentSeqNo4A_2++;
                    if (PresentSeqNo4A_2 >= pCANxObj->MaxSeqQTY4ChannelA)  {
                        PresentSeqNo4A_2 = 0;
                        //Clear "Sync RxCANID arrive " flag if Channel B and C Don't be used and if Sync Enable
                        if ((pCANxObj->pLogicRuleB == NULL) && ((pCANxObj->pLogicRuleC == NULL))) {
                            pCANxObj->SyncCANID &= 0x7FFFffff;  //actually we don't care if Sync Enable  
                        }
                    }
                        
                } else {
                    //Run again next time (continue), so do nothing
                }
            }
            else
            {
                PresentSeqNo4A_2++;
                if (0x80000000==(*pCANxObj->pLogicRuleA))  //Keep Seq0 is the highest Priority
                {
                    PresentSeqNo4A_2=0;
                    //Clear "Sync RxCANID arrive " flag if Channel B and C Don't be used and if Sync Enable
                    if ((pCANxObj->pLogicRuleB == NULL) && ((pCANxObj->pLogicRuleC == NULL))) {
                        pCANxObj->SyncCANID &= 0x7FFFffff;  //actually we don't care if Sync Enable  
                    }
                } else {
                //if (32==PresentSeqNo4A_2) PresentSeqNo4A_2=0;
                if (pCANxObj->MaxSeqQTY4ChannelA==PresentSeqNo4A_2) PresentSeqNo4A_2=0; }
            }
        }
        *pCANxObj->pLogicRuleA = temp4state;

    }

    //Channel B : Transmit
    if ((pCANxObj->pLogicRuleB != NULL) && ((*pCANxObj->pLogicRuleB) & 0x80000000UL)) {
        temp4state = *pCANxObj->pLogicRuleB;
        if (pCANxObj->CAN_Peripheral_No == 1) {
            //CAN1 
            if (temp4state & (1UL << PresentSeqNo4B_1)) {
                //Transmit Seq: PresentSeqNo4B_1
                TxResult = CAN1_B_Channel_Transmit4HL(PresentSeqNo4B_1);
                if (0 == TxResult) {
                    //Transmit Success
                    pCANxObj->TxChannelB_Success = pCANxObj->TxChannelB_Success | (1UL << PresentSeqNo4B_1);
                    temp4state = temp4state & ~(1UL << PresentSeqNo4B_1);
                    PresentSeqNo4B_1++;
                    if (PresentSeqNo4B_1 >= pCANxObj->MaxSeqQTY4ChannelB) {
                        PresentSeqNo4B_1 = 0;
                        //Clear "Sync RxCANID arrive " flag if Channel C Don't be used and if Sync Enable
                        if ((pCANxObj->pLogicRuleC == NULL)) {
                            pCANxObj->SyncCANID &= 0x7FFFffff;  //actually we don't care if Sync Enable  
                        }
                    }                        
                } else if (TxResult < 0) {
                    //transmit fail
                    pCANxObj->TxChannelB_Success = pCANxObj->TxChannelB_Success & ~(1UL << PresentSeqNo4B_1);
                    temp4state = temp4state & ~(1UL << PresentSeqNo4B_1);
                    PresentSeqNo4B_1++;
                    if (PresentSeqNo4B_1 >= pCANxObj->MaxSeqQTY4ChannelB) {
                        PresentSeqNo4B_1 = 0;
                        //Clear "Sync RxCANID arrive " flag if Channel C Don't be used and if Sync Enable
                        if ((pCANxObj->pLogicRuleC == NULL)) {
                            pCANxObj->SyncCANID &= 0x7FFFffff;  //actually we don't care if Sync Enable  
                        }
                    }                        
                }
            }
            else
            {
                PresentSeqNo4B_1++;
                if (0x80000000==(*pCANxObj->pLogicRuleB))  //Keep Seq0 is the highest Priority
                {
                    PresentSeqNo4B_1=0;
                    //Clear "Sync RxCANID arrive " flag if Channel C Don't be used and if Sync Enable
                    if ((pCANxObj->pLogicRuleC == NULL)) {
                        pCANxObj->SyncCANID &= 0x7FFFffff;  //actually we don't care if Sync Enable  
                    }
                } else {
                //if (32==PresentSeqNo4B_1) PresentSeqNo4B_1=0;
                if (pCANxObj->MaxSeqQTY4ChannelB==PresentSeqNo4B_1) PresentSeqNo4B_1=0; }
            }
        } else {
            //CAN2
            if (temp4state & (1UL << PresentSeqNo4B_2)) {
                //Transmit Seq: PresentSeqNo4B_2
                TxResult = CAN2_B_Channel_Transmit4HL(PresentSeqNo4B_2);
                if (0 == TxResult) {
                    //Transmit Success
                    pCANxObj->TxChannelB_Success = pCANxObj->TxChannelB_Success | (1UL << PresentSeqNo4B_2);
                    temp4state = temp4state & ~(1UL << PresentSeqNo4B_2);
                    PresentSeqNo4B_2++;
                    if (PresentSeqNo4B_2 >= pCANxObj->MaxSeqQTY4ChannelB) {
                        PresentSeqNo4B_2 = 0;
                        //Clear "Sync RxCANID arrive " flag if Channel C Don't be used and if Sync Enable
                        if ((pCANxObj->pLogicRuleC == NULL)) {
                            pCANxObj->SyncCANID &= 0x7FFFffff;  //actually we don't care if Sync Enable  
                        }
                    }                        
                } else if (TxResult < 0) {
                    //transmit fail
                    pCANxObj->TxChannelB_Success = pCANxObj->TxChannelB_Success & ~(1UL << PresentSeqNo4B_2);
                    temp4state = temp4state & ~(1UL << PresentSeqNo4B_2);
                    PresentSeqNo4B_2++;
                    if (PresentSeqNo4B_2 >= pCANxObj->MaxSeqQTY4ChannelB) {
                        PresentSeqNo4B_2 = 0;
                        //Clear "Sync RxCANID arrive " flag if Channel C Don't be used and if Sync Enable
                        if ((pCANxObj->pLogicRuleC == NULL)) {
                            pCANxObj->SyncCANID &= 0x7FFFffff;  //actually we don't care if Sync Enable  
                        }
                    }
                        
                }
            }
            else
            {
                PresentSeqNo4B_2++;
                if (0x80000000==(*pCANxObj->pLogicRuleB))  //Keep Seq0 is the highest Priority
                {
                    PresentSeqNo4B_2=0;
                    //Clear "Sync RxCANID arrive " flag if Channel C Don't be used and if Sync Enable
                    if ((pCANxObj->pLogicRuleC == NULL)) {
                        pCANxObj->SyncCANID &= 0x7FFFffff;  //actually we don't care if Sync Enable  
                    }
                } else {
                //if (32==PresentSeqNo4B_2) PresentSeqNo4B_2=0;
                if (pCANxObj->MaxSeqQTY4ChannelB==PresentSeqNo4B_2) PresentSeqNo4B_2=0; }
            }
        }
        *pCANxObj->pLogicRuleB = temp4state;
    }

    //Channel C : Transmit
    if ((pCANxObj->pLogicRuleC != NULL) && ((*pCANxObj->pLogicRuleC) & 0x80000000UL)) {
        temp4state = *pCANxObj->pLogicRuleC;
        if (pCANxObj->CAN_Peripheral_No == 1) {
            //CAN1 
            if (temp4state & (1UL << PresentSeqNo4C_1)) {
                //Transmit Seq: PresentSeqNo4C_1
                TxResult = CAN1_C_Channel_Transmit4HL(PresentSeqNo4C_1);
                if (0 == TxResult) {
                    //Transmit Success
                    pCANxObj->TxChannelC_Success = pCANxObj->TxChannelC_Success | (1UL << PresentSeqNo4C_1);
                    temp4state = temp4state & ~(1UL << PresentSeqNo4C_1);
                    PresentSeqNo4C_1++;
                    if (PresentSeqNo4C_1 >= pCANxObj->MaxSeqQTY4ChannelC) {
                        PresentSeqNo4C_1 = 0;
                        //Clear "Sync RxCANID arrive " flag if Sync Enable
                        pCANxObj->SyncCANID &= 0x7FFFffff;  //actually we don't care if Sync Enable
                    }                        
                    
                } else if (TxResult < 0) {
                    //transmit fail
                    pCANxObj->TxChannelC_Success = pCANxObj->TxChannelC_Success & ~(1UL << PresentSeqNo4C_1);
                    temp4state = temp4state & ~(1UL << PresentSeqNo4C_1);
                    PresentSeqNo4C_1++;
                    if (PresentSeqNo4C_1 >= pCANxObj->MaxSeqQTY4ChannelC) {
                        PresentSeqNo4C_1 = 0;
                        //Clear "Sync RxCANID arrive " flag if Sync Enable
                        pCANxObj->SyncCANID &= 0x7FFFffff;  //actually we don't care if Sync Enable
                    }                        
                }
            }
            else
            {
                PresentSeqNo4C_1++;
                if (0x80000000==(*pCANxObj->pLogicRuleC))  //Keep Seq0 is the highest Priority
                {
                    PresentSeqNo4C_1=0;
                    //Clear "Sync RxCANID arrive " flag if Sync Enable
                    pCANxObj->SyncCANID &= 0x7FFFffff;  //actually we don't care if Sync Enable
                } else {
                //if (32==PresentSeqNo4C_1) PresentSeqNo4C_1=0;
                if (pCANxObj->MaxSeqQTY4ChannelC==PresentSeqNo4C_1) PresentSeqNo4C_1=0; }
            }
        } else {
            //CAN2
            if (temp4state & (1UL << PresentSeqNo4C_2)) {
                //Transmit Seq: PresentSeqNo4C_2
                TxResult = CAN2_C_Channel_Transmit4HL(PresentSeqNo4C_2);
                if (0 == TxResult) {
                    //Transmit Success
                    pCANxObj->TxChannelC_Success = pCANxObj->TxChannelC_Success | (1UL << PresentSeqNo4C_2);
                    temp4state = temp4state & ~(1UL << PresentSeqNo4C_2);
                    PresentSeqNo4C_2++;
                    if (PresentSeqNo4C_2 >= pCANxObj->MaxSeqQTY4ChannelC) {
                        PresentSeqNo4C_2 = 0;
                        //Clear "Sync RxCANID arrive " flag if Sync Enable
                        pCANxObj->SyncCANID &= 0x7FFFffff;  //actually we don't care if Sync Enable
                    }                        
                } else if (TxResult < 0) {
                    //transmit fail
                    pCANxObj->TxChannelC_Success = pCANxObj->TxChannelC_Success & ~(1UL << PresentSeqNo4C_2);
                    temp4state = temp4state & ~(1UL << PresentSeqNo4C_2);
                    PresentSeqNo4C_2++;
                    if (PresentSeqNo4C_2 >= pCANxObj->MaxSeqQTY4ChannelC) {
                        PresentSeqNo4C_2 = 0;
                        //Clear "Sync RxCANID arrive " flag if Sync Enable
                        pCANxObj->SyncCANID &= 0x7FFFffff;  //actually we don't care if Sync Enable
                        
                    }
                        
                }
            }
            else
            {
                PresentSeqNo4C_2++;
                if (0x80000000==(*pCANxObj->pLogicRuleC))  //Keep Seq0 is the highest Priority
                {
                    PresentSeqNo4C_2=0;
                    //Clear "Sync RxCANID arrive " flag if Sync Enable
                    pCANxObj->SyncCANID &= 0x7FFFffff;  //actually we don't care if Sync Enable
                } else {
                //if (32==PresentSeqNo4C_2) PresentSeqNo4C_2=0;
                if (pCANxObj->MaxSeqQTY4ChannelC==PresentSeqNo4C_2) PresentSeqNo4C_2=0;  }
            }
        }
        *pCANxObj->pLogicRuleC = temp4state;
    }
//    #if defined(__dsPIC33C__)
//    if (pCANxObj->CAN_Peripheral_No == 1) {
//      #ifdef USE_CAN1
//        CAN1_Tasks();
//      #endif
//    }
//    else {
//      #ifdef USE_CAN2
//        CAN2_Tasks();
//      #endif  
//    }              
//    #endif
    
}

#ifdef USE_CAN1

/**
 * For CAN1, Put one Rx Standard frame  into our high level buffer such as : RxStdData_1[],RxStdData_Long_1[][][]
 *  And for long frame, set/clear Data integrity flag in Bit15 of RxStdCANID_Long_1
 * for respond frame, when received response, clear Bit14 of RxStdCANID_1 or clear Bit14 of RxStdCANID_Long_1 when Max frame No is what I expect.
 *   To set Bit14 for RxStdCANID_1 or RxStdCANID_Long_1 is in function for transmitting when expecting response
 * @param CANID
 * @param RTR
 * @param DLC
 * @param pCAN_Data
 */
void putCAN1RxStdCAN2Buffer(uint16_t CANID, bool RTR, uint8_t DLC, uint8_t * pCAN_Data) {
    uint16_t index_min, index_max, index;
    if (CAN_myObj1.EnableSyn && CAN_myObj1.SynUseStdCANID) {
        //modify new, because we don't need to set it to 1 if Bit31  of CAN_myObj1.SyncCANID=1
        //if (CANID==(CAN_myObj1.SyncCANID & 0x7FFFffff))  CAN_myObj1.SyncCANID = CAN_myObj1.SyncCANID | 0x80000000;
        if (CANID==CAN_myObj1.SyncCANID )  CAN_myObj1.SyncCANID = CAN_myObj1.SyncCANID | 0x80000000;
    }    
    
    //CANID belongs to RxStdCANID_1? (short receive std frame)
    // We clear Bit15 Bit14 in comparing standard CAN ID because Bit15 used in Data integrity of long frame, Bit 14 used for Response Flag for transmit which needs response
    if (RxStdCANID_1 != NULL) {
        index_min = 0;
        //index_max = sizeof (RxStdCANID_1) - 1;
        index_max =sizeof_RxStdCANID_1-1;
        // We clear Bit15 Bit14 in comparing standard CAN ID because Bit15 used in Data integrity of long frame, Bit 14 used for Response Flag for transmit which needs response
        if ((CANID >= (RxStdCANID_1[index_min] & 0x3FFF)) && (CANID <= (RxStdCANID_1[index_max]& 0x3FFF))) {
            while (1) {
                index = (index_min + index_max) >> 1;
                if (CANID == (RxStdCANID_1[index]& 0x3FFF)) {
                    RxStdDLC_1[index] = DLC;
                    if (RTR) {
                        RxStdDLC_1[index] = 0x80;
                        return;
                    }
                    if (0x4000 == (0x4000 & RxStdCANID_1[index])) {
                        //from transmit which needs short frame response
                        RxStdCANID_1[index] = RxStdCANID_1[index] & 0xBFFF; //clear flag from transmit which needs short frame response
                    }
                    int8_t i;
                    for (i = 0; i < DLC; i++) {
                        RxStdData_1[index][i] = *(pCAN_Data + i);
                    }
                    return;
                }
                if (CANID < (RxStdCANID_1[index]& 0x3FFF)) {
                    index_max = index;
                } else {
                    index_min = index;
                }
                if (index_min == index_max) break;
                if (index_max == (index_min + 1)) {
                    //in case stuck (Never occur for index_min == index_max)
                    if (CANID == (RxStdCANID_1[index_min]& 0x3FFF)) {
                        RxStdDLC_1[index_min] = DLC;
                        if (RTR) {
                            RxStdDLC_1[index_min] = 0x80;
                            return;
                        }
                        if (0x4000 == (0x4000 & RxStdCANID_1[index_min])) {
                            //from transmit which needs short frame response
                            RxStdCANID_1[index_min] = RxStdCANID_1[index_min] & 0xBFFF; //clear flag from transmit which needs short frame response
                        }
                        int8_t i;
                        for (i = 0; i < DLC; i++) {
                            RxStdData_1[index_min][i] = *(pCAN_Data + i);
                        }
                        return;
                    } else if (CANID == (RxStdCANID_1[index_max]& 0x3FFF)) {
                        RxStdDLC_1[index_max] = DLC;
                        if (RTR) {
                            RxStdDLC_1[index_max] = 0x80;
                            return;
                        }
                        if (0x4000 == (0x4000 & RxStdCANID_1[index_max])) {
                            //from transmit which needs short frame response
                            RxStdCANID_1[index_max] = RxStdCANID_1[index_max] & 0xBFFF; //clear flag from transmit which needs short frame response
                        }
                        int8_t i;
                        for (i = 0; i < DLC; i++) {
                            RxStdData_1[index_max][i] = *(pCAN_Data + i);
                        }
                        return;
                    } else break;
                }
            }
        }

    }

    //CANID belongs to RxStdCANID_Long_1? (long receive std frame) 
    // We clear Bit15 Bit14 in comparing standard CAN ID because Bit15 used in Data integrity of long frame, Bit 14 used for Response Flag for transmit which needs response
    if (RxStdCANID_Long_1 != NULL) {
        index_min = 0;
        //index_max = sizeof (RxStdCANID_Long_1) - 1;
        index_max=sizeof_RxStdCANID_Long_1-1;
        if ((CANID >= (RxStdCANID_Long_1[index_min] &0x3FFF)) && (CANID <= (RxStdCANID_Long_1[index_max]&0x3FFF))) {
            while (1) {
                index = (index_min + index_max) >> 1;
                if (CANID == (RxStdCANID_Long_1[index]&0x3FFF)) {
                    if (RTR) {
                        RxStdDLC_Long_1[index][0] = 0x80;
                        return;
                    } else if (DLC > 0) {
                        uint8_t MaxFramNo, Index4CurrentFrameNo;
                        if (CAN_myObj1.FrameNoIs0_based) {
                            // 0-based Frame No
                            //MaxFramNo = CAN_myObj1.RxStdFrameQTY4long - 1;
                            MaxFramNo = CAN1_RX_MAX_STD_FRAME_QTY4LONG - 1;
                            Index4CurrentFrameNo = *pCAN_Data;
                        } else {
                            // 1-based Frame No
                            //MaxFramNo = CAN_myObj1.RxStdFrameQTY4long;
                            MaxFramNo = CAN1_RX_MAX_STD_FRAME_QTY4LONG;
                            Index4CurrentFrameNo = *pCAN_Data - 1;
                            if (*pCAN_Data == 0) {
                                //error frame no
                                return;
                            }
                        }
                        if ((*pCAN_Data) <= MaxFramNo) {                        
                            RxStdDLC_Long_1[index][Index4CurrentFrameNo] = DLC;
                            if (0 == Index4CurrentFrameNo) {
                                //clear Data integrity flag in bit15 of CAN ID (bit15=1 : non-integrity)
                                RxStdCANID_Long_1[index] = RxStdCANID_Long_1[index] | 0x8000;
                            }
                            int8_t i;
                            if (0x4000 == (0x4000 & RxStdCANID_Long_1[index])) {
                                //This CAN ID is "Transmit frame's respond long frame
                                //Check Channel A Seq0 to 3, its response CAN ID, if it is, verify Max FramwNo
                                //for (i = 0; i<sizeof (TxResponseCANID_Index_A_1); i++) {
                                for (i = 0; i<4; i++) {                                    
                                    if (0x8000 == (TxResponseCANID_Index_A_1[i] & 0xc000)) {
                                        //Standard long frame response
                                        if (index == (TxResponseCANID_Index_A_1[i] & 0x3FFF)) {
                                            if (ResponseMaxFrameNo_A_1[i] == *pCAN_Data) {
                                                RxStdCANID_Long_1[index] = RxStdCANID_Long_1[index] & 0xBFFF; //clear flag from transmit which needs long frame response
                                            }
                                            goto exitSearch1;
                                        }
                                    }
                                }
                                //Check Channel B Seq0 to 3, its response CAN ID, if it is, verify Max FramwNo
#ifdef USE_CAN1_CHANNELB
                                for (i = 0; i<4; i++) {                                    
                                    if (0x8000 == (TxResponseCANID_Index_B_1[i] & 0xc000)) {
                                        //Standard long frame response
                                        if (index == (TxResponseCANID_Index_B_1[i] & 0x3FFF)) {
                                            if (ResponseMaxFrameNo_B_1[i] == *pCAN_Data) {
                                                RxStdCANID_Long_1[index] = RxStdCANID_Long_1[index] & 0xBFFF; //clear flag from transmit which needs long frame response
                                            }
                                            goto exitSearch1;
                                        }
                                    }
                                }
#endif
                                //Check Channel C Seq0 to 3, its response CAN ID, if it is, verify Max FramwNo
#ifdef USE_CAN1_CHANNELC
                                for (i = 0; i<4; i++) {
                                    if (0x8000 == (TxResponseCANID_Index_C_1[i] & 0xc000)) {
                                        //Standard long frame response
                                        if (index == (TxResponseCANID_Index_C_1[i] & 0x3FFF)) {
                                            if (ResponseMaxFrameNo_C_1[i] == *pCAN_Data) {
                                                RxStdCANID_Long_1[index] = RxStdCANID_Long_1[index] & 0xBFFF; //clear flag from transmit which needs long frame response
                                            }
                                            goto exitSearch1;
                                        }
                                    }
                                }
#endif
                            }

exitSearch1:
                            for (i = 0; i < DLC; i++) {
                                RxStdData_Long_1[index][Index4CurrentFrameNo][i] = *(pCAN_Data + i);
                            }
                            if (LastStdMAXFrmNo_1[index] == *pCAN_Data) {
                                //Set Data integrity flag in bit15 of CAN ID (bit15=0 : integrity)
                                RxStdCANID_Long_1[index] = RxStdCANID_Long_1[index] & 0x7FFF;
                            }
                            return;
                        }


                    }
                    return;
                }
                if (CANID < (RxStdCANID_Long_1[index]&0x3FFF)) {
                    index_max = index;
                } else {
                    index_min = index;
                }
                if (index_min == index_max) break;
                if (index_max == (index_min + 1)) {
                    //in case stuck (Never occur for index_min == index_max)
                    if (CANID == (RxStdCANID_Long_1[index_min]&0x3FFF)) {
                        if (RTR) {
                            RxStdDLC_Long_1[index_min][0] = 0x80;
                            return;
                        } else if (DLC > 0) {
                            uint8_t MaxFramNo, Index4CurrentFrameNo;
                            if (CAN_myObj1.FrameNoIs0_based) {
                                // 0-based Frame No
                                //MaxFramNo = CAN_myObj1.RxStdFrameQTY4long - 1;
                                MaxFramNo = CAN1_RX_MAX_STD_FRAME_QTY4LONG - 1;
                                Index4CurrentFrameNo = *pCAN_Data;
                            } else {
                                // 1-based Frame No
                                //MaxFramNo = CAN_myObj1.RxStdFrameQTY4long;
                                MaxFramNo = CAN1_RX_MAX_STD_FRAME_QTY4LONG;
                                Index4CurrentFrameNo = *pCAN_Data - 1;
                                if (*pCAN_Data == 0) {
                                    //error frame no
                                    return;
                                }
                            }
                            if ((*pCAN_Data) <= MaxFramNo) {
                                RxStdDLC_Long_1[index_min][Index4CurrentFrameNo] = DLC;
                                if (0 == Index4CurrentFrameNo) {
                                    //clear Data integrity flag in bit15 of CAN ID (bit15=1 : non-integrity)
                                    RxStdCANID_Long_1[index_min] = RxStdCANID_Long_1[index_min] | 0x8000;
                                }
                                int8_t i;
                                if (0x4000 == (0x4000 & RxStdCANID_Long_1[index_min])) {
                                    //This CAN ID is "Transmit frame's respond long frame
                                    //Check Channel A Seq0 to 3, its response CAN ID, if it is, verify Max FramwNo
                                    //for (i = 0; i<sizeof (TxResponseCANID_Index_A_1); i++) {
                                    for (i = 0; i<4; i++) {                                        
                                        if (0x8000 == (TxResponseCANID_Index_A_1[i] & 0xc000)) {
                                            //Standard long frame response
                                            if (index_min == (TxResponseCANID_Index_A_1[i] & 0x3FFF)) {
                                                if (ResponseMaxFrameNo_A_1[i] == *pCAN_Data) {
                                                    RxStdCANID_Long_1[index_min] = RxStdCANID_Long_1[index_min] & 0xBFFF; //clear flag from transmit which needs long frame response
                                                }
                                                goto exitSearch2;
                                            }
                                        }
                                    }
                                    //Check Channel B Seq0 to 3, its response CAN ID, if it is, verify Max FramwNo
#ifdef USE_CAN1_CHANNELB
                                    for (i = 0; i<4; i++) {
                                        if (0x8000 == (TxResponseCANID_Index_B_1[i] & 0xc000)) {
                                            //Standard long frame response
                                            if (index_min == (TxResponseCANID_Index_B_1[i] & 0x3FFF)) {
                                                if (ResponseMaxFrameNo_B_1[i] == *pCAN_Data) {
                                                    RxStdCANID_Long_1[index_min] = RxStdCANID_Long_1[index_min] & 0xBFFF; //clear flag from transmit which needs long frame response
                                                }
                                                goto exitSearch2;
                                            }
                                        }
                                    }
#endif
                                    //Check Channel C Seq0 to 3, its response CAN ID, if it is, verify Max FramwNo
#ifdef USE_CAN1_CHANNELC
                                    for (i = 0; i<4; i++) {
                                        if (0x8000 == (TxResponseCANID_Index_C_1[i] & 0xc000)) {
                                            //Standard long frame response
                                            if (index_min == (TxResponseCANID_Index_C_1[i] & 0x3FFF)) {
                                                if (ResponseMaxFrameNo_C_1[i] == *pCAN_Data) {
                                                    RxStdCANID_Long_1[index_min] = RxStdCANID_Long_1[index_min] & 0xBFFF; //clear flag from transmit which needs long frame response
                                                }
                                                goto exitSearch2;
                                            }
                                        }
                                    }
#endif

                                }


exitSearch2:
                                for (i = 0; i < DLC; i++) {
                                    RxStdData_Long_1[index_min][Index4CurrentFrameNo][i] = *(pCAN_Data + i);
                                }
                                if (LastStdMAXFrmNo_1[index_min] == *pCAN_Data) {
                                    //Set Data integrity flag in bit15 of CAN ID (bit15=0 : integrity)
                                    RxStdCANID_Long_1[index_min] = RxStdCANID_Long_1[index_min] & 0x7FFF;
                                }
                                return;
                            }


                        }
                        return;
                    } else if (CANID == (RxStdCANID_Long_1[index_max]&0x3FFF)) {
                        if (RTR) {
                            RxStdDLC_Long_1[index_max][0] = 0x80;
                            return;
                        } else if (DLC > 0) {
                            uint8_t MaxFramNo, Index4CurrentFrameNo;
                            if (CAN_myObj1.FrameNoIs0_based) {
                                // 0-based Frame No
                                //MaxFramNo = CAN_myObj1.RxStdFrameQTY4long - 1;
                                MaxFramNo = CAN1_RX_MAX_STD_FRAME_QTY4LONG - 1;
                                Index4CurrentFrameNo = *pCAN_Data;
                            } else {
                                // 1-based Frame No
                                //MaxFramNo = CAN_myObj1.RxStdFrameQTY4long;
                                MaxFramNo = CAN1_RX_MAX_STD_FRAME_QTY4LONG ;
                                Index4CurrentFrameNo = *pCAN_Data - 1;
                                if (*pCAN_Data == 0) {
                                    //error frame no
                                    return;
                                }
                            }
                            if ((*pCAN_Data) <= MaxFramNo) {
                                RxStdDLC_Long_1[index_max][Index4CurrentFrameNo] = DLC;
                                if (0 == Index4CurrentFrameNo) {
                                    //clear Data integrity flag in bit15 of CAN ID (bit15=1 : non-integrity)
                                    RxStdCANID_Long_1[index_max] = RxStdCANID_Long_1[index_max] | 0x8000;
                                }
                                int8_t i;
                                if (0x4000 == (0x4000 & RxStdCANID_Long_1[index_max])) {
                                    //This CAN ID is "Transmit frame's respond long frame
                                    //Check Channel A Seq0 to 3, its response CAN ID, if it is, verify Max FramwNo
                                    //for (i = 0; i<sizeof (TxResponseCANID_Index_A_1); i++) {
                                    for (i = 0; i<4; i++) {
                                        if (0x8000 == (TxResponseCANID_Index_A_1[i] & 0xc000)) {
                                            //Standard long frame response
                                            if (index_max == (TxResponseCANID_Index_A_1[i] & 0x3FFF)) {
                                                if (ResponseMaxFrameNo_A_1[i] == *pCAN_Data) {
                                                    RxStdCANID_Long_1[index_max] = RxStdCANID_Long_1[index_max] & 0xBFFF; //clear flag from transmit which needs long frame response
                                                }
                                                goto exitSearch3;
                                            }
                                        }
                                    }
                                    //Check Channel B Seq0 to 3, its response CAN ID, if it is, verify Max FramwNo
#ifdef USE_CAN1_CHANNELB
                                    for (i = 0; i<4; i++) {
                                        if (0x8000 == (TxResponseCANID_Index_B_1[i] & 0xc000)) {
                                            //Standard long frame response
                                            if (index_max == (TxResponseCANID_Index_B_1[i] & 0x3FFF)) {
                                                if (ResponseMaxFrameNo_B_1[i] == *pCAN_Data) {
                                                    RxStdCANID_Long_1[index_max] = RxStdCANID_Long_1[index_max] & 0xBFFF; //clear flag from transmit which needs long frame response
                                                }
                                                goto exitSearch3;
                                            }
                                        }
                                    }
                                    
#endif                                    
                                    //Check Channel C Seq0 to 3, its response CAN ID, if it is, verify Max FramwNo
#ifdef USE_CAN1_CHANNELC
                                    for (i = 0; i<4; i++) {
                                        if (0x8000 == (TxResponseCANID_Index_C_1[i] & 0xc000)) {
                                            //Standard long frame response
                                            if (index_max == (TxResponseCANID_Index_C_1[i] & 0x3FFF)) {
                                                if (ResponseMaxFrameNo_C_1[i] == *pCAN_Data) {
                                                    RxStdCANID_Long_1[index_max] = RxStdCANID_Long_1[index_max] & 0xBFFF; //clear flag from transmit which needs long frame response
                                                }
                                                goto exitSearch3;
                                            }
                                        }
                                    }
#endif                                     
                                }                                

                                exitSearch3:
                                for (i = 0; i < DLC; i++) {
                                    RxStdData_Long_1[index_max][Index4CurrentFrameNo][i] = *(pCAN_Data + i);
                                }
                                if (LastStdMAXFrmNo_1[index_max] == *pCAN_Data) {
                                    //Set Data integrity flag in bit15 of CAN ID (bit15=0 : integrity)
                                    RxStdCANID_Long_1[index_max] = RxStdCANID_Long_1[index_max] & 0x7FFF;
                                }
                                return;
                            }  //end of "if ((*pCAN_Data) <= MaxFramNo)"
                            }  // end of "else if (DLC > 0)"
                            

                        } else break;
                        
                    }   //end of "if (index_max == (index_min + 1))"
                }   //ENF OF "while (1)"
            
            }   //end of "CANID received belongs to range of long frame CAN ID"
        
        }  //end of "if (RxStdCANID_Long_1 != NULL)"
    
}
/**
 * For CAN1, Put one Rx Ext frame  into our high level buffer such as : RxExtData_1[],RxExtData_Long_1[][][]
 *   And for long frame, set/clear Data integrity flag in Bit31 of RxExtCANID_Long_1
 * for respond frame, when received response, clear Bit30 of RxExtCANID_1 or clear Bit30 of RxExtCANID_Long_1 when Max frame No is what I expect.
 *   To set Bit30 for RxExtCANID_1 or RxExtCANID_Long_1 is in function for transmitting when expecting response
 * @param CANID
 * @param RTR
 * @param DLC
 * @param pCAN_Data
 */
void putCAN1RxExtCAN2Buffer(uint32_t CANID, bool RTR, uint8_t DLC, uint8_t * pCAN_Data) {
    uint16_t index_min, index_max, index;
    if (CAN_myObj1.EnableSyn && (0==CAN_myObj1.SynUseStdCANID)) {
        //modify new, because we don't need to set it to 1 if Bit31  of CAN_myObj1.SyncCANID=1
        //if (CANID==(CAN_myObj1.SyncCANID & 0x7FFFffff))  CAN_myObj1.SyncCANID = CAN_myObj1.SyncCANID | 0x80000000;
        if (CANID==CAN_myObj1.SyncCANID )  CAN_myObj1.SyncCANID = CAN_myObj1.SyncCANID | 0x80000000;
    }  
    // We clear Bit31 Bit30 in comparing standard CAN ID because Bit31 used in Data integrity of long frame, Bit 30 used for Response Flag for transmit which needs response        
    //CANID belongs to RxExtCANID_1? (short receive Ext frame)
    if (RxExtCANID_1 != NULL) {
        index_min = 0;
        //index_max = sizeof (RxExtCANID_1) - 1;
        index_max=sizeof_RxExtCANID_1-1;
        if ((CANID >= (RxExtCANID_1[index_min] & 0x3FFFFFFF)) && (CANID <= (RxExtCANID_1[index_max]& 0x3FFFFFFF))) {
            while (1) {
                index = (index_min + index_max) >> 1;
                if (CANID == (RxExtCANID_1[index]& 0x3FFFFFFF)) {
                    RxExtDLC_1[index] = DLC;
                    if (RTR) {
                        RxExtDLC_1[index] = 0x80;
                        return;
                    }
                    if (0x40000000 == (0x40000000 & RxExtCANID_1[index])) {
                        //from transmit which needs short frame response
                        RxExtCANID_1[index] = RxExtCANID_1[index] & 0xBFFFffff; //clear flag from transmit which needs short frame response
                    }
                    int8_t i;
                    for (i = 0; i < DLC; i++) {
                        RxExtData_1[index][i] = *(pCAN_Data + i);
                    }
                    return;
                }
                if (CANID < (RxExtCANID_1[index]& 0x3FFFFFFF)) {
                    index_max = index;
                } else {
                    index_min = index;
                }
                if (index_min == index_max) break;
                if (index_max == (index_min + 1)) {
                    //in case stuck (Never occur for index_min == index_max)
                    if (CANID == (RxExtCANID_1[index_min]& 0x3FFFFFFF)) {
                        RxExtDLC_1[index_min] = DLC;
                        if (RTR) {
                            RxExtDLC_1[index_min] = 0x80;
                            return;
                        }
                        if (0x40000000 == (0x40000000 & RxExtCANID_1[index_min])) {
                            //from transmit which needs short frame response
                            RxExtCANID_1[index_min] = RxExtCANID_1[index_min] & 0xBFFFffff; //clear flag from transmit which needs short frame response
                        }
                        int8_t i;
                        for (i = 0; i < DLC; i++) {
                            RxExtData_1[index_min][i] = *(pCAN_Data + i);
                        }
                        return;
                    } else if (CANID == (RxExtCANID_1[index_max]& 0x3FFFFFFF)) {
                        RxExtDLC_1[index_max] = DLC;
                        if (RTR) {
                            RxExtDLC_1[index_max] = 0x80;
                            return;
                        }
                        if (0x40000000 == (0x40000000 & RxExtCANID_1[index_max])) {
                            //from transmit which needs short frame response
                            RxExtCANID_1[index_max] = RxExtCANID_1[index_max] & 0xBFFFffff; //clear flag from transmit which needs short frame response
                        }
                        int8_t i;
                        for (i = 0; i < DLC; i++) {
                            RxExtData_1[index_max][i] = *(pCAN_Data + i);
                        }
                        return;
                    } else break;
                }
            }
        }

    }

    //CANID belongs to RxExtCANID_Long_1? (long receive Ext frame) 
    // We clear Bit31 Bit30 in comparing standard CAN ID because Bit31 used in Data integrity of long frame, Bit 30 used for Response Flag for transmit which needs response

    if (RxExtCANID_Long_1 != NULL) {
        index_min = 0;
        //index_max = sizeof (RxExtCANID_Long_1) - 1;
        index_max=sizeof_RxExtCANID_Long_1-1;
        if ((CANID >= (RxExtCANID_Long_1[index_min] &0x3FFFffff)) && (CANID <= (RxExtCANID_Long_1[index_max]&0x3FFFffff))) {
            while (1) {
                index = (index_min + index_max) >> 1;
                if (CANID == (RxExtCANID_Long_1[index]&0x3FFFffff)) {
                    if (RTR) {
                        RxExtDLC_Long_1[index][0] = 0x80;
                        return;
                    } else if (DLC > 0) {
                        uint8_t MaxFramNo, Index4CurrentFrameNo;
                        if (CAN_myObj1.FrameNoIs0_based) {
                            // 0-based Frame No
                            //MaxFramNo = CAN_myObj1.RxExtFrameQTY4long - 1;
                            MaxFramNo = CAN1_RX_MAX_EXT_FRAME_QTY4LONG - 1;
                            Index4CurrentFrameNo = *pCAN_Data;
                        } else {
                            // 1-based Frame No
                            //MaxFramNo = CAN_myObj1.RxExtFrameQTY4long;
                            MaxFramNo = CAN1_RX_MAX_EXT_FRAME_QTY4LONG ;
                            Index4CurrentFrameNo = *pCAN_Data - 1;
                            if (*pCAN_Data == 0) {
                                //error frame no
                                return;
                            }
                        }
                        if ((*pCAN_Data) <= MaxFramNo) {
                            RxExtDLC_Long_1[index][Index4CurrentFrameNo] = DLC;
                            if (0 == Index4CurrentFrameNo) {
                                //clear Data integrity flag in bit31 of CAN ID (bit31=1 : non-integrity)
                                RxExtCANID_Long_1[index] = RxExtCANID_Long_1[index] | 0x80000000;
                            }

                            int8_t i;
                            if (0x40000000 == (0x40000000 & RxExtCANID_Long_1[index])) {
                                //This CAN ID is "Transmit frame's respond long frame
                                //Check Channel A Seq0 to 3, its response CAN ID, if it is, verify Max FramwNo
                                //for (i = 0; i<sizeof (TxResponseCANID_Index_A_1); i++) {
                                for (i = 0; i<4; i++) {                                ;
                                    if (0xc000 == (TxResponseCANID_Index_A_1[i] & 0xc000)) {
                                        //ext long frame response
                                        if (index == (TxResponseCANID_Index_A_1[i] & 0x3FFF)) {
                                            if (ResponseMaxFrameNo_A_1[i] == *pCAN_Data) {
                                                RxExtCANID_Long_1[index] = RxExtCANID_Long_1[index] & 0xBFFFffff; //clear flag from transmit which needs long frame response
                                            }
                                            goto exitSearch1;
                                        }
                                    }
                                }
                                //Check Channel B Seq0 to 3, its response CAN ID, if it is, verify Max FramwNo
#ifdef USE_CAN1_CHANNELB
                                for (i = 0; i<4; i++) {
                                    if (0xc000 == (TxResponseCANID_Index_B_1[i] & 0xc000)) {
                                        //ext long frame response
                                        if (index == (TxResponseCANID_Index_B_1[i] & 0x3FFF)) {
                                            if (ResponseMaxFrameNo_B_1[i] == *pCAN_Data) {
                                                RxExtCANID_Long_1[index] = RxExtCANID_Long_1[index] & 0xBFFFffff; //clear flag from transmit which needs long frame response
                                            }
                                            goto exitSearch1;
                                        }
                                    }
                                }
#endif

                                //Check Channel C Seq0 to 3, its response CAN ID, if it is, verify Max FramwNo
#ifdef USE_CAN1_CHANNELC
                                for (i = 0; i<4; i++) {
                                    if (0xc000 == (TxResponseCANID_Index_C_1[i] & 0xc000)) {
                                        //ext long frame response
                                        if (index == (TxResponseCANID_Index_C_1[i] & 0x3FFF)) {
                                            if (ResponseMaxFrameNo_C_1[i] == *pCAN_Data) {
                                                RxExtCANID_Long_1[index] = RxExtCANID_Long_1[index] & 0xBFFFffff; //clear flag from transmit which needs long frame response
                                            }
                                            goto exitSearch1;
                                        }
                                    }
                                }
#endif
                            }

exitSearch1:
                            for (i = 0; i < DLC; i++) {
                                RxExtData_Long_1[index][Index4CurrentFrameNo][i] = *(pCAN_Data + i);
                            }
                            //LastExtMAXFrmNo_1[index] = *pCAN_Data;
                            if (LastExtMAXFrmNo_1[index] == *pCAN_Data) {
                                //Set Data integrity flag in bit15 of CAN ID (bit31=0 : integrity)
                                RxExtCANID_Long_1[index] = RxExtCANID_Long_1[index] & 0x7FFFFFFF;
                            }
                            return;
                        }


                    }
                    return;
                }
                if (CANID < (RxExtCANID_Long_1[index]&0x3FFFffff)) {
                    index_max = index;
                } else {
                    index_min = index;
                }
                if (index_min == index_max) break;
                if (index_max == (index_min + 1)) {
                    //in case stuck (Never occur for index_min == index_max)
                    if (CANID == (RxExtCANID_Long_1[index_min]&0x3FFFffff)) {
                        if (RTR) {
                            RxExtDLC_Long_1[index_min][0] = 0x80;
                            return;
                        } else if (DLC > 0) {
                            uint8_t MaxFramNo, Index4CurrentFrameNo;
                            if (CAN_myObj1.FrameNoIs0_based) {
                                // 0-based Frame No
                                //MaxFramNo = CAN_myObj1.RxExtFrameQTY4long - 1;
                                MaxFramNo = CAN1_RX_MAX_EXT_FRAME_QTY4LONG - 1;
                                Index4CurrentFrameNo = *pCAN_Data;
                            } else {
                                // 1-based Frame No
                                //MaxFramNo = CAN_myObj1.RxExtFrameQTY4long;
                                MaxFramNo = CAN1_RX_MAX_EXT_FRAME_QTY4LONG ;
                                Index4CurrentFrameNo = *pCAN_Data - 1;
                                if (*pCAN_Data == 0) {
                                    //error frame no
                                    return;
                                }
                            }
                            if ((*pCAN_Data) <= MaxFramNo) {
                                RxExtDLC_Long_1[index_min][Index4CurrentFrameNo] = DLC;
                                if (0 == Index4CurrentFrameNo) {
                                    //clear Data integrity flag in bit31 of CAN ID (bit31=1 : non-integrity)
                                    RxExtCANID_Long_1[index_min] = RxExtCANID_Long_1[index_min] | 0x80000000;
                                }

                                int8_t i;
                                if (0x40000000 == (0x40000000 & RxExtCANID_Long_1[index_min])) {
                                    //This CAN ID is "Transmit frame's respond long frame
                                    //Check Channel A Seq0 to 3, its response CAN ID, if it is, verify Max FramwNo
                                    //for (i = 0; i<sizeof (TxResponseCANID_Index_A_1); i++) {
                                    for (i = 0; i<4; i++) {     
                                        if (0xc000 == (TxResponseCANID_Index_A_1[i] & 0xc000)) {
                                            //ext long frame response
                                            if (index_min == (TxResponseCANID_Index_A_1[i] & 0x3FFF)) {
                                                if (ResponseMaxFrameNo_A_1[i] == *pCAN_Data) {
                                                    RxExtCANID_Long_1[index_min] = RxExtCANID_Long_1[index_min] & 0xBFFFffff; //clear flag from transmit which needs long frame response
                                                }
                                                goto exitSearch2;
                                            }
                                        }
                                    }
                                    //Check Channel B Seq0 to 3, its response CAN ID, if it is, verify Max FramwNo
#ifdef USE_CAN1_CHANNELB
                                    for (i = 0; i<4; i++) {
                                        if (0xc000 == (TxResponseCANID_Index_B_1[i] & 0xc000)) {
                                            //ext long frame response
                                            if (index_min == (TxResponseCANID_Index_B_1[i] & 0x3FFF)) {
                                                if (ResponseMaxFrameNo_B_1[i] == *pCAN_Data) {
                                                    RxExtCANID_Long_1[index_min] = RxExtCANID_Long_1[index_min] & 0xBFFFffff; //clear flag from transmit which needs long frame response
                                                }
                                                goto exitSearch2;
                                            }
                                        }
                                    }
#endif
                                    //Check Channel C Seq0 to 3, its response CAN ID, if it is, verify Max FramwNo
#ifdef USE_CAN1_CHANNELC
                                    for (i = 0; i<4; i++) {
                                        if (0xc000 == (TxResponseCANID_Index_C_1[i] & 0xc000)) {
                                            //ext long frame response
                                            if (index_min == (TxResponseCANID_Index_C_1[i] & 0x3FFF)) {
                                                if (ResponseMaxFrameNo_C_1[i] == *pCAN_Data) {
                                                    RxExtCANID_Long_1[index_min] = RxExtCANID_Long_1[index_min] & 0xBFFFffff; //clear flag from transmit which needs long frame response
                                                }
                                                goto exitSearch2;
                                            }
                                        }
                                    }
#endif
                                }

exitSearch2:
                                for (i = 0; i < DLC; i++) {
                                    RxExtData_Long_1[index_min][Index4CurrentFrameNo][i] = *(pCAN_Data + i);
                                }
                                //LastExtMAXFrmNo_1[index_min] = *pCAN_Data;
                                if (LastExtMAXFrmNo_1[index_min] == *pCAN_Data) {
                                    //Set Data integrity flag in bit15 of CAN ID (bit31=0 : integrity)
                                    RxExtCANID_Long_1[index_min] = RxExtCANID_Long_1[index_min] & 0x7FFFFFFF;
                                }
                                return;
                            }


                        }
                        return;
                    } else if (CANID == (RxExtCANID_Long_1[index_max]&0x3FFFffff)) {
                        if (RTR) {
                            RxExtDLC_Long_1[index_max][0] = 0x80;
                            return;
                        } else if (DLC > 0) {
                            uint8_t MaxFramNo, Index4CurrentFrameNo;
                            if (CAN_myObj1.FrameNoIs0_based) {
                                // 0-based Frame No
                                //MaxFramNo = CAN_myObj1.RxExtFrameQTY4long - 1;
                                MaxFramNo = CAN1_RX_MAX_EXT_FRAME_QTY4LONG - 1;
                                Index4CurrentFrameNo = *pCAN_Data;
                            } else {
                                // 1-based Frame No
                                //MaxFramNo = CAN_myObj1.RxExtFrameQTY4long;
                                MaxFramNo = CAN1_RX_MAX_EXT_FRAME_QTY4LONG ;
                                Index4CurrentFrameNo = *pCAN_Data - 1;
                                if (*pCAN_Data == 0) {
                                    //error frame no
                                    return;
                                }
                            }
                            if ((*pCAN_Data) <= MaxFramNo) {
                                RxExtDLC_Long_1[index_max][Index4CurrentFrameNo] = DLC;
                                if (0 == Index4CurrentFrameNo) {
                                    //clear Data integrity flag in bit31 of CAN ID (bit31=1 : non-integrity)
                                    RxExtCANID_Long_1[index_max] = RxExtCANID_Long_1[index_max] | 0x80000000;
                                }

                                int8_t i;
                                if (0x40000000 == (0x40000000 & RxExtCANID_Long_1[index_max])) {
                                    //This CAN ID is "Transmit frame's respond long frame
                                    //Check Channel A Seq0 to 3, its response CAN ID, if it is, verify Max FramwNo
                                    //for (i = 0; i<sizeof (TxResponseCANID_Index_A_1); i++) {
                                    for (i = 0; i<4; i++) {     
                                        if (0xc000 == (TxResponseCANID_Index_A_1[i] & 0xc000)) {
                                            //ext long frame response
                                            if (index_max == (TxResponseCANID_Index_A_1[i] & 0x3FFF)) {
                                                if (ResponseMaxFrameNo_A_1[i] == *pCAN_Data) {
                                                    RxExtCANID_Long_1[index_max] = RxExtCANID_Long_1[index_max] & 0xBFFFffff; //clear flag from transmit which needs long frame response
                                                }
                                                goto exitSearch3;
                                            }
                                        }
                                    }
                                    //Check Channel B Seq0 to 3, its response CAN ID, if it is, verify Max FramwNo
#ifdef USE_CAN1_CHANNELB
                                    for (i = 0; i<4; i++) {
                                        if (0xc000 == (TxResponseCANID_Index_B_1[i] & 0xc000)) {
                                            //ext long frame response
                                            if (index_max == (TxResponseCANID_Index_B_1[i] & 0x3FFF)) {
                                                if (ResponseMaxFrameNo_B_1[i] == *pCAN_Data) {
                                                    RxExtCANID_Long_1[index_max] = RxExtCANID_Long_1[index_max] & 0xBFFFffff; //clear flag from transmit which needs long frame response
                                                }
                                                goto exitSearch3;
                                            }
                                        }
                                    }
#endif
                                    //Check Channel C Seq0 to 3, its response CAN ID, if it is, verify Max FramwNo
#ifdef USE_CAN1_CHANNELC
                                    for (i = 0; i<4; i++) {
                                        if (0xc000 == (TxResponseCANID_Index_C_1[i] & 0xc000)) {
                                            //ext long frame response
                                            if (index_max == (TxResponseCANID_Index_C_1[i] & 0x3FFF)) {
                                                if (ResponseMaxFrameNo_C_1[i] == *pCAN_Data) {
                                                    RxExtCANID_Long_1[index_max] = RxExtCANID_Long_1[index_max] & 0xBFFFffff; //clear flag from transmit which needs long frame response
                                                }
                                                goto exitSearch3;
                                            }
                                        }
                                    }
#endif
                                }

exitSearch3:
                                for (i = 0; i < DLC; i++) {
                                    RxExtData_Long_1[index_max][Index4CurrentFrameNo][i] = *(pCAN_Data + i);
                                }
                                //LastExtMAXFrmNo_1[index_max] = *pCAN_Data;
                                if (LastExtMAXFrmNo_1[index_max] == *pCAN_Data) {
                                    //Set Data integrity flag in bit15 of CAN ID (bit31=0 : integrity)
                                    RxExtCANID_Long_1[index_max] = RxExtCANID_Long_1[index_max] & 0x7FFFFFFF;
                                }
                                return;
                            }


                        }
                        return;
                    } else break;
                }
            } //while (1)  

        } //end of "if  CANID belongs to [min, max]"

    } //end of "Long frame handle:  long frame poiny is not NULL"

}

#endif


#ifdef USE_CAN2

/**
 * For CAN2, Put one Rx Standard frame  into our high level buffer such as : RxStdData_2[],RxStdData_Long_2[][][]
 * *  And for long frame, set/clear Data integrity flag in Bit15 of RxStdCANID_Long_2
 * for respond frame, when received response, clear Bit14 of RxStdCANID_2 or clear Bit14 of RxStdCANID_Long_2 when Max frame No is what I expect.
 *   To set Bit14 for RxStdCANID_2 or RxStdCANID_Long_2 is in function for transmitting when expecting response
 * @param CANID
 * @param RTR
 * @param DLC
 * @param pCAN_Data
 */
void putCAN2RxStdCAN2Buffer(uint16_t CANID, bool RTR, uint8_t DLC, uint8_t * pCAN_Data) {
    uint16_t index_min, index_max, index;
    if (CAN_myObj2.EnableSyn && CAN_myObj2.SynUseStdCANID) {
        //modify new, because we don't need to set it to 1 if Bit31  of CAN_myObj2.SyncCANID=1
        //if (CANID==(CAN_myObj2.SyncCANID & 0x7FFFffff))  CAN_myObj2.SyncCANID = CAN_myObj2.SyncCANID | 0x80000000;
        if (CANID==CAN_myObj2.SyncCANID )  CAN_myObj2.SyncCANID = CAN_myObj2.SyncCANID | 0x80000000;
    }  
    //CANID belongs to RxStdCANID_2? (short receive std frame)
    // We clear Bit15 Bit14 in comparing standard CAN ID because Bit15 used in Data integrity of long frame, Bit 14 used for Response Flag for transmit which needs response
    if (RxStdCANID_2 != NULL) {
        index_min = 0;
        index_max = sizeof_RxStdCANID_2 - 1;
        if ((CANID >= (RxStdCANID_2[index_min] & 0x3fff)) && (CANID <= (RxStdCANID_2[index_max] & 0x3fff))) {
            while (1) {
                index = (index_min + index_max) >> 1;
                if (CANID == (RxStdCANID_2[index] & 0x3fff)) {
                    RxStdDLC_2[index] = DLC;
                    if (RTR) {
                        RxStdDLC_2[index] = 0x80;
                        return;
                    }
                    if (0x4000 == (0x4000 & RxStdCANID_2[index])) {
                        //from transmit which needs short frame response
                        RxStdCANID_2[index] = RxStdCANID_2[index] & 0xBFFF; //clear flag from transmit which needs short frame response
                    }
                    int8_t i;
                    for (i = 0; i < DLC; i++) {
                        RxStdData_2[index][i] = *(pCAN_Data + i);
                    }
                    return;
                }
                if (CANID < (RxStdCANID_2[index] & 0x3fff)) {
                    index_max = index;
                } else {
                    index_min = index;
                }
                if (index_min == index_max) break;
                if (index_max == (index_min + 1)) {
                    //in case stuck (Never occur for index_min == index_max)
                    if (CANID == (RxStdCANID_2[index_min] & 0x3fff)) {
                        RxStdDLC_2[index_min] = DLC;
                        if (RTR) {
                            RxStdDLC_2[index_min] = 0x80;
                            return;
                        }
                        if (0x4000 == (0x4000 & RxStdCANID_2[index_min])) {
                            //from transmit which needs short frame response
                            RxStdCANID_2[index_min] = RxStdCANID_2[index_min] & 0xBFFF; //clear flag from transmit which needs short frame response
                        }
                        int8_t i;
                        for (i = 0; i < DLC; i++) {
                            RxStdData_2[index_min][i] = *(pCAN_Data + i);
                        }
                        return;
                    } else if (CANID == (RxStdCANID_2[index_max] & 0x3fff)) {
                        RxStdDLC_2[index_max] = DLC;
                        if (RTR) {
                            RxStdDLC_2[index_max] = 0x80;
                            return;
                        }
                        if (0x4000 == (0x4000 & RxStdCANID_2[index_max])) {
                            //from transmit which needs short frame response
                            RxStdCANID_2[index_max] = RxStdCANID_2[index_max] & 0xBFFF; //clear flag from transmit which needs short frame response
                        }
                        int8_t i;
                        for (i = 0; i < DLC; i++) {
                            RxStdData_2[index_max][i] = *(pCAN_Data + i);
                        }
                        return;
                    } else break;
                }
            }
        }

    }

    //CANID belongs to RxStdCANID_Long_2? (long receive std frame) 
    // We clear Bit15 Bit14 in comparing standard CAN ID because Bit15 used in Data integrity of long frame, Bit 14 used for Response Flag for transmit which needs response
    if (RxStdCANID_Long_2 != NULL) {
        index_min = 0;
        index_max = sizeof_RxStdCANID_Long_2 - 1;
        if ((CANID >= (RxStdCANID_Long_2[index_min] & 0x3FFF)) && (CANID <= (RxStdCANID_Long_2[index_max]& 0x3FFF))) {
            while (1) {
                index = (index_min + index_max) >> 1;
                if (CANID == (RxStdCANID_Long_2[index]& 0x3FFF)) {
                    if (RTR) {
                        RxStdDLC_Long_2[index][0] = 0x80;
                        return;
                    } else if (DLC > 0) {
                        uint8_t MaxFramNo, Index4CurrentFrameNo;
                        if (CAN_myObj2.FrameNoIs0_based) {
                            // 0-based Frame No
                            //MaxFramNo = CAN_myObj2.RxStdFrameQTY4long - 1;
                            MaxFramNo = CAN2_RX_MAX_STD_FRAME_QTY4LONG - 1;
                            Index4CurrentFrameNo = *pCAN_Data;
                        } else {
                            // 1-based Frame No
                            //MaxFramNo = CAN_myObj2.RxStdFrameQTY4long;
                            MaxFramNo = CAN2_RX_MAX_STD_FRAME_QTY4LONG;
                            Index4CurrentFrameNo = *pCAN_Data - 1;
                            if (*pCAN_Data == 0) {
                                //error frame no
                                return;
                            }
                        }
                        if ((*pCAN_Data) <= MaxFramNo) {
                            RxStdDLC_Long_2[index][Index4CurrentFrameNo] = DLC;
                            if (0 == Index4CurrentFrameNo) {
                                //clear Data integrity flag in bit15 of CAN ID (bit15=1 : non-integrity)
                                RxStdCANID_Long_2[index] = RxStdCANID_Long_2[index] | 0x8000;
                            }

                            int8_t i;
                            if (0x4000 == (0x4000 & RxStdCANID_Long_2[index])) {
                                //This CAN ID is "Transmit frame's respond long frame
                                //Check Channel A Seq0 to 3, its response CAN ID, if it is, verify Max FramwNo
                                for (i = 0; i<4; i++) {
                                    if (0x8000 == (TxResponseCANID_Index_A_2[i] & 0xc000)) {
                                        //Standard long frame response
                                        if (index == (TxResponseCANID_Index_A_2[i] & 0x3FFF)) {
                                            if (ResponseMaxFrameNo_A_2[i] == *pCAN_Data) {
                                                RxStdCANID_Long_2[index] = RxStdCANID_Long_2[index] & 0xBFFF; //clear flag from transmit which needs long frame response
                                            }
                                            goto exitSearch1;
                                        }
                                    }
                                }
                                //Check Channel B Seq0 to 3, its response CAN ID, if it is, verify Max FramwNo
#ifdef USE_CAN2_CHANNELB
                                for (i = 0; i<4; i++) {
                                    if (0x8000 == (TxResponseCANID_Index_B_2[i] & 0xc000)) {
                                        //Standard long frame response
                                        if (index == (TxResponseCANID_Index_B_2[i] & 0x3FFF)) {
                                            if (ResponseMaxFrameNo_B_2[i] == *pCAN_Data) {
                                                RxStdCANID_Long_2[index] = RxStdCANID_Long_2[index] & 0xBFFF; //clear flag from transmit which needs long frame response
                                            }
                                            goto exitSearch1;
                                        }
                                    }
                                }
#endif                                
                                //Check Channel C Seq0 to 3, its response CAN ID, if it is, verify Max FramwNo
#ifdef USE_CAN2_CHANNELC
                                for (i = 0; i<4; i++) {
                                    if (0x8000 == (TxResponseCANID_Index_C_2[i] & 0xc000)) {
                                        //Standard long frame response
                                        if (index == (TxResponseCANID_Index_C_2[i] & 0x3FFF)) {
                                            if (ResponseMaxFrameNo_C_2[i] == *pCAN_Data) {
                                                RxStdCANID_Long_2[index] = RxStdCANID_Long_2[index] & 0xBFFF; //clear flag from transmit which needs long frame response
                                            }
                                            goto exitSearch1;
                                        }
                                    }
                                }
#endif                                  
                            }
                            exitSearch1:
                            for (i = 0; i < DLC; i++) {
                                RxStdData_Long_2[index][Index4CurrentFrameNo][i] = *(pCAN_Data + i);
                            }
                            if (LastStdMAXFrmNo_2[index] == *pCAN_Data) {
                                //Set Data integrity flag in bit15 of CAN ID (bit15=0 : integrity)
                                RxStdCANID_Long_2[index] = RxStdCANID_Long_2[index] & 0x7FFF;
                            }
                            return;
                        }


                    }
                    return;
                }
                if (CANID < (RxStdCANID_Long_2[index]& 0x3FFF)) {
                    index_max = index;
                } else {
                    index_min = index;
                }
                if (index_min == index_max) break;
                if (index_max == (index_min + 1)) {
                    //in case stuck (Never occur for index_min == index_max)
                    if (CANID == (RxStdCANID_Long_2[index_min]& 0x3FFF)) {
                        if (RTR) {
                            RxStdDLC_Long_2[index_min][0] = 0x80;
                            return;
                        } else if (DLC > 0) {
                            uint8_t MaxFramNo, Index4CurrentFrameNo;
                            if (CAN_myObj2.FrameNoIs0_based) {
                                // 0-based Frame No
                                //MaxFramNo = CAN_myObj2.RxStdFrameQTY4long - 1;
                                MaxFramNo = CAN2_RX_MAX_STD_FRAME_QTY4LONG - 1;
                                Index4CurrentFrameNo = *pCAN_Data;
                            } else {
                                // 1-based Frame No
                                //MaxFramNo = CAN_myObj2.RxStdFrameQTY4long;
                                MaxFramNo = CAN2_RX_MAX_STD_FRAME_QTY4LONG ;
                                Index4CurrentFrameNo = *pCAN_Data - 1;
                                if (*pCAN_Data == 0) {
                                    //error frame no
                                    return;
                                }
                            }
                            if ((*pCAN_Data) <= MaxFramNo) {
                                RxStdDLC_Long_2[index_min][Index4CurrentFrameNo] = DLC;
                                if (0 == Index4CurrentFrameNo) {
                                    //clear Data integrity flag in bit15 of CAN ID (bit15=1 : non-integrity)
                                    RxStdCANID_Long_2[index_min] = RxStdCANID_Long_2[index_min] | 0x8000;
                                }
                                int8_t i;
                                if (0x4000 == (0x4000 & RxStdCANID_Long_2[index_min])) {
                                    //This CAN ID is "Transmit frame's respond long frame
                                    //Check Channel A Seq0 to 3, its response CAN ID, if it is, verify Max FramwNo
                                    for (i = 0; i<4; i++) {
                                        if (0x8000 == (TxResponseCANID_Index_A_2[i] & 0xc000)) {
                                            //Standard long frame response
                                            if (index_min == (TxResponseCANID_Index_A_2[i] & 0x3FFF)) {
                                                if (ResponseMaxFrameNo_A_2[i] == *pCAN_Data) {
                                                    RxStdCANID_Long_2[index_min] = RxStdCANID_Long_2[index_min] & 0xBFFF; //clear flag from transmit which needs long frame response
                                                }
                                                goto exitSearch2;
                                            }
                                        }
                                    }
                                    //Check Channel B Seq0 to 3, its response CAN ID, if it is, verify Max FramwNo
#ifdef USE_CAN2_CHANNELB
                                    for (i = 0; i<4; i++) {
                                        if (0x8000 == (TxResponseCANID_Index_B_2[i] & 0xc000)) {
                                            //Standard long frame response
                                            if (index_min == (TxResponseCANID_Index_B_2[i] & 0x3FFF)) {
                                                if (ResponseMaxFrameNo_B_2[i] == *pCAN_Data) {
                                                    RxStdCANID_Long_2[index_min] = RxStdCANID_Long_2[index_min] & 0xBFFF; //clear flag from transmit which needs long frame response
                                                }
                                                goto exitSearch2;
                                            }
                                        }
                                    }
#endif                                
                                    //Check Channel C Seq0 to 3, its response CAN ID, if it is, verify Max FramwNo
#ifdef USE_CAN2_CHANNELC
                                    for (i = 0; i<4; i++) {
                                        if (0x8000 == (TxResponseCANID_Index_C_2[i] & 0xc000)) {
                                            //Standard long frame response
                                            if (index_min == (TxResponseCANID_Index_C_2[i] & 0x3FFF)) {
                                                if (ResponseMaxFrameNo_C_2[i] == *pCAN_Data) {
                                                    RxStdCANID_Long_2[index_min] = RxStdCANID_Long_2[index_min] & 0xBFFF; //clear flag from transmit which needs long frame response
                                                }
                                                goto exitSearch2;
                                            }
                                        }
                                    }
#endif  
                                }
                                
                                exitSearch2:
                                for (i = 0; i < DLC; i++) {
                                    RxStdData_Long_2[index_min][Index4CurrentFrameNo][i] = *(pCAN_Data + i);
                                }
                                if (LastStdMAXFrmNo_2[index_min] == *pCAN_Data) {
                                    //Set Data integrity flag in bit15 of CAN ID (bit15=0 : integrity)
                                    RxStdCANID_Long_2[index_min] = RxStdCANID_Long_2[index_min] & 0x7FFF;
                                }
                                return;
                            }


                        }
                        return;
                    } else if (CANID == (RxStdCANID_Long_2[index_max]& 0x3FFF)) {
                        if (RTR) {
                            RxStdDLC_Long_2[index_max][0] = 0x80;
                            return;
                        } else if (DLC > 0) {
                            uint8_t MaxFramNo, Index4CurrentFrameNo;
                            if (CAN_myObj2.FrameNoIs0_based) {
                                // 0-based Frame No
                                //MaxFramNo = CAN_myObj2.RxStdFrameQTY4long - 1;
                                MaxFramNo = CAN2_RX_MAX_STD_FRAME_QTY4LONG - 1;
                                Index4CurrentFrameNo = *pCAN_Data;
                            } else {
                                // 1-based Frame No
                                //MaxFramNo = CAN_myObj2.RxStdFrameQTY4long;
                                MaxFramNo = CAN2_RX_MAX_STD_FRAME_QTY4LONG ;
                                Index4CurrentFrameNo = *pCAN_Data - 1;
                                if (*pCAN_Data == 0) {
                                    //error frame no
                                    return;
                                }
                            }
                            if ((*pCAN_Data) <= MaxFramNo) {
                                RxStdDLC_Long_2[index_max][Index4CurrentFrameNo] = DLC;
                                if (0 == Index4CurrentFrameNo) {
                                    //clear Data integrity flag in bit15 of CAN ID (bit15=1 : non-integrity)
                                    RxStdCANID_Long_2[index_max] = RxStdCANID_Long_2[index_max] | 0x8000;
                                }
                                int8_t i;
                                if (0x4000 == (0x4000 & RxStdCANID_Long_2[index_max])) {
                                    //This CAN ID is "Transmit frame's respond long frame
                                    //Check Channel A Seq0 to 3, its response CAN ID, if it is, verify Max FramwNo
                                    for (i = 0; i<4; i++) {
                                        if (0x8000 == (TxResponseCANID_Index_A_2[i] & 0xc000)) {
                                            //Standard long frame response
                                            if (index_max == (TxResponseCANID_Index_A_2[i] & 0x3FFF)) {
                                                if (ResponseMaxFrameNo_A_2[i] == *pCAN_Data) {
                                                    RxStdCANID_Long_2[index_max] = RxStdCANID_Long_2[index_max] & 0xBFFF; //clear flag from transmit which needs long frame response
                                                }
                                                goto exitSearch3;
                                            }
                                        }
                                    }
                                    //Check Channel B Seq0 to 3, its response CAN ID, if it is, verify Max FramwNo
#ifdef USE_CAN2_CHANNELB
#endif                                
                                    //Check Channel C Seq0 to 3, its response CAN ID, if it is, verify Max FramwNo
#ifdef USE_CAN2_CHANNELC
#endif  
                                }

                                exitSearch3:
                                for (i = 0; i < DLC; i++) {
                                    RxStdData_Long_2[index_max][Index4CurrentFrameNo][i] = *(pCAN_Data + i);
                                }
                                if (LastStdMAXFrmNo_2[index_max] == *pCAN_Data) {
                                    //Set Data integrity flag in bit15 of CAN ID (bit15=0 : integrity)
                                    RxStdCANID_Long_2[index_max] = RxStdCANID_Long_2[index_max] & 0x7FFF;
                                }
                                return;
                            }


                        }
                        return;
                    } else break;
                }
            }
        }
    }
}

/**
 * For CAN2, Put one Rx Ext frame  into our high level buffer such as : RxExtData_2[],RxExtData_Long_2[][][]
 *   And for long frame, set/clear Data integrity flag in Bit31 of RxExtCANID_Long_2
 * for respond frame, when received response, clear Bit30 of RxExtCANID_2 or clear Bit30 of RxExtCANID_Long_2 when Max frame No is what I expect.
 *   To set Bit30 for RxExtCANID_2 or RxExtCANID_Long_2 is in function for transmitting when expecting response
 * @param CANID
 * @param RTR
 * @param DLC
 * @param pCAN_Data
 */
void putCAN2RxExtCAN2Buffer(uint32_t CANID, bool RTR, uint8_t DLC, uint8_t * pCAN_Data) {
    uint16_t index_min, index_max, index;
    if (CAN_myObj2.EnableSyn && (0==CAN_myObj2.SynUseStdCANID)) {
        //modify new, because we don't need to set it to 1 if Bit31  of CAN_myObj2.SyncCANID=1
        //if (CANID==(CAN_myObj2.SyncCANID & 0x7FFFffff))  CAN_myObj2.SyncCANID = CAN_myObj2.SyncCANID | 0x80000000;
        if (CANID==CAN_myObj2.SyncCANID )  CAN_myObj2.SyncCANID = CAN_myObj2.SyncCANID | 0x80000000;
    }  
    //CANID belongs to RxExtCANID_2? (short receive Ext frame)
    // We clear Bit31 Bit30 in comparing standard CAN ID because Bit31 used in Data integrity of long frame, Bit 30 used for Response Flag for transmit which needs response
    if (RxExtCANID_2 != NULL) {
        index_min = 0;
        index_max = sizeof_RxExtCANID_2 - 1;
        if ((CANID >= (RxExtCANID_2[index_min] & 0x3FFFffff)) && (CANID <= (RxExtCANID_2[index_max] & 0x3FFFffff))) {
            while (1) {
                index = (index_min + index_max) >> 1;
                if (CANID == (RxExtCANID_2[index] & 0x3FFFffff)) {
                    RxExtDLC_2[index] = DLC;
                    if (RTR) {
                        RxExtDLC_2[index] = 0x80;
                        return;
                    }
                    if (0x40000000 == (0x40000000 & RxExtCANID_2[index])) {
                        //from transmit which needs short frame response
                        RxExtCANID_2[index] = RxExtCANID_2[index] & 0xBFFFffff; //clear flag from transmit which needs short frame response
                    }
                    int8_t i;
                    for (i = 0; i < DLC; i++) {
                        RxExtData_2[index][i] = *(pCAN_Data + i);
                    }
                    return;
                }
                if (CANID < (RxExtCANID_2[index] & 0x3FFFffff)) {
                    index_max = index;
                } else {
                    index_min = index;
                }
                if (index_min == index_max) break;
                if (index_max == (index_min + 1)) {
                    //in case stuck (Never occur for index_min == index_max)
                    if (CANID == (RxExtCANID_2[index_min] & 0x3FFFffff)) {
                        RxExtDLC_2[index_min] = DLC;
                        if (RTR) {
                            RxExtDLC_2[index_min] = 0x80;
                            return;
                        }
                        if (0x40000000 == (0x40000000 & RxExtCANID_2[index_min])) {
                            //from transmit which needs short frame response
                            RxExtCANID_2[index_min] = RxExtCANID_2[index_min] & 0xBFFFffff; //clear flag from transmit which needs short frame response
                        }
                        int8_t i;
                        for (i = 0; i < DLC; i++) {
                            RxExtData_2[index_min][i] = *(pCAN_Data + i);
                        }
                        return;
                    } else if (CANID == (RxExtCANID_2[index_max] & 0x3FFFffff)) {
                        RxExtDLC_2[index_max] = DLC;
                        if (RTR) {
                            RxExtDLC_2[index_max] = 0x80;
                            return;
                        }
                        if (0x40000000 == (0x40000000 & RxExtCANID_2[index_max])) {
                            //from transmit which needs short frame response
                            RxExtCANID_2[index_max] = RxExtCANID_2[index_max] & 0xBFFFffff; //clear flag from transmit which needs short frame response
                        }
                        int8_t i;
                        for (i = 0; i < DLC; i++) {
                            RxExtData_2[index_max][i] = *(pCAN_Data + i);
                        }
                        return;
                    } else break;
                }
            }
        }

    }

    //CANID belongs to RxExtCANID_Long_2? (long receive Ext frame) 
    // We clear Bit31 Bit30 in comparing standard CAN ID because Bit31 used in Data integrity of long frame, Bit 30 used for Response Flag for transmit which needs response
    if (RxExtCANID_Long_2 != NULL) {
        index_min = 0;
        index_max = sizeof_RxExtCANID_Long_2 - 1;
        if ((CANID >= (RxExtCANID_Long_2[index_min] & 0x7FFFFFFF)) && (CANID <= (RxExtCANID_Long_2[index_max]& 0x7FFFFFFF))) {
            while (1) {
                index = (index_min + index_max) >> 1;
                if (CANID == (RxExtCANID_Long_2[index]& 0x7FFFFFFF)) {
                    if (RTR) {
                        RxExtDLC_Long_2[index][0] = 0x80;
                        return;
                    } else if (DLC > 0) {
                        uint8_t MaxFramNo, Index4CurrentFrameNo;
                        if (CAN_myObj2.FrameNoIs0_based) {
                            // 0-based Frame No
                            //MaxFramNo = CAN_myObj2.RxExtFrameQTY4long - 1;
                            MaxFramNo = CAN2_RX_MAX_EXT_FRAME_QTY4LONG - 1;
                            Index4CurrentFrameNo = *pCAN_Data;
                        } else {
                            // 1-based Frame No
                            //MaxFramNo = CAN_myObj2.RxExtFrameQTY4long;
                            MaxFramNo = CAN2_RX_MAX_EXT_FRAME_QTY4LONG ;
                            Index4CurrentFrameNo = *pCAN_Data - 1;
                            if (*pCAN_Data == 0) {
                                //error frame no
                                return;
                            }
                        }
                        if ((*pCAN_Data) <= MaxFramNo) {
                            RxExtDLC_Long_2[index][Index4CurrentFrameNo] = DLC;
                            if (0 == Index4CurrentFrameNo) {
                                //clear Data integrity flag in bit31 of CAN ID (bit31=1 : non-integrity)
                                RxExtCANID_Long_2[index] = RxExtCANID_Long_2[index] | 0x80000000;
                            }
                            int8_t i;
                            if (0x40000000 == (0x40000000 & RxExtCANID_Long_2[index])) {
                                //This CAN ID is "Transmit frame's respond long frame
                                //Check Channel A Seq0 to 3, its response CAN ID, if it is, verify Max FramwNo
                                for (i = 0; i<4; i++) {
                                    if (0xc000 == (TxResponseCANID_Index_A_2[i] & 0xc000)) {
                                        //ext long frame response
                                        if (index == (TxResponseCANID_Index_A_2[i] & 0x3FFF)) {
                                            if (ResponseMaxFrameNo_A_2[i] == *pCAN_Data) {
                                                RxExtCANID_Long_2[index] = RxExtCANID_Long_2[index] & 0xBFFFffff; //clear flag from transmit which needs long frame response
                                            }
                                            goto exitSearch1;
                                        }
                                    }
                                }
                                //Check Channel B Seq0 to 3, its response CAN ID, if it is, verify Max FramwNo
#ifdef USE_CAN2_CHANNELB
                                for (i = 0; i<4; i++) {
                                    if (0xc000 == (TxResponseCANID_Index_B_2[i] & 0xc000)) {
                                        //ext long frame response
                                        if (index == (TxResponseCANID_Index_B_2[i] & 0x3FFF)) {
                                            if (ResponseMaxFrameNo_B_2[i] == *pCAN_Data) {
                                                RxExtCANID_Long_2[index] = RxExtCANID_Long_2[index] & 0xBFFFffff; //clear flag from transmit which needs long frame response
                                            }
                                            goto exitSearch1;
                                        }
                                    }
                                }
#endif                                
                                //Check Channel C Seq0 to 3, its response CAN ID, if it is, verify Max FramwNo
#ifdef USE_CAN2_CHANNELC
                                for (i = 0; i<4; i++) {
                                    if (0xc000 == (TxResponseCANID_Index_C_2[i] & 0xc000)) {
                                        //ext long frame response
                                        if (index == (TxResponseCANID_Index_C_2[i] & 0x3FFF)) {
                                            if (ResponseMaxFrameNo_C_2[i] == *pCAN_Data) {
                                                RxExtCANID_Long_2[index] = RxExtCANID_Long_2[index] & 0xBFFFffff; //clear flag from transmit which needs long frame response
                                            }
                                            goto exitSearch1;
                                        }
                                    }
                                }
#endif  
                            }

                            exitSearch1:
                            for (i = 0; i < DLC; i++) {
                                RxExtData_Long_2[index][Index4CurrentFrameNo][i] = *(pCAN_Data + i);
                            }
                            //LastExtMAXFrmNo_2[index] = *pCAN_Data;
                            if (LastExtMAXFrmNo_2[index] == *pCAN_Data) {
                                //Set Data integrity flag in bit15 of CAN ID (bit31=0 : integrity)
                                RxExtCANID_Long_2[index] = RxExtCANID_Long_2[index] & 0x7FFFFFFF;
                            }
                            return;
                        }


                    }
                    return;
                }
                if (CANID < (RxExtCANID_Long_2[index]& 0x7FFFFFFF)) {
                    index_max = index;
                } else {
                    index_min = index;
                }
                if (index_min == index_max) break;
                if (index_max == (index_min + 1)) {
                    //in case stuck (Never occur for index_min == index_max)
                    if (CANID == (RxExtCANID_Long_2[index_min]& 0x7FFFFFFF)) {
                        if (RTR) {
                            RxExtDLC_Long_2[index_min][0] = 0x80;
                            return;
                        } else if (DLC > 0) {
                            uint8_t MaxFramNo, Index4CurrentFrameNo;
                            if (CAN_myObj2.FrameNoIs0_based) {
                                // 0-based Frame No
                                //MaxFramNo = CAN_myObj2.RxExtFrameQTY4long - 1;
                                MaxFramNo = CAN2_RX_MAX_EXT_FRAME_QTY4LONG - 1;
                                Index4CurrentFrameNo = *pCAN_Data;
                            } else {
                                // 1-based Frame No
                                //MaxFramNo = CAN_myObj2.RxExtFrameQTY4long;
                                MaxFramNo = CAN2_RX_MAX_EXT_FRAME_QTY4LONG ;
                                Index4CurrentFrameNo = *pCAN_Data - 1;
                                if (*pCAN_Data == 0) {
                                    //error frame no
                                    return;
                                }
                            }
                            if ((*pCAN_Data) <= MaxFramNo) {
                                RxExtDLC_Long_2[index_min][Index4CurrentFrameNo] = DLC;
                                if (0 == Index4CurrentFrameNo) {
                                    //clear Data integrity flag in bit31 of CAN ID (bit31=1 : non-integrity)
                                    RxExtCANID_Long_2[index_min] = RxExtCANID_Long_2[index_min] | 0x80000000;
                                }
                                int8_t i;
                                if (0x40000000 == (0x40000000 & RxExtCANID_Long_2[index_min])) {
                                    //This CAN ID is "Transmit frame's respond long frame
                                    //Check Channel A Seq0 to 3, its response CAN ID, if it is, verify Max FramwNo
                                    for (i = 0; i<4; i++) {
                                        if (0xc000 == (TxResponseCANID_Index_A_2[i] & 0xc000)) {
                                            //ext long frame response
                                            if (index_min == (TxResponseCANID_Index_A_2[i] & 0x3FFF)) {
                                                if (ResponseMaxFrameNo_A_2[i] == *pCAN_Data) {
                                                    RxExtCANID_Long_2[index_min] = RxExtCANID_Long_2[index_min] & 0xBFFFffff; //clear flag from transmit which needs long frame response
                                                }
                                                goto exitSearch2;
                                            }
                                        }
                                    }
                                    //Check Channel B Seq0 to 3, its response CAN ID, if it is, verify Max FramwNo
#ifdef USE_CAN2_CHANNELB
                                    for (i = 0; i<4; i++) {
                                        if (0xc000 == (TxResponseCANID_Index_B_2[i] & 0xc000)) {
                                            //ext long frame response
                                            if (index_min == (TxResponseCANID_Index_B_2[i] & 0x3FFF)) {
                                                if (ResponseMaxFrameNo_B_2[i] == *pCAN_Data) {
                                                    RxExtCANID_Long_2[index_min] = RxExtCANID_Long_2[index_min] & 0xBFFFffff; //clear flag from transmit which needs long frame response
                                                }
                                                goto exitSearch2;
                                            }
                                        }
                                    }
#endif                                
                                    //Check Channel C Seq0 to 3, its response CAN ID, if it is, verify Max FramwNo
#ifdef USE_CAN2_CHANNELC
                                    for (i = 0; i<4; i++) {
                                        if (0xc000 == (TxResponseCANID_Index_C_2[i] & 0xc000)) {
                                            //ext long frame response
                                            if (index_min == (TxResponseCANID_Index_C_2[i] & 0x3FFF)) {
                                                if (ResponseMaxFrameNo_C_2[i] == *pCAN_Data) {
                                                    RxExtCANID_Long_2[index_min] = RxExtCANID_Long_2[index_min] & 0xBFFFffff; //clear flag from transmit which needs long frame response
                                                }
                                                goto exitSearch2;
                                            }
                                        }
                                    }
#endif  
                                }

                                exitSearch2:
                                for (i = 0; i < DLC; i++) {
                                    RxExtData_Long_2[index_min][Index4CurrentFrameNo][i] = *(pCAN_Data + i);
                                }
                                //LastExtMAXFrmNo_2[index_min] = *pCAN_Data;
                                if (LastExtMAXFrmNo_2[index_min] == *pCAN_Data) {
                                    //Set Data integrity flag in bit15 of CAN ID (bit31=0 : integrity)
                                    RxExtCANID_Long_2[index_min] = RxExtCANID_Long_2[index_min] & 0x7FFFFFFF;
                                }
                                return;
                            }


                        }
                        return;
                    } else if (CANID == (RxExtCANID_Long_2[index_max]& 0x7FFFFFFF)) {
                        if (RTR) {
                            RxExtDLC_Long_2[index_max][0] = 0x80;
                            return;
                        } else if (DLC > 0) {
                            uint8_t MaxFramNo, Index4CurrentFrameNo;
                            if (CAN_myObj2.FrameNoIs0_based) {
                                // 0-based Frame No
                                //MaxFramNo = CAN_myObj2.RxExtFrameQTY4long - 1;
                                MaxFramNo = CAN2_RX_MAX_EXT_FRAME_QTY4LONG - 1;
                                Index4CurrentFrameNo = *pCAN_Data;
                            } else {
                                // 1-based Frame No
                                //MaxFramNo = CAN_myObj2.RxExtFrameQTY4long;
                                MaxFramNo = CAN2_RX_MAX_EXT_FRAME_QTY4LONG ;
                                Index4CurrentFrameNo = *pCAN_Data - 1;
                                if (*pCAN_Data == 0) {
                                    //error frame no
                                    return;
                                }
                            }
                            if ((*pCAN_Data) <= MaxFramNo) {
                                RxExtDLC_Long_2[index_max][Index4CurrentFrameNo] = DLC;
                                if (0 == Index4CurrentFrameNo) {
                                    //clear Data integrity flag in bit31 of CAN ID (bit31=1 : non-integrity)
                                    RxExtCANID_Long_2[index_max] = RxExtCANID_Long_2[index_max] | 0x80000000;
                                }
                                int8_t i;
                                if (0x40000000 == (0x40000000 & RxExtCANID_Long_2[index_max])) {
                                    //This CAN ID is "Transmit frame's respond long frame
                                    //Check Channel A Seq0 to 3, its response CAN ID, if it is, verify Max FramwNo
                                    for (i = 0; i<4; i++) {
                                        if (0xc000 == (TxResponseCANID_Index_A_2[i] & 0xc000)) {
                                            //ext long frame response
                                            if (index_max == (TxResponseCANID_Index_A_2[i] & 0x3FFF)) {
                                                if (ResponseMaxFrameNo_A_2[i] == *pCAN_Data) {
                                                    RxExtCANID_Long_2[index_max] = RxExtCANID_Long_2[index_max] & 0xBFFFffff; //clear flag from transmit which needs long frame response
                                                }
                                                goto exitSearch3;
                                            }
                                        }
                                    }
                                    //Check Channel B Seq0 to 3, its response CAN ID, if it is, verify Max FramwNo
#ifdef USE_CAN2_CHANNELB
                                    for (i = 0; i<4; i++) {
                                        if (0xc000 == (TxResponseCANID_Index_B_2[i] & 0xc000)) {
                                            //ext long frame response
                                            if (index_max == (TxResponseCANID_Index_B_2[i] & 0x3FFF)) {
                                                if (ResponseMaxFrameNo_B_2[i] == *pCAN_Data) {
                                                    RxExtCANID_Long_2[index_max] = RxExtCANID_Long_2[index_max] & 0xBFFFffff; //clear flag from transmit which needs long frame response
                                                }
                                                goto exitSearch3;
                                            }
                                        }
                                    }
#endif                                
                                    //Check Channel C Seq0 to 3, its response CAN ID, if it is, verify Max FramwNo
#ifdef USE_CAN2_CHANNELC
                                    for (i = 0; i<4; i++) {
                                        if (0xc000 == (TxResponseCANID_Index_C_2[i] & 0xc000)) {
                                            //ext long frame response
                                            if (index_max == (TxResponseCANID_Index_C_2[i] & 0x3FFF)) {
                                                if (ResponseMaxFrameNo_C_2[i] == *pCAN_Data) {
                                                    RxExtCANID_Long_2[index_max] = RxExtCANID_Long_2[index_max] & 0xBFFFffff; //clear flag from transmit which needs long frame response
                                                }
                                                goto exitSearch3;
                                            }
                                        }
                                    }
#endif  
                                }

                                exitSearch3:
                                for (i = 0; i < DLC; i++) {
                                    RxExtData_Long_2[index_max][Index4CurrentFrameNo][i] = *(pCAN_Data + i);
                                }
                                //LastExtMAXFrmNo_2[index_max] = *pCAN_Data;
                                if (LastExtMAXFrmNo_2[index_max] == *pCAN_Data) {
                                    //Set Data integrity flag in bit15 of CAN ID (bit31=0 : integrity)
                                    RxExtCANID_Long_2[index_max] = RxExtCANID_Long_2[index_max] & 0x7FFFFFFF;
                                }
                                return;
                            }


                        }
                        return;
                    } else break;
                }
            }
        }
    }
}

#endif