Can_User.c 9.04 KB
Newer Older
李俭双's avatar
李俭双 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
#include "Can_User.h"
#include "Diag_ID_Def.h"
#include "DoCAN_ISO15765.h"
#include <string.h>
#include "can.h"
#include "RTE.h"

__align(4)
uint32_t pRXBuff[CAN_RX_MSG_Block * CAN_CH0_ID_TOTAL_MAX];
uint32_t pTXBuff[CAN_TX_MSG_Block * CAN_CH0_ID_SEND_TOTAL];
static RSCAN0BusoffMonitorStruct RSCAN0Busoff;

void Can_Init(void)
{
    can_config_st_t Can_Config;
    Can_Config.ch = CAN_CH_0;
    Can_Config.freq = CAN_500Kbps;
    Can_Config.MASK[0] = 0x001fffffU;
    Can_Config.MASK[1] = 0x000fffffU;
    Can_Config.MASK[2] = 0x1fffffffU;
    Can_Config.MASK[3] = 0x1fffffffU;
    Can_Config.rx_callback = Can_Rx_Cak;
    COM_CAN_Init();
    rte_can_init(&Can_Config);

    Can_Tx_Apply_Buff();
    Can_RX_Apply_Buff();

    RTE_GPIO_Set_Level(RTE_GPIO_PORT05_PIN02,RTE_GPIO_LEVEL_LOW);

}

/**
 * @brief 用于申请RXBUFF CAN0MSG00~CAN0MSG07
 *
 */
void Can_RX_Apply_Buff(void)
{
    CAN_RecvMsg.Id = 0x101;
    CAN_RecvMsg.IDE = CAN_Id_Standard;
    CAN_RecvMsg.CacheType = CAN_CacheType_Rx_NoMask;
    CAN_RecvMsg.RTR = CAN_RTR_Data;
    CAN_RecvMsg.Interrupt = ENABLE;
    CAN_MessageCache_DeInit(CAN0MSG01);
    CAN_MessageCache_Init(CAN0MSG01, &CAN_RecvMsg);
    CAN_MessageCache_OverWriteConfig(CAN0MSG01, ENABLE);

    CAN_RecvMsg.Id = 0x402;
    CAN_RecvMsg.IDE = CAN_Id_Standard;
    CAN_RecvMsg.CacheType = CAN_CacheType_Rx_NoMask;
    CAN_RecvMsg.RTR = CAN_RTR_Data;
    CAN_RecvMsg.Interrupt = ENABLE;
    CAN_MessageCache_DeInit(CAN0MSG02);
    CAN_MessageCache_Init(CAN0MSG02, &CAN_RecvMsg);
    CAN_MessageCache_OverWriteConfig(CAN0MSG02, ENABLE);

    CAN_RecvMsg.Id = 0x401;
    CAN_RecvMsg.IDE = CAN_Id_Standard;
    CAN_RecvMsg.CacheType = CAN_CacheType_Rx_NoMask;
    CAN_RecvMsg.RTR = CAN_RTR_Data;
    CAN_RecvMsg.Interrupt = ENABLE;
    CAN_MessageCache_DeInit(CAN0MSG06);
    CAN_MessageCache_Init(CAN0MSG06, &CAN_RecvMsg);
    CAN_MessageCache_OverWriteConfig(CAN0MSG06, ENABLE);

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
    CAN_RecvMsg.Id = 0x341;
    CAN_RecvMsg.IDE = CAN_Id_Standard;
    CAN_RecvMsg.CacheType = CAN_CacheType_Rx_NoMask;
    CAN_RecvMsg.RTR = CAN_RTR_Data;
    CAN_RecvMsg.Interrupt = ENABLE;
    CAN_MessageCache_DeInit(CAN0MSG05);
    CAN_MessageCache_Init(CAN0MSG05, &CAN_RecvMsg);
    CAN_MessageCache_OverWriteConfig(CAN0MSG05, ENABLE);

    CAN_RecvMsg.Id = 0x111;
    CAN_RecvMsg.IDE = CAN_Id_Standard;
    CAN_RecvMsg.CacheType = CAN_CacheType_Rx_NoMask;
    CAN_RecvMsg.RTR = CAN_RTR_Data;
    CAN_RecvMsg.Interrupt = ENABLE;
    CAN_MessageCache_DeInit(CAN0MSG07);
    CAN_MessageCache_Init(CAN0MSG07, &CAN_RecvMsg);
    CAN_MessageCache_OverWriteConfig(CAN0MSG07, ENABLE);    

    CAN_RecvMsg.Id = 0x120;
    CAN_RecvMsg.IDE = CAN_Id_Standard;
    CAN_RecvMsg.CacheType = CAN_CacheType_Rx_NoMask;
    CAN_RecvMsg.RTR = CAN_RTR_Data;
    CAN_RecvMsg.Interrupt = ENABLE;
    CAN_MessageCache_DeInit(CAN0MSG11);
    CAN_MessageCache_Init(CAN0MSG11, &CAN_RecvMsg);
    CAN_MessageCache_OverWriteConfig(CAN0MSG11, ENABLE);

李俭双's avatar
李俭双 committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
    CAN_RecvMsg.Id = DIAG_ID_Rx_PHY;
    CAN_RecvMsg.IDE = CAN_Id_Standard;
    CAN_RecvMsg.CacheType = CAN_CacheType_Rx_NoMask;
    CAN_RecvMsg.RTR = CAN_RTR_Data;
    CAN_RecvMsg.Interrupt = ENABLE;
    CAN_MessageCache_DeInit(CAN0MSG03);
    CAN_MessageCache_Init(CAN0MSG03, &CAN_RecvMsg);
    CAN_MessageCache_OverWriteConfig(CAN0MSG03, ENABLE);

    CAN_RecvMsg.Id = DIAG_ID_Rx_FUN;
    CAN_RecvMsg.IDE = CAN_Id_Standard;
    CAN_RecvMsg.CacheType = CAN_CacheType_Rx_NoMask;
    CAN_RecvMsg.RTR = CAN_RTR_Data;
    CAN_RecvMsg.Interrupt = ENABLE;
    CAN_MessageCache_DeInit(CAN0MSG04);
    CAN_MessageCache_Init(CAN0MSG04, &CAN_RecvMsg);
    CAN_MessageCache_OverWriteConfig(CAN0MSG04, ENABLE);
}

/**
 * @brief 用于申请TXBUFF
 *
 */
void Can_Tx_Apply_Buff(void)
{
    CanTxRxMsg CAN_SendMsg;
    CanTxRxMsg CAN_SendMsg_Diag_Tx;
120
	CAN_SendMsg.Id = 0x6EE;
李俭双's avatar
李俭双 committed
121 122 123 124 125 126 127 128 129 130 131 132 133
	CAN_SendMsg.IDE = CAN_Id_Standard;
	CAN_SendMsg.CacheType = CAN_CacheType_Tx;
	CAN_SendMsg.RTR = CAN_RTR_Data;
	CAN_RecvMsg.Interrupt = DISABLE;
	CAN_SendMsg.DLC = 8;
    for (uint8_t i = 0; i < CAN_SendMsg.DLC; i++)
    {
        CAN_SendMsg.Data[i] = 0;
    }

    CAN_MessageCache_DeInit(CAN0MSG08);
    CAN_MessageCache_Init(CAN0MSG08, &CAN_SendMsg);

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
    CAN_SendMsg.Id = 0x6EF;
	CAN_SendMsg.IDE = CAN_Id_Standard;
	CAN_SendMsg.CacheType = CAN_CacheType_Tx;
	CAN_SendMsg.RTR = CAN_RTR_Data;
	CAN_RecvMsg.Interrupt = DISABLE;
	CAN_SendMsg.DLC = 8;
    for (uint8_t i = 0; i < CAN_SendMsg.DLC; i++)
    {
        CAN_SendMsg.Data[i] = 0;
    }

    CAN_MessageCache_DeInit(CAN0MSG12);
    CAN_MessageCache_Init(CAN0MSG12, &CAN_SendMsg);

    CAN_SendMsg.Id = 0x450;
	CAN_SendMsg.IDE = CAN_Id_Standard;
	CAN_SendMsg.CacheType = CAN_CacheType_Tx;
	CAN_SendMsg.RTR = CAN_RTR_Data;
	CAN_RecvMsg.Interrupt = DISABLE;
	CAN_SendMsg.DLC = 8;
    for (uint8_t i = 0; i < CAN_SendMsg.DLC; i++)
    {
        CAN_SendMsg.Data[i] = 0;
    }

    CAN_MessageCache_DeInit(CAN0MSG13);
    CAN_MessageCache_Init(CAN0MSG13, &CAN_SendMsg);

    CAN_SendMsg.Id = 0x220;
	CAN_SendMsg.IDE = CAN_Id_Standard;
	CAN_SendMsg.CacheType = CAN_CacheType_Tx;
	CAN_SendMsg.RTR = CAN_RTR_Data;
	CAN_RecvMsg.Interrupt = DISABLE;
	CAN_SendMsg.DLC = 8;
    for (uint8_t i = 0; i < CAN_SendMsg.DLC; i++)
    {
        CAN_SendMsg.Data[i] = 0;
    }

    CAN_MessageCache_DeInit(CAN0MSG10);
    CAN_MessageCache_Init(CAN0MSG10, &CAN_SendMsg);


李俭双's avatar
李俭双 committed
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
    
	CAN_SendMsg_Diag_Tx.Id = DIAG_ID_Tx;
	CAN_SendMsg_Diag_Tx.IDE = CAN_Id_Standard;
	CAN_SendMsg_Diag_Tx.CacheType = CAN_CacheType_Tx;
	CAN_SendMsg_Diag_Tx.RTR = CAN_RTR_Data;
	CAN_SendMsg_Diag_Tx.Interrupt = DISABLE;
	CAN_SendMsg_Diag_Tx.DLC = 8;
    for (uint8_t i = 0; i < CAN_SendMsg_Diag_Tx.DLC; i++)
    {
        CAN_SendMsg_Diag_Tx.Data[i] = 0;
    }

    CAN_MessageCache_DeInit(CAN0MSG09);
    CAN_MessageCache_Init(CAN0MSG09, &CAN_SendMsg_Diag_Tx);
}
/**
 * @brief 用于接收回调函数
 * @code
 */
void Can_Rx_Cak(CanTxRxMsg *Msg)
{
    Co_Can_Buff_Set(&CAN_CH0_CanMsgOp, Co_Can_ConvertSubID_CAN_CH0(Msg->Id), Msg->DLC, Msg->Data);

    if ((Msg->Id == DIAG_ID_Rx_FUN) || (Msg->Id == DIAG_ID_Rx_PHY))
    {
      DoCAN_L_Data_Indication(Msg->Id, Msg->DLC, Msg->Data);
    }
}
/**
 * @brief CAN库初始化TX/RX
 *
 */
void COM_CAN_Init(void)
{
    memset(pRXBuff, 0, sizeof(pRXBuff));
    CAN_CH0_CanMsgOp.CAN_MSG_Analysis = (st_CANMsgStruct *)pRXBuff;
    CAN_CH0_CanMsgOp.pAttrubute = CAN_CH0_CAN_MSG_CONST_ARRAY;
    CAN_CH0_CanMsgOp.Total_Msg = CAN_CH0_ID_TOTAL_MAX;
    CAN_CH0_CanMsgOp.u8CAN_RX_ENABLE = CAN_N_RX_Enable;
    Can_RX_BuffInit(&CAN_CH0_CanMsgOp, CAN_CH0_CAN_MSG_CONST_ARRAY, CAN_CH0_ID_TOTAL_MAX);
    CAN_RX_SetEnable(&CAN_CH0_CanMsgOp, CAN_N_RX_Enable);

    memset(pTXBuff, 0, sizeof(pTXBuff));
    CAN_CH0_CanMsgTxOp.CanMsg = (st_CAN_SendOperation *)pTXBuff;
    Can_TX_BuffInit(&CAN_CH0_CanMsgTxOp, CAN_CH0_CANSendAttr, CAN_CH0_ID_SEND_TOTAL, COM_APP_Process);
    CAN_TX_SetEnable(&CAN_CH0_CanMsgTxOp, CAN_N_TX_Enable);
}


/**
 * @brief Buff恢复函数
 * @param deltaTime 调用时间 单位ms 10MS调用
 */
void Can_BusOff_Recover(uint8_t deltaTime)
{
    if (get_can_busoff(CAN_CH_0) == 2)
    {
        if (RSCAN0Busoff.Status == RSCAN0_BUS_STABLE)
        {
            RSCAN0Busoff.Status = RSCAN0_BUS_OFF_LV1;
        }

        if (RSCAN0Busoff.Status == RSCAN0_BUS_OFF_LV1)
        {
            RSCAN0Busoff.Timer++;
            if (RSCAN0Busoff.Timer >= RSCAN0_BUS_OFF_LV1_RECOVERY_TIME / deltaTime)
            {
                RSCAN0Busoff.Timer = 0U;

                RSCAN0Busoff.Cnt++;
                if (RSCAN0Busoff.Cnt >= 10U)
                {

                    RSCAN0Busoff.Cnt = 0U;

                    RSCAN0Busoff.Status = RSCAN0_BUS_OFF_LV2;
                }
                reset_busoff(CAN_CH_0);
            }
        }
        else if (RSCAN0Busoff.Status == RSCAN0_BUS_OFF_LV2)
        {
            RSCAN0Busoff.Timer++;

            if (RSCAN0Busoff.Timer >= RSCAN0_BUS_OFF_LV2_RECOVERY_TIME / deltaTime)
            {
                RSCAN0Busoff.Timer = 0U;
                reset_busoff(CAN_CH_0);
            }
        }
        else
        {
        }
    }
    else /* 总线正常,没有Bus-off发生*/
    {
        
    }
}

/**
 * @brief 获取busoff状态
 * @return 0 没有busoff
 *         1 错误的操作
 *         2 busoff
 */
uint8_t Get_Busoff_Status(void)
{
    return get_can_busoff(CAN_CH_0);
}

uint8_t COM_APP_Process(st_CAN_Msg *Msg)
{
    Can_Write((st_CAN_Msg *)Msg);
    return 0;
}

void Can_Write(st_CAN_Msg *Msg)
{
    uint32_t i = 0;
    CanTxRxMsg CAN_SendMsg;

    CAN_SendMsg.Id = Msg->MsgID;       /* ID */
    CAN_SendMsg.DLC = Msg->MsgDLC;     /* Len */
    CAN_SendMsg.RTR = Msg->MsgRTR;     /* Default 0 */
    CAN_SendMsg.IDE = CAN_Id_Standard; /* Stand 0 Ext 1 */
    CAN_SendMsg.CacheType = CAN_CacheType_Tx;

    for (i = 0; i < CAN_SendMsg.DLC; i++) /* 获取数据 */
    {
        CAN_SendMsg.Data[i] = Msg->Msg[i];
    }

    switch (CAN_SendMsg.Id)
    {
312
    case 0x6EE:
李俭双's avatar
李俭双 committed
313 314
        CAN_Transmit(CAN0MSG08, &CAN_SendMsg);
        break;
315 316 317 318 319 320 321 322 323
    case 0x6EF:
        CAN_Transmit(CAN0MSG12, &CAN_SendMsg);
        break;
    case 0x450:
        CAN_Transmit(CAN0MSG13, &CAN_SendMsg);
        break;
    case 0x220:
        CAN_Transmit(CAN0MSG10, &CAN_SendMsg);
        break;
李俭双's avatar
李俭双 committed
324 325 326 327 328

    default:
        break;
    }
}