summaryrefslogtreecommitdiff
path: root/bsp/mm32f103x/Libraries/MM32F103/HAL_lib/src/HAL_can.c
blob: 49978356c11fc21fd033bbd50313a4411960bf68 (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
/**
******************************************************************************
* @file    HAL_can.c
* @author  AE Team
* @version V1.1.0
* @date    28/08/2019
* @brief   This file provides all the CAN firmware functions.
******************************************************************************
* @attention
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, MindMotion SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
* <h2><center>&copy; COPYRIGHT 2019 MindMotion</center></h2>
******************************************************************************
*/

/* Includes ------------------------------------------------------------------*/
#include "HAL_can.h"
#include "HAL_rcc.h"

/**
* @brief: Deinitialinzes the CAN registers to their default reset values
* @retval: None
*/
void CAN_DeInit(CAN_TypeDef* CANx)
{
    /* Check the parameters */
    assert_param(IS_CAN_ALL_PERIPH(CANx));
    switch (*(uint32_t*)&CANx)
    {
        case CAN1_BASE:
            /* Enable CAN1 reset state */
            RCC_APB1PeriphResetCmd(RCC_APB1Periph_CAN1, ENABLE);
            /* Release CAN1 from reset state */
            RCC_APB1PeriphResetCmd(RCC_APB1Periph_CAN1, DISABLE);
            break;
        default:
            break;
    }

}

/**
* @brief  Initializes the CAN peripheral according to the specified
*   parameters in the CAN_InitStruct.
* @param CANx: where x can be 1 to select the CAN peripheral.
* @param CAN_InitStruct: pointer to a CAN_InitTypeDef structure that
*   contains the configuration information for the CAN peripheral.
* @retval : Constant indicates initialization succeed which will be
*   CANINITFAILED or CANINITOK.
*/
uint8_t CAN_Init(CAN_TypeDef* CANx, CAN_Basic_InitTypeDef* CAN_Basic_InitStruct)
{
    uint8_t InitStatus = CANINITFAILED;

    /* Check the parameters */
    assert_param(IS_FUNCTIONAL_STATE(CAN_Basic_InitStruct->SJW));
    assert_param(IS_FUNCTIONAL_STATE(CAN_Basic_InitStruct->BRP));
    assert_param(IS_FUNCTIONAL_STATE(CAN_Basic_InitStruct->SAM));
    assert_param(IS_FUNCTIONAL_STATE(CAN_Basic_InitStruct->TESG2));
    assert_param(IS_FUNCTIONAL_STATE(CAN_Basic_InitStruct->TESG1));

    CANx->BTR0 = ((uint32_t)(CAN_Basic_InitStruct->SJW) << 6) | ((uint32_t)(CAN_Basic_InitStruct->BRP));
    CANx->BTR1 = ((uint32_t)(CAN_Basic_InitStruct->SAM) << 7) | ((uint32_t)(CAN_Basic_InitStruct->TESG2) << 4) | \
                 ((uint32_t)(CAN_Basic_InitStruct->TESG1));
    if(CAN_Basic_InitStruct->GTS == ENABLE)
    {
        CANx->CMR |= (uint32_t)CAN_SleepMode;
    }
    else
    {
        CANx->CMR &= ~(uint32_t)CAN_SleepMode;
    }

    CANx->CDR |= ((CAN_Basic_InitStruct->CBP) << 6) | ((CAN_Basic_InitStruct->RXINTEN) << 5) | \
                 ((CAN_Basic_InitStruct->CLOSE_OPEN_CLK) << 3) | (CAN_Basic_InitStruct->CDCLK);

    InitStatus = CANINITOK;
    return InitStatus;
}

/**
* @brief  Configures the CAN_Basic reception filter according to the specified
*         parameters in the CAN_Basic_FilterInitStruct.
* @param  CAN_Basic_FilterInitStruct: pointer to a CAN_Basic_FilterInitTypeDef structure that
*         contains the configuration information.
* @retval None
*/
void CAN_FilterInit(CAN_Basic_FilterInitTypeDef* CAN_Basic_FilterInitStruct)
{
    /* Filter Mode */
    CAN1->ACR = CAN_Basic_FilterInitStruct->CAN_FilterId;
    CAN1->AMR = CAN_Basic_FilterInitStruct->CAN_FilterMaskId;
}


/**
* @brief  Fills each CAN_Basic_InitStruct member with its default value.
* @param CAN_Basic_InitStruct : pointer to a CAN_Basic_InitTypeDef structure
*   which will be initialized.
* @retval : None
*/
void CAN_StructInit(CAN_Basic_InitTypeDef* CAN_Basic_InitStruct)
{
    /*--------------- Reset CAN_Basic init structure parameters values -----------------*/


    /* initialize the BRP member(where can be set with (0..63))*/
    CAN_Basic_InitStruct->BRP = 0x0;
    /* initialize the SJW member(where can be set with (0..3)) */
    CAN_Basic_InitStruct->SJW = 0x0;
    /* Initialize the TESG1 member(where can be set with (0..15)) */
    CAN_Basic_InitStruct->TESG1 = 0x0;
    /* Initialize the TESG2 member(where can be set with(0..7)) */
    CAN_Basic_InitStruct->TESG2 = 0x0;
    /* Initialize the SAM member(where can be set (SET or RESET)) */
    CAN_Basic_InitStruct->SAM = RESET;
    /* Initialize the GTS member to Sleep Mode(where can be set (ENABLE or DISABLE)) */
    CAN_Basic_InitStruct->GTS = DISABLE;
    /* Initialize the external pin CLKOUT frequence */
    CAN_Basic_InitStruct->CDCLK = 0x0;
    /* Initialize the external clk is open or close */
    CAN_Basic_InitStruct->CLOSE_OPEN_CLK = 0x0;
    /* Initialize the TX1 pin work as rx interrupt output */
    CAN_Basic_InitStruct->RXINTEN = 0x0;
    /* Initialize the CBP of CDR register */
    CAN_Basic_InitStruct->CBP = 0x0;
}

/**
* @brief  Enables or disables the specified CAN interrupts.
* @param CANx: where x can be 1 to select the CAN peripheral.
* @param CAN_IT: specifies the CAN interrupt sources to be enabled or
*   disabled.
*   This parameter can be: CAN_IT_OIE, CAN_IT_EIE, CAN_IT_TIE,
*   CAN_IT_RIE,.
* @param Newstate: new state of the CAN interrupts.
*   This parameter can be: ENABLE or DISABLE.
* @retval : None.
*/
void CAN_ITConfig(CAN_TypeDef* CANx, uint32_t CAN_IT, FunctionalState Newstate)
{
    /* Check the parameters */
    assert_param(IS_CAN_ALL_PERIPH(CANx));
    assert_param(IS_CAN_ITConfig(CAN_IT));
    assert_param(IS_FUNCTIONAL_STATE(Newstate));
    if (Newstate != DISABLE)
    {
        /* Enable the selected CAN interrupt */
        CANx->CR |= CAN_IT;
    }
    else
    {
        /* Disable the selected CAN interrupt */
        CANx->CR &= ~CAN_IT;
    }
}


/**
* @brief  Initiates and transmits a CAN frame message.
* @param CANx: where x can be 1 to select the CAN peripheral.
* @param  TxMessage: pointer to a structure which contains CAN Id, CAN DLC and CAN data.
* @retval CANTXOK if the CAN driver transmits the message
*/
uint8_t CAN_Transmit(CAN_TypeDef* CANx, CanBasicTxMsg* BasicTxMessage)
{
    /* TXOK and TME bits */
    uint8_t state = 0;
    /* Check the parameters */
    assert_param(IS_CAN_RTR(BasicTxMessage->RTR));
    assert_param(IS_CAN_DLC(BasicTxMessage->DLC));

    CANx->TXID0 = (BasicTxMessage->IDH);
    CANx->TXID1 = (BasicTxMessage->IDL << 5) | (BasicTxMessage->RTR << 4) | (BasicTxMessage->DLC);
    if((FunctionalState)(BasicTxMessage->RTR) != ENABLE)
    {
        CANx->TXDR0 = BasicTxMessage->Data[0];
        CANx->TXDR1 = BasicTxMessage->Data[1];
        CANx->TXDR2 = BasicTxMessage->Data[2];
        CANx->TXDR3 = BasicTxMessage->Data[3];
        CANx->TXDR4 = BasicTxMessage->Data[4];
        CANx->TXDR5 = BasicTxMessage->Data[5];
        CANx->TXDR6 = BasicTxMessage->Data[6];
        CANx->TXDR7 = BasicTxMessage->Data[7];
    }

    CANx->CMR = CAN_CMR_TR;

    return state;
}

/**
* @brief  Cancels a transmit request.
* @param CANx: where x can be 1 to select the CAN peripheral.

* @retval None
*/
void CAN_CancelTransmit(CAN_TypeDef* CANx)
{
    /* Check the parameters */
    assert_param(IS_CAN_ALL_PERIPH(CANx));
    assert_param(IS_CAN_TRANSMITMAILBOX(Mailbox));
    /* abort transmission */
    CANx->CMR = CAN_AT;

}

/**
* @brief  Releases the specified receive FIFO.
* @param CANx: where x can be 1 to select the CAN peripheral.
* @retval None
*/
void CAN_FIFORelease(CAN_TypeDef* CANx)
{
    /* Check the parameters */
    assert_param(IS_CAN_ALL_PERIPH(CANx));

    /* Release FIFO */
    CANx->CMR |= (uint32_t)CAN_RRB;

}

/**
* @brief  Receives a correct CAN frame.
* @param CANx: where x can be 1 to select the CAN peripheral.
* @param  RxMessage: pointer to a structure receive frame which contains CAN Id,
*         CAN DLC, CAN data and FMI number.
* @retval None
*/
void CAN_Receive(CAN_TypeDef* CANx, CanBasicRxMsg* BasicRxMessage)
{
    uint16_t tempid;
    /* Check the parameters */
    assert_param(IS_CAN_ALL_PERIPH(CANx));
    assert_param(IS_CAN_FIFO(FIFONumber));
    BasicRxMessage->RTR = (uint8_t)((CANx->RXID1) >> 4) & 0x1;
    BasicRxMessage->DLC = (uint8_t)((CANx->RXID1) & 0xf);
    tempid = (uint16_t)(((CANx->RXID1) & 0xe0) >> 5);
    tempid |= (uint16_t)(CANx->RXID0 << 3);
    BasicRxMessage->ID = tempid;
    BasicRxMessage->Data[0] = CAN1->RXDR0;
    BasicRxMessage->Data[1] = CAN1->RXDR1;
    BasicRxMessage->Data[2] = CAN1->RXDR2;
    BasicRxMessage->Data[3] = CAN1->RXDR3;
    BasicRxMessage->Data[4] = CAN1->RXDR4;
    BasicRxMessage->Data[5] = CAN1->RXDR5;
    BasicRxMessage->Data[6] = CAN1->RXDR6;
    BasicRxMessage->Data[7] = CAN1->RXDR7;
    CAN_FIFORelease( CANx);
}

/**
* @brief: Select the Sleep mode or not in Basic workmode
* @param: NewState to go into the Sleep mode or go out
* @retval: None
*/
uint8_t CAN_Sleep(CAN_TypeDef* CANx)
{
    uint8_t sleepstatus = CANSLEEPFAILED;

    /* Check the parameters */
    assert_param(IS_CAN_ALL_PERIPH(CANx));
    CANx->CMR |= CAN_SleepMode;
    if((CANx->CMR & 0x10) == CAN_SleepMode)
    {
        sleepstatus =  CANSLEEPOK;
    }
    /* At this step, sleep mode status */
    return (uint8_t)sleepstatus;

}

/**
* @brief  Wakes the CAN up.
* @param CANx: where x can be 1 to select the CAN peripheral.
* @retval : CANWAKEUPOK if sleep mode left, CANWAKEUPFAILED in an other
*   case.
*/
uint8_t CAN_WakeUp(CAN_TypeDef* CANx)
{
    uint8_t wakeupstatus = CANWAKEUPFAILED;

    /* Check the parameters */
    assert_param(IS_CAN_ALL_PERIPH(CANx));

    /* Wake up request */
    CANx->CMR &= ~CAN_SleepMode;

    /* Sleep mode status */
    if((CANx->CMR & 0x01) == 0)
    {
        /* Sleep mode exited */
        wakeupstatus = CANWAKEUPOK;
    }
    /* At this step, sleep mode status */
    return (uint8_t)wakeupstatus;
}


/**
* @brief  Checks whether the specified CAN flag is set or not.
* @param  CANx: where x can be 1 or 2 to to select the CAN peripheral.
* @param  CAN_FLAG: specifies the flag to check.
*          This parameter can be one of the following values:
*            @arg CAN_STATUS_RBS: Receive buffer status
*            @arg CAN_STATUS_DOS: Data overflow status
*            @arg CAN_STATUS_TBS: Transmit buffer status
*            @arg CAN_STATUS_TCS: Transmit complete status
*            @arg CAN_STATUS_RS: Receiving status
*            @arg CAN_STATUS_TS: Transmiting status
*            @arg CAN_STATUS_ES: Error status
*            @arg CAN_STATUS_BS: bus status, close or open
* @retval The new state of CAN_FLAG (SET or RESET).
*/
FlagStatus CAN_GetFlagStatus(CAN_TypeDef* CANx, uint32_t CAN_FLAG)
{
    FlagStatus bitstatus = RESET;

    /* Check the parameters */
    assert_param(IS_CAN_ALL_PERIPH(CANx));
    assert_param(IS_CAN_GET_FLAG(CAN_FLAG));


    if((CANx->SR & CAN_FLAG) == CAN_FLAG)
    {
        /* CAN_FLAG is set */
        bitstatus = SET;
    }
    else
    {
        /* CAN_FLAG is reset */
        bitstatus = RESET;
    }
    /* Return the CAN_FLAG status */
    return bitstatus;
}

/**
* @brief  Checks whether the specified CAN interrupt has occurred or not.
* @param CANx: where x can be 1 to select the CAN peripheral.
* @param  CAN_IT: specifies the CAN interrupt source to check.
*          This parameter can be one of the following values:
*            @arg CAN_IT_RI: Receive FIFO not empty Interrupt
*            @arg CAN_IT_TI: Transmit Interrupt
*            @arg CAN_IT_EI: ERROR Interrupt
*            @arg CAN_IT_DOI: Data voerflow Interrupt
*            @arg CAN_IT_WUI: Wakeup Interrupt
*            @arg CAN_IT_ALL: use it can enble all Interrupt
* @retval The current state of CAN_IT (SET or RESET).
*/
ITStatus CAN_GetITStatus(CAN_TypeDef* CANx, uint32_t CAN_IT)
{
    ITStatus itstatus = RESET;

    /* Check the parameters */
    assert_param(IS_CAN_ALL_PERIPH(CANx));
    assert_param(IS_CAN_IT(CAN_IT));

    /* check the interrupt enable bit */
    if((CANx->IR & CAN_IT) != CAN_IT)
    {
        itstatus = RESET;
    }
    else
    {
        itstatus = SET;
    }

    return itstatus;
}


/**
* @brief: Select the can work as peli mode or basic mode
* @param  CANx: where x can be 1 or 2 to to select the CAN peripheral.
* @param  CAN_MODE: specifies the work mode:CAN_BASICMode,CAN_PELIMode
* @retval: None
*/
void CAN_Mode_Cmd(CAN_TypeDef* CANx, uint32_t CAN_MODE)
{
    /* Check the parameters */
    assert_param(IS_CAN_ALL_PERIPH(CANx));

    CANx->CDR |= CAN_MODE;
}


/**
* @brief: Select the Reset mode or not
* @param  CANx: where x can be 1 or 2 to to select the CAN peripheral.
* @param: NewState to go into the Reset mode or go out
* @retval: None
*/
void CAN_ResetMode_Cmd(CAN_TypeDef* CANx, FunctionalState NewState)
{
    /* Check the parameters */
    assert_param(IS_CAN_ALL_PERIPH(CANx));

    if(NewState == ENABLE)
    {
        CANx->CR |= CAN_ResetMode;
    }
    else
    {
        CANx->CR &= ~CAN_ResetMode;
    }
}

/**
* @brief  Clear the data overflow.
* @param  CANx: where x can be 1 or 2 to to select the CAN peripheral.
* @retval None
*/
void CAN_ClearDataOverflow(CAN_TypeDef* CANx)
{
    /* Check the parameters */
    assert_param(IS_CAN_ALL_PERIPH(CANx));

    CANx->CMR |= (uint32_t)CAN_CDO;

}

/**
* @brief  Clears the CAN's IT pending.
* @param  CANx: where x can be 1 or 2 to to select the CAN peripheral.
* @retval None
*/
void CAN_ClearITPendingBit(CAN_TypeDef* CANx)
{
    uint32_t temp = 0;
    temp = temp;
    temp = CANx->IR; //read this register clear all interrupt

}



/**
* @brief: Select the Sleep mode or not in Peli workmode
* @param: NewState to go into the Sleep mode or go out
* @retval: None
*/
void CAN_Peli_SleepMode_Cmd(FunctionalState NewState)
{
    if(NewState == ENABLE)
        CAN1_PELI->MOD |= CAN_SleepMode;
    else
        CAN1_PELI->MOD &= ~CAN_SleepMode;
}


/**
* @brief  Fills each CAN1_PELI_InitStruct member with its default value.
* @param CAN_Peli_InitStruct : pointer to a CAN_Peli_InitTypeDef structure
*   which will be initialized.
* @retval : None
*/
void CAN_Peli_StructInit(CAN_Peli_InitTypeDef* CAN_Peli_InitStruct)
{
    /*--------------- Reset CAN_Peli init structure parameters values -----------------*/

    /* initialize the BRP member(where can be set with (0..63))*/
    CAN_Peli_InitStruct->BRP = 0x0;
    /* initialize the SJW member(where can be set with (0..3)) */
    CAN_Peli_InitStruct->SJW = 0x0;
    /* Initialize the TESG1 member(where can be set with (0..15)) */
    CAN_Peli_InitStruct->TESG1 = 0x0;
    /* Initialize the TESG2 member(where can be set with(0..7)) */
    CAN_Peli_InitStruct->TESG2 = 0x0;
    /* Initialize the SAM member(where can be set (SET or RESET)) */
    CAN_Peli_InitStruct->SAM = RESET;
    /* Initialize the LOM member*/
    CAN_Peli_InitStruct->LOM = DISABLE;
    /* Initialize the STM member*/
    CAN_Peli_InitStruct->STM = DISABLE;
    /* Initialize the SM member*/
    CAN_Peli_InitStruct->SM = DISABLE;
    CAN_Peli_InitStruct->SRR = DISABLE;
    CAN_Peli_InitStruct->EWLR = 0x96;
}

/**
* @brief  Initializes the CAN_Peli peripheral according to the specified
*         parameters in the CAN_Peli_InitStruct.
* @param  CAN_Basic_InitStruct: pointer to a CAN_Peli_InitTypeDef structure that contains
*         the configuration information for the CAN peripheral in the peli workmode.
* @retval None
*/
void CAN_Peli_Init(CAN_Peli_InitTypeDef* CAN_Peli_InitStruct)
{
    /* Check the parameters */
    assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->SJW));
    assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->BRP));
    assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->SAM));
    assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->TESG2));
    assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->TESG1));

    CAN1_PELI->BTR0 = ((uint32_t)CAN_Peli_InitStruct->SJW << 6) | ((uint32_t)CAN_Peli_InitStruct->BRP);
    CAN1_PELI->BTR1 = ((uint32_t)CAN_Peli_InitStruct->SAM << 7) | ((uint32_t)CAN_Peli_InitStruct->TESG2 << 4) | \
                      ((uint32_t)CAN_Peli_InitStruct->TESG1);
    if(CAN_Peli_InitStruct->LOM == ENABLE)
        CAN1_PELI->MOD |= (uint32_t)CAN_ListenOnlyMode;
    else
        CAN1_PELI->MOD &= ~(uint32_t)CAN_ListenOnlyMode;
    if(CAN_Peli_InitStruct->STM == ENABLE)
        CAN1_PELI->MOD |= (uint32_t)CAN_SeftTestMode;
    else
        CAN1_PELI->MOD &= ~(uint32_t)CAN_SeftTestMode;
    if(CAN_Peli_InitStruct->SM == ENABLE)
        CAN1_PELI->MOD |= (uint32_t)CAN_SleepMode;
    else
        CAN1_PELI->MOD &= ~(uint32_t)CAN_SleepMode;
    CAN1_PELI->EWLR = (uint32_t)CAN_Peli_InitStruct->EWLR;
}



/**
* @brief  Configures the CAN_Peli reception filter according to the specified
*         parameters in the CAN_Peli_FilterInitStruct.
* @param  CAN_Peli_FilterInitStruct: pointer to a CAN_Peli_FilterInitTypeDef structure that
*         contains the configuration information.
* @retval None
*/
void CAN_Peli_FilterInit(CAN_Peli_FilterInitTypeDef* CAN_Peli_FilterInitStruct)
{
    if(CAN_Peli_FilterInitStruct->AFM == CAN_FilterMode_Singal)
        CAN1_PELI->MOD |= (uint32_t)CAN_FilterMode_Singal;
    else
        CAN1_PELI->MOD &= (uint32_t)CAN_FilterMode_Double;

    CAN1_PELI->FF = CAN_Peli_FilterInitStruct->CAN_FilterId0;
    CAN1_PELI->ID0 = CAN_Peli_FilterInitStruct->CAN_FilterId1;
    CAN1_PELI->ID1 = CAN_Peli_FilterInitStruct->CAN_FilterId2;
    CAN1_PELI->DATA0 = CAN_Peli_FilterInitStruct->CAN_FilterId3;

    CAN1_PELI->DATA1 = CAN_Peli_FilterInitStruct->CAN_FilterMaskId0;
    CAN1_PELI->DATA2 = CAN_Peli_FilterInitStruct->CAN_FilterMaskId1;
    CAN1_PELI->DATA3 = CAN_Peli_FilterInitStruct->CAN_FilterMaskId2;
    CAN1_PELI->DATA4 = CAN_Peli_FilterInitStruct->CAN_FilterMaskId3;
}

/**
* @brief  Fills each CAN_Peli_FilterInitStruct member with its default value.
* @param  CAN_Peli_FilterInitStruct: pointer to a CAN_InitTypeDef structure which ill be initialized.
* @retval None
*/
void CAN_Peli_FilterStructInit(CAN_Peli_FilterInitTypeDef* CAN_Peli_FilterInitStruct)
{
    CAN_Peli_FilterInitStruct->CAN_FilterId0 = 0;
    CAN_Peli_FilterInitStruct->CAN_FilterId1 = 0;
    CAN_Peli_FilterInitStruct->CAN_FilterId2 = 0;
    CAN_Peli_FilterInitStruct->CAN_FilterId3 = 0;

    CAN_Peli_FilterInitStruct->CAN_FilterMaskId0 = 0;
    CAN_Peli_FilterInitStruct->CAN_FilterMaskId1 = 0;
    CAN_Peli_FilterInitStruct->CAN_FilterMaskId2 = 0;
    CAN_Peli_FilterInitStruct->CAN_FilterMaskId3 = 0;
}


/**
* @brief  Initiates and transmits a CAN frame message.
* @param  TxMessage: pointer to a structure which contains CAN Id, CAN DLC and CAN data.
* @retval None
*/
void CAN_Peli_Transmit(CanPeliTxMsg* PeliTxMessage)
{
    /* Check the parameters */
    assert_param(IS_CAN_RTR(PeliTxMessage->RTR));
    assert_param(IS_CAN_DLC(PeliTxMessage->DLC));

    CAN1_PELI->FF = (PeliTxMessage->FF << 7) | (PeliTxMessage->RTR << 6) | (PeliTxMessage->DLC);
    if(((FunctionalState)PeliTxMessage->FF) != ENABLE)
    {
        CAN1_PELI->ID0 = (PeliTxMessage->IDHH);
        //    CAN1_PELI->ID1 = ((PeliTxMessage->IDHL)<<5);
        CAN1_PELI->ID1 = (PeliTxMessage->IDHL & 0xE0);
        if((FunctionalState)(PeliTxMessage->RTR) != ENABLE)
        {
            CAN1_PELI->DATA0 = PeliTxMessage->Data[0];
            CAN1_PELI->DATA1 = PeliTxMessage->Data[1];
            CAN1_PELI->DATA2 = PeliTxMessage->Data[2];
            CAN1_PELI->DATA3 = PeliTxMessage->Data[3];
            CAN1_PELI->DATA4 = PeliTxMessage->Data[4];
            CAN1_PELI->DATA5 = PeliTxMessage->Data[5];
            CAN1_PELI->DATA6 = PeliTxMessage->Data[6];
            CAN1_PELI->DATA7 = PeliTxMessage->Data[7];
        }
    }
    else
    {
        CAN1_PELI->ID0 = PeliTxMessage->IDHH;
        CAN1_PELI->ID1 = PeliTxMessage->IDHL;
        CAN1_PELI->DATA0 = PeliTxMessage->IDLH;
        CAN1_PELI->DATA1 = PeliTxMessage->IDLL;
        if((FunctionalState)(PeliTxMessage->RTR) != ENABLE)
        {
            CAN1_PELI->DATA2 = PeliTxMessage->Data[0];
            CAN1_PELI->DATA3 = PeliTxMessage->Data[1];
            CAN1_PELI->DATA4 = PeliTxMessage->Data[2];
            CAN1_PELI->DATA5 = PeliTxMessage->Data[3];
            CAN1_PELI->DATA6 = PeliTxMessage->Data[4];
            CAN1_PELI->DATA7 = PeliTxMessage->Data[5];
            CAN1_PELI->DATA8 = PeliTxMessage->Data[6];
            CAN1_PELI->DATA9 = PeliTxMessage->Data[7];
        }
    }
    if(CAN1_PELI->MOD & CAN_MOD_STM)
    {
        CAN1->CMR = CAN_CMR_GTS | CAN_CMR_AT;
    }
    else
    {
        CAN1->CMR = CAN_TR | CAN_AT;
    }

}



/**
* @brief  Initiates and transmits a CAN frame message.
* @param  TxMessage: pointer to a structure which contains CAN Id, CAN DLC and CAN data.
* @retval None
*/
void CAN_Peli_TransmitRepeat(CanPeliTxMsg* PeliTxMessage)
{
    /* Check the parameters */
    assert_param(IS_CAN_RTR(PeliTxMessage->RTR));
    assert_param(IS_CAN_DLC(PeliTxMessage->DLC));

    CAN1_PELI->FF = (PeliTxMessage->FF << 7) | (PeliTxMessage->RTR << 6) | (PeliTxMessage->DLC);
    if(((FunctionalState)PeliTxMessage->FF) != ENABLE)
    {
        CAN1_PELI->ID0 = (PeliTxMessage->IDHH);
        CAN1_PELI->ID1 = ((PeliTxMessage->IDHL) << 5);
        if((FunctionalState)(PeliTxMessage->RTR) != ENABLE)
        {
            CAN1_PELI->DATA0 = PeliTxMessage->Data[0];
            CAN1_PELI->DATA1 = PeliTxMessage->Data[1];
            CAN1_PELI->DATA2 = PeliTxMessage->Data[2];
            CAN1_PELI->DATA3 = PeliTxMessage->Data[3];
            CAN1_PELI->DATA4 = PeliTxMessage->Data[4];
            CAN1_PELI->DATA5 = PeliTxMessage->Data[5];
            CAN1_PELI->DATA6 = PeliTxMessage->Data[6];
            CAN1_PELI->DATA7 = PeliTxMessage->Data[7];
        }
    }
    else
    {
        CAN1_PELI->ID0 = PeliTxMessage->IDHH;
        CAN1_PELI->ID1 = PeliTxMessage->IDHL;
        CAN1_PELI->DATA0 = PeliTxMessage->IDLH;
        CAN1_PELI->DATA1 = PeliTxMessage->IDLL;
        if((FunctionalState)(PeliTxMessage->RTR) != ENABLE)
        {
            CAN1_PELI->DATA2 = PeliTxMessage->Data[0];
            CAN1_PELI->DATA3 = PeliTxMessage->Data[1];
            CAN1_PELI->DATA4 = PeliTxMessage->Data[2];
            CAN1_PELI->DATA5 = PeliTxMessage->Data[3];
            CAN1_PELI->DATA6 = PeliTxMessage->Data[4];
            CAN1_PELI->DATA7 = PeliTxMessage->Data[5];
            CAN1_PELI->DATA8 = PeliTxMessage->Data[6];
            CAN1_PELI->DATA9 = PeliTxMessage->Data[7];
        }
    }

    if(CAN1_PELI->MOD & CAN_MOD_STM)
    {
        CAN1->CMR = CAN_CMR_GTS | CAN_CMR_AT;
    }
    else
    {
        CAN1->CMR = CAN_CMR_TR;
    }
}

/** @defgroup CAN_Group3 CAN Frames Reception functions
*  @brief    CAN Frames Reception functions
*
@verbatim
===============================================================================
##### CAN Frames Reception functions #####
===============================================================================
[..] This section provides functions allowing to
(+) Receive a correct CAN frame.
(+) Release a specified receive FIFO
(+) Return the number of the pending received CAN frames.

@endverbatim
* @{
*/


/**
* @brief  Receives a correct CAN frame.
* @param  RxMessage: pointer to a structure receive frame which contains CAN Id,
*         CAN DLC, CAN data and FMI number.
* @retval None
*/
void CAN_Peli_Receive(CanPeliRxMsg* PeliRxMessage)
{
    uint32_t tempid;
    PeliRxMessage->FF = (CAN1_PELI->FF) >> 7;
    PeliRxMessage->RTR = ((CAN1_PELI->FF) >> 6) & 0x1;
    PeliRxMessage->DLC = (CAN1_PELI->FF) & 0xf;

    if(((FunctionalState)PeliRxMessage->FF) != ENABLE)
    {
        tempid = (uint32_t)(CAN1_PELI->ID1 >> 5);
        tempid |= (uint32_t)(CAN1_PELI->ID0 << 3);
        PeliRxMessage->ID = tempid;
        PeliRxMessage->Data[0] = CAN1_PELI->DATA0;
        PeliRxMessage->Data[1] = CAN1_PELI->DATA1;
        PeliRxMessage->Data[2] = CAN1_PELI->DATA2;
        PeliRxMessage->Data[3] = CAN1_PELI->DATA3;
        PeliRxMessage->Data[4] = CAN1_PELI->DATA4;
        PeliRxMessage->Data[5] = CAN1_PELI->DATA5;
        PeliRxMessage->Data[6] = CAN1_PELI->DATA6;
        PeliRxMessage->Data[7] = CAN1_PELI->DATA7;
    }
    else
    {
        tempid = (uint32_t)((CAN1_PELI->DATA1 & 0xf8) >> 3);
        tempid |= (uint32_t)(CAN1_PELI->DATA0 << 5);
        tempid |= (uint32_t)(CAN1_PELI->ID1 << 13);
        tempid |= (uint32_t)(CAN1_PELI->ID0 << 21);
        PeliRxMessage->ID = tempid;
        PeliRxMessage->Data[0] = CAN1_PELI->DATA2;
        PeliRxMessage->Data[1] = CAN1_PELI->DATA3;
        PeliRxMessage->Data[2] = CAN1_PELI->DATA4;
        PeliRxMessage->Data[3] = CAN1_PELI->DATA5;
        PeliRxMessage->Data[4] = CAN1_PELI->DATA6;
        PeliRxMessage->Data[5] = CAN1_PELI->DATA7;
        PeliRxMessage->Data[6] = CAN1_PELI->DATA8;
        PeliRxMessage->Data[7] = CAN1_PELI->DATA9;
    }
    CAN_FIFORelease( CAN1);
}


/**
* @brief  Get available current informatoin in receive FIFO only in Peli workmode.
* @retval The value in reg RMC
*/
uint32_t CAN_Peli_GetRxFIFOInfo(void)
{
    return CAN1_PELI->RMC;
}


/** @defgroup CAN_Group5 CAN Bus Error management functions
*  @brief    CAN Bus Error management functions
*
@verbatim
===============================================================================
##### CAN Bus Error management functions #####
===============================================================================

@endverbatim
* @{
*/

/**
* @brief  Returns the CAN's last error code (LEC).
* @retval Error code:
*          - CAN_ERRORCODE_NoErr: No Error
*          - CAN_ERRORCODE_StuffErr: Stuff Error
*          - CAN_ERRORCODE_FormErr: Form Error
*          - CAN_ERRORCODE_ACKErr : Acknowledgment Error
*          - CAN_ERRORCODE_BitRecessiveErr: Bit Recessive Error
*          - CAN_ERRORCODE_BitDominantErr: Bit Dominant Error
*          - CAN_ERRORCODE_CRCErr: CRC Error
*          - CAN_ERRORCODE_SoftwareSetErr: Software Set Error
*/
uint8_t CAN_Peli_GetLastErrorCode(void)
{
    uint8_t errorcode = 0;

    /* Get the error code*/
    errorcode = ((uint8_t)CAN1_PELI->ECC);

    /* Return the error code*/
    return errorcode;
}
/**
* @brief  Returns the CAN Receive Error Counter (REC).
* @note   In case of an error during reception, this counter is incremented
*         by 1 or by 8 depending on the error condition as defined by the CAN
*         standard. After every successful reception, the counter is
*         decremented by 1 or reset to 120 if its value was higher than 128.
*         When the counter value exceeds 127, the CAN controller enters the
*         error passive state.
* @retval CAN Receive Error Counter.
*/
uint8_t CAN_Peli_GetReceiveErrorCounter(void)
{
    uint8_t counter = 0;

    /* Check the parameters */
    assert_param(IS_CAN_ALL_PERIPH(CANx));

    /* Get the Receive Error Counter*/
    counter = (uint8_t)(CAN1_PELI->RXERR);

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


/**
* @brief  Returns the LSB of the 9-bit CANx Transmit Error Counter(TEC).
* @retval LSB of the 8-bit CAN Transmit Error Counter.
*/
uint8_t CAN_Peli_GetLSBTransmitErrorCounter(void)
{
    uint8_t counter = 0;

    /* Check the parameters */
    assert_param(IS_CAN_ALL_PERIPH(CANx));

    /* Get the LSB of the 8-bit CAN Transmit Error Counter(TEC) */
    counter = (uint8_t)(CAN1_PELI->TXERR);

    /* Return the LSB of the 8-bit CAN Transmit Error Counter(TEC) */
    return counter;
}
/** @defgroup CAN_Group6 Interrupts and flags management functions
*  @brief   Interrupts and flags management functions
*
@verbatim
===============================================================================
##### Interrupts and flags management functions #####
===============================================================================
[..] This section provides functions allowing to configure the CAN Interrupts
and to get the status and clear flags and Interrupts pending bits.
[..] The CAN provides 14 Interrupts sources and 15 Flags:

*** Flags ***
=============
*/
/**
* @brief  Enables or disables the specified CAN interrupts in peli workmode.
* @param  CAN_IT: specifies the CAN interrupt sources to be enabled or disabled.
*          This parameter can be:
*            @arg CAN_IT_RI: Receive FIFO not empty Interrupt
*            @arg CAN_IT_TI: Transmit Interrupt
*            @arg CAN_IT_EI: ERROR Interrupt
*            @arg CAN_IT_DOI: Data voerflow Interrupt
*            @arg CAN_IT_WUI: Wakeup Interrupt
*            @arg CAN_IT_EPI(only Peli): passive error Interrupt
*            @arg CAN_IT_ALI(only Peli): arbiter lose Interrupt
*            @arg CAN_IT_BEI(only Peli): bus error Interrupt
@arg CAN_IT_ALL: use it can enble all Interrupt
* @param  NewState: new state of the CAN interrupts.
*          This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void CAN_Peli_ITConfig(uint32_t CAN_IT, FunctionalState NewState)
{
    /* Check the parameters */
    assert_param(IS_CAN_IT(CAN_IT));
    assert_param(IS_FUNCTIONAL_STATE(NewState));

    if (NewState != DISABLE)
    {
        /* Enable the selected CAN interrupt */
        CAN1_PELI->IER |= CAN_IT;
    }
    else
    {
        /* Disable the selected CAN interrupt */
        CAN1_PELI->IER &= ~CAN_IT;
    }
}


/**
* @brief  Checks whether the specified CAN interrupt has occurred or not.
* @param  CAN_IT: specifies the CAN interrupt source to check.
*          This parameter can be one of the following values:
*            @arg CAN_IT_RI: Receive FIFO not empty Interrupt
*            @arg CAN_IT_TI: Transmit Interrupt
*            @arg CAN_IT_EI: ERROR Interrupt
*            @arg CAN_IT_DOI: Data voerflow Interrupt
*            @arg CAN_IT_WUI: Wakeup Interrupt
*            @arg CAN_IT_EPI(only Peli): passive error Interrupt
*            @arg CAN_IT_ALI(only Peli): arbiter lose Interrupt
*            @arg CAN_IT_BEI(only Peli): bus error Interrupt
@arg CAN_IT_ALL: use it can enble all Interrupt
* @retval The current state of CAN_IT (SET or RESET).
*/
ITStatus CAN_Peli_GetITStatus(uint32_t CAN_IT)
{
    ITStatus itstatus = RESET;
    /* Check the parameters */
    assert_param(IS_CAN_IT(CAN_IT));

    /* check the interrupt enable bit */
    if((CAN1_PELI->IR & CAN_IT) != CAN_IT)
    {
        itstatus = RESET;
    }
    else
    {
        itstatus = SET;
    }

    return itstatus;
}

/**
* @brief  Config CAN_Peli_InitTypeDef baud parameter.
* @param  CAN_Peli_InitTypeDef: CAN struct.
* @param  SrcClk: CAN module clock.
* @param  baud: specified baud.
* @retval The current state of CAN_IT (SET or RESET).
*/
void CAN_AutoCfg_BaudParam(CAN_Peli_InitTypeDef  *CAN_Peli_InitStruct, unsigned int SrcClk, unsigned int baud )
{
    unsigned int i, value = baud, record = 1;
    unsigned int remain = 0, sumPrescaler = 0;
    while(( baud == 0 ) || ( SrcClk == 0 )); //��ֹ�����ʼ�ʱ��Ϊ0
    sumPrescaler = SrcClk / baud; //�ܷ�Ƶ
    sumPrescaler = sumPrescaler / 2; //
    for( i = 25; i > 3; i -- )
    {
        remain = sumPrescaler - ((sumPrescaler / i) * i);
        if( remain == 0 )		//����
        {
            record = i;
            break;
        }
        else
        {
            if(remain < value)
            {
                value = remain;
                record = i;
            }
        }
    }
    CAN_Peli_InitStruct->SJW = 0;
    CAN_Peli_InitStruct->BRP = (sumPrescaler / record) - 1;
    CAN_Peli_InitStruct->TESG2 = (record - 3) / 3;
    CAN_Peli_InitStruct->TESG1 = (record - 3) - CAN_Peli_InitStruct->TESG2;
}

/**
* @}
*/

/**
* @}
*/

/**
* @}
*/



/*-------------------------(C) COPYRIGHT 2019 MindMotion ----------------------*/