Can_User.c 11.6 KB
Newer Older
李俭双's avatar
李俭双 committed
1 2 3 4 5 6 7 8
#include "Can_User.h"
#include "Diag_ID_Def.h"
#include "DoCAN_ISO15765.h"
#include <string.h>
#include "can.h"
#include "RTE.h"

__align(4)
9
    uint32_t pRXBuff[CAN_RX_MSG_Block * CAN_CH0_ID_TOTAL_MAX];
李俭双's avatar
李俭双 committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
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();

29
    RTE_GPIO_Set_Level(RTE_GPIO_PORT05_PIN02, RTE_GPIO_LEVEL_LOW);
李俭双's avatar
李俭双 committed
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
}

/**
 * @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);

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    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);
81
    CAN_MessageCache_OverWriteConfig(CAN0MSG07, ENABLE);
82 83 84 85 86 87 88 89 90 91

    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
92 93 94 95 96 97 98 99 100
    CAN_RecvMsg.Id = 0x373;
    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(CAN0MSG15);
    CAN_MessageCache_Init(CAN0MSG15, &CAN_RecvMsg);
    CAN_MessageCache_OverWriteConfig(CAN0MSG15, ENABLE);

李俭双's avatar
李俭双 committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
    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;
128 129 130 131 132 133
    CAN_SendMsg.Id = 0x6EE;
    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;
李俭双's avatar
李俭双 committed
134 135 136 137 138 139 140 141
    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);

142
    CAN_SendMsg.Id = 0x6EF;
143 144 145 146 147
    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;
148 149 150 151 152 153 154 155 156
    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;
157 158 159 160 161
    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;
162 163 164 165 166 167 168 169 170
    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;
171 172 173 174 175
    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;
176 177 178 179 180 181 182 183
    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);

184
    CAN_SendMsg.Id = 0x580;
185 186 187 188 189
    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;
190 191 192 193 194 195 196 197
    for (uint8_t i = 0; i < CAN_SendMsg.DLC; i++)
    {
        CAN_SendMsg.Data[i] = 0;
    }

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

198 199 200 201 202 203
    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;
李俭双's avatar
李俭双 committed
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
    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))
    {
222
        DoCAN_L_Data_Indication(Msg->Id, Msg->DLC, Msg->Data);
李俭双's avatar
李俭双 committed
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
    }
}
/**
 * @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);

239 240 241 242 243 244 245

}


void CAN_TX_Init(void)
{

李俭双's avatar
李俭双 committed
246 247 248
    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);
249
    CAN_TX_SetEnable(&CAN_CH0_CanMsgTxOp, CAN_N_TX_Disable);
250 251 252 253




李俭双's avatar
李俭双 committed
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
void Can_QuickTimer_Init(void)
{
    uint32_t i = 0u;
    for (i = 0u; i < CAN_CH0_ID_SEND_TOTAL; i++)
    {
        Can_Msg_TX_FastInitCycle(&CAN_CH0_CanMsgTxOp, i, CAN_CH0_CANSendAttr[i].u32MsgCycleOffset);
    }
}
李俭双's avatar
李俭双 committed
280 281 282 283 284 285 286 287 288

/**
 * @brief Buff恢复函数
 * @param deltaTime 调用时间 单位ms 10MS调用
 */
void Can_BusOff_Recover(uint8_t deltaTime)
{
    if (get_can_busoff(CAN_CH_0) == 2)
    {
289
        CAN_TX_Count_Init();
李俭双's avatar
李俭双 committed
290 291 292 293 294 295 296 297
        if (RSCAN0Busoff.Status == RSCAN0_BUS_STABLE)
        {
            RSCAN0Busoff.Status = RSCAN0_BUS_OFF_LV1;
        }

        if (RSCAN0Busoff.Status == RSCAN0_BUS_OFF_LV1)
        {
            RSCAN0Busoff.Timer++;
298
            if (RSCAN0Busoff.Timer >= RSCAN0_BUS_OFF_LV1_RECOVERY_TIME / deltaTime)/**/
李俭双's avatar
李俭双 committed
299 300 301 302
            {
                RSCAN0Busoff.Timer = 0U;

                RSCAN0Busoff.Cnt++;
303
                if (RSCAN0Busoff.Cnt >= 10U)/**/
李俭双's avatar
李俭双 committed
304 305 306 307 308 309
                {

                    RSCAN0Busoff.Cnt = 0U;

                    RSCAN0Busoff.Status = RSCAN0_BUS_OFF_LV2;
                }
310 311 312
                //需调用CAN中止函数,中止所有硬件buf内要发送的数据
                CAN_TX_Count_Init();
                Can_QuickTimer_Init();
李俭双's avatar
李俭双 committed
313 314 315 316 317 318 319
                reset_busoff(CAN_CH_0);
            }
        }
        else if (RSCAN0Busoff.Status == RSCAN0_BUS_OFF_LV2)
        {
            RSCAN0Busoff.Timer++;

320
            if (RSCAN0Busoff.Timer >= RSCAN0_BUS_OFF_LV2_RECOVERY_TIME / deltaTime)/**/
李俭双's avatar
李俭双 committed
321 322
            {
                RSCAN0Busoff.Timer = 0U;
323 324 325
                //需调用CAN中止函数,中止所有硬件buf内要发送的数据
                CAN_TX_Count_Init();
                Can_QuickTimer_Init();
李俭双's avatar
李俭双 committed
326 327 328 329 330 331 332 333 334
                reset_busoff(CAN_CH_0);
            }
        }
        else
        {
        }
    }
    else /* 总线正常,没有Bus-off发生*/
    {
335 336 337 338 339 340 341 342 343
        RSCAN0Busoff.RecoverTimer++;
        if (RSCAN0Busoff.RecoverTimer >= 4)
        {
            RSCAN0Busoff.RecoverTimer = 0;

            RSCAN0Busoff.Status = RSCAN0_BUS_STABLE;
            RSCAN0Busoff.Timer = 0;
            RSCAN0Busoff.Cnt = 0;
        }
李俭双's avatar
李俭双 committed
344 345 346 347 348 349 350 351 352 353 354 355 356
    }
}

/**
 * @brief 获取busoff状态
 * @return 0 没有busoff
 *         1 错误的操作
 *         2 busoff
 */
uint8_t Get_Busoff_Status(void)
{
    return get_can_busoff(CAN_CH_0);
}
357 358 359 360 361
//extern uint8_t Can_341_Flg;
//extern uint8_t Can_580Send_flag;
uint8_t CAN_Send_Cound = 0;

extern uint8_t Tpms_TX_Flag;
李俭双's avatar
李俭双 committed
362 363
uint8_t COM_APP_Process(st_CAN_Msg *Msg)
{
364 365 366
    //     Can_Write((st_CAN_Msg *)Msg);
    //     return 0;
    // }
李俭双's avatar
李俭双 committed
367

368 369
    // void Can_Write(st_CAN_Msg *Msg)
    // {
李俭双's avatar
李俭双 committed
370 371
    uint32_t i = 0;
    CanTxRxMsg CAN_SendMsg;
372
    uint8_t CAN_result = 0;
李俭双's avatar
李俭双 committed
373 374 375 376 377 378 379 380 381 382 383 384 385 386

    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)
    {
387
    case 0x6EE:
388
        CAN_result = CAN_Transmit(CAN0MSG08, &CAN_SendMsg);
李俭双's avatar
李俭双 committed
389
        break;
390
    case 0x6EF:
391
        CAN_result = CAN_Transmit(CAN0MSG12, &CAN_SendMsg);
392 393
        break;
    case 0x450:
394

395 396 397 398 399
        // if(Can_341_Flg == 1 || Common_GetIgnOnTime() > 2000)
        {
            CAN_result = CAN_Transmit(CAN0MSG13, &CAN_SendMsg);
        }

400 401
        break;
    case 0x220:
402
        CAN_result = CAN_Transmit(CAN0MSG10, &CAN_SendMsg);
403
        if (AliveCountTimer < 7)
404 405 406 407 408 409 410
        {
            AliveCountTimer++;
        }
        else
        {
            AliveCountTimer = 0;
        }
411
        break; 
李俭双's avatar
李俭双 committed
412
#if (IC_Current == TY200_080000b_ty)
413
    case 0x580:
414 415
        if (Get_Tpms_TX_Flag() != 0)
        {
416 417 418 419 420 421 422 423 424 425 426
            if(CAN_Send_Cound++ <= 2)
            {
                CAN_result = CAN_Transmit(CAN0MSG14, &CAN_SendMsg);
            }
            else
            {
                CAN_Send_Cound = 0;
                Tpms_TX_Flag = 0;
            }
            
            //Can_580Send_flag = 0;
427 428
        }

429
        break;
李俭双's avatar
李俭双 committed
430
#endif
李俭双's avatar
李俭双 committed
431 432 433
    default:
        break;
    }
434
    return (!CAN_result);
李俭双's avatar
李俭双 committed
435
}