forked from ARMmbed/mbed-os
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcan_api.c
1438 lines (1273 loc) · 44.6 KB
/
can_api.c
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
/* mbed Microcontroller Library
* Copyright (c) 2006-2017 ARM Limited
* 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
*
* http://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.
*/
#include "can_api.h"
#if DEVICE_CAN
#ifdef FDCAN1
#include "pinmap.h"
#include "PeripheralPins.h"
#include "mbed_error.h"
static uintptr_t can_irq_contexts[2] = {0};
static can_irq_handler irq_handler;
/** Call all the init functions
*
* @returns
* 0 if mode change failed or unsupported,
* 1 if mode change was successful
*/
int can_internal_init(can_t *obj)
{
if (HAL_FDCAN_Init(&obj->CanHandle) != HAL_OK) {
error("HAL_FDCAN_Init error\n");
}
if (can_filter(obj, 0, 0, CANStandard, 0) == 0) {
error("can_filter error\n");
}
if (can_filter(obj, 0, 0, CANExtended, 0) == 0) {
error("can_filter error\n");
}
if (HAL_FDCAN_ConfigGlobalFilter(&obj->CanHandle, FDCAN_REJECT, FDCAN_REJECT, FDCAN_FILTER_REMOTE, FDCAN_FILTER_REMOTE) != HAL_OK) {
error("HAL_FDCAN_ConfigGlobalFilter error\n");
}
if (HAL_FDCAN_Start(&obj->CanHandle) != HAL_OK) {
error("HAL_FDCAN_Start error\n");
}
return 1;
}
#if STATIC_PINMAP_READY
#define CAN_INIT_FREQ_DIRECT can_init_freq_direct
void can_init_freq_direct(can_t *obj, const can_pinmap_t *pinmap, int hz)
#else
#define CAN_INIT_FREQ_DIRECT _can_init_freq_direct
static void _can_init_freq_direct(can_t *obj, const can_pinmap_t *pinmap, int hz)
#endif
{
MBED_ASSERT((int)pinmap->peripheral != NC);
#if defined(__HAL_RCC_FDCAN1_CLK_ENABLE)
__HAL_RCC_FDCAN1_CLK_ENABLE();
#else
__HAL_RCC_FDCAN_CLK_ENABLE();
#endif
if (pinmap->peripheral == CAN_1) {
obj->index = 0;
}
#if defined(FDCAN2_BASE)
else if (pinmap->peripheral == CAN_2) {
obj->index = 1;
}
#endif
#if defined(FDCAN3_BASE)
else if (pinmap->peripheral == CAN_3) {
obj->index = 2;
}
#endif
else {
error("can_init wrong instance\n");
return;
}
// Select PLL1Q as source of FDCAN clock
RCC_PeriphCLKInitTypeDef RCC_PeriphClkInit;
#if (defined RCC_PERIPHCLK_FDCAN1)
RCC_PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_FDCAN1;
RCC_PeriphClkInit.Fdcan1ClockSelection = RCC_FDCAN1CLKSOURCE_PLL1;
#else
RCC_PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_FDCAN;
RCC_PeriphClkInit.FdcanClockSelection = RCC_FDCANCLKSOURCE_PLL;
#endif
#if defined(DUAL_CORE) && (TARGET_STM32H7)
while (LL_HSEM_1StepLock(HSEM, CFG_HW_RCC_SEMID)) {
}
#endif /* DUAL_CORE */
if (HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphClkInit) != HAL_OK) {
error("HAL_RCCEx_PeriphCLKConfig error\n");
}
#if defined(DUAL_CORE) && (TARGET_STM32H7)
LL_HSEM_ReleaseLock(HSEM, CFG_HW_RCC_SEMID, HSEM_CR_COREID_CURRENT);
#endif /* DUAL_CORE */
// Configure CAN pins
pin_function(pinmap->rd_pin, pinmap->rd_function);
pin_function(pinmap->td_pin, pinmap->td_function);
// Add pull-ups
pin_mode(pinmap->rd_pin, PullUp);
pin_mode(pinmap->td_pin, PullUp);
// Default values
obj->CanHandle.Instance = (FDCAN_GlobalTypeDef *)pinmap->peripheral;
/* Bit time parameter
ex with 100 kHz requested frequency hz
fdcan_ker_ck | 10 MHz | 10 MHz
Prescaler | 1 | 1
Time_quantum (tq) | 100 ns | 100 ns
Bit_rate | 0.1 MBit/s | <hz>
Bit_length | 10 µs = 100 tq | <n_tq> = 10 000 000 / <hz>
Synchronization_segment | 1 tq | 1 tq
Phase_segment_1 | 69 tq | <nts1> = <n_tq> * 0.75
Phase_segment_2 | 30 tq | <nts2> = <n_tq> - 1 - <nts1>
Synchronization_Jump_width | 30 tq | <nsjw> = <nts2>
*/
// !Attention Not all bitrates can be covered with all fdcan-core-clk values. When a clk
// does not work for the desired bitrate, change system_clock settings for FDCAN_CLK
// (default FDCAN_CLK is PLLQ)
#if (defined TARGET_STM32H7)
// STM32H7 doesn't support yet HAL_RCCEx_GetPeriphCLKFreq for FDCAN
// We use PLL1.Q clock right now so get its frequency
PLL1_ClocksTypeDef pll1_clocks;
HAL_RCCEx_GetPLL1ClockFreq(&pll1_clocks);
uint32_t ntq = pll1_clocks.PLL1_Q_Frequency / (uint32_t)hz;
#else
#if (defined RCC_PERIPHCLK_FDCAN1)
uint32_t ntq = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_FDCAN1) / (uint32_t)hz;
#else
uint32_t ntq = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_FDCAN) / (uint32_t)hz;
#endif
#endif
uint32_t nominalPrescaler = 1;
// !When the sample point should be lower than 50%, this must be changed to
// !IS_FDCAN_NOMINAL_TSEG2(ntq/nominalPrescaler), since
// NTSEG2 and SJW max values are lower. For now the sample point is fix @75%
while (!IS_FDCAN_NOMINAL_TSEG1(ntq / nominalPrescaler)) {
nominalPrescaler ++;
if (!IS_FDCAN_NOMINAL_PRESCALER(nominalPrescaler)) {
error("Could not determine good nominalPrescaler. Bad clock value\n");
}
}
ntq = ntq / nominalPrescaler;
obj->CanHandle.Init.FrameFormat = FDCAN_FRAME_CLASSIC;
obj->CanHandle.Init.Mode = FDCAN_MODE_NORMAL;
obj->CanHandle.Init.AutoRetransmission = ENABLE;
obj->CanHandle.Init.TransmitPause = DISABLE;
obj->CanHandle.Init.ProtocolException = ENABLE;
obj->CanHandle.Init.NominalPrescaler = nominalPrescaler; // Prescaler
obj->CanHandle.Init.NominalTimeSeg1 = ntq * 0.75; // Phase_segment_1
obj->CanHandle.Init.NominalTimeSeg2 = ntq - 1 - obj->CanHandle.Init.NominalTimeSeg1; // Phase_segment_2
obj->CanHandle.Init.NominalSyncJumpWidth = obj->CanHandle.Init.NominalTimeSeg2; // Synchronization_Jump_width
obj->CanHandle.Init.DataPrescaler = 0x1; // Not used - only in FDCAN
obj->CanHandle.Init.DataSyncJumpWidth = 0x1; // Not used - only in FDCAN
obj->CanHandle.Init.DataTimeSeg1 = 0x1; // Not used - only in FDCAN
obj->CanHandle.Init.DataTimeSeg2 = 0x1; // Not used - only in FDCAN
#ifdef TARGET_STM32H7
/* Message RAM offset is only supported in STM32H7 platforms of supported FDCAN platforms */
obj->CanHandle.Init.MessageRAMOffset = 0;
/* The number of Standard and Extended ID filters are initialized to the maximum possile extent
* for STM32H7 platforms
*/
obj->CanHandle.Init.StdFiltersNbr = 128; // to be aligned with the handle parameter in can_filter
obj->CanHandle.Init.ExtFiltersNbr = 64; // to be aligned with the handle parameter in can_filter
#else
/* The number of Standard and Extended ID filters are initialized to the maximum possile extent
* for STM32G0x1, STM32G4 and STM32L5 platforms
*/
obj->CanHandle.Init.StdFiltersNbr = 28; // to be aligned with the handle parameter in can_filter
obj->CanHandle.Init.ExtFiltersNbr = 8; // to be aligned with the handle parameter in can_filter
#endif
#ifdef TARGET_STM32H7
obj->CanHandle.Init.RxFifo0ElmtsNbr = 8;
obj->CanHandle.Init.RxFifo0ElmtSize = FDCAN_DATA_BYTES_8;
obj->CanHandle.Init.RxFifo1ElmtsNbr = 0;
obj->CanHandle.Init.RxFifo1ElmtSize = FDCAN_DATA_BYTES_8;
obj->CanHandle.Init.RxBuffersNbr = 0;
obj->CanHandle.Init.RxBufferSize = FDCAN_DATA_BYTES_8;
obj->CanHandle.Init.TxEventsNbr = 3;
obj->CanHandle.Init.TxBuffersNbr = 0;
obj->CanHandle.Init.TxFifoQueueElmtsNbr = 3;
#endif
obj->CanHandle.Init.TxFifoQueueMode = FDCAN_TX_FIFO_OPERATION;
#ifdef TARGET_STM32H7
obj->CanHandle.Init.TxElmtSize = FDCAN_DATA_BYTES_8;
#endif
can_internal_init(obj);
}
void can_init_direct(can_t *obj, const can_pinmap_t *pinmap)
{
/* default frequency is 100 kHz */
CAN_INIT_FREQ_DIRECT(obj, pinmap, 100000);
}
void can_init_freq(can_t *obj, PinName rd, PinName td, int hz)
{
CANName can_rd = (CANName)pinmap_peripheral(rd, PinMap_CAN_RD);
CANName can_td = (CANName)pinmap_peripheral(td, PinMap_CAN_TD);
int peripheral = (int) pinmap_merge(can_rd, can_td);
int function_rd = (int)pinmap_find_function(rd, PinMap_CAN_RD);
int function_td = (int)pinmap_find_function(td, PinMap_CAN_TD);
const can_pinmap_t static_pinmap = {peripheral, rd, function_rd, td, function_td};
CAN_INIT_FREQ_DIRECT(obj, &static_pinmap, hz);
}
void can_init(can_t *obj, PinName rd, PinName td)
{
can_init_freq(obj, rd, td, 100000);
}
void can_irq_init(can_t *obj, can_irq_handler handler, uintptr_t context)
{
irq_handler = handler;
can_irq_contexts[obj->index] = context;
}
void can_irq_free(can_t *obj)
{
CANName can = (CANName)obj->CanHandle.Instance;
if (can == CAN_1) {
HAL_NVIC_DisableIRQ(FDCAN1_IT0_IRQn);
HAL_NVIC_DisableIRQ(FDCAN1_IT1_IRQn);
}
#if defined(FDCAN2_BASE)
else if (can == CAN_2) {
HAL_NVIC_DisableIRQ(FDCAN2_IT0_IRQn);
HAL_NVIC_DisableIRQ(FDCAN2_IT1_IRQn);
}
#endif
#if defined(FDCAN3_BASE)
else if (can == CAN_3) {
HAL_NVIC_DisableIRQ(FDCAN3_IT0_IRQn);
HAL_NVIC_DisableIRQ(FDCAN3_IT1_IRQn);
}
#endif
else {
return;
}
#if (defined TARGET_STM32H7)
HAL_NVIC_DisableIRQ(FDCAN_CAL_IRQn);
#endif
can_irq_contexts[obj->index] = 0;
}
void can_free(can_t *obj)
{
#if defined(DUAL_CORE) && (TARGET_STM32H7)
while (LL_HSEM_1StepLock(HSEM, CFG_HW_RCC_SEMID)) {
}
#endif /* DUAL_CORE */
#if defined(__HAL_RCC_FDCAN1_FORCE_RESET)
__HAL_RCC_FDCAN1_FORCE_RESET();
__HAL_RCC_FDCAN1_RELEASE_RESET();
#else
__HAL_RCC_FDCAN_FORCE_RESET();
__HAL_RCC_FDCAN_RELEASE_RESET();
#endif
#if defined(DUAL_CORE) && (TARGET_STM32H7)
LL_HSEM_ReleaseLock(HSEM, CFG_HW_RCC_SEMID, HSEM_CR_COREID_CURRENT);
#endif /* DUAL_CORE */
#if defined(__HAL_RCC_FDCAN1_CLK_DISABLE)
__HAL_RCC_FDCAN1_CLK_DISABLE();
#else
__HAL_RCC_FDCAN_CLK_DISABLE();
#endif
}
/** Reset CAN interface.
*
* To use after error overflow.
*/
void can_reset(can_t *obj)
{
can_mode(obj, MODE_RESET);
HAL_FDCAN_ResetTimeoutCounter(&obj->CanHandle);
HAL_FDCAN_ResetTimestampCounter(&obj->CanHandle);
}
int can_frequency(can_t *obj, int f)
{
if (HAL_FDCAN_Stop(&obj->CanHandle) != HAL_OK) {
error("HAL_FDCAN_Stop error\n");
}
/* See can_init_freq function for calculation details
*
* !Attention Not all bitrates can be covered with all fdcan-core-clk values. When a clk
* does not work for the desired bitrate, change system_clock settings for FDCAN_CLK
* (default FDCAN_CLK is PLLQ)
*/
#if (defined TARGET_STM32H7)
// STM32H7 doesn't support yet HAL_RCCEx_GetPeriphCLKFreq for FDCAN
PLL1_ClocksTypeDef pll1_clocks;
HAL_RCCEx_GetPLL1ClockFreq(&pll1_clocks);
uint32_t ntq = pll1_clocks.PLL1_Q_Frequency / (uint32_t)f;
#else
#if (defined RCC_PERIPHCLK_FDCAN1)
uint32_t ntq = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_FDCAN1) / (uint32_t)f;
#else
uint32_t ntq = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_FDCAN) / (uint32_t)f;
#endif
#endif
uint32_t nominalPrescaler = 1;
// !When the sample point should be lower than 50%, this must be changed to
// !IS_FDCAN_NOMINAL_TSEG2(ntq/nominalPrescaler), since
// NTSEG2 and SJW max values are lower. For now the sample point is fix @75%
while (!IS_FDCAN_NOMINAL_TSEG1(ntq / nominalPrescaler)) {
nominalPrescaler ++;
if (!IS_FDCAN_NOMINAL_PRESCALER(nominalPrescaler)) {
error("Could not determine good nominalPrescaler. Bad clock value\n");
}
}
ntq = ntq / nominalPrescaler;
obj->CanHandle.Init.NominalPrescaler = nominalPrescaler;
obj->CanHandle.Init.NominalTimeSeg1 = ntq * 0.75; // Phase_segment_1
obj->CanHandle.Init.NominalTimeSeg2 = ntq - 1 - obj->CanHandle.Init.NominalTimeSeg1; // Phase_segment_2
obj->CanHandle.Init.NominalSyncJumpWidth = obj->CanHandle.Init.NominalTimeSeg2; // Synchronization_Jump_width
return can_internal_init(obj);
}
/** Filter out incoming messages
*
* @param obj CAN object
* @param id the id to filter on
* @param mask the mask applied to the id
* @param format format to filter on
* @param handle message filter handle (not supported yet)
*
* @returns
* 0 if filter change failed or unsupported,
* new filter handle if successful (not supported yet => returns 1)
*/
int can_filter(can_t *obj, uint32_t id, uint32_t mask, CANFormat format, int32_t handle)
{
FDCAN_FilterTypeDef sFilterConfig = {0};
if (format == CANStandard) {
sFilterConfig.IdType = FDCAN_STANDARD_ID;
sFilterConfig.FilterIndex = handle;
sFilterConfig.FilterType = FDCAN_FILTER_MASK;
sFilterConfig.FilterConfig = FDCAN_FILTER_TO_RXFIFO0;
sFilterConfig.FilterID1 = id;
sFilterConfig.FilterID2 = mask;
} else if (format == CANExtended) {
sFilterConfig.IdType = FDCAN_EXTENDED_ID;
sFilterConfig.FilterIndex = handle;
sFilterConfig.FilterType = FDCAN_FILTER_MASK;
sFilterConfig.FilterConfig = FDCAN_FILTER_TO_RXFIFO0;
sFilterConfig.FilterID1 = id;
sFilterConfig.FilterID2 = mask;
} else { // Filter for CANAny format cannot be configured for STM32
return 0;
}
if (HAL_FDCAN_ConfigFilter(&obj->CanHandle, &sFilterConfig) != HAL_OK) {
return 0;
}
return 1;
}
int can_write(can_t *obj, CAN_Message msg, int cc)
{
FDCAN_TxHeaderTypeDef TxHeader = {0};
UNUSED(cc);
// Configure Tx buffer message
TxHeader.Identifier = msg.id;
if (msg.format == CANStandard) {
TxHeader.IdType = FDCAN_STANDARD_ID;
} else {
TxHeader.IdType = FDCAN_EXTENDED_ID;
}
TxHeader.TxFrameType = FDCAN_DATA_FRAME;
TxHeader.DataLength = msg.len << 16;
TxHeader.ErrorStateIndicator = FDCAN_ESI_ACTIVE;
TxHeader.BitRateSwitch = FDCAN_BRS_OFF;
TxHeader.FDFormat = FDCAN_CLASSIC_CAN;
TxHeader.TxEventFifoControl = FDCAN_STORE_TX_EVENTS;
TxHeader.MessageMarker = 0;
if (HAL_FDCAN_AddMessageToTxFifoQ(&obj->CanHandle, &TxHeader, msg.data) != HAL_OK) {
// Note for debug: you can get the error code calling HAL_FDCAN_GetError(&obj->CanHandle)
return 0;
}
return 1;
}
int can_read(can_t *obj, CAN_Message *msg, int handle)
{
UNUSED(handle); // Not supported, RXFIFO0 is set default by can_filter and cannot be changed.
if (HAL_FDCAN_GetRxFifoFillLevel(&obj->CanHandle, FDCAN_RX_FIFO0) == 0) {
return 0; // No message arrived
}
FDCAN_RxHeaderTypeDef RxHeader = {0};
if (HAL_FDCAN_GetRxMessage(&obj->CanHandle, FDCAN_RX_FIFO0, &RxHeader, msg->data) != HAL_OK) {
error("HAL_FDCAN_GetRxMessage error\n"); // Should not occur as previous HAL_FDCAN_GetRxFifoFillLevel call reported some data
return 0;
}
if (RxHeader.IdType == FDCAN_STANDARD_ID) {
msg->format = CANStandard;
} else {
msg->format = CANExtended;
}
msg->id = RxHeader.Identifier;
msg->type = (RxHeader.RxFrameType == FDCAN_DATA_FRAME) ? CANData : CANRemote;
msg->len = RxHeader.DataLength >> 16; // see FDCAN_data_length_code value
return 1;
}
unsigned char can_rderror(can_t *obj)
{
FDCAN_ErrorCountersTypeDef ErrorCounters;
HAL_FDCAN_GetErrorCounters(&obj->CanHandle, &ErrorCounters);
return (unsigned char)ErrorCounters.RxErrorCnt;
}
unsigned char can_tderror(can_t *obj)
{
FDCAN_ErrorCountersTypeDef ErrorCounters;
HAL_FDCAN_GetErrorCounters(&obj->CanHandle, &ErrorCounters);
return (unsigned char)ErrorCounters.TxErrorCnt;
}
void can_monitor(can_t *obj, int silent)
{
CanMode mode = MODE_NORMAL;
if (silent) {
switch (obj->CanHandle.Init.Mode) {
case FDCAN_MODE_INTERNAL_LOOPBACK:
mode = MODE_TEST_SILENT;
break;
default:
mode = MODE_SILENT;
break;
}
} else {
switch (obj->CanHandle.Init.Mode) {
case FDCAN_MODE_INTERNAL_LOOPBACK:
case FDCAN_MODE_EXTERNAL_LOOPBACK:
mode = MODE_TEST_LOCAL;
break;
default:
mode = MODE_NORMAL;
break;
}
}
can_mode(obj, mode);
}
/** Change CAN operation to the specified mode
*
* @param mode The new operation mode (MODE_RESET, MODE_NORMAL, MODE_SILENT, MODE_TEST_LOCAL, MODE_TEST_GLOBAL, MODE_TEST_SILENT)
*
* @returns
* 0 if mode change failed or unsupported,
* 1 if mode change was successful
*/
int can_mode(can_t *obj, CanMode mode)
{
if (HAL_FDCAN_Stop(&obj->CanHandle) != HAL_OK) {
error("HAL_FDCAN_Stop error\n");
}
switch (mode) {
case MODE_RESET:
break;
case MODE_NORMAL:
obj->CanHandle.Init.Mode = FDCAN_MODE_NORMAL;
// obj->CanHandle.Init.NominalPrescaler = 100; // Prescaler
break;
case MODE_SILENT: // Bus Monitoring
obj->CanHandle.Init.Mode = FDCAN_MODE_BUS_MONITORING;
break;
case MODE_TEST_GLOBAL: // External LoopBack
case MODE_TEST_LOCAL:
obj->CanHandle.Init.Mode = FDCAN_MODE_EXTERNAL_LOOPBACK;
break;
case MODE_TEST_SILENT: // Internal LoopBack
obj->CanHandle.Init.Mode = FDCAN_MODE_INTERNAL_LOOPBACK;
// obj->CanHandle.Init.NominalPrescaler = 1; // Prescaler
break;
default:
return 0;
}
return can_internal_init(obj);
}
static void can_irq(CANName name, int id)
{
FDCAN_HandleTypeDef CanHandle;
CanHandle.Instance = (FDCAN_GlobalTypeDef *)name;
if (__HAL_FDCAN_GET_IT_SOURCE(&CanHandle, FDCAN_IT_TX_COMPLETE)) {
if (__HAL_FDCAN_GET_FLAG(&CanHandle, FDCAN_FLAG_TX_COMPLETE)) {
__HAL_FDCAN_CLEAR_FLAG(&CanHandle, FDCAN_FLAG_TX_COMPLETE);
irq_handler(can_irq_contexts[id], IRQ_TX);
}
}
#if (defined FDCAN_IT_RX_BUFFER_NEW_MESSAGE)
if (__HAL_FDCAN_GET_IT_SOURCE(&CanHandle, FDCAN_IT_RX_BUFFER_NEW_MESSAGE)) {
if (__HAL_FDCAN_GET_FLAG(&CanHandle, FDCAN_IT_RX_BUFFER_NEW_MESSAGE)) {
__HAL_FDCAN_CLEAR_FLAG(&CanHandle, FDCAN_IT_RX_BUFFER_NEW_MESSAGE);
irq_handler(can_irq_contexts[id], IRQ_RX);
}
}
#else
if (__HAL_FDCAN_GET_IT_SOURCE(&CanHandle, FDCAN_IT_RX_FIFO0_NEW_MESSAGE)) {
if (__HAL_FDCAN_GET_FLAG(&CanHandle, FDCAN_IT_RX_FIFO0_NEW_MESSAGE)) {
__HAL_FDCAN_CLEAR_FLAG(&CanHandle, FDCAN_IT_RX_FIFO0_NEW_MESSAGE);
irq_handler(can_irq_contexts[id], IRQ_RX);
}
}
#endif
if (__HAL_FDCAN_GET_IT_SOURCE(&CanHandle, FDCAN_IT_ERROR_WARNING)) {
if (__HAL_FDCAN_GET_FLAG(&CanHandle, FDCAN_FLAG_ERROR_WARNING)) {
__HAL_FDCAN_CLEAR_FLAG(&CanHandle, FDCAN_FLAG_ERROR_WARNING);
irq_handler(can_irq_contexts[id], IRQ_ERROR);
}
}
if (__HAL_FDCAN_GET_IT_SOURCE(&CanHandle, FDCAN_IT_ERROR_PASSIVE)) {
if (__HAL_FDCAN_GET_FLAG(&CanHandle, FDCAN_FLAG_ERROR_PASSIVE)) {
__HAL_FDCAN_CLEAR_FLAG(&CanHandle, FDCAN_FLAG_ERROR_PASSIVE);
irq_handler(can_irq_contexts[id], IRQ_PASSIVE);
}
}
if (__HAL_FDCAN_GET_IT_SOURCE(&CanHandle, FDCAN_IT_BUS_OFF)) {
if (__HAL_FDCAN_GET_FLAG(&CanHandle, FDCAN_FLAG_BUS_OFF)) {
__HAL_FDCAN_CLEAR_FLAG(&CanHandle, FDCAN_FLAG_BUS_OFF);
irq_handler(can_irq_contexts[id], IRQ_BUS);
}
}
}
void FDCAN1_IT0_IRQHandler(void)
{
can_irq(CAN_1, 0);
}
void FDCAN1_IT1_IRQHandler(void)
{
can_irq(CAN_1, 0);
}
#if defined(FDCAN2_BASE)
void FDCAN2_IT0_IRQHandler(void)
{
can_irq(CAN_2, 1);
}
void FDCAN2_IT1_IRQHandler(void)
{
can_irq(CAN_2, 1);
}
#endif //FDCAN2_BASE
#if defined(FDCAN3_BASE)
void FDCAN3_IT0_IRQHandler(void)
{
can_irq(CAN_3, 2);
}
void FDCAN3_IT1_IRQHandler(void)
{
can_irq(CAN_3, 2);
}
#endif //FDCAN3_BASE
// TODO Add other interrupts ?
void can_irq_set(can_t *obj, CanIrqType type, uint32_t enable)
{
uint32_t interrupts = 0;
switch (type) {
case IRQ_TX:
interrupts = FDCAN_IT_TX_COMPLETE;
break;
case IRQ_RX:
#if (defined FDCAN_IT_RX_BUFFER_NEW_MESSAGE)
interrupts = FDCAN_IT_RX_BUFFER_NEW_MESSAGE;
#else
interrupts = FDCAN_IT_RX_FIFO0_NEW_MESSAGE;
#endif
break;
case IRQ_ERROR:
interrupts = FDCAN_IT_ERROR_WARNING;
break;
case IRQ_PASSIVE:
interrupts = FDCAN_IT_ERROR_PASSIVE;
break;
case IRQ_BUS:
interrupts = FDCAN_IT_BUS_OFF;
default:
return;
}
if (enable) {
/* The TXBTIE register controls the TX complete interrupt in FDCAN
* and is only used in case of TX interrupts, Hence in case of enabling the
* TX interrupts the bufferIndexes of TXBTIE are to be set */
#ifdef TARGET_STM32H7
// TXBTIE for STM32H7 is 2 bytes long
HAL_FDCAN_ActivateNotification(&obj->CanHandle, interrupts, 0xFFFF);
#else
//TXBTIE for rest supported FDCAN Platforms(STM32G0x1, STM32G4 and STM32L5) is 3 bits.
HAL_FDCAN_ActivateNotification(&obj->CanHandle, interrupts, 0x07);
#endif
} else {
HAL_FDCAN_DeactivateNotification(&obj->CanHandle, interrupts);
}
NVIC_SetVector(FDCAN1_IT0_IRQn, (uint32_t)&FDCAN1_IT0_IRQHandler);
NVIC_EnableIRQ(FDCAN1_IT0_IRQn);
NVIC_SetVector(FDCAN1_IT1_IRQn, (uint32_t)&FDCAN1_IT1_IRQHandler);
NVIC_EnableIRQ(FDCAN1_IT1_IRQn);
#if defined(FDCAN2_BASE)
NVIC_SetVector(FDCAN2_IT0_IRQn, (uint32_t)&FDCAN2_IT0_IRQHandler);
NVIC_EnableIRQ(FDCAN2_IT0_IRQn);
NVIC_SetVector(FDCAN2_IT1_IRQn, (uint32_t)&FDCAN2_IT1_IRQHandler);
NVIC_EnableIRQ(FDCAN2_IT1_IRQn);
#endif
#if defined(FDCAN3_BASE)
NVIC_SetVector(FDCAN3_IT0_IRQn, (uint32_t)&FDCAN3_IT0_IRQHandler);
NVIC_EnableIRQ(FDCAN3_IT0_IRQn);
NVIC_SetVector(FDCAN3_IT1_IRQn, (uint32_t)&FDCAN3_IT1_IRQHandler);
NVIC_EnableIRQ(FDCAN3_IT1_IRQn);
#endif
}
#else /* FDCAN1 */
#include "cmsis.h"
#include "pinmap.h"
#include "PeripheralPins.h"
#include "mbed_error.h"
#include "can_device.h" // Specific to STM32 serie
#include <math.h>
#include <string.h>
#include <inttypes.h>
#define DEFAULT_RXFIFO 0 // default rx fifo for can by hardware is FIFO0
static uint32_t can_irq_contexts[CAN_NUM] = {0};
static can_irq_handler irq_handler;
static void can_registers_init(can_t *obj)
{
if (HAL_CAN_Init(&obj->CanHandle) != HAL_OK) {
error("Cannot initialize CAN");
}
// Set initial CAN frequency to specified frequency
if (can_frequency(obj, obj->hz) != 1) {
error("Can frequency could not be set\n");
}
}
#if STATIC_PINMAP_READY
#define CAN_INIT_FREQ_DIRECT can_init_freq_direct
void can_init_freq_direct(can_t *obj, const can_pinmap_t *pinmap, int hz)
#else
#define CAN_INIT_FREQ_DIRECT _can_init_freq_direct
static void _can_init_freq_direct(can_t *obj, const can_pinmap_t *pinmap, int hz)
#endif
{
MBED_ASSERT((int)pinmap->peripheral != NC);
if (pinmap->peripheral == CAN_1) {
__HAL_RCC_CAN1_CLK_ENABLE();
obj->index = 0;
}
#if defined(CAN2_BASE) && (CAN_NUM > 1)
else if (pinmap->peripheral == CAN_2) {
__HAL_RCC_CAN1_CLK_ENABLE(); // needed to set filters
__HAL_RCC_CAN2_CLK_ENABLE();
obj->index = 1;
}
#endif
#if defined(CAN3_BASE) && (CAN_NUM > 2)
else if (pinmap->peripheral == CAN_3) {
__HAL_RCC_CAN3_CLK_ENABLE();
obj->index = 2;
}
#endif
else {
return;
}
// Configure CAN pins
pin_function(pinmap->rd_pin, pinmap->rd_function);
pin_function(pinmap->td_pin, pinmap->td_function);
// Add pull-ups
pin_mode(pinmap->rd_pin, PullUp);
pin_mode(pinmap->td_pin, PullUp);
/* Use default values for rist init */
obj->CanHandle.Instance = (CAN_TypeDef *)pinmap->peripheral;
obj->CanHandle.Init.TTCM = DISABLE;
obj->CanHandle.Init.ABOM = DISABLE;
obj->CanHandle.Init.AWUM = DISABLE;
obj->CanHandle.Init.NART = DISABLE;
obj->CanHandle.Init.RFLM = DISABLE;
obj->CanHandle.Init.TXFP = DISABLE;
obj->CanHandle.Init.Mode = CAN_MODE_NORMAL;
obj->CanHandle.Init.SJW = CAN_SJW_1TQ;
obj->CanHandle.Init.BS1 = CAN_BS1_6TQ;
obj->CanHandle.Init.BS2 = CAN_BS2_8TQ;
obj->CanHandle.Init.Prescaler = 2;
/* Store frequency to be restored in case of reset */
obj->hz = hz;
can_registers_init(obj);
/* Bits 27:14 are available for dual CAN configuration and are reserved for
single CAN configuration: */
#if defined(CAN3_BASE) && (CAN_NUM > 2)
uint32_t filter_number = (pinmap->peripheral == CAN_1 || pinmap->peripheral == CAN_3) ? 0 : 14;
#else
uint32_t filter_number = (pinmap->peripheral == CAN_1) ? 0 : 14;
#endif
can_filter(obj, 0, 0, CANStandard, filter_number);
}
void can_init_direct(can_t *obj, const can_pinmap_t *pinmap)
{
/* default frequency is 100 kHz */
CAN_INIT_FREQ_DIRECT(obj, pinmap, 100000);
}
void can_init_freq(can_t *obj, PinName rd, PinName td, int hz)
{
CANName can_rd = (CANName)pinmap_peripheral(rd, PinMap_CAN_RD);
CANName can_td = (CANName)pinmap_peripheral(td, PinMap_CAN_TD);
int peripheral = (int) pinmap_merge(can_rd, can_td);
int function_rd = (int)pinmap_find_function(rd, PinMap_CAN_RD);
int function_td = (int)pinmap_find_function(td, PinMap_CAN_TD);
const can_pinmap_t static_pinmap = {peripheral, rd, function_rd, td, function_td};
CAN_INIT_FREQ_DIRECT(obj, &static_pinmap, hz);
}
void can_init(can_t *obj, PinName rd, PinName td)
{
can_init_freq(obj, rd, td, 100000);
}
void can_irq_init(can_t *obj, can_irq_handler handler, uintptr_t context)
{
irq_handler = handler;
can_irq_contexts[obj->index] = context;
obj->rxIrqEnabled = false;
}
void can_irq_free(can_t *obj)
{
CAN_TypeDef *can = obj->CanHandle.Instance;
can->IER &= ~(CAN_IT_FMP0 | CAN_IT_FMP1 | CAN_IT_TME | \
CAN_IT_ERR | CAN_IT_EPV | CAN_IT_BOF);
can_irq_contexts[obj->index] = 0;
obj->rxIrqEnabled = false;
}
void can_free(can_t *obj)
{
CANName can = (CANName) obj->CanHandle.Instance;
#if defined(DUAL_CORE) && (TARGET_STM32H7)
while (LL_HSEM_1StepLock(HSEM, CFG_HW_RCC_SEMID)) {
}
#endif /* DUAL_CORE */
// Reset CAN and disable clock
if (can == CAN_1) {
__HAL_RCC_CAN1_FORCE_RESET();
__HAL_RCC_CAN1_RELEASE_RESET();
__HAL_RCC_CAN1_CLK_DISABLE();
}
#if defined(CAN2_BASE) && (CAN_NUM > 1)
if (can == CAN_2) {
__HAL_RCC_CAN2_FORCE_RESET();
__HAL_RCC_CAN2_RELEASE_RESET();
__HAL_RCC_CAN2_CLK_DISABLE();
}
#endif
#if defined(CAN3_BASE) && (CAN_NUM > 2)
if (can == CAN_3) {
__HAL_RCC_CAN3_FORCE_RESET();
__HAL_RCC_CAN3_RELEASE_RESET();
__HAL_RCC_CAN3_CLK_DISABLE();
}
#endif
#if defined(DUAL_CORE) && (TARGET_STM32H7)
LL_HSEM_ReleaseLock(HSEM, CFG_HW_RCC_SEMID, HSEM_CR_COREID_CURRENT);
#endif /* DUAL_CORE */
}
// The following table is used to program bit_timing. It is an adjustment of the sample
// point by synchronizing on the start-bit edge and resynchronizing on the following edges.
// This table has the sampling points as close to 75% as possible (most commonly used).
// The first value is TSEG1, the second TSEG2.
static const int timing_pts[23][2] = {
{0x0, 0x0}, // 2, 67%
{0x1, 0x0}, // 3, 75%
{0x2, 0x0}, // 4, 80%
{0x2, 0x1}, // 5, 67%
{0x3, 0x1}, // 6, 71%
{0x4, 0x1}, // 7, 75%
{0x4, 0x2}, // 8, 67%
{0x5, 0x2}, // 9, 70%
{0x6, 0x2}, // 10, 73%
{0x7, 0x2}, // 11, 75%
{0x7, 0x3}, // 12, 69%
{0x8, 0x3}, // 13, 71%
{0x9, 0x3}, // 14, 73%
{0xA, 0x3}, // 15, 75%
{0xA, 0x4}, // 16, 71%
{0xB, 0x4}, // 17, 72%
{0xC, 0x4}, // 18, 74%
{0xD, 0x4}, // 19, 75%
{0xD, 0x5}, // 20, 71%
{0xE, 0x5}, // 21, 73%
{0xF, 0x5}, // 22, 74%
{0xF, 0x6}, // 23, 71%
{0xF, 0x7}, // 24, 68%
};
static unsigned int can_speed(unsigned int pclk, unsigned int cclk, unsigned char psjw)
{
uint32_t btr;
uint16_t brp = 0;
uint32_t calcbit;
uint32_t bitwidth;
int hit = 0;
int bits;
bitwidth = (pclk / cclk);
brp = bitwidth / 0x18;
while ((!hit) && (brp < bitwidth / 4)) {
brp++;
for (bits = 22; bits > 0; bits--) {
calcbit = (bits + 3) * (brp + 1);
if (calcbit == bitwidth) {
hit = 1;
break;
}
}
}
if (hit) {
btr = ((timing_pts[bits][1] << CAN_BTR_TS2_Pos) & CAN_BTR_TS2) |
((timing_pts[bits][0] << CAN_BTR_TS1_Pos) & CAN_BTR_TS1) |
((psjw << CAN_BTR_SJW_Pos) & CAN_BTR_SJW) |
((brp << CAN_BTR_BRP_Pos) & CAN_BTR_BRP);
} else {
btr = 0xFFFFFFFF;
}
return btr;
}
int can_frequency(can_t *obj, int f)
{
int pclk = HAL_RCC_GetPCLK1Freq();
int btr = can_speed(pclk, (unsigned int)f, 1);
CAN_TypeDef *can = obj->CanHandle.Instance;
uint32_t tickstart = 0;
int status = 1;
if (btr > 0) {
can->MCR |= CAN_MCR_INRQ ;
/* Get tick */
tickstart = HAL_GetTick();
while ((can->MSR & CAN_MSR_INAK) != CAN_MSR_INAK) {
if ((HAL_GetTick() - tickstart) > 2) {
status = 0;
break;
}
}
if (status != 0) {
/* Do not erase all BTR registers (e.g. silent mode), only the
* ones calculated in can_speed */
can->BTR &= ~(CAN_BTR_TS2 | CAN_BTR_TS1 | CAN_BTR_SJW | CAN_BTR_BRP);
can->BTR |= btr;
can->MCR &= ~(uint32_t)CAN_MCR_INRQ;
/* Get tick */
tickstart = HAL_GetTick();
while ((can->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) {
if ((HAL_GetTick() - tickstart) > 2) {
status = 0;
break;
}
}
if (status == 0) {
error("can ESR 0x%04" PRIx32 ".%04" PRIx32 " + timeout status %d", (can->ESR & 0xFFFF0000) >> 16, (can->ESR & 0xFFFF), status);
}
} else {
error("can init request timeout\n");
}
} else {
status = 0;
}
return status;
}
int can_write(can_t *obj, CAN_Message msg, int cc)
{
uint32_t transmitmailbox = CAN_TXSTATUS_NOMAILBOX;
CAN_TypeDef *can = obj->CanHandle.Instance;
/* Select one empty transmit mailbox */
if ((can->TSR & CAN_TSR_TME0) == CAN_TSR_TME0) {
transmitmailbox = 0;
} else if ((can->TSR & CAN_TSR_TME1) == CAN_TSR_TME1) {
transmitmailbox = 1;
} else if ((can->TSR & CAN_TSR_TME2) == CAN_TSR_TME2) {
transmitmailbox = 2;
} else {
return 0;
}
can->sTxMailBox[transmitmailbox].TIR &= CAN_TI0R_TXRQ;
if (!(msg.format)) {
can->sTxMailBox[transmitmailbox].TIR |= ((msg.id << 21) | (msg.type << 1));
} else {
can->sTxMailBox[transmitmailbox].TIR |= ((msg.id << 3) | CAN_ID_EXT | (msg.type << 1));
}
/* Set up the DLC */
can->sTxMailBox[transmitmailbox].TDTR &= (uint32_t)0xFFFFFFF0;
can->sTxMailBox[transmitmailbox].TDTR |= (msg.len & (uint8_t)0x0000000F);
/* Set up the data field */
can->sTxMailBox[transmitmailbox].TDLR = (((uint32_t)msg.data[3] << 24) |
((uint32_t)msg.data[2] << 16) |
((uint32_t)msg.data[1] << 8) |
((uint32_t)msg.data[0]));
can->sTxMailBox[transmitmailbox].TDHR = (((uint32_t)msg.data[7] << 24) |
((uint32_t)msg.data[6] << 16) |
((uint32_t)msg.data[5] << 8) |
((uint32_t)msg.data[4]));
/* Request transmission */
can->sTxMailBox[transmitmailbox].TIR |= CAN_TI0R_TXRQ;