Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSTM32CanFD.cpp
More file actions
624 lines (550 loc) · 20 KB
/
Copy pathSTM32CanFD.cpp
File metadata and controls
624 lines (550 loc) · 20 KB
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
#include "STM32CanFD.h"
/*
* _txHeader.fdFormat の決定ロジック (提案1: 明示的指定の有無で分岐)
* =====================================================================
* 1. classic() または fd() で明示的に指定された場合
* → その指定に従う(_txFormatExplicit = true)
*
* 2. 明示的指定なし(beginPacket()のみ)
* → データ送信時に以下で判定:
* - データ ≤ 8バイト : begin()で指定されたフレームフォーマット継承
* - データ > 8バイト : 自動的にCANFD (fdFormat=1) に切り替え
* クラシックCAN最大8バイトの制約を超えるため
*/
// 複数のインスタンス管理用
static STM32CanFD *_instances[3] = {NULL, NULL, NULL};
STM32CanFD::STM32CanFD(FDCAN_GlobalTypeDef *instance)
: _rxPin(NC), _txPin(NC), _mode(FDCAN_MODE_NORMAL)
#if STM32CANFD_STREAM_API
,
_rxHead(0), _rxTail(0)
#endif
{
hfdcan.Instance = instance;
if (instance == FDCAN1)
_instances[0] = this;
#if defined(FDCAN2)
else if (instance == FDCAN2)
_instances[1] = this;
#endif
#if defined(FDCAN3)
else if (instance == FDCAN3)
_instances[2] = this;
#endif
}
STM32CanFD::~STM32CanFD() { end(); }
void STM32CanFD::setPins(uint32_t rxPin, uint32_t txPin)
{
_rxPin = rxPin;
_txPin = txPin;
}
void STM32CanFD::setMode(uint32_t mode)
{
_mode = mode;
}
bool STM32CanFD::begin(uint32_t nomBaud, uint32_t dataBaud)
{
// if (_rxPin == NC || _txPin == NC)
// return false;
// 170MHzソースを想定した基本設定
hfdcan.Init.ClockDivider = FDCAN_CLOCK_DIV1;
hfdcan.Init.FrameFormat = (dataBaud > 0) ? FDCAN_FRAME_FD_BRS : FDCAN_FRAME_CLASSIC;
hfdcan.Init.Mode = _mode;
hfdcan.Init.AutoRetransmission = DISABLE;
hfdcan.Init.TransmitPause = DISABLE;
hfdcan.Init.ProtocolException = DISABLE;
hfdcan.Init.TxFifoQueueMode = FDCAN_TX_FIFO_OPERATION;
hfdcan.Init.StdFiltersNbr = 28;
hfdcan.Init.ExtFiltersNbr = 8;
applyBaudrate(nomBaud, dataBaud);
/** Initializes the peripherals clocks */
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_FDCAN;
PeriphClkInit.FdcanClockSelection = RCC_FDCANCLKSOURCE_PCLK1;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
Error_Handler();
}
/* Peripheral clock enable */
__HAL_RCC_FDCAN_CLK_ENABLE();
// PinName _can_rd = digitalPinToPinName(_rxPin);
// PinName _can_tx = digitalPinToPinName(_txPin);
// void *fdcan_rd = pinmap_peripheral(_can_rd, PinMap_CAN_RD);
// void *fdcan_tx = pinmap_peripheral(_can_tx, PinMap_CAN_TD);
// void *instance = pinmap_merge_peripheral(fdcan_rd, fdcan_tx);
// pinmap_pinout(_can_rd, PinMap_CAN_RD);
// pinmap_pinout(_can_tx, PinMap_CAN_TD);
__HAL_RCC_GPIOB_CLK_ENABLE();
/** FDCAN1 GPIO Configuration
PB8-BOOT0 ------> FDCAN1_RX
PB9 ------> FDCAN1_TX
*/
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF9_FDCAN1;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
__HAL_SYSCFG_FASTMODEPLUS_ENABLE(SYSCFG_FASTMODEPLUS_PB8);
__HAL_SYSCFG_FASTMODEPLUS_ENABLE(SYSCFG_FASTMODEPLUS_PB9);
if (HAL_FDCAN_Init(&hfdcan) != HAL_OK)
return false;
// フィルタ:デフォルトは無効(ユーザーが設定するまで何も受信しない)
FDCAN_FilterTypeDef sFilterConfig;
sFilterConfig.IdType = FDCAN_STANDARD_ID;
sFilterConfig.FilterIndex = 0;
sFilterConfig.FilterType = FDCAN_FILTER_MASK;
sFilterConfig.FilterConfig = FDCAN_FILTER_DISABLE;
sFilterConfig.FilterID1 = 0x000;
sFilterConfig.FilterID2 = 0x000;
HAL_FDCAN_ConfigFilter(&hfdcan, &sFilterConfig);
HAL_FDCAN_ConfigGlobalFilter(&hfdcan, FDCAN_REJECT, FDCAN_REJECT, FDCAN_REJECT_REMOTE, FDCAN_REJECT_REMOTE);
// 割り込み設定
HAL_FDCAN_ActivateNotification(&hfdcan, FDCAN_IT_RX_FIFO0_NEW_MESSAGE | FDCAN_IT_RX_FIFO1_NEW_MESSAGE, 0);
IRQn_Type irq0, irq1;
if (hfdcan.Instance == FDCAN1) {
irq0 = FDCAN1_IT0_IRQn;
irq1 = FDCAN1_IT1_IRQn;
}
#if defined(FDCAN2)
else if (hfdcan.Instance == FDCAN2)
irq0 = FDCAN2_IT0_IRQn;
irq1 = FDCAN2_IT1_IRQn;
#endif
#if defined(FDCAN3)
else if (hfdcan.Instance == FDCAN3)
irq0 = FDCAN3_IT0_IRQn;
irq1 = FDCAN3_IT1_IRQn;
#endif
HAL_NVIC_SetPriority(irq0, 0, 0);
HAL_NVIC_EnableIRQ(irq0);
HAL_NVIC_SetPriority(irq1, 0, 0);
HAL_NVIC_EnableIRQ(irq1);
return HAL_FDCAN_Start(&hfdcan) == HAL_OK;
}
void STM32CanFD::end()
{
HAL_FDCAN_DeInit(&hfdcan);
}
// 170MHz FDCAN Clock用ボーレート計算
// 内部で使用するパラメータ保持用の構造体
struct FDCAN_TimingParams {
uint32_t baudrate;
uint16_t prescaler;
uint16_t timeSeg1;
uint16_t timeSeg2;
uint16_t sjw;
};
// 170MHzクロック向け Nominal (仲裁) フェーズ用テーブル
// Sample Point (SP) は 80% 付近をターゲットに
static const FDCAN_TimingParams nominalTimings[] = {
// Baud, Prescaler, Seg1, Seg2, SJW
{1000000, 1, 135, 34, 17}, // 1Mbps: 170MHz/1/(1+135+34) = 1.0M, SP=80%
{ 500000, 2, 135, 34, 17}, // 500kbps: 170MHz/2/(1+135+34) = 500k, SP=80%
{ 250000, 4, 135, 34, 17}, // 250kbps: 170MHz/4/(1+135+34) = 250k, SP=80%
{ 125000, 8, 135, 34, 17} // 125kbps: 170MHz/8/(1+135+34) = 125k, SP=80%
};
// 170MHzクロック向け Data (データ) フェーズ用テーブル
// Sample Point (SP) は 75%〜80% 付近、SJWはSeg2未満を確保
static const FDCAN_TimingParams dataTimings[] = {
// Baud, Prescaler, Seg1, Seg2, SJW
{8000000, 1, 15, 5, 2}, // 8Mbps: 170/1/21 = 8.09M(+1.2%), SP=76%
{5000000, 1, 26, 7, 3}, // 5Mbps: 170/1/34 = 5.0M, SP=79%
{4000000, 1, 31, 10, 5}, // 4Mbps: 170/1/42 = 4.04M(+1.1%), SP=76% (Seg1 max is 32)
{2000000, 5, 12, 4, 2}, // 2Mbps: 170/5/17 = 2.0M, SP=80%
{1000000, 10, 12, 4, 2} // 1Mbps: 170/10/17 = 1.0M, SP=80%
};
void STM32CanFD::applyBaudrate(uint32_t nom, uint32_t data)
{
// --- Nominal (Arbitration) Phase 設定 ---
bool nomFound = false;
for (const auto& timing : nominalTimings) {
if (timing.baudrate == nom) {
hfdcan.Init.NominalPrescaler = timing.prescaler;
hfdcan.Init.NominalTimeSeg1 = timing.timeSeg1;
hfdcan.Init.NominalTimeSeg2 = timing.timeSeg2;
hfdcan.Init.NominalSyncJumpWidth = timing.sjw;
nomFound = true;
break;
}
}
// テーブルにない場合はデフォルト設定 (500kbps相当) または計算ロジックを入れる
if (!nomFound) {
// Fallback: 500kbps defaults
hfdcan.Init.NominalPrescaler = 2;
hfdcan.Init.NominalTimeSeg1 = 135;
hfdcan.Init.NominalTimeSeg2 = 34;
hfdcan.Init.NominalSyncJumpWidth = 17;
}
// --- Data Phase 設定 ---
if (data > 0)
{
bool dataFound = false;
for (const auto& timing : dataTimings) {
if (timing.baudrate == data) {
hfdcan.Init.DataPrescaler = timing.prescaler;
hfdcan.Init.DataTimeSeg1 = timing.timeSeg1;
hfdcan.Init.DataTimeSeg2 = timing.timeSeg2;
hfdcan.Init.DataSyncJumpWidth = timing.sjw;
dataFound = true;
break;
}
}
if (!dataFound) {
// Fallback: 2Mbps defaults
hfdcan.Init.DataPrescaler = 5;
hfdcan.Init.DataTimeSeg1 = 12;
hfdcan.Init.DataTimeSeg2 = 4;
hfdcan.Init.DataSyncJumpWidth = 2;
}
// TDC設定 (通信速度に応じて調整が必要だが、ここでは安全な値を設定)
// 2Mbps以上なら少し小さめに、それ以外は標準的な値を設定
uint32_t tdcOffset = (data >= 4000000) ? 20 : 25;
HAL_FDCAN_ConfigTxDelayCompensation(&hfdcan, tdcOffset, 0);
HAL_FDCAN_EnableTxDelayCompensation(&hfdcan);
}
else
{
HAL_FDCAN_DisableTxDelayCompensation(&hfdcan);
}
}
/* --- 送信ロジック --- */
#if STM32CANFD_STREAM_API
/**
* beginPacket - パケット送信を開始
* begin()で指定されたフレームフォーマットに基づいて_txHeaderを初期化
* _txFormatExplicit = false で設定し、classic()やfd()の呼び出しが可能に
*/
STM32CanFD &STM32CanFD::beginPacket(uint32_t id)
{
_txHeader.identifier = id;
_txHeader.dataLength = 0;
_txHeader.extended = (id > 0x7FF) ? 1 : 0;
_txHeader.remote = 0;
_txHeader.fdFormat = 0;
_txHeader.brs = 0;
_txHeader.esiPassive = 0;
// begin()で指定されたフレームフォーマットを反映
if (hfdcan.Init.FrameFormat != FDCAN_FRAME_CLASSIC)
{
_txHeader.fdFormat = 1;
_txHeader.brs = 1;
}
_txBufferIdx = 0;
_txFormatExplicit = false; // 明示的指定なしのマーク
return *this;
}
STM32CanFD &STM32CanFD::standard()
{
_txHeader.extended = 0;
return *this;
}
STM32CanFD &STM32CanFD::extended()
{
_txHeader.extended = 1;
return *this;
}
/**
* fd - CANFD フォーマットを明示的に指定
* このメソッド呼び出しで _txFormatExplicit = true となり、
* endPacket()で自動判定は行われず、ここで指定した値が確定する
*
* @param brs - Bit Rate Switching (true: BRS有効, false: BRS無効)
*/
STM32CanFD &STM32CanFD::fd(bool brs)
{
_txHeader.fdFormat = 1;
_txHeader.brs = brs ? 1 : 0;
_txFormatExplicit = true; // 明示的指定のマーク
return *this;
}
/**
* classic - クラシックCANフォーマットを明示的に指定
* このメソッド呼び出しで _txFormatExplicit = true となり、
* endPacket()で自動判定は行われず、ここで指定した値が確定する
*/
STM32CanFD &STM32CanFD::classic()
{
_txHeader.fdFormat = 0;
_txHeader.brs = 0;
_txFormatExplicit = true; // 明示的指定のマーク
return *this;
}
size_t STM32CanFD::write(uint8_t byte)
{
if (_txBufferIdx < 64)
{
_txData[_txBufferIdx++] = byte;
return 1;
}
return 0;
}
size_t STM32CanFD::write(const uint8_t *buffer, size_t size)
{
size_t n = min(size, (size_t)(64 - _txBufferIdx));
memcpy(&_txData[_txBufferIdx], buffer, n);
_txBufferIdx += n;
return n;
}
/**
* endPacket - パケット送信を終了
*
* _txFormatExplicit のフラグに基づいて異なる動作:
*
* 1. _txFormatExplicit == true(classic()またはfd()で明示的に指定)
* → その指定が確定、自動判定なし
*
* 2. _txFormatExplicit == false(明示的指定なし)
* → 自動判定ロジックが実行される:
* - _txBufferIdx ≤ 8 : クラシックCAN(fdFormat は変わらず)
* - _txBufferIdx > 8 : 自動的にCANFD (fdFormat = 1) に切り替え
* クラシックCAN最大8バイト制約の回避
*/
int STM32CanFD::endPacket()
{
// 明示的指定がなく、データが8バイトを超える場合のみ自動判定
if (!_txFormatExplicit && _txBufferIdx > 8)
{
_txHeader.fdFormat = 1; // 自動的にCANFDに切り替え
}
_txHeader.dataLength = len2dlc(_txBufferIdx);
size_t fullLen = dlc2len(_txHeader.dataLength);
if (fullLen > _txBufferIdx)
{
memset(&_txData[_txBufferIdx], 0, fullLen - _txBufferIdx);
}
return sendPacket(_txHeader, _txData) ? 1 : 0;
}
/* --- 受信ロジック --- */
bool STM32CanFD::parsePacket()
{
uint32_t level0 = HAL_FDCAN_GetRxFifoFillLevel(&hfdcan, FDCAN_RX_FIFO0);
uint32_t level1 = HAL_FDCAN_GetRxFifoFillLevel(&hfdcan, FDCAN_RX_FIFO1);
uint8_t fifo = 0;
if (level0 > 0)
{
fifo = 0;
}
else if (level1 > 0)
{
fifo = 1;
}
else
{
return false;
}
int received = readPacket(fifo, &_currentRx.header, _rxBuffer);
if (received < 0)
return false;
_currentRx.length = (uint8_t)received;
_currentRx.fifo = fifo;
_rxHead = _currentRx.length;
_rxTail = 0;
return true;
}
#endif
int STM32CanFD::readPacket(uint8_t fifo, CanMessageHeader *header, uint8_t *buffer)
{
if (fifo > 1 || header == nullptr || buffer == nullptr)
return -1;
FDCAN_RxHeaderTypeDef rxHeader;
uint32_t fifoType = (fifo == 0) ? FDCAN_RX_FIFO0 : FDCAN_RX_FIFO1;
if (HAL_FDCAN_GetRxMessage(&hfdcan, fifoType, &rxHeader, buffer) != HAL_OK)
return -1;
*header = compactRxHeader(rxHeader);
return (int)dlc2len(header->dataLength);
}
bool STM32CanFD::sendPacket(const CanMessageHeader &header, const uint8_t *data)
{
FDCAN_TxHeaderTypeDef txHeader = expandTxHeader(header);
return HAL_FDCAN_AddMessageToTxFifoQ(&hfdcan, &txHeader, (uint8_t *)data) == HAL_OK;
}
// --- Standard ID Filters (index: 0-27) ---
bool STM32CanFD::setFilterMask(uint8_t index, uint32_t id, uint32_t mask, int fifo)
{
FDCAN_FilterTypeDef sFilterConfig;
sFilterConfig.IdType = FDCAN_STANDARD_ID;
sFilterConfig.FilterIndex = index;
sFilterConfig.FilterType = FDCAN_FILTER_MASK;
sFilterConfig.FilterConfig = (fifo == 1) ? FDCAN_FILTER_TO_RXFIFO1 : FDCAN_FILTER_TO_RXFIFO0;
sFilterConfig.FilterID1 = id;
sFilterConfig.FilterID2 = mask;
return HAL_FDCAN_ConfigFilter(&hfdcan, &sFilterConfig) == HAL_OK;
}
bool STM32CanFD::setFilterRange(uint8_t index, uint32_t id1, uint32_t id2, int fifo)
{
FDCAN_FilterTypeDef sFilterConfig;
sFilterConfig.IdType = FDCAN_STANDARD_ID;
sFilterConfig.FilterIndex = index;
sFilterConfig.FilterType = FDCAN_FILTER_RANGE;
sFilterConfig.FilterConfig = (fifo == 1) ? FDCAN_FILTER_TO_RXFIFO1 : FDCAN_FILTER_TO_RXFIFO0;
sFilterConfig.FilterID1 = id1;
sFilterConfig.FilterID2 = id2;
return HAL_FDCAN_ConfigFilter(&hfdcan, &sFilterConfig) == HAL_OK;
}
bool STM32CanFD::setFilterDual(uint8_t index, uint32_t id1, uint32_t id2, int fifo)
{
FDCAN_FilterTypeDef sFilterConfig;
sFilterConfig.IdType = FDCAN_STANDARD_ID;
sFilterConfig.FilterIndex = index;
sFilterConfig.FilterType = FDCAN_FILTER_DUAL;
sFilterConfig.FilterConfig = (fifo == 1) ? FDCAN_FILTER_TO_RXFIFO1 : FDCAN_FILTER_TO_RXFIFO0;
sFilterConfig.FilterID1 = id1;
sFilterConfig.FilterID2 = id2;
return HAL_FDCAN_ConfigFilter(&hfdcan, &sFilterConfig) == HAL_OK;
}
// --- Extended ID Filters (index: 0-7) ---
bool STM32CanFD::setFilterMaskExt(uint8_t index, uint32_t id, uint32_t mask, int fifo)
{
FDCAN_FilterTypeDef sFilterConfig;
sFilterConfig.IdType = FDCAN_EXTENDED_ID;
sFilterConfig.FilterIndex = index;
sFilterConfig.FilterType = FDCAN_FILTER_MASK;
sFilterConfig.FilterConfig = (fifo == 1) ? FDCAN_FILTER_TO_RXFIFO1 : FDCAN_FILTER_TO_RXFIFO0;
sFilterConfig.FilterID1 = id;
sFilterConfig.FilterID2 = mask;
return HAL_FDCAN_ConfigFilter(&hfdcan, &sFilterConfig) == HAL_OK;
}
bool STM32CanFD::setFilterRangeExt(uint8_t index, uint32_t id1, uint32_t id2, int fifo)
{
FDCAN_FilterTypeDef sFilterConfig;
sFilterConfig.IdType = FDCAN_EXTENDED_ID;
sFilterConfig.FilterIndex = index;
sFilterConfig.FilterType = FDCAN_FILTER_RANGE;
sFilterConfig.FilterConfig = (fifo == 1) ? FDCAN_FILTER_TO_RXFIFO1 : FDCAN_FILTER_TO_RXFIFO0;
sFilterConfig.FilterID1 = id1;
sFilterConfig.FilterID2 = id2;
return HAL_FDCAN_ConfigFilter(&hfdcan, &sFilterConfig) == HAL_OK;
}
bool STM32CanFD::setFilterDualExt(uint8_t index, uint32_t id1, uint32_t id2, int fifo)
{
FDCAN_FilterTypeDef sFilterConfig;
sFilterConfig.IdType = FDCAN_EXTENDED_ID;
sFilterConfig.FilterIndex = index;
sFilterConfig.FilterType = FDCAN_FILTER_DUAL;
sFilterConfig.FilterConfig = (fifo == 1) ? FDCAN_FILTER_TO_RXFIFO1 : FDCAN_FILTER_TO_RXFIFO0;
sFilterConfig.FilterID1 = id1;
sFilterConfig.FilterID2 = id2;
return HAL_FDCAN_ConfigFilter(&hfdcan, &sFilterConfig) == HAL_OK;
}
uint32_t STM32CanFD::getErrorCode()
{
return HAL_FDCAN_GetError(&hfdcan);
}
bool STM32CanFD::isBusOff()
{
return (hfdcan.Instance->PSR & FDCAN_PSR_BO) != 0;
}
#if STM32CANFD_STREAM_API
int STM32CanFD::available() { return _rxHead - _rxTail; }
int STM32CanFD::read() { return (_rxHead > _rxTail) ? _rxBuffer[_rxTail++] : -1; }
int STM32CanFD::peek() { return (_rxHead > _rxTail) ? _rxBuffer[_rxTail] : -1; }
#endif
/* --- ユーティリティ --- */
uint8_t STM32CanFD::len2dlc(size_t len)
{
if (len <= 8)
return len;
if (len <= 12)
return FDCAN_DLC_BYTES_12;
if (len <= 16)
return FDCAN_DLC_BYTES_16;
if (len <= 20)
return FDCAN_DLC_BYTES_20;
if (len <= 24)
return FDCAN_DLC_BYTES_24;
if (len <= 32)
return FDCAN_DLC_BYTES_32;
if (len <= 48)
return FDCAN_DLC_BYTES_48;
return FDCAN_DLC_BYTES_64;
}
size_t STM32CanFD::dlc2len(uint32_t dlc)
{
if (dlc <= 8)
return dlc;
switch (dlc)
{
case FDCAN_DLC_BYTES_12:
return 12;
case FDCAN_DLC_BYTES_16:
return 16;
case FDCAN_DLC_BYTES_20:
return 20;
case FDCAN_DLC_BYTES_24:
return 24;
case FDCAN_DLC_BYTES_32:
return 32;
case FDCAN_DLC_BYTES_48:
return 48;
case FDCAN_DLC_BYTES_64:
return 64;
}
return 0;
}
FDCAN_TxHeaderTypeDef STM32CanFD::expandTxHeader(const CanMessageHeader &header)
{
FDCAN_TxHeaderTypeDef halHeader = {};
halHeader.Identifier = header.identifier;
halHeader.IdType = header.extended ? FDCAN_EXTENDED_ID : FDCAN_STANDARD_ID;
halHeader.TxFrameType = header.remote ? FDCAN_REMOTE_FRAME : FDCAN_DATA_FRAME;
halHeader.DataLength = header.dataLength;
halHeader.ErrorStateIndicator = header.esiPassive ? FDCAN_ESI_PASSIVE : FDCAN_ESI_ACTIVE;
halHeader.BitRateSwitch = header.brs ? FDCAN_BRS_ON : FDCAN_BRS_OFF;
halHeader.FDFormat = header.fdFormat ? FDCAN_FD_CAN : FDCAN_CLASSIC_CAN;
halHeader.TxEventFifoControl = FDCAN_NO_TX_EVENTS;
halHeader.MessageMarker = 0;
return halHeader;
}
STM32CanFD::CanMessageHeader STM32CanFD::compactRxHeader(const FDCAN_RxHeaderTypeDef &header)
{
CanMessageHeader compact = {};
compact.identifier = header.Identifier;
compact.dataLength = (uint8_t)header.DataLength;
compact.extended = (header.IdType == FDCAN_EXTENDED_ID) ? 1 : 0;
compact.remote = (header.RxFrameType == FDCAN_REMOTE_FRAME) ? 1 : 0;
compact.fdFormat = (header.FDFormat == FDCAN_FD_CAN) ? 1 : 0;
compact.brs = (header.BitRateSwitch == FDCAN_BRS_ON) ? 1 : 0;
compact.esiPassive = (header.ErrorStateIndicator == FDCAN_ESI_PASSIVE) ? 1 : 0;
return compact;
}
// 割り込みハンドラの実装
void STM32CanFD::irqHandler()
{
HAL_FDCAN_IRQHandler(&hfdcan);
}
extern "C" void FDCAN1_IT0_IRQHandler(void)
{
if (_instances[0])
_instances[0]->irqHandler();
}
extern "C" void FDCAN1_IT1_IRQHandler(void)
{
if (_instances[0])
_instances[0]->irqHandler();
}
#if defined(FDCAN2)
extern "C" void FDCAN2_IT0_IRQHandler(void)
{
if (_instances[1])
_instances[1]->irqHandler();
}
extern "C" void FDCAN2_IT1_IRQHandler(void)
{
if (_instances[1])
_instances[1]->irqHandler();
}
#endif
#if defined(FDCAN3)
extern "C" void FDCAN3_IT0_IRQHandler(void)
{
if (_instances[2])
_instances[2]->irqHandler();
}
extern "C" void FDCAN3_IT1_IRQHandler(void)
{
if (_instances[2])
_instances[2]->irqHandler();
}
#endif
You can’t perform that action at this time.
