gd32f10x_can.c 35.9 KB
Newer Older
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 625 626 627 628 629 630 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 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 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 758 759 760 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 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 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 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
/**
  ******************************************************************************
  * @brief   CAN functions of the firmware library.
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "gd32f10x_can.h"
#include "gd32f10x_rcc.h"

/** @addtogroup GD32F10x_Firmware
  * @{
  */

/** @defgroup CAN
  * @brief CAN driver modules
  * @{
  */

/** @defgroup CAN_Private_Defines
  * @{
  */


/* CAN Mailbox Transmit Request */
#define CAN_TMIR_TE                       ((uint32_t)0x00000001)

/* CAN Filter Master Register bits */
#define FMR_FINIT                         ((uint32_t)0x00000001)

/* Time out for IWS bit */
#define IWS_TIMEOUT                       ((uint32_t)0x0000FFFF)
/* Time out for SWS bit */
#define SWS_TIMEOUT                       ((uint32_t)0x0000FFFF)

/* CAN TMIR_StdId masks */
#define CAN_TMIR_StdId_Mask               ((uint32_t)0x000007FF)

/* CAN TMIR_ExtId masks */
#define CAN_TMIR_ExtId_Mask               ((uint32_t)0x1FFFFFFF)

/* CAN TMIR_ExtId masks */
#define CAN_FLAG_Mask                     ((uint32_t)0x000FFFFF)

/* Flags in TSTR register */
#define CAN_FLAGS_TSTR                    ((uint32_t)0x08000000)

/* Flags in RFR1 register */
#define CAN_FLAGS_RFR1                    ((uint32_t)0x04000000)

/* Flags in RFR0register */
#define CAN_FLAGS_RFR0                    ((uint32_t)0x02000000)

/* Flags in STR register */
#define CAN_FLAGS_STR                     ((uint32_t)0x01000000)

/* Flags in ER register */
#define CAN_FLAGS_ER                      ((uint32_t)0x00F00000)

/* Mailboxes definition */
#define CAN_TXMAILBOX_0                   ((uint8_t)0x00)
#define CAN_TXMAILBOX_1                   ((uint8_t)0x01)
#define CAN_TXMAILBOX_2                   ((uint8_t)0x02)

/* CAN Master Control Register bit */
#define MCR_DBF                           ((uint32_t)0x00010000)


#define CAN_MODE_MASK                     ((uint32_t)0x00000003)
/**
  * @}
  */


/** @defgroup CAN_Private_Functions
  * @{
  */

/**
  * @brief  Deinitialize the CAN peripheral registers.
  * @param  CANx: where x:[1,2] to select the CAN peripheral.
  * @retval None.
  */
void CAN_DeInit(CAN_TypeDef *CANx)
{
    if (CANx == CAN1) {
        /* Force CAN1 reset state */
        RCC_APB1PeriphReset_Enable(RCC_APB1PERIPH_CAN1, ENABLE);
        /* Release CAN1 from reset state */
        RCC_APB1PeriphReset_Enable(RCC_APB1PERIPH_CAN1, DISABLE);
    } else {
        /* Force CAN2 reset state */
        RCC_APB1PeriphReset_Enable(RCC_APB1PERIPH_CAN2, ENABLE);
        /* Release CAN2 from reset state */
        RCC_APB1PeriphReset_Enable(RCC_APB1PERIPH_CAN2, DISABLE);
    }
}

/**
  * @brief  Initialize the CAN peripheral according to the CAN_InitParaStruct.
  * @param  CANx:where x:[1,2]
  * @param  CAN_InitParaStruct: contain the configuration information for the CAN peripheral.
  * @retval It will be returned the status of CAN_INITSTATE_FAILED or CAN_INITSTATE_SUCCESS.
  */
uint8_t CAN_Init(CAN_TypeDef *CANx, CAN_InitPara *CAN_InitParaStruct)
{
    uint32_t wait_ack = 0x00000000;

    /* Out of sleep mode */
    CANx->CTLR &= (~(uint32_t)CAN_CTLR_SWM);

    /* Enable initial working */
    CANx->CTLR |= CAN_CTLR_IWM ;

    /* Wait the acknowledge */
    while (((CANx->STR & CAN_STR_IWS) != CAN_STR_IWS) && (wait_ack != IWS_TIMEOUT)) {
        wait_ack++;
    }

    /* Check whether initial working is success */
    if ((CANx->STR & CAN_STR_IWS) != CAN_STR_IWS) {
        return CAN_INITSTATE_FAILED;
    } else {
        /* Set the time triggered communication mode */
        if (CAN_InitParaStruct->CAN_TTC == ENABLE) {
            CANx->CTLR |= CAN_CTLR_TTC;
        } else {
            CANx->CTLR &= ~(uint32_t)CAN_CTLR_TTC;
        }

        /* Set the automatic bus-off management */
        if (CAN_InitParaStruct->CAN_ABOR == ENABLE) {
            CANx->CTLR |= CAN_CTLR_ABOR;
        } else {
            CANx->CTLR &= ~(uint32_t)CAN_CTLR_ABOR;
        }

        /* Set the automatic wake-up mode */
        if (CAN_InitParaStruct->CAN_AWK == ENABLE) {
            CANx->CTLR |= CAN_CTLR_AWK;
        } else {
            CANx->CTLR &= ~(uint32_t)CAN_CTLR_AWK;
        }

        /* Set the automatic retransmission mode */
        if (CAN_InitParaStruct->CAN_ARD == ENABLE) {
            CANx->CTLR |= CAN_CTLR_ARD;
        } else {
            CANx->CTLR &= ~(uint32_t)CAN_CTLR_ARD;
        }

        /* Set receive FIFO overwrite mode */
        if (CAN_InitParaStruct->CAN_RFOD == ENABLE) {
            CANx->CTLR |= CAN_CTLR_RFOD;
        } else {
            CANx->CTLR &= ~(uint32_t)CAN_CTLR_RFOD;
        }

        /* Set the Transmit FIFO order */
        if (CAN_InitParaStruct->CAN_TFO == ENABLE) {
            CANx->CTLR |= CAN_CTLR_TFO;
        } else {
            CANx->CTLR &= ~(uint32_t)CAN_CTLR_TFO;
        }

        /* Set the bit timing register */
        CANx->BTR = (uint32_t)((uint32_t)CAN_InitParaStruct->CAN_Mode << 30) | \
                    ((uint32_t)CAN_InitParaStruct->CAN_SJW << 24) | \
                    ((uint32_t)CAN_InitParaStruct->CAN_BS1 << 16) | \
                    ((uint32_t)CAN_InitParaStruct->CAN_BS2 << 20) | \
                    ((uint32_t)CAN_InitParaStruct->CAN_Prescaler - 1);

        /* leave initialisation */
        CANx->CTLR &= ~(uint32_t)CAN_CTLR_IWM;

        /* Wait the acknowledge */
        wait_ack = 0;

        while (((CANx->STR & CAN_STR_IWS) == CAN_STR_IWS) && (wait_ack != IWS_TIMEOUT)) {
            wait_ack++;
        }

        /* Check whether qiut initial working mode */
        if ((CANx->STR & CAN_STR_IWS) == CAN_STR_IWS) {
            return CAN_INITSTATE_FAILED;
        } else {
            return CAN_INITSTATE_SUCCESS;
        }
    }

}

/**
  * @brief  Initialize the CAN peripheral according to the CAN_FilterInitStruct.
  * @param  CAN_FilterInitParaStruct:contain the information for the CAN peripheral.
  * @retval None.
  */
void CAN_FilterInit(CAN_FilterInitPara *CAN_FilterInitParaStruct)
{
    uint32_t filter_number = 0;

    filter_number = ((uint32_t)1) << CAN_FilterInitParaStruct->CAN_FilterNumber;

    /* Set filter lock disable */
    CAN1->FCTLR |= CAN_FCTLR_FLD;

    /* Disable Filter */
    CAN1->FWR &= ~(uint32_t)filter_number;

    /* Filter Scale */
    if (CAN_FilterInitParaStruct->CAN_FilterScale == CAN_FILTERSCALE_16BIT) {
        /* Set the filter 16-bit scale*/
        CAN1->FSR &= ~(uint32_t)filter_number;

        /* First 16-bit list and First 16-bit mask */
        /* Or First 16-bit list and Second 16-bit list */
        CAN1->FilterRegister[CAN_FilterInitParaStruct->CAN_FilterNumber].FD0R =
            ((0x0000FFFF & (uint32_t)CAN_FilterInitParaStruct->CAN_FilterMaskListLow) << 16) |
            (0x0000FFFF & (uint32_t)CAN_FilterInitParaStruct->CAN_FilterListLow);

        /* Second 16-bit identifier and Second 16-bit mask */
        /* Or Third 16-bit identifier and Fourth 16-bit identifier */
        CAN1->FilterRegister[CAN_FilterInitParaStruct->CAN_FilterNumber].FD1R =
            ((0x0000FFFF & (uint32_t)CAN_FilterInitParaStruct->CAN_FilterMaskListHigh) << 16) |
            (0x0000FFFF & (uint32_t)CAN_FilterInitParaStruct->CAN_FilterListHigh);
    }

    if (CAN_FilterInitParaStruct->CAN_FilterScale == CAN_FILTERSCALE_32BIT) {
        /* 32-bit scale for the filter */
        CAN1->FSR |= filter_number;
        /* 32-bit identifier or First 32-bit identifier */
        CAN1->FilterRegister[CAN_FilterInitParaStruct->CAN_FilterNumber].FD0R =
            ((0x0000FFFF & (uint32_t)CAN_FilterInitParaStruct->CAN_FilterListHigh) << 16) |
            (0x0000FFFF & (uint32_t)CAN_FilterInitParaStruct->CAN_FilterListLow);
        /* 32-bit mask or Second 32-bit identifier */
        CAN1->FilterRegister[CAN_FilterInitParaStruct->CAN_FilterNumber].FD1R =
            ((0x0000FFFF & (uint32_t)CAN_FilterInitParaStruct->CAN_FilterMaskListHigh) << 16) |
            (0x0000FFFF & (uint32_t)CAN_FilterInitParaStruct->CAN_FilterMaskListLow);
    }

    /* Filter Mode */
    if (CAN_FilterInitParaStruct->CAN_FilterMode == CAN_FILTERMODE_MASK) {
        /*Filter with Mask mode*/
        CAN1->FMR &= ~(uint32_t)filter_number;
    } else { /* CAN_FilterInitStruct->CAN_FilterMode == CAN_FILTERMODE_LIST */
        /*Filter with List mode*/
        CAN1->FMR |= (uint32_t)filter_number;
    }

    /* Filter associated with FIFO */
    if (CAN_FilterInitParaStruct->CAN_FilterFIFOAssociation == CAN_FILTER_FIFO0) {
        /* Filter associated with FIFO 0 */
        CAN1->FAFR &= ~(uint32_t)filter_number;
    }

    if (CAN_FilterInitParaStruct->CAN_FilterFIFOAssociation == CAN_FILTER_FIFO1) {
        /* Filter associated with FIFO 1 */
        CAN1->FAFR |= (uint32_t)filter_number;
    }

    /* Filter working */
    if (CAN_FilterInitParaStruct->CAN_FilterWork == ENABLE) {
        CAN1->FWR |= filter_number;
    }

    /* Exit the initialisation mode for the filter */
    CAN1->FCTLR &= ~CAN_FCTLR_FLD;
}

/**
  * @brief  Configure each CAN_InitParaStruct member with default value.
  * @param  CAN_InitParaStruct: pointer to a CAN_InitPara structure to initialize.
  * @retval None.
  */
void CAN_StructInit(CAN_InitPara *CAN_InitParaStruct)
{
    /* Set the time triggered communication mode */
    CAN_InitParaStruct->CAN_TTC = DISABLE;

    /* Set the automatic bus-off recovery */
    CAN_InitParaStruct->CAN_ABOR = DISABLE;

    /* Set the automatic wake up mode */
    CAN_InitParaStruct->CAN_AWK = DISABLE;

    /* Set the automatic retransmission disable */
    CAN_InitParaStruct->CAN_ARD = DISABLE;

    /* Set the receive FIFO overwrite mode */
    CAN_InitParaStruct->CAN_RFOD = DISABLE;

    /* Set the transmit FIFO order */
    CAN_InitParaStruct->CAN_TFO = DISABLE;

    /* Set the CAN_Mode member */
    CAN_InitParaStruct->CAN_Mode = CAN_MODE_NORMAL;

    /* Set the CAN_SJW member */
    CAN_InitParaStruct->CAN_SJW = CAN_SJW_1TQ;

    /* Set the CAN_BS1 member */
    CAN_InitParaStruct->CAN_BS1 = CAN_BS1_4TQ;

    /* Set the CAN_BS2 member */
    CAN_InitParaStruct->CAN_BS2 = CAN_BS2_3TQ;

    /* Set the CAN_Prescaler member */
    CAN_InitParaStruct->CAN_Prescaler = 1;
}

/**
  * @brief  Set header bank of CAN2 filter.
  * @param  CAN2_HeaderBankNumber: Select header bank of CAN2 filter.It can be 1 to 27.
  * @retval None.
  */
void CAN_HeaderBank(uint8_t CAN_HeaderBankNumber)
{
    /* Set filter lock disable */
    CAN1->FCTLR |= CAN_FCTLR_FLD;

    /* Select filter start number for slave CAN */
    CAN1->FCTLR &= (uint32_t)0xFFFFC0F1 ;
    CAN1->FCTLR |= (uint32_t)(CAN_HeaderBankNumber) << 8;

    /* Filter out the initialization mode */
    CAN1->FCTLR |= ~CAN_FCTLR_FLD;
}

/**
  * @brief  Enable or disable the Debug Freeze for CAN.
  * @param  CANx: where x :[1,2] for selecting the CAN peripheral.
  * @param  NewValue: new state of the CAN peripheral.
  *   This parameter can be: ENABLE or DISABLE.
  * @retval None.
  */
void CAN_DebugFreeze(CAN_TypeDef *CANx, TypeState NewValue)
{
    if (NewValue != DISABLE) {
        CANx->CTLR |= CAN_CTLR_DFZ;
    } else {
        CANx->CTLR &= ~CAN_CTLR_DFZ;
    }
}


/**
  * @brief  Enable or disabe the CAN Time Triggered communication mode.
  * @param  CANx:where x :[1,2] for selecting the CAN peripheral.
  * @param  NewValue : Mode new state , This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void CAN_TimeTrigComMode_Enable(CAN_TypeDef *CANx, TypeState NewValue)
{
    if (NewValue != DISABLE) {
        /* Enable the TTC mode */
        CANx->CTLR |= CAN_CTLR_TTC;

        /* Set TSEbits */
        CANx->TxMailBox[0].TMPR |= ((uint32_t)CAN_TMPR0_TSE);
        CANx->TxMailBox[1].TMPR |= ((uint32_t)CAN_TMPR1_TSE);
        CANx->TxMailBox[2].TMPR |= ((uint32_t)CAN_TMPR2_TSE);
    } else {
        /* Disable the TTC mode */
        CANx->CTLR &= (uint32_t)(~(uint32_t)CAN_CTLR_TTC);

        /* Reset TSE bits */
        CANx->TxMailBox[0].TMPR &= ((uint32_t)~CAN_TMPR0_TSE);
        CANx->TxMailBox[1].TMPR &= ((uint32_t)~CAN_TMPR1_TSE);
        CANx->TxMailBox[2].TMPR &= ((uint32_t)~CAN_TMPR2_TSE);
    }
}
/**
  * @brief  Initiate to transmit a message.
  * @param  CANx: where x:[1,2]to to select the CAN peripheral.
  * @param  TxMessage: contain CAN Id, CAN DLC and CAN data for the structure.
  * @retval The number of the mailbox that is used for transmission
  *         or CAN_TxState_NoMailBox if there is no empty mailbox.
  */
uint8_t CAN_Transmit(CAN_TypeDef *CANx, CanTxMessage *TxMessage)
{
    uint8_t transmit_mailbox_number = 0;

    /* Select one empty transmit mailbox */
    if ((CANx->TSTR & CAN_TSTR_TME0) == CAN_TSTR_TME0) {
        transmit_mailbox_number = 0;
    } else if ((CANx->TSTR & CAN_TSTR_TME1) == CAN_TSTR_TME1) {
        transmit_mailbox_number = 1;
    } else if ((CANx->TSTR & CAN_TSTR_TME2) == CAN_TSTR_TME2) {
        transmit_mailbox_number = 2;
    } else {
        transmit_mailbox_number = CAN_TXSTATE_NOMAILBOX;
    }

    if (transmit_mailbox_number != CAN_TXSTATE_NOMAILBOX) {
        /* Set up the Id */
        CANx->TxMailBox[transmit_mailbox_number].TMIR &= CAN_TMIR_TE;
        if (TxMessage->FF == CAN_FF_STANDARD) {
            CANx->TxMailBox[transmit_mailbox_number].TMIR |= ((TxMessage->StdId << 21) | \
                    TxMessage->FT);
        } else {
            CANx->TxMailBox[transmit_mailbox_number].TMIR |= ((TxMessage->ExtId << 3) | \
                    TxMessage->FF | \
                    TxMessage->FT);
        }

        /* Set up the DLC */
        TxMessage->DLC &= ((uint8_t)CAN_TMPR0_DLC);
        CANx->TxMailBox[transmit_mailbox_number].TMPR &= ((uint32_t)~CAN_TMPR0_DLC);
        CANx->TxMailBox[transmit_mailbox_number].TMPR |= TxMessage->DLC;

        /* Set up the data field */
        CANx->TxMailBox[transmit_mailbox_number].TMD0R = (((uint32_t)TxMessage->Data[3] << 24) |
                ((uint32_t)TxMessage->Data[2] << 16) |
                ((uint32_t)TxMessage->Data[1] << 8) |
                ((uint32_t)TxMessage->Data[0]));
        CANx->TxMailBox[transmit_mailbox_number].TMD1R = (((uint32_t)TxMessage->Data[7] << 24) |
                ((uint32_t)TxMessage->Data[6] << 16) |
                ((uint32_t)TxMessage->Data[5] << 8) |
                ((uint32_t)TxMessage->Data[4]));
        /* Request transmission */
        CANx->TxMailBox[transmit_mailbox_number].TMIR |= CAN_TMIR0_TE;
    }
    return transmit_mailbox_number;
}

/**
  * @brief  Check the state of message transmission.
  * @param  CANx: where x:[1,2] to to select the CAN peripheral.
  * @param  TransmitMailbox: the number of the used transmission mailbox.
  * @retval Return CAN_TXSTATE_OK or CAN_TXSTATE_FAILED.
  */
uint8_t CAN_TransmitState(CAN_TypeDef *CANx, uint8_t TransmitMailbox)
{
    uint32_t state = 0;

    switch (TransmitMailbox) {
    case (CAN_TXMAILBOX_0):
        state =   CANx->TSTR & (CAN_TSTR_MTF0 | CAN_TSTR_MTFNE0 | CAN_TSTR_TME0);
        break;
    case (CAN_TXMAILBOX_1):
        state =   CANx->TSTR & (CAN_TSTR_MTF1 | CAN_TSTR_MTFNE1 | CAN_TSTR_TME1);
        break;
    case (CAN_TXMAILBOX_2):
        state =   CANx->TSTR & (CAN_TSTR_MTF2 | CAN_TSTR_MTFNE2 | CAN_TSTR_TME2);
        break;
    default:
        state = CAN_TXSTATE_FAILED;
        break;
    }
    switch (state) {
    /* transmit pending  */
    case (0x0):
        state = CAN_TXSTATE_PENDING;
        break;
    /* transmit failed  */
    case (CAN_TSTR_MTF0 | CAN_TSTR_TME0):
        state = CAN_TXSTATE_FAILED;
        break;
    case (CAN_TSTR_MTF1 | CAN_TSTR_TME1):
        state = CAN_TXSTATE_FAILED;
        break;
    case (CAN_TSTR_MTF2 | CAN_TSTR_TME2):
        state = CAN_TXSTATE_FAILED;
        break;
    /* transmit succeeded  */
    case (CAN_TSTR_MTF0 | CAN_TSTR_MTFNE0 | CAN_TSTR_TME0):
        state = CAN_TXSTATE_OK;
        break;
    case (CAN_TSTR_MTF1 | CAN_TSTR_MTFNE1 | CAN_TSTR_TME1):
        state = CAN_TXSTATE_OK;
        break;
    case (CAN_TSTR_MTF2 | CAN_TSTR_MTFNE2 | CAN_TSTR_TME2):
        state = CAN_TXSTATE_OK;
        break;
    default:
        state = CAN_TXSTATE_FAILED;
        break;
    }
    return (uint8_t) state;
}

/**
  * @brief  Stop a transmit mission.
  * @param  CANx: where x:[1,2] to to select the CAN peripheral.
  * @param  Mailbox:  Mailbox number.
  * @retval None.
  */
void CAN_StopTransmit(CAN_TypeDef *CANx, uint8_t Mailbox)
{
    /* Stop transmission */
    switch (Mailbox) {
    case (CAN_TXMAILBOX_0):
        CANx->TSTR |= CAN_TSTR_MST0;
        break;
    case (CAN_TXMAILBOX_1):
        CANx->TSTR |= CAN_TSTR_MST1;
        break;
    case (CAN_TXMAILBOX_2):
        CANx->TSTR |= CAN_TSTR_MST2;
        break;
    default:
        break;
    }
}


/**
  * @brief  Receive a message.
  * @param  CANx: where x:[1,2] to to select the CAN peripheral.
  * @param  FIFONumber: Receive FIFO number, CAN_FIFO0 or CAN_FIFO1.
  * @param  RxMessage: a structure receive message which contains
  *                    CAN Id, CAN DLC, CAN datas and FI number.
  * @retval None.
  */
void CAN_Receive(CAN_TypeDef *CANx, uint8_t FIFONumber, CanRxMessage *RxMessage)
{
    /* Get the frame identifier */
    RxMessage->FF = (uint8_t)0x04 & CANx->FIFOMailBox[FIFONumber].RFMIR;
    if (RxMessage->FF == CAN_FF_STANDARD) {
        RxMessage->StdId = CAN_TMIR_StdId_Mask & (CANx->FIFOMailBox[FIFONumber].RFMIR >> 21);
    } else {
        RxMessage->ExtId = CAN_TMIR_ExtId_Mask & (CANx->FIFOMailBox[FIFONumber].RFMIR >> 3);
    }

    RxMessage->FT = (uint8_t)0x02 & CANx->FIFOMailBox[FIFONumber].RFMIR;
    /* Get the data length code */
    RxMessage->DLC = (uint8_t)0x0F & CANx->FIFOMailBox[FIFONumber].RFMPR;
    /* Get the filtering index */
    RxMessage->FI = (uint8_t)0xFF & (CANx->FIFOMailBox[FIFONumber].RFMPR >> 8);
    /* Get FIFO mailbox data */
    RxMessage->Data[0] = (uint8_t)0xFF & CANx->FIFOMailBox[FIFONumber].RFMD0R;
    RxMessage->Data[1] = (uint8_t)0xFF & (CANx->FIFOMailBox[FIFONumber].RFMD0R >> 8);
    RxMessage->Data[2] = (uint8_t)0xFF & (CANx->FIFOMailBox[FIFONumber].RFMD0R >> 16);
    RxMessage->Data[3] = (uint8_t)0xFF & (CANx->FIFOMailBox[FIFONumber].RFMD0R >> 24);
    RxMessage->Data[4] = (uint8_t)0xFF & CANx->FIFOMailBox[FIFONumber].RFMD1R;
    RxMessage->Data[5] = (uint8_t)0xFF & (CANx->FIFOMailBox[FIFONumber].RFMD1R >> 8);
    RxMessage->Data[6] = (uint8_t)0xFF & (CANx->FIFOMailBox[FIFONumber].RFMD1R >> 16);
    RxMessage->Data[7] = (uint8_t)0xFF & (CANx->FIFOMailBox[FIFONumber].RFMD1R >> 24);
    /* Release the FIFO */
    if (FIFONumber == CAN_FIFO0) {
        CANx->RFR0 |= CAN_RFR0_RFD0;
    }

    /* FIFONumber == CAN_FIFO1 */
    else {
        CANx->RFR1 |= CAN_RFR1_RFD1;
    }
}

/**
  * @brief  Dequeue the FIFO.
  * @param  CANx: where x:[1,2] to to select the CAN peripheral.
  * @param  FIFONumber: FIFO to dequeue, it can be CAN_FIFO0 or CAN_FIFO1.
  * @retval None.
  */
void CAN_FIFODequeue(CAN_TypeDef *CANx, uint8_t FIFONumber)
{

    if (FIFONumber == CAN_FIFO0) {
        /* Release FIFO 0 */
        CANx->RFR0 |= CAN_RFR0_RFD0;
    } else { /* FIFONumber == CAN_FIFO1 */
        /* Release FIFO1 */
        CANx->RFR1 |= CAN_RFR1_RFD1;
    }
}

/**
  * @brief  Return the length of receiving messages.
  * @param  CANx: where x:[1,2] to to select the CAN peripheral.
  * @param  FIFONumber: Receive FIFO number, CAN_FIFO0 or CAN_FIFO1.
  * @retval message_length : which is the Length of pending message.
  */
uint8_t CAN_MessageLength(CAN_TypeDef *CANx, uint8_t FIFONumber)
{
    uint8_t message_length = 0;
    if (FIFONumber == CAN_FIFO0) {
        message_length = (uint8_t)(CANx->RFR0 & (uint32_t)0x03);
    } else if (FIFONumber == CAN_FIFO1) {
        message_length = (uint8_t)(CANx->RFR1 & (uint32_t)0x03);
    } else {
        message_length = 0;
    }
    return message_length;
}


/**
  * @brief   Select the CAN Working mode.
  * @param CAN_WorkingMode : CAN Working Mode.It can be the following one.
  *     @arg CAN_WORKINGMODE_INITIAL
  *     @arg CAN_WORKINGMODE_NORMAL
  *     @arg CAN_WORKINGMODE_SLEEP
  * @retval state of the requested mode which can be
  *     @arg CAN_MODESTATE_FAILED
  *     @arg CAN_MODESTATE_SUCCESS
  */
uint8_t CAN_WorkingMode(CAN_TypeDef *CANx, uint8_t CAN_WorkingMode)
{
    uint8_t state = CAN_MODESTATE_FAILED;

    /* Timeout for IWS or also for SWS bits*/
    uint32_t timeout = IWS_TIMEOUT;

    if (CAN_WorkingMode == CAN_WORKINGMODE_INITIAL) {
        /* Set initialisation */
        CANx->CTLR = (uint32_t)((CANx->CTLR & (uint32_t)(~(uint32_t)CAN_CTLR_SWM)) | CAN_CTLR_IWM);

        /* Wait the acknowledge */
        while (((CANx->STR & CAN_MODE_MASK) != CAN_STR_IWS) && (timeout != 0)) {
            timeout--;
        }
        if ((CANx->STR & CAN_MODE_MASK) != CAN_STR_IWS) {
            state = CAN_MODESTATE_FAILED;
        } else {
            state = CAN_MODESTATE_SUCCESS;
        }
    } else  if (CAN_WorkingMode == CAN_WORKINGMODE_NORMAL) {
        /* Enter Normal mode */
        CANx->CTLR &= (uint32_t)(~(CAN_CTLR_SWM | CAN_CTLR_IWM));

        /* Wait the acknowledge */
        while (((CANx->STR & CAN_MODE_MASK) != 0) && (timeout != 0)) {
            timeout--;
        }
        if ((CANx->STR & CAN_MODE_MASK) != 0) {
            state = CAN_MODESTATE_FAILED;
        } else {
            state = CAN_MODESTATE_SUCCESS;
        }
    } else  if (CAN_WorkingMode == CAN_WORKINGMODE_SLEEP) {
        /* Set Sleep mode */
        CANx->CTLR = (uint32_t)((CANx->CTLR & (uint32_t)(~(uint32_t)CAN_CTLR_IWM)) | CAN_CTLR_SWM);

        /* Wait the acknowledge */
        while (((CANx->STR & CAN_MODE_MASK) != CAN_STR_SWS) && (timeout != 0)) {
            timeout--;
        }
        if ((CANx->STR & CAN_MODE_MASK) != CAN_STR_SWS) {
            state = CAN_MODESTATE_FAILED;
        } else {
            state = CAN_MODESTATE_SUCCESS;
        }
    } else {
        state = CAN_MODESTATE_FAILED;
    }

    return (uint8_t) state;
}

/**
  * @brief  Enter the Sleep mode.
  * @param  CANx: where x:[1,2] to to select the CAN peripheral.
  * @retval state: CAN_SLEEP_OK or CAN_SLEEP_FAILED.
  */
uint8_t CAN_EnterSleep(CAN_TypeDef *CANx)
{
    /* Set Sleep mode */
    CANx->CTLR = (((CANx->CTLR) & (uint32_t)(~(uint32_t)CAN_CTLR_IWM)) | CAN_CTLR_SWM);

    if ((CANx->STR & (CAN_STR_SWS | CAN_STR_IWS)) == CAN_STR_SWS) {
        /* Sleep mode entered success*/
        return (uint8_t)CAN_SLEEP_OK;
    } else {
        /* Sleep mode entered failure */
        return (uint8_t)CAN_SLEEP_FAILED;
    }
}

/**
  * @brief  Wake up CAN.
  * @param  CANx: where x:[1,2] to to select the CAN peripheral.
  * @retval status:  CAN_WAKEUP_OK or CAN_WAKEUP_FAILED.
  */
uint8_t CAN_WakeUp(CAN_TypeDef *CANx)
{
    uint32_t wait_sws = SWS_TIMEOUT;

    /*set wake up */
    CANx->CTLR &= ~(uint32_t)CAN_CTLR_SWM;

    /* Sleep mode state */
    while (((CANx->CTLR & CAN_CTLR_SWM) == CAN_CTLR_SWM) && (wait_sws != 0x00)) {
        wait_sws--;
    }
    if ((CANx->CTLR & CAN_CTLR_SWM) != CAN_CTLR_SWM) {
        /*Sleep mode exited */
        return (uint8_t)CAN_WAKEUP_OK;
    } else {
        /*Sleep mode exited failure */
        return (uint8_t)CAN_WAKEUP_FAILED;
    }
}


/**
  * @brief  Return the CANx's last error type (LEC).
  * @param  CANx: where x:[1,2] to to select the CAN peripheral.
  * @retval CAN_ErrorType: specify the Error type:
  *     @arg CAN_ERRORTYPE_NOERR
  *     @arg CAN_ERRORTYPE_STUFFERR
  *     @arg CAN_ERRORTYPE_FORMERR
  *     @arg CAN_ERRORTYPE_ACKERR
  *     @arg CAN_ERRORTYPE_BITRECESSIVEERR
  *     @arg CAN_ERRORTYPE_BITDOMINANTERR
  *     @arg CAN_ERRORTYPE_CRCERR
  *     @arg CAN_ERRORTYPE_SOFTWARESETERR
  */

uint8_t CAN_GetErrorType(CAN_TypeDef *CANx)
{
    uint8_t error_type = 0;

    /* Get the error type*/
    error_type = (((uint8_t)CANx->ER) & (uint8_t)CAN_ER_ET);

    /* Return the error type*/
    return error_type;
}

/**
  * @brief  Get the Counter of CANx Receive Error(REC).
  * @note   According to the error condition as defined by the CAN standard,
  *         the counter of CANx Receive Error is increased by 1 or by 8.
  *         When CAN received success everytime, the counter is decreased by 1
  *         or reset to 120 if its value was over than 128.
  *         When the counter value is over 127, the CAN controller enters the
  *         error passive state.
  * @param  CANx: where x:[1,2] to to select the CAN peripheral.
  * @retval CAN Receive Error Counter.
  */
uint8_t CAN_GetReceiveErrorCounter(CAN_TypeDef *CANx)
{
    uint8_t receive_counter = 0;

    /* Get the counter of CANx Receive Error*/
    receive_counter = (uint8_t)((CANx->ER & CAN_ER_REC) >> 24);

    /* Return the Receive Error Counter*/
    return receive_counter;
}


/**
  * @brief  Get the Counter of CANx Transmit Error (TEC).
  * @param  CANx: where x:[1,2] to to select the CAN peripheral.
  * @retval CAN Transmit Error Counter.
  */
uint8_t CAN_GetTransmitErrorCounter(CAN_TypeDef *CANx)
{
    uint8_t transmit_counter = 0;

    /* Get the Counter of CANx Transmit Error(TEC) */
    transmit_counter = (uint8_t)((CANx->ER & CAN_ER_TEC) >> 16);

    /* Return the Transmit Error Counter*/
    return transmit_counter;
}


/**
  * @brief  Enable or disable the specified CANx interrupts.
  * @param  CANx: where x:[1,2] to to select the CAN peripheral.
  * @param  CAN_INT: specify the CAN interrupt sources to be enabled or disabled.
  *   This parameter can be:
  *     @arg CAN_INT_TME
  *     @arg CAN_INT_RFNE0
  *     @arg CAN_INT_RFF0
  *     @arg CAN_INT_RFO0
  *     @arg CAN_INT_RFNE1
  *     @arg CAN_INT_RFF1
  *     @arg CAN_INT_RFO1
  *     @arg CAN_INT_WE
  *     @arg CAN_INT_PE
  *     @arg CAN_INT_BOE
  *     @arg CAN_INT_ET
  *     @arg CAN_INT_ERR
  *     @arg CAN_INT_WU
  *     @arg CAN_INT_SLP
  * @param  NewValue: new state of the CAN interrupts.
  *                   This parameter can be: ENABLE or DISABLE.
  * @retval None.
  */
void CAN_INTConfig(CAN_TypeDef *CANx, uint32_t CAN_INT, TypeState NewValue)
{
    if (NewValue != DISABLE) {
        /* Enable interrupt */
        CANx->IER |= CAN_INT;
    } else {
        /* Disable interrupt */
        CANx->IER &= ~CAN_INT;
    }
}
/**
  * @brief  Check whether the specified CAN flag is set or not.
  * @param  CANx: where x:[1,2] to to select the CAN peripheral.
  * @param  CAN_FLAG: specify the flag to check.
  *   This parameter can be one of the following flags:
  *     @arg CAN_FLAG_WE:   Warning error Flag
  *     @arg CAN_FLAG_PE:   Passive error Flag
  *     @arg CAN_FLAG_BOE:  Bus-off error Flag
  *     @arg CAN_FLAG_MTF0: Mailbox 0 transmit finished Flag
  *     @arg CAN_FLAG_MTF1: Mailbox 1 transmit finished Flag
  *     @arg CAN_FLAG_MTF2: Mailbox 2 transmit finished Flag
  *     @arg CAN_FLAG_RFL0: the length of the receive FIFO0 Flag
  *     @arg CAN_FLAG_RFF0: Receive FIFO 0 full Flag
  *     @arg CAN_FLAG_RFO0: Receive FIFO 0 overfull  Flag
  *     @arg CAN_FLAG_RFL1: the length of the receive FIFO1 Flag
  *     @arg CAN_FLAG_RFF1: Receive FIFO 1 full Flag
  *     @arg CAN_FLAG_RFO1: Receive FIFO 0 overfull  Flag
  *     @arg CAN_FLAG_WU:   Wake up Flag
  *     @arg CAN_FLAG_SLP:  Sleep working state Flag
  *     @arg CAN_FLAG_ET:   Error type Flag
  * @retval The new state of CAN_FLAG (SET or RESET).
  */
TypeState CAN_GetBitState(CAN_TypeDef *CANx, uint32_t CAN_FLAG)
{
    if ((CAN_FLAG & CAN_FLAGS_ER) != (uint32_t)RESET) {
        /* Check the state of the specified CAN flag */
        if ((CANx->ER & (CAN_FLAG & CAN_FLAG_Mask)) != (uint32_t)RESET) {
            /* CAN_FLAG is set */
            return SET;
        } else {
            /* CAN_FLAG is reset */
            return RESET;
        }
    } else if ((CAN_FLAG & CAN_FLAGS_STR) != (uint32_t)RESET) {
        /* Check the state of the specified CAN flag */
        if ((CANx->STR & (CAN_FLAG & CAN_FLAG_Mask)) != (uint32_t)RESET) {
            /* CAN_FLAG is set */
            return SET;
        } else {
            /* CAN_FLAG is reset */
            return RESET;
        }
    } else if ((CAN_FLAG & CAN_FLAGS_TSTR) != (uint32_t)RESET) {
        /* Check the state of the specified CAN flag */
        if ((CANx->TSTR & (CAN_FLAG & CAN_FLAG_Mask)) != (uint32_t)RESET) {
            /* CAN_FLAG is set */
            return SET;
        } else {
            /* CAN_FLAG is reset */
            return RESET;
        }
    } else if ((CAN_FLAG & CAN_FLAGS_RFR0) != (uint32_t)RESET) {
        /* Check the state of the specified CAN flag */
        if ((CANx->RFR0 & (CAN_FLAG & CAN_FLAG_Mask)) != (uint32_t)RESET) {
            /* CAN_FLAG is set */
            return SET;
        } else {
            /* CAN_FLAG is reset */
            return RESET;
        }
    }
    /* If(CAN_FLAG & CAN_FLAGS_RFR1 != (uint32_t)RESET) */
    else {
        /* Check the state of the specified CAN flag */
        if ((uint32_t)(CANx->RFR1 & (CAN_FLAG & CAN_FLAG_Mask)) != (uint32_t)RESET) {
            /* CAN_FLAG is set */
            return SET;
        } else {
            /* CAN_FLAG is reset */
            return RESET;
        }
    }
}

/**
  * @brief  Clear the CAN's flags.
  * @param  CANx: where x:[1,2] to to select the CAN peripheral.
  * @param  CAN_FLAG: specify the flag to clear.
  *   This parameter can be one of the following flags:
  *     @arg CAN_FLAG_MTF0: Mailbox 0 transmit finished Flag
  *     @arg CAN_FLAG_MTF1: Mailbox 1 transmit finished Flag
  *     @arg CAN_FLAG_MTF2: Mailbox 2 transmit finished Flag
  *     @arg CAN_FLAG_RFF0: Receive FIFO 0 full Flag
  *     @arg CAN_FLAG_RFO0: Receive FIFO 0 overfull  Flag
  *     @arg CAN_FLAG_RFF1: Receive FIFO 1 full Flag
  *     @arg CAN_FLAG_RFO1: Receive FIFO 0 overfull  Flag
  *     @arg CAN_FLAG_WU:   Wake up Flag
  *     @arg CAN_FLAG_SLP:  Sleep working state Flag
  *     @arg CAN_FLAG_ET:   Error type Flag
  * @retval None.
  */
void CAN_ClearBitState(CAN_TypeDef *CANx, uint32_t CAN_FLAG)
{
    uint32_t temp = 0;

    /* ER register */
    if (CAN_FLAG == CAN_FLAG_ET) {
        /* Clear the selected CAN flags */
        CANx->ER = (uint32_t)RESET;
    }
    /* STR or TSTR or RFR0 or RFR1 */
    else {
        temp = CAN_FLAG & CAN_FLAG_Mask;

        if ((CAN_FLAG & CAN_FLAGS_RFR0) != (uint32_t)RESET) {
            /* Receive Flags */
            CANx->RFR0 = (uint32_t)(temp);
        } else if ((CAN_FLAG & CAN_FLAGS_RFR1) != (uint32_t)RESET) {
            /* Receive Flags */
            CANx->RFR1 = (uint32_t)(temp);
        } else if ((CAN_FLAG & CAN_FLAGS_TSTR) != (uint32_t)RESET) {
            /* Transmit Flags */
            CANx->TSTR = (uint32_t)(temp);
        }
        /* If((CAN_FLAG & CAN_FLAGS_STR)!=(uint32_t)RESET) */
        else {
            CANx->STR = (uint32_t)(temp);
        }
    }
}

/**
  * @brief  Check whether the specified CANx interrupt has occurred or not.
  * @param  CANx: where x:[1,2] to to select the CAN peripheral.
  * @param  CAN_INT:  specify the CAN interrupt source to check.
  *   This parameter can be:
  *     @arg CAN_INT_TME
  *     @arg CAN_INT_RFNE0
  *     @arg CAN_INT_RFF0
  *     @arg CAN_INT_RFO0
  *     @arg CAN_INT_RFNE1
  *     @arg CAN_INT_RFF1
  *     @arg CAN_INT_RFO1
  *     @arg CAN_INT_WE
  *     @arg CAN_INT_PE
  *     @arg CAN_INT_BOE
  *     @arg CAN_INT_ET
  *     @arg CAN_INT_ERR
  *     @arg CAN_INT_WU
  *     @arg CAN_INT_SLP
  * @retval The current state of CAN_INT (SET or RESET).
  */
TypeState CAN_GetIntBitState(CAN_TypeDef *CANx, uint32_t CAN_INT)
{
    TypeState intstate = RESET;

    /* Get the enable interrupt bit */
    if ((CANx->IER & CAN_INT) != RESET) {
        switch (CAN_INT) {
        case CAN_INT_TME:
            /* Check CAN_TSTR_MTFx bits */
            intstate = CheckINTState(CANx->TSTR, CAN_TSTR_MTF0 | CAN_TSTR_MTF1 | CAN_TSTR_MTF2);
            break;
        case CAN_INT_RFNE0:
            /* Check CAN_RFR0_RFL0 bit */
            intstate = CheckINTState(CANx->RFR0, CAN_RFR0_RFL0);
            break;
        case CAN_INT_RFF0:
            /* Check CAN_RFR0_RFF0 bit */
            intstate = CheckINTState(CANx->RFR0, CAN_RFR0_RFF0);
            break;
        case CAN_INT_RFO0:
            /* Check CAN_RFR0_RFO0 bit */
            intstate = CheckINTState(CANx->RFR0, CAN_RFR0_RFO0);
            break;
        case CAN_INT_RFNE1:
            /* Check CAN_RFR1_RFL1 bit */
            intstate = CheckINTState(CANx->RFR1, CAN_RFR1_RFL1);
            break;
        case CAN_INT_RFF1:
            /* Check CAN_RFR1_RFF1 bit */
            intstate = CheckINTState(CANx->RFR1, CAN_RFR1_RFF1);
            break;
        case CAN_INT_RFO1:
            /* Check CAN_RFR1_RFO1 bit */
            intstate = CheckINTState(CANx->RFR1, CAN_RFR1_RFO1);
            break;
        case CAN_INT_WU:
            /* Check CAN_STR_WIF bit */
            intstate = CheckINTState(CANx->STR, CAN_STR_WIF);
            break;
        case CAN_INT_SLP:
            /* Check CAN_STR_SEIF bit */
            intstate = CheckINTState(CANx->STR, CAN_STR_SEIF);
            break;
        case CAN_INT_WE:
            /* Check CAN_INT_WE bit */
            intstate = CheckINTState(CANx->ER, CAN_ER_WE);
            break;
        case CAN_INT_PE:
            /* Check CAN_INT_EP bit */
            intstate = CheckINTState(CANx->ER, CAN_ER_PE);
            break;
        case CAN_INT_BOE:
            /* Check CAN_ER_BOE bit */
            intstate = CheckINTState(CANx->ER, CAN_ER_BOE);
            break;
        case CAN_INT_ET:
            /* Check CAN_ER_ET bit */
            intstate = CheckINTState(CANx->ER, CAN_ER_ET);
            break;
        case CAN_INT_ERR:
            /* Check CAN_STR_EIF bit */
            intstate = CheckINTState(CANx->STR, CAN_STR_EIF);
            break;
        default :
            /* in case of error, return RESET */
            intstate = RESET;
            break;
        }
    } else {
        /* in case the Interrupt is not enabled, return RESET */
        intstate  = RESET;
    }

    /* Return the CAN_INT status */
    return  intstate;
}

/**
  * @brief  Clear the CANx's interrupt pending bits.
  * @param  CANx: where x:[1,2] to to select the CAN peripheral.
  * @param  CAN_INT: specify the interrupt pending bit to clear.
  *   This parameter can be:
  *     @arg CAN_INT_TME
  *     @arg CAN_INT_RFF0
  *     @arg CAN_INT_RFO0
  *     @arg CAN_INT_RFF1
  *     @arg CAN_INT_RFO1
  *     @arg CAN_INT_WE
  *     @arg CAN_INT_PE
  *     @arg CAN_INT_BOE
  *     @arg CAN_INT_ET
  *     @arg CAN_INT_ERR
  *     @arg CAN_INT_WU
  *     @arg CAN_INT_SLP
  * @retval None.
  */
void CAN_ClearIntBitState(CAN_TypeDef *CANx, uint32_t CAN_INT)
{
    switch (CAN_INT) {
    case CAN_INT_TME:
        /* Clear CAN_TSTR_MTFx (rc_w1)*/
        CANx->TSTR = CAN_TSTR_MTF0 | CAN_TSTR_MTF1 | CAN_TSTR_MTF2;
        break;
    case CAN_INT_RFF0:
        /* Clear CAN_RFR0_RFF0 (rc_w1)*/
        CANx->RFR0 = CAN_RFR0_RFF0;
        break;
    case CAN_INT_RFO0:
        /* Clear CAN_RFR0_RFO0 (rc_w1)*/
        CANx->RFR0 = CAN_RFR0_RFO0;
        break;
    case CAN_INT_RFF1:
        /* Clear CAN_RFR1_RFF1 (rc_w1)*/
        CANx->RFR1 = CAN_RFR1_RFF1;
        break;
    case CAN_INT_RFO1:
        /* Clear CAN_RFR1_RFO1 (rc_w1)*/
        CANx->RFR1 = CAN_RFR1_RFO1;
        break;
    case CAN_INT_WU:
        /* Clear CAN_STR_WIF (rc_w1)*/
        CANx->STR = CAN_STR_WIF;
        break;
    case CAN_INT_SLP:
        /* Clear CAN_STR_SEIF (rc_w1)*/
        CANx->STR = CAN_STR_SEIF;
        break;
    case CAN_INT_WE:
        /* Clear CAN_STR_EIF (rc_w1) */
        CANx->STR = CAN_STR_EIF;
        break;
    case CAN_INT_PE:
        /* Clear CAN_MSR_ERRI (rc_w1) */
        CANx->STR = CAN_STR_EIF;
        break;
    case CAN_INT_BOE:
        /* Clear CAN_STR_EIF (rc_w1) */
        CANx->STR = CAN_STR_EIF;
        break;
    case CAN_INT_ET:
        /*  Clear ET bits */
        CANx->ER = RESET;
        /* Clear CAN_STR_EIF (rc_w1) */
        CANx->STR = CAN_STR_EIF;
        break;
    case CAN_INT_ERR:
        /*Clear ET bits */
        CANx->ER = RESET;
        /* Clear CAN_STR_EIF (rc_w1) */
        CANx->STR = CAN_STR_EIF;
        break;
    default :
        break;
    }
}

/**
  * @brief  Check whether the CAN interrupt has occurred or not.
  * @param  CAN_Reg: the register of CAN interrupt to check.
  * @param  Int_Bit: the bit of interrupt source to check.
  * @retval The new state of the CAN Interrupt (SET or RESET).
  */
static TypeState CheckINTState(uint32_t CAN_Reg, uint32_t Int_Bit)
{
    if ((CAN_Reg & Int_Bit) != (uint32_t)RESET) {
        /* CAN_INT is set */
        return SET;
    } else {
        /* CAN_IT is reset */
        return RESET;
    }
}


/**
  * @}
  */

/**
  * @}
  */

/**
  * @}
  */