/* 
 * File:   CANBUS.h
 * Author: chang
 *
 * Created on November 5, 2024, 7:56 AM
 */

#ifndef CANBUS_H
#define	CANBUS_H

#ifdef	__cplusplus
extern "C" {
#endif

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

#ifndef CAN_O_IF1CMD
#define CAN_O_IF1CMD   0x100U
#endif
#ifndef CAN_O_IF1NO
#define CAN_O_IF1NO    (CAN_O_IF1CMD + 0x04U)
#endif
#ifndef CAN_O_IF1MCTL
#define CAN_O_IF1MCTL  0x10CU
#endif


#ifndef CAN_O_IF2CMD
#define CAN_O_IF2CMD   0x120U
#endif
#ifndef CAN_O_IF2NO
#define CAN_O_IF2NO    (CAN_O_IF2CMD + 0x04U)  // IF2 Message Object Number Register
#endif
#ifndef CAN_O_IF2MCTL
#define CAN_O_IF2MCTL  0x12CU
#endif



/* void CAN_getMessageObject(uint32_t base,
                          uint32_t objNo,
                          uint32_t *CANID,
                          CAN_MsgFrameType *frame,
                          CAN_MsgObjType *msgType,
                          uint32_t *msgIDMask,
                          uint32_t *flags,
                          uint16_t *msgLen); */


typedef struct {
    uint8_t CAN_Peripheral_No;  //1 for CAN1 or 2 for CAN2
	volatile bool RxCANReceived;		//Receive CAN Packet
    uint32_t * pLogicRuleA; //Transmit logic rule point for Channel A
    uint32_t * pLogicRuleB; //Transmit logic rule point for Channel B
    uint32_t * pLogicRuleC; //Transmit logic rule point for Channel C
	bool EnableSyn;   //enable Rx Sync
    bool SynUseStdCANID; //Sync is 11 bit standard frame
    //we change to "uint32_t SyncCANID" to replace "uint16_t SyncCANID_Index" because Sync CAN ID may not consist in RxStdCANID_1/2 RxExtCANID_1/2
    //uint16_t SyncCANID_Index;    //It is index in "RxStdCANID_1[] (for CAN1) or RxStdCANID_2[] (for CAN2)"  if std CAN ID Sync
                                 // or index in "RxExtCANID_1[] (for CAN1) or RxExtCANID_2[] (for CAN2)"  if Ext CAN ID Sync
    uint32_t SyncCANID;   //BIT31 is arrived flag.  When receive SyncID, we set Bit31=1.  And after we transmit all channels, we clear Bit31
	uint8_t MaxSeqQTY4ChannelA;	//Maximum Seq QTY for transmit Channel A
    uint8_t MaxSeqQTY4ChannelB;	//Maximum Seq QTY for transmit Channel B
    uint8_t MaxSeqQTY4ChannelC;	//Maximum Seq QTY for transmit Channel C
    
    //uint8_t RxStdFrameQTY4short;    //Rx std frame Max QTY for short frame
    //uint8_t RxExtFrameQTY4short;    //Rx Ext frame Max QTY for short frame
    
    //uint8_t RxStdFrameQTY4long;    //Rx std frame Max QTY for long frame    
    //uint8_t RxExtFrameQTY4long;    //Rx Ext frame Max QTY for long frame   
    
    bool FrameNoIs0_based;
    
    uint16_t COMFaultDetectTimeInms;
    uint16_t COMFaultCounter;   //RxWatchdog for CAN 
    bool CANBusCOMFault;  //got CAN bus Com fault according to RxWatchdog
    bool CANInitConfigFail;
    uint32_t TxChannelA_Success;   //bit0: 1--Seq 0 success,  bit30: 1--Seq 30 success,
    uint32_t TxChannelB_Success;   //bit0: 1--Seq 0 success,  bit30: 1--Seq 30 success,
    uint32_t TxChannelC_Success;   //bit0: 1--Seq 0 success,  bit30: 1--Seq 30 success,
    
    
}CANBUS_t;    
    
  
//#Define Start from Simulink
#define USE_CAN1                       1

extern CANBUS_t CAN_myObj1;

#ifndef USE_CANFD

extern CAN_HandleTypeDef hcan1;

#else

extern FDCAN_HandleTypeDef hfdcan1;

#endif

#define CAN1_RX_MAX_STD_FRAME_QTY4LONG 8
#define CAN1_RX_MAX_EXT_FRAME_QTY4LONG 8
#define MAX_SEQQTY4A_1                 31                        //CAN1 TRansmit A channel, Max QTY of seq
#define MAX_SEQQTY4B_1                 0                         //CAN1 TRansmit B channel, Max QTY of seq
#define MAX_SEQQTY4C_1                 0                         //CAN1 TRansmit C channel, Max QTY of seq
#define USE_EXT_CANID_SYNC_1           1
#define CANID_SYNC_1                   67
#define CAN_FAULT_DETECTION_TIME_1     150
#define MASK0USE_STD_FRAME_1           1
#define MASK0_1                        -1
#define MASK1USE_STD_FRAME_1           1
#define MASK1_1                        -1
#define MASK2USE_STD_FRAME_1           1
#define MASK2_1                        -1

//#Define end from Simulink

    


 
#if (MAX_SEQQTY4B_1>0)
    #define USE_CAN1_CHANNELB 
#endif
#if (MAX_SEQQTY4C_1>0)
    #define USE_CAN1_CHANNELC 
#endif
#ifdef USE_CAN2
#if (MAX_SEQQTY4A_2>0) 
    #define USE_CAN2_CHANNELA 
#endif
#if (MAX_SEQQTY4B_2>0)
    #define USE_CAN2_CHANNELB 
#endif
#if (MAX_SEQQTY4C_2>0)
    #define USE_CAN2_CHANNELC 
#endif

#endif






void myCAN_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);
void CANBus_Poll(CANBUS_t * pCANMyOBJ);

#ifdef USE_CAN1
/**
 * For CAN1, Put one Rx Standard frame  into our high level buffer such as : RxStdData_1[],RxStdData_Long_1[][][]
 * @param CANID
 * @param RTR
 * @param DLC
 * @param pCAN_Data
 */
void putCAN1RxStdCAN2Buffer(uint16_t CANID, bool RTR, uint8_t DLC, uint8_t * pCAN_Data);
/**
 * For CAN1, Put one Rx Ext frame  into our high level buffer such as : RxExtData_1[],RxExtData_Long_1[][][]
 * @param CANID
 * @param RTR
 * @param DLC
 * @param pCAN_Data
 */
void putCAN1RxExtCAN2Buffer(uint32_t CANID, bool RTR, uint8_t DLC, uint8_t * pCAN_Data);
#endif

#ifdef USE_CAN2
/**
 * For CAN2, Put one Rx Standard frame  into our high level buffer such as : RxStdData_2[],RxStdData_Long_2[][][]
 * @param CANID
 * @param RTR
 * @param DLC
 * @param pCAN_Data
 */
void putCAN2RxStdCAN2Buffer(uint16_t CANID, bool RTR, uint8_t DLC, uint8_t * pCAN_Data);
/**
 * For CAN2, Put one Rx Ext frame  into our high level buffer such as : RxExtData_2[],RxExtData_Long_2[][][]
 * @param CANID
 * @param RTR
 * @param DLC
 * @param pCAN_Data
 */
void putCAN2RxExtCAN2Buffer(uint32_t CANID, bool RTR, uint8_t DLC, uint8_t * pCAN_Data);
#endif
#ifdef	__cplusplus
}
#endif

#endif	/* CANBUS_H */












































































































































