#include <stdint.h>
#include <stdbool.h>  
#include "CANBUS.h"
#define tick uwTick
volatile uint32_t CanUsedMailBox[2][3]; //first index 0 for can1, 1 for can2  second index is channel, 0--ChannelA , 1--ChannelB , 2--ChannelC
                                         //ARRAY Value is CAN TX mailbox
static int8_t CAN_All_Channel_Transmit4HL(CANBUS_t * CAN_Obj, uint8_t ChannelNo, uint8_t Seq, uint32_t *state, uint8_t * CurrntFrmNo, uint16_t * Tick4Timeout, uint32_t *TransmitCANEnterBuffer);                                         
#ifdef USE_CAN1
volatile bool Can1RxSuccess=false;
volatile uint8_t Can1TxSuccess=0;   //we use Success tx :  Bit0 For Tx ChannelA, Bit1 Tx for ChannelB ,Bit2 for Tx ChannelC

//Transmit Channel A for CAN1
extern uint16_t TxCAN_A_RTR_DLC_UseStd_1[]; //For CAN1 channel A transmit:  index is Seq No
//bit15: RTR, bit14: Std CANID , Bit13: Need response, Bit12: Response is long frame response,Bit10: reserved. Bit10-0: DLC (Max 255*7=1785 bytes, so 2048 has some no use, >8 as long frame) 
//if index>3 (Seq4, seq5, ...), we will ignore Bit13, take it as 0 (no need response)
//if transmit is long frame (Bit10-0: DLC>8), we will ignore Bit13, take it as 0 (no need response)
// if bit15: RTR, we will ignore Bit13, take it as 0 (no need response)
extern uint16_t TxCAN_A_Timeout_1[]; //Timeout in ms.  This is timeout for Transmit, which needs response. Index is Seq No , Max index=3 
extern uint32_t TransmitFrameCANID_A_1[]; //CAN ID.   Index is Seq No 

//We changed it to use index of CAN ID
//extern uint32_t TxResponseCANID_A_1[];   //Respnse CAN ID, Index is Seq No. Max Index=3.  Bit31:1----Ext frame. 0--std frame.  
extern uint16_t TxResponseCANID_Index_A_1[]; //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 ResponseDataLen_A_1[]; //Response Data length in byte, Index is Seq No. Max Index=3.  
//When Length>8, respnse is long frame.  But it is possible, it is long frame  when Length <8, for example. Long frame response, but this time, only feedback one frame
//extern uint8_t ResponseData_A_1[][]; //Response Data length in byte, first Index is Seq No. Max Index=3. 2nd index is byte no.
extern uint8_t * ResponseData_A_1[]; //Response Data , first Index is Seq No. Max Index=3. 2nd index is byte no.
extern uint8_t ResponseMaxFrameNo_A_1[]; //Respnse Max frame Number for this time, Index is Seq No. Max Index=3.  

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 RxExtDLC_1[]; //CAN1 short Ext rx: Bit7 :RTR (1 is RTR, after reading, clear to 0), Bit6-0: DLC (0 to 8)


#if 0    //we use array point to replace.  And pointer is given in simulink
#ifdef CAN1_TX_CHANNELA_SEQ0_EXIST_DATA
extern uint8_t TransmitDataArrayA0_1[]; //CAN1 Channel A transmit , Seq0 Data Array
#endif
#ifdef CAN1_TX_CHANNELA_SEQ1_EXIST_DATA
extern uint8_t TransmitDataArrayA1_1[]; //CAN1 Channel A transmit , Seq1 Data Array
#endif
#ifdef CAN1_TX_CHANNELA_SEQ2_EXIST_DATA
extern uint8_t TransmitDataArrayA2_1[];
#endif
#ifdef CAN1_TX_CHANNELA_SEQ3_EXIST_DATA
extern uint8_t TransmitDataArrayA3_1[];
#endif
#ifdef CAN1_TX_CHANNELA_SEQ4_EXIST_DATA
extern uint8_t TransmitDataArrayA4_1[];
#endif
#ifdef CAN1_TX_CHANNELA_SEQ5_EXIST_DATA
extern uint8_t TransmitDataArrayA5_1[];
#endif
#ifdef CAN1_TX_CHANNELA_SEQ6_EXIST_DATA
extern uint8_t TransmitDataArrayA6_1[];
#endif
#ifdef CAN1_TX_CHANNELA_SEQ7_EXIST_DATA
extern uint8_t TransmitDataArrayA7_1[];
#endif
#ifdef CAN1_TX_CHANNELA_SEQ8_EXIST_DATA
extern uint8_t TransmitDataArrayA8_1[];
#endif
#ifdef CAN1_TX_CHANNELA_SEQ9_EXIST_DATA
extern uint8_t TransmitDataArrayA9_1[];
#endif
#ifdef CAN1_TX_CHANNELA_SEQ10_EXIST_DATA
extern uint8_t TransmitDataArrayA10_1[];
#endif

#ifdef CAN1_TX_CHANNELA_SEQ11_EXIST_DATA
extern uint8_t TransmitDataArrayA11_1[]; //CAN1 Channel A transmit , Seq11 Data Array
#endif
#ifdef CAN1_TX_CHANNELA_SEQ12_EXIST_DATA
extern uint8_t TransmitDataArrayA12_1[];
#endif
#ifdef CAN1_TX_CHANNELA_SEQ13_EXIST_DATA
extern uint8_t TransmitDataArrayA13_1[];
#endif
#ifdef CAN1_TX_CHANNELA_SEQ14_EXIST_DATA
extern uint8_t TransmitDataArrayA14_1[];
#endif
#ifdef CAN1_TX_CHANNELA_SEQ15_EXIST_DATA
extern uint8_t TransmitDataArrayA15_1[];
#endif
#ifdef CAN1_TX_CHANNELA_SEQ16_EXIST_DATA
extern uint8_t TransmitDataArrayA16_1[];
#endif
#ifdef CAN1_TX_CHANNELA_SEQ17_EXIST_DATA
extern uint8_t TransmitDataArrayA17_1[];
#endif
#ifdef CAN1_TX_CHANNELA_SEQ18_EXIST_DATA
extern uint8_t TransmitDataArrayA18_1[];
#endif
#ifdef CAN1_TX_CHANNELA_SEQ19_EXIST_DATA
extern uint8_t TransmitDataArrayA19_1[];
#endif
#ifdef CAN1_TX_CHANNELA_SEQ20_EXIST_DATA
extern uint8_t TransmitDataArrayA20_1[];
#endif



#ifdef CAN1_TX_CHANNELA_SEQ21_EXIST_DATA
extern uint8_t TransmitDataArrayA21_1[]; //CAN1 Channel A transmit , Seq21 Data Array
#endif
#ifdef CAN1_TX_CHANNELA_SEQ22_EXIST_DATA
extern uint8_t TransmitDataArrayA22_1[];
#endif
#ifdef CAN1_TX_CHANNELA_SEQ23_EXIST_DATA
extern uint8_t TransmitDataArrayA23_1[];
#endif
#ifdef CAN1_TX_CHANNELA_SEQ24_EXIST_DATA
extern uint8_t TransmitDataArrayA24_1[];
#endif
#ifdef CAN1_TX_CHANNELA_SEQ25_EXIST_DATA
extern uint8_t TransmitDataArrayA25_1[];
#endif
#ifdef CAN1_TX_CHANNELA_SEQ26_EXIST_DATA
extern uint8_t TransmitDataArrayA26_1[];
#endif
#ifdef CAN1_TX_CHANNELA_SEQ27_EXIST_DATA
extern uint8_t TransmitDataArrayA27_1[];
#endif
#ifdef CAN1_TX_CHANNELA_SEQ28_EXIST_DATA
extern uint8_t TransmitDataArrayA28_1[];
#endif
#ifdef CAN1_TX_CHANNELA_SEQ29_EXIST_DATA
extern uint8_t TransmitDataArrayA29_1[];
#endif
#ifdef CAN1_TX_CHANNELA_SEQ30_EXIST_DATA
extern uint8_t TransmitDataArrayA30_1[];
#endif
#ifdef CAN1_TX_CHANNELA_SEQ31_EXIST_DATA
extern uint8_t TransmitDataArrayA31_1[];
#endif
#else
extern uint8_t *TransmitDataArrayA_Seq_1[]; //CAN1 Channel A transmit , Data Array, point to data array. index is Seq No.  This variable is from Simulink

#endif

//Transmit Channel B for CAN1
#ifdef USE_CAN1_CHANNELB
extern uint16_t TxCAN_B_RTR_DLC_UseStd_1[]; //For CAN1 channel B transmit:  index is Seq No
//bit15: RTR, bit14: Std CANID , Bit13: Need response, Bit12: long frame response,Bit10: reserved. Bit10-0: DLC (Max 255*7=1785 bytes, so 2048 has some no use, >8 as long frame) 
//if index>3 (Seq4, seq5, ...), we will ignore Bit13, take it as 0 (no need response)
//if transmit is long frame (Bit10-0: DLC>8), we will ignore Bit13, take it as 0 (no need response)
// if bit15: RTR, we will ignore Bit13, take it as 0 (no need response)
extern uint16_t TxCAN_B_Timeout_1[]; //Timeout in ms.  This is timeout for Transmit, which needs response. Index is Seq No , Max index=3 

extern uint32_t TransmitFrameCANID_B_1[]; //CAN ID.   Index is Seq No  
extern uint16_t TxResponseCANID_Index_B_1[]; //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 uint32_t TxResponseCANID_B_1[]; //Respnse CAN ID, Index is Seq No. Max Index=3.  Bit31:1----Ext frame. 0--std frame.  
extern uint8_t ResponseDataLen_B_1[]; //Respnse Data length in byte, Index is Seq No. Max Index=3.  
//When Length>8, respnse is long frame.  But it is possible, it is long frame  when Length <8, for example. Long frame response, but this time, only feedback one frame
//extern uint8_t ResponseData_B_1[][]; //Respnse Data length in byte, first Index is Seq No. Max Index=3. 2nd index is byte no.
extern uint8_t * ResponseData_B_1[]; //Respnse Data length in byte, first Index is Seq No. Max Index=3. 2nd index is byte no.
extern uint8_t ResponseMaxFrameNo_B_1[]; //Respnse Max frame Number for this time, Index is Seq No. Max Index=3.  

#if 0   //we use array point to replace.  And pointer is given in simulink
#ifdef CAN1_TX_CHANNELB_SEQ0_EXIST_DATA
extern uint8_t TransmitDataArrayB0_1[]; //CAN1 Channel B transmit , Seq0 Data Array
#endif
#ifdef CAN1_TX_CHANNELB_SEQ1_EXIST_DATA
extern uint8_t TransmitDataArrayB1_1[]; //CAN1 Channel B transmit , Seq1 Data Array
#endif
#ifdef CAN1_TX_CHANNELB_SEQ2_EXIST_DATA
extern uint8_t TransmitDataArrayB2_1[];
#endif
#ifdef CAN1_TX_CHANNELB_SEQ3_EXIST_DATA
extern uint8_t TransmitDataArrayB3_1[];
#endif
#ifdef CAN1_TX_CHANNELB_SEQ4_EXIST_DATA
extern uint8_t TransmitDataArrayB4_1[];
#endif
#ifdef CAN1_TX_CHANNELB_SEQ5_EXIST_DATA
extern uint8_t TransmitDataArrayB5_1[];
#endif
#ifdef CAN1_TX_CHANNELB_SEQ6_EXIST_DATA
extern uint8_t TransmitDataArrayB6_1[];
#endif
#ifdef CAN1_TX_CHANNELB_SEQ7_EXIST_DATA
extern uint8_t TransmitDataArrayB7_1[];
#endif
#ifdef CAN1_TX_CHANNELB_SEQ8_EXIST_DATA
extern uint8_t TransmitDataArrayB8_1[];
#endif
#ifdef CAN1_TX_CHANNELB_SEQ9_EXIST_DATA
extern uint8_t TransmitDataArrayB9_1[];
#endif
#ifdef CAN1_TX_CHANNELB_SEQ10_EXIST_DATA
extern uint8_t TransmitDataArrayB10_1[];
#endif

#ifdef CAN1_TX_CHANNELB_SEQ11_EXIST_DATA
extern uint8_t TransmitDataArrayB11_1[]; //CAN1 Channel B transmit , Seq11 Data Array
#endif
#ifdef CAN1_TX_CHANNELB_SEQ12_EXIST_DATA
extern uint8_t TransmitDataArrayB12_1[];
#endif
#ifdef CAN1_TX_CHANNELB_SEQ13_EXIST_DATA
extern uint8_t TransmitDataArrayB13_1[];
#endif
#ifdef CAN1_TX_CHANNELB_SEQ14_EXIST_DATA
extern uint8_t TransmitDataArrayB14_1[];
#endif
#ifdef CAN1_TX_CHANNELB_SEQ15_EXIST_DATA
extern uint8_t TransmitDataArrayB15_1[];
#endif
#ifdef CAN1_TX_CHANNELB_SEQ16_EXIST_DATA
extern uint8_t TransmitDataArrayB16_1[];
#endif
#ifdef CAN1_TX_CHANNELB_SEQ17_EXIST_DATA
extern uint8_t TransmitDataArrayB17_1[];
#endif
#ifdef CAN1_TX_CHANNELB_SEQ18_EXIST_DATA
extern uint8_t TransmitDataArrayB18_1[];
#endif
#ifdef CAN1_TX_CHANNELB_SEQ19_EXIST_DATA
extern uint8_t TransmitDataArrayB19_1[];
#endif
#ifdef CAN1_TX_CHANNELB_SEQ20_EXIST_DATA
extern uint8_t TransmitDataArrayB20_1[];
#endif



#ifdef CAN1_TX_CHANNELB_SEQ21_EXIST_DATA
extern uint8_t TransmitDataArrayB21_1[]; //CAN1 Channel B transmit , Seq21 Data Array
#endif
#ifdef CAN1_TX_CHANNELB_SEQ22_EXIST_DATA
extern uint8_t TransmitDataArrayB22_1[];
#endif
#ifdef CAN1_TX_CHANNELB_SEQ23_EXIST_DATA
extern uint8_t TransmitDataArrayB23_1[];
#endif
#ifdef CAN1_TX_CHANNELB_SEQ24_EXIST_DATA
extern uint8_t TransmitDataArrayB24_1[];
#endif
#ifdef CAN1_TX_CHANNELB_SEQ25_EXIST_DATA
extern uint8_t TransmitDataArrayB25_1[];
#endif
#ifdef CAN1_TX_CHANNELB_SEQ26_EXIST_DATA
extern uint8_t TransmitDataArrayB26_1[];
#endif
#ifdef CAN1_TX_CHANNELB_SEQ27_EXIST_DATA
extern uint8_t TransmitDataArrayB27_1[];
#endif
#ifdef CAN1_TX_CHANNELB_SEQ28_EXIST_DATA
extern uint8_t TransmitDataArrayB28_1[];
#endif
#ifdef CAN1_TX_CHANNELB_SEQ29_EXIST_DATA
extern uint8_t TransmitDataArrayB29_1[];
#endif
#ifdef CAN1_TX_CHANNELB_SEQ30_EXIST_DATA
extern uint8_t TransmitDataArrayB30_1[];
#endif
#ifdef CAN1_TX_CHANNELB_SEQ31_EXIST_DATA
extern uint8_t TransmitDataArrayB31_1[];
#endif
#else
extern uint8_t *TransmitDataArrayB_Seq_1[]; //CAN1 Channel B transmit , Data Array, point to data array. index is Seq No.  This variable is from Simulink
#endif


#endif  //end of "#ifdef USE_CAN1_CHANNELB"

//Transmit Channel C for CAN1
#ifdef USE_CAN1_CHANNELC
extern uint16_t TxCAN_C_RTR_DLC_UseStd_1[]; //For CAN1 channel C transmit:  index is Seq No
//bit15: RTR, bit14: Std CANID , Bit13: Need response, Bit12: long frame response,Bit10: reserved. Bit10-0: DLC (Max 255*7=1785 bytes, so 2048 has some no use, >8 as long frame) 
//if index>3 (Seq4, seq5, ...), we will ignore Bit13, take it as 0 (no need response)
//if transmit is long frame (Bit10-0: DLC>8), we will ignore Bit13, take it as 0 (no need response)
// if bit15: RTR, we will ignore Bit13, take it as 0 (no need response)
extern uint16_t TxCAN_C_Timeout_1[]; //Timeout in ms.  This is timeout for Transmit, which needs response. Index is Seq No.  Max index=3  
extern uint32_t TransmitFrameCANID_C_1[]; //CAN ID.   Index is Seq No  
//extern uint32_t TxResponseCANID_C_1[];   //Respnse CAN ID, Index is Seq No. Max Index=3.  Bit31:1----Ext frame. 0--std frame.  
extern uint16_t TxResponseCANID_Index_C_1[]; //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 ResponseDataLen_C_1[]; //Respnse Data length in byte, Index is Seq No. Max Index=3.  
//When Length>8, respnse is long frame.  But it is possible, it is long frame  when Length <8, for example. Long frame response, but this time, only feedback one frame
//extern uint8_t ResponseData_C_1[][]; //Respnse Data length in byte, first Index is Seq No. Max Index=3. 2nd index is byte no.
extern uint8_t * ResponseData_C_1[]; //Respnse Data length in byte, first Index is Seq No. Max Index=3. 2nd index is byte no.
extern uint8_t ResponseMaxFrameNo_C_1[]; //Respnse Max frame Number for this time, Index is Seq No. Max Index=3.  

#if 0   //we use array point to replace.  And pointer is given in simulink
#ifdef CAN1_TX_CHANNELC_SEQ0_EXIST_DATA
extern uint8_t TransmitDataArrayC0_1[]; //CAN1 Channel C transmit , Seq0 Data Array
#endif
#ifdef CAN1_TX_CHANNELC_SEQ1_EXIST_DATA
extern uint8_t TransmitDataArrayC1_1[]; //CAN1 Channel C transmit , Seq1 Data Array
#endif
#ifdef CAN1_TX_CHANNELC_SEQ2_EXIST_DATA
extern uint8_t TransmitDataArrayC2_1[];
#endif
#ifdef CAN1_TX_CHANNELC_SEQ3_EXIST_DATA
extern uint8_t TransmitDataArrayC3_1[];
#endif
#ifdef CAN1_TX_CHANNELC_SEQ4_EXIST_DATA
extern uint8_t TransmitDataArrayC4_1[];
#endif
#ifdef CAN1_TX_CHANNELC_SEQ5_EXIST_DATA
extern uint8_t TransmitDataArrayC5_1[];
#endif
#ifdef CAN1_TX_CHANNELC_SEQ6_EXIST_DATA
extern uint8_t TransmitDataArrayC6_1[];
#endif
#ifdef CAN1_TX_CHANNELC_SEQ7_EXIST_DATA
extern uint8_t TransmitDataArrayC7_1[];
#endif
#ifdef CAN1_TX_CHANNELC_SEQ8_EXIST_DATA
extern uint8_t TransmitDataArrayC8_1[];
#endif
#ifdef CAN1_TX_CHANNELC_SEQ9_EXIST_DATA
extern uint8_t TransmitDataArrayC9_1[];
#endif
#ifdef CAN1_TX_CHANNELC_SEQ10_EXIST_DATA
extern uint8_t TransmitDataArrayC10_1[];
#endif

#ifdef CAN1_TX_CHANNELC_SEQ11_EXIST_DATA
extern uint8_t TransmitDataArrayC11_1[]; //CAN1 Channel C transmit , Seq11 Data Array
#endif
#ifdef CAN1_TX_CHANNELC_SEQ12_EXIST_DATA
extern uint8_t TransmitDataArrayC12_1[];
#endif
#ifdef CAN1_TX_CHANNELC_SEQ13_EXIST_DATA
extern uint8_t TransmitDataArrayC13_1[];
#endif
#ifdef CAN1_TX_CHANNELC_SEQ14_EXIST_DATA
extern uint8_t TransmitDataArrayC14_1[];
#endif
#ifdef CAN1_TX_CHANNELC_SEQ15_EXIST_DATA
extern uint8_t TransmitDataArrayC15_1[];
#endif
#ifdef CAN1_TX_CHANNELC_SEQ16_EXIST_DATA
extern uint8_t TransmitDataArrayC16_1[];
#endif
#ifdef CAN1_TX_CHANNELC_SEQ17_EXIST_DATA
extern uint8_t TransmitDataArrayC17_1[];
#endif
#ifdef CAN1_TX_CHANNELC_SEQ18_EXIST_DATA
extern uint8_t TransmitDataArrayC18_1[];
#endif
#ifdef CAN1_TX_CHANNELC_SEQ19_EXIST_DATA
extern uint8_t TransmitDataArrayC19_1[];
#endif
#ifdef CAN1_TX_CHANNELC_SEQ20_EXIST_DATA
extern uint8_t TransmitDataArrayC20_1[];
#endif



#ifdef CAN1_TX_CHANNELC_SEQ21_EXIST_DATA
extern uint8_t TransmitDataArrayC21_1[]; //CAN1 Channel C transmit , Seq21 Data Array
#endif
#ifdef CAN1_TX_CHANNELC_SEQ22_EXIST_DATA
extern uint8_t TransmitDataArrayC22_1[];
#endif
#ifdef CAN1_TX_CHANNELC_SEQ23_EXIST_DATA
extern uint8_t TransmitDataArrayC23_1[];
#endif
#ifdef CAN1_TX_CHANNELC_SEQ24_EXIST_DATA
extern uint8_t TransmitDataArrayC24_1[];
#endif
#ifdef CAN1_TX_CHANNELC_SEQ25_EXIST_DATA
extern uint8_t TransmitDataArrayC25_1[];
#endif
#ifdef CAN1_TX_CHANNELC_SEQ26_EXIST_DATA
extern uint8_t TransmitDataArrayC26_1[];
#endif
#ifdef CAN1_TX_CHANNELC_SEQ27_EXIST_DATA
extern uint8_t TransmitDataArrayC27_1[];
#endif
#ifdef CAN1_TX_CHANNELC_SEQ28_EXIST_DATA
extern uint8_t TransmitDataArrayC28_1[];
#endif
#ifdef CAN1_TX_CHANNELC_SEQ29_EXIST_DATA
extern uint8_t TransmitDataArrayC29_1[];
#endif
#ifdef CAN1_TX_CHANNELC_SEQ30_EXIST_DATA
extern uint8_t TransmitDataArrayC30_1[];
#endif
#ifdef CAN1_TX_CHANNELC_SEQ31_EXIST_DATA
extern uint8_t TransmitDataArrayC31_1[];
#endif
#else
extern uint8_t *TransmitDataArrayC_Seq_1[]; //CAN1 Channel B transmit , Data Array, point to data array. index is Seq No.  This variable is from Simulink
#endif

#endif  //end of "#ifdef USE_CAN1_CHANNELC"

extern volatile uint16_t RxStdCANID_1[]; //Rx Std short frame CANID for CAN1
extern volatile uint32_t RxExtCANID_1[]; //Rx Ext short frame for CAN1
extern volatile uint16_t RxStdCANID_Long_1[]; //Rx Std long frame CANID for CAN1, index is CAN ID index
extern volatile uint32_t RxExtCANID_Long_1[]; //Rx ext long frame CANID for CAN1, index is CAN ID index
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 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 RxStdData_1[][8]; //CAN1 short Std rx Data:  1st index  -> No of packet, 2nd index -> Data No
extern volatile uint8_t RxExtData_1[][8]; //CAN1 short Ext rx Data:  1st index  -> No of packet, 2nd index -> Data No
#endif   //end of "#ifdef USE_CAN1"

#ifdef USE_CAN2
volatile bool Can2RxSuccess=false;
volatile uint32_t Can2TxSuccess=0;   //we use Success tx :  Bit0 For Tx ChannelA, Bit1 Tx for ChannelB ,Bit2 for Tx ChannelC
//Transmit Channel A for CAN2
extern uint16_t TxCAN_A_RTR_DLC_UseStd_2[]; //For CAN2 channel A transmit:  index is Seq No
//bit15: RTR, bit14: Std CANID , Bit13: Need response, Bit12: long frame response,Bit10: reserved. Bit10-0: DLC (Max 255*7=1785 bytes, so 2048 has some no use, >8 as long frame) 
//if index>3 (Seq4, seq5, ...), we will ignore Bit13, take it as 0 (no need response)
//if transmit is long frame (Bit10-0: DLC>8), we will ignore Bit13, take it as 0 (no need response)
// if bit15: RTR, we will ignore Bit13, take it as 0 (no need response)
extern uint16_t TxCAN_A_Timeout_2[]; //Timeout in ms.  This is timeout for Transmit, which needs response. Index is Seq No  ,Max  Index=3
extern uint32_t TransmitFrameCANID_A_2[]; //CAN ID.   Index is Seq No  
//extern uint32_t TxResponseCANID_A_2[];   //Respnse CAN ID, Index is Seq No. Max Index=3.  Bit31:1----Ext frame. 0--std frame. 
extern uint16_t TxResponseCANID_Index_A_2[]; //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 ResponseDataLen_A_2[]; //Respnse Data length in byte, Index is Seq No. Max Index=3.  
//When Length>8, respnse is long frame.  But it is possible, it is long frame  when Length <8, for example. Long frame response, but this time, only feedback one frame
//extern uint8_t ResponseData_A_2[][]; //Respnse Data length in byte, first Index is Seq No. Max Index=3. 2nd index is byte no.
extern uint8_t * ResponseData_A_2[]; //Respnse Data length in byte, first Index is Seq No. Max Index=3. 2nd index is byte no.
extern uint8_t ResponseMaxFrameNo_A_2[]; //Respnse Max frame Number for this time, Index is Seq No. Max Index=3.  

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 RxExtDLC_2[]; //CAN2 short Ext rx: Bit7 :RTR (1 is RTR, after reading, clear to 0), Bit6-0: DLC (0 to 8)

#if 0   //we use array point to replace.  And pointer is given in simulink
#ifdef CAN2_TX_CHANNELA_SEQ0_EXIST_DATA
extern uint8_t TransmitDataArrayA0_2[]; //CAN2 Channel A transmit , Seq0 Data Array
#endif
#ifdef CAN2_TX_CHANNELA_SEQ1_EXIST_DATA
extern uint8_t TransmitDataArrayA1_2[]; //CAN2 Channel A transmit , Seq1 Data Array
#endif
#ifdef CAN2_TX_CHANNELA_SEQ2_EXIST_DATA
extern uint8_t TransmitDataArrayA2_2[];
#endif
#ifdef CAN2_TX_CHANNELA_SEQ3_EXIST_DATA
extern uint8_t TransmitDataArrayA3_2[];
#endif
#ifdef CAN2_TX_CHANNELA_SEQ4_EXIST_DATA
extern uint8_t TransmitDataArrayA4_2[];
#endif
#ifdef CAN2_TX_CHANNELA_SEQ5_EXIST_DATA
extern uint8_t TransmitDataArrayA5_2[];
#endif
#ifdef CAN2_TX_CHANNELA_SEQ6_EXIST_DATA
extern uint8_t TransmitDataArrayA6_2[];
#endif
#ifdef CAN2_TX_CHANNELA_SEQ7_EXIST_DATA
extern uint8_t TransmitDataArrayA7_2[];
#endif
#ifdef CAN2_TX_CHANNELA_SEQ8_EXIST_DATA
extern uint8_t TransmitDataArrayA8_2[];
#endif
#ifdef CAN2_TX_CHANNELA_SEQ9_EXIST_DATA
extern uint8_t TransmitDataArrayA9_2[];
#endif
#ifdef CAN2_TX_CHANNELA_SEQ10_EXIST_DATA
extern uint8_t TransmitDataArrayA10_2[];
#endif

#ifdef CAN2_TX_CHANNELA_SEQ11_EXIST_DATA
extern uint8_t TransmitDataArrayA11_2[]; //CAN2 Channel A transmit , Seq11 Data Array
#endif
#ifdef CAN2_TX_CHANNELA_SEQ12_EXIST_DATA
extern uint8_t TransmitDataArrayA12_2[];
#endif
#ifdef CAN2_TX_CHANNELA_SEQ13_EXIST_DATA
extern uint8_t TransmitDataArrayA13_2[];
#endif
#ifdef CAN2_TX_CHANNELA_SEQ14_EXIST_DATA
extern uint8_t TransmitDataArrayA14_2[];
#endif
#ifdef CAN2_TX_CHANNELA_SEQ15_EXIST_DATA
extern uint8_t TransmitDataArrayA15_2[];
#endif
#ifdef CAN2_TX_CHANNELA_SEQ16_EXIST_DATA
extern uint8_t TransmitDataArrayA16_2[];
#endif
#ifdef CAN2_TX_CHANNELA_SEQ17_EXIST_DATA
extern uint8_t TransmitDataArrayA17_2[];
#endif
#ifdef CAN2_TX_CHANNELA_SEQ18_EXIST_DATA
extern uint8_t TransmitDataArrayA18_2[];
#endif
#ifdef CAN2_TX_CHANNELA_SEQ19_EXIST_DATA
extern uint8_t TransmitDataArrayA19_2[];
#endif
#ifdef CAN2_TX_CHANNELA_SEQ20_EXIST_DATA
extern uint8_t TransmitDataArrayA20_2[];
#endif



#ifdef CAN2_TX_CHANNELA_SEQ21_EXIST_DATA
extern uint8_t TransmitDataArrayA21_2[]; //CAN2 Channel A transmit , Seq21 Data Array
#endif
#ifdef CAN2_TX_CHANNELA_SEQ22_EXIST_DATA
extern uint8_t TransmitDataArrayA22_2[];
#endif
#ifdef CAN2_TX_CHANNELA_SEQ23_EXIST_DATA
extern uint8_t TransmitDataArrayA23_2[];
#endif
#ifdef CAN2_TX_CHANNELA_SEQ24_EXIST_DATA
extern uint8_t TransmitDataArrayA24_2[];
#endif
#ifdef CAN2_TX_CHANNELA_SEQ25_EXIST_DATA
extern uint8_t TransmitDataArrayA25_2[];
#endif
#ifdef CAN2_TX_CHANNELA_SEQ26_EXIST_DATA
extern uint8_t TransmitDataArrayA26_2[];
#endif
#ifdef CAN2_TX_CHANNELA_SEQ27_EXIST_DATA
extern uint8_t TransmitDataArrayA27_2[];
#endif
#ifdef CAN2_TX_CHANNELA_SEQ28_EXIST_DATA
extern uint8_t TransmitDataArrayA28_2[];
#endif
#ifdef CAN2_TX_CHANNELA_SEQ29_EXIST_DATA
extern uint8_t TransmitDataArrayA29_2[];
#endif
#ifdef CAN2_TX_CHANNELA_SEQ30_EXIST_DATA
extern uint8_t TransmitDataArrayA30_2[];
#endif
#ifdef CAN2_TX_CHANNELA_SEQ31_EXIST_DATA
extern uint8_t TransmitDataArrayA31_2[];
#endif
#else
extern uint8_t *TransmitDataArrayA_Seq_2[]; //CAN2 Channel A transmit , Data Array, point to data array. index is Seq No.  This variable is from Simulink
#endif

//Transmit Channel B for CAN2
#ifdef USE_CAN2_CHANNELB
extern uint16_t TxCAN_B_RTR_DLC_UseStd_2[]; //For CAN2 channel B transmit:  index is Seq No
//bit15: RTR, bit14: Std CANID , Bit13: Need response, Bit12: long frame response,Bit10: reserved. Bit10-0: DLC (Max 255*7=1785 bytes, so 2048 has some no use, >8 as long frame) 
//if index>3 (Seq4, seq5, ...), we will ignore Bit13, take it as 0 (no need response)
//if transmit is long frame (Bit10-0: DLC>8), we will ignore Bit13, take it as 0 (no need response)
// if bit15: RTR, we will ignore Bit13, take it as 0 (no need response)
extern uint16_t TxCAN_B_Timeout_2[]; //Timeout in ms.  This is timeout for Transmit, which needs response. Index is Seq No  ,Max  Index=3
extern uint32_t TransmitFrameCANID_B_2[]; //CAN ID.   Index is Seq No  
//extern uint32_t TxResponseCANID_B_2[];   //Respnse CAN ID, Index is Seq No. Max Index=3.  Bit31:1----Ext frame. 0--std frame.  
extern uint16_t TxResponseCANID_Index_B_2[]; //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 ResponseDataLen_B_2[]; //Respnse Data length in byte, Index is Seq No. Max Index=3.  
//When Length>8, respnse is long frame.  But it is possible, it is long frame  when Length <8, for example. Long frame response, but this time, only feedback one frame
//extern uint8_t ResponseData_B_2[][]; //Respnse Data length in byte, first Index is Seq No. Max Index=3. 2nd index is byte no.
extern uint8_t * ResponseData_B_2[]; //Respnse Data length in byte, first Index is Seq No. Max Index=3. 2nd index is byte no.
extern uint8_t ResponseMaxFrameNo_B_2[]; //Respnse Max frame Number for this time, Index is Seq No. Max Index=3. 

#if 0   //we use array point to replace.  And pointer is given in simulink
#ifdef CAN2_TX_CHANNELB_SEQ0_EXIST_DATA
extern uint8_t TransmitDataArrayB0_2[]; //CAN2 Channel B transmit , Seq0 Data Array
#endif
#ifdef CAN2_TX_CHANNELB_SEQ1_EXIST_DATA
extern uint8_t TransmitDataArrayB1_2[]; //CAN2 Channel B transmit , Seq1 Data Array
#endif
#ifdef CAN2_TX_CHANNELB_SEQ2_EXIST_DATA
extern uint8_t TransmitDataArrayB2_2[];
#endif
#ifdef CAN2_TX_CHANNELB_SEQ3_EXIST_DATA
extern uint8_t TransmitDataArrayB3_2[];
#endif
#ifdef CAN2_TX_CHANNELB_SEQ4_EXIST_DATA
extern uint8_t TransmitDataArrayB4_2[];
#endif
#ifdef CAN2_TX_CHANNELB_SEQ5_EXIST_DATA
extern uint8_t TransmitDataArrayB5_2[];
#endif
#ifdef CAN2_TX_CHANNELB_SEQ6_EXIST_DATA
extern uint8_t TransmitDataArrayB6_2[];
#endif
#ifdef CAN2_TX_CHANNELB_SEQ7_EXIST_DATA
extern uint8_t TransmitDataArrayB7_2[];
#endif
#ifdef CAN2_TX_CHANNELB_SEQ8_EXIST_DATA
extern uint8_t TransmitDataArrayB8_2[];
#endif
#ifdef CAN2_TX_CHANNELB_SEQ9_EXIST_DATA
extern uint8_t TransmitDataArrayB9_2[];
#endif
#ifdef CAN2_TX_CHANNELB_SEQ10_EXIST_DATA
extern uint8_t TransmitDataArrayB10_2[];
#endif

#ifdef CAN2_TX_CHANNELB_SEQ11_EXIST_DATA
extern uint8_t TransmitDataArrayB11_2[]; //CAN2 Channel B transmit , Seq11 Data Array
#endif
#ifdef CAN2_TX_CHANNELB_SEQ12_EXIST_DATA
extern uint8_t TransmitDataArrayB12_2[];
#endif
#ifdef CAN2_TX_CHANNELB_SEQ13_EXIST_DATA
extern uint8_t TransmitDataArrayB13_2[];
#endif
#ifdef CAN2_TX_CHANNELB_SEQ14_EXIST_DATA
extern uint8_t TransmitDataArrayB14_2[];
#endif
#ifdef CAN2_TX_CHANNELB_SEQ15_EXIST_DATA
extern uint8_t TransmitDataArrayB15_2[];
#endif
#ifdef CAN2_TX_CHANNELB_SEQ16_EXIST_DATA
extern uint8_t TransmitDataArrayB16_2[];
#endif
#ifdef CAN2_TX_CHANNELB_SEQ17_EXIST_DATA
extern uint8_t TransmitDataArrayB17_2[];
#endif
#ifdef CAN2_TX_CHANNELB_SEQ18_EXIST_DATA
extern uint8_t TransmitDataArrayB18_2[];
#endif
#ifdef CAN2_TX_CHANNELB_SEQ19_EXIST_DATA
extern uint8_t TransmitDataArrayB19_2[];
#endif
#ifdef CAN2_TX_CHANNELB_SEQ20_EXIST_DATA
extern uint8_t TransmitDataArrayB20_2[];
#endif



#ifdef CAN2_TX_CHANNELB_SEQ21_EXIST_DATA
extern uint8_t TransmitDataArrayB21_2[]; //CAN2 Channel B transmit , Seq21 Data Array
#endif
#ifdef CAN2_TX_CHANNELB_SEQ22_EXIST_DATA
extern uint8_t TransmitDataArrayB22_2[];
#endif
#ifdef CAN2_TX_CHANNELB_SEQ23_EXIST_DATA
extern uint8_t TransmitDataArrayB23_2[];
#endif
#ifdef CAN2_TX_CHANNELB_SEQ24_EXIST_DATA
extern uint8_t TransmitDataArrayB24_2[];
#endif
#ifdef CAN2_TX_CHANNELB_SEQ25_EXIST_DATA
extern uint8_t TransmitDataArrayB25_2[];
#endif
#ifdef CAN2_TX_CHANNELB_SEQ26_EXIST_DATA
extern uint8_t TransmitDataArrayB26_2[];
#endif
#ifdef CAN2_TX_CHANNELB_SEQ27_EXIST_DATA
extern uint8_t TransmitDataArrayB27_2[];
#endif
#ifdef CAN2_TX_CHANNELB_SEQ28_EXIST_DATA
extern uint8_t TransmitDataArrayB28_2[];
#endif
#ifdef CAN2_TX_CHANNELB_SEQ29_EXIST_DATA
extern uint8_t TransmitDataArrayB29_2[];
#endif
#ifdef CAN2_TX_CHANNELB_SEQ30_EXIST_DATA
extern uint8_t TransmitDataArrayB30_2[];
#endif
#ifdef CAN2_TX_CHANNELB_SEQ31_EXIST_DATA
extern uint8_t TransmitDataArrayB31_2[];
#endif
#else
extern uint8_t *TransmitDataArrayB_Seq_2[]; //CAN2 Channel B transmit , Data Array, point to data array. index is Seq No.  This variable is from Simulink
#endif

#endif  //end of "#ifdef USE_CAN2_CHANNELB"


//Transmit Channel C for CAN2
#ifdef USE_CAN2_CHANNELC
extern uint16_t TxCAN_C_RTR_DLC_UseStd_2[]; //For CAN2 channel C transmit:  index is Seq No
//bit15: RTR, bit14: Std CANID , Bit13: Need response, Bit12: long frame response,Bit10: reserved. Bit10-0: DLC (Max 255*7=1785 bytes, so 2048 has some no use, >8 as long frame) 
//if index>3 (Seq4, seq5, ...), we will ignore Bit13, take it as 0 (no need response)
//if transmit is long frame (Bit10-0: DLC>8), we will ignore Bit13, take it as 0 (no need response)
// if bit15: RTR, we will ignore Bit13, take it as 0 (no need response)
extern uint16_t TxCAN_C_Timeout_2[]; //Timeout in ms.  This is timeout for Transmit, which needs response. Index is Seq No  ,Max  Index=3
extern uint32_t TransmitFrameCANID_C_2[]; //CAN ID.   Index is Seq No  
//extern uint32_t TxResponseCANID_C_2[];   //Respnse CAN ID, Index is Seq No. Max Index=3.  Bit31:1----Ext frame. 0--std frame.  
extern uint16_t TxResponseCANID_Index_C_2[]; //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 ResponseDataLen_C_2[]; //Respnse Data length in byte, Index is Seq No. Max Index=3.  
//When Length>8, respnse is long frame.  But it is possible, it is long frame  when Length <8, for example. Long frame response, but this time, only feedback one frame
//extern uint8_t ResponseData_C_2[][]; //Respnse Data length in byte, first Index is Seq No. Max Index=3. 2nd index is byte no.
extern uint8_t * ResponseData_C_2[]; //Respnse Data length in byte, first Index is Seq No. Max Index=3. 2nd index is byte no.
extern uint8_t ResponseMaxFrameNo_C_2[]; //Respnse Max frame Number for this time, Index is Seq No. Max Index=3. 

#if 0   //we use array point to replace.  And pointer is given in simulink
#ifdef CAN2_TX_CHANNELC_SEQ0_EXIST_DATA
extern uint8_t TransmitDataArrayC0_2[]; //CAN2 Channel C transmit , Seq0 Data Array
#endif
#ifdef CAN2_TX_CHANNELC_SEQ1_EXIST_DATA
extern uint8_t TransmitDataArrayC1_2[]; //CAN2 Channel C transmit , Seq1 Data Array
#endif
#ifdef CAN2_TX_CHANNELC_SEQ2_EXIST_DATA
extern uint8_t TransmitDataArrayC2_2[];
#endif
#ifdef CAN2_TX_CHANNELC_SEQ3_EXIST_DATA
extern uint8_t TransmitDataArrayC3_2[];
#endif
#ifdef CAN2_TX_CHANNELC_SEQ4_EXIST_DATA
extern uint8_t TransmitDataArrayC4_2[];
#endif
#ifdef CAN2_TX_CHANNELC_SEQ5_EXIST_DATA
extern uint8_t TransmitDataArrayC5_2[];
#endif
#ifdef CAN2_TX_CHANNELC_SEQ6_EXIST_DATA
extern uint8_t TransmitDataArrayC6_2[];
#endif
#ifdef CAN2_TX_CHANNELC_SEQ7_EXIST_DATA
extern uint8_t TransmitDataArrayC7_2[];
#endif
#ifdef CAN2_TX_CHANNELC_SEQ8_EXIST_DATA
extern uint8_t TransmitDataArrayC8_2[];
#endif
#ifdef CAN2_TX_CHANNELC_SEQ9_EXIST_DATA
extern uint8_t TransmitDataArrayC9_2[];
#endif
#ifdef CAN2_TX_CHANNELC_SEQ10_EXIST_DATA
extern uint8_t TransmitDataArrayC10_2[];
#endif

#ifdef CAN2_TX_CHANNELC_SEQ11_EXIST_DATA
extern uint8_t TransmitDataArrayC11_2[]; //CAN2 Channel C transmit , Seq11 Data Array
#endif
#ifdef CAN2_TX_CHANNELC_SEQ12_EXIST_DATA
extern uint8_t TransmitDataArrayC12_2[];
#endif
#ifdef CAN2_TX_CHANNELC_SEQ13_EXIST_DATA
extern uint8_t TransmitDataArrayC13_2[];
#endif
#ifdef CAN2_TX_CHANNELC_SEQ14_EXIST_DATA
extern uint8_t TransmitDataArrayC14_2[];
#endif
#ifdef CAN2_TX_CHANNELC_SEQ15_EXIST_DATA
extern uint8_t TransmitDataArrayC15_2[];
#endif
#ifdef CAN2_TX_CHANNELC_SEQ16_EXIST_DATA
extern uint8_t TransmitDataArrayC16_2[];
#endif
#ifdef CAN2_TX_CHANNELC_SEQ17_EXIST_DATA
extern uint8_t TransmitDataArrayC17_2[];
#endif
#ifdef CAN2_TX_CHANNELC_SEQ18_EXIST_DATA
extern uint8_t TransmitDataArrayC18_2[];
#endif
#ifdef CAN2_TX_CHANNELC_SEQ19_EXIST_DATA
extern uint8_t TransmitDataArrayC19_2[];
#endif
#ifdef CAN2_TX_CHANNELC_SEQ20_EXIST_DATA
extern uint8_t TransmitDataArrayC20_2[];
#endif



#ifdef CAN2_TX_CHANNELC_SEQ21_EXIST_DATA
extern uint8_t TransmitDataArrayC21_2[]; //CAN2 Channel C transmit , Seq21 Data Array
#endif
#ifdef CAN2_TX_CHANNELC_SEQ22_EXIST_DATA
extern uint8_t TransmitDataArrayC22_2[];
#endif
#ifdef CAN2_TX_CHANNELC_SEQ23_EXIST_DATA
extern uint8_t TransmitDataArrayC23_2[];
#endif
#ifdef CAN2_TX_CHANNELC_SEQ24_EXIST_DATA
extern uint8_t TransmitDataArrayC24_2[];
#endif
#ifdef CAN2_TX_CHANNELC_SEQ25_EXIST_DATA
extern uint8_t TransmitDataArrayC25_2[];
#endif
#ifdef CAN2_TX_CHANNELC_SEQ26_EXIST_DATA
extern uint8_t TransmitDataArrayC26_2[];
#endif
#ifdef CAN2_TX_CHANNELC_SEQ27_EXIST_DATA
extern uint8_t TransmitDataArrayC27_2[];
#endif
#ifdef CAN2_TX_CHANNELC_SEQ28_EXIST_DATA
extern uint8_t TransmitDataArrayC28_2[];
#endif
#ifdef CAN2_TX_CHANNELC_SEQ29_EXIST_DATA
extern uint8_t TransmitDataArrayC29_2[];
#endif
#ifdef CAN2_TX_CHANNELC_SEQ30_EXIST_DATA
extern uint8_t TransmitDataArrayC30_2[];
#endif
#ifdef CAN2_TX_CHANNELC_SEQ31_EXIST_DATA
extern uint8_t TransmitDataArrayC31_2[];
#endif
#else
extern uint8_t *TransmitDataArrayC_Seq_2[]; //CAN2 Channel C transmit , Data Array, point to data array. index is Seq No.  This variable is from Simulink
#endif

#endif  //end of "#ifdef USE_CAN2_CHANNELC"
extern volatile uint16_t RxStdCANID_2[]; //Rx Std short frame CANID for CAN2
extern volatile uint32_t RxExtCANID_2[]; //Rx Ext short frame for CAN2
extern volatile uint16_t RxStdCANID_Long_2[]; //Rx Std long frame CANID for CAN2, index is CAN ID index
extern volatile uint32_t RxExtCANID_Long_2[]; //Rx ext long frame CANID for CAN2, index is CAN ID index
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 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 RxStdData_2[][8]; //CAN2 short Std rx Data:  1st index  -> No of packet, 2nd index -> Data No
extern volatile uint8_t RxExtData_2[][8]; //CAN2 short Ext rx Data:  1st index  -> No of packet, 2nd index -> Data No
#endif   //end of "#ifdef USE_CAN2"

//Wrap CAN_Rx_Function
#if defined(USE_CANFD)
    //CANFD
    #define TxHeaderType FDCAN_TxHeaderTypeDef
    #define RxHeaderType FDCAN_RxHeaderTypeDef
    #define HandleTypeDef FDCAN_HandleTypeDef
    #define RX_FIFO0 FDCAN_RX_FIFO0
    #define RX_FIFO1 FDCAN_RX_FIFO1
    #define TX_MAILBOX0 FDCAN_TX_BUFFER0
    #define TX_MAILBOX1 FDCAN_TX_BUFFER1
    #define TX_MAILBOX2 FDCAN_TX_BUFFER2
    #ifdef USE_CAN1
        #define hcan1_general hfdcan1
        #define CAN1_general FDCAN1  //Base address
    #endif
    #ifdef USE_CAN2
        #define hcan2_general hfdcan2
        #define CAN2_general FDCAN2
    #endif
    #define CAN_ID_STD_general FDCAN_STANDARD_ID   //IDE constant value
    #define CAN_ID_EXT_general FDCAN_EXTENDED_ID
    #define RTR_DATA FDCAN_DATA_FRAME    //RTR constant value
    #define RTR_REMOTE FDCAN_REMOTE_FRAME
    #define CAN_TransmitMessage(phfdcan_instance, pTxHeader, pData, pTxMailbox) \
            HAL_FDCAN_AddMessageToTxFifoQ((phfdcan_instance), (pTxHeader), (pData)) // pTxMailbox might be ignored or passed as NULL if not applicable
    #define CAN_ReceiveMessage(phfdcan_instance, RxFifo, pRxHeader, pData) \
            HAL_FDCAN_GetRxMessage((phfdcan_instance), (RxFifo), (pRxHeader), (pData))
    #define CAN_GetRxFifoFillLevel(phfdcan_instance, RxFifo) \
		    HAL_FDCAN_GetRxFifoFillLevel(phfdcan_instance, RxFifo)
    #define CAN_AbortTxRequest(phfdcan_instance, TxBufferIndex) \
		    HAL_FDCAN_AbortTxRequest(phfdcan_instance, TxBufferIndex)

    #define CAN_DLC(header)    ((header).DataLength)
    #define CAN_TX_RTR(header)    ((header).TxFrameType) // For FDCAN, RTR is part of TxFrameType
    #define CAN_RX_RTR(header)    ((header).RxFrameType) // For FDCAN, RTR is part of RxFrameType


    #define CAN_ID_SET_STD(header, id_val)       \
        do {                                        \
            (header).Identifier = (id_val);         \
            (header).IdType = FDCAN_STANDARD_ID;    \
        } while(0)

    #define CAN_ID_SET_EXT(header, id_val)       \
        do {                                        \
            (header).Identifier = (id_val);         \
            (header).IdType = FDCAN_EXTENDED_ID;    \
        } while(0)

    #define CAN_ID_GET(header)                   ((header).Identifier)
    #define CAN_IDE_GET(header)                  ((header).IdType)


    #define CAN_SET_BitRateSwitch_OFF(header)       \
       do {                                        \
           (header).BitRateSwitch = FDCAN_BRS_OFF;         \
       } while(0)
    #define CAN_SET_CLASSIC(header)       \
       do {                                        \
           (header).FDFormat = FDCAN_CLASSIC_CAN;         \
       } while(0)
    #define CAN_SET_TxEventFifo(header)       \
       do {                                        \
           (header).TxEventFifoControl = FDCAN_STORE_TX_EVENTS;         \
       } while(0)
    #define CAN_SET_MessageMarker(header, MarkerValue)       \
       do {                                        \
           (header).MessageMarker = MarkerValue;         \
       } while(0)

    #define CAN_GetLatestTxFifoQRequestBuffer(phfdcan_instance) HAL_FDCAN_GetLatestTxFifoQRequestBuffer(phfdcan_instance)
#else
    //CAN Classic
    #define TxHeaderType CAN_TxHeaderTypeDef
    #define RxHeaderType CAN_RxHeaderTypeDef
    #define HandleTypeDef CAN_HandleTypeDef
    #define RX_FIFO0 CAN_RX_FIFO0
    #define RX_FIFO1 CAN_RX_FIFO1
    #define TX_MAILBOX0 CAN_TX_MAILBOX0
    #define TX_MAILBOX1 CAN_TX_MAILBOX1
    #define TX_MAILBOX2 CAN_TX_MAILBOX2
    #define CAN_ID_STD_general CAN_ID_STD   //IDE constant value
    #define CAN_ID_EXT_general CAN_ID_EXT
    #define RTR_DATA CAN_RTR_DATA    //RTR constant value
    #define RTR_REMOTE CAN_RTR_REMOTE
    #ifdef USE_CAN1
        #define hcan1_general hcan1
        #define CAN1_general CAN1   //base address
    #endif
    #ifdef USE_CAN2
        #define hcan2_general hcan2
        #define CAN2_general CAN2
    #endif
    #define CAN_TransmitMessage(phcan_instance, pTxHeader, pData, pTxMailbox) \
            HAL_CAN_AddTxMessage((phcan_instance), (pTxHeader), (pData), (pTxMailbox))
    #define CAN_ReceiveMessage(phcan_instance, RxFifo, pRxHeader, pData) \
            HAL_CAN_GetRxMessage((phcan_instance), (RxFifo), (pRxHeader), (pData))
    #define CAN_GetRxFifoFillLevel(phcan_instance, RxFifo) \
		    HAL_CAN_GetRxFifoFillLevel(phcan_instance, RxFifo)
    #define CAN_AbortTxRequest(phcan_instance, TxMailbox) \
	        HAL_CAN_AbortTxRequest(phcan_instance, TxMailbox)

   #define CAN_DLC(header)    ((header).DLC)
   #define CAN_TX_RTR(header)    ((header).RTR)
   #define CAN_RX_RTR(header)    ((header).RTR)

   #define CAN_ID_SET_STD(header, id_val)       \
        do {                                        \
            (header).StdId = (id_val);              \
            (header).IDE = CAN_ID_STD;              \
        } while(0)

    #define CAN_ID_SET_EXT(header, id_val)       \
        do {                                        \
            (header).ExtId = (id_val);              \
            (header).IDE = CAN_ID_EXT;              \
        } while(0)

    #define CAN_ID_GET(header)                   \
        (((header).IDE == CAN_ID_EXT) ? (header).ExtId : (header).StdId)

    #define CAN_IDE_GET(header)                  ((header).IDE)

    #define CAN_SET_BitRateSwitch_OFF(header)
    #define CAN_SET_CLASSIC(header)
    #define CAN_SET_TxEventFifo(header)
    #define CAN_SET_MessageMarker(header, MarkerValue)

   #define CAN_GetLatestTxFifoQRequestBuffer(phcan_instance) 0xFFFFFFFFUL
#endif


#ifndef USE_CANFD
//CAN classic
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)  {
	//Standard frame received
#ifdef USE_CAN1
	if (hcan->Instance == CAN1) {
		HAL_CAN_DeactivateNotification(hcan, CAN_IT_RX_FIFO0_MSG_PENDING);
		CAN_myObj1.RxStdCANReceived = 1;
	}
#endif
#ifdef USE_CAN2
	if (hcan->Instance == CAN2) {
		HAL_CAN_DeactivateNotification(hcan, CAN_IT_RX_FIFO0_MSG_PENDING);
		CAN_myObj2.RxStdCANReceived = 1;
	}
#endif
}
void HAL_CAN_RxFifo1MsgPendingCallback(CAN_HandleTypeDef *hcan)  {
	//Ext frame received
#ifdef USE_CAN1
	if (hcan->Instance == CAN1) {
		HAL_CAN_DeactivateNotification(hcan, CAN_IT_RX_FIFO1_MSG_PENDING);
		CAN_myObj1.RxExtCANReceived = 1;
	}
#endif
#ifdef USE_CAN2
	if (hcan->Instance == CAN2) {
		HAL_CAN_DeactivateNotification(hcan, CAN_IT_RX_FIFO1_MSG_PENDING);
		CAN_myObj2.RxExtCANReceived = 1;
	}
#endif
}
void HAL_CAN_TxMailbox0CompleteCallback(CAN_HandleTypeDef *hcan)
{
#ifdef USE_CAN1
	if (hcan->Instance==CAN1_general) {
		if (CanUsedMailBox[0][0]==0) {
	        Can1TxSuccess |= 1;
		}
		else if (CanUsedMailBox[0][1]==0) {
			Can1TxSuccess |= 2;
		}
		else if (CanUsedMailBox[0][2]==0) {
			Can1TxSuccess |= 4;
		}
	}
#endif
#ifdef USE_CAN2
	if (hcan->Instance==CAN2_general) {
		if (CanUsedMailBox[1][0]==0) {
			Can2TxSuccess |= 1;
		}
		else if (CanUsedMailBox[1][1]==0) {
			Can2TxSuccess |= 2;
		}
		else if (CanUsedMailBox[1][2]==0) {
			Can2TxSuccess |= 4;
		}
	}
#endif
}
void HAL_CAN_TxMailbox1CompleteCallback(CAN_HandleTypeDef *hcan)
{
#ifdef USE_CAN1
	if (hcan->Instance==CAN1_general) {
		if (CanUsedMailBox[0][0]==1) {
			Can1TxSuccess |= 1;
		}
		else if (CanUsedMailBox[0][1]==1) {
			Can1TxSuccess |= 2;
		}
		else if (CanUsedMailBox[0][2]==1) {
			Can1TxSuccess |= 4;
		}
	}
#endif
#ifdef USE_CAN2
	if (hcan->Instance==CAN2_general) {
		if (CanUsedMailBox[1][0]==1) {
			Can2TxSuccess |= 1;
		}
		else if (CanUsedMailBox[1][1]==1) {
			Can2TxSuccess |= 2;
		}
		else if (CanUsedMailBox[1][2]==1) {
			Can2TxSuccess |= 4;
		}
	}
#endif
}
void HAL_CAN_TxMailbox2CompleteCallback(CAN_HandleTypeDef *hcan)
{
#ifdef USE_CAN1
	if (hcan->Instance==CAN1_general) {
		if (CanUsedMailBox[0][0]==2) {
			Can1TxSuccess |= 1;
		}
		else if (CanUsedMailBox[0][1]==2) {
			Can1TxSuccess |= 2;
		}
		else if (CanUsedMailBox[0][2]==2) {
			Can1TxSuccess |= 4;
		}
	}

#endif
#ifdef USE_CAN2
	if (hcan->Instance==CAN2_general) {
		if (CanUsedMailBox[1][0]==2) {
			Can2TxSuccess |= 1;
		}
		else if (CanUsedMailBox[1][1]==2) {
			Can2TxSuccess |= 2;
		}
		else if (CanUsedMailBox[1][2]==2) {
			Can2TxSuccess |= 4;
		}
	}
#endif
}
#else
//CANFD
void HAL_FDCAN_RxFifo0Callback(FDCAN_HandleTypeDef *hfdcan, uint32_t RxFifo0ITs) {
	//Standard frame received
#ifdef USE_CAN1
	if (hfdcan->Instance == FDCAN1) {
		HAL_FDCAN_DeactivateNotification(hfdcan, FDCAN_IT_RX_FIFO0_NEW_MESSAGE);
		CAN_myObj1.RxStdCANReceived = 1;
	}
#endif
#ifdef USE_CAN2
	if (hfdcan->Instance == FDCAN2) {
		HAL_FDCAN_DeactivateNotification(hfdcan, FDCAN_IT_RX_FIFO0_NEW_MESSAGE);
		CAN_myObj2.RxStdCANReceived = 1;
	}
#endif
}
void HAL_FDCAN_RxFifo1Callback(FDCAN_HandleTypeDef *hfdcan, uint32_t RxFifo0ITs) {
	//Ext frame received
#ifdef USE_CAN1
	if (hfdcan->Instance == FDCAN1) {
		HAL_FDCAN_DeactivateNotification(hfdcan, FDCAN_IT_RX_FIFO1_NEW_MESSAGE);
		CAN_myObj1.RxExtCANReceived = 1;
	}
#endif
#ifdef USE_CAN2
	if (hfdcan->Instance == FDCAN2) {
		HAL_FDCAN_DeactivateNotification(hfdcan, FDCAN_IT_RX_FIFO1_NEW_MESSAGE);
		CAN_myObj2.RxExtCANReceived = 1;
	}
#endif
}
void HAL_FDCAN_TxBufferCompleteCallback(FDCAN_HandleTypeDef *hcan, uint32_t BufferIndexes)
{
	FDCAN_TxEventFifoTypeDef txEvent;
#ifdef USE_CAN1
	if (HAL_FDCAN_GetTxEvent(hcan, &txEvent) == HAL_OK)
	{
		if (txEvent.MessageMarker == 0) {
			if (hcan->Instance==CAN1_general) {
				    Can1TxSuccess |= 1;
			}
		}
		else if (txEvent.MessageMarker == 1) {
			if (hcan->Instance==CAN1_general) {
					Can1TxSuccess |= 2;
		    }
		}
		else if (txEvent.MessageMarker == 2){
			if (hcan->Instance==CAN1_general) {
					Can1TxSuccess |= 4;
		    }
		}

	}
#endif
#ifdef USE_CAN2
	if (HAL_FDCAN_GetTxEvent(hcan, &txEvent) == HAL_OK)
	{
		if (txEvent.MessageMarker == 0) {
			if (hcan->Instance==CAN2_general) {
				Can2TxSuccess |= 1;
			}
		}
		else if (txEvent.MessageMarker == 1) {
			if (hcan->Instance==CAN2_general) {
				Can2TxSuccess |= 2;
			}
		}
		else if (txEvent.MessageMarker == 2){
			if (hcan->Instance==CAN2_general) {
				Can2TxSuccess |= 4;
			}
		}

	}
#endif
}
#endif





#ifdef USE_CAN1
extern CANBUS_t CAN_myObj1; //for CAN1

/**
 * Put all CAN1  Rx data into our high level buffers
 */
void CAN1_fill_Rx_Data() {
    uint8_t MyCANData[8];
    RxHeaderType tempCAN;
    bool RTR;
    if (CAN_myObj1.RxStdCANReceived) {
        while(CAN_GetRxFifoFillLevel(&hcan1_general, RX_FIFO0)>0) {
    	    if (HAL_OK == CAN_ReceiveMessage(&hcan1_general, RX_FIFO0, &tempCAN, MyCANData) ) {
    	    	RTR=(CAN_RX_RTR(tempCAN)==RTR_REMOTE) ? 1 : 0 ;
    	    	putCAN1RxStdCAN2Buffer(/*uint16_t CANID*/(uint16_t) CAN_ID_GET(tempCAN), /*bool RTR*/RTR, /*uint8_t DLC*/CAN_DLC(tempCAN), /*uint8_t * pCAN_Data*/MyCANData);
    	    }

        }
        CAN_myObj1.RxStdCANReceived=0;
    }
    if (CAN_myObj1.RxExtCANReceived) {
        while(CAN_GetRxFifoFillLevel(&hcan1_general, RX_FIFO1)>0) {
    	    if (HAL_OK == CAN_ReceiveMessage(&hcan1_general, RX_FIFO1, &tempCAN, MyCANData) ) {
    	    	RTR=(CAN_RX_RTR(tempCAN)==RTR_REMOTE) ? 1 : 0 ;
    		    putCAN1RxExtCAN2Buffer(/*uint32_t CANID*/(uint32_t) CAN_ID_GET(tempCAN), /*bool RTR*/RTR, /*uint8_t DLC*/CAN_DLC(tempCAN), /*uint8_t * pCAN_Data*/MyCANData);
    	    }

        }
        CAN_myObj1.RxExtCANReceived=0;
    }

}
void CAN1_Resume_RxInterrupt()  {
#ifndef USE_CANFD
	//CAN classic
	HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_MSG_PENDING|
	            CAN_IT_RX_FIFO1_MSG_PENDING|CAN_IT_TX_MAILBOX_EMPTY);
#else
	//FDCAN
	HAL_FDCAN_ActivateNotification(&hfdcan1, FDCAN_IT_RX_FIFO0_NEW_MESSAGE|
	            FDCAN_IT_RX_FIFO1_NEW_MESSAGE|FDCAN_IT_TX_COMPLETE, 0xFFFFFFFF);
#endif
}

#endif

/**
 * CAN1 Channel A transmit in Seq No, and it can get response data
 * @param Seq
 * @return 
 * 0: Success
 * 1: continue
 * negative: fail
 */
int8_t CAN1_A_Channel_Transmit4HL(uint8_t Seq) {
#ifndef USE_CAN1
    return 0;
#else
    static uint32_t state = 0;
    static uint32_t TransmitCANEnterBuffer=0;
    static uint8_t CurrntFrmNo[MAX_SEQQTY4A_1]; //for long frame transmit
    static uint16_t Tick4Timeout[4]; //long frame response tick for timeout, Max Seq No=3    
    //NO NEED tick for Sync because we clear Sync arrived flag in high level when all Channel transmit sone.
    //static uint16_t SyncArrivedTick;  //Syn arrived tick.  After you get sync, you can transmit if Sync Enable. Sync over 10ms, if no transmit, then Sync expires
    return CAN_All_Channel_Transmit4HL(/*uCANBUS_t * CAN_Obj*/ &CAN_myObj1, /*uint8_t ChannelNo*/0, Seq,
            /*uint32_t *state*/&state, /*uint8_t * CurrntFrmNo*/CurrntFrmNo, /*uint16_t * Tick4Timeout*/Tick4Timeout, /* uint32_t *TransmitCANEnterBuffer*/&TransmitCANEnterBuffer );

#endif


}



/**
 * CAN1 Channel B transmit in Seq No
 * @param Seq
 * @return 
 * 0: Success
 * 1: continue
 * negative: fail
 */
int8_t CAN1_B_Channel_Transmit4HL(uint8_t Seq) {
#ifndef USE_CAN1
    return 0;
#else
#ifndef USE_CAN1_CHANNELB
    return 0;
#else
    static uint32_t state = 0;
    static uint32_t TransmitCANEnterBuffer=0;
    static uint8_t CurrntFrmNo[MAX_SEQQTY4B_1]; //for long frame transmit
    static uint16_t Tick4Timeout[4]; //long frame response tick for timeout, Max Seq No=3    
    //NO NEED tick for Sync because we clear Sync arrived flag in high level when all Channel transmit sone.
    //static uint16_t SyncArrivedTick;  //Syn arrived tick.  After you get sync, you can transmit if Sync Enable. Sync over 10ms, if no transmit, then Sync expires
     return CAN_All_Channel_Transmit4HL(/*uCANBUS_t * CAN_Obj*/ &CAN_myObj1, /*uint8_t ChannelNo*/1, Seq,
            /*uint32_t *state*/&state, /*uint8_t * CurrntFrmNo*/CurrntFrmNo, /*uint16_t * Tick4Timeout*/Tick4Timeout, &TransmitCANEnterBuffer);
#endif

#endif
}

/**
 * CAN1 Channel C transmit in Seq No
 * @param Seq
 * @return 
 * 0: Success
 * 1: continue
 * negative: fail
 */
int8_t CAN1_C_Channel_Transmit4HL(uint8_t Seq) {
#ifndef USE_CAN1
    return 0;
#else   
#ifndef USE_CAN1_CHANNELC
    return 0;
#else
    static uint32_t state = 0;
    static uint32_t TransmitCANEnterBuffer=0;
    static uint8_t CurrntFrmNo[MAX_SEQQTY4C_1]; //for long frame transmit
    static uint16_t Tick4Timeout[4]; //long frame response tick for timeout, Max Seq No=3    
    //NO NEED tick for Sync because we clear Sync arrived flag in high level when all Channel transmit sone.
    //static uint16_t SyncArrivedTick;  //Syn arrived tick.  After you get sync, you can transmit if Sync Enable. Sync over 10ms, if no transmit, then Sync expires
    return CAN_All_Channel_Transmit4HL(/*uCANBUS_t * CAN_Obj*/ &CAN_myObj1, /*uint8_t ChannelNo*/2, Seq,
            /*uint32_t *state*/&state, /*uint8_t * CurrntFrmNo*/CurrntFrmNo, /*uint16_t * Tick4Timeout*/Tick4Timeout,&TransmitCANEnterBuffer);
#endif
#endif
}

#ifdef USE_CAN2
extern CANBUS_t CAN_myObj2; //for CAN2
/**
 * Put all CAN2  Rx data into our high level buffers
 */
void CAN2_fill_Rx_Data() {
    uint8_t MyCANData[8];
    RxHeaderType tempCAN;
    bool RTR;
    if (CAN_myObj2.RxStdCANReceived) {
        while(CAN_GetRxFifoFillLevel(&hcan2_general, RX_FIFO0)>0) {
    	    if (HAL_OK == CAN_ReceiveMessage(&hcan2_general, RX_FIFO0, &tempCAN, MyCANData) ) {
    	    	RTR=(CAN_RX_RTR(tempCAN)==RTR_REMOTE) ? 1 : 0 ;
    	    	putCAN2RxStdCAN2Buffer(/*uint16_t CANID*/(uint16_t) CAN_ID_GET(tempCAN), /*bool RTR*/RTR, /*uint8_t DLC*/CAN_DLC(tempCAN), /*uint8_t * pCAN_Data*/MyCANData);
    	    }

        }
        CAN_myObj2.RxStdCANReceived=0;
    }
    if (CAN_myObj2.RxExtCANReceived) {
        while(CAN_GetRxFifoFillLevel(&hcan2_general, RX_FIFO1)>0) {
    	    if (HAL_OK == CAN_ReceiveMessage(&hcan2_general, RX_FIFO1, &tempCAN, MyCANData) ) {
    	    	RTR=(CAN_RX_RTR(tempCAN)==RTR_REMOTE) ? 1 : 0 ;
    		    putCAN2RxExtCAN2Buffer(/*uint32_t CANID*/(uint32_t) CAN_ID_GET(tempCAN), /*bool RTR*/RTR, /*uint8_t DLC*/CAN_DLC(tempCAN), /*uint8_t * pCAN_Data*/MyCANData);
    	    }

        }
        CAN_myObj2.RxExtCANReceived=0;
    }

}

void CAN2_Resume_RxInterrupt()  {
#ifndef USE_CANFD
	//CAN classic
	HAL_CAN_ActivateNotification(&hcan2, CAN_IT_RX_FIFO0_MSG_PENDING|
	            CAN_IT_RX_FIFO1_MSG_PENDING|CAN_IT_TX_MAILBOX_EMPTY);
#else
	//FDCAN
	HAL_FDCAN_ActivateNotification(&hfdcan2, FDCAN_IT_RX_FIFO0_NEW_MESSAGE|
	            FDCAN_IT_RX_FIFO1_NEW_MESSAGE|FDCAN_IT_TX_COMPLETE, 0xFFFFFFFF);
#endif
}



#endif

/**
 * CAN2 Channel A transmit in Seq No
 * @param Seq
 * @return 
 * 0: Success
 * 1: continue
 * negative: fail
 */
int8_t CAN2_A_Channel_Transmit4HL(uint8_t Seq) {
#ifndef USE_CAN2
    return 0;
#else
#ifndef USE_CAN2_CHANNELA
    return 0;
#else
    static uint32_t state = 0;
    static uint32_t TransmitCANEnterBuffer=0;
    static uint8_t CurrntFrmNo[MAX_SEQQTY4A_2]; //for long frame transmit
    static uint16_t Tick4Timeout[4]; //long frame response tick for timeout, Max Seq No=3    
    //NO NEED tick for Sync because we clear Sync arrived flag in high level when all Channel transmit sone.
    //static uint16_t SyncArrivedTick;  //Syn arrived tick.  After you get sync, you can transmit if Sync Enable. Sync over 10ms, if no transmit, then Sync expires
    return CAN_All_Channel_Transmit4HL(/*uCANBUS_t * CAN_Obj*/ &CAN_myObj2, /*uint8_t ChannelNo*/0, Seq,
            /*uint32_t *state*/&state, /*uint8_t * CurrntFrmNo*/CurrntFrmNo, /*uint16_t * Tick4Timeout*/Tick4Timeout,&TransmitCANEnterBuffer);
#endif
#endif
}

/**
 * CAN2 Channel B transmit in Seq No
 * @param Seq
 * @return 
 * 0: Success
 * 1: continue
 * negative: fail
 */
int8_t CAN2_B_Channel_Transmit4HL(uint8_t Seq) {
#ifndef USE_CAN2
    return 0;
#else
#ifndef USE_CAN2_CHANNELB
    return 0;
#else
    static uint32_t state = 0;
    static uint32_t TransmitCANEnterBuffer=0;
    static uint8_t CurrntFrmNo[MAX_SEQQTY4B_2]; //for long frame transmit
    static uint16_t Tick4Timeout[4]; //long frame response tick for timeout, Max Seq No=3    
    //NO NEED tick for Sync because we clear Sync arrived flag in high level when all Channel transmit sone.
    //static uint16_t SyncArrivedTick;  //Syn arrived tick.  After you get sync, you can transmit if Sync Enable. Sync over 10ms, if no transmit, then Sync expires
    return CAN_All_Channel_Transmit4HL(/*uCANBUS_t * CAN_Obj*/ &CAN_myObj2, /*uint8_t ChannelNo*/1, Seq,
            /*uint32_t *state*/&state, /*uint8_t * CurrntFrmNo*/CurrntFrmNo, /*uint16_t * Tick4Timeout*/Tick4Timeout,&TransmitCANEnterBuffer);
#endif
#endif
}

/**
 * CAN2 Channel C transmit in Seq No
 * @param Seq
 * @return 
 * 0: Success
 * 1: continue
 * negative: fail
 */
int8_t CAN2_C_Channel_Transmit4HL(uint8_t Seq) {
#ifndef USE_CAN2
    return 0;
#else    
#ifndef USE_CAN2_CHANNELC
    return 0;
#else
    static uint32_t state = 0;
    static uint32_t TransmitCANEnterBuffer=0;
    static uint8_t CurrntFrmNo[MAX_SEQQTY4C_2]; //for long frame transmit
    static uint16_t Tick4Timeout[4]; //long frame response tick for timeout, Max Seq No=3    
    //NO NEED tick for Sync because we clear Sync arrived flag in high level when all Channel transmit sone.
    //static uint16_t SyncArrivedTick;  //Syn arrived tick.  After you get sync, you can transmit if Sync Enable. Sync over 10ms, if no transmit, then Sync expires
    return CAN_All_Channel_Transmit4HL(/*uCANBUS_t * CAN_Obj*/ &CAN_myObj2, /*uint8_t ChannelNo*/2, Seq,
            /*uint32_t *state*/&state, /*uint8_t * CurrntFrmNo*/CurrntFrmNo, /*uint16_t * Tick4Timeout*/Tick4Timeout,&TransmitCANEnterBuffer);
#endif
#endif
}

int8_t CAN_All_Channel_Transmit4HL(CANBUS_t * CAN_Obj, uint8_t ChannelNo, uint8_t Seq, uint32_t *state, uint8_t * CurrntFrmNo, uint16_t * Tick4Timeout, uint32_t *TransmitCANEnterBuffer) {
    //struct CAN_MSG_OBJ msg;
	struct msg_struc {
		TxHeaderType txHead;
		uint8_t * myData;
	};
	struct msg_struc msg;
	HandleTypeDef * pMyCANhandle;
    bool Stage4Seq;
    bool TransmitEnterBuffer4Seq;
    bool BusOFF_F_Return;
    uint16_t TimeOutSettings;
    uint8_t * ResponseData;
    uint8_t ResponseDataLen;
    uint16_t TxResponseCANID_Index;
    uint16_t TxCAN_RTR_DLC_UseStd;

    uint32_t TransmitFrameCANID;
    uint8_t * TransmitDataArray_Seq;
    volatile uint8_t * pRxdDLC;
    volatile uint8_t(* pRxData)[8];

    volatile uint16_t * RxStdCANID;
    volatile uint32_t * RxExtCANID;
    volatile uint8_t * successTx;

    volatile uint32_t *mailbox4classic;  //this is for returned transmitted mailbox
    if (1 == CAN_Obj->CAN_Peripheral_No) {
#ifndef USE_CAN1
        return 0;
#else
        successTx=&Can1TxSuccess;
        switch (ChannelNo) {
            case 0:
                //A channel
                TimeOutSettings = TxCAN_A_Timeout_1[Seq];
                ResponseData = ResponseData_A_1[Seq];
                ResponseDataLen = ResponseDataLen_A_1[Seq];
                TxResponseCANID_Index = TxResponseCANID_Index_A_1[Seq];
                TxCAN_RTR_DLC_UseStd = TxCAN_A_RTR_DLC_UseStd_1[Seq];

                TransmitDataArray_Seq = TransmitDataArrayA_Seq_1[Seq];
                TransmitFrameCANID = TransmitFrameCANID_A_1[Seq];
                mailbox4classic=&CanUsedMailBox[0][0];
                break;
#ifdef USE_CAN1_CHANNELB
            case 1:
                //B channel
                TimeOutSettings = TxCAN_B_Timeout_1[Seq];
                ResponseData = ResponseData_B_1[Seq];
                ResponseDataLen = ResponseDataLen_B_1[Seq];
                TxResponseCANID_Index = TxResponseCANID_Index_B_1[Seq];
                TxCAN_RTR_DLC_UseStd = TxCAN_B_RTR_DLC_UseStd_1[Seq];

                TransmitDataArray_Seq = TransmitDataArrayB_Seq_1[Seq];
                TransmitFrameCANID = TransmitFrameCANID_B_1[Seq];
                mailbox4classic=&CanUsedMailBox[0][1];
                break;
#endif
#ifdef USE_CAN1_CHANNELC
            case 2:
                //C Channel
                TimeOutSettings = TxCAN_C_Timeout_1[Seq];
                ResponseData = ResponseData_C_1[Seq];
                ResponseDataLen = ResponseDataLen_C_1[Seq];
                TxResponseCANID_Index = TxResponseCANID_Index_C_1[Seq];
                TxCAN_RTR_DLC_UseStd = TxCAN_C_RTR_DLC_UseStd_1[Seq];

                TransmitDataArray_Seq = TransmitDataArrayC_Seq_1[Seq];
                TransmitFrameCANID = TransmitFrameCANID_C_1[Seq];
                mailbox4classic=&CanUsedMailBox[0][2];
#endif

        }
#endif
    } else if (2 == CAN_Obj->CAN_Peripheral_No) {
#ifndef USE_CAN2
        return 0;
#else
        successTx=&Can2TxSuccess;
        switch (ChannelNo) {
            case 0:
                //A channel
                TimeOutSettings = TxCAN_A_Timeout_2[Seq];
                ResponseData = ResponseData_A_2[Seq];
                ResponseDataLen = ResponseDataLen_A_2[Seq];
                TxResponseCANID_Index = TxResponseCANID_Index_A_2[Seq];
                TxCAN_RTR_DLC_UseStd = TxCAN_A_RTR_DLC_UseStd_2[Seq];

                TransmitDataArray_Seq = TransmitDataArrayA_Seq_2[Seq];
                TransmitFrameCANID = TransmitFrameCANID_A_2[Seq];
                mailbox4classic=&CanUsedMailBox[1][0];
                break;
#ifdef USE_CAN2_CHANNELB
            case 1:
                //B channel
                TimeOutSettings = TxCAN_B_Timeout_2[Seq];
                ResponseData = ResponseData_B_2[Seq];
                ResponseDataLen = ResponseDataLen_B_2[Seq];
                TxResponseCANID_Index = TxResponseCANID_Index_B_2[Seq];
                TxCAN_RTR_DLC_UseStd = TxCAN_B_RTR_DLC_UseStd_2[Seq];

                TransmitDataArray_Seq = TransmitDataArrayB_Seq_2[Seq];
                TransmitFrameCANID = TransmitFrameCANID_B_2[Seq];
                mailbox4classic=&CanUsedMailBox[1][1];
                break;
#endif
#ifdef USE_CAN2_CHANNELC
            case 2:
                //C Channel
                TimeOutSettings = TxCAN_C_Timeout_2[Seq];
                ResponseData = ResponseData_C_2[Seq];
                ResponseDataLen = ResponseDataLen_C_2[Seq];
                TxResponseCANID_Index = TxResponseCANID_Index_C_2[Seq];
                TxCAN_RTR_DLC_UseStd = TxCAN_C_RTR_DLC_UseStd_2[Seq];

                TransmitDataArray_Seq = TransmitDataArrayC_Seq_2[Seq];
                TransmitFrameCANID = TransmitFrameCANID_C_2[Seq];
                mailbox4classic=&CanUsedMailBox[1][2];
#endif

        }

#endif
    }

    Stage4Seq = (*state >> Seq) &0x1;
    TransmitEnterBuffer4Seq=(*TransmitCANEnterBuffer >> Seq) &0x1;
    if (1 == CAN_Obj->CAN_Peripheral_No) {
         #ifdef USE_CAN1
            #ifndef USE_CANFD
            //CAN CLASSIC
    	    if (hcan1.Instance->ESR & CAN_ESR_BOFF) {
    	        // Bus-Off state is active
    	    	BusOFF_F_Return=true;
    	    }
            else BusOFF_F_Return=false;
            #else
            //CANFD
            if (hfdcan1.Instance->PSR & FDCAN_PSR_BO) {
                // Bus-Off state detected
            	BusOFF_F_Return=true;
            }
            else BusOFF_F_Return=false;
            #endif
            pMyCANhandle = &hcan1_general;
        #else
            BusOFF_F_Return=true;
        #endif
    } else {
        #ifdef USE_CAN2
            #ifndef USE_CANFD
            //CAN CLASSIC
            if (hcan2.Instance->ESR & CAN_ESR_BOFF) {
    	        // Bus-Off state is active
                BusOFF_F_Return=true;
            }
            else BusOFF_F_Return=false;
            #else
            //CANFD
            if (hfdcan2.Instance->PSR & FDCAN_PSR_BO) {
                // Bus-Off state detected
    	        BusOFF_F_Return=true;
            }
            else BusOFF_F_Return=false;
            #endif
            pMyCANhandle = &hcan2_general;

        #else
            BusOFF_F_Return=true;
        #endif
    }
    //if (CAN1_IsBusOff() == false) {
    if (BusOFF_F_Return == false) {
        if (0 == Stage4Seq) {
            //Transmit stage
            if (CAN_Obj->EnableSyn) {
                //Enable Sync
                //if sync does not arrive, we return "continue next time"
                if (0 == (CAN_Obj->SyncCANID & 0x80000000)) return 1;
            }
            if (TxCAN_RTR_DLC_UseStd & 0x4000)
            	CAN_ID_SET_STD(msg.txHead,TransmitFrameCANID);
            else
            	CAN_ID_SET_EXT(msg.txHead,TransmitFrameCANID);

            if (TxCAN_RTR_DLC_UseStd & 0x8000) {
                //RTR transmit
                if (!TransmitEnterBuffer4Seq) {
                    //Didn't send tx to buffer
                    //msg.field.frameType = CAN_FRAME_RTR;
                	CAN_TX_RTR(msg.txHead)=RTR_REMOTE;
                	CAN_DLC(msg.txHead)=0;
                	CAN_SET_BitRateSwitch_OFF(msg.txHead);
                	CAN_SET_CLASSIC(msg.txHead);
                	CAN_SET_TxEventFifo(msg.txHead);
                	CAN_SET_MessageMarker(msg.txHead,ChannelNo);
                	msg.myData=TransmitDataArray_Seq;

                	if (HAL_OK == CAN_TransmitMessage(pMyCANhandle,&msg.txHead,msg.myData,mailbox4classic))  {
                		uint32_t myTemp;
                		myTemp=CAN_GetLatestTxFifoQRequestBuffer(pMyCANhandle);
                		if (myTemp <3) {
                			*mailbox4classic=myTemp;
                		}
                		else {
                		    (*mailbox4classic) --;  //classic CAN problem:  API use bit wise:  0->1 1->2 2->4
                            if ((*mailbox4classic)>2)  (*mailbox4classic)=2;
                        }    
                		*TransmitCANEnterBuffer |= (1U << Seq);
                		Tick4Timeout[ChannelNo]=tick;
                		return 1;  //continue
                	}  else return -1;
                }
                else {
                    //already send tx to buffer, so judge for sent out
                    if (*successTx & (1U<<ChannelNo)) {
                        //Success sent out
                        *TransmitCANEnterBuffer &= ~(1U << Seq);
                        return 0;
                    }
                    else
                    {
                        if (tick-Tick4Timeout[ChannelNo]>=500) {
                            //timeout for 500ms
                            *TransmitCANEnterBuffer &= ~(1U << Seq);
                            CAN_AbortTxRequest(pMyCANhandle,*mailbox4classic);
                            return -1;

                        }
                    }

                }

            } else {
                //Data frame
            	CAN_TX_RTR(msg.txHead)=RTR_DATA;   //Very important because msg is in stack. Value is random
            	CAN_DLC(msg.txHead)=TxCAN_RTR_DLC_UseStd & 0x7FF; //Bit10-0: length


                if ((Seq <= 3) && (CAN_DLC(msg.txHead) <= 8) && (TxCAN_RTR_DLC_UseStd & 0x2000)) { //Bit13: need response
                    //Need response
                    if (!TransmitEnterBuffer4Seq) {
                        //Tx didn't send to Budffe
                    	msg.myData=TransmitDataArray_Seq;
                    	CAN_SET_BitRateSwitch_OFF(msg.txHead);
                    	CAN_SET_CLASSIC(msg.txHead);
                    	CAN_SET_TxEventFifo(msg.txHead);
                    	CAN_SET_MessageMarker(msg.txHead,ChannelNo);
                    	if (HAL_OK == CAN_TransmitMessage(pMyCANhandle,&msg.txHead,msg.myData,mailbox4classic)) {
                    		//success to put tx into buffer
                    		uint32_t myTemp;
                    		myTemp=CAN_GetLatestTxFifoQRequestBuffer(pMyCANhandle);
                    		if (myTemp <3) {
                    			*mailbox4classic=myTemp;
                    		}
                    		else {
                    		    (*mailbox4classic) --;  //classic CAN problem:  API use bit wise:  0->1 1->2 2->4
                                if ((*mailbox4classic)>2)  (*mailbox4classic)=2;
                            }
                    		*TransmitCANEnterBuffer |= (1U << Seq);
                    		Tick4Timeout[ChannelNo]=tick;
                    		return 1;  //continue


                    	} else {
                    		//fail to put tx into buffer
                    		return -1; //fail
                    	}
                    }
                    else {
                        //already send tx to buffer, so judge for sent out
                        if (*successTx & (1U<<ChannelNo)) {
                            //Success sent out
                            *TransmitCANEnterBuffer &= ~(1U << Seq);

                            *state = *state | (1 << Seq);
                            //set bit14 of Rx CANID to 1 for std frame response , or set bit30 of Rx canid to 1 for ext frame response
                            if (0x0000 == (TxResponseCANID_Index & 0xc000)) {
                                //short std frame response
                                if (1 == CAN_Obj->CAN_Peripheral_No) {
#ifdef USE_CAN1
                                    RxStdCANID = RxStdCANID_1;
#endif
                                } else {
#ifdef USE_CAN2
                                    RxStdCANID = RxStdCANID_2;
#endif
                                }
                                //RxStdCANID_1[TxResponseCANID_Index & 0x3FFF] |= 0x4000;
                                RxStdCANID[TxResponseCANID_Index & 0x3FFF] |= 0x4000;

                            } else if (0x4000 == (TxResponseCANID_Index & 0xc000)) {
                                //short ext frame response
                                if (1 == CAN_Obj->CAN_Peripheral_No) {
#ifdef USE_CAN1
                                    RxExtCANID = RxExtCANID_1;
#endif
                                } else {
#ifdef USE_CAN2
                                    RxExtCANID = RxExtCANID_2;
#endif
                                }
                                //RxExtCANID_1[TxResponseCANID_Index & 0x3FFF] |= 0x40000000;
                                RxExtCANID[TxResponseCANID_Index & 0x3FFF] |= 0x40000000;

                            } else if (0x8000 == (TxResponseCANID_Index & 0xc000)) {
                                //long std frame response
                                if (1 == CAN_Obj->CAN_Peripheral_No) {
#ifdef USE_CAN1
                                    RxStdCANID = RxStdCANID_Long_1;
#endif
                                } else {
#ifdef USE_CAN2
                                    RxStdCANID = RxStdCANID_Long_2;
#endif
                                }
                                //RxStdCANID_Long_1[TxResponseCANID_Index & 0x3FFF] |= 0x4000;
                                RxStdCANID[TxResponseCANID_Index & 0x3FFF] |= 0x4000;
                            } else if (0xC000 == (TxResponseCANID_Index & 0xc000)) {
                                //long ext frame response
                                if (1 == CAN_Obj->CAN_Peripheral_No) {
#ifdef USE_CAN1
                                    RxExtCANID = RxExtCANID_Long_1;
#endif
                                } else {
#ifdef USE_CAN2
                                    RxExtCANID = RxExtCANID_Long_2;
#endif
                                }
                                //RxExtCANID_Long_1[TxResponseCANID_Index & 0x3FFF] |= 0x40000000;
                                RxExtCANID[TxResponseCANID_Index & 0x3FFF] |= 0x40000000;
                            }
                            Tick4Timeout[Seq] = tick;
                            return 1; //Continue


                        }
                        else
                        {
                            if (tick-Tick4Timeout[ChannelNo]>=500) {
                                //timeout for 500ms
                                *TransmitCANEnterBuffer &= ~(1U << Seq);
                                CAN_AbortTxRequest(pMyCANhandle,*mailbox4classic);
                                return -1;
                            }
                        }

                    }


                } else {
                    //No need response
                    if (CAN_DLC(msg.txHead) <= 8) {
                        //short frame
                        if (!TransmitEnterBuffer4Seq) {
                            //Tx didn't send to Budffe
                        	msg.myData=TransmitDataArray_Seq;
                        	CAN_SET_BitRateSwitch_OFF(msg.txHead);
                        	CAN_SET_CLASSIC(msg.txHead);
                        	CAN_SET_TxEventFifo(msg.txHead);
                        	CAN_SET_MessageMarker(msg.txHead,ChannelNo);
                            if (HAL_OK == CAN_TransmitMessage(pMyCANhandle,&msg.txHead,msg.myData,mailbox4classic)) {
                            	uint32_t myTemp;
                            	myTemp=CAN_GetLatestTxFifoQRequestBuffer(pMyCANhandle);
                            	if (myTemp <3) {
                            		*mailbox4classic=myTemp;
                            	}
                        		else {
                        		    (*mailbox4classic) --;  //classic CAN problem:  API use bit wise:  0->1 1->2 2->4
                                    if ((*mailbox4classic)>2)  (*mailbox4classic)=2;
                                }
                            	*TransmitCANEnterBuffer |= (1U << Seq);
                            	Tick4Timeout[ChannelNo]=tick;
                            	return 1;

                            } else return -1;
                        }
                        else {
                            //already send tx to buffer, so judge for sent out
                            if (*successTx & (1U<<ChannelNo)) {
                                //Success sent out
                                *TransmitCANEnterBuffer &= ~(1U << Seq);
                                return 0;  //success
                            }
                            else {
                               //judge time out because not done
                               if (tick-Tick4Timeout[ChannelNo]>=500) {
                                    //timeout for 500ms
                                    *TransmitCANEnterBuffer &= ~(1U << Seq);
                                    CAN_AbortTxRequest(pMyCANhandle,*mailbox4classic);
                                    return -1;
                                }
                            }

                        }

                    } else {
                        //long frame
                        uint8_t HasTransmittedByteQty;
                        uint8_t pData[8];
                        uint8_t dataDLCNew;
                        uint8_t i;
                        if (0 == CAN_Obj->FrameNoIs0_based) {
                            //1-based frame No
                            if (0 == CurrntFrmNo[Seq]) CurrntFrmNo[Seq] = 1;
                            HasTransmittedByteQty = (CurrntFrmNo[Seq] - 1)*7;
                        } else {
                            //0 based frame No
                            HasTransmittedByteQty = CurrntFrmNo[Seq]*7;
                        }

                        pData[0] = CurrntFrmNo[Seq];
                        if (CAN_DLC(msg.txHead) - HasTransmittedByteQty >= 7) {
                            //msg.field.dlc = 8;
                            dataDLCNew=8;
                            for (i = 1; i < 8; i++) {
                                pData[i] = *(TransmitDataArray_Seq + HasTransmittedByteQty + i - 1);
                            }

                        } else if (CAN_DLC(msg.txHead) - HasTransmittedByteQty > 0) {
                            //msg.field.dlc = TxDATALen - HasTransmittedByteQty + 1;
                            dataDLCNew=CAN_DLC(msg.txHead) - HasTransmittedByteQty + 1;
                            for (i = 0; i < CAN_DLC(msg.txHead) - HasTransmittedByteQty; i++) {
                                pData[i + 1] = *(TransmitDataArray_Seq + HasTransmittedByteQty + i);
                            }
                        } else {
                            //==-, no need to transmit
                            *TransmitCANEnterBuffer &= ~(1U << Seq);
                            CurrntFrmNo[Seq] = 0;
                            return 0; //success done all frames Tx
                        }

                        if (!TransmitEnterBuffer4Seq) {
                            //Tx didn't send to Budffe
                        	msg.myData=pData;
                        	CAN_DLC(msg.txHead)=dataDLCNew;
                        	CAN_SET_BitRateSwitch_OFF(msg.txHead);
                        	CAN_SET_CLASSIC(msg.txHead);
                        	CAN_SET_TxEventFifo(msg.txHead);
                        	CAN_SET_MessageMarker(msg.txHead,ChannelNo);
                            if (HAL_OK == CAN_TransmitMessage(pMyCANhandle,&msg.txHead,msg.myData,mailbox4classic)) {
                                //success to put tx into buffer
                            	uint32_t myTemp;
                            	myTemp=CAN_GetLatestTxFifoQRequestBuffer(pMyCANhandle);
                            	if (myTemp <3) {
                            		*mailbox4classic=myTemp;
                            	}
                            	else {
                            	    (*mailbox4classic) --;  //classic CAN problem:  API use bit wise:  0->1 1->2 2->4
                                    if ((*mailbox4classic)>2)  (*mailbox4classic)=2;
                                }
                                *TransmitCANEnterBuffer |= (1U << Seq);
                                Tick4Timeout[ChannelNo]=tick;
                                return 1; //continue to next frame in next time
                            } else {
                                //fail to put tx into buffer
                                CurrntFrmNo[Seq] = 0; //init it
                                return -1; //fail
                            }

                        }
                        else {
                            //already send tx to buffer, so judge for sent out
                            if (*successTx & (1U<<ChannelNo)) {
                                //Success sent out
                                *TransmitCANEnterBuffer &= ~(1U << Seq);
                                if (CAN_DLC(msg.txHead) == HasTransmittedByteQty + dataDLCNew - 1) { //Judgement ahead of time in case 255+1=0 case
                                    CurrntFrmNo[Seq] = 0;
                                    return 0; //success done all frames Tx
                                }
                                CurrntFrmNo[Seq]++;
                                return 1; //continue to next frame in next time

                            }
                            else {
                               //judge time out because not done
                               if (tick-Tick4Timeout[ChannelNo]>=500) {
                                    //timeout for 500ms
                                    *TransmitCANEnterBuffer &= ~(1U << Seq);
                                    CurrntFrmNo[Seq] = 0; //init it
                                    CAN_AbortTxRequest(pMyCANhandle,*mailbox4classic);
                                    return -1;
                                }
                            }
                        }





                    }
                }

            }


        } else {
            //response stage
            //wait for response stage:  This is situation we need response frame
            if (TxCAN_RTR_DLC_UseStd & 0x1000) { //bit12 : long frame response
                //need long frame response
                if (0x8000 == (TxResponseCANID_Index & 0xc000)) {
                    //Standard 11 bit CAN ID long frame
                    //2025 May 06 add for uninitialze pointer----start
                    if (1 == CAN_Obj->CAN_Peripheral_No) {
#ifdef USE_CAN1

                        RxStdCANID = RxStdCANID_Long_1;
#endif

                    } else {
#ifdef USE_CAN2

                        RxStdCANID = RxStdCANID_Long_2;
#endif
                    }
                    //2025 May 06 add for uninitialze pointer----end
                    //if (!(RxStdCANID_Long_1[TxResponseCANID_Index & 0x3FFF] & 0x4000)) {
                    if (!(RxStdCANID[TxResponseCANID_Index & 0x3FFF] & 0x4000)) {
                        //Response coming
                        //get data from long frame
                        //ResponseDataLen=TxCAN_RTR_DLC_UseStd & 0x3ff;
                        int16_t i;
                        uint8_t j;
                        if (1 == CAN_Obj->CAN_Peripheral_No) {
#ifdef USE_CAN1
                            for (i = 0; i < ResponseDataLen; i++) {
                                j = i / 7;
                                ResponseData[i] = RxStdData_Long_1[TxResponseCANID_Index & 0x3FFF][j][(i % 7) + 1];
                                //ResponseData[i] = RxData_Long[TxResponseCANID_Index & 0x3FFF][j][(i % 7) + 1];
                            }
#endif
                        } else {
#ifdef USE_CAN2
                            for (i = 0; i < ResponseDataLen; i++) {
                                j = i / 7;
                                ResponseData[i] = RxStdData_Long_2[TxResponseCANID_Index & 0x3FFF][j][(i % 7) + 1];
                                //ResponseData[i] = RxData_Long[TxResponseCANID_Index & 0x3FFF][j][(i % 7) + 1];
                            }
#endif

                        }
                        *state = *state & ~(1UL << Seq);
                        return 0; //success
                    } else {
                        //check time out
                        if (tick - Tick4Timeout[Seq] > TimeOutSettings) {
                            //Time out
                            *state = *state & ~(1UL << Seq);
                            return -1; //fail
                        } else return 1; //continue
                    }

                } else if (0xC000 == (TxResponseCANID_Index & 0xc000)) {
                    //Ext 29 bit CAN ID long frame
                    //2025 May 06 add for uninitialze pointer----start
                    if (1 == CAN_Obj->CAN_Peripheral_No) {
#ifdef USE_CAN1
                        RxExtCANID = RxExtCANID_Long_1;
#endif

                    } else {
#ifdef USE_CAN2
                        RxExtCANID = RxExtCANID_Long_2;
#endif
                    }
                    //2025 May 06 add for uninitialze pointer----end
                    //if (!(RxExtCANID_Long_1[TxResponseCANID_Index & 0x3FFF] & 0x40000000)) {
                    if (!(RxExtCANID[TxResponseCANID_Index & 0x3FFF] & 0x40000000)) {
                        //Response coming
                        //get data from long frame
                        //ResponseDataLen=TxCAN_RTR_DLC_UseStd & 0x3ff;
                        int16_t i;
                        uint8_t j;
                        if (1 == CAN_Obj->CAN_Peripheral_No) {
#ifdef USE_CAN1
                            for (i = 0; i < ResponseDataLen; i++) {
                                j = i / 7;
                                ResponseData[i] = RxExtData_Long_1[TxResponseCANID_Index & 0x3FFF][j][(i % 7) + 1];
                                //ResponseData[i] = RxData_Long[TxResponseCANID_Index & 0x3FFF][j][(i % 7) + 1];
                            }
#endif
                        } else {
#ifdef USE_CAN2
                            for (i = 0; i < ResponseDataLen; i++) {
                                j = i / 7;
                                ResponseData[i] = RxExtData_Long_2[TxResponseCANID_Index & 0x3FFF][j][(i % 7) + 1];
                                //ResponseData[i] = RxData_Long[TxResponseCANID_Index & 0x3FFF][j][(i % 7) + 1];
                            }
#endif
                        }
                        *state = *state & ~(1UL << Seq);
                        return 0; //success
                    } else {
                        //check time out
                        if (tick - Tick4Timeout[Seq] > TimeOutSettings) {
                            //Time out
                            *state = *state & ~(1UL << Seq);
                            return -1; //fail
                        } else return 1; //continue
                    }
                }

            } else {
                //need short frame response
                if (0x0000 == (TxResponseCANID_Index & 0xc000)) {
                    //short std frame response
                    if (1 == CAN_Obj->CAN_Peripheral_No) {
#ifdef USE_CAN1
                        pRxdDLC = RxStdDLC_1;
                        pRxData = RxStdData_1;
                        RxStdCANID = RxStdCANID_1;
#endif

                    } else {
#ifdef USE_CAN2
                        pRxdDLC = RxStdDLC_2;
                        pRxData = RxStdData_2;
                        RxStdCANID = RxStdCANID_2;
#endif
                    }

                    //if (!(RxStdCANID_1[TxResponseCANID_Index & 0x3FFF] & 0x4000)) {
                    if (!(RxStdCANID[TxResponseCANID_Index & 0x3FFF] & 0x4000)) {
                        //Response coming
                        //get data
                        //if (ResponseDataLen <= (0x7f & RxStdDLC_1[TxResponseCANID_Index & 0x3FFF])) {
                        if (ResponseDataLen <= (0x7f & pRxdDLC[TxResponseCANID_Index & 0x3FFF])) {
                            int8_t i;
                            for (i = 0; i < ResponseDataLen; i++) {
                                //*(ResponseData + i) = RxStdData_1[TxResponseCANID_Index & 0x3FFF][i];
                                *(ResponseData + i) = pRxData[TxResponseCANID_Index & 0x3FFF][i];
                            }
                            *state = *state & ~(1UL << Seq);
                            return 0; //success
                        }
                    } else {
                        //check time out
                        if (tick - Tick4Timeout[Seq] > TimeOutSettings) {
                            //Time out
                            *state = *state & ~(1UL << Seq);
                            return -1; //fail
                        } else return 1; //continue
                    }


                } else if (0x4000 == (TxResponseCANID_Index & 0xc000)) {
                    //short ext frame response
                    if (1 == CAN_Obj->CAN_Peripheral_No) {
#ifdef USE_CAN1
                        pRxdDLC = RxExtDLC_1;
                        pRxData = RxExtData_1;
                        RxExtCANID = RxExtCANID_1;
#endif

                    } else {
#ifdef USE_CAN2
                        pRxdDLC = RxExtDLC_2;
                        pRxData = RxExtData_2;
                        RxExtCANID = RxExtCANID_2;
#endif
                    }

                    //if (!(RxExtCANID_1[TxResponseCANID_Index & 0x3FFF] & 0x40000000)) {
                    if (!(RxExtCANID[TxResponseCANID_Index & 0x3FFF] & 0x40000000)) {
                        //REspons coming
                        //get data
                        //if (ResponseDataLen <= (0x7f & RxExtDLC_1[TxResponseCANID_Index & 0x3FFF])) {
                        if (ResponseDataLen <= (0x7f & pRxdDLC[TxResponseCANID_Index & 0x3FFF])) {
                            if (ResponseDataLen > 8) ResponseDataLen = 8;
                            int8_t i;
                            for (i = 0; i < ResponseDataLen; i++) {
                                //*(ResponseData + i) = RxExtData_1[TxResponseCANID_Index & 0x3FFF][i];
                                *(ResponseData + i) = pRxData[TxResponseCANID_Index & 0x3FFF][i];
                            }
                            *state = *state & ~(1UL << Seq);
                            return 0; //success
                        }
                    } else {
                        //check time out
                        if (tick - Tick4Timeout[Seq] > TimeOutSettings) {
                            //Time out
                            *state = *state & ~(1UL << Seq);
                            return -1; //fail
                        } else return 1; //continue
                    }
                }
            }


        }




    } else {
        //Bus off
        CurrntFrmNo[Seq] = 0; //init it
        return -1;
    }




}
