-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmcan.c
745 lines (665 loc) · 23 KB
/
mcan.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
/*
This code was originally written for the SAMA5D2, but has been modified to work with the SAMC21.
The peripheral is the same (except for Message RAM addressing), but
Atmel's SAMC21 register definition include files have a different and peculiar syntax.
*/
/* ----------------------------------------------------------------------------
* SAM Software Package License
* ----------------------------------------------------------------------------
* Copyright (c) 2015, Atmel Corporation
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
*
* Atmel's name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ----------------------------------------------------------------------------
*/
/** \file
* Implements functions for Controller Area Network with Flexible Data-rate,
* relying on the MCAN peripheral.
*/
/** \addtogroup can_module
*@{*/
/*----------------------------------------------------------------------------
* Headers
*----------------------------------------------------------------------------*/
#include "chip.h"
#include "compiler.h"
#include "mcan.h"
#include "pmc.h"
#include <assert.h>
#include <string.h>
/*---------------------------------------------------------------------------
* Local definitions
*---------------------------------------------------------------------------*/
enum mcan_dlc
{
CAN_DLC_0 = 0,
CAN_DLC_1 = 1,
CAN_DLC_2 = 2,
CAN_DLC_3 = 3,
CAN_DLC_4 = 4,
CAN_DLC_5 = 5,
CAN_DLC_6 = 6,
CAN_DLC_7 = 7,
CAN_DLC_8 = 8,
CAN_DLC_12 = 9,
CAN_DLC_16 = 10,
CAN_DLC_20 = 11,
CAN_DLC_24 = 12,
CAN_DLC_32 = 13,
CAN_DLC_48 = 14,
CAN_DLC_64 = 15
};
/*---------------------------------------------------------------------------
* Local functions
*---------------------------------------------------------------------------*/
/**
* \brief Convert data length to Data Length Code.
* \param len length, in bytes
* \param dlc address where the matching Data Length Code will be written
* \return true if a code matched the provided length, false if this exact
* length is not supported.
*/
static bool get_length_code(uint8_t len, enum mcan_dlc *dlc)
{
assert(dlc);
if (len <= 8) {
*dlc = (enum mcan_dlc)len;
return true;
}
if (len % 4)
return false;
len /= 4;
if (len <= 6) {
*dlc = (enum mcan_dlc)(len + 6);
return true;
}
if (len % 4)
return false;
len /= 4;
if (len > 4)
return false;
*dlc = (enum mcan_dlc)(len + 11);
return true;
}
/**
* \brief Convert Data Length Code to actual data length.
* \param dlc CAN_DLC_xx enum value
* \return Data length, expressed in bytes.
*/
static uint8_t get_data_length(enum mcan_dlc dlc)
{
assert((dlc == CAN_DLC_0 || dlc > CAN_DLC_0) && dlc <= CAN_DLC_64);
if (dlc <= CAN_DLC_8)
return (uint8_t)dlc;
if (dlc <= CAN_DLC_24)
return ((uint8_t)dlc - 6) * 4;
return ((uint8_t)dlc - 11) * 16;
}
/**
* \brief Compute the size of the Message RAM, depending on the application.
* \param set Pointer to a MCAN instance that will be setup accordingly.
* \param cfg MCAN configuration to be considered. Only integer size parameters
* need to be configured. The other parameters can be left blank at this stage.
* \param size address where the required size of the Message RAM will be
* written, expressed in (32-bit) words.
* \return true if successful, false if a parameter is set to an unsupported
* value.
*/
static bool configure_ram(struct mcan_set *set,
const struct mcan_config *cfg, uint32_t *size)
{
if (cfg->array_size_filt_std > 128 || cfg->array_size_filt_ext > 64
|| cfg->fifo_size_rx0 > 64 || cfg->fifo_size_rx1 > 64
|| cfg->array_size_rx > 64 || cfg->fifo_size_tx_evt > 32
|| cfg->array_size_tx > 32 || cfg->fifo_size_tx > 32
|| cfg->array_size_tx + cfg->fifo_size_tx > 32
|| cfg->buf_size_rx_fifo0 > 64 || cfg->buf_size_rx_fifo1 > 64
|| cfg->buf_size_rx > 64 || cfg->buf_size_tx > 64)
return false;
set->ram_filt_std = cfg->msg_ram;
*size = (uint32_t)cfg->array_size_filt_std * MCAN_RAM_FILT_STD_SIZE;
set->ram_filt_ext = cfg->msg_ram + *size;
*size += (uint32_t)cfg->array_size_filt_ext * MCAN_RAM_FILT_EXT_SIZE;
set->ram_fifo_rx0 = cfg->msg_ram + *size;
*size += (uint32_t)cfg->fifo_size_rx0 * (MCAN_RAM_BUF_HDR_SIZE
+ cfg->buf_size_rx_fifo0 / 4);
set->ram_fifo_rx1 = cfg->msg_ram + *size;
*size += (uint32_t)cfg->fifo_size_rx1 * (MCAN_RAM_BUF_HDR_SIZE
+ cfg->buf_size_rx_fifo1 / 4);
set->ram_array_rx = cfg->msg_ram + *size;
*size += (uint32_t)cfg->array_size_rx * (MCAN_RAM_BUF_HDR_SIZE
+ cfg->buf_size_rx / 4);
set->ram_fifo_tx_evt = cfg->msg_ram + *size;
*size += (uint32_t)cfg->fifo_size_tx_evt * MCAN_RAM_TX_EVT_SIZE;
set->ram_array_tx = cfg->msg_ram + *size;
*size += (uint32_t)cfg->array_size_tx * (MCAN_RAM_BUF_HDR_SIZE
+ cfg->buf_size_tx / 4);
*size += (uint32_t)cfg->fifo_size_tx * (MCAN_RAM_BUF_HDR_SIZE
+ cfg->buf_size_tx / 4);
return true;
}
/*---------------------------------------------------------------------------
* Exported Functions
*---------------------------------------------------------------------------*/
bool mcan_configure_msg_ram(const struct mcan_config *cfg, uint32_t *size)
{
assert(cfg);
assert(size);
struct mcan_set tmp_set = { .cfg = { 0 } };
return configure_ram(&tmp_set, cfg, size);
}
bool mcan_initialize(struct mcan_set *set, const struct mcan_config *cfg)
{
assert(set);
assert(cfg);
assert(cfg->regs);
assert(cfg->msg_ram);
Can *mcan = cfg->regs;
uint32_t *element = NULL, *elem_end = NULL;
uint32_t freq, regVal32;
enum mcan_dlc dlc;
memset(set, 0, sizeof(*set));
if (!configure_ram(set, cfg, ®Val32))
return false;
set->cfg = *cfg;
/* SAMC21 does not set the MSB of the Message RAM Base Address */
/* Reset the CC Control Register */
mcan->CCCR.reg = 0 | CAN_CCCR_INIT;
mcan_disable(set);
mcan_reconfigure(set);
/* Global Filter Configuration: Reject remote frames, reject non-matching frames */
mcan->GFC.reg = CAN_GFC_RRFE | CAN_GFC_RRFS
| CAN_GFC_ANFE(2) | CAN_GFC_ANFS(2);
/* Extended ID Filter AND mask */
mcan->XIDAM.reg = 0x1FFFFFFF;
/* Interrupt configuration - leave initialization with all interrupts off
* Disable all interrupts */
mcan->IE.reg = 0;
mcan->TXBTIE.reg = 0x00000000;
/* All interrupts directed to Line 0 */
mcan->ILS.reg = 0x00000000;
/* Disable both interrupt LINE 0 & LINE 1 */
mcan->ILE.reg = 0x00;
/* Clear all interrupt flags */
mcan->IR.reg = 0xFFCFFFFF;
/* Configure CAN bit timing */
if (cfg->bit_rate == 0
|| cfg->quanta_before_sp < 3 || cfg->quanta_before_sp > 257
|| cfg->quanta_after_sp < 1 || cfg->quanta_after_sp > 128
|| cfg->quanta_sync_jump < 1 || cfg->quanta_sync_jump > 128)
return false;
/* Retrieve the frequency of the CAN core clock i.e. the Generated Clock */
freq = pmc_get_gck_clock(cfg->id);
/* Compute the Nominal Baud Rate Prescaler */
regVal32 = ROUND_INT_DIV(freq, cfg->bit_rate
* (cfg->quanta_before_sp + cfg->quanta_after_sp));
if (regVal32 < 1 || regVal32 > 512)
return false;
/* Apply bit timing configuration */
mcan->NBTP.reg = CAN_NBTP_NBRP(regVal32 - 1)
| CAN_NBTP_NTSEG1(cfg->quanta_before_sp - 1 - 1)
| CAN_NBTP_NTSEG2(cfg->quanta_after_sp - 1)
| CAN_NBTP_NSJW(cfg->quanta_sync_jump - 1);
/* Configure fast CAN FD bit timing */
if (cfg->bit_rate_fd < cfg->bit_rate
|| cfg->quanta_before_sp_fd < 3 || cfg->quanta_before_sp_fd > 33
|| cfg->quanta_after_sp_fd < 1 || cfg->quanta_after_sp_fd > 16
|| cfg->quanta_sync_jump_fd < 1 || cfg->quanta_sync_jump_fd > 8)
return false;
/* Compute the Fast Baud Rate Prescaler */
regVal32 = ROUND_INT_DIV(freq, cfg->bit_rate_fd
* (cfg->quanta_before_sp_fd + cfg->quanta_after_sp_fd));
if (regVal32 < 1 || regVal32 > 32)
return false;
/* Apply bit timing configuration */
mcan->DBTP.reg = CAN_DBTP_DBRP(regVal32 - 1)
| CAN_DBTP_DTSEG1(cfg->quanta_before_sp_fd - 1 - 1)
| CAN_DBTP_DTSEG2(cfg->quanta_after_sp_fd - 1)
| CAN_DBTP_DSJW(cfg->quanta_sync_jump_fd - 1);
/* Configure Message RAM starting addresses and element count */
/* 11-bit Message ID Rx Filters */
mcan->SIDFC.reg =
CAN_SIDFC_FLSSA((uint32_t)set->ram_filt_std)
| CAN_SIDFC_LSS(cfg->array_size_filt_std);
/* 29-bit Message ID Rx Filters */
mcan->XIDFC.reg =
CAN_XIDFC_FLESA((uint32_t)set->ram_filt_ext)
| CAN_XIDFC_LSE(cfg->array_size_filt_ext);
/* Rx FIFO 0 */
mcan->RXF0C.reg =
CAN_RXF0C_F0SA((uint32_t)set->ram_fifo_rx0)
| CAN_RXF0C_F0S(cfg->fifo_size_rx0)
| CAN_RXF0C_F0WM(0)
| 0; /* clear MCAN_RXF0C_F0OM */
/* Rx FIFO 1 */
mcan->RXF1C.reg =
CAN_RXF1C_F1SA((uint32_t)set->ram_fifo_rx1)
| CAN_RXF1C_F1S(cfg->fifo_size_rx1)
| CAN_RXF1C_F1WM(0)
| 0; /* clear MCAN_RXF1C_F1OM */
/* Dedicated Rx Buffers
* Note: the HW does not know (and does not care about) how many
* dedicated Rx Buffers are used by the application. */
mcan->RXBC.reg =
CAN_RXBC_RBSA((uint32_t)set->ram_array_rx);
/* Tx Event FIFO */
mcan->TXEFC.reg =
CAN_TXEFC_EFSA((uint32_t)set->ram_fifo_tx_evt)
| CAN_TXEFC_EFS(cfg->fifo_size_tx_evt)
| CAN_TXEFC_EFWM(0);
/* Tx Buffers */
mcan->TXBC.reg =
CAN_TXBC_TBSA((uint32_t)set->ram_array_tx)
| CAN_TXBC_NDTB(cfg->array_size_tx)
| CAN_TXBC_TFQS(cfg->fifo_size_tx)
| 0; /* clear MCAN_TXBC_TFQM */
/* Configure the size of data fields in Rx and Tx Buffer Elements */
if (!get_length_code(cfg->buf_size_rx_fifo0, &dlc))
return false;
regVal32 = CAN_RXESC_F0DS(dlc < CAN_DLC_8 ? 0 : dlc - CAN_DLC_8);
if (!get_length_code(cfg->buf_size_rx_fifo1, &dlc))
return false;
regVal32 |= CAN_RXESC_F1DS(dlc < CAN_DLC_8 ? 0 : dlc - CAN_DLC_8);
if (!get_length_code(cfg->buf_size_rx, &dlc))
return false;
regVal32 |= CAN_RXESC_RBDS(dlc < CAN_DLC_8 ? 0 : dlc - CAN_DLC_8);
mcan->RXESC.reg = regVal32;
if (!get_length_code(cfg->buf_size_tx, &dlc))
return false;
mcan->TXESC.reg =
CAN_TXESC_TBDS(dlc < CAN_DLC_8 ? 0 : dlc - CAN_DLC_8);
/* Configure Message ID Filters
* ...Disable all standard filters */
for (element = set->ram_filt_std, elem_end = set->ram_filt_std
+ (uint32_t)cfg->array_size_filt_std * MCAN_RAM_FILT_STD_SIZE;
element < elem_end;
element += MCAN_RAM_FILT_STD_SIZE)
element[0] = MCAN_RAM_FILT_SFEC_DIS;
/* ...Disable all extended filters */
for (element = set->ram_filt_ext, elem_end = set->ram_filt_ext
+ (uint32_t)cfg->array_size_filt_ext * MCAN_RAM_FILT_EXT_SIZE;
element < elem_end;
element += MCAN_RAM_FILT_EXT_SIZE)
element[0] = MCAN_RAM_FILT_EFEC_DIS;
mcan->NDAT1.reg = 0xFFFFFFFF; /* clear new (rx) data flags */
mcan->NDAT2.reg = 0xFFFFFFFF; /* clear new (rx) data flags */
regVal32 = mcan->CCCR.reg & ~(CAN_CCCR_BRSE | CAN_CCCR_FDOE);
mcan->CCCR.reg = regVal32 | CAN_CCCR_PXHD;
return true;
}
void mcan_reconfigure(struct mcan_set *set)
{
Can *mcan = set->cfg.regs;
uint32_t regVal32;
regVal32 = mcan->CCCR.reg & ~CAN_CCCR_CCE;
assert((regVal32 & CAN_CCCR_INIT) == CAN_CCCR_INIT);
/* Enable writing to configuration registers */
mcan->CCCR.reg = regVal32 | CAN_CCCR_CCE;
}
void mcan_set_mode(struct mcan_set *set, enum mcan_can_mode mode)
{
Can *mcan = set->cfg.regs;
uint32_t regVal32;
regVal32 = mcan->CCCR.reg & ~(CAN_CCCR_BRSE | CAN_CCCR_FDOE);
switch (mode) {
case MCAN_MODE_CAN:
regVal32 |= 0;
break;
case MCAN_MODE_EXT_LEN_CONST_RATE:
regVal32 |= CAN_CCCR_FDOE;
break;
case MCAN_MODE_EXT_LEN_DUAL_RATE:
regVal32 |= CAN_CCCR_BRSE | CAN_CCCR_FDOE;
break;
default:
return;
}
mcan->CCCR.reg = regVal32;
}
enum mcan_can_mode mcan_get_mode(const struct mcan_set *set)
{
const uint32_t cccr = set->cfg.regs->CCCR.reg;
if (!(cccr & CAN_CCCR_FDOE))
return MCAN_MODE_CAN;
if (!(cccr & CAN_CCCR_BRSE))
return MCAN_MODE_EXT_LEN_CONST_RATE;
return MCAN_MODE_EXT_LEN_DUAL_RATE;
}
void mcan_init_loopback(struct mcan_set *set)
{
Can *mcan = set->cfg.regs;
mcan->CCCR.reg |= CAN_CCCR_TEST;
#if 0
mcan->CCCR.reg |= CAN_CCCR_MON; /* for internal loop back */
#endif
mcan->TEST.reg |= CAN_TEST_LBCK;
}
void mcan_set_tx_queue_mode(struct mcan_set *set)
{
Can *mcan = set->cfg.regs;
mcan->TXBC.reg |= CAN_TXBC_TFQM;
}
void mcan_enable(struct mcan_set *set)
{
uint32_t index, val;
/* Depending on bus condition, the HW may switch back to the
* Initialization state, by itself. Therefore, upon timeout, return.
* [Using an arbitrary timeout criterion.] */
for (index = 0; index < 1024; index++) {
val = set->cfg.regs->CCCR.reg;
if (!(val & CAN_CCCR_INIT))
break;
if (index == 0)
set->cfg.regs->CCCR.reg = (val & ~CAN_CCCR_INIT);
}
}
void mcan_disable(struct mcan_set *set)
{
uint32_t val;
bool initial;
for (initial = true; true; initial = false) {
val = set->cfg.regs->CCCR.reg;
if ((val & CAN_CCCR_INIT) == CAN_CCCR_INIT)
break;
if (initial)
set->cfg.regs->CCCR.reg = (val & ~CAN_CCCR_INIT)
| CAN_CCCR_INIT;
}
}
void mcan_loopback_on(struct mcan_set *set)
{
Can *mcan = set->cfg.regs;
mcan->TEST.reg |= CAN_TEST_LBCK;
}
void mcan_loopback_off(struct mcan_set *set)
{
Can *mcan = set->cfg.regs;
mcan->TEST.reg &= ~CAN_TEST_LBCK;
}
void mcan_enable_rx_array_flag(struct mcan_set *set, uint8_t line)
{
assert(line == 0 || line == 1);
Can *mcan = set->cfg.regs;
if (line) {
mcan->ILS.reg |= CAN_ILS_DRXL;
mcan->ILE.reg |= CAN_ILE_EINT1;
} else {
mcan->ILS.reg &= ~CAN_ILS_DRXL;
mcan->ILE.reg |= CAN_ILE_EINT0;
}
mcan->IR.reg = CAN_IR_DRX; /* clear previous flag */
mcan->IE.reg |= CAN_IE_DRXE; /* enable it */
}
uint8_t * mcan_prepare_tx_buffer(struct mcan_set *set, uint8_t buf_idx,
uint32_t id, uint8_t len)
{
assert(buf_idx < set->cfg.array_size_tx);
assert(len <= set->cfg.buf_size_tx);
Can *mcan = set->cfg.regs;
uint32_t *pThisTxBuf = 0;
uint32_t val;
const enum mcan_can_mode mode = mcan_get_mode(set);
enum mcan_dlc dlc;
if (buf_idx >= set->cfg.array_size_tx)
return NULL;
if (!get_length_code(len, &dlc))
dlc = CAN_DLC_0;
pThisTxBuf = set->ram_array_tx + buf_idx
* (MCAN_RAM_BUF_HDR_SIZE + set->cfg.buf_size_tx / 4);
if (mcan_is_extended_id(id))
*pThisTxBuf++ = MCAN_RAM_BUF_XTD | MCAN_RAM_BUF_ID_XTD(id);
else
*pThisTxBuf++ = MCAN_RAM_BUF_ID_STD(id);
val = MCAN_RAM_BUF_MM(0) | MCAN_RAM_BUF_DLC((uint32_t)dlc);
if (mode == MCAN_MODE_EXT_LEN_CONST_RATE)
val |= MCAN_RAM_BUF_FDF;
else if (mode == MCAN_MODE_EXT_LEN_DUAL_RATE)
val |= MCAN_RAM_BUF_FDF | MCAN_RAM_BUF_BRS;
*pThisTxBuf++ = val;
/* enable transmit from buffer to set TC interrupt bit in IR,
* but interrupt will not happen unless TC interrupt is enabled */
mcan->TXBTIE.reg = (1 << buf_idx);
return (uint8_t *)pThisTxBuf; /* now it points to the data field */
}
void mcan_send_tx_buffer(struct mcan_set *set, uint8_t buf_idx)
{
Can *mcan = set->cfg.regs;
if (buf_idx < set->cfg.array_size_tx)
mcan->TXBAR.reg = (1 << buf_idx);
}
uint8_t mcan_enqueue_outgoing_msg(struct mcan_set *set, uint32_t id,
uint8_t len, const uint8_t *data)
{
assert(len <= set->cfg.buf_size_tx);
Can *mcan = set->cfg.regs;
uint32_t val;
uint32_t *pThisTxBuf = 0;
const enum mcan_can_mode mode = mcan_get_mode(set);
enum mcan_dlc dlc;
uint8_t putIdx = 255;
if (!get_length_code(len, &dlc))
dlc = CAN_DLC_0;
/* Configured for FifoQ and FifoQ not full? */
if (set->cfg.fifo_size_tx == 0 || (mcan->TXFQS.reg & CAN_TXFQS_TFQF))
return putIdx;
putIdx = (uint8_t)((mcan->TXFQS.reg & CAN_TXFQS_TFQPI_Msk)
>> CAN_TXFQS_TFQPI_Pos);
pThisTxBuf = set->ram_array_tx + (uint32_t)
putIdx * (MCAN_RAM_BUF_HDR_SIZE + set->cfg.buf_size_tx / 4);
if (mcan_is_extended_id(id))
*pThisTxBuf++ = MCAN_RAM_BUF_XTD | MCAN_RAM_BUF_ID_XTD(id);
else
*pThisTxBuf++ = MCAN_RAM_BUF_ID_STD(id);
val = MCAN_RAM_BUF_MM(0) | MCAN_RAM_BUF_DLC((uint32_t)dlc);
if (mode == MCAN_MODE_EXT_LEN_CONST_RATE)
val |= MCAN_RAM_BUF_FDF;
else if (mode == MCAN_MODE_EXT_LEN_DUAL_RATE)
val |= MCAN_RAM_BUF_FDF | MCAN_RAM_BUF_BRS;
*pThisTxBuf++ = val;
memcpy(pThisTxBuf, data, len);
/* enable transmit from buffer to set TC interrupt bit in IR,
* but interrupt will not happen unless TC interrupt is enabled
*/
mcan->TXBTIE.reg = (1 << putIdx);
/* request to send */
mcan->TXBAR.reg = (1 << putIdx);
return putIdx;
}
bool mcan_is_buffer_sent(const struct mcan_set *set, uint8_t buf_idx)
{
Can *mcan = set->cfg.regs;
return mcan->TXBTO.reg & (1 << buf_idx) ? true : false;
}
void mcan_filter_single_id(struct mcan_set *set,
uint8_t buf_idx, uint8_t filter, uint32_t id)
{
assert(buf_idx < set->cfg.array_size_rx);
assert(id & CAN_EXT_MSG_ID ? filter < set->cfg.array_size_filt_ext
: filter < set->cfg.array_size_filt_std);
assert(id & CAN_EXT_MSG_ID ? (id & ~CAN_EXT_MSG_ID) <= 0x1fffffff :
id <= 0x7ff);
uint32_t *pThisRxFilt = 0;
if (buf_idx >= set->cfg.array_size_rx)
return;
if (mcan_is_extended_id(id)) {
pThisRxFilt = set->ram_filt_ext + filter
* MCAN_RAM_FILT_EXT_SIZE;
*pThisRxFilt++ = MCAN_RAM_FILT_EFEC_BUF
| MCAN_RAM_FILT_EFID1(id);
*pThisRxFilt = MCAN_RAM_FILT_EFID2_BUF
| MCAN_RAM_FILT_EFID2_BUF_IDX(buf_idx);
} else {
pThisRxFilt = set->ram_filt_std + filter
* MCAN_RAM_FILT_STD_SIZE;
*pThisRxFilt = MCAN_RAM_FILT_SFEC_BUF
| MCAN_RAM_FILT_SFID1(id)
| MCAN_RAM_FILT_SFID2_BUF
| MCAN_RAM_FILT_SFID2_BUF_IDX(buf_idx);
}
}
void mcan_filter_id_mask(struct mcan_set *set, uint8_t fifo, uint8_t filter,
uint32_t id, uint32_t mask)
{
assert(fifo == 0 || fifo == 1);
assert(id & CAN_EXT_MSG_ID ? filter < set->cfg.array_size_filt_ext
: filter < set->cfg.array_size_filt_std);
assert(id & CAN_EXT_MSG_ID ? (id & ~CAN_EXT_MSG_ID) <= 0x1fffffff :
id <= 0x7ff);
assert(id & CAN_EXT_MSG_ID ? mask <= 0x1fffffff : mask <= 0x7ff);
uint32_t *pThisRxFilt = 0;
uint32_t val;
if (mcan_is_extended_id(id)) {
pThisRxFilt = set->ram_filt_ext + filter
* MCAN_RAM_FILT_EXT_SIZE;
*pThisRxFilt++ = (fifo ? MCAN_RAM_FILT_EFEC_FIFO1
: MCAN_RAM_FILT_EFEC_FIFO0) | MCAN_RAM_FILT_EFID1(id);
*pThisRxFilt = MCAN_RAM_FILT_EFT_CLASSIC
| MCAN_RAM_FILT_EFID2(mask);
} else {
pThisRxFilt = set->ram_filt_std + filter
* MCAN_RAM_FILT_STD_SIZE;
val = MCAN_RAM_FILT_SFT_CLASSIC
| MCAN_RAM_FILT_SFID1(id)
| MCAN_RAM_FILT_SFID2(mask);
*pThisRxFilt = (fifo ? MCAN_RAM_FILT_SFEC_FIFO1
: MCAN_RAM_FILT_SFEC_FIFO0) | val;
}
}
bool mcan_rx_buffer_data(const struct mcan_set *set, uint8_t buf_idx)
{
Can *mcan = set->cfg.regs;
if (buf_idx < 32)
return mcan->NDAT1.reg & (1 << buf_idx) ? true : false;
else if (buf_idx < 64)
return mcan->NDAT2.reg & (1 << (buf_idx - 32)) ? true : false;
else
return false;
}
void mcan_read_rx_buffer(struct mcan_set *set, uint8_t buf_idx,
struct mcan_msg_info *msg)
{
assert(buf_idx < set->cfg.array_size_rx);
Can *mcan = set->cfg.regs;
const uint32_t *pThisRxBuf = 0;
uint32_t tempRy; /* temp copy of RX buffer word */
uint8_t len;
if (buf_idx >= set->cfg.array_size_rx) {
msg->id = 0;
msg->timestamp = 0;
msg->full_len = 0;
msg->data_len = 0;
return;
}
pThisRxBuf = set->ram_array_rx + (buf_idx
* (MCAN_RAM_BUF_HDR_SIZE + set->cfg.buf_size_rx / 4));
tempRy = *pThisRxBuf++; /* word R0 contains ID */
if (tempRy & MCAN_RAM_BUF_XTD)
msg->id = CAN_EXT_MSG_ID | (tempRy & MCAN_RAM_BUF_ID_XTD_Msk)
>> MCAN_RAM_BUF_ID_XTD_Pos;
else
msg->id = (tempRy & MCAN_RAM_BUF_ID_STD_Msk)
>> MCAN_RAM_BUF_ID_STD_Pos;
tempRy = *pThisRxBuf++; /* word R1 contains DLC & time stamp */
msg->full_len = len = get_data_length((enum mcan_dlc)
((tempRy & MCAN_RAM_BUF_DLC_Msk) >> MCAN_RAM_BUF_DLC_Pos));
msg->timestamp = (tempRy & MCAN_RAM_BUF_RXTS_Msk)
>> MCAN_RAM_BUF_RXTS_Pos;
if (msg->data) {
/* copy the data from the Rx Buffer Element to the
* application-owned buffer */
if (len > set->cfg.buf_size_rx)
len = set->cfg.buf_size_rx;
if (len > msg->data_len)
len = msg->data_len;
memcpy(msg->data, pThisRxBuf, len);
msg->data_len = len;
}
else
msg->data_len = 0;
/* clear the new data flag for the buffer */
if (buf_idx < 32)
mcan->NDAT1.reg = (1 << buf_idx);
else
mcan->NDAT2.reg = (1 << (buf_idx - 32));
}
uint8_t mcan_dequeue_received_msg(struct mcan_set *set, uint8_t fifo,
struct mcan_msg_info *msg)
{
assert(fifo == 0 || fifo == 1);
Can *mcan = set->cfg.regs;
uint32_t *pThisRxBuf = 0;
uint32_t tempRy; /* temp copy of RX buffer word */
uint8_t buf_elem_data_size, len;
uint32_t *fifo_ack_reg;
uint32_t get_index;
uint8_t fill_level = 0; /* default: fifo empty */
if (fifo) {
get_index = (mcan->RXF1S.reg & CAN_RXF1S_F1GI_Msk) >>
CAN_RXF1S_F1GI_Pos;
fill_level = (uint8_t)((mcan->RXF1S.reg & CAN_RXF1S_F1FL_Msk)
>> CAN_RXF1S_F1FL_Pos);
pThisRxBuf = set->ram_fifo_rx1;
buf_elem_data_size = set->cfg.buf_size_rx_fifo1;
fifo_ack_reg = (uint32_t *) & mcan->RXF1A;
} else {
get_index = (mcan->RXF0S.reg & CAN_RXF0S_F0GI_Msk)
>> CAN_RXF0S_F0GI_Pos;
fill_level = (uint8_t)((mcan->RXF0S.reg & CAN_RXF0S_F0FL_Msk)
>> CAN_RXF0S_F0FL_Pos);
pThisRxBuf = set->ram_fifo_rx0;
buf_elem_data_size = set->cfg.buf_size_rx_fifo0;
fifo_ack_reg = (uint32_t *) & mcan->RXF0A.reg;
}
if (fill_level == 0)
return 0;
pThisRxBuf += get_index * (MCAN_RAM_BUF_HDR_SIZE + buf_elem_data_size
/ 4);
tempRy = *pThisRxBuf++; /* word R0 contains ID */
if (tempRy & MCAN_RAM_BUF_XTD)
msg->id = CAN_EXT_MSG_ID | (tempRy & MCAN_RAM_BUF_ID_XTD_Msk)
>> MCAN_RAM_BUF_ID_XTD_Pos;
else
msg->id = (tempRy & MCAN_RAM_BUF_ID_STD_Msk)
>> MCAN_RAM_BUF_ID_STD_Pos;
tempRy = *pThisRxBuf++; /* word R1 contains DLC & timestamps */
msg->full_len = len = get_data_length((enum mcan_dlc)
((tempRy & MCAN_RAM_BUF_DLC_Msk) >> MCAN_RAM_BUF_DLC_Pos));
msg->timestamp = (tempRy & MCAN_RAM_BUF_RXTS_Msk)
>> MCAN_RAM_BUF_RXTS_Pos;
if (msg->data) {
/* copy the data from the Rx Buffer Element to the
* application-owned buffer */
if (len > buf_elem_data_size)
len = buf_elem_data_size;
if (len > msg->data_len)
len = msg->data_len;
memcpy(msg->data, pThisRxBuf, len);
msg->data_len = len;
}
else
msg->data_len = 0;
/* acknowledge reading the fifo entry */
*fifo_ack_reg = get_index;
/* return entries remaining in FIFO */
return (fill_level);
}
/**@}*/