summaryrefslogtreecommitdiff
path: root/bsp/apm32/libraries/APM32F0xx_Library/APM32F0xx_StdPeriphDriver/src/apm32f0xx_can.c
blob: f108be7c7eabd7e6a3c15b0599a5f129981e64b1 (plain)
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
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
/*!
 * @file        apm32f0xx_can.c
 *
 * @brief       This file contains all the functions for the CAN peripheral
 *
 * @version     V1.0.3
 *
 * @date        2022-09-20
 *
 * @attention
 *
 *  Copyright (C) 2020-2022 Geehy Semiconductor
 *
 *  You may not use this file except in compliance with the
 *  GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
 *
 *  The program is only for reference, which is distributed in the hope
 *  that it will be useful and instructional for customers to develop
 *  their software. Unless required by applicable law or agreed to in
 *  writing, the program is distributed on an "AS IS" BASIS, WITHOUT
 *  ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
 *  and limitations under the License.
 */

/* include */
#include "apm32f0xx_can.h"
#include "apm32f0xx_rcm.h"

/** @addtogroup APM32F0xx_StdPeriphDriver
  @{
*/

/** @addtogroup CAN_Driver
  @{
*/

/** @defgroup CAN_Macros Macros
  @{
*/

/**@} end of group CAN_Macros */

/** @defgroup CAN_Enumerations Enumerations
  @{
*/

/**@} end of group CAN_Enumerations */

/** @defgroup CAN_Structures Structures
  @{
*/

/**@} end of group CAN_Structures */

/** @defgroup CAN_Variables Variables
  @{
*/

/**@} end of group CAN_Variables */

/** @defgroup CAN_Functions Functions
  @{
*/

/*!
 * @brief       Reset CAN peripheral registers to their default values.
 *
 * @param       None
 *
 * @retval      None
 *
 */
void CAN_Reset(void)
{
    RCM_EnableAPB1PeriphReset(RCM_APB1_PERIPH_CAN);
    RCM_DisableAPB1PeriphReset(RCM_APB1_PERIPH_CAN);
}

/*!
 * @brief       Congfig the CAN peripheral using the specified parameters in the canConfig.
 *
 * @param       canConfig : pointer to a CAN_Config_T structure that contains
 *                          the configuration information for the CAN peripheral.
 *
 * @retval      Configuration status (SET or RESET)
 */
uint8_t CAN_Config(CAN_Config_T* canConfig)
{
    uint8_t InitStatus = RESET;
    uint32_t ackWait = 0x00000000;

    CAN->MCTRL_B.SLEEPREQ = BIT_RESET;

    CAN->MCTRL_B.INITREQ = BIT_SET;

    while ((CAN->MSTS_B.INITFLG != BIT_SET) && (ackWait != (uint32_t)0x00FFFFFF))
    {
        ackWait++;
    }

    if (CAN->MSTS_B.INITFLG != BIT_SET)
    {
        InitStatus = RESET;
    }
    else
    {
        if (canConfig->timeTrigComMode == ENABLE)
        {
            CAN->MCTRL_B.TTCM = BIT_SET;
        }
        else
        {
            CAN->MCTRL_B.TTCM = BIT_RESET;
        }

        if (canConfig->autoBusOffManage == ENABLE)
        {
            CAN->MCTRL_B.ALBOFFM = BIT_SET;
        }
        else
        {
            CAN->MCTRL_B.ALBOFFM = BIT_RESET;
        }

        if (canConfig->autoWakeUpMode == ENABLE)
        {
            CAN->MCTRL_B.AWUPCFG = BIT_SET;
        }
        else
        {
            CAN->MCTRL_B.AWUPCFG = BIT_RESET;
        }

        if (canConfig->nonAutoRetran == ENABLE)
        {
            CAN->MCTRL_B.ARTXMD = BIT_SET;
        }
        else
        {
            CAN->MCTRL_B.ARTXMD = BIT_RESET;
        }

        if (canConfig->rxFIFOLockMode == ENABLE)
        {
            CAN->MCTRL_B.RXFLOCK = BIT_SET;
        }
        else
        {
            CAN->MCTRL_B.RXFLOCK = BIT_RESET;
        }

        if (canConfig->txFIFOPriority == ENABLE)
        {
            CAN->MCTRL_B.TXFPCFG = BIT_SET;
        }
        else
        {
            CAN->MCTRL_B.TXFPCFG = BIT_RESET;
        }

        CAN->BITTIM = (uint32_t)canConfig->mode << 30;
        CAN->BITTIM_B.RSYNJW     = canConfig->syncJumpWidth;
        CAN->BITTIM_B.TIMSEG1 = canConfig->timeSegment1;
        CAN->BITTIM_B.TIMSEG2 = canConfig->timeSegment2;
        CAN->BITTIM_B.BRPSC     = canConfig->prescaler - 1;

        CAN->MCTRL_B.INITREQ = BIT_RESET;

        ackWait = 0x00000000;

        while ((CAN->MSTS_B.INITFLG == BIT_SET) && (ackWait != (uint32_t)0x00FFFFFF))
        {
            ackWait++;
        }

        if (CAN->MSTS_B.INITFLG == BIT_RESET)
        {
            InitStatus = SET;
        }
        else
        {
            InitStatus = RESET;
        }
    }
    return InitStatus;
}

/*!
 * @brief       Congfig the CAN filter according to the specified parameters in the filterConfig.
 *
 * @param       filterConfig: pointer to a CAN_Filter_Config_T structure that
 *                            contains the configuration information.
 *
 * @retval      None
 */
void CAN_ConfigFilter(CAN_FilterConfig_T* filterConfig)
{
    uint16_t filterNumber = 0;

    filterNumber = BIT0 << filterConfig->filterNumber;

    CAN->FCTRL_B.FIM = BIT_SET;

    CAN->FACT &= ~filterNumber;

    if (filterConfig->filterScale == CAN_FILTER_SCALE_16BIT)
    {
        CAN->FSCFG &= ~filterNumber;

        CAN->sFilterRegister[filterConfig->filterNumber].R1 =
            ((0x0000FFFF & filterConfig->filterMaskIdLow) << 16) |
            (0x0000FFFF & filterConfig->filterIdLow);

        CAN->sFilterRegister[filterConfig->filterNumber].R2 =
            ((0x0000FFFF & filterConfig->filterMaskIdHigh) << 16) |
            (0x0000FFFF & filterConfig->filterIdHigh);
    }

    if (filterConfig->filterScale == CAN_FILTER_SCALE_32BIT)
    {
        CAN->FSCFG |= filterNumber;

        CAN->sFilterRegister[filterConfig->filterNumber].R1 =
            ((0x0000FFFF & filterConfig->filterIdHigh) << 16) |
            (0x0000FFFF & filterConfig->filterIdLow);

        CAN->sFilterRegister[filterConfig->filterNumber].R2 =
            ((0x0000FFFF & filterConfig->filterMaskIdHigh) << 16) |
            (0x0000FFFF & filterConfig->filterMaskIdLow);
    }

    if (filterConfig->filterMode == CAN_FILTER_MODE_IDMASK)
    {
        CAN->FMCFG &= ~filterNumber;
    }
    else
    {
        CAN->FMCFG |= filterNumber;
    }

    if (filterConfig->filterFIFO == CAN_FIFO_0)
    {
        CAN->FFASS &= ~filterNumber;
    }
    else
    {
        CAN->FFASS |= filterNumber;
    }

    if (filterConfig->filterActivation == ENABLE)
    {
        CAN->FACT |= filterNumber;
    }

    CAN->FCTRL_B.FIM = BIT_RESET;
}

/*!
 * @brief   Fills each canConfig member with its default value.
 *
 * @param   canConfig : Pointer to a CAN_Config_T structure which is used for configuration.
 *
 * @retval  None
 */
void CAN_ConfigStructInit(CAN_Config_T* canConfig)
{
    canConfig->timeTrigComMode  = DISABLE;

    canConfig->autoBusOffManage  = DISABLE;

    canConfig->autoWakeUpMode   = DISABLE;

    canConfig->nonAutoRetran   = DISABLE;

    canConfig->rxFIFOLockMode = DISABLE;

    canConfig->txFIFOPriority  = DISABLE;

    canConfig->mode = CAN_MODE_NORMAL;

    canConfig->syncJumpWidth = CAN_SJW_1;

    canConfig->timeSegment1 = CAN_TIME_SEGMENT1_4;

    canConfig->timeSegment2 = CAN_TIME_SEGMENT2_3;

    canConfig->prescaler = 1;
}

/*!
 * @brief     Select the start bank filter for slave CAN.
 *
 * @param     bankNumber:Select the start slave bank filter from 1 to 27.
 *
 * @retval    None
 *
 * @note      This function applies only to APM32 Connectivity line devices.
 */
void CAN_StartBankSlave(uint8_t bankNumber)
{
    CAN->FCTRL_B.FIM = BIT_SET;

    CAN->FCTRL &= 0xFFFFC0F1;
    CAN->FCTRL |= (uint32_t)(bankNumber) << 8;

    CAN->FCTRL_B.FIM = BIT_RESET;
}

/*!
 * @brief      Enable the Debug Freeze mdoe for CAN.
 *
 * @param      None
 *
 * @retval     None
 */
void CAN_EnableDebugFreeze(void)
{
    CAN->MCTRL_B.DBGFRZE = BIT_SET;
}

/*!
 * @brief      Disable the Debug Freeze mode for CAN.
 *
 * @param      None
 *
 * @retval     None
 */
void CAN_DisableDebugFreeze(void)
{
    CAN->MCTRL_B.DBGFRZE = BIT_RESET;
}

/*!
 * @brief      Enables the Time Triggered Communication mode for CAN.
 *
 * @param      None
 *
 * @retval     None
 */
void CAN_EnableTTComMode(void)
{
    CAN->MCTRL_B.TTCM = BIT_SET;

    CAN->sTxMailBox[0].TXDLEN_B.TXGT = BIT_SET;
    CAN->sTxMailBox[1].TXDLEN_B.TXGT = BIT_SET;
    CAN->sTxMailBox[2].TXDLEN_B.TXGT = BIT_SET;
}

/*!
 * @brief      Disables the Time Triggered Communication mode for CAN.
 *
 * @param      None
 *
 * @retval     None
 */
void CAN_DisableTTComMode(void)
{
    CAN->MCTRL_B.TTCM = BIT_RESET;

    CAN->sTxMailBox[0].TXDLEN_B.TXGT = BIT_RESET;
    CAN->sTxMailBox[1].TXDLEN_B.TXGT = BIT_RESET;
    CAN->sTxMailBox[2].TXDLEN_B.TXGT = BIT_RESET;
}

/*!
 * @brief     Transmit a message from TxMessage of CAN_Tx_Message structure.
 *
 * @param     TxMessage : Pointer to a structure which contains CAN Id, CAN DLCODE and CAN data.
 *
 * @retval    Returns the number of the transmit mailbox state.It can be one of value:
 *                 @arg 0: Transmit mailbox 0
 *                 @arg 1: Transmit mailbox 1
 *                 @arg 2: Transmit mailbox 2
 *                 @arg 4: There is no empty mailbox
 */
uint8_t CAN_TxMessage(CAN_Tx_Message* TxMessage)
{
    uint8_t TxMailbox = 0;

    if ((CAN->TXSTS_B.TXMEFLG0 == BIT_SET))
    {
        TxMailbox = 0;
    }
    else if ((CAN->TXSTS_B.TXMEFLG1 == BIT_SET))
    {
        TxMailbox = 1;
    }
    else if ((CAN->TXSTS_B.TXMEFLG2 == BIT_SET))
    {
        TxMailbox = 2;
    }
    else
    {
        TxMailbox = CAN_TX_MAILBOX_FULL;
    }

    if (TxMailbox != CAN_TX_MAILBOX_FULL)
    {
        /* Set up the Id */
        CAN->sTxMailBox[TxMailbox].TXMID &= BIT0;
        if (TxMessage->typeID == CAN_TYPEID_STD)
        {
            CAN->sTxMailBox[TxMailbox].TXMID |= (((uint32_t)TxMessage->stanID << 21) | \
                                                 TxMessage->typeID        | \
                                                 TxMessage->remoteTxReq);
        }
        else
        {
            CAN->sTxMailBox[TxMailbox].TXMID |= (((uint32_t)TxMessage->extenID << 3) | \
                                                 TxMessage->typeID       | \
                                                 TxMessage->remoteTxReq);
        }

        /* Set up the DLCODE */
        CAN->sTxMailBox[TxMailbox].TXDLEN_B.DLCODE = (TxMessage->dataLengthCode & 0x0F);

        /* Set up the data field */
        CAN->sTxMailBox[TxMailbox].TXMDH_B.DATABYTE7 = TxMessage->data[7];
        CAN->sTxMailBox[TxMailbox].TXMDH_B.DATABYTE6 = TxMessage->data[6];
        CAN->sTxMailBox[TxMailbox].TXMDH_B.DATABYTE5 = TxMessage->data[5];
        CAN->sTxMailBox[TxMailbox].TXMDH_B.DATABYTE4 = TxMessage->data[4];

        CAN->sTxMailBox[TxMailbox].TXMDL_B.DATABYTE3 = TxMessage->data[3];
        CAN->sTxMailBox[TxMailbox].TXMDL_B.DATABYTE2 = TxMessage->data[2];
        CAN->sTxMailBox[TxMailbox].TXMDL_B.DATABYTE1 = TxMessage->data[1];
        CAN->sTxMailBox[TxMailbox].TXMDL_B.DATABYTE0 = TxMessage->data[0];

        /* Request transmission */
        CAN->sTxMailBox[TxMailbox].TXMID |= BIT0;
    }

    return TxMailbox;
}

/*!
 * @brief     Reads the status of a transmission.
 *
 * @param     TxMailbox: the number of the mailbox
 *              This parameter can be one of the following values:
 *              @arg CAN_MAILBOX_0 : Transmit mailbox 0
 *              @arg CAN_MAILBOX_1 : Transmit mailbox 1
 *              @arg CAN_MAILBOX_2 : Transmit mailbox 2
 *
 * @retval    Returns the transmission state.
 *              It can be one of the following values:
 *              @arg CAN_TX_FAILED
 *              @arg CAN_TX_OK
 *              @arg CAN_TX_WAITING
 */
uint8_t CAN_TxMessageStatus(CAN_MAILBOX_T TxMailbox)
{
    uint8_t state = CAN_TX_FAILED;

    switch (TxMailbox)
    {
        case (CAN_MAILBOX_0):
            if (CAN->TXSTS_B.REQCFLG0 && CAN->TXSTS_B.TXSUSFLG0 && CAN->TXSTS_B.TXMEFLG0)
            {
                state = CAN_TX_OK;
            }
            else if (!(CAN->TXSTS_B.REQCFLG0 || CAN->TXSTS_B.TXSUSFLG0 || CAN->TXSTS_B.TXMEFLG0))
            {
                state = CAN_TX_WAITING;
            }
            break;

        case (CAN_MAILBOX_1):
            if (CAN->TXSTS_B.REQCFLG1 && CAN->TXSTS_B.TXSUSFLG1 && CAN->TXSTS_B.TXMEFLG1)
            {
                state = CAN_TX_OK;
            }
            else if (!(CAN->TXSTS_B.REQCFLG1 || CAN->TXSTS_B.TXSUSFLG1 || CAN->TXSTS_B.TXMEFLG1))
            {
                state = CAN_TX_WAITING;
            }
            break;

        case (CAN_MAILBOX_2):
            if (CAN->TXSTS_B.REQCFLG2 && CAN->TXSTS_B.TXSUSFLG2 && CAN->TXSTS_B.TXMEFLG2)
            {
                state = CAN_TX_OK;
            }
            else if (!(CAN->TXSTS_B.REQCFLG2 || CAN->TXSTS_B.TXSUSFLG2 || CAN->TXSTS_B.TXMEFLG2))
            {
                state = CAN_TX_WAITING;
            }
            break;

        default:
            break;
    }

    return state;
}

/*!
 * @brief     Cancels a transmit request.
 *
 * @param     TxMailbox: the number of the mailbox
 *              This parameter can be one of the following values:
 *                @arg CAN_MAILBOX_0 : Abort transmission request for mailbox 0
 *                @arg CAN_MAILBOX_1 : Abort transmission request for mailbox 1
 *                @arg CAN_MAILBOX_2 : Abort transmission request for mailbox 2
 *
 * @retval    None
 */
void CAN_CancelTx(CAN_MAILBOX_T TxMailbox)
{
    switch (TxMailbox)
    {
        case 0:
            CAN->TXSTS_B.ABREQFLG0 = BIT_SET;
            break;

        case 1:
            CAN->TXSTS_B.ABREQFLG1 = BIT_SET;
            break;

        case 2:
            CAN->TXSTS_B.ABREQFLG2 = BIT_SET;
            break;

        default:
            break;
    }
}

/*!
 * @brief     Receive a valid message through the filter.
 *
 * @param     FIFONumber: The number of Receive FIFO
 *              This parameter can be one of the following values:
 *                @arg CAN_FIFO_0 : CAN FIFO 0 used to receive
 *                @arg CAN_FIFO_1 : CAN FIFO 1 used to receive
 *
 * @param     RxMessage : pointer to a structure that is used to receive message
 *
 * @retval    None
 */
void CAN_RxMessage(uint8_t FIFONumber, CAN_Rx_Message* RxMessage)
{
    RxMessage->typeID = (CAN_TYPEID_T)(BIT2 & CAN->sRxFIFO[FIFONumber].RXMID);

    if (RxMessage->typeID == CAN_TYPEID_STD)
    {
        RxMessage->stanID = CAN->sRxFIFO[FIFONumber].RXMID_B.STDID;
    }
    else
    {
        RxMessage->extenID = (CAN->sRxFIFO[FIFONumber].RXMID_B.STDID << 18);
        RxMessage->extenID |= CAN->sRxFIFO[FIFONumber].RXMID_B.EXTID;
    }

    RxMessage->remoteTxReq = (CAN_RTXR_T)(BIT1 & CAN->sRxFIFO[FIFONumber].RXMID);
    RxMessage->dataLengthCode = CAN->sRxFIFO[FIFONumber].RXDLEN_B.DLCODE;
    RxMessage->filterMatchIndex = CAN->sRxFIFO[FIFONumber].RXDLEN_B.FMIDX;

    RxMessage->data[7] = CAN->sRxFIFO[FIFONumber].RXMDH_B.DATABYTE7;
    RxMessage->data[6] = CAN->sRxFIFO[FIFONumber].RXMDH_B.DATABYTE6;
    RxMessage->data[5] = CAN->sRxFIFO[FIFONumber].RXMDH_B.DATABYTE5;
    RxMessage->data[4] = CAN->sRxFIFO[FIFONumber].RXMDH_B.DATABYTE4;

    RxMessage->data[3] = CAN->sRxFIFO[FIFONumber].RXMDL_B.DATABYTE3;
    RxMessage->data[2] = CAN->sRxFIFO[FIFONumber].RXMDL_B.DATABYTE2;
    RxMessage->data[1] = CAN->sRxFIFO[FIFONumber].RXMDL_B.DATABYTE1;
    RxMessage->data[0] = CAN->sRxFIFO[FIFONumber].RXMDL_B.DATABYTE0;

    if (FIFONumber == CAN_FIFO_0)
    {
        CAN->RXF0_B.RFOM0 = BIT_SET;
    }
    else
    {
        CAN->RXF1_B.RFOM1 = BIT_SET;
    }
}

/*!
 * @brief     Releases the specified receive FIFO.
 *
 * @param     FIFONumber : the number of receive FIFO
 *              This parameter can be one of the following values:
 *                @arg CAN_FIFO_0 : CAN FIFO 0 used to receive
 *                @arg CAN_FIFO_1 : CAN FIFO 1 used to receive
 *
 * @retval    None
 */
void CAN_ReleaseFIFO(uint8_t FIFONumber)
{
    if (FIFONumber == CAN_FIFO_0)
    {
        CAN->RXF0_B.RFOM0 = BIT_SET;
    }
    else
    {
        CAN->RXF1_B.RFOM1 = BIT_SET;
    }
}

/*!
 * @brief    Reads the number of pending messages.
 *
 * @param    FIFONumber : the number of receive FIFO
 *              This parameter can be one of the following values:
 *                @arg CAN_FIFO_0 : CAN FIFO 0 used to receive
 *                @arg CAN_FIFO_1 : CAN FIFO 1 used to receive
 *
 * @retval   which is the number of pending message.
 */
uint8_t CAN_PendingMessage(uint8_t FIFONumber)
{
    if (FIFONumber == CAN_FIFO_0)
    {
        return  CAN->RXF0_B.FMNUM0;
    }
    else
    {
        return  CAN->RXF1_B.FMNUM1;
    }
}

/*!
 * @brief     Select the CAN Operation mode.
 *
 * @param     operatingMode: CAN Operating Mode
 *              This parameter can be one of the following values:
 *              @arg CAN_OPERATING_MODE_INIT  : Initialization mode
 *              @arg CAN_OPERATING_MODE_NORMAL: Normal mode
 *              @arg CAN_OPERATING_MODE_SLEEP : sleep mode
 *
 * @retval    status: 0 : CAN failed entering the specific mode
 *                    1 : CAN Succeed entering the specific mode
 */
uint8_t CAN_OperatingMode(CAN_OPERATING_MODE_T operatingMode)
{
    uint8_t status = RESET;
    uint32_t timeout = 0x00FFFFFF;
    uint8_t sleepflg, initfig;

    sleepflg = CAN->MSTS_B.SLEEPFLG;
    initfig = CAN->MSTS_B.INITFLG;

    switch ((uint8_t)operatingMode)
    {
        case CAN_OPERATING_MODE_INIT:
            CAN->MCTRL_B.SLEEPREQ  = BIT_RESET;
            CAN->MCTRL_B.INITREQ = BIT_SET;

            while ((sleepflg | !initfig) && (timeout != 0))
            {
                sleepflg = CAN->MSTS_B.SLEEPFLG;
                initfig = CAN->MSTS_B.INITFLG;
                timeout--;
            }

            if (sleepflg | !initfig)
            {
                status = RESET;
            }
            else
            {
                status = SET;
            }
            break;

        case CAN_OPERATING_MODE_NORMAL:

            CAN->MCTRL_B.INITREQ = BIT_RESET;
            CAN->MCTRL_B.SLEEPREQ     = BIT_RESET;

            while ((sleepflg | initfig) && (timeout != 0))
            {
                sleepflg = CAN->MSTS_B.SLEEPFLG;
                initfig = CAN->MSTS_B.INITFLG;
                timeout--;
            }

            if (sleepflg | initfig)
            {
                status = RESET;
            }
            else
            {
                status = SET;
            }
            break;

        case CAN_OPERATING_MODE_SLEEP:
            CAN->MCTRL_B.INITREQ = BIT_RESET;
            CAN->MCTRL_B.SLEEPREQ     = BIT_SET;

            while ((!sleepflg | initfig) && (timeout != 0))
            {
                sleepflg = CAN->MSTS_B.SLEEPFLG;
                initfig = CAN->MSTS_B.INITFLG;
                timeout--;
            }

            if (!sleepflg | initfig)
            {
                status = RESET;
            }
            else
            {
                status = SET;
            }
            break;

        default:
            status = RESET;
            break;
    }

    return status;
}

/*!
 * @brief     Enters the sleep mode.
 *
 * @param     None
 *
 * @retval    status: 0 : Into sleep fail
 *                    1 : Into sleep success
 */
uint8_t CAN_SleepMode(void)
{
    CAN_SLEEP_T sleepstatus = CAN_SLEEP_FAILED;

    CAN->MCTRL_B.INITREQ = BIT_RESET;
    CAN->MCTRL_B.SLEEPREQ     = BIT_SET;

    if ((CAN->MSTS_B.SLEEPFLG == BIT_SET) && (CAN->MSTS_B.INITFLG == BIT_RESET))
    {
        sleepstatus =  CAN_SLEEP_OK;
    }

    return (uint8_t)sleepstatus;
}

/*!
 * @brief     Wakes up the CAN from sleep mode.
 *
 * @param     None
 *
 * @retval    status: 0 : WakeUp CAN fail
 *                    1 : WakeUp CAN success
 */
uint8_t CAN_WakeUpMode(void)
{
    uint32_t slakWait = 0x00FFFFFF;
    CAN_WUP_T wakeupstatus = CAN_WAKEUP_FAILED;

    CAN->MCTRL_B.SLEEPREQ = BIT_RESET;

    while ((CAN->MSTS_B.SLEEPFLG == BIT_SET) && (slakWait != 0x00))
    {
        slakWait--;
    }
    if (CAN->MSTS_B.SLEEPFLG != BIT_SET)
    {
        wakeupstatus = CAN_WAKEUP_OK;
    }
    return (uint8_t)wakeupstatus;
}

/*!
 * @brief     Read the last error code of CAN.
 *
 * @param     None
 *
 * @retval    CAN_ErrorCode
 */
uint8_t CAN_ReadLastErrorCode(void)
{
    uint8_t errorcode = 0;
    errorcode = CAN->ERRSTS_B.LERRC;
    return errorcode;
}

/*!
 * @brief    Read the Receive Error Counter of CAN.
 *
 * @param    None
 *
 * @retval   CAN Receive Error Counter.
 */
uint8_t CAN_ReadRxErrorCounter(void)
{
    uint8_t counter = 0;
    counter = CAN->ERRSTS_B.RXERRCNT;
    return counter;
}

/*!
 * @brief   Reads the Least significant byte of the 9-bit CAN Transmit Error Counter.
 *
 * @param   None
 *
 * @retval  Least significant byte of the 9-bit CAN Transmit Error Counter.
 */
uint8_t CAN_ReadLSBTxErrorCounter(void)
{
    uint8_t counter = 0;
    counter = CAN->ERRSTS_B.TXERRCNT;
    return counter;
}

/*!
 * @brief  Enables the specified CAN interrupts.
 *
 * @param  interrupt: Selects the CAN interrupt sources to be enabled.
 *          The parameter can be any combination of the following values:
 *            @arg CAN_INT_TXME : Transmit mailbox empty Interrupt
 *            @arg CAN_INT_F0MP : FIFO 0 message pending Interrupt
 *            @arg CAN_INT_F0FUL: FIFO 0 full Interrupt
 *            @arg CAN_INT_F0OVR: FIFO 0 overrun Interrupt
 *            @arg CAN_INT_F1MP : FIFO 1 message pending Interrupt
 *            @arg CAN_INT_F1FUL: FIFO 1 full Interrupt
 *            @arg CAN_INT_F1OVR: FIFO 1 overrun Interrupt
 *            @arg CAN_INT_EWIE : Error warning Interrupt
 *            @arg CAN_INT_EPIE : Error passive Interrupt
 *            @arg CAN_INT_BOIE : Bus-off Interrupt
 *            @arg CAN_INT_LEC  : Last error code Interrupt
 *            @arg CAN_INT_ERR  : Error Interrupt
 *            @arg CAN_INT_WUP  : Wake-up Interrupt
 *            @arg CAN_INT_SLE  : Sleep acknowledge Interrupt
 *
 * @retval  None
 */
void CAN_EnableInterrupt(uint32_t interrupt)
{
    CAN->INTEN |= interrupt;
}

/*!
 * @brief  Disables the specified CAN interrupts.
 *
 * @param  interrupt: Selects the CAN interrupt sources to be disabled.
 *          The parameter can be any combination of the following values:
 *            @arg CAN_INT_TXME : Transmit mailbox empty Interrupt
 *            @arg CAN_INT_F0MP : FIFO 0 message pending Interrupt
 *            @arg CAN_INT_F0FUL: FIFO 0 full Interrupt
 *            @arg CAN_INT_F0OVR: FIFO 0 overrun Interrupt
 *            @arg CAN_INT_F1MP : FIFO 1 message pending Interrupt
 *            @arg CAN_INT_F1FUL: FIFO 1 full Interrupt
 *            @arg CAN_INT_F1OVR: FIFO 1 overrun Interrupt
 *            @arg CAN_INT_WUP  : Wake-up Interrupt
 *            @arg CAN_INT_SLE  : Sleep acknowledge Interrupt
 *            @arg CAN_INT_EWIE : Error warning Interrupt
 *            @arg CAN_INT_EPIE : Error passive Interrupt
 *            @arg CAN_INT_BOIE : Bus-off Interrupt
 *            @arg CAN_INT_LEC  : Last error code Interrupt
 *            @arg CAN_INT_ERR  : Error Interrupt
 *
 * @retval None
 */
void CAN_DisableInterrupt(uint32_t interrupt)
{
    CAN->INTEN &= ~interrupt;
}

/*!
 * @brief  Reads the specified CAN flag status.
 *
 * @param  flag: specifies the flag of CAN.
 *          The parameter can be one of following values:
 *            @arg CAN_FLAG_EWF  : Error Warning Flag
 *            @arg CAN_FLAG_EPF  : Error Passive Flag
 *            @arg CAN_FLAG_BOF  : Bus-Off Flag
 *            @arg CAN_FLAG_LEC  : Last error code Flag
 *            @arg CAN_FLAG_WUP  : Wake up Flag
 *            @arg CAN_FLAG_SLAK : Sleep acknowledge Flag
 *            @arg CAN_FLAG_RQCP0: Request MailBox0 Flag
 *            @arg CAN_FLAG_RQCP1: Request MailBox1 Flag
 *            @arg CAN_FLAG_RQCP2: Request MailBox2 Flag
 *            @arg CAN_FLAG_FMP0 : FIFO 0 Message Pending Flag
 *            @arg CAN_FLAG_FF0  : FIFO 0 Full Flag
 *            @arg CAN_FLAG_FOV0 : FIFO 0 Overrun Flag
 *            @arg CAN_FLAG_FMP1 : FIFO 1 Message Pending Flag
 *            @arg CAN_FLAG_FF1  : FIFO 1 Full Flag
 *            @arg CAN_FLAG_FOV1 : FIFO 1 Overrun Flag
 *
 * @retval  staus : RESET or SET
 */
uint8_t CAN_ReadStatusFlag(CAN_FLAG_T flag)
{
    uint8_t status = RESET;

    if ((flag & 0x00F00000) != (uint32_t)RESET)
    {
        if ((CAN->ERRSTS & (flag & 0x000FFFFF)) != (uint32_t)RESET)
        {
            status = SET;
        }
        else
        {
            status = RESET;
        }
    }
    else if ((flag & 0x01000000) != (uint32_t)RESET)
    {
        if ((CAN->MSTS & (flag & 0x000FFFFF)) != (uint32_t)RESET)
        {
            status = SET;
        }
        else
        {
            status = RESET;
        }
    }
    else if ((flag & 0x02000000) != (uint32_t)RESET)
    {
        if ((CAN->TXSTS & (flag & 0x000FFFFF)) != (uint32_t)RESET)
        {
            status = SET;
        }
        else
        {
            status = RESET;
        }
    }
    else if ((flag & 0x04000000) != (uint32_t)RESET)
    {
        if ((CAN->RXF0 & (flag & 0x000FFFFF)) != (uint32_t)RESET)
        {
            status = SET;
        }
        else
        {
            status = RESET;
        }
    }
    else
    {
        if ((CAN->RXF1 & (flag & 0x000FFFFF)) != (uint32_t)RESET)
        {
            status = SET;
        }
        else
        {
            status = RESET;
        }
    }
    return  status;
}

/*!
 * @brief  Clears the status flags of CAN.
 *
 * @param  flag : specifies the flag to clear.
 *           The parameter can be one of following values:
 *            @arg CAN_FLAG_LEC  : Last error code Flag
 *            @arg CAN_FLAG_WUP  : Wake up Flag
 *            @arg CAN_FLAG_SLAK : Sleep acknowledge Flag
 *            @arg CAN_FLAG_RQCP0: Request MailBox0 Flag
 *            @arg CAN_FLAG_RQCP1: Request MailBox1 Flag
 *            @arg CAN_FLAG_RQCP2: Request MailBox2 Flag
 *            @arg CAN_FLAG_FF0  : FIFO 0 Full Flag
 *            @arg CAN_FLAG_FOV0 : FIFO 0 Overrun Flag
 *            @arg CAN_FLAG_FF1  : FIFO 1 Full Flag
 *            @arg CAN_FLAG_FOV1 : FIFO 1 Overrun Flag
 *
 * @retval  None
 */
void CAN_ClearStatusFlag(CAN_FLAG_T flag)
{
    uint32_t flagtmp = 0;

    if (flag == CAN_FLAG_LEC)
    {
        CAN->ERRSTS_B.LERRC = 0;
    }
    else
    {
        flagtmp = flag & 0x000FFFFF;

        if ((flag & 0x01000000) != RESET)
        {
            CAN->MSTS = flagtmp;
        }
        else if ((flag & 0x02000000) != RESET)
        {
            CAN->TXSTS = flagtmp;
        }
        else if ((flag & 0x04000000) != RESET)
        {
            CAN->RXF0 = flagtmp;
        }
        else
        {
            CAN->RXF1 = flagtmp;
        }
    }
}

/*!
 * @brief  Reads the specified CAN interrupt flag status.
 *
 * @param  interrupt: specifies the CAN interrupt source.
 *          The parameter can be one of following values:
 *            @arg CAN_INT_TXME : Transmit mailbox empty Interrupt
 *            @arg CAN_INT_F0MP : FIFO 0 message pending Interrupt
 *            @arg CAN_INT_F0FUL: FIFO 0 full Interrupt
 *            @arg CAN_INT_F0OVR: FIFO 0 overrun Interrupt
 *            @arg CAN_INT_F1MP : FIFO 1 message pending Interrupt
 *            @arg CAN_INT_F1FUL: FIFO 1 full Interrupt
 *            @arg CAN_INT_F1OVR: FIFO 1 overrun Interrupt
 *            @arg CAN_INT_EWIE : Error warning Interrupt
 *            @arg CAN_INT_EPIE : Error passive Interrupt
 *            @arg CAN_INT_BOIE : Bus-off Interrupt
 *            @arg CAN_INT_LEC  : Last error code Interrupt
 *            @arg CAN_INT_ERR  : Error Interrupt
 *            @arg CAN_INT_WUP  : Wake-up Interrupt
 *            @arg CAN_INT_SLE  : Sleep acknowledge Interrupt
 *
 * @retval  status : SET or RESET
 */
uint8_t CAN_ReadIntFlag(CAN_INT_T interrupt)
{
    uint8_t status = 0;

    if ((CAN->INTEN & interrupt) != RESET)
    {
        switch (interrupt)
        {
            case CAN_INT_TXME:
                status = CAN->TXSTS_B.REQCFLG0;
                status |= CAN->TXSTS_B.REQCFLG1;
                status |= CAN->TXSTS_B.REQCFLG2;
                break;
            case CAN_INT_F0MP:
                status = !(!CAN->RXF0_B.FMNUM0);
                break;
            case CAN_INT_F0FUL:
                status = CAN->RXF0_B.FFULLFLG0;
                break;
            case CAN_INT_F0OVR:
                status = CAN->RXF0_B.FOVRFLG0;
                break;
            case CAN_INT_F1MP:
                status = !(!CAN->RXF1_B.FMNUM1);
                break;
            case CAN_INT_F1FUL:
                status = CAN->RXF1_B.FFULLFLG1;
                break;
            case CAN_INT_F1OVR:
                status = CAN->RXF1_B.FOVRFLG1;
                break;
            case CAN_INT_EWIE:
                status = CAN->ERRSTS_B.ERRWFLG;
                break;
            case CAN_INT_EPIE:
                status = CAN->ERRSTS_B.ERRPFLG;
                break;
            case CAN_INT_BOIE:
                status = CAN->ERRSTS_B.BOFLG;
                break;
            case CAN_INT_LEC:
                status = !(!CAN->ERRSTS_B.LERRC);
                break;
            case CAN_INT_ERR:
                status = CAN->MSTS_B.ERRIFLG;
                break;
            case CAN_INT_WUP:
                status = CAN->MSTS_B.WUPIFLG;
                break;
            case CAN_INT_SLE:
                status = CAN->MSTS_B.SLEEPIFLG;
                break;
            default:
                status = RESET;
                break;
        }
    }
    else
    {
        status = RESET;
    }
    return status;
}

/*!
 * @brief  Clears the CAN's interrupt flag.
 *
 * @param  interrupt: specifies the interrupt flag to clear.
 *          The parameter can be any combination of the following values:
 *            @arg CAN_INT_TXME : Transmit mailbox empty Interrupt
 *            @arg CAN_INT_F0FUL: FIFO 0 full Interrupt
 *            @arg CAN_INT_F0OVR: FIFO 0 overrun Interrupt
 *            @arg CAN_INT_F1FUL: FIFO 1 full Interrupt
 *            @arg CAN_INT_F1OVR: FIFO 1 overrun Interrupt
 *            @arg CAN_INT_EWIE : Error warning Interrupt
 *            @arg CAN_INT_EPIE : Error passive Interrupt
 *            @arg CAN_INT_BOIE : Bus-off Interrupt
 *            @arg CAN_INT_LEC  : Last error code Interrupt
 *            @arg CAN_INT_ERR  : Error Interrupt
 *            @arg CAN_INT_WUP  : Wake-up Interrupt
 *            @arg CAN_INT_SLE  : Sleep acknowledge Interrupt
 *
 * @retval  None
 */
void CAN_ClearIntFlag(uint32_t interrupt)
{
    if (interrupt & CAN_INT_TXME)
    {
        CAN->TXSTS_B.REQCFLG0 = BIT_SET;
        CAN->TXSTS_B.REQCFLG1 = BIT_SET;
        CAN->TXSTS_B.REQCFLG2 = BIT_SET;
    }
    if (interrupt & CAN_INT_F0FUL)
    {
        CAN->RXF0_B.FFULLFLG0 = BIT_SET;
    }
    if (interrupt & CAN_INT_F0OVR)
    {
        CAN->RXF0_B.FOVRFLG0 = BIT_SET;
    }
    if (interrupt & CAN_INT_F1FUL)
    {
        CAN->RXF1_B.FFULLFLG1 = BIT_SET;
    }
    if (interrupt & CAN_INT_F1OVR)
    {
        CAN->RXF1_B.FOVRFLG1 = BIT_SET;
    }
    if (interrupt & CAN_INT_EWIE)
    {
        /**  Note : the corresponding Flag is cleared by hardware
         *          depending of the CAN Bus status
         */
        CAN->MSTS_B.ERRIFLG = BIT_SET;
    }
    if (interrupt & CAN_INT_EPIE)
    {
        /**  Note : the corresponding Flag is cleared by hardware
         *          depending of the CAN Bus status
         */
        CAN->MSTS_B.ERRIFLG = BIT_SET;
    }
    if (interrupt & CAN_INT_BOIE)
    {
        /**  Note : the corresponding Flag is cleared by hardware
         *          depending of the CAN Bus status
         */
        CAN->MSTS_B.ERRIFLG = BIT_SET;
    }
    if (interrupt & CAN_INT_LEC)
    {
        CAN->ERRSTS_B.LERRC = 0;
        CAN->MSTS_B.ERRIFLG = BIT_SET;
    }
    if (interrupt & CAN_INT_ERR)
    {
        CAN->ERRSTS_B.LERRC = 0;
        CAN->MSTS_B.ERRIFLG = BIT_SET;
    }
    if (interrupt & CAN_INT_WUP)
    {
        CAN->MSTS_B.WUPIFLG = BIT_SET;
    }
    if (interrupt & CAN_INT_SLE)
    {
        CAN->MSTS_B.SLEEPIFLG = BIT_SET;
    }
}

/**@} end of group CAN_Functions */
/**@} end of group CAN_Driver */
/**@} end of group APM32F0xx_StdPeriphDriver */