
//#include "UDS_Common.h"
#include "CAN_CH0_CAN_Communication_Matrix.h"
//#include "include.h"
//#include "UDS_Common.h"
//#include "IC_Configuration.h"

//#include "RTE_CAN.h"
//#include "TYW_stdint.h"
#include <string.h>
//#include <EOL_Para.h>
//#include <DataProcess_Common.h>
//#include <CAN_Signal_Select.h>
#include "g_includes.h"


#pragma alignvar(8)
static uint32_t pRXBuff[CAN_RX_MSG_Block * CAN_CH0_ID_TOTAL_MAX];
//#pragma alignvar(8)
//static uint32_t pTXBuff[CAN_TX_MSG_Block * CAN_CH0_ID_SEND_TOTAL];

uint32_t pRXBuff_CH0[CAN_RX_MSG_Block * CAN_CH0_ID_TOTAL_MAX];


uint8_t COM_APP_Process(st_CAN_Msg *Msg);

void COM_CAN_Init(void)
{
    memset(pRXBuff, 0, sizeof(pRXBuff));
    CAN_CH0_CanMsgOp.CAN_MSG_Analysis = ( st_CANMsgStruct * )pRXBuff;
    //CAN_CH0_CanMsgTxOp.CanMsg         = pTXBuff;
    Can_RX_BuffInit(&CAN_CH0_CanMsgOp, CAN_CH0_CAN_MSG_CONST_ARRAY, CAN_CH0_ID_TOTAL_MAX);
    //Can_TX_BuffInit(&CAN_CH0_CanMsgTxOp, CAN_CH0_CANSendAttr, CAN_CH0_ID_SEND_TOTAL, Can_Write);
    //CAN_TX_SetEnable(&CAN_CH0_CanMsgTxOp, CAN_N_TX_Disable);
    CAN_RX_SetEnable(&CAN_CH0_CanMsgOp, CAN_N_RX_Enable);


}

void COM_RX_Process(void)
{
    CanMSg_XMS_Analysis(&CAN_CH0_CanMsgOp, 5u);
}

uint8_t BusOffEvent;
/**
  *获取busoff状态
  *@return busoff状态 0正常,1异常
  */
uint8_t getBUS_OFF_Status(void)
{
    return BusOffEvent;
}
typedef enum
{
    BUSSOFF_CH_CH0,
    BUSSOFF_CH_SUM,
}BUSSSOFF_CH_enum;

static RSCAN0BusoffMonitorStruct     RSCAN0Busoff[BUSSOFF_CH_SUM];

void Can_CH0_BusOffRecover(void);
void Can_CH0_BusOff_Fun(void);


void Can_CH0_BusOff_Fun(void)
{
    if ((RSCAN0_CH0_Get_Busoff_Status() != 0U))
    {
        RSCAN0_CH0_Abort(0);
    
        if (RSCAN0Busoff[BUSSOFF_CH_CH0].Status == RSCAN0_BUS_STABLE)
        {
            RSCAN0Busoff[BUSSOFF_CH_CH0].Status = RSCAN0_BUS_OFF_LV1;
        }

        if (RSCAN0Busoff[BUSSOFF_CH_CH0].Status == RSCAN0_BUS_OFF_LV1)
        {
            RSCAN0Busoff[BUSSOFF_CH_CH0].Timer++;
            if (RSCAN0Busoff[BUSSOFF_CH_CH0].Timer >= (RSCAN0_BUS_OFF_LV1_RECOVERY_TIME / 10U))
            {
                RSCAN0Busoff[BUSSOFF_CH_CH0].Timer = 0U;

                RSCAN0Busoff[BUSSOFF_CH_CH0].Cnt++;
                if (RSCAN0Busoff[BUSSOFF_CH_CH0].Cnt >= 10U)
                {
               
                    RSCAN0Busoff[BUSSOFF_CH_CH0].Cnt = 0U;

                    RSCAN0Busoff[BUSSOFF_CH_CH0].Status = RSCAN0_BUS_OFF_LV2;
                }
                RSCAN0_CH0_Busoff_Recover( );
            }
        }
        else if (RSCAN0Busoff[BUSSOFF_CH_CH0].Status == RSCAN0_BUS_OFF_LV2)
        {
            RSCAN0Busoff[BUSSOFF_CH_CH0].Timer++;

            if (RSCAN0Busoff[BUSSOFF_CH_CH0].Timer >= (RSCAN0_BUS_OFF_LV2_RECOVERY_TIME / 10U))
            {
                RSCAN0Busoff[BUSSOFF_CH_CH0].Timer = 0U;
                RSCAN0_CH0_Busoff_Recover( );
            }
        }
        else
        {

        }
    }
    else                                          /* 总线正常,没有Bus-off发生/**/
    {
        Can_CH0_BusOffRecover();
    }
}

void Can_BusOff_Fun(void)
{
    Can_CH0_BusOff_Fun();
}

void Can_BusOffRecover(void)
{   
    Can_CH0_BusOffRecover();
}

void Can_CH0_BusOffRecover(void)
{
    RSCAN0Busoff[BUSSOFF_CH_CH0].Cnt = 0u;
    RSCAN0Busoff[BUSSOFF_CH_CH0].Status = 0u;
    RSCAN0Busoff[BUSSOFF_CH_CH0].Timer = 0u;
}