-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSTM32_CAN.cpp
More file actions
657 lines (551 loc) · 21.8 KB
/
STM32_CAN.cpp
File metadata and controls
657 lines (551 loc) · 21.8 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
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
/*
STM32_CAN.cpp
Use with STM32F1 HAL for your MCU!
Copyright (c) 2022 Minos Eigenheer
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use,
copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "STM32_CAN.hpp"
#ifdef DEBUG // STM32CubeIDE is automatically defining DEBUG for the debug build
//#define STM32_CAN_DEBUG
#define STM32_CAN_DEBUG_ERRORS
#endif
#if defined(STM32_CAN_DEBUG)
# define DbgPrintf(fmt, args...) printf(fmt, ## args); printf("\n")
#else
# define DbgPrintf(fmt, args...)
#endif
#if defined(STM32_CAN_DEBUG_ERRORS)
# define ErrDbgPrintf(fmt, args...) printf(fmt, ## args); printf("\n")
#else
# define ErrDbgPrintf(fmt, args...)
#endif
size_t nextInstanceID = 0;
tSTM32_CAN *STM32CANInstances[4];
//*****************************************************************************
tSTM32_CAN::tSTM32_CAN(CAN_HandleTypeDef *_canBus, CANbaudRatePrescaler _CANbaudRate) :
canBus(_canBus),
CANbaudRate(_CANbaudRate) {
//NMEA2000_STM32_instance = this;
STM32CANInstances[nextInstanceID] = this;
nextInstanceID++;
//STM32CANInstances.push_back(this); // add this instance to STM32CANInstances
DbgPrintf("created tSTM32_CAN instance %p", this);
// TODO for now we only use RX fifo 0
CANRxFIFO = CAN_RX_FIFO0;
rxRing = 0;
txRing = 0;
MaxCANReceiveFrames = 32;
MaxCANSendFrames = 16;
// TODO make prioBits configurable for each instance
prioBits = 3; // For NMEA2000 or SAE J1939 we use only the 3 highest bits of the ID for the priority
maxPrio = pow(2, prioBits) - 1; // max unsigned value is 2^prioBits -1 (for 3 bits priority 0...7)
bufferFull = false;
bxCanErrorCode = 0;
}
//*****************************************************************************
bool tSTM32_CAN::CANOpen() {
bool ret = true;
// CAN initialisation instead of using the by the STM32cubeIDE configuration tool generated init function
if (CANInit() != HAL_OK) {
ret = false;
}
// activate CAN callback interrupts
if (HAL_CAN_ActivateNotification(canBus,
CAN_IT_RX_FIFO0_MSG_PENDING
| CAN_IT_TX_MAILBOX_EMPTY
| CAN_IT_ERROR_WARNING
| CAN_IT_ERROR_PASSIVE
| CAN_IT_BUSOFF
| CAN_IT_LAST_ERROR_CODE
| CAN_IT_ERROR
) != HAL_OK) {
ret = false;
}
// Enable CAN
if (HAL_CAN_Start(canBus) == HAL_OK) {
DbgPrintf("%s started", CANname.c_str());
}
else {
ret = false;
}
return ret;
}
//*****************************************************************************
bool tSTM32_CAN::CANSendFrame(unsigned long id, unsigned char len, const unsigned char* buf, bool wait_sent, bool extended_id)
{
bool ret = false;
uint8_t prio;
if (extended_id) {
prio = (uint8_t)(id >> (29-prioBits) & maxPrio);
}
else {
prio = (uint8_t)(id >> (11-prioBits) & maxPrio);
}
HAL_CAN_DeactivateNotification(canBus, CAN_IT_TX_MAILBOX_EMPTY);
bool TxMailboxesFull = HAL_CAN_GetTxMailboxesFreeLevel(canBus) == 0;
bool SendFromBuffer = false;
// If TX buffer has already some frames waiting with higher prio or mailbox is full, buffer frame
if ( !txRing->isEmpty(prio) || TxMailboxesFull ) {
CAN_message_t *msg = txRing->getAddRef(prio);
if ( msg!=0 ) {
msg->id = id;
msg->flags.extended = extended_id;
if ( len > 8 ) len = 8;
msg->flags.remote = 0;
msg->len = len;
memcpy(msg->buf, buf, len);
ret = true;
//frame buffered
DbgPrintf("%s frame 0x%lx buffered", CANname.c_str(), id);
}
SendFromBuffer = true;
}
if ( !TxMailboxesFull ) {
if ( SendFromBuffer ) {
ret = SendFromTxRing();
} else {
ret = CANWriteTxMailbox(id, len, buf, extended_id);
}
/* transmit entry accepted */
}
else {
DbgPrintf("%s All TX mailboxes full", CANname.c_str());
}
HAL_CAN_ActivateNotification(canBus, CAN_IT_TX_MAILBOX_EMPTY);
return ret;
}
//*****************************************************************************
bool tSTM32_CAN::CANSendFrame(tSTM32_CAN::CAN_message_t* message)
{
bool ret = false;
uint8_t prio;
if (message->flags.extended) {
prio = (uint8_t)(message->id >> (29-prioBits) & maxPrio);
}
else {
prio = (uint8_t)(message->id >> (11-prioBits) & maxPrio);
}
HAL_CAN_DeactivateNotification(canBus, CAN_IT_TX_MAILBOX_EMPTY);
bool TxMailboxesFull = HAL_CAN_GetTxMailboxesFreeLevel(canBus) == 0;
if ( TxMailboxesFull ) {
DbgPrintf("%s all TX mailboxes are full", CANname.c_str());
}
bool SendFromBuffer = false;
// If TX buffer has already some frames waiting with higher prio or mailbox is full, buffer frame
if ( !txRing->isEmpty(prio) || TxMailboxesFull ) {
CAN_message_t *msg = txRing->getAddRef(prio);
if ( msg!=0 ) {
memcpy(msg, message, sizeof(*msg));
ret = true;
//frame buffered
bufferFull = false;
DbgPrintf("%s frame 0x%lx buffered", CANname.c_str(), message->id);
}
else if (!bufferFull){
bufferFull = true;
ErrDbgPrintf("%s TX ringbuffer is full", CANname.c_str());
}
SendFromBuffer = true;
}
if ( !TxMailboxesFull ) {
if ( SendFromBuffer ) {
ret = SendFromTxRing();
} else {
ret = CANWriteTxMailbox(message->id, message->len, message->buf, message->flags.extended);
}
/* transmit entry accepted */
}
HAL_CAN_ActivateNotification(canBus, CAN_IT_TX_MAILBOX_EMPTY);
return ret;
}
//*****************************************************************************
bool tSTM32_CAN::CANGetFrame(unsigned long& id, unsigned char& len, unsigned char* buf) {
bool ret = false;
HAL_CAN_DeactivateNotification(canBus, CAN_IT_RX_FIFO0_MSG_PENDING);
const CAN_message_t *msg = rxRing->getReadRef();
if ( msg!=0 ) {
id = msg->id;
len = msg->len;
if ( len > 8 ) len = 8;
memcpy(buf, msg->buf, len);
ret = true;
}
HAL_CAN_ActivateNotification(canBus, CAN_IT_RX_FIFO0_MSG_PENDING);
return ret;
}
//*****************************************************************************
bool tSTM32_CAN::CANGetFrameStruct(tSTM32_CAN::CAN_message_t* message) {
bool ret = false;
HAL_CAN_DeactivateNotification(canBus, CAN_IT_RX_FIFO0_MSG_PENDING);
const CAN_message_t *msg = rxRing->getReadRef();
if ( msg!=0 ) {
//message = msg;
memcpy(message, msg, sizeof(*msg));
ret = true;
}
HAL_CAN_ActivateNotification(canBus, CAN_IT_RX_FIFO0_MSG_PENDING);
return ret;
}
// *****************************************************************************
void tSTM32_CAN::InitCANFrameBuffers() {
if ( MaxCANReceiveFrames == 0 ) MaxCANReceiveFrames = 32; // Use default, if not set
if ( MaxCANReceiveFrames < 10 ) MaxCANReceiveFrames = 10; // Do not allow less than 10 - should have enough memory.
if ( MaxCANSendFrames == 0 ) MaxCANSendFrames = 50; // Use big enough default buffer
if ( MaxCANSendFrames < 30 ) MaxCANSendFrames = 30; // Do not allow less than 30 - should have enough memory.
//TODO deleting ring buffer results in hard fault!
// if buffer is initialized with a different size delete it
if ( rxRing != 0 && rxRing->getSize() != MaxCANReceiveFrames ) {
delete rxRing;
rxRing = 0;
}
if ( txRing != 0 && txRing->getSize() != MaxCANSendFrames ) {
delete txRing;
txRing = 0;
}
if ( rxRing == 0 ) {
rxRing=new tPriorityRingBuffer<CAN_message_t>(MaxCANReceiveFrames, maxPrio);
DbgPrintf("%s RX ring buffer initialized: MaxCANReceiveFrames: %u | maxPrio: %lu", CANname.c_str(), MaxCANReceiveFrames, maxPrio);
}
if ( txRing == 0 ) {
txRing=new tPriorityRingBuffer<CAN_message_t>(MaxCANSendFrames, maxPrio);
DbgPrintf("%s TX ring buffer initialized: MaxCANSendFrames: %u | maxPrio: %lu", CANname.c_str(), MaxCANReceiveFrames, maxPrio);
}
}
// *****************************************************************************
bool tSTM32_CAN::CANWriteTxMailbox(unsigned long id, unsigned char len, const unsigned char *buf, bool extended) {
if (extended) {
CANTxHeader.IDE = CAN_ID_EXT;
CANTxHeader.ExtId = id & 0x1FFFFFFF;
} else {
CANTxHeader.IDE = CAN_ID_STD;
CANTxHeader.StdId = id & 0x7FF;
}
CANTxHeader.RTR = CAN_RTR_DATA;
CANTxHeader.DLC = len;
CANTxHeader.TransmitGlobalTime = DISABLE;
if ( len > 8 ) len = 8;
memcpy(CANTxdata, buf, len);
//for (int i = 0; i < len; i++) {
// CANTxdata[i] = buf[i];
//}
// send message
if (HAL_CAN_AddTxMessage(canBus, &CANTxHeader, CANTxdata, &CANTxMailbox) == HAL_OK) {
DbgPrintf("%s Added frame 0x%lx to TX mailbox %lu", CANname.c_str(), id, CANTxMailbox);
return true;
} else {
ErrDbgPrintf("%s Failed to write TX mailbox %lu (0x%lx)", CANname.c_str(), CANTxMailbox, id);
return false;
}
}
// *****************************************************************************
bool tSTM32_CAN::SendFromTxRing() {
const CAN_message_t *txMsg;
txMsg = txRing->getReadRef(); // always get highest prio message from the buffer
if ( txMsg != 0 ) {
return CANWriteTxMailbox(txMsg->id, txMsg->len, txMsg->buf, txMsg->flags.extended);
} else {
return false;
}
}
// *****************************************************************************
void tSTM32_CAN::CANReadRxMailbox(CAN_HandleTypeDef *hcan, uint32_t CANRxFIFOn) {
CAN_message_t *rxMsg;
uint32_t id;
uint8_t prio;
if (hcan == canBus) {
if (HAL_CAN_GetRxMessage(hcan, CANRxFIFOn, &CANRxHeader, CANRxdata) == HAL_OK) {
if (CANRxHeader.IDE == CAN_ID_EXT) {
id = CANRxHeader.ExtId;
prio = (uint8_t)(id >> (29-prioBits) & maxPrio);
}
else {
id = CANRxHeader.StdId;
prio = (uint8_t)(id >> (11-prioBits) & maxPrio);
}
rxMsg = rxRing->getAddRef(prio);
if ( rxMsg!=0 ) {
rxMsg->len = CANRxHeader.DLC;
if ( rxMsg->len > 8 ) rxMsg->len = 8;
rxMsg->flags.remote = CANRxHeader.RTR == CAN_RTR_REMOTE;
rxMsg->flags.extended = CANRxHeader.IDE == CAN_ID_EXT;
rxMsg->id = id;
memcpy(rxMsg->buf, CANRxdata, rxMsg->len);
}
DbgPrintf("%s Received CAN message 0x%lx", CANname.c_str(), id);
}
}
// I think we don't have to check the fifo fill level if we use interrupts?
// HAL_CAN_GetRxFifoFillLevel(*canBus, CAN_RX_FIFO1);
}
/**
* @brief CAN Initialization Function
* @retval bool success or not
*/
HAL_StatusTypeDef tSTM32_CAN::CANInit()
{
// CAN1000kbitPrescaler, TimeSeg1 and TimeSeg2 are configured for 1000 kbit/s @ defined clock speed
// Baud rate has to be dividable by 1000 (500, 250, 200, 125, 100...)
CAN_TypeDef *CANinstance;
#ifdef CAN1
if (canBus == &hcan1) {
CANinstance = CAN1;
CANname = "CAN1";
}
#ifdef CAN2
else if (canBus == &hcan2) {
CANinstance = CAN2;
CANname = "CAN2";
}
#endif
#ifdef CAN3
else if (canBus == &hcan3) {
CANinstance = CAN3;
CANname = "CAN3";
}
#endif
else {
// CAN_HandleTypeDef *hcan is unknown
return HAL_ERROR;
}
#endif // CAN1
DbgPrintf("%s initialization", CANname.c_str());
uint32_t CAN1000kbitPrescaler;
uint32_t CANtimeSeg1;
uint32_t CANtimeSeg2;
// usually the APB1 clock is running at the following speed if max clock frequencies are used:
// STM32F103/105/107 36'000'000 Hz
// STM32F405/407 42 000'000 Hz
uint32_t APB1clockSpeed = HAL_RCC_GetPCLK1Freq();
if (APB1clockSpeed == 24000000) {
CAN1000kbitPrescaler = 2;
CANtimeSeg1 = CAN_BS1_10TQ;
CANtimeSeg2 = CAN_BS2_1TQ;
}
else if (APB1clockSpeed == 36000000) {
CAN1000kbitPrescaler = 2;
CANtimeSeg1 = CAN_BS1_15TQ;
CANtimeSeg2 = CAN_BS2_2TQ;
}
else if (APB1clockSpeed == 42000000) {
CAN1000kbitPrescaler = 3;
CANtimeSeg1 = CAN_BS1_11TQ;
CANtimeSeg2 = CAN_BS2_2TQ;
}
else if (APB1clockSpeed == 48000000) {
CAN1000kbitPrescaler = 3;
CANtimeSeg1 = CAN_BS1_13TQ;
CANtimeSeg2 = CAN_BS2_2TQ;
}
else {
// There are no settings four your ABT1 clock speed yet!
// On the following website you can find a matching prescaler, TS1 and TS2
// Add the values for your clock speed and 1000kbit/s
// http://www.bittiming.can-wiki.info/?CLK=36&ctype=bxCAN&SamplePoint=87.5
ErrDbgPrintf("%s initialization error (No CAN settings for this clock speed.)", CANname.c_str());
return HAL_ERROR;
}
canBus->Instance = CANinstance;
canBus->Init.Prescaler = (CAN1000kbitPrescaler * CANbaudRate);
canBus->Init.Mode = CAN_MODE_NORMAL;
canBus->Init.SyncJumpWidth = CAN_SJW_1TQ;
canBus->Init.TimeSeg1 = CANtimeSeg1;
canBus->Init.TimeSeg2 = CANtimeSeg2;
canBus->Init.TimeTriggeredMode = DISABLE;
canBus->Init.AutoBusOff = ENABLE;
canBus->Init.AutoWakeUp = ENABLE;
canBus->Init.AutoRetransmission = ENABLE;
canBus->Init.ReceiveFifoLocked = DISABLE;
canBus->Init.TransmitFifoPriority = ENABLE;
return HAL_CAN_Init(canBus);
}
void tSTM32_CAN::CANDeinit()
{
HAL_CAN_DeInit(canBus);
}
/*******************************************************************************
* @brief Set STM32 HAL CAN filter
* @param ExtendedIdentifier: 0 = normal CAN identifier; 1 = extended CAN identifier
* @param FilterNum CAN bus filter number for this CAN bus 0...13 / 0...27
* @param Mask uint32_t bit mask
* @param Filter uint32_t CAN identifier
* @retval success or not
*/
HAL_StatusTypeDef tSTM32_CAN::SetCANFilter(bool ExtendedId, uint32_t FilterNum, uint32_t Mask, uint32_t Id)
{
HAL_StatusTypeDef ret = HAL_ERROR;
// For dual CAN MCU's we have 28 filter banks to share between the CAN busses
// For single CAN SlaveStartFilterBank does nothing and we have 14 filter banks.
// If not defined different we use filter 0 .. 13 for primary CAN bus and 14 ... 27 for secondary CAN bus
#if !defined(SlaveStartFilterBank)
const uint32_t SlaveStartFilterBank = 14;
#endif
#if defined(CAN2) // we have two CAN busses
const int32_t TotalFilterBanks = 27;
#elif defined(CAN1) // we have only one CAN bus
const int32_t TotalFilterBanks = 13;
#else // we have no CAN defined
const int32_t TotalFilterBanks = -1;
#endif
int32_t FilterBank = -1;
if (canBus->Instance == CAN1
&& FilterNum <= TotalFilterBanks
&& FilterNum < SlaveStartFilterBank ) {
FilterBank = FilterNum;
}
else if (canBus->Instance == CAN2
&& FilterNum <= TotalFilterBanks - SlaveStartFilterBank) {
FilterBank = FilterNum + SlaveStartFilterBank;
}
if ( FilterBank >= 0
&& IS_CAN_ALL_INSTANCE(canBus->Instance) )
{
CAN_FilterTypeDef sFilterConfig;
sFilterConfig.FilterBank = FilterBank;
sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
if (ExtendedId == false)
{
sFilterConfig.FilterMaskIdHigh = Mask << 5 & 0xFFFF;
sFilterConfig.FilterMaskIdLow = 1 << 2; // Mask the IDE bit 2 (standard ID) // allows both remote request and data frames
sFilterConfig.FilterIdHigh = Id << 5 & 0xFFFF; // STDID[10:0]
sFilterConfig.FilterIdLow = 0x0000; // IDE bit 2 needs to be 0 for standard ID
}
else
{ // ExtendedIdentifier == true
sFilterConfig.FilterMaskIdHigh = Mask >> 13 & 0xFFFF;
sFilterConfig.FilterMaskIdLow = (Mask << 3 & 0xFFF8) | (1 << 2);
sFilterConfig.FilterIdHigh = Id >> 13 & 0xFFFF; // EXTID[28:13]
sFilterConfig.FilterIdLow = (Id << 3 & 0xFFF8) | (1 << 2); // EXTID[12:0] + IDE
}
sFilterConfig.FilterFIFOAssignment = CANRxFIFO;
sFilterConfig.FilterActivation = CAN_FILTER_ENABLE;
sFilterConfig.SlaveStartFilterBank = SlaveStartFilterBank; // CAN 0: 0...13 // CAN 1: 14...27 (28 filter banks in total)
ret = HAL_CAN_ConfigFilter(canBus, &sFilterConfig);
DbgPrintf("%s filter bank %li mask: %08lx, Id: %08lx", CANname.c_str(), FilterBank, Mask, Id);
}
return ret;
}
/*******************************************************************************
* @brief returns pointer to tSTM32_CAN instance from certain CAN_HandleTypeDef struct
* @param hcan CAN_HandleTypeDef pointer
* @retval tSTM32_CAN pointer
*/
tSTM32_CAN* getInstance(CAN_HandleTypeDef *hcan)
{
tSTM32_CAN* ret = 0;
//DbgPrintf("searching for instance with CAN_HandleTypeDef = %p", hcan);
for(size_t i = 0; i < nextInstanceID; ++i) {
if (STM32CANInstances[i]->canBus == hcan) {
ret = STM32CANInstances[i];
//DbgPrintf("found matching instance %p", ret->canBus);
}
}
return ret;
}
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
{
getInstance(hcan)->CANReadRxMailbox(hcan, CAN_RX_FIFO0);
//DbgPrintf("%s HAL_CAN_RxFifo0MsgPendingCallback", getInstance(hcan)->CANname.c_str());
getInstance(hcan)->setImportantCanError(hcan->ErrorCode); // copy CAN error state from HAL CAN handler to STM_CAN_Lib instance
}
void HAL_CAN_TxMailbox0CompleteCallback(CAN_HandleTypeDef *hcan)
{
getInstance(hcan)->SendFromTxRing(); // send message with highest priority on ring buffer
//DbgPrintf("%s HAL_CAN_TxMailbox0CompleteCallback", getInstance(hcan)->CANname.c_str());
getInstance(hcan)->setImportantCanError(hcan->ErrorCode);
}
void HAL_CAN_TxMailbox1CompleteCallback(CAN_HandleTypeDef *hcan)
{
getInstance(hcan)->SendFromTxRing(); // send message with highest priority on ring buffer
getInstance(hcan)->setImportantCanError(hcan->ErrorCode);
}
void HAL_CAN_TxMailbox2CompleteCallback(CAN_HandleTypeDef *hcan)
{
getInstance(hcan)->SendFromTxRing(); // send message with highest priority on ring buffer
getInstance(hcan)->setImportantCanError(hcan->ErrorCode);
}
void HAL_CAN_ErrorCallback(CAN_HandleTypeDef *hcan)
{
getInstance(hcan)->setImportantCanError(hcan->ErrorCode);
#if defined(STM32_CAN_DEBUG_ERRORS)
if(hcan->ErrorCode == 0) { ErrDbgPrintf("%s: No error", getInstance(hcan)->CANname.c_str()); }
if(hcan->ErrorCode & HAL_CAN_ERROR_EWG) { ErrDbgPrintf("%s: Protocol Error Warning", getInstance(hcan)->CANname.c_str()); }
if(hcan->ErrorCode & HAL_CAN_ERROR_EPV) { ErrDbgPrintf("%s: Error Passive", getInstance(hcan)->CANname.c_str()); }
if(hcan->ErrorCode & HAL_CAN_ERROR_BOF) { ErrDbgPrintf("%s: Bus-off error", getInstance(hcan)->CANname.c_str()); }
if(hcan->ErrorCode & HAL_CAN_ERROR_STF) { ErrDbgPrintf("%s: Stuff error", getInstance(hcan)->CANname.c_str()); }
if(hcan->ErrorCode & HAL_CAN_ERROR_FOR) { ErrDbgPrintf("%s: Form error", getInstance(hcan)->CANname.c_str()); }
if(hcan->ErrorCode & HAL_CAN_ERROR_ACK) { ErrDbgPrintf("%s: Acknowledgment error", getInstance(hcan)->CANname.c_str()); }
if(hcan->ErrorCode & HAL_CAN_ERROR_BR) { ErrDbgPrintf("%s: Bit recessive error", getInstance(hcan)->CANname.c_str()); }
if(hcan->ErrorCode & HAL_CAN_ERROR_BD) { ErrDbgPrintf("%s: Bit dominant error", getInstance(hcan)->CANname.c_str()); }
if(hcan->ErrorCode & HAL_CAN_ERROR_CRC) { ErrDbgPrintf("%s: CRC error", getInstance(hcan)->CANname.c_str()); }
if(hcan->ErrorCode & HAL_CAN_ERROR_RX_FOV0) { ErrDbgPrintf("%s: Rx FIFO 0 overrun error", getInstance(hcan)->CANname.c_str()); }
if(hcan->ErrorCode & HAL_CAN_ERROR_RX_FOV1) { ErrDbgPrintf("%s: Rx FIFO 1 overrun error", getInstance(hcan)->CANname.c_str()); }
if(hcan->ErrorCode & HAL_CAN_ERROR_TX_ALST0) { ErrDbgPrintf("%s: TxMailbox 0 transmit failure due to arbitration lost", getInstance(hcan)->CANname.c_str()); }
if(hcan->ErrorCode & HAL_CAN_ERROR_TX_TERR0) { ErrDbgPrintf("%s: TxMailbox 0 transmit failure due to transmit error", getInstance(hcan)->CANname.c_str()); }
if(hcan->ErrorCode & HAL_CAN_ERROR_TX_ALST1) { ErrDbgPrintf("%s: TxMailbox 1 transmit failure due to arbitration lost", getInstance(hcan)->CANname.c_str()); }
if(hcan->ErrorCode & HAL_CAN_ERROR_TX_TERR1) { ErrDbgPrintf("%s: TxMailbox 1 transmit failure due to transmit error", getInstance(hcan)->CANname.c_str()); }
if(hcan->ErrorCode & HAL_CAN_ERROR_TX_ALST2) { ErrDbgPrintf("%s: TxMailbox 2 transmit failure due to arbitration lost", getInstance(hcan)->CANname.c_str()); }
if(hcan->ErrorCode & HAL_CAN_ERROR_TX_TERR2) { ErrDbgPrintf("%s: TxMailbox 2 transmit failure due to transmit error", getInstance(hcan)->CANname.c_str()); }
if(hcan->ErrorCode & HAL_CAN_ERROR_TIMEOUT) { ErrDbgPrintf("%s: Timeout error", getInstance(hcan)->CANname.c_str()); }
if(hcan->ErrorCode & HAL_CAN_ERROR_NOT_INITIALIZED) { ErrDbgPrintf("%s: Peripheral not initialized", getInstance(hcan)->CANname.c_str()); }
if(hcan->ErrorCode & HAL_CAN_ERROR_NOT_READY) { ErrDbgPrintf("%s: Peripheral not ready", getInstance(hcan)->CANname.c_str()); }
if(hcan->ErrorCode & HAL_CAN_ERROR_NOT_STARTED) { ErrDbgPrintf("%s: Peripheral not started", getInstance(hcan)->CANname.c_str()); }
if(hcan->ErrorCode & HAL_CAN_ERROR_PARAM) { ErrDbgPrintf("%s: Parameter error", getInstance(hcan)->CANname.c_str()); }
#endif
}
void HAL_CAN_SleepCallback(CAN_HandleTypeDef *hcan)
{
DbgPrintf("%s: sleep callback", getInstance(hcan)->CANname.c_str());
}
/*
void tSTM32_CAN::LoopbackMode()
{
canBus->Init.Mode = CAN_MODE_LOOPBACK;
HAL_CAN_Init(canBus);
ErrDbgPrintf("%s: loopback mode", CANname.c_str());
}
void tSTM32_CAN::NormalMode()
{
canBus->Init.Mode = CAN_MODE_NORMAL;
HAL_CAN_Init(canBus);
ErrDbgPrintf("%s: Normal mode", CANname.c_str());
}
*/
// *****************************************************************************
// Other 'Bridge' functions
//*****************************************************************************
// switch printf() to the debug interface SWO (STM32 ARM debug)
extern "C" int _write(int file, char *ptr, int len) {
int DataIdx;
for (DataIdx = 0; DataIdx < len; DataIdx++) {
ITM_SendChar(*ptr++);
}
return len;
}
uint32_t pow(uint32_t base, uint32_t exp) {
uint32_t result = 1;
while (exp != 0)
{
result *= base;
-- exp;
}
return result;
}