aboutsummaryrefslogtreecommitdiff
path: root/KSDK_1.2.0/platform/drivers/src/flexcan/fsl_flexcan_driver.c
blob: aa4504c430bb189abfd1cdcfd0c8a183f8debe69 (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
/*
 * Copyright (c) 2013 - 2014, Freescale Semiconductor, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * o Redistributions of source code must retain the above copyright notice, this list
 *   of conditions and the following disclaimer.
 *
 * o Redistributions in binary form must reproduce the above copyright notice, this
 *   list of conditions and the following disclaimer in the documentation and/or
 *   other materials provided with the distribution.
 *
 * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
 *   contributors may be used to endorse or promote products derived from this
 *   software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "fsl_flexcan_driver.h"
#include "fsl_clock_manager.h"
#include "fsl_interrupt_manager.h"
#if FSL_FEATURE_SOC_FLEXCAN_COUNT

/*******************************************************************************
 * Definitions
 ******************************************************************************/

/*******************************************************************************
 * Variables
 ******************************************************************************/

/* Pointer to runtime state structure.*/
flexcan_state_t * g_flexcanStatePtr[CAN_INSTANCE_COUNT] = { NULL };

/*******************************************************************************
 * Private Functions
 ******************************************************************************/
static flexcan_status_t FLEXCAN_DRV_StartSendData(
                    uint8_t instance,
                    uint32_t mb_idx,
                    flexcan_data_info_t *tx_info,
                    uint32_t msg_id,
                    uint8_t *mb_data
                    );
static flexcan_status_t FLEXCAN_DRV_StartRxMessageBufferData(
                    uint8_t instance,
                    uint32_t mb_idx,
                    flexcan_msgbuff_t *data
                    );
static flexcan_status_t FLEXCAN_DRV_StartRxMessageFifoData(
                    uint8_t instance,
                    flexcan_msgbuff_t *data
                    );
static void FLEXCAN_DRV_CompleteSendData(uint32_t instance);
static void FLEXCAN_DRV_CompleteRxMessageBufferData(uint32_t instance);
static void FLEXCAN_DRV_CompleteRxMessageFifoData(uint32_t instance);

/*******************************************************************************
 * Code
 ******************************************************************************/

/*FUNCTION**********************************************************************
 *
 * Function Name : FLEXCAN_DRV_SetBitrate
 * Description   : Set FlexCAN baudrate.
 * This function will set up all the time segment values. Those time segment
 * values are passed in by the user and are based on the required baudrate.
 *
 *END**************************************************************************/
flexcan_status_t FLEXCAN_DRV_SetBitrate(uint8_t instance, flexcan_time_segment_t *bitrate)
{
    assert(instance < CAN_INSTANCE_COUNT);

    /* Set time segments*/
    FLEXCAN_HAL_SetTimeSegments(g_flexcanBase[instance], bitrate);

    return kStatus_FLEXCAN_Success;
}

/*FUNCTION**********************************************************************
 *
 * Function Name : FLEXCAN_DRV_GetBitrate
 * Description   : Get FlexCAN baudrate.
 * This function will be return the current bit rate settings
 *
 *END**************************************************************************/
flexcan_status_t  FLEXCAN_DRV_GetBitrate(uint8_t instance, flexcan_time_segment_t *bitrate)
{
    assert(instance < CAN_INSTANCE_COUNT);

    /* Get the time segments*/
    FLEXCAN_HAL_GetTimeSegments(g_flexcanBase[instance], bitrate);

    return kStatus_FLEXCAN_Success;
}

/*FUNCTION**********************************************************************
 *
 * Function Name : FLEXCAN_DRV_SetMasktype
 * Description   : Set RX masking type.
 * This function will set RX masking type as RX global mask or RX individual
 * mask.
 *
 *END**************************************************************************/
void  FLEXCAN_DRV_SetRxMaskType(uint8_t instance, flexcan_rx_mask_type_t type)
{
    assert(instance < CAN_INSTANCE_COUNT);

    FLEXCAN_HAL_SetRxMaskType(g_flexcanBase[instance], type);
}

/*FUNCTION**********************************************************************
 *
 * Function Name : FLEXCAN_DRV_SetRxFifoGlobalMask
 * Description   : Set Rx FIFO global mask as the 11-bit standard mask or the
 * 29-bit extended mask.
 *
 *END**************************************************************************/
flexcan_status_t FLEXCAN_DRV_SetRxFifoGlobalMask(
    uint8_t instance,
    flexcan_msgbuff_id_type_t id_type,
    uint32_t mask)
{
    assert(instance < CAN_INSTANCE_COUNT);

    CAN_Type * base = g_flexcanBase[instance];

    if (id_type == kFlexCanMsgIdStd)
    {
        /* Set standard global mask for RX FIOF*/
        FLEXCAN_HAL_SetRxFifoGlobalStdMask(base, mask);
    }
    else if (id_type == kFlexCanMsgIdExt)
    {
        /* Set extended global mask for RX FIFO*/
        FLEXCAN_HAL_SetRxFifoGlobalExtMask(base, mask);
    }
    else
    {
        return kStatus_FLEXCAN_InvalidArgument;
    }

    return kStatus_FLEXCAN_Success;
}

/*FUNCTION**********************************************************************
 *
 * Function Name : FLEXCAN_DRV_SetRxMbGlobalMask
 * Description   : Set Rx Message Buffer global mask as the 11-bit standard mask
 * or the 29-bit extended mask.
 *
 *END**************************************************************************/
flexcan_status_t FLEXCAN_DRV_SetRxMbGlobalMask(
    uint8_t instance,
    flexcan_msgbuff_id_type_t id_type,
    uint32_t mask)
{
    assert(instance < CAN_INSTANCE_COUNT);

    CAN_Type * base = g_flexcanBase[instance];

    if (id_type == kFlexCanMsgIdStd)
    {
        /* Set standard global mask for RX MB*/
        FLEXCAN_HAL_SetRxMsgBuffGlobalStdMask(base, mask);
    }
    else if (id_type == kFlexCanMsgIdExt)
    {
        /* Set extended global mask for RX MB*/
        FLEXCAN_HAL_SetRxMsgBuffGlobalExtMask(base, mask);
    }
    else
    {
        return kStatus_FLEXCAN_InvalidArgument;
    }

    return kStatus_FLEXCAN_Success;
}

/*FUNCTION**********************************************************************
 *
 * Function Name : FLEXCAN_DRV_SetRxIndividualMask
 * Description   : Set Rx individual mask as the 11-bit standard mask or the
 * 29-bit extended mask.
 *
 *END**************************************************************************/
flexcan_status_t FLEXCAN_DRV_SetRxIndividualMask(
    uint8_t instance,
    flexcan_msgbuff_id_type_t id_type,
    uint32_t mb_idx,
    uint32_t mask)
{
    assert(instance < CAN_INSTANCE_COUNT);

    CAN_Type * base = g_flexcanBase[instance];

    if (id_type == kFlexCanMsgIdStd)
    {
        /* Set standard individual mask*/
        return FLEXCAN_HAL_SetRxIndividualStdMask(base, mb_idx, mask);
    }
    else if (id_type == kFlexCanMsgIdExt)
    {
        /* Set extended individual mask*/
        return FLEXCAN_HAL_SetRxIndividualExtMask(base, mb_idx, mask);
    }
    else
    {
        return kStatus_FLEXCAN_InvalidArgument;
    }
}

/*FUNCTION**********************************************************************
 *
 * Function Name : FLEXCAN_DRV_Init
 * Description   : Initialize FlexCAN driver.
 * This function will select a source clock, reset FlexCAN module, set maximum
 * number of message buffers, initialize all message buffers as inactive, enable
 * RX FIFO if needed, mask all mask bits, disable all MB interrupts, enable
 * FlexCAN normal mode, and enable all the error interrupts if needed.
 *
 *END**************************************************************************/
flexcan_status_t FLEXCAN_DRV_Init(
   uint32_t instance,
   flexcan_state_t *state,
   const flexcan_user_config_t *data)
{
    assert(instance < CAN_INSTANCE_COUNT);
    assert(state);

    flexcan_status_t result;
    CAN_Type * base = g_flexcanBase[instance];

    /* Enable clock gate to FlexCAN module */
    CLOCK_SYS_EnableFlexcanClock(instance);

    /* Select a source clock for FlexCAN*/
    result = FLEXCAN_HAL_SelectClock(base, kFlexCanClkSourceIpbus);
    if (result)
    {
        return result;
    }

    /* Enable the CAN clock */
    FLEXCAN_HAL_Enable(base);

    /* Initialize FLEXCAN device */
    result = FLEXCAN_HAL_Init(base);
    if (result)
    {
        return result;
    }

    FLEXCAN_HAL_SetMaxMsgBuffNum(base, data->max_num_mb);
    if (data->is_rx_fifo_needed)
    {
        FLEXCAN_HAL_EnableRxFifo(base, data->num_id_filters);
    }
    /* Select mode */
    result = FLEXCAN_HAL_SetOperationMode(base, data->flexcanMode);
    if (result)
    {
        return result;
    }

    /* Init the interrupt sync object.*/
    OSA_SemaCreate(&state->txIrqSync, 0);
    OSA_SemaCreate(&state->rxIrqSync, 0);
    /* Enable FlexCAN interrupts.*/
    INT_SYS_EnableIRQ(g_flexcanWakeUpIrqId[instance]);
    INT_SYS_EnableIRQ(g_flexcanErrorIrqId[instance]);
    INT_SYS_EnableIRQ(g_flexcanBusOffIrqId[instance]);
    INT_SYS_EnableIRQ(g_flexcanOredMessageBufferIrqId[instance]);

    state->isTxBusy = false;
    state->isRxBusy = false;
    state->fifo_message = NULL;
    state->rx_mb_idx = 0;
    state->tx_mb_idx = 0;
    /* Save runtime structure pointers so irq handler can point to the correct state structure */
    g_flexcanStatePtr[instance] = state;

    return (kStatus_FLEXCAN_Success);
}

/*FUNCTION**********************************************************************
 *
 * Function Name : FLEXCAN_DRV_ConfigTxMb
 * Description   : Configure a Tx message buffer.
 * This function will first check if RX FIFO is enabled. If RX FIFO is enabled,
 * the function will make sure if the MB requested is not occupied by RX FIFO
 * and ID filter table. Then this function will set up the message buffer fields,
 * configure the message buffer code for Tx buffer as INACTIVE, and enable the
 * Message Buffer interrupt.
 *
 *END**************************************************************************/
flexcan_status_t FLEXCAN_DRV_ConfigTxMb(
    uint8_t instance,
    uint32_t mb_idx,
    flexcan_data_info_t *tx_info,
    uint32_t msg_id)
{
    assert(instance < CAN_INSTANCE_COUNT);

    flexcan_msgbuff_code_status_t cs;
    CAN_Type * base = g_flexcanBase[instance];
    flexcan_state_t * state = g_flexcanStatePtr[instance];

    state->tx_mb_idx = mb_idx;
    /* Initialize transmit mb*/
    cs.dataLen = tx_info->data_length;
    cs.msgIdType = tx_info->msg_id_type;
    cs.code = kFlexCanTXInactive;
    return FLEXCAN_HAL_SetTxMsgBuff(base, mb_idx, &cs, msg_id, NULL);
}

/*FUNCTION**********************************************************************
 *
 * Function Name : FLEXCAN_DRV_Send_Blocking
 * Description   : Set up FlexCAN Message buffer for transmitting data.
 * This function will set the MB CODE field as DATA for Tx buffer. Then this
 * function will copy user's buffer into the message buffer data area, and wait
 * for the Message Buffer interrupt.
 *
 *END**************************************************************************/
flexcan_status_t FLEXCAN_DRV_SendBlocking(
    uint8_t instance,
    uint32_t mb_idx,
    flexcan_data_info_t *tx_info,
    uint32_t msg_id,
    uint8_t *mb_data,
    uint32_t timeout_ms)
{
    assert(instance < CAN_INSTANCE_COUNT);

    flexcan_status_t result;
    flexcan_state_t * state = g_flexcanStatePtr[instance];
    CAN_Type * base  = g_flexcanBase[instance];
    osa_status_t syncStatus;

    state->isTxBlocking = true;
    result = FLEXCAN_DRV_StartSendData(instance, mb_idx, tx_info, msg_id, mb_data);
    if(result == kStatus_FLEXCAN_Success)
    {
        /* Enable message buffer interrupt*/
        FLEXCAN_HAL_SetMsgBuffIntCmd(base, mb_idx, true);
		/* Enable error interrupts */
		FLEXCAN_HAL_SetErrIntCmd(base,kFlexCanIntErr,true);
        do
        {
            syncStatus = OSA_SemaWait(&state->txIrqSync, timeout_ms);
        }while(syncStatus == kStatus_OSA_Idle);

        /* Wait for the interrupt*/
        if (syncStatus != kStatus_OSA_Success)
        {
            return kStatus_FLEXCAN_TimeOut;
        }
    }
    else
    {
        return result;
    }

    return (kStatus_FLEXCAN_Success);
}

/*FUNCTION**********************************************************************
 *
 * Function Name : FLEXCAN_DRV_Send
 * Description   : Set up FlexCAN Message buffer for transmitting data.
 * This function will set the MB CODE field as DATA for Tx buffer. Then this
 * function will copy user's buffer into the message buffer data area.
 *
 *END**************************************************************************/
flexcan_status_t FLEXCAN_DRV_Send(
    uint8_t instance,
    uint32_t mb_idx,
    flexcan_data_info_t *tx_info,
    uint32_t msg_id,
    uint8_t *mb_data)
{
    assert(instance < CAN_INSTANCE_COUNT);

    flexcan_status_t result;
    flexcan_state_t * state = g_flexcanStatePtr[instance];
    CAN_Type * base = g_flexcanBase[instance];

    state->isTxBlocking = false;

    result = FLEXCAN_DRV_StartSendData(instance, mb_idx, tx_info, msg_id, mb_data);
    if(result == kStatus_FLEXCAN_Success)
    {
        /* Enable message buffer interrupt*/
        FLEXCAN_HAL_SetMsgBuffIntCmd(base, mb_idx, true);
		/* Enable error interrupts */
		FLEXCAN_HAL_SetErrIntCmd(base,kFlexCanIntErr,true);
    }
    else
    {
        return result;
    }

    return (kStatus_FLEXCAN_Success);
}

/*FUNCTION**********************************************************************
 *
 * Function Name : FLEXCAN_DRV_ConfigMb
 * Description   : Configure a Rx message buffer.
 * This function will first check if RX FIFO is enabled. If RX FIFO is enabled,
 * the function will make sure if the MB requested is not occupied by RX FIFO
 * and ID filter table. Then this function will set up the message buffer fields,
 * configure the message buffer code for Rx message buffer as NOT_USED, enable
 * the Message Buffer interrupt, configure the message buffer code for Rx
 * message buffer as INACTIVE, copy user's buffer into the message buffer data
 * area, and configure the message buffer code for Rx message buffer as EMPTY.
 *
 *END**************************************************************************/
flexcan_status_t FLEXCAN_DRV_ConfigRxMb(
    uint8_t instance,
    uint32_t mb_idx,
    flexcan_data_info_t *rx_info,
    uint32_t msg_id)
{
    assert(instance < CAN_INSTANCE_COUNT);

    flexcan_status_t result;
    flexcan_msgbuff_code_status_t cs;
    CAN_Type * base = g_flexcanBase[instance];
    flexcan_state_t * state = g_flexcanStatePtr[instance];

    state->rx_mb_idx = mb_idx;
    cs.dataLen = rx_info->data_length;
    cs.msgIdType = rx_info->msg_id_type;

    /* Initialize rx mb*/
    cs.code = kFlexCanRXNotUsed;
    result = FLEXCAN_HAL_SetRxMsgBuff(base, mb_idx, &cs, msg_id);
    if (result)
    {
         return result;
    }

    /* Initialize receive MB*/
    cs.code = kFlexCanRXInactive;
    result = FLEXCAN_HAL_SetRxMsgBuff(base, mb_idx, &cs, msg_id);
    if (result)
    {
         return result;
    }

    /* Set up FlexCAN message buffer fields for receiving data*/
    cs.code = kFlexCanRXEmpty;
    return FLEXCAN_HAL_SetRxMsgBuff(base, mb_idx, &cs, msg_id);
}

/*FUNCTION**********************************************************************
 *
 * Function Name : FLEXCAN_DRV_ConfigRxFifo
 * Description   : Confgure RX FIFO ID filter table elements.
 * This function will confgure RX FIFO ID filter table elements, and enable RX
 * FIFO interrupts.
 *
 *END**************************************************************************/
flexcan_status_t FLEXCAN_DRV_ConfigRxFifo(
    uint8_t instance,
    flexcan_rx_fifo_id_element_format_t id_format,
    flexcan_id_table_t *id_filter_table)
{
    assert(instance < CAN_INSTANCE_COUNT);

    flexcan_status_t result;
    CAN_Type * base = g_flexcanBase[instance];

    /* Initialize rx fifo*/
    result = FLEXCAN_HAL_SetRxFifoFilter(base, id_format, id_filter_table);
    if(result)
    {
         return result;
    }

    return result;
}

/*FUNCTION**********************************************************************
 *
 * Function Name : FLEXCAN_DRV_RxMessageBufferBlocking
 * Description   : Start receive data after a Rx MB interrupt occurs.
 * This function will lock Rx MB after a Rx MB interrupt occurs.
 *
 *END**************************************************************************/
flexcan_status_t FLEXCAN_DRV_RxMessageBufferBlocking(
    uint8_t instance,
    uint32_t mb_idx,
    flexcan_msgbuff_t *data,
    uint32_t timeout_ms)
{
    assert(instance < CAN_INSTANCE_COUNT);
    assert(data);

    flexcan_status_t result;
    flexcan_state_t * state = g_flexcanStatePtr[instance];
    osa_status_t syncStatus;

    state->isRxBlocking = true;

    result = FLEXCAN_DRV_StartRxMessageBufferData(instance, mb_idx, data);
    if(result == kStatus_FLEXCAN_Success)
    {
        do
        {
             syncStatus = OSA_SemaWait(&state->rxIrqSync, timeout_ms);
        }while(syncStatus == kStatus_OSA_Idle);

        /* Wait for the interrupt*/
        if (syncStatus != kStatus_OSA_Success)
        {
            return kStatus_FLEXCAN_TimeOut;
        }
    }
    return result;
}

/*FUNCTION**********************************************************************
 *
 * Function Name : FLEXCAN_DRV_RxMessageBuffer
 * Description   : Start receive data after a Rx MB interrupt occurs.
 *
 *END**************************************************************************/
flexcan_status_t FLEXCAN_DRV_RxMessageBuffer(
    uint8_t instance,
    uint32_t mb_idx,
    flexcan_msgbuff_t *data)
{
    assert(instance < CAN_INSTANCE_COUNT);
    assert(data);

    flexcan_status_t result;
    flexcan_state_t * state = g_flexcanStatePtr[instance];

    state->isRxBlocking = false;

    result = FLEXCAN_DRV_StartRxMessageBufferData(instance, mb_idx, data);

    return result;
}

/*FUNCTION**********************************************************************
 *
 * Function Name : FLEXCAN_DRV_RxFifoBlocking
 * Description   : Start receive data after a Rx FIFO interrupt occurs.
 * This function will lock Rx FIFO after a Rx FIFO interrupt occurs
 *
 *END**************************************************************************/
flexcan_status_t FLEXCAN_DRV_RxFifoBlocking(
    uint8_t instance,
    flexcan_msgbuff_t *data,
    uint32_t timeout_ms)
{
    assert(instance < CAN_INSTANCE_COUNT);
    assert(data);

    flexcan_status_t result;
    flexcan_state_t * state = g_flexcanStatePtr[instance];
    osa_status_t syncStatus;

    state->isRxBlocking = true;
    result = FLEXCAN_DRV_StartRxMessageFifoData(instance, data);
    if(result == kStatus_FLEXCAN_Success)
    {
        do
        {
            syncStatus = OSA_SemaWait(&state->rxIrqSync, timeout_ms);
        } while(syncStatus == kStatus_OSA_Idle);

        /* Wait for the interrupt*/
        if (syncStatus != kStatus_OSA_Success)
        {
            return kStatus_FLEXCAN_TimeOut;
        }
    }

    return result;
}

/*FUNCTION**********************************************************************
 *
 * Function Name : FLEXCAN_DRV_RxFifoBlocking
 * Description   : Start receive data after a Rx FIFO interrupt occurs.
 *
 *END**************************************************************************/
flexcan_status_t FLEXCAN_DRV_RxFifo(
    uint8_t instance,
    flexcan_msgbuff_t *data)
{
    assert(instance < CAN_INSTANCE_COUNT);
    assert(data);

    flexcan_status_t result;
    flexcan_state_t * state = g_flexcanStatePtr[instance];

    state->isRxBlocking = false;
    result = FLEXCAN_DRV_StartRxMessageFifoData(instance, data);

    return result;
}

/*FUNCTION**********************************************************************
 *
 * Function Name : FLEXCAN_DRV_Deinit
 * Description   : Shutdown a FlexCAN module.
 * This function will disable all FlexCAN interrupts, and disable the FlexCAN.
 *
 *END**************************************************************************/
uint32_t FLEXCAN_DRV_Deinit(uint8_t instance)
{
    assert(instance < CAN_INSTANCE_COUNT);
    flexcan_state_t * state = g_flexcanStatePtr[instance];

    /* Destroy FlexCAN sema. */
    OSA_SemaDestroy(&state->txIrqSync);
    OSA_SemaDestroy(&state->rxIrqSync);
    /* Disable FlexCAN interrupts.*/
    INT_SYS_DisableIRQ(g_flexcanWakeUpIrqId[instance]);
    INT_SYS_DisableIRQ(g_flexcanErrorIrqId[instance]);
    INT_SYS_DisableIRQ(g_flexcanBusOffIrqId[instance]);
    INT_SYS_DisableIRQ(g_flexcanOredMessageBufferIrqId[instance]);

    /* Disable FlexCAN.*/
    FLEXCAN_HAL_Disable(g_flexcanBase[instance]);

    /* Clear the state pointer */
    g_flexcanStatePtr[instance] = NULL;

    /* Disable clock gate to FlexCAN module */
    CLOCK_SYS_DisableFlexcanClock(instance);
    return 0;
}
/*FUNCTION**********************************************************************
 *
 * Function Name : FLEXCAN_DRV_IRQHandler
 * Description   : Interrupt handler for FLEXCAN.
 * This handler read data from MB or FIFO, and then clear the interrupt flags.
 * This is not a public API as it is called whenever an interrupt occurs.
 *
 *END**************************************************************************/
void FLEXCAN_DRV_IRQHandler(uint8_t instance)
{
    volatile uint32_t flag_reg;
    uint32_t temp;
    CAN_Type * base = g_flexcanBase[instance];
    flexcan_state_t * state = g_flexcanStatePtr[instance];

    /* Get the interrupts that are enabled and ready */
    flag_reg = ((FLEXCAN_HAL_GetAllMsgBuffIntStatusFlag(base)) & CAN_IMASK1_BUFLM_MASK) &
                CAN_RD_IMASK1(base);

    /* Check Tx/Rx interrupt flag and clear the interrupt */
    if(flag_reg)
    {
        if ((flag_reg & 0x20) && CAN_BRD_MCR_RFEN(base))
        {
            if (state->fifo_message != NULL)
            {
                /* Get RX FIFO field values */
                FLEXCAN_HAL_ReadRxFifo(base, state->fifo_message);
                /* Complete receive data */
                FLEXCAN_DRV_CompleteRxMessageFifoData(instance);
                FLEXCAN_HAL_ClearMsgBuffIntStatusFlag(base, flag_reg);
            }
        }
        else
        {
           /* Check mailbox completed reception*/
            temp = (1 << state->rx_mb_idx);
            if (temp & flag_reg)
            {
                /* Unlock RX message buffer and RX FIFO*/
                FLEXCAN_HAL_LockRxMsgBuff(base, state->rx_mb_idx);
                /* Get RX MB field values*/
                FLEXCAN_HAL_GetMsgBuff(base, state->rx_mb_idx, state->mb_message);
                /* Unlock RX message buffer and RX FIFO*/
                FLEXCAN_HAL_UnlockRxMsgBuff(base);

                /* Complete receive data */
                FLEXCAN_DRV_CompleteRxMessageBufferData(instance);
                FLEXCAN_HAL_ClearMsgBuffIntStatusFlag(base, temp & flag_reg);
            }
            /* Check mailbox completed transmission*/
            temp = (1 << state->tx_mb_idx);
            if (temp & flag_reg)
            {
                /* Complete transmit data */
                FLEXCAN_DRV_CompleteSendData(instance);
                FLEXCAN_HAL_ClearMsgBuffIntStatusFlag(base, temp & flag_reg);
            }
        }
        /* Check mailbox completed transmission*/
        temp = (1 << state->tx_mb_idx);
        if (flag_reg & temp)
        {
                /* Complete transmit data */
                FLEXCAN_DRV_CompleteSendData(instance);
                FLEXCAN_HAL_ClearMsgBuffIntStatusFlag(base, temp & flag_reg);
        }
    }

    /* Clear all other interrupts in ERRSTAT register (Error, Busoff, Wakeup) */
    FLEXCAN_HAL_ClearErrIntStatusFlag(base);

    return;
}

/*FUNCTION**********************************************************************
 *
 * Function Name : FLEXCAN_DRV_GetTransmitStatus
 * Description   : This function returns whether the previous FLEXCAN receive is
 *                 completed.
 * When performing a non-blocking receive, the user can call this function to
 * ascertain the state of the current receive progress: in progress (or busy)
 * or complete (success).
 *
 *END**************************************************************************/
flexcan_status_t FLEXCAN_DRV_GetTransmitStatus(uint32_t instance)
{
    assert(instance < CAN_INSTANCE_COUNT);

    flexcan_state_t * state = g_flexcanStatePtr[instance];

    return (state->isTxBusy ? kStatus_FLEXCAN_TxBusy : kStatus_FLEXCAN_Success);
}

/*FUNCTION**********************************************************************
 *
 * Function Name : FLEXCAN_DRV_GetReceiveStatus
 * Description   : This function returns whether the previous FLEXCAN receive is
 *                 completed.
 * When performing a non-blocking receive, the user can call this function to
 * ascertain the state of the current receive progress: in progress (or busy)
 * or complete (success).
 *
 *END**************************************************************************/
flexcan_status_t FLEXCAN_DRV_GetReceiveStatus(uint32_t instance)
{
    assert(instance < CAN_INSTANCE_COUNT);

    flexcan_state_t * state = g_flexcanStatePtr[instance];

    return (state->isRxBusy ? kStatus_FLEXCAN_RxBusy : kStatus_FLEXCAN_Success);
}

/*FUNCTION**********************************************************************
 *
 * Function Name : FLEXCAN_DRV_AbortSendingData
 * Description   : This function ends a non-blocking FLEXCAN transmission early.
 * During a non-blocking FLEXCAN transmission, the user has the option to terminate
 * the transmission early if the transmission is still in progress.
 *
 *END**************************************************************************/
flexcan_status_t FLEXCAN_DRV_AbortSendingData(uint32_t instance)
{
    assert(instance < CAN_INSTANCE_COUNT);

    flexcan_state_t * state = g_flexcanStatePtr[instance];

    /* Check if a transfer is running. */
    if (!state->isTxBusy)
    {
        return kStatus_FLEXCAN_NoTransmitInProgress;
    }

    /* Stop the running transfer. */
    FLEXCAN_DRV_CompleteSendData(instance);

    return kStatus_FLEXCAN_Success;
}

/*FUNCTION**********************************************************************
 *
 * Function Name : FLEXCAN_DRV_AbortReceivingData
 * Description   : This function shuts down the FLEXCAN by disabling interrupts and
 *                 the transmitter/receiver.
 * This function disables the FLEXCAN interrupts, disables the transmitter and
 * receiver.
 *
 *END**************************************************************************/
flexcan_status_t FLEXCAN_DRV_AbortReceivingData(uint32_t instance)
{
    assert(instance < CAN_INSTANCE_COUNT);
    flexcan_state_t * state = g_flexcanStatePtr[instance];

    /* Check if a transfer is running. */
    if (!state->isRxBusy)
    {
        return kStatus_FLEXCAN_NoReceiveInProgress;
    }

    /* Stop the running transfer. */
    FLEXCAN_DRV_CompleteRxMessageBufferData(instance);

    return kStatus_FLEXCAN_Success;
}


/*FUNCTION**********************************************************************
 *
 * Function Name : FLEXCAN_DRV_StartSendData
 * Description   : Initiate (start) a transmit by beginning the process of
 * sending data.
 * This is not a public API as it is called from other driver functions.
 *
 *END**************************************************************************/
static flexcan_status_t FLEXCAN_DRV_StartSendData(
                    uint8_t instance,
                    uint32_t mb_idx,
                    flexcan_data_info_t *tx_info,
                    uint32_t msg_id,
                    uint8_t *mb_data
                    )
{
    flexcan_status_t result;
    flexcan_msgbuff_code_status_t cs;
    flexcan_state_t * state = g_flexcanStatePtr[instance];
    CAN_Type * base = g_flexcanBase[instance];

    if (state->isTxBusy)
    {
        return kStatus_FLEXCAN_TxBusy;
    }
    state->isTxBusy = true;

    state->tx_mb_idx = mb_idx;
    cs.dataLen = tx_info->data_length;
    cs.msgIdType = tx_info->msg_id_type;

    /* Set up FlexCAN message buffer for transmitting data*/
    cs.code = kFlexCanTXData;
    result = FLEXCAN_HAL_SetTxMsgBuff(base, mb_idx, &cs, msg_id, mb_data);
    return result;
}

/*FUNCTION**********************************************************************
 *
 * Function Name : FLEXCAN_DRV_StartRxMessageBufferData
 * Description   : Initiate (start) a receive by beginning the process of
 * receiving data and enabling the interrupt.
 * This is not a public API as it is called from other driver functions.
 *
 *END**************************************************************************/
static flexcan_status_t FLEXCAN_DRV_StartRxMessageBufferData(
                    uint8_t instance,
                    uint32_t mb_idx,
                    flexcan_msgbuff_t *data
                    )
{
    flexcan_status_t result;
    CAN_Type * base = g_flexcanBase[instance];
    flexcan_state_t * state = g_flexcanStatePtr[instance];

    /* Start receiving mailbox */
    if(state->isRxBusy)
    {
        return kStatus_FLEXCAN_RxBusy;
    }
    state->isRxBusy = true;
    state->mb_message = data;

    /* Enable MB interrupt*/
    result = FLEXCAN_HAL_SetMsgBuffIntCmd(base, mb_idx, true);
	/* Enable error interrupts */
	FLEXCAN_HAL_SetErrIntCmd(base,kFlexCanIntErr,true);

    return result;
}


/*FUNCTION**********************************************************************
 *
 * Function Name : FLEXCAN_DRV_StartRxMessageFifoData
 * Description   : Initiate (start) a receive by beginning the process of
 * receiving data and enabling the interrupt.
 * This is not a public API as it is called from other driver functions.
 *
 *END**************************************************************************/
static flexcan_status_t FLEXCAN_DRV_StartRxMessageFifoData(
                    uint8_t instance,
                    flexcan_msgbuff_t *data
                    )
{
    flexcan_status_t result;
    CAN_Type * base = g_flexcanBase[instance];
    flexcan_state_t * state = g_flexcanStatePtr[instance];

    /* Start receiving fifo */
    if(state->isRxBusy)
    {
        return kStatus_FLEXCAN_RxBusy;
    }
    state->isRxBusy = true;

    /* This will get filled by the interrupt handler */
    state->fifo_message = data;

    /* Enable RX FIFO interrupts*/
    for (uint8_t i = 5; i <= 7; i++)
    {
        result = FLEXCAN_HAL_SetMsgBuffIntCmd(base, i, true);
        if(result)
        {
             return result;
        }
    }
	/* Enable error interrupts */
	FLEXCAN_HAL_SetErrIntCmd(base,kFlexCanIntErr,true);

    return kStatus_FLEXCAN_Success;
}

/*FUNCTION**********************************************************************
 *
 * Function Name : FLEXCAN_DRV_CompleteSendData
 * Description   : Finish up a transmit by completing the process of sending
 * data and disabling the interrupt.
 * This is not a public API as it is called from other driver functions.
 *
 *END**************************************************************************/
static void FLEXCAN_DRV_CompleteSendData(uint32_t instance)
{
    assert(instance < CAN_INSTANCE_COUNT);
    CAN_Type * base = g_flexcanBase[instance];
    flexcan_state_t * state = g_flexcanStatePtr[instance];

    /* Disable the transmitter data register empty interrupt */
    FLEXCAN_HAL_SetMsgBuffIntCmd(base, state->tx_mb_idx, false);
	/* Disable error interrupts */
	FLEXCAN_HAL_SetErrIntCmd(base,kFlexCanIntErr,false);

    /* Signal the synchronous completion object. */
    if (state->isTxBlocking)
    {
        OSA_SemaPost(&state->txIrqSync);
    }

    /* Update the information of the module driver state */
    state->isTxBusy = false;
}

/*FUNCTION**********************************************************************
 *
 * Function Name : FLEXCAN_DRV_CompleteRxMessageBufferData
 * Description   : Finish up a receive by completing the process of receiving
 * data and disabling the interrupt.
 * This is not a public API as it is called from other driver functions.
 *
 *END**************************************************************************/
static void FLEXCAN_DRV_CompleteRxMessageBufferData(uint32_t instance)
{
    assert(instance < CAN_INSTANCE_COUNT);

    CAN_Type * base = g_flexcanBase[instance];
    flexcan_state_t * state = g_flexcanStatePtr[instance];

    FLEXCAN_HAL_SetMsgBuffIntCmd(base, state->rx_mb_idx, false);
	/* Disable error interrupts */
	FLEXCAN_HAL_SetErrIntCmd(base,kFlexCanIntErr,false);

    /* Signal the synchronous completion object. */
    if (state->isRxBlocking)
    {
        OSA_SemaPost(&state->rxIrqSync);
    }
    /* Update the information of the module driver state */
    state->isRxBusy = false;
}

/*FUNCTION**********************************************************************
 *
 * Function Name : FLEXCAN_DRV_CompleteRxMessageFifoData
 * Description   : Finish up a receive by completing the process of receiving
 * data and disabling the interrupt.
 * This is not a public API as it is called from other driver functions.
 *
 *END**************************************************************************/
static void FLEXCAN_DRV_CompleteRxMessageFifoData(uint32_t instance)
{
    assert(instance < CAN_INSTANCE_COUNT);
    uint8_t i;

    CAN_Type * base = g_flexcanBase[instance];
    flexcan_state_t * state = g_flexcanStatePtr[instance];

    for (i = 5; i <= 7; i++)
    {
        FLEXCAN_HAL_SetMsgBuffIntCmd(base, i, false);
    }
	/* Disable error interrupts */
	FLEXCAN_HAL_SetErrIntCmd(base,kFlexCanIntErr,false);

    /* Clear fifo message*/
    state->fifo_message = NULL;

    /* Update status for receive by using fifo*/
    state->isRxBusy = false;

    if(state->isRxBlocking)
    {
        OSA_SemaPost(&state->rxIrqSync);
    }
}
#endif
/*******************************************************************************
 * EOF
 ******************************************************************************/