summaryrefslogtreecommitdiff
path: root/bsp/essemi/es32f0654/libraries/ES32F065x_ALD_StdPeriph_Driver/Source/ald_can.c
blob: b665165077770bb2d773acd8c2a2272352062a80 (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
/**
  ******************************************************************************
  * @file    ald_can.c
  * @brief   CAN module driver.
  *          This file provides firmware functions to manage the following
  *          functionalities of the Controller Area Network (CAN) peripheral:
  *           + Initialization  functions
  *           + IO operation functions
  *           + Peripheral Control functions
  *           + Peripheral State and Error functions
  * @version V1.0
  * @date    25 Apr 2019
  * @author  AE Team
  *
  * Copyright (C) Shanghai Eastsoft Microelectronics Co. Ltd. All rights reserved.
  *
  * SPDX-License-Identifier: Apache-2.0
  *
  * Licensed under the Apache License, Version 2.0 (the License); you may
  * not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
  * www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
  ********************************************************************************
  * @verbatim
  ==============================================================================
                        ##### How to use this driver #####
  ==============================================================================
    [..]
      (#) Enable the CAN controller interface clock.
      (#) CAN pins configuration
        (++) Enable the clock for the CAN GPIOs;
        (++) Connect and configure the involved CAN pins using the
             following function ald_gpio_init();
      (#) Initialise and configure the CAN using ald_can_init() function.
      (#) Transmit the CAN frame using ald_can_send()/ald_can_send_by_it() function.
      (#) Receive a CAN frame using ald_can_recv()/ald_can_recv_by_it function.

     *** Polling mode IO operation ***
     =================================
     [..]
       (+) Start the CAN peripheral transmission and wait the end of this operation
           using ald_can_send(), at this stage user can specify the value of timeout
           according to his end application.
       (+) Start the CAN peripheral reception and wait the end of this operation
           using ald_can_recv(), at this stage user can specify the value of timeout
           according to his end application

     *** Interrupt mode IO operation ***
     ===================================
     [..]
       (+) Start the CAN peripheral transmission using ald_can_send_by_it()
       (+) Start the CAN peripheral reception using ald_can_recv_by_it()
       (+) Use ald_can_irq_handler() called under the used CAN Interrupt subroutine
       (+) At CAN end of transmission pherh->tx_cplt_cbk() function is executed and user can
            add his own code by customization of function pointer pherh->tx_cplt_cbk()
       (+) In case of CAN Error, pherh->rx_cplt_cbk() function is executed and user can
            add his own code by customization of function pointer pherh->rx_cplt_cbk()

     *** CAN ALD driver macros list ***
     =============================================
     [..]
       Below the list of most used macros in CAN driver.

      (+) CAN_RESET_HANDLE_STATE(): Reset CAN handle state.
      (+) CAN_RX_MSG_PENDING(): Return the number of pending received messages.
      (+) CAN_DBG_FREEZE(): Enable or disables the DBG Freeze for CAN.

     [..]
      (@) You can refer to the CAN driver header file for used the macros

  @endverbatim
  ******************************************************************************
  */

#include "ald_can.h"


/** @addtogroup ES32FXXX_ALD
  * @{
  */

/** @defgroup CAN CAN
  * @brief CAN module driver
  * @{
  */
#ifdef ALD_CAN

/** @addtogroup CAN_Private_Functions  CAN Private Functions
  * @{
  */
static void can_rx_fifo_release(can_handle_t *hperh, can_rx_fifo_t num);
static ald_status_t __can_send_by_it(can_handle_t *hperh, uint8_t err);
static ald_status_t __can_recv_by_it(can_handle_t *hperh, can_rx_fifo_t num);
/**
  * @}
  */

/** @defgroup CAN_Public_Functions CAN Public Functions
  * @{
  */

/** @defgroup CAN_Public_Functions_Group1 Initialization  functions
  * @brief    Initialization and Configuration functions
  *
  * @verbatim
  ==============================================================================
              ##### Initialization and Configuration functions #####
  ==============================================================================
    [..]  This section provides functions allowing to:
      (+) Initialize and configure the CAN.
      (+) Configures the CAN reception filter.
      (+) Reset the CAN.

    @endverbatim
  * @{
  */

/**
  * @brief  Initializes the CAN peripheral according to the specified
  *         parameters in the CAN_init_t.
  * @param  hperh: pointer to a can_handle_t structure that contains
  *         the configuration information for the specified CAN.
  * @retval Status, see ald_status_t.
  */
ald_status_t ald_can_init(can_handle_t *hperh)
{
	uint32_t tickstart = 0;

	assert_param(IS_CAN_ALL(hperh->perh));
	assert_param(IS_FUNC_STATE(hperh->init.ttcm));
	assert_param(IS_FUNC_STATE(hperh->init.abom));
	assert_param(IS_FUNC_STATE(hperh->init.awk));
	assert_param(IS_FUNC_STATE(hperh->init.abom));
	assert_param(IS_FUNC_STATE(hperh->init.rfom));
	assert_param(IS_FUNC_STATE(hperh->init.txmp));
	assert_param(IS_CAN_MODE(hperh->init.mode));
	assert_param(IS_CAN_SJW(hperh->init.sjw));
	assert_param(IS_CAN_BS1(hperh->init.seg1));
	assert_param(IS_CAN_BS2(hperh->init.seg2));
	assert_param(IS_CAN_PRESCALER(hperh->init.psc));

	if (hperh->state == CAN_STATE_RESET)
		hperh->lock = UNLOCK;

	hperh->state = CAN_STATE_BUSY;
	tickstart    = ald_get_tick();

	CLEAR_BIT(hperh->perh->CON, CAN_CON_SLPREQ_MSK);
	SET_BIT(hperh->perh->CON, CAN_CON_INIREQ_MSK);

	while (!READ_BIT(hperh->perh->STAT, CAN_STAT_INISTAT_MSK)) {
		if ((ald_get_tick() - tickstart) > CAN_TIMEOUT_VALUE) {
			hperh->state = CAN_STATE_TIMEOUT;
			__UNLOCK(hperh);

			return TIMEOUT;
		}
	}

	MODIFY_REG(hperh->perh->CON, CAN_CON_TTCEN_MSK, hperh->init.ttcm << CAN_CON_TTCEN_POS);
	MODIFY_REG(hperh->perh->CON, CAN_CON_ABOFFEN_MSK, hperh->init.abom << CAN_CON_ABOFFEN_POS);
	MODIFY_REG(hperh->perh->CON, CAN_CON_AWKEN_MSK, hperh->init.awk << CAN_CON_AWKEN_POS);
	MODIFY_REG(hperh->perh->CON, CAN_CON_ARTXDIS_MSK, hperh->init.artx << CAN_CON_ARTXDIS_POS);
	MODIFY_REG(hperh->perh->CON, CAN_CON_RXFOPM_MSK, hperh->init.rfom << CAN_CON_RXFOPM_POS);
	MODIFY_REG(hperh->perh->CON, CAN_CON_TXMP_MSK, hperh->init.txmp << CAN_CON_TXMP_POS);
	MODIFY_REG(hperh->perh->BTIME, CAN_BTIME_LOOP_MSK, (hperh->init.mode & 0x1) << CAN_BTIME_LOOP_POS);
	MODIFY_REG(hperh->perh->BTIME, CAN_BTIME_SILENT_MSK, ((hperh->init.mode >> 1) & 0x1) << CAN_BTIME_SILENT_POS);
	MODIFY_REG(hperh->perh->BTIME, CAN_BTIME_RESJW_MSK, hperh->init.sjw << CAN_BTIME_RESJW_POSS);
	MODIFY_REG(hperh->perh->BTIME, CAN_BTIME_SEG1_MSK, hperh->init.seg1 << CAN_BTIME_SEG1_POSS);
	MODIFY_REG(hperh->perh->BTIME, CAN_BTIME_SEG2_MSK, hperh->init.seg2 << CAN_BTIME_SEG2_POSS);
	MODIFY_REG(hperh->perh->BTIME, CAN_BTIME_BPSC_MSK, (hperh->init.psc - 1) << CAN_BTIME_BPSC_POSS);

	CLEAR_BIT(hperh->perh->CON, CAN_CON_INIREQ_MSK);
	tickstart = ald_get_tick();

	while (READ_BIT(hperh->perh->STAT, CAN_STAT_INISTAT_MSK)) {
		if ((ald_get_tick() - tickstart) > CAN_TIMEOUT_VALUE) {
			hperh->state = CAN_STATE_TIMEOUT;
			__UNLOCK(hperh);

			return TIMEOUT;
		}
	}

	hperh->err   = CAN_ERROR_NONE;
	hperh->state = CAN_STATE_READY;

	return OK;
}

/**
  * @brief  Configures the CAN reception filter according to the specified
  *         parameters in the can_filter_t.
  * @param  hperh: pointer to a can_handle_t structure.
  * @param  config: pointer to a can_filter_t structure that
  *         contains the filter configuration information.
  * @retval Status, see ald_status_t.
  */
ald_status_t ald_can_filter_config(can_handle_t *hperh, can_filter_t *config)
{
	uint32_t pos;

	assert_param(IS_CAN_ALL(hperh->perh));
	assert_param(IS_CAN_FILTER_NUMBER(config->number));
	assert_param(IS_CAN_FILTER_MODE(config->mode));
	assert_param(IS_CAN_FILTER_SCALE(config->scale));
	assert_param(IS_CAN_FILTER_FIFO(config->fifo));
	assert_param(IS_FUNC_STATE(config->active));

	pos = 1 << config->number;
	SET_BIT(hperh->perh->FLTCON, CAN_FLTCON_FLTINI_MSK);
	CLEAR_BIT(hperh->perh->FLTGO, pos);

	if (config->scale == CAN_FILTER_SCALE_16) {
		CLEAR_BIT(hperh->perh->FLTWS, pos);
		hperh->perh->Filter[config->number].FLT1 =
			((0xFFFF & config->mask_id_low) << 16) |
			 (0xFFFF & config->id_low);

		hperh->perh->Filter[config->number].FLT2 =
			((0xFFFF & config->mask_id_high) << 16) |
			 (0xFFFF & config->id_high);
	}

	if (config->scale == CAN_FILTER_SCALE_32) {
		SET_BIT(hperh->perh->FLTWS, pos);
		hperh->perh->Filter[config->number].FLT1 =
			((0xFFFF & config->id_high) << 16) |
			 (0xFFFF & config->id_low);
		hperh->perh->Filter[config->number].FLT2 =
			((0xFFFF & config->mask_id_high) << 16) |
			 (0xFFFF & config->mask_id_low);
	}

	MODIFY_REG(hperh->perh->FLTM, pos, config->mode << config->number);
	MODIFY_REG(hperh->perh->FLTAS, pos, config->fifo << config->number);
	MODIFY_REG(hperh->perh->FLTGO, pos, config->active << config->number);
	CLEAR_BIT(hperh->perh->FLTCON, CAN_FLTCON_FLTINI_MSK);

	return OK;
}

/**
  * @brief  Reset the CAN peripheral.
  * @param  hperh: pointer to a can_handle_t structure.
  * @retval None
  */
void ald_can_reset(can_handle_t *hperh)
{
	assert_param(IS_CAN_ALL(hperh->perh));

	hperh->state = CAN_STATE_RESET;
	__UNLOCK(hperh);

	return;
}

/**
  * @}
  */

/** @defgroup CAN_Public_Functions_Group2 IO operation functions
 *  @brief    I/O operation functions
 *
 *  @verbatim
  ==============================================================================
                      ##### IO operation functions #####
  ==============================================================================
    [..]  This section provides functions allowing to:
      (+) Send a CAN frame message.
      (+) Send a CAN frame message using interrupt.
      (+) Receive a CAN frame message.
      (+) Receive a CAN frame message using interrupt.

  *@endverbatim
  * @{
  */

/**
  * @brief  Send a CAN frame message.
  * @param  hperh: pointer to a can_handle_t structure.
  * @param  msg: message which will be sent.
  * @param  timeout: specify Timeout value
  * @retval Status, see ald_status_t.
  */
ald_status_t ald_can_send(can_handle_t *hperh, can_tx_msg_t *msg, uint32_t timeout)
{
	uint32_t tick;
	can_tx_mailbox_t idx;

	assert_param(IS_CAN_ALL(hperh->perh));
	assert_param(IS_CAN_IDTYPE(msg->type));
	assert_param(IS_CAN_RTR(msg->rtr));
	assert_param(IS_CAN_DATA_LEN(msg->len));

	__LOCK(hperh);
	SET_BIT(hperh->state, CAN_STATE_TX_MASK);

	if (READ_BIT(hperh->perh->TXSTAT, CAN_TXSTAT_TXM0EF_MSK))
		idx = CAN_TX_MAILBOX_0;
	else if (READ_BIT(hperh->perh->TXSTAT, CAN_TXSTAT_TXM1EF_MSK))
		idx = CAN_TX_MAILBOX_1;
	else if (READ_BIT(hperh->perh->TXSTAT, CAN_TXSTAT_TXM2EF_MSK))
		idx = CAN_TX_MAILBOX_2;
	else
		idx = CAN_TX_MAILBOX_NONE;

	if (idx == CAN_TX_MAILBOX_NONE) {
		hperh->state = CAN_STATE_ERROR;
		__UNLOCK(hperh);
		return ERROR;
	}

	CLEAR_BIT(hperh->perh->TxMailBox[idx].TXID, CAN_TXID0_TXMREQ_MSK);
	MODIFY_REG(hperh->perh->TxMailBox[idx].TXID, CAN_TXID0_IDE_MSK, msg->type << CAN_TXID0_IDE_POS);
	MODIFY_REG(hperh->perh->TxMailBox[idx].TXID, CAN_TXID0_RTR_MSK, msg->rtr << CAN_TXID0_RTR_POS);

	if (msg->type == CAN_ID_STD) {
		assert_param(IS_CAN_STDID(msg->std));
		MODIFY_REG(hperh->perh->TxMailBox[idx].TXID, CAN_TXID0_STDID_MSK, msg->std << CAN_TXID0_STDID_POSS);
		CLEAR_BIT(hperh->perh->TxMailBox[idx].TXID, CAN_TXID0_EXID_MSK);
	}
	else {
		assert_param(IS_CAN_EXTID(msg->ext));
		MODIFY_REG(hperh->perh->TxMailBox[idx].TXID, CAN_TXID0_STDID_MSK, ((msg->ext >> 18) & 0x7FF) << CAN_TXID0_STDID_POSS);
		MODIFY_REG(hperh->perh->TxMailBox[idx].TXID, CAN_TXID0_EXID_MSK, (msg->ext & 0x3FFFF) << CAN_TXID0_EXID_POSS);
	}
	MODIFY_REG(hperh->perh->TxMailBox[idx].TXFCON, CAN_TXFCON0_DLEN_MSK, (msg->len & 0xF) << CAN_TXFCON0_DLEN_POSS);
	WRITE_REG(hperh->perh->TxMailBox[idx].TXDL, msg->data[0] | (msg->data[1] << 8) | (msg->data[2] << 16) | (msg->data[3] << 24));
	WRITE_REG(hperh->perh->TxMailBox[idx].TXDH, msg->data[4] | (msg->data[5] << 8) | (msg->data[6] << 16) | (msg->data[7] << 24));
	SET_BIT(hperh->perh->TxMailBox[idx].TXID, CAN_TXID0_TXMREQ_MSK);
	tick = ald_get_tick();

	while (!(ald_can_get_tx_status(hperh, idx))) {
		if ((timeout == 0) || ((ald_get_tick() - tick) > timeout)) {
			hperh->state = CAN_STATE_TIMEOUT;
			__UNLOCK(hperh);
			return TIMEOUT;
		}
	}

	CLEAR_BIT(hperh->state, CAN_STATE_TX_MASK);
	__UNLOCK(hperh);
	return OK;
}

/**
  * @brief  Send a CAN frame message using interrupt.
  * @param  hperh: pointer to a can_handle_t structure.
  * @param  msg: message which will be sent.
  * @retval Status, see ald_status_t.
  */
ald_status_t ald_can_send_by_it(can_handle_t *hperh, can_tx_msg_t *msg)
{
	uint8_t idx = CAN_TX_MAILBOX_NONE;

	assert_param(IS_CAN_ALL(hperh->perh));
	assert_param(IS_CAN_IDTYPE(msg->type));
	assert_param(IS_CAN_RTR(msg->rtr));
	assert_param(IS_CAN_DATA_LEN(msg->len));

	if ((hperh->state & CAN_STATE_TX_MASK) || ((hperh->state & 0xF) != CAN_STATE_READY))
		return BUSY;

	if (READ_BIT(hperh->perh->TXSTAT, CAN_TXSTAT_TXM0EF_MSK))
		idx = CAN_TX_MAILBOX_0;
	else if (READ_BIT(hperh->perh->TXSTAT, CAN_TXSTAT_TXM1EF_MSK))
		idx = CAN_TX_MAILBOX_1;
	else if (READ_BIT(hperh->perh->TXSTAT, CAN_TXSTAT_TXM2EF_MSK))
		idx = CAN_TX_MAILBOX_2;
	else
		idx = CAN_TX_MAILBOX_NONE;

	if (idx == CAN_TX_MAILBOX_NONE)
		return BUSY;

	CLEAR_BIT(hperh->perh->TxMailBox[idx].TXID, CAN_TXID0_TXMREQ_MSK);
	MODIFY_REG(hperh->perh->TxMailBox[idx].TXID, CAN_TXID0_IDE_MSK, msg->type << CAN_TXID0_IDE_POS);
	MODIFY_REG(hperh->perh->TxMailBox[idx].TXID, CAN_TXID0_RTR_MSK, msg->rtr << CAN_TXID0_RTR_POS);

	if (msg->type == CAN_ID_STD) {
		assert_param(IS_CAN_STDID(msg->std));
		MODIFY_REG(hperh->perh->TxMailBox[idx].TXID, CAN_TXID0_STDID_MSK, msg->std << CAN_TXID0_STDID_POSS);
		CLEAR_BIT(hperh->perh->TxMailBox[idx].TXID, CAN_TXID0_EXID_MSK);
	}
	else {
		assert_param(IS_CAN_EXTID(msg->ext));
		MODIFY_REG(hperh->perh->TxMailBox[idx].TXID, CAN_TXID0_STDID_MSK, ((msg->ext >> 18) & 0x7FF) << CAN_TXID0_STDID_POSS);
		MODIFY_REG(hperh->perh->TxMailBox[idx].TXID, CAN_TXID0_EXID_MSK, (msg->ext & 0x3FFFF) << CAN_TXID0_EXID_POSS);
	}

	MODIFY_REG(hperh->perh->TxMailBox[idx].TXFCON, CAN_TXFCON0_DLEN_MSK, (msg->len & 0xF) << CAN_TXFCON0_DLEN_POSS);
	WRITE_REG(hperh->perh->TxMailBox[idx].TXDL, msg->data[0] | (msg->data[1] << 8) | (msg->data[2] << 16) | (msg->data[3] << 24));
	WRITE_REG(hperh->perh->TxMailBox[idx].TXDH, msg->data[4] | (msg->data[5] << 8) | (msg->data[6] << 16) | (msg->data[7] << 24));

	SET_BIT(hperh->state, CAN_STATE_TX_MASK);

	ald_can_interrupt_config(hperh, CAN_IT_WARN, ENABLE);
	ald_can_interrupt_config(hperh, CAN_IT_PERR, ENABLE);
	ald_can_interrupt_config(hperh, CAN_IT_BOF, ENABLE);
	ald_can_interrupt_config(hperh, CAN_IT_PRERR, ENABLE);
	ald_can_interrupt_config(hperh, CAN_IT_ERR, ENABLE);
	ald_can_interrupt_config(hperh, CAN_IT_TXM, ENABLE);

	SET_BIT(hperh->perh->TxMailBox[idx].TXID, CAN_TXID0_TXMREQ_MSK);
	return OK;
}

/**
  * @brief  Receives a correct CAN frame.
  * @param  hperh: pointer to a can_handle_t structure.
  * @param  num: Receive fifo number, CAN_RX_FIFO0 or CAN_RX_FIFO1
  * @param  msg: Storing message.
  * @param  timeout: Specify timeout value
  * @retval Status, see ald_status_t.
  */
ald_status_t ald_can_recv(can_handle_t *hperh, can_rx_fifo_t num, can_rx_msg_t *msg, uint32_t timeout)
{
	uint32_t tick, stid, exid;

	assert_param(IS_CAN_ALL(hperh->perh));
	assert_param(IS_CAN_FIFO(num));
	assert_param(msg != NULL);

	__LOCK(hperh);
	SET_BIT(hperh->state, num == CAN_RX_FIFO0 ? CAN_STATE_RX0_MASK : CAN_STATE_RX1_MASK);
	tick = ald_get_tick();

	while (CAN_RX_MSG_PENDING(hperh, num) == 0) {
		if ((timeout == 0) || ((ald_get_tick() - tick) > timeout)) {
			hperh->state = CAN_STATE_TIMEOUT;
			__UNLOCK(hperh);
			return TIMEOUT;
		}
	}

	stid = READ_BITS(hperh->perh->RxFIFO[num].RXFID, CAN_RXF0ID_STDID_MSK, CAN_RXF0ID_STDID_POSS);
	exid = READ_BITS(hperh->perh->RxFIFO[num].RXFID, CAN_RXF0ID_EXID_MSK, CAN_RXF0ID_EXID_POSS);
	msg->type = (can_id_type_t)READ_BITS(hperh->perh->RxFIFO[num].RXFID, CAN_RXF0ID_IDE_MSK, CAN_RXF0ID_IDE_POS);

	if (msg->type == CAN_ID_STD)
		msg->std = stid;
	else
		msg->ext = (stid << 18) | exid;

	msg->rtr     = (can_remote_req_t)READ_BITS(hperh->perh->RxFIFO[num].RXFID, CAN_RXF0ID_RTR_MSK, CAN_RXF0ID_RTR_POS);
	msg->len     = READ_BITS(hperh->perh->RxFIFO[num].RXFINF, CAN_RXF0INF_DLEN_MSK, CAN_RXF0INF_DLEN_POSS);
	msg->fmi     = READ_BITS(hperh->perh->RxFIFO[num].RXFINF, CAN_RXF0INF_FLTIDX_MSK, CAN_RXF0INF_FLTIDX_POSS);
	msg->data[0] = hperh->perh->RxFIFO[num].RXFDL & 0xFF;
	msg->data[1] = (hperh->perh->RxFIFO[num].RXFDL >> 8) & 0xFF;
	msg->data[2] = (hperh->perh->RxFIFO[num].RXFDL >> 16) & 0xFF;
	msg->data[3] = (hperh->perh->RxFIFO[num].RXFDL >> 24) & 0xFF;
	msg->data[4] = hperh->perh->RxFIFO[num].RXFDH & 0xFF;
	msg->data[5] = (hperh->perh->RxFIFO[num].RXFDH >> 8) & 0xFF;
	msg->data[6] = (hperh->perh->RxFIFO[num].RXFDH >> 16) & 0xFF;
	msg->data[7] = (hperh->perh->RxFIFO[num].RXFDH >> 24) & 0xFF;

	can_rx_fifo_release(hperh, num);
	CLEAR_BIT(hperh->state, num == CAN_RX_FIFO0 ? CAN_STATE_RX0_MASK : CAN_STATE_RX1_MASK);
	__UNLOCK(hperh);

	return OK;
}

/**
  * @brief  Receives a correct CAN frame using interrupt.
  * @param  hperh: pointer to a can_handle_t structure.
  * @param  num: Specify the FIFO number
  * @param  msg: Storing message.
  * @retval Status, see ald_status_t.
  */
ald_status_t ald_can_recv_by_it(can_handle_t *hperh, can_rx_fifo_t num, can_rx_msg_t *msg)
{
	assert_param(IS_CAN_ALL(hperh->perh));
	assert_param(IS_CAN_FIFO(num));
	assert_param(msg != NULL);

	if (num == CAN_RX_FIFO0) {
		if ((hperh->state & CAN_STATE_RX0_MASK) || ((hperh->state & 0xF) != CAN_STATE_READY))
			return BUSY;

		SET_BIT(hperh->state, CAN_STATE_RX0_MASK);
		hperh->rx0_msg = msg;
	}
	else {
		if ((hperh->state & CAN_STATE_RX1_MASK) || ((hperh->state & 0xF) != CAN_STATE_READY))
			return BUSY;

		SET_BIT(hperh->state, CAN_STATE_RX1_MASK);
		hperh->rx1_msg = msg;
	}

	ald_can_interrupt_config(hperh, CAN_IT_WARN, ENABLE);
	ald_can_interrupt_config(hperh, CAN_IT_PERR, ENABLE);
	ald_can_interrupt_config(hperh, CAN_IT_BOF, ENABLE);
	ald_can_interrupt_config(hperh, CAN_IT_PRERR, ENABLE);
	ald_can_interrupt_config(hperh, CAN_IT_ERR, ENABLE);

	if (num == CAN_RX_FIFO0)
		ald_can_interrupt_config(hperh, CAN_IT_FP0, ENABLE);
	else
		ald_can_interrupt_config(hperh, CAN_IT_FP1, ENABLE);

	return OK;
}
/**
  * @}
  */

/** @defgroup CAN_Public_Functions_Group3 Peripheral Control functions
  * @brief    Peripheral Control functions
  *
  * @verbatim
  ==============================================================================
            ##### Peripheral Control functions #####
  ==============================================================================
    [..]
    This section provides functions allowing to:
      (+) Configure CAN sleep.
      (+) Configure CAN wakeup.
      (+) CAN cancel send message.
      (+) Handle CAN interrupt.
      (+) Get CAN transmit status.
      (+) Configure CAN interrupt ENABLE/DISABLE.
      (+) Get CAN interrupt source status.
      (+) Get CAN interrupt flag status.
      (+) Clear CAN interrupt flag.

  * @endverbatim
  * @{
  */

/**
  * @brief  Enters the Sleep(low power) mode.
  * @param  hperh: pointer to a can_handle_t.
  * @retval Status, see ald_status_t.
  */
ald_status_t ald_can_sleep(can_handle_t *hperh)
{
	uint32_t tick;

	assert_param(IS_CAN_ALL(hperh->perh));

	__LOCK(hperh);
	hperh->state = CAN_STATE_BUSY;

	CLEAR_BIT(hperh->perh->CON, CAN_CON_INIREQ_MSK);
	SET_BIT(hperh->perh->CON, CAN_CON_SLPREQ_MSK);
	tick = ald_get_tick();

	while ((!(READ_BIT(hperh->perh->STAT, CAN_STAT_SLPSTAT_MSK))) || READ_BIT(hperh->perh->STAT, CAN_STAT_INISTAT_MSK)) {
		if ((ald_get_tick() - tick) > CAN_TIMEOUT_VALUE) {
			hperh->state = CAN_STATE_TIMEOUT;
			__UNLOCK(hperh);
			return TIMEOUT;
		}
	}

	hperh->state = CAN_STATE_READY;
	__UNLOCK(hperh);

	return OK;
}

/**
  * @brief  Wakes up the CAN peripheral from sleep mode, after that the CAN peripheral
  *         is in the normal mode.
  * @param  hperh: pointer to a can_handle_t structure.
  * @retval Status, see ald_status_t.
  */
ald_status_t ald_can_wake_up(can_handle_t *hperh)
{
	uint32_t tick;

	assert_param(IS_CAN_ALL(hperh->perh));

	__LOCK(hperh);
	hperh->state = CAN_STATE_BUSY;

	CLEAR_BIT(hperh->perh->CON, CAN_CON_SLPREQ_MSK);
	tick = ald_get_tick();

	while (READ_BIT(hperh->perh->STAT, CAN_STAT_SLPSTAT_MSK)) {
		if ((ald_get_tick() - tick) > CAN_TIMEOUT_VALUE) {
			hperh->state = CAN_STATE_TIMEOUT;
			__UNLOCK(hperh);
			return TIMEOUT;
		}
	}

	hperh->state = CAN_STATE_READY;
	__UNLOCK(hperh);

	return OK;
}

/**
  * @brief  Handles CAN interrupt request
  * @param  hperh: pointer to a can_handle_t structure.
  * @retval None
  */
void ald_can_irq_handler(can_handle_t* hperh)
{
	if (ald_can_get_it_status(hperh, CAN_IT_TXM)) {
		if ((ald_can_get_tx_status(hperh, CAN_TX_MAILBOX_0))
                     || (ald_can_get_tx_status(hperh, CAN_TX_MAILBOX_1))
                     || (ald_can_get_tx_status(hperh, CAN_TX_MAILBOX_2)))
			__can_send_by_it(hperh, 0);
		if (hperh->perh->TXSTAT & CAN_TXSTAT_M0TXERR_MSK) {
			SET_BIT(hperh->perh->TXSTATC, CAN_TXSTATC_M0REQC_MSK);
			__can_send_by_it(hperh, 1);
		}
		if (hperh->perh->TXSTAT & CAN_TXSTAT_M1TXERR_MSK) {
			SET_BIT(hperh->perh->TXSTATC, CAN_TXSTATC_M1REQC_MSK);
			__can_send_by_it(hperh, 1);
		}
		if (hperh->perh->TXSTAT & CAN_TXSTAT_M2TXERR_MSK) {
			SET_BIT(hperh->perh->TXSTATC, CAN_TXSTATC_M2REQC_MSK);
			__can_send_by_it(hperh, 1);
		}
	}

	if ((ald_can_get_it_status(hperh, CAN_IT_FP0))
                   && (CAN_RX_MSG_PENDING(hperh, CAN_RX_FIFO0) != 0))
		__can_recv_by_it(hperh, CAN_RX_FIFO0);

	if ((ald_can_get_it_status(hperh, CAN_IT_FP1))
                   && (CAN_RX_MSG_PENDING(hperh, CAN_RX_FIFO1) != 0))
		__can_recv_by_it(hperh, CAN_RX_FIFO1);

	if ((ald_can_get_flag_status(hperh, CAN_FLAG_WARN))
                     && (ald_can_get_it_status(hperh, CAN_IT_WARN))
                     && (ald_can_get_it_status(hperh, CAN_IT_ERR)))
		hperh->err |= CAN_ERROR_EWG;

	if ((ald_can_get_flag_status(hperh, CAN_FLAG_PERR))
                     && (ald_can_get_it_status(hperh, CAN_IT_PERR))
                     && (ald_can_get_it_status(hperh, CAN_IT_ERR)))
		hperh->err |= CAN_ERROR_EPV;

	if ((ald_can_get_flag_status(hperh, CAN_FLAG_BOF))
                     && (ald_can_get_it_status(hperh, CAN_IT_BOF))
                     && (ald_can_get_it_status(hperh, CAN_IT_ERR)))
		hperh->err |= CAN_ERROR_BOF;

	if (READ_BIT(hperh->perh->ERRSTAT, CAN_ERRSTAT_PRERRF_MSK)
                   && (ald_can_get_it_status(hperh, CAN_IT_PRERR))
                   && (ald_can_get_it_status(hperh, CAN_IT_ERR))) {

		switch (READ_BITS(hperh->perh->ERRSTAT, CAN_ERRSTAT_PRERRF_MSK, CAN_ERRSTAT_PRERRF_POSS)) {
		case(1):
			hperh->err |= CAN_ERROR_STF;
			break;
		case(2):
			hperh->err |= CAN_ERROR_FOR;
			break;
		case(3):
			hperh->err |= CAN_ERROR_ACK;
			break;
		case(4):
			hperh->err |= CAN_ERROR_BR;
			break;
		case(5):
			hperh->err |= CAN_ERROR_BD;
			break;
		case(6):
			hperh->err |= CAN_ERROR_CRC;
			break;
		default:
			break;
		}

		CLEAR_BIT(hperh->perh->ERRSTAT, CAN_ERRSTAT_PRERRF_MSK);

		if (READ_BIT(hperh->perh->IE, CAN_IE_TXMEIE_MSK))
			ald_can_interrupt_config(hperh, CAN_IT_TXM, DISABLE);

		if (READ_BIT(hperh->perh->IE, CAN_IE_F0PIE_MSK))
			ald_can_interrupt_config(hperh, CAN_IT_FP0, DISABLE);

		if (READ_BIT(hperh->perh->IE, CAN_IE_F1PIE_MSK))
			ald_can_interrupt_config(hperh, CAN_IT_FP1, DISABLE);
	}

	if ((ald_can_get_flag_status(hperh, CAN_FLAG_ERR)) && (hperh->err == CAN_ERROR_NONE))
		hperh->err = CAN_ERROR_UNK;

	if (hperh->err != CAN_ERROR_NONE) {
		SET_BIT(hperh->perh->IFC, CAN_IFC_ERRIFC_MSK);
		hperh->state = CAN_STATE_READY;

		if (hperh->error_cbk != NULL)
			hperh->error_cbk(hperh);
	}
}

/**
  * @brief  Check the transmission status of a CAN Frame.
  * @param  hperh: pointer to a can_handle_t structure.
  * @param  box: the index of the mailbox that is used for transmission.
  * @retval The new status of transmission(TRUE or FALSE).
  */
type_bool_t ald_can_get_tx_status(can_handle_t *hperh, can_tx_mailbox_t box)
{
	assert_param(IS_CAN_ALL(hperh->perh));
	assert_param(IS_CAN_TX_MAILBOX(box));

	switch (box) {
	case CAN_TX_MAILBOX_0:
		if (!READ_BIT(hperh->perh->TXSTAT, CAN_TXSTAT_M0REQC_MSK))
			return FALSE;
		if (!READ_BIT(hperh->perh->TXSTAT, CAN_TXSTAT_M0TXC_MSK))
			return FALSE;
		if (!READ_BIT(hperh->perh->TXSTAT, CAN_TXSTAT_TXM0EF_MSK))
			return FALSE;

		return TRUE;

	case CAN_TX_MAILBOX_1:
		if (!READ_BIT(hperh->perh->TXSTAT, CAN_TXSTAT_M1REQC_MSK))
			return FALSE;
		if (!READ_BIT(hperh->perh->TXSTAT, CAN_TXSTAT_M1TXC_MSK))
			return FALSE;
		if (!READ_BIT(hperh->perh->TXSTAT, CAN_TXSTAT_TXM1EF_MSK))
			return FALSE;

		return TRUE;

	case CAN_TX_MAILBOX_2:
		if (!READ_BIT(hperh->perh->TXSTAT, CAN_TXSTAT_M2REQC_MSK))
			return FALSE;
		if (!READ_BIT(hperh->perh->TXSTAT, CAN_TXSTAT_M2TXC_MSK))
			return FALSE;
		if (!READ_BIT(hperh->perh->TXSTAT, CAN_TXSTAT_TXM2EF_MSK))
			return FALSE;

		return TRUE;

	default:
		break;
	}

	return FALSE;
}

/**
  * @brief  Cancel transmission.
  * @param  hperh: pointer to a can_handle_t structure.
  * @param  box: the index of the mailbox that is used for transmission.
  * @retval None
  */
void ald_can_cancel_send(can_handle_t *hperh, can_tx_mailbox_t box)
{
	assert_param(IS_CAN_ALL(hperh->perh));
	assert_param(IS_CAN_TX_MAILBOX(box));

	switch (box) {
	case CAN_TX_MAILBOX_0:
		SET_BIT(hperh->perh->TXSTAT, CAN_TXSTAT_M0STPREQ_MSK);
		break;
	case CAN_TX_MAILBOX_1:
		SET_BIT(hperh->perh->TXSTAT, CAN_TXSTAT_M1STPREQ_MSK);
		break;
	case CAN_TX_MAILBOX_2:
		SET_BIT(hperh->perh->TXSTAT, CAN_TXSTAT_M2STPREQ_MSK);
		break;
	default:
		break;
	}

	return;
}

/**
  * @brief  Enable/disable the specified CAN interrupts.
  * @param  hperh: Pointer to a can_handle_t structure.
  * @param  it: Specifies the CAN interrupt sources to be enabled or disabled.
  *         This parameter can be one of the @ref can_it_t.
  * @param  state: New state of the specified CAN interrupts.
  *         This parameter can be:
  *             @arg ENABLE
  *             @arg DISABLE
  * @retval None
  */
void ald_can_interrupt_config(can_handle_t *hperh, can_it_t it, type_func_t state)
{
	assert_param(IS_CAN_ALL(hperh->perh));
	assert_param(IS_CAN_IT(it));
	assert_param(IS_FUNC_STATE(state));

	if (state == ENABLE)
		SET_BIT(hperh->perh->IE, it);
	else
		CLEAR_BIT(hperh->perh->IE, it);

	return;
}

/**
  * @brief  Get the status of CAN interrupt source.
  * @param  hperh: Pointer to a can_handle_t structure.
  * @param  it: Specifies the CAN interrupt source.
  *         This parameter can be one of the @ref can_it_t.
  * @retval Status:
  *           - 0: RESET
  *           - 1: SET
  */
it_status_t ald_can_get_it_status(can_handle_t *hperh, can_it_t it)
{
	assert_param(IS_CAN_ALL(hperh->perh));
	assert_param(IS_CAN_IT(it));

	if (READ_BIT(hperh->perh->IE, it))
		return SET;

	return RESET;
}

/**
  * @brief  Get the status of CAN interrupt flag.
  * @param  hperh: Pointer to a can_handle_t structure.
  * @param  flag: Specifies the CAN interrupt flag.
  *         This parameter can be one of the @ref can_flag_t.
  * @retval Status:
  *           - 0: RESET
  *           - 1: SET
  */
flag_status_t ald_can_get_flag_status(can_handle_t *hperh, can_flag_t flag)
{
	uint32_t idx   = (flag >> 20) & 0x7;
	uint32_t _flag = flag & 0xFF8FFFFF;

	assert_param(IS_CAN_GET_FLAG(flag));

	switch (idx) {
	case 0:
		if (READ_BIT(hperh->perh->STAT, _flag))
			return SET;

		break;
	case 1:
		if (READ_BIT(hperh->perh->TXSTAT, _flag))
			return SET;

		break;
	case 2:
		if (READ_BIT(hperh->perh->RXF0, _flag))
			return SET;

		break;
	case 3:
		if (READ_BIT(hperh->perh->RXF1, _flag))
			return SET;

		break;
	case 4:
		if (READ_BIT(hperh->perh->ERRSTAT, _flag))
			return SET;

		break;
	default:
		break;
	}

	return RESET;
}

/** @brief  Clear the specified CAN pending flag.
  * @param  hperh: pointer to a can_handle_t structure.
  * @param  flag: specifies the flag to check.
  * @retval None.
  */
void ald_can_clear_flag_status(can_handle_t *hperh, can_flag_t flag)
{
	uint32_t idx   = ((uint32_t)flag >> 20) & 0x7;
	uint32_t _flag = flag & 0xFF8FFFFF;

	assert_param(IS_CAN_CLEAR_FLAG(flag));

	switch (idx) {
	case 0:
		WRITE_REG(hperh->perh->IFC, _flag);
		break;
	case 1:
		WRITE_REG(hperh->perh->TXSTATC, _flag);
		break;
	case 2:
		WRITE_REG(hperh->perh->RXF0C, _flag);
		break;
	case 3:
		WRITE_REG(hperh->perh->RXF1C, _flag);
		break;
	default:
		break;
	}

	return;
}
/**
  * @}
  */

/** @defgroup CAN_Public_Functions_Group4 Peripheral State and Error functions
  * @brief    CAN Peripheral State functions
  *
  * @verbatim
  ==============================================================================
            ##### Peripheral State and Error functions #####
  ==============================================================================
    [..]
    This subsection provides functions allowing to:
      (+) Check the CAN state.
      (+) Check CAN Errors detected during interrupt process

  * @endverbatim
  * @{
  */
/**
  * @brief  return the CAN state
  * @param  hperh: pointer to a can_handle_t structure.
  * @retval Status, see can_state_t.
  */
can_state_t ald_can_get_state(can_handle_t *hperh)
{
	return hperh->state;
}

/**
  * @brief  Return the CAN error code
  * @param  hperh: pointer to a can_handle_t structure.
  * @retval CAN Error Code
  */
can_error_t ald_can_get_error(can_handle_t *hperh)
{
	return hperh->err;
}

/**
  * @}
  */

/**
  * @}
  */

/** @defgroup CAN_Private_Functions CAN Private Functions
  * @{
  */

/**
  * @brief  Release rx FIFO
  * @param  hperh: pointer to a can_handle_t structure.
  * @param  num: Receive fifo number, CAN_RX_FIFO0 or CAN_RX_FIFO1
  * @retval None
  */
static void can_rx_fifo_release(can_handle_t *hperh, can_rx_fifo_t num)
{
	if (num == CAN_RX_FIFO0)
		SET_BIT(hperh->perh->RXF0, CAN_RXF0_FREE_MSK);
	else
		SET_BIT(hperh->perh->RXF1, CAN_RXF1_FREE_MSK);
}


/**
  * @brief  transmits a CAN frame message using interrupt.
  * @param  hperh: pointer to a can_handle_t structure.
  * @param  err: Error code, 0 - success, 1 - error.
  * @retval Status, see ald_status_t.
  */
static ald_status_t __can_send_by_it(can_handle_t *hperh, uint8_t err)
{
	ald_can_interrupt_config(hperh, CAN_IT_TXM, DISABLE);

	if (hperh->state == CAN_STATE_BUSY_TX) {
		ald_can_interrupt_config(hperh, CAN_IT_WARN, DISABLE);
		ald_can_interrupt_config(hperh, CAN_IT_PERR, DISABLE);
		ald_can_interrupt_config(hperh, CAN_IT_BOF, DISABLE);
		ald_can_interrupt_config(hperh, CAN_IT_PRERR, DISABLE);
		ald_can_interrupt_config(hperh, CAN_IT_ERR, DISABLE);
	}

	CLEAR_BIT(hperh->state, CAN_STATE_TX_MASK);

	if ((err == 0) && (hperh->tx_cplt_cbk))
		hperh->tx_cplt_cbk(hperh);

	if ((err) && (hperh->error_cbk != NULL)) {
		hperh->err = CAN_ERROR_UNK;
		hperh->error_cbk(hperh);
	}

	return OK;
}

/**
  * @brief  Check whether the data in the mailbox is contaminated.
  * @param  hperh:  Pointer to a can_handle_t structure.
  * @param  num: Specify the FIFO number
  * @retval status:
  *           - 0: Data is valid
  *           - 1: Data is invalid
  */
static int __can_rx_check(can_handle_t *hperh, can_rx_fifo_t num)
{
	int i;

	if (!(hperh->perh->RESERVED0[0] & 0x200000U))
		return 0;

	if (num == CAN_RX_FIFO0) {
		for (i = 0; i < 3; ++i) {
			if (hperh->perh->RXF0 & 0x3)
				SET_BIT(hperh->perh->RXF0, CAN_RXF0_FREE_MSK);
			else
				break;
		}
	}
	else {
		for (i = 0; i < 3; ++i) {
			if (hperh->perh->RXF1 & 0x3)
				SET_BIT(hperh->perh->RXF1, CAN_RXF1_FREE_MSK);
			else
				break;
		}
	}

	hperh->perh->RESERVED0[0] |= 0x200000U;
	return 1;
}

/**
  * @brief  Receives a correct CAN frame using interrupt.
  * @param  hperh:  Pointer to a can_handle_t structure.
  * @param  num: Specify the FIFO number
  * @retval Status, see ald_status_t.
  */
static ald_status_t __can_recv_by_it(can_handle_t *hperh, can_rx_fifo_t num)
{
	uint32_t stid, exid;
	can_rx_msg_t *_msg;

	if (__can_rx_check(hperh, num))
		return ERROR;

	stid = READ_BITS(hperh->perh->RxFIFO[num].RXFID, CAN_RXF0ID_STDID_MSK, CAN_RXF0ID_STDID_POSS);
	exid = READ_BITS(hperh->perh->RxFIFO[num].RXFID, CAN_RXF0ID_EXID_MSK, CAN_RXF0ID_EXID_POSS);

	_msg = num == CAN_RX_FIFO0 ? hperh->rx0_msg : hperh->rx1_msg;
	_msg->type = (can_id_type_t)READ_BITS(hperh->perh->RxFIFO[num].RXFID, CAN_RXF0ID_IDE_MSK, CAN_RXF0ID_IDE_POS);

	if (_msg->type == CAN_ID_STD)
		_msg->std = stid;
	else
		_msg->ext = (stid << 18) | exid;

	_msg->rtr     = (can_remote_req_t)READ_BITS(hperh->perh->RxFIFO[num].RXFID, CAN_RXF0ID_RTR_MSK, CAN_RXF0ID_RTR_POS);
	_msg->len     = READ_BITS(hperh->perh->RxFIFO[num].RXFINF, CAN_RXF0INF_DLEN_MSK, CAN_RXF0INF_DLEN_POSS);
	_msg->fmi     = READ_BITS(hperh->perh->RxFIFO[num].RXFINF, CAN_RXF0INF_FLTIDX_MSK, CAN_RXF0INF_FLTIDX_POSS);
	_msg->data[0] = hperh->perh->RxFIFO[num].RXFDL & 0xFF;
	_msg->data[1] = (hperh->perh->RxFIFO[num].RXFDL >> 8) & 0xFF;
	_msg->data[2] = (hperh->perh->RxFIFO[num].RXFDL >> 16) & 0xFF;
	_msg->data[3] = (hperh->perh->RxFIFO[num].RXFDL >> 24) & 0xFF;
	_msg->data[4] = hperh->perh->RxFIFO[num].RXFDH & 0xFF;
	_msg->data[5] = (hperh->perh->RxFIFO[num].RXFDH >> 8) & 0xFF;
	_msg->data[6] = (hperh->perh->RxFIFO[num].RXFDH >> 16) & 0xFF;
	_msg->data[7] = (hperh->perh->RxFIFO[num].RXFDH >> 24) & 0xFF;

	if (num == CAN_RX_FIFO0) {
		can_rx_fifo_release(hperh, CAN_RX_FIFO0);
		ald_can_interrupt_config(hperh, CAN_IT_FP0, DISABLE);
		CLEAR_BIT(hperh->state, CAN_STATE_RX0_MASK);
	}
	else {
		can_rx_fifo_release(hperh, CAN_RX_FIFO1);
		ald_can_interrupt_config(hperh, CAN_IT_FP1, DISABLE);
		CLEAR_BIT(hperh->state, CAN_STATE_RX1_MASK);
	}

	if (hperh->state == CAN_STATE_READY) {
		ald_can_interrupt_config(hperh, CAN_IT_WARN, DISABLE);
		ald_can_interrupt_config(hperh, CAN_IT_PERR, DISABLE);
		ald_can_interrupt_config(hperh, CAN_IT_BOF, DISABLE);
		ald_can_interrupt_config(hperh, CAN_IT_PRERR, DISABLE);
		ald_can_interrupt_config(hperh, CAN_IT_ERR, DISABLE);
	}

	if (hperh->rx_cplt_cbk)
		hperh->rx_cplt_cbk(hperh, num);

	return OK;
}
/**
 * @}
 */

#endif /* ALD_CAN */

/**
  * @}
  */

/**
  * @}
  */