kwp2000_tp.c 19.2 KB
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 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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 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 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 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 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 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 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 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 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

/*
*********************************************************************
* Includes
*********************************************************************
*/

#include "kwp2000_interface.h"
#include "kwp2000_tp.h"

/*
*********************************************************************
* defines
*********************************************************************
*/
/* Possible initialization formats */
#define KWP2000_FORMAT_NOADDRESS 0u            /* 00 */
#define KWP2000_FORMAT_CARB 1u                 /* 01 Exception mode */
#define KWP2000_FORMAT_PHYSICALADDRESSING 2u   /* 10 */
#define KWP2000_FORMAT_FUNCTIONALADDRESSING 3u /* 11 */

#define KWP2000_WUP_TI_IDLE_DU 5ul /* idle time of RX pins [us] */

#define KWP2000_WUP_TI_MIN_DU 24ul /* duration of WUP, min value (low time/hi time) [ms]  */
#define KWP2000_WUP_TI_MAX_DU 26ul /* duration of WUP, max value (low time/hi time) [ms]  */

/*
*********************************************************************
* structure
*********************************************************************
*/

typedef enum
{
    KWP2000_WUP_WAIT4IDLE_E = 0x00,
    KWP2000_WUP_WAIT4SCANLO_E = 0x10,
    KWP2000_WUP_SCANLO_E,
    KWP2000_WUP_WAIT4SCANHI_E = 0x20,
    KWP2000_WUP_SCANHI_E,
    KWP2000_WUP_SCANASC_E = 0x30,
    KWP2000_WUP_DETECTED_E = 0x80,
} Kwp2000_ScanStates_t;

typedef struct
{
    unsigned long xPortIn_pu32;
    unsigned long xPortInMsk_u32;
    unsigned long tiStartSample_u32;
    Kwp2000_ScanStates_t xScanState_u32;
    unsigned char xBusType_u32;
    unsigned long tiCom_u32;
} Kwp2000_ScanWup_t;

/*
*********************************************************************
* variable
*********************************************************************
*/

/* Carb address and format */
unsigned char kwp2000_CARB_Address;
unsigned char kwp2000_CARB_TgtRequest;
unsigned char kwp2000_CARB_TgtResponse;
unsigned char kwp2000_CARB_Fmt;

unsigned long Kwp2000_TimeCounter;
unsigned long Kwp2000_P2_TimeCounter;
unsigned long Kwp2000_ComMode;
Kwp2000_ScanWup_t Kwp2000_ScanWup;
Kwp2000_Modify_t Kwp2000_Modify;
Kwp2000_ComState_t Kwp2000_ComState;

/*
*********************************************************************
* function
*********************************************************************
*/

void Kwp2000_ProtocolInit(void);

/*-------------------------------------------------------------------------
* Function Name  : Kwp2000_Init
* Description    : Kwp2000初始化
* Input          :
* Output         : None
* Return         : None
* onther         :
--------------------------------------------------------------------------*/
void Kwp2000_Init(void)
{
    Kwp2000_AscInit();
    Kwp2000_ProtocolInit();
}

/*-------------------------------------------------------------------------
* Function Name  : Kwp2000_ProtocolInit
* Description    : KWP2000协议初始化
* Input          :
* Output         : None
* Return         : None
* onther         :
--------------------------------------------------------------------------*/
void Kwp2000_ProtocolInit(void)
{
    /* Initial time base */
    Kwp2000_TimeCounter = 0;
    Kwp2000_P2_TimeCounter = 0;

    /* Prepare Wup scan */
    Kwp2000_ComMode = COM_INIT;

    Kwp2000_ScanWup.xScanState_u32 = KWP2000_WUP_WAIT4IDLE_E;
    Kwp2000_ScanWup.tiStartSample_u32 = Kwp2000_TimeCounter;

    //Uart_DisableReceiveIsrReq(); /*----20220309----*/

    /*Set RX as GPIO-IN*/
    Uart_SetRxGpio();

    Kwp2000_Modify.typeOfModif = KWP2000_MODIFY_NULL;

    Kwp2000_ComState.Rx_len = 0;

    Kwp2000_SetAddress();
    Kwp2000_SetTiming();

    kwp2000_CARB_TgtRequest = 0x6A;
    kwp2000_CARB_TgtResponse = 0x6B;

    Kwp2000_ComState.ResponsePending = 0;
}

/*-------------------------------------------------------------------------
* Function Name  : Kwp2000_Timeout
* Description    :
* Input          :
* Output         : None
* Return         : None
* onther         :
--------------------------------------------------------------------------*/
void Kwp2000_Timeout(void)
{
    unsigned long tiSample_u32;

    if ((Kwp2000_ComMode & PROTOCOL) == KWP2000)
    {
        tiSample_u32 = Kwp2000_TimeCounter;

        /**/
        if (Kwp2000_ComMode == KWP2000_FAST_INIT)
        {

            if ((tiSample_u32 - Kwp2000_ComState.tiCom_u32) >= 20u)
            {
                Kwp2000_ProtocolInit();
            }
        }
        else
        {

            if ((tiSample_u32 - Kwp2000_ComState.tiCom_u32) >= kwp2000_P3_MAX_LIMIT)
            {
                Kwp2000_ProtocolInit();
            }
        }
        /*
        tiSample_u32 = Kwp2000_TimeCounter;

        if((tiSample_u32 - Kwp2000_ComState.tiCom_u32) >= kwp2000_P3_MAX_LIMIT)
        {
            Kwp2000_ProtocolInit();
        }
        */
    }
}

/*-------------------------------------------------------------------------
* Function Name  : Kwp2000_CommuniationDown
* Description    : 通讯底层与协议层初始化
* Input          :
* Output         : None
* Return         : None
* onther         :
--------------------------------------------------------------------------*/
void Kwp2000_CommuniationDown(void)
{
    Kwp2000_AscInit();
    Kwp2000_ProtocolInit();
}

/*-------------------------------------------------------------------------
* Function Name  : Kwp2000_VerifyChecksum
* Description    : Verify the message checksum
* Input          :
* Output         : None
* Return         : TRUE or FALSE
* onther         :
--------------------------------------------------------------------------*/
unsigned char Kwp2000_VerifyChecksum(void)
{
    unsigned char answer;
    unsigned char CS;

    /* Calculates the message Checksum */
    CS = Kwp2000_ChecksumCalculate((unsigned char *) & (Kwp2000_ComState.RxBuffer[0]), (unsigned short)(Kwp2000_ComState.headerSize + Kwp2000_ComState.kwp2000_Len));
    /* Verifies it with the one that was in the message */
    if (CS == Kwp2000_ComState.kwp2000_Checksum)
    {
        /* it is ok */
        answer = 1;
    }
    else
    {
        /* it is not the same */
        answer = 0;
    }

    return (answer);
}

/*-------------------------------------------------------------------------
* Function Name  : Kwp2000_ChecksumCalculate
* Description    : calculates the checksum
* Input          : dataBuffer, numberOfBytes
* Output         : None
* Return         : sum
* onther         :
--------------------------------------------------------------------------*/
unsigned char Kwp2000_ChecksumCalculate(unsigned char *dataBuffer, unsigned short numberOfBytes)
{
    unsigned char sum;
    unsigned short i;

    /* Resets the sum */
    sum = 0;

    for (i = 0; i < numberOfBytes; i++)
    {
        /* add each byte to the sum */
        sum += dataBuffer[i];
    }

    return (sum);
}

/*-------------------------------------------------------------------------
* Function Name  : Kwp2000_PrepareHeader
* Description    : constructs the message header
* Input          : headerSize
* Output         : dataSize
* Return         : sum
* onther         :
--------------------------------------------------------------------------*/
unsigned char Kwp2000_PrepareHeader(unsigned char dataSize)
{
    unsigned char headerSize;
    unsigned char fmt;

    /* Resets the header size */
    headerSize = 1;
    /* Tests the data size */
    if (dataSize > 63)
    {
        /* Too big to be written in the format byte, needs additional length byte */
        fmt = Kwp2000_ComState.Format << 6;
        Kwp2000_ComState.TxBuffer[4 - (headerSize++)] = dataSize;
    }
    else
    {
        /* Format and size in the same byte */
        fmt = (Kwp2000_ComState.Format << 6) | dataSize;
    }
    /* Test the format used */
    switch (Kwp2000_ComState.Format)
    {
    case KWP2000_FORMAT_NOADDRESS:
        /* if no address then nothing to be added to the header */
        break;
    case KWP2000_FORMAT_CARB:
        //   /* CARB mode */
        //   fmt = kwp2000_CARB_Fmt;
        //   asc0_CommunicationBuf.b[4-(headerSize++)]= kwp2000_CARB_Address;
        //   asc0_CommunicationBuf.b[4-(headerSize++)]= kwp2000_CARB_TgtResponse;
        break;
    case KWP2000_FORMAT_FUNCTIONALADDRESSING:
    case KWP2000_FORMAT_PHYSICALADDRESSING:
        /* Physical addressing */
        Kwp2000_ComState.TxBuffer[4 - (headerSize++)] = kwp2000_PhysicalAddress;
        Kwp2000_ComState.TxBuffer[4 - (headerSize++)] = Kwp2000_ComState.SrcAddr; // kwp2000_ServerAddress;
        break;
    }
    Kwp2000_ComState.TxBuffer[4 - headerSize] = fmt;

    Kwp2000_ComState.Response_Datas = (unsigned char *)&Kwp2000_ComState.TxBuffer[4 - headerSize];

    return (headerSize);
}

/*-------------------------------------------------------------------------
* Function Name  : Kwp2000_FormatAnalyse
* Description    : Copies the datas from the buffer to the variables related to the header information
* Input          :
* Output         : None
* Return         : None
* onther         :
--------------------------------------------------------------------------*/
void Kwp2000_HeaderAnalyse(void)
{
    /* Tests the header size */
    switch (Kwp2000_ComState.headerSize)
    {
    case 1:
        /* 1 byte  -> FMT */
        break;
    case 2:
        /* 2 bytes -> FMT LEN */
        Kwp2000_ComState.kwp2000_Len = Kwp2000_ComState.RxBuffer[1];
        break;
    case 3:
        /* 3 bytes -> FMT TGT SRC */
        Kwp2000_ComState.TgtAddr = Kwp2000_ComState.RxBuffer[1];
        Kwp2000_ComState.SrcAddr = Kwp2000_ComState.RxBuffer[2];
        break;
    case 4:
        /* 4 bytes -> FMT TGT SRC LEN */
        Kwp2000_ComState.TgtAddr = Kwp2000_ComState.RxBuffer[1];
        Kwp2000_ComState.SrcAddr = Kwp2000_ComState.RxBuffer[2];
        Kwp2000_ComState.kwp2000_Len = Kwp2000_ComState.RxBuffer[3];
        break;
    }
}

/*-------------------------------------------------------------------------
* Function Name  : Kwp2000_FormatAnalyse
* Description    : ����֡����
* Input          :
* Output         : None
* Return         : None
* onther         :
--------------------------------------------------------------------------*/
unsigned char Kwp2000_FormatAnalyse(void)
{
    unsigned char headerSize;

    /* Extracts the format information */
    Kwp2000_ComState.Format = (Kwp2000_ComState.RxBuffer[0] & 0xC0) >> 6;
    /* Extracts the message length */
    Kwp2000_ComState.kwp2000_Len = Kwp2000_ComState.RxBuffer[0] & 0x3F;
    /* Resets the headerSize */
    headerSize = 1;
    /* Tests the format */
    switch (Kwp2000_ComState.Format)
    {
    case KWP2000_FORMAT_NOADDRESS:
        /* There is no address */
        break;
    case KWP2000_FORMAT_CARB:
        /* There are source and target addresses */
        Kwp2000_ComState.kwp2000_Len = 0;
    case KWP2000_FORMAT_PHYSICALADDRESSING:
    case KWP2000_FORMAT_FUNCTIONALADDRESSING:
        /* There are source and target addresses */
        headerSize += 2;
        break;
    }
    /* Tests the message length */
    if (Kwp2000_ComState.kwp2000_Len == 0)
    {
        /* it was 0 so there is an additional length byte */
        headerSize++;
    }
    else
    {
    }
    /* Sets the pointer at the beginning of the datas */
    Kwp2000_ComState.Request_Datas = (unsigned char *) & (Kwp2000_ComState.RxBuffer[headerSize]);

    return (headerSize);
}

/*-------------------------------------------------------------------------
* Function Name  : kwp2000_AddressTest
* Description    : verifies the target address to know if this ECU is the target
* Input          :
* Output         : None
* Return         : TRUE or FALSE
* onther         :
--------------------------------------------------------------------------*/
unsigned char kwp2000_AddressTest(void)
{
    unsigned char answer = 0;

    /* Tests the format */
    if (Kwp2000_ComState.Format == KWP2000_FORMAT_NOADDRESS)
    {
        /* No addressing bytes */
        /* The message is for this ECU */
        answer = 1;
    }
    else
    {
        switch (Kwp2000_ComState.Format)
        {
        case KWP2000_FORMAT_CARB:
            /* CARB addressing mode */
            if (Kwp2000_ComState.TgtAddr == kwp2000_CARB_TgtRequest)
            {
                /* it is the correct address */
                answer = 1;
            }
            else
            {
                /* it is the false address */
                answer = 0;
            }
            break;
        case KWP2000_FORMAT_PHYSICALADDRESSING:
            /* Physical addressing mode */
            if (Kwp2000_ComState.TgtAddr == kwp2000_PhysicalAddress)
            {
                /* it is the correct address */
                answer = 1;
            }
            else
            {
                /* it is the false address */
                answer = 0;
            }
            break;
        case KWP2000_FORMAT_FUNCTIONALADDRESSING:
            /* Functional addressing mode */
            if ((Kwp2000_ComState.TgtAddr == kwp2000_FunctionalAddress) || (Kwp2000_ComState.TgtAddr == kwp2000_PhysicalAddress))
            {
                /* it is the correct address */
                answer = 1;
            }
            else
            {
                /* it is the false address */
                answer = 0;
            }
            break;
        default:
            break;
        }
    }

    return (answer);
}

/*-------------------------------------------------------------------------
* Function Name  : Kwp2000_ComInit_Handle
* Description    : 测量Twup
* Input          :
* Output         : None
* Return         : None
* onther         :
--------------------------------------------------------------------------*/
void Kwp2000_ComInit_Handle(void)
{
    unsigned char RxPortState;

    /* scan all RX-pins, which are configured, for WUP */
    RxPortState = Uart_GetRxLevel();

    /* scan WUP (100ms) for each ASC available */

    /* wait Tidle */
    if (Kwp2000_ScanWup.xScanState_u32 == KWP2000_WUP_WAIT4IDLE_E)
    {
        if (RxPortState != 0)
        {
            if ((Kwp2000_TimeCounter - Kwp2000_ScanWup.tiStartSample_u32) >= KWP2000_WUP_TI_IDLE_DU)
            {
                Kwp2000_ScanWup.xScanState_u32 = KWP2000_WUP_WAIT4SCANLO_E;
            }
        }
        else
        {
            Kwp2000_ScanWup.tiStartSample_u32 = Kwp2000_TimeCounter;
        }
    }

    /* wait for low level on RX-pin and start time measurement */
    if (Kwp2000_ScanWup.xScanState_u32 == KWP2000_WUP_WAIT4SCANLO_E)
    {
        if (RxPortState == 0)
        {
            Kwp2000_ScanWup.xScanState_u32 = KWP2000_WUP_SCANLO_E;
            Kwp2000_ScanWup.tiStartSample_u32 = Kwp2000_TimeCounter;
        }
    }
    // measure low level time on RX-pin, if level changes to high during the first 25ms
    // abort measurement
    if (Kwp2000_ScanWup.xScanState_u32 == KWP2000_WUP_SCANLO_E)
    {
        if (RxPortState != 0)
        {
            if (((Kwp2000_TimeCounter - Kwp2000_ScanWup.tiStartSample_u32) >= KWP2000_WUP_TI_MIN_DU) && ((Kwp2000_TimeCounter - Kwp2000_ScanWup.tiStartSample_u32) <= KWP2000_WUP_TI_MAX_DU))
            {
                Kwp2000_ScanWup.tiStartSample_u32 = Kwp2000_TimeCounter;
                Kwp2000_ScanWup.xScanState_u32 = KWP2000_WUP_WAIT4SCANHI_E;
            }
            else
            {
                Kwp2000_ScanWup.tiStartSample_u32 = Kwp2000_TimeCounter;
                Kwp2000_ScanWup.xScanState_u32 = KWP2000_WUP_WAIT4IDLE_E;
            }
        }
    }
    // After 25ms - 4%, wait until RX-pin level goes hi
    // If level stays low, abort measurement
    if (Kwp2000_ScanWup.xScanState_u32 == KWP2000_WUP_WAIT4SCANHI_E)
    {
        if (RxPortState != 0)
        {
            if ((Kwp2000_TimeCounter - Kwp2000_ScanWup.tiStartSample_u32) >= KWP2000_WUP_TI_MIN_DU)
            {
                Kwp2000_ScanWup.xScanState_u32 = KWP2000_WUP_DETECTED_E;

                Uart_SetRxGpio(); // RX
                Kwp2000_AscEnableRx();

                /* reset variables, which are used by the receive interrupt function */
                Kwp2000_ComState.Rx_len = 0;

                /* enable irq */
                Uart_EnableReceiveIsrReq();

                Kwp2000_ComMode = KWP2000_FAST_INIT;

                Kwp2000_ComState.tiCom_u32 = Kwp2000_TimeCounter;
            }
        }
        else
        {
            if (RxPortState == 0)
            {
                Kwp2000_ScanWup.tiStartSample_u32 = Kwp2000_TimeCounter;
                Kwp2000_ScanWup.xScanState_u32 = KWP2000_WUP_WAIT4IDLE_E;
            }
        }
    }

    Kwp2000_TimeCounter += 1;
    Kwp2000_P2_TimeCounter += 1;
}

/*-------------------------------------------------------------------------
* Function Name  : Kwp2000_AscRxInterrupt
* Description    : 串口接收中断
* Input          :
* Output         : None
* Return         : None
* onther         :
--------------------------------------------------------------------------*/
void Kwp2000_AscRxInterrupt(void)
{
    unsigned short rxUartData;

    Uart_ClearRxFullFlag(); /* clear service request flags */

    if ((Kwp2000_ComMode & PROTOCOL) == KWP2000)
    {
        if (Kwp2000_ComMode < KWP2000_BUILD_RESPONSE)
        {
            /* if receve on going */
            if (Kwp2000_ComMode == KWP2000_FAST_INIT)
            {
                Kwp2000_ComMode = KWP2000_WAIT_RECEPTION;
            }

            if (Kwp2000_ComState.Rx_len < KWP2000_RX_BUFFER_SIZE)
            {
                rxUartData = Uart_GetData(); /*读取串口数据 ----20220303----*/

                Kwp2000_ComState.RxBuffer[Kwp2000_ComState.Rx_len] = (unsigned char)rxUartData;

                Kwp2000_ComState.tiCom_u32 = Kwp2000_TimeCounter;

                Kwp2000_ComState.Rx_len++;
            }
        }
        else
        {
            /* if transmit on going, do nothing */
            rxUartData = Uart_GetData(); /* 读取串口数据----20220303----*/

            if (Kwp2000_ComState.Tx_len)
            {
                Kwp2000_ComState.Tx_len--;
            }

            if (Kwp2000_ComState.Tx_len > 0)
            {
                if (rxUartData == (*Kwp2000_ComState.Response_Datas))
                {
                    Kwp2000_P2_TimeCounter = 0;
                    Kwp2000_ComState.Response_Datas++;
                    Kwp2000_AscTx(Kwp2000_ComState.Response_Datas); /* 数据发送----20220303----*/
                }
                else
                {
                    Kwp2000_ComState.Tx_len = 0;
                    Kwp2000_ComMode = KWP2000_WAIT_RECEPTION;
                }
            }
            else
            {
                if (Kwp2000_ComState.ResponsePending)
                {
                    Kwp2000_ComMode = KWP2000_BUILD_RESPONSE;

                    /* Start a new timeout counter */
                    Kwp2000_ComState.tiCom_u32 = Kwp2000_TimeCounter;
                }
                else
                {
                    Kwp2000_ComMode = KWP2000_MODIFY_CONFIG;
                }
            }
        }
    }
}