UDS_Common.c 26.7 KB
Newer Older
hu's avatar
hu committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#include "UDS_Common.h"

uint8_t  SessionType = DefaultSession;

uint16_t S3_ServerCnt = S3_SERVER;
uint8_t  S3_ServerClear = 0u;
uint8_t  S3_ServerTimeOut = 1u;

uint8_t  UDSKL15Status = 0u;
uint8_t  UDSSleepStatus = 0u;

uint16_t UDSTimer10ms = 0u;

uint8_t  S31DIDC004RequestStatus = 0u;
uint8_t  S31DIDC005RequestStatus = 0u;
uint8_t  S31DIDC014RequestStatus = 0u;

uint8_t  isDTCOpen = 1u;
uint8_t  isReceive = 1u;
uint8_t  isEnable = 0u;
uint16_t enableTimer = 0u;
uint16_t enableMaxTimer = KL15DTCEnableTimer;

hu's avatar
hu committed
24 25 26 27 28 29 30
const uint8_t Ser22_DID_F187[13] = {0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20};                       /*IC 零件号*/
const uint8_t Ser22_DID_F18A[8] =  {0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20};                                                     /*系统供应商编号*/
const uint8_t Ser22_DID_F18B[4] =  {0x20, 0x21, 0x08, 0x03};                                                                             /*IC 生产日期*/
const uint8_t Ser22_DID_F18C[20] = {0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20};
/*IC 系列号*/
const uint8_t Ser22_DID_F190[17] = {0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20};
/*车辆标识号 VIN*/
hu's avatar
hu committed
31 32
const uint8_t Ser22_DID_F193[6] =  {'V', '1', '.', '2', '.', '0'};                                          /*IC 硬件版本号        SystemSupplierECUHardwareVersionNumberApp*/
const uint8_t Ser22_DID_F195[6] =  {'V', '1', '.', '1', '.', '0'};                                          /*IC 逻辑程序软件版本号 SystemSupplierECUSoftwareInformationApp*/
hu's avatar
hu committed
33

hu's avatar
hu committed
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 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 93 94 95 96 97 98 99 100 101 102 103 104
DiagMSGUnion DiagMSG;
DiagFlagUnion DiagFlag;
Ser27_FlowCtrlUnion Ser27_FlowCtrl;
Ser27_FlowCtrlCntUnion Ser27_FlowCtrlCnt;
BootToDFlashInfoUnion BootToDFlashInfo;
Ser2EToDFlashInfoUnion Ser2EToDFlashInfo;
DTCToDFlashInfoUnion DTCToDFlashInfo;

/**
  *设置会话类型
  *@param status 会话类型
  */
void setSessionType(uint8_t status)
{
    if ((status == DefaultSession) || (status == ProgrammingSession) || (status == ExtendedDiagnosticSession))
    {
        SessionType = status;
    }
    else
    {
        SessionType = DefaultSession;
    }
}

/**
  *获取会话类型
  *@return 当前会话类型
  */
uint8_t getSessionType(void)
{
    return SessionType;
}

/**
  *设置诊断信息
  *@param Identifier 收到的诊断报文ID
  *@param dlc 收到的诊断报文长度
  *@param Data 收到的诊断报文数据
  */
void setDiagMSG(uint32_t Identifier, uint16_t dlc, uint8_t Data[])
{
    uint16_t i;
    DiagMSG.msgID = Identifier;
    DiagMSG.msgDLC = dlc;
    for (i = 0u; i < dlc; ++i)
    {
        DiagMSG.msgData.Data[i] = Data[i];
    }
}

/**
  *清除诊断信息
  */
void clearDiagMSG(void)
{
    uint16_t i;
    DiagMSG.msgID = 0u;
    DiagMSG.msgDLC = 0u;
    for (i = 0u; i < 500u; ++i)
    {
        DiagMSG.msgData.Data[i] = 0u;
    }
}

/**
  *获取当前车速
  *@return 返回当前车速
  */
uint16_t getSpeed(void)
{
    uint16_t VSpeed = Common_Get_Act_V_Speed();
hu's avatar
hu committed
105
    if (VSpeed == 0xFFFFu)
hu's avatar
hu committed
106 107 108 109 110 111 112 113 114 115 116 117 118
    {
        VSpeed = 0u;
    }
    return (VSpeed / 10u);
}

/**
  *获取当前转速
  *@return 返回当前转速
  */
uint16_t getESpeed(void)
{
    uint16_t ESpeed = Common_Get_Act_E_Speed();
hu's avatar
hu committed
119
    if (ESpeed == 0xFFFFu)
hu's avatar
hu committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
    {
        ESpeed = 0u;
    }
    return ESpeed;
}

/**
  *获取当前30电压
  *@return 返回当前30电压
  */
uint32_t getKL30Voltage(void)
{
    uint32_t res;
    res = RTE_Read_KL30_Voltage();
    return res;
}

/**
  *获取15电状态
  *@return 15点状态 0为IGN OFF,1为IGN ON
  */
uint8_t getKL15Status(void)
{
hu's avatar
hu committed
143

hu's avatar
hu committed
144 145 146 147 148 149 150 151 152
    return System_LINE_KL15();
}

/**
  *获取当前安全级别
  *@return 返回当前安全级别
  */
uint8_t getSecurityLevel(uint8_t SecurityLevel)
{
hu's avatar
hu committed
153 154
    if (SecurityLevel == requestSeed_LV1)
    {
hu's avatar
hu committed
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
        return Ser27_FlowCtrl.LV1SafetyStatus;
    }
    return Ser27_FlowCtrl.LV3SafetyStatus;
}

/**
  *重置安全等级
  */
void Ser27_ChangeSessionHandle(void)
{
    Ser27_FlowCtrl.LV1Ctrl = SER27_CLEAR_REQ_SEEDED;
    Ser27_FlowCtrl.LV1Key[0]  = 0x00u;
    Ser27_FlowCtrl.LV1Key[1]  = 0x00u;
    Ser27_FlowCtrl.LV1SafetyStatus = NormalKeyLock;
    Ser27_FlowCtrl.LV1Seed[0] = 0x00u;
    Ser27_FlowCtrl.LV1Seed[1] = 0x00u;
    Ser27_FlowCtrl.LV3Ctrl = SER27_CLEAR_REQ_SEEDED;
    Ser27_FlowCtrl.LV3Key[0]  = 0x00u;
    Ser27_FlowCtrl.LV3Key[1]  = 0x00u;
    Ser27_FlowCtrl.LV3SafetyStatus = NormalKeyLock;
    Ser27_FlowCtrl.LV3Seed[0] = 0x00u;
    Ser27_FlowCtrl.LV3Seed[1] = 0x00u;
}

/**
hu's avatar
hu committed
180 181
 *上电/复位 1s不可请求种子
 */
hu's avatar
hu committed
182
void Ser27_IgOnDelayTimerService(uint16_t timer)
hu's avatar
hu committed
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
{
    if (!Ser27_FlowCtrl.AllowRequestSeedFlag)
    {
        Ser27_FlowCtrl.Counter_1000ms += timer;
        if (Ser27_FlowCtrl.Counter_1000ms >= 1000u)
        {
            Ser27_FlowCtrl.AllowRequestSeedFlag = 1u;
            Ser27_FlowCtrl.Counter_1000ms = 0;
        }
    }

}

/**
 * 获取是否允许请求种子
 */
hu's avatar
hu committed
199 200 201 202
uint8_t GetRequestSeedFlag(void)
{
    return Ser27_FlowCtrl.AllowRequestSeedFlag;
}
hu's avatar
hu committed
203

hu's avatar
hu committed
204 205 206 207 208 209 210

/**
  *复位函数
  */
void UDS_RESET(void)
{
    ControlMessageCommunication(networkManagementMsgAndnormalMsg, disableRxAndTx);
hu's avatar
hu committed
211 212
    UDS_Delay(6000u);
    hardware_reset();
hu's avatar
hu committed
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
}

/**
  *诊断延时
  *@param count 延时次数
  */
void UDS_Delay(uint16_t count)
{
    uint16_t tmpcnt;
    tmpcnt = count;
    WDT_Clear();
    while (tmpcnt--)
    {
        nop();
    }
    WDT_Clear();
}

/**
  *发送诊断11服务正反馈
  */
void UDS_S11_Sand(uint8_t subid)
{
    _CAN_Msg canMag;
    canMag.MsgID = DIAG_ID_Tx;
    canMag.Msg[0] = 0x02u;
    canMag.Msg[1] = 0x51u;
    canMag.Msg[2] = subid;
    canMag.Msg[3] = FILLER_BYTE;
    canMag.Msg[4] = FILLER_BYTE;
    canMag.Msg[5] = FILLER_BYTE;
    canMag.Msg[6] = FILLER_BYTE;
    canMag.Msg[7] = FILLER_BYTE;
    canMag.MsgDLC = 8u;
hu's avatar
hu committed
247
    canMag.MsgPro = 9u;
hu's avatar
hu committed
248
    canMag.MsgStd = 0u;
hu's avatar
hu committed
249
    Can_Write(&canMag);
hu's avatar
hu committed
250
    UDS_S11_Confirm();
hu's avatar
hu committed
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
}

/**
  *诊断11服务正反馈确认
  */
void UDS_S11_Confirm(void)
{
    UDS_RESET();
}

/**
  *发送诊断78负反馈
  */
void UDS_NRC78_Sand(uint8_t sid)
{
    _CAN_Msg canMag;
    canMag.MsgID = DIAG_ID_Tx;
    canMag.Msg[0] = 0x03u;
    canMag.Msg[1] = 0x7Fu;
    canMag.Msg[2] = sid;
    canMag.Msg[3] = 0x78u;
    canMag.Msg[4] = FILLER_BYTE;
    canMag.Msg[5] = FILLER_BYTE;
    canMag.Msg[6] = FILLER_BYTE;
    canMag.Msg[7] = FILLER_BYTE;
    canMag.MsgDLC = 8u;
hu's avatar
hu committed
277
    canMag.MsgPro = 9u;
hu's avatar
hu committed
278
    canMag.MsgStd = 0u;
hu's avatar
hu committed
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
    Can_Write(&canMag);
}

/**
  *发送诊断78负反馈
  */
void UDS_14_SendRsp(uint8_t sid)
{
    _CAN_Msg canMag;
    canMag.MsgID = DIAG_ID_Tx;
    canMag.Msg[0] = 0x01u;
    canMag.Msg[1] = 0x54u;
    canMag.Msg[2] = FILLER_BYTE;
    canMag.Msg[3] = FILLER_BYTE;
    canMag.Msg[4] = FILLER_BYTE;
    canMag.Msg[5] = FILLER_BYTE;
    canMag.Msg[6] = FILLER_BYTE;
    canMag.Msg[7] = FILLER_BYTE;
    canMag.MsgDLC = 8u;
hu's avatar
hu committed
298
    canMag.MsgPro = 9u;
hu's avatar
hu committed
299
    canMag.MsgStd = 0u;
hu's avatar
hu committed
300 301 302 303 304 305
    Can_Write(&canMag);
}

/**
  *发送负反馈
  */
hu's avatar
hu committed
306
void UDS_SendNRCRsp(uint8_t sid, uint8_t status)
hu's avatar
hu committed
307 308 309 310 311 312 313 314 315 316 317 318
{
    _CAN_Msg canMag;
    canMag.MsgID = DIAG_ID_Tx;
    canMag.Msg[0] = 0x03u;
    canMag.Msg[1] = 0x7fu;
    canMag.Msg[2] = sid;
    canMag.Msg[3] = status;
    canMag.Msg[4] = FILLER_BYTE;
    canMag.Msg[5] = FILLER_BYTE;
    canMag.Msg[6] = FILLER_BYTE;
    canMag.Msg[7] = FILLER_BYTE;
    canMag.MsgDLC = 8u;
hu's avatar
hu committed
319
    canMag.MsgPro = 9u;
hu's avatar
hu committed
320
    canMag.MsgStd = 0u;
hu's avatar
hu committed
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
    Can_Write(&canMag);
}

/**
  *诊断50us任务
  */
void UDS_50us_Service(void)
{
    //if(UDSSleepStatus == 1u)
    {
        ++UDSTimer10ms;
    }
}

/**
  *诊断10ms任务
  */
void UDS_10ms_Service(void)
{
    uint16_t count;
hu's avatar
hu committed
341
    if (UDSTimer10ms >= 200u)
hu's avatar
hu committed
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
    {
        count = UDSTimer10ms / 200u;
        UDSTimer10ms %= 200u;
        enableDTCTimer(count * 10u);
        MsgLostTimer(count * 10u);
        TestDTC(count * 10u);
        S3_Service(count * 10u);
        Ser27_ErrDelayTimerService(count * 10u);
        Ser27_IgOnDelayTimerService(count * 10u);
    }
}

/**
  *诊断KL15init
  */
void UDS_KL15_init(void)
{
    uint8_t KL15Status;
    KL15Status = getKL15Status();
hu's avatar
hu committed
361
    if (UDSKL15Status == 0u)
hu's avatar
hu committed
362
    {
hu's avatar
hu committed
363
        if ((KL15Status == 1u))
hu's avatar
hu committed
364 365 366 367 368 369 370 371 372 373
        {
            UDSKL15Status = 1u;
            UDSSleepStatus = 1u;
            setDTCEnableTimer(KL15DTCEnableTimer);
            S3_ServiceTimeOut();
            DTCAgeing();
        }
    }
    else
    {
hu's avatar
hu committed
374
        if (KL15Status == 0u)
hu's avatar
hu committed
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
        {
            UDSKL15Status = 0u;
            setDTCEnableTimer(KL15DTCEnableTimer);
        }
    }
}

void UDS_Slepp(void)
{
    UDSSleepStatus = 0u;
    UDSTimer10ms = 0u;
}

/**
  *报文掉线计时器
  *@param timer 计时器增加的时间,单位:ms
  */
void MsgLostTimer(uint16_t timer)
{
    uint8_t i;
    uint8_t BUS_OFF_Status = getBusoffStatus();
hu's avatar
hu committed
396
    if ((isEnable == 1u) && (isReceive == 1u) && (UDSKL15Status == 1u) && (BUS_OFF_Status == 0u) && (HVoltageStatus == 0u) && (LVoltageStatus == 0u))
hu's avatar
hu committed
397
    {
hu's avatar
hu committed
398
        for (i = 0u; i < (sizeof(msgList) / sizeof(msgList[0u])); ++i)
hu's avatar
hu committed
399
        {
hu's avatar
hu committed
400 401

            if (msgList[i][3u] < msgList[i][2u] * OfflineLoopMultiple)
hu's avatar
hu committed
402 403 404
            {
                msgList[i][3u] += timer;
            }
hu's avatar
hu committed
405

hu's avatar
hu committed
406 407 408 409 410 411 412 413 414 415 416 417
        }
    }
}

/**
  *收到报文清除计时器
  *@param MsgID 报文ID
  *@param DLC   报文长度
  */
void ReceivedMsg(uint32_t MsgID, uint8_t DLC)
{
    uint8_t i;
hu's avatar
hu committed
418
    if (isReceive == 1u)
hu's avatar
hu committed
419
    {
hu's avatar
hu committed
420
        for (i = 0u; i < (sizeof(msgList) / sizeof(msgList[0u])); ++i)
hu's avatar
hu committed
421
        {
hu's avatar
hu committed
422
            if ((MsgID == msgList[i][0u]) && (DLC == msgList[i][1u]))
hu's avatar
hu committed
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
            {
                msgList[i][3u] = 0u;
                break;
            }
        }
    }
}

/**
  *S3服务
  */
void S3_Service(uint16_t timer)
{
    if (S3_ServerClear == 1u)
    {
        S3_ServerClear = 0u;
        S3_ServerCnt = 0u;
        S3_ServerTimeOut = 0u;
    }
    else
    {
        if (S3_ServerCnt < S3_SERVER)
        {
            S3_ServerCnt += timer;
        }
        else
        {
            if (S3_ServerTimeOut == 0u)
            {
                S3_ServerTimeOut = 1u;
                S3_ServiceTimeOut();
            }
        }
    }
}

/**
  *S3重置服务
  */
void S3_ServiceClear(void)
{
    S3_ServerClear = 1u;
}

/**
  *S3等待服务
  */
void S3_ServiceWait(void)
{
    S3_ServerClear = 0u;
    S3_ServerCnt = S3_SERVER;
    S3_ServerTimeOut = 1u;
}

/**
  *S3超时服务
  */
void S3_ServiceTimeOut(void)
{
    /*10服务重置*/
    setSessionType(DefaultSession);
    /*27服务重置*/
    Ser27_ChangeSessionHandle();
    /*28服务重置*/
    ControlMessageCommunication(networkManagementMsgAndnormalMsg, enableRxAndTx);
    /*2F服务重置*/

    /*85服务重置*/
    OpenDTCTest();
    /*诊断发送重置*/
    clearDiagMSG();
hu's avatar
hu committed
494 495
    /*2F/31关指示灯*/
    IndicatorTurnOn(2u);
hu's avatar
hu committed
496 497 498 499 500 501 502 503 504 505 506 507 508
}

/**
  *27服务10s延时
  */
void Ser27_ErrDelayTimerService(uint16_t timer)
{
    if ( ( Ser27_FlowCtrl.LV1DelayCtrl == SER27_ERR_DELAY_TIMER_EN ) )
    {
        if (Ser27_FlowCtrl.LV1DelayTimer < 0xfff0u )
        {
            Ser27_FlowCtrl.LV1DelayTimer += timer;
            if (Ser27_FlowCtrl.LV1DelayTimer >= SER27_ERR_DELAY_TIMEOUT)
hu's avatar
hu committed
509
            {
hu's avatar
hu committed
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
                Ser27_FlowCtrlCnt.LV1ReqSeedCnt  = SER27_MAX_REQ_SEED_NUM - 1u;
                Ser27_FlowCtrl.LV1Ctrl = SER27_CLEAR_REQ_SEEDED;
                Ser27_FlowCtrl.LV1DelayTimer = 0u;
                Ser27_FlowCtrl.LV1DelayCtrl = 0u;
                /*调用向DFlash写入函数。*/
                WriteDFlashData(0x02u, (uint32_t *)&Ser27_FlowCtrlCnt.Flag, sizeof(Ser27_FlowCtrlCnt) / 4u, NoNeedWait);
            }
        }
    }
    if ( ( Ser27_FlowCtrl.LV3DelayCtrl == SER27_ERR_DELAY_TIMER_EN ) )
    {
        if (Ser27_FlowCtrl.LV3DelayTimer < 0xfff0u )
        {
            Ser27_FlowCtrl.LV3DelayTimer += timer;
            if (Ser27_FlowCtrl.LV3DelayTimer >= SER27_ERR_DELAY_TIMEOUT)
            {
                Ser27_FlowCtrlCnt.LV3ReqSeedCnt  = SER27_MAX_REQ_SEED_NUM - 1u;
                Ser27_FlowCtrl.LV3Ctrl = SER27_CLEAR_REQ_SEEDED;
                Ser27_FlowCtrl.LV3DelayTimer = 0u;
                Ser27_FlowCtrl.LV3DelayCtrl = 0u;
                /*调用向DFlash写入函数。*/
                WriteDFlashData(0x02u, (uint32_t *)&Ser27_FlowCtrlCnt.Flag, sizeof(Ser27_FlowCtrlCnt) / 4u, NoNeedWait);
            }
        }
    }
}

/**
  *控制报文通信
  *@param Type 控制报文的类型
  *@param Status 控制的标志位
  */
void ControlMessageCommunication(uint8_t Type, uint8_t Status)
{
    if ((uint8_t)(Type & normalMsg) == normalMsg)
    {
        switch (Status)
        {
        case enableRxAndTx:
            RTE_CAN_SetAPP_TX_Mode(CAN_APP_TX_ENABLE);
hu's avatar
hu committed
550 551 552
            //RTE_CAN_SetAPP_RX_Mode(CAN_APP_RX_ENABLE);
            isReceive = 1;
            break;
hu's avatar
hu committed
553 554
        case enableRxAndDisableTx:
            RTE_CAN_SetAPP_TX_Mode(CAN_APP_TX_DISABLE);
hu's avatar
hu committed
555 556 557
            //RTE_CAN_SetAPP_RX_Mode(CAN_APP_RX_ENABLE);
            isReceive = 1;
            break;
hu's avatar
hu committed
558 559
        case disableRxAndEnableTx:
            RTE_CAN_SetAPP_TX_Mode(CAN_APP_TX_ENABLE);
hu's avatar
hu committed
560 561 562
            //RTE_CAN_SetAPP_RX_Mode(CAN_APP_RX_DISABLE);
            isReceive = 0;
            break;
hu's avatar
hu committed
563 564
        case disableRxAndTx:
            RTE_CAN_SetAPP_TX_Mode(CAN_APP_TX_DISABLE);
hu's avatar
hu committed
565 566 567
            //RTE_CAN_SetAPP_RX_Mode(CAN_APP_RX_DISABLE);
            isReceive = 0;
            break;
hu's avatar
hu committed
568
        default:
hu's avatar
hu committed
569
            break;
hu's avatar
hu committed
570 571 572 573 574
        }
        /*if ((uint8_t)(Status & enableRxAndDisableTx) == enableRxAndTx)
        {
           // RTE_CAN_SetAPP_TX_Status(RTE_CAN_TX_DISABLE);
           // RTE_CAN_SetAPP_RX_Status(RTE_CAN_RX_ENABLE);
hu's avatar
hu committed
575

hu's avatar
hu committed
576 577
        }
        else if ((uint8_t)(Status & enableRxAndDisableTx) == enableRxAndTx)
hu's avatar
hu committed
578
        {
hu's avatar
hu committed
579 580 581 582 583 584
           // RTE_CAN_SetAPP_TX_Status(RTE_CAN_TX_ENABLE);
           // RTE_CAN_SetAPP_RX_Status(RTE_CAN_RX_ENABLE);
           RTE_CAN_SetAPP_TX_Mode(CAN_APP_TX_ENABLE);

        }
        if ((uint8_t)(Status & disableRxAndEnableTx) == disableRxAndEnableTx)
hu's avatar
hu committed
585
        {
hu's avatar
hu committed
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
           // RTE_CAN_SetAPP_TX_Status(RTE_CAN_TX_ENABLE);
           // RTE_CAN_SetAPP_RX_Status(RTE_CAN_RX_DISABLE);
           RTE_CAN_SetAPP_TX_Mode(CAN_APP_TX_ENABLE);
            isReceive = 0u;
        }
        else
        {
            isReceive = 1u;
            RTE_CAN_SetAPP_TX_Mode(CAN_APP_TX_DISABLE);
           // RTE_CAN_SetAPP_TX_Status(RTE_CAN_TX_ENABLE);
          //  RTE_CAN_SetAPP_RX_Status(RTE_CAN_RX_ENABLE);
        }*/
    }
    if ((uint8_t)(Type & networkManagementMsg) == networkManagementMsg)
    {
        if ((uint8_t)(Status & enableRxAndDisableTx) == enableRxAndDisableTx)
        {

        }
        else
        {

        }
        if ((uint8_t)(Status & disableRxAndEnableTx) == disableRxAndEnableTx)
        {

        }
        else
        {

        }
    }
}

/**
  *DTC使能定时
  */
void enableDTCTimer(uint16_t Timer)
{
    uint8_t BUS_OFF_Status = getBusoffStatus();
hu's avatar
hu committed
626
    if ((UDSKL15Status == 1u) && (BUS_OFF_Status == 0u) && (HVoltageStatus == 0u) && (LVoltageStatus == 0u))
hu's avatar
hu committed
627
    {
hu's avatar
hu committed
628
        if (isEnable == 0u)
hu's avatar
hu committed
629
        {
hu's avatar
hu committed
630
            if (enableTimer >= enableMaxTimer)
hu's avatar
hu committed
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
            {
                isEnable = 1u;
                enableTimer = 0u;
            }
            else
            {
                enableTimer += Timer;
            }
        }
    }
}

/**
  *DTC是否使能
  *@return DTC使能状态 0为未使能,1为使能
  */
uint8_t isDTCEnable(void)
{
    return isEnable;
}

/**
  *设置DTC使能时间
  */
void setDTCEnableTimer(uint16_t Timer)
{
    uint8_t i;
hu's avatar
hu committed
658
    if (isEnable == 1u)
hu's avatar
hu committed
659 660 661 662
    {
        isEnable = 0u;
        enableTimer = 0u;
        enableMaxTimer = Timer;
hu's avatar
hu committed
663
        for (i = 0u; i < (sizeof(msgList) / sizeof(msgList[0u])); ++i)
hu's avatar
hu committed
664 665 666 667 668 669
        {
            msgList[i][3] = 0u;
        }
    }
    else
    {
hu's avatar
hu committed
670
        if (enableMaxTimer == Timer)
hu's avatar
hu committed
671 672 673 674 675
        {
            enableTimer = 0u;
        }
        else
        {
hu's avatar
hu committed
676
            if (enableMaxTimer < enableTimer + Timer)
hu's avatar
hu committed
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
            {
                enableMaxTimer = enableTimer + Timer;
            }
        }
    }
}

/**
  *开启DTC检测
  */
void OpenDTCTest(void)
{
    isDTCOpen = 1u;
}

/**
  *关闭DTC检测
  */
void CloseDTCTest(void)
{
    isDTCOpen = 0u;
}

/**
  *DTC是否开启
  *@return DTC状态 0关闭,1开启
  */
uint8_t DTCTestIsOpen(void)
{
    return isDTCOpen;
}

/**
  *DTC老化
  */
void DTCAgeing(void)
{
    uint8_t i;
    for (i = 0u; i < sizeof(DTCToDFlashInfo.DTC) / sizeof(DTCUnion); ++i)
    {
        DTCToDFlashInfo.DTC[i].DTCStatus |= (DTCNoTestThisCycle);
        if ((uint8_t)(DTCToDFlashInfo.DTC[i].DTCStatus & DTCErrorThisCycle) != DTCErrorThisCycle)
        {
            DTCToDFlashInfo.DTC[i].DTCStatus &= (~DTCPending);
            DTCToDFlashInfo.DTC[i].DTCPendingCount = 0u;
            if ((uint8_t)(DTCToDFlashInfo.DTC[i].DTCStatus & DTCConfirmed) == DTCConfirmed)
            {
                DTCToDFlashInfo.DTC[i].DTCAgeing++;
                if (DTCToDFlashInfo.DTC[i].DTCAgeing >= AgeingCount)
                {
                    DTCToDFlashInfo.DTC[i].DTCStatus &= (~DTCConfirmed);
                }
            }
        }
        else
        {
            DTCToDFlashInfo.DTC[i].DTCStatus &= (~DTCErrorThisCycle);
        }
        DTCToDFlashInfo.DTC[i].DTCStatus &= DTCNoError;
    }
}

/**
  *读取所有的诊断信息
  */
void ReadAllDFlashData(void)
{
    ReadDFlashData(EEPROM_BLOCK_UDS_FLAG, (uint32_t *)&DiagFlag.Flag         , sizeof(DiagFlag)          / 4u, AllFlagInit);
    ReadDFlashData(EEPROM_BLOCK_UDS_S27, (uint32_t *)&Ser27_FlowCtrlCnt.Flag, sizeof(Ser27_FlowCtrlCnt) / 4u, Ser27ToDFlashInfoInit);
    ReadDFlashData(EEPROM_BLOCK_UDS_CONFIG, (uint32_t *)&EOL_Data_Type.Flag , sizeof(EOL_Data_Type)  / 4u, EOL_InitVal);
    ReadDFlashData(EEPROM_BLOCK_UDS_S2E, (uint32_t *)&Ser2EToDFlashInfo.Flag, sizeof(Ser2EToDFlashInfo) / 4u, Ser2EToDFlashInfoInit);
    //ReadDFlashData(0x05u, (uint32_t *)&DTCToDFlashInfo.Flag  , sizeof(DTCToDFlashInfo)   / 4u, DTCToDFlashInfoInit);

    if (Ser27_FlowCtrlCnt.LV1ReqSeedCnt >= SER27_MAX_REQ_SEED_NUM )
    {
        Ser27_FlowCtrl.LV1DelayCtrl = SER27_ERR_DELAY_TIMER_EN;
    }
    if (Ser27_FlowCtrlCnt.LV3ReqSeedCnt >= SER27_MAX_REQ_SEED_NUM)
    {
        Ser27_FlowCtrl.LV3DelayCtrl = SER27_ERR_DELAY_TIMER_EN;
    }
hu's avatar
hu committed
758

hu's avatar
hu committed
759
    DTCConfigurationInit();
hu's avatar
hu committed
760
    BootReadFlahInit();
hu's avatar
hu committed
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840
}

/**
  *读取信息
  */
uint8_t ReadDFlashData(uint8_t u8BlockNum, uint32_t u32Data[], uint16_t u16Len, InitFunction initFunction)
{
    uint8_t enResult = EEPROM_RW_FAIL;
    enResult = EEPROM_Get_Block_Status(u8BlockNum);
    if (enResult == EEPROM_STAT_BLANK)
    {
        initFunction();
        enResult = WriteDFlashData(u8BlockNum, u32Data, u16Len, NeedWait);
    }
    else if (enResult == EEPROM_STAT_ACTIVE)
    {
        enResult = EEPROM_Read_Data(u8BlockNum, u32Data, u16Len);
        if (enResult == EEPROM_RW_PASS)
        {
            while (EEPROM_Get_Status() == EEPROM_STAT_BUSY)
            {
                WDT_Clear();
            }
            if (u32Data[0] != 0x7AA7A55Au)
            {
                initFunction();
                enResult = WriteDFlashData(u8BlockNum, u32Data, u16Len, NeedWait);
            }
        }
    }
    else
    {
        initFunction();
        enResult = WriteDFlashData(u8BlockNum, u32Data, u16Len, NeedWait);
    }/**/
    return enResult;
}

/**
  *写入信息
  */
uint8_t WriteDFlashData(uint8_t u8BlockNum, uint32_t u32Data[], uint16_t u16Len, uint8_t isWait)
{
    uint8_t enResult = EEPROM_RW_FAIL;
    if (EEPROM_Get_Block_Status (u8BlockNum) != EEPROM_STAT_ERROR)
    {
        while (EEPROM_Get_Status() == EEPROM_STAT_BUSY)
        {
            WDT_Clear();
        }
        enResult = EEPROM_Write_Data(u8BlockNum, u32Data, u16Len);
        if (isWait == 1u)
        {
            while (EEPROM_Get_Status() == EEPROM_STAT_BUSY)
            {
                WDT_Clear();
            }
        }
    }
    /*enResult = EEPROM_Write_Data(u8BlockNum, u32Data, u16Len);*/
    return enResult;
}

/**
  *所有标志位初始化
  */
void AllFlagInit(void)
{
    DiagFlag.Flag = 0x7AA7A55Au;
    DiagFlag.JumpToBootFlag = 0x00000000u;
    DiagFlag.AppValidFlag = 0x00000000u;
    DiagFlag.S11Flag = 0x00000000u;
}

/**
  *27信息初始化
  */
void Ser27ToDFlashInfoInit(void)
{
    Ser27_FlowCtrlCnt.Flag = 0x7AA7A55Au;
hu's avatar
hu committed
841 842
    //  Ser27_FlowCtrlCnt.LV11_InvaidKyeCnt = 0u;
    //  Ser27_FlowCtrlCnt.LV11_ReqSeedCnt = 0u;
hu's avatar
hu committed
843 844 845 846 847 848 849 850 851 852 853 854 855 856
    Ser27_FlowCtrlCnt.LV1InvaidKyeCnt = 0u;
    Ser27_FlowCtrlCnt.LV1ReqSeedCnt = 0u;
    Ser27_FlowCtrlCnt.RandNum = 0u;
}

/**
  *boot信息初始化
  */
void BootToDFlashInfoInit(void)
{
    /*
    BootToDFlashInfo.Flag = 0x7AA7A55Au;
    BootToDFlashInfo.DID_0200 = 0u;
    BootToDFlashInfo.DID_0201 = 0u;
hu's avatar
hu committed
857

hu's avatar
hu committed
858 859 860 861 862 863 864 865 866
    memset(BootToDFlashInfo.DID_F180, 0x20, 7u);
    BootToDFlashInfo.DID_F180[0] = '1';
    BootToDFlashInfo.DID_F180[1] = '0';
    BootToDFlashInfo.DID_F180[2] = '3';

    memset(BootToDFlashInfo.DID_F181, 0x20, 7u);
    BootToDFlashInfo.DID_F181[0] = '1';
    BootToDFlashInfo.DID_F181[1] = '0';
    BootToDFlashInfo.DID_F181[2] = '3';
hu's avatar
hu committed
867

hu's avatar
hu committed
868 869 870 871 872 873 874 875 876 877 878
    memset(BootToDFlashInfo.DID_F187, 0x20, 10u);
    BootToDFlashInfo.DID_F187[0] = '8';
    BootToDFlashInfo.DID_F187[1] = '8';
    BootToDFlashInfo.DID_F187[2] = '9';
    BootToDFlashInfo.DID_F187[3] = '1';
    BootToDFlashInfo.DID_F187[4] = '1';
    BootToDFlashInfo.DID_F187[5] = '2';
    BootToDFlashInfo.DID_F187[6] = '8';
    BootToDFlashInfo.DID_F187[7] = '9';
    BootToDFlashInfo.DID_F187[8] = '7';
    BootToDFlashInfo.DID_F187[9] = '0';
hu's avatar
hu committed
879

hu's avatar
hu committed
880 881 882 883 884 885 886 887 888 889 890 891 892 893
    memset(BootToDFlashInfo.DID_F189, 0x20, 16u);
    BootToDFlashInfo.DID_F189[0] = '0';
    BootToDFlashInfo.DID_F189[1] = '0';
    BootToDFlashInfo.DID_F189[2] = '0';
    BootToDFlashInfo.DID_F189[3] = '7';
    BootToDFlashInfo.DID_F189[4] = '2';
    BootToDFlashInfo.DID_F189[5] = '1';
    BootToDFlashInfo.DID_F189[6] = '1';
    BootToDFlashInfo.DID_F189[7] = 'C';
    BootToDFlashInfo.DID_F189[8] = '4';
    BootToDFlashInfo.DID_F189[9] = '0';
    BootToDFlashInfo.DID_F189[10] = '/';
    BootToDFlashInfo.DID_F189[11] = 'A';
    BootToDFlashInfo.DID_F189[12] = 'A';
hu's avatar
hu committed
894

hu's avatar
hu committed
895 896 897 898
    memset(BootToDFlashInfo.DID_F193, '0', 16u);
    BootToDFlashInfo.DID_F193[0] = '1';
    BootToDFlashInfo.DID_F193[1] = '0';
    BootToDFlashInfo.DID_F193[2] = '4';
hu's avatar
hu committed
899

hu's avatar
hu committed
900 901 902 903 904 905 906 907 908 909 910 911 912
    memset(BootToDFlashInfo.DID_F198, 0x20, 16u);

    BootToDFlashInfo.DID_F199[0] = 0x20u;
    BootToDFlashInfo.DID_F199[1] = 0x00u;
    BootToDFlashInfo.DID_F199[2] = 0x01u;
    BootToDFlashInfo.DID_F199[3] = 0x01u;
    */
}

/**
  *2E信息初始化
  */
void Ser2EToDFlashInfoInit(void)
hu's avatar
hu committed
913 914
{
    Ser2EToDFlashInfo.Flag = 0x7AA7A55Au;
hu's avatar
hu committed
915 916 917 918
    /*
    uint8_t i;
    _EOL_F101 EOL_F101;
    _EOL_F110 EOL_F110;
hu's avatar
hu committed
919 920


hu's avatar
hu committed
921 922 923
    memset(Ser2EToDFlashInfo.DID_DFEC, 0, 64u);

    memset(Ser2EToDFlashInfo.DID_DFED, 0, 64u);
hu's avatar
hu committed
924

hu's avatar
hu committed
925
    memset(Ser2EToDFlashInfo.DID_DFEE, 0, 64u);
hu's avatar
hu committed
926

hu's avatar
hu committed
927
    memset(Ser2EToDFlashInfo.DID_DFEF, 0, 64u);
hu's avatar
hu committed
928

hu's avatar
hu committed
929 930 931 932 933 934
    memset(Ser2EToDFlashInfo.DID_F112, 0x20, 8u);
    Ser2EToDFlashInfo.DID_F112[0] = 'F';
    Ser2EToDFlashInfo.DID_F112[1] = 'E';
    Ser2EToDFlashInfo.DID_F112[2] = '_';
    Ser2EToDFlashInfo.DID_F112[3] = '7';
    Ser2EToDFlashInfo.DID_F112[4] = 'B';
hu's avatar
hu committed
935

hu's avatar
hu committed
936 937 938 939 940 941
    memset(Ser2EToDFlashInfo.DID_F190, 0x20, 17u);

    memset(Ser2EToDFlashInfo.DID_F197, 0x20, 8u);
    Ser2EToDFlashInfo.DID_F197[0] = 'I';
    Ser2EToDFlashInfo.DID_F197[1] = 'P';
    Ser2EToDFlashInfo.DID_F197[2] = 'K';
hu's avatar
hu committed
942

hu's avatar
hu committed
943 944 945 946
    Ser2EToDFlashInfo.DID_F19D[0] = 0x20u;
    Ser2EToDFlashInfo.DID_F19D[1] = 0x00u;
    Ser2EToDFlashInfo.DID_F19D[2] = 0x01u;
    Ser2EToDFlashInfo.DID_F19D[3] = 0x01u;
hu's avatar
hu committed
947

hu's avatar
hu committed
948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
    EOL_InitVal();
    EOL_F101 = EOL_GetF101();
    EOL_F110 = EOL_GetF110();
    for (i = 0u; i < 16u; ++i)
    {
        Ser2EToDFlashInfo.DID_F101[i] = EOL_F101.F101_Byte[i];
    }
    for (i = 0u; i < 8u; ++i)
    {
        Ser2EToDFlashInfo.DID_F110[i] = EOL_F110.F110_Byte[i];
    }
    memset(Ser2EToDFlashInfo.DID_6208, 0, 3u);
    Ser2EToDFlashInfo.DID_620A[0] = 0u;
    */
}

/**
  *DTC信息初始化
  */
void DTCToDFlashInfoInit(void)
{
    uint8_t i;
    DTCToDFlashInfo.Flag = 0x7AA7A55Au;
    for (i = 0u; i < sizeof(DTCToDFlashInfo.DTC) / sizeof(DTCUnion); ++i)
    {
        DTCToDFlashInfo.DTC[i].DTCStatus = DTCNoTestLastClear | DTCNoTestThisCycle;
        DTCToDFlashInfo.DTC[i].DTCAgeing = 0x00u;
        DTCToDFlashInfo.DTC[i].DTCCount = 0x00u;
        DTCToDFlashInfo.DTC[i].DTCPendingCount = 0x00u;
        /*DTCToDFlashInfo.DTC[i].Snapshot.ECUPowerVoltage = 0x00u;
        DTCToDFlashInfo.DTC[i].Snapshot.VehicleSpeed = 0x0000u;
        DTCToDFlashInfo.DTC[i].Snapshot.OccurrenceCounter = 0x00u;
        DTCToDFlashInfo.DTC[i].Snapshot.FirstMalfunction = 0x00000000u;
        DTCToDFlashInfo.DTC[i].Snapshot.LastMalfunction = 0x00000000u;*/
    }
hu's avatar
hu committed
983
    for (i = 0u; i < (sizeof(msgList) / sizeof(msgList[0u])); ++i)
hu's avatar
hu committed
984 985 986
    {
        msgList[i][3] = 0u;
    }
hu's avatar
hu committed
987
    for (i = 0u; i < (sizeof(DTCTimerList) / sizeof(DTCTimerList[0u])); ++i)
hu's avatar
hu committed
988 989 990 991 992 993 994 995 996 997
    {
        DTCTimerList[i] = 0u;
    }
}

/**
  *DTC信息配置初始化
  */
void DTCConfigurationInit(void)
{
hu's avatar
hu committed
998
    if (!Val_EOL_605C_LDWS())
hu's avatar
hu committed
999 1000 1001 1002 1003 1004
    {
        DTCToDFlashInfo.DTC[cnU100706].DTCStatus = DTCNoTestLastClear | DTCNoTestThisCycle;
        DTCToDFlashInfo.DTC[cnU100706].DTCAgeing = 0x00u;
        DTCToDFlashInfo.DTC[cnU100706].DTCCount = 0x00u;
        DTCToDFlashInfo.DTC[cnU100706].DTCPendingCount = 0x00u;
    }
hu's avatar
hu committed
1005

hu's avatar
hu committed
1006 1007 1008 1009
    /*    DTCToDFlashInfo.DTC[cnU010087].Snapshot.ECUPowerVoltage = 0x00u;
        DTCToDFlashInfo.DTC[cnU010087].Snapshot.VehicleSpeed = 0x0000u;
        DTCToDFlashInfo.DTC[cnU010087].Snapshot.OccurrenceCounter = 0x00u;
        DTCToDFlashInfo.DTC[cnU010087].Snapshot.FirstMalfunction = 0x00000000u;
hu's avatar
hu committed
1010 1011 1012 1013 1014 1015 1016 1017
        DTCToDFlashInfo.DTC[cnU010087].Snapshot.LastMalfunction = 0x00000000u;*/
}

void BootReadFlahInit(void)
{
    uint8_t i;
    uint8_t WriteFlag = 0;
    for (i = 0; i < sizeof(Ser2EToDFlashInfo.DID_F187); i++)
hu's avatar
hu committed
1018
    {
hu's avatar
hu committed
1019 1020 1021 1022 1023
        if (Ser2EToDFlashInfo.DID_F187[i] != Ser22_DID_F187[i])
        {
            Ser2EToDFlashInfo.DID_F187[i] = Ser22_DID_F187[i];
            WriteFlag = 1;
        }
hu's avatar
hu committed
1024
    }
hu's avatar
hu committed
1025
    for (i = 0; i < sizeof(Ser2EToDFlashInfo.DID_F18A); i++)
hu's avatar
hu committed
1026
    {
hu's avatar
hu committed
1027 1028 1029 1030 1031
        if (Ser2EToDFlashInfo.DID_F18A[i] != Ser22_DID_F18A[i])
        {
            Ser2EToDFlashInfo.DID_F18A[i] = Ser22_DID_F18A[i];
            WriteFlag = 1;
        }
hu's avatar
hu committed
1032
    }
hu's avatar
hu committed
1033
    for (i = 0; i < sizeof(Ser2EToDFlashInfo.DID_F18B); i++)
hu's avatar
hu committed
1034
    {
hu's avatar
hu committed
1035 1036 1037 1038 1039
        if (Ser2EToDFlashInfo.DID_F18B[i] != Ser22_DID_F18B[i])
        {
            Ser2EToDFlashInfo.DID_F18B[i] = Ser22_DID_F18B[i];
            WriteFlag = 1;
        }
hu's avatar
hu committed
1040
    }
hu's avatar
hu committed
1041
    for (i = 0; i < sizeof(Ser2EToDFlashInfo.DID_F18C); i++)
hu's avatar
hu committed
1042
    {
hu's avatar
hu committed
1043 1044 1045 1046 1047
        if (Ser2EToDFlashInfo.DID_F18C[i] != Ser22_DID_F18C[i])
        {
            Ser2EToDFlashInfo.DID_F18C[i] = Ser22_DID_F18C[i];
            WriteFlag = 1;
        }
hu's avatar
hu committed
1048
    }
hu's avatar
hu committed
1049
    for (i = 0; i < sizeof(Ser2EToDFlashInfo.DID_F193); i++)
hu's avatar
hu committed
1050
    {
hu's avatar
hu committed
1051 1052 1053 1054 1055
        if (Ser2EToDFlashInfo.DID_F193[i] != Ser22_DID_F193[i])
        {
            Ser2EToDFlashInfo.DID_F193[i] = Ser22_DID_F193[i];
            WriteFlag = 1;
        }
hu's avatar
hu committed
1056
    }
hu's avatar
hu committed
1057
    for (i = 0; i < sizeof(Ser2EToDFlashInfo.DID_F195); i++)
hu's avatar
hu committed
1058
    {
hu's avatar
hu committed
1059 1060 1061 1062 1063
        if (Ser2EToDFlashInfo.DID_F195[i] != Ser22_DID_F195[i])
        {
            Ser2EToDFlashInfo.DID_F195[i] = Ser22_DID_F195[i];
            WriteFlag = 1;
        }
hu's avatar
hu committed
1064
    }
hu's avatar
hu committed
1065
    if (WriteFlag)
hu's avatar
hu committed
1066
    {
hu's avatar
hu committed
1067
        write2EData(Write2EData);
hu's avatar
hu committed
1068 1069
    }
}