libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
fdcan.h
Go to the documentation of this file.
1/*
2 * This file is part of the libopencm3 project.
3 *
4 * Copyright (C) 2021 Eduard Drusa <ventyl86 at netkosice dot sk>
5 *
6 * This library is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this library. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#pragma once
21
24
25#if defined(STM32G4)
26# include <libopencm3/stm32/g4/fdcan.h>
27#elif defined(STM32H7)
29#endif
30
31/** @addtogroup fdcan_defines
32 * @{
33 */
34
35/** @defgroup fdcan_fifo Named constants for FIFOs
36 * @{
37 */
38#define FDCAN_FIFO0 0
39#define FDCAN_FIFO1 1
40/**@}*/
41
42#define FDCAN_BLOCK_ID(can_base) (((can_base) - CAN1)/(CAN2 - CAN1))
43
44/** @defgroup FDCAN registers file in each FDCAN block. */
45
46#define FDCAN_CREL(can_base) MMIO32(can_base + 0x0000)
47#define FDCAN_ENDN(can_base) MMIO32(can_base + 0x0004)
48#define FDCAN_DBTP(can_base) MMIO32(can_base + 0x000C)
49#define FDCAN_TEST(can_base) MMIO32(can_base + 0x0010)
50#define FDCAN_RWD(can_base) MMIO32(can_base + 0x0014)
51#define FDCAN_CCCR(can_base) MMIO32(can_base + 0x0018)
52#define FDCAN_NBTP(can_base) MMIO32(can_base + 0x001C)
53#define FDCAN_TSCC(can_base) MMIO32(can_base + 0x0020)
54#define FDCAN_TSCV(can_base) MMIO32(can_base + 0x0024)
55#define FDCAN_TOCC(can_base) MMIO32(can_base + 0x0028)
56#define FDCAN_TOCV(can_base) MMIO32(can_base + 0x002C)
57#define FDCAN_ECR(can_base) MMIO32(can_base + 0x0040)
58#define FDCAN_PSR(can_base) MMIO32(can_base + 0x0044)
59#define FDCAN_TDCR(can_base) MMIO32(can_base + 0x0048)
60#define FDCAN_IR(can_base) MMIO32(can_base + 0x0050)
61#define FDCAN_IE(can_base) MMIO32(can_base + 0x0054)
62#define FDCAN_ILS(can_base) MMIO32(can_base + 0x0058)
63#define FDCAN_ILE(can_base) MMIO32(can_base + 0x005C)
64
65
66/** Generic access to Rx FIFO status registers.
67 * @param can_base FDCAN block base address @ref fdcan_block
68 * @param fifo_id ID of FIFO, 0 or 1
69 */
70#define FDCAN_RXFIS(can_base, fifo_id) \
71 MMIO32(can_base + FDCAN_RXFIS_BASE + (FDCAN_RXFI_OFFSET * fifo_id))
72
73#define FDCAN_RXF0S(can_base) FDCAN_RXFIS(can_base, 0)
74#define FDCAN_RXF1S(can_base) FDCAN_RXFIS(can_base, 1)
75
76
77/** Generic access to Rx FIFO acknowledge registers.
78 * @param can_base FDCAN block base address @ref fdcan_block
79 * @param fifo_id ID of FIFO, 0 or 1
80 */
81#define FDCAN_RXFIA(can_base, fifo_id) \
82 MMIO32(can_base + FDCAN_RXFIA_BASE + (FDCAN_RXFI_OFFSET * fifo_id))
83
84#define FDCAN_RXF0A(can_base) FDCAN_RXFIA(can_base, 0)
85#define FDCAN_RXF1A(can_base) FDCAN_RXFIA(can_base, 1)
86
87#define FDCAN_TXBC(can_base) MMIO32(can_base + 0x00C0)
88#define FDCAN_TXFQS(can_base) MMIO32(can_base + 0x00C4)
89
90/** DAY[7:0]: FDCAN core revision date */
91#define FDCAN_CREL_DAY_SHIFT 0
92#define FDCAN_CREL_DAY_MASK 0xFF
93
94/** MON[7:0]: FDCAN core revision month */
95#define FDCAN_CREL_MON_SHIFT 8
96#define FDCAN_CREL_MON_MASK 0xFF
97
98/** YEAR[3:0]: FDCAN core revision year */
99#define FDCAN_CREL_YEAR_SHIFT 16
100#define FDCAN_CREL_YEAR_MASK 0xF
101
102/** SUBSTEP[3:0]: FDCAN core release sub stepping */
103#define FDCAN_CREL_SUBSTEP_SHIFT 20
104#define FDCAN_CREL_SUBSTEP_MASK 0xF
105
106/** STEP[3:0]: FDCAN core release stepping */
107#define FDCAN_CREL_STEP_SHIFT 24
108#define FDCAN_CREL_STEP_MASK 0xF
109
110/** REL[3:0]: FDCAN core release number */
111#define FDCAN_CREL_REL_SHIFT 28
112#define FDCAN_CREL_REL_MASK 0xF
113
114
115/** DSJW[3:0]: Synchronization jump width */
116#define FDCAN_DBTP_DSJW_SHIFT 0
117#define FDCAN_DBTP_DSJW_MASK 0xF
118
119/** DTSEG2[3:0]: Data time segment after sample point */
120#define FDCAN_DBTP_DTSEG2_SHIFT 4
121#define FDCAN_DBTP_DTSEG2_MASK 0xF
122
123/** DTSEG1[4:0]: Data time segment before sample point */
124#define FDCAN_DBTP_DTSEG1_SHIFT 8
125#define FDCAN_DBTP_DTSEG1_MASK 0x1F
126
127/** DBRP[4:0]: Data bit rate prescaler */
128#define FDCAN_DBTP_DBRP_SHIFT 16
129#define FDCAN_DBTP_DBRP_MASK 0x1F
130
131#define FDCAN_DBTP_TDC (1 << 23)
132
133#define FDCAN_TEST_LBCK (1 << 4)
134/** TX[1:0]: Control of transmit pin */
135#define FDCAN_TEST_TX_SHIFT 5
136#define FDCAN_TEST_TX_MASK 0x3
137
138#define FDCAN_TEST_RX (1 << 7)
139
140/** WDC[7:0]: RAM watchdog configuration */
141#define FDCAN_RWD_WDC_SHIFT 0
142#define FDCAN_RWD_WDC_MASK 0xFF
143
144/** WDV[7:0]: RAM watchdog actual value */
145#define FDCAN_RWD_WDV_SHIFT 7
146#define FDCAN_RWD_WDV_MASK 0xFF
147
148/** @defgroup fdcan_cccr FDCAN CC control register bits
149 * @{
150 */
151#define FDCAN_CCCR_INIT (1 << 0)
152#define FDCAN_CCCR_CCE (1 << 1)
153#define FDCAN_CCCR_ASM (1 << 2)
154#define FDCAN_CCCR_CSA (1 << 3)
155#define FDCAN_CCCR_CSR (1 << 4)
156#define FDCAN_CCCR_MON (1 << 5)
157#define FDCAN_CCCR_DAR (1 << 6)
158#define FDCAN_CCCR_TEST (1 << 7)
159#define FDCAN_CCCR_FDOE (1 << 8)
160#define FDCAN_CCCR_BRSE (1 << 9)
161#define FDCAN_CCCR_PXHD (1 << 12)
162#define FDCAN_CCCR_EFBI (1 << 13)
163#define FDCAN_CCCR_TXP (1 << 14)
164#define FDCAN_CCCR_NISO (1 << 15)
165/**@}*/
166
167/** Timeout for FDCAN_CCCR register INIT bit to accept set value.
168 *
169 * This timeout is required because FDCAN uses two different clocks
170 * feeding two different portions of block. There can be slight delay
171 * based on how clocks are set up. While amount of FDCAN_clk /
172 * FDCAN_pclk combinations is high and clock speeds may vary a lot,
173 * following value has been choosen as sane default. You are free to
174 * use any timeout value you want.
175 */
176#define FDCAN_CCCR_INIT_TIMEOUT 0x0000FFFF
177
178/** NTSEG2[6:0]: Nominal timing segment after sample point length */
179#define FDCAN_NBTP_NTSEG2_SHIFT 0
180#define FDCAN_NBTP_NTSEG2_MASK 0x7F
181
182/** NTSEG1[7:0]: Nominal timing segment before sample point length */
183#define FDCAN_NBTP_NTSEG1_SHIFT 8
184#define FDCAN_NBTP_NTSEG1_MASK 0xFF
185
186/** NBRP[8:0]: Norminal timing bit rate prescaler */
187#define FDCAN_NBTP_NBRP_SHIFT 16
188#define FDCAN_NBTP_NBRP_MASK 0x1FF
189
190/** NSJW[6:0]: Norminal timing resynchronization jumb width*/
191#define FDCAN_NBTP_NSJW_SHIFT 25
192#define FDCAN_NBTP_NSJW_MASK 0x7F
193
194/** TSS[1:0]: Timestamp select */
195#define FDCAN_TSCC_TSS_SHIFT 0
196#define FDCAN_TSCC_TSS_MASK 0x3
197
198/** TCP[3:0]: Timestamp counter prescaler */
199#define FDCAN_TSCC_TCP_SHIFT 16
200#define FDCAN_TSCC_TCP_MASK 0xF
201
202
203/** TSC[15:0]: Timestamp counter value */
204#define FDCAN_TSCV_TSC_SHIFT 0
205#define FDCAN_TSCV_TSC_MASK 0xFFFF
206
207#define FDCAN_TOCC_ETOC (1 << 0)
208/** TOS[1:0]: Timeout select */
209#define FDCAN_TOCC_TOS_SHIFT 1
210#define FDCAN_TOCC_TOS_MASK 0x3
211
212/** TOP[15:0]: Timeout period */
213#define FDCAN_TOCC_TOP_SHIFT 16
214#define FDCAN_TOCC_TOP_MASK 0xFFFF
215
216/** TOC[15:0]: Timeout counter */
217#define FDCAN_TOCV_TOC_SHIFT 0
218#define FDCAN_TOCV_TOC_MASK 0xFFFF
219
220/** TEC[7:0]: Transmit error counter */
221#define FDCAN_ECR_TEC_SHIFT 0
222#define FDCAN_ECR_TEC_MASK 0xFF
223
224/** REC[6:0]: Receive error counter */
225#define FDCAN_ECR_REC_SHIFT 8
226#define FDCAN_ECR_REC_MASK 0x7F
227
228#define FDCAN_ECR_RP (1 << 15)
229/** CEL[7:0]: CAN error logging */
230#define FDCAN_ECR_CEL_SHIFT 16
231#define FDCAN_ECR_CEL_MASK 0xFF
232
233
234/** LEC[2:0]: Last error code */
235#define FDCAN_PSR_LEC_SHIFT 0
236#define FDCAN_PSR_LEC_MASK 0x7
237
238/** ACT[1:0]: CAN block activity */
239#define FDCAN_PSR_ACT_SHIFT 3
240#define FDCAN_PSR_ACT_MASK 0x3
241
242#define FDCAN_PSR_EP (1 << 5)
243#define FDCAN_PSR_EW (1 << 6)
244#define FDCAN_PSR_BO (1 << 7)
245/** DLEC[2:0]: Last error code in data section */
246#define FDCAN_PSR_DLEC_SHIFT 8
247#define FDCAN_PSR_DLEC_MASK 0x7
248
249#define FDCAN_PSR_RESI (1 << 11)
250
251/* the what? */
252#define FDCAN_PSR_RBRSRESI1 (1 << 12)
253#define FDCAN_PSR_REDL (1 << 13)
254#define FDCAN_PSR_PXE (1 << 14)
255
256/** TDCV[6:0]: Transmitter delay compensation value */
257#define FDCAN_PSR_TDCV_SHIFT 16
258#define FDCAN_PSR_TDCV_MASK 0x7F
259
260/** TDCF[6:0]: Transmitter delay compensation filter window length */
261#define FDCAN_TDCR_TDCF_SHIFT 0
262#define FDCAN_TDCR_TDCF_MASK 0x7F
263
264/** TDCO[6:0]: Transmitter delay compensation offset */
265#define FDCAN_TDCR_TDCO_SHIFT 8
266#define FDCAN_TDCR_TDCO_MASK 0x7F
267
268/** @defgroup fdcan_ir FDCAN interrupt register flags
269 * @{
270 */
271#define FDCAN_IR_RF0N (1 << 0)
272#define FDCAN_IR_RF0F (1 << 1)
273#define FDCAN_IR_RF0L (1 << 2)
274#define FDCAN_IR_RF1N (1 << 3)
275#define FDCAN_IR_RF1F (1 << 4)
276#define FDCAN_IR_RF1L (1 << 5)
277#define FDCAN_IR_HPM (1 << 6)
278#define FDCAN_IR_TC (1 << 7)
279#define FDCAN_IR_TCF (1 << 8)
280#define FDCAN_IR_TFE (1 << 9)
281#define FDCAN_IR_TEFN (1 << 10)
282#define FDCAN_IR_TEFF (1 << 11)
283#define FDCAN_IR_TEFL (1 << 12)
284#define FDCAN_IR_TSW (1 << 13)
285#define FDCAN_IR_MRAF (1 << 14)
286#define FDCAN_IR_TOO (1 << 15)
287#define FDCAN_IR_ELO (1 << 16)
288#define FDCAN_IR_EP (1 << 17)
289#define FDCAN_IR_EW (1 << 18)
290#define FDCAN_IR_BO (1 << 19)
291#define FDCAN_IR_WDI (1 << 20)
292#define FDCAN_IR_PEA (1 << 21)
293#define FDCAN_IR_PED (1 << 22)
294#define FDCAN_IR_ARA (1 << 23)
295/**@}*/
296
297/** @defgroup fdcan_ie FDCAN interrupt enable flags
298 * @{
299 */
300#define FDCAN_IE_RF0NE (1 << 0)
301#define FDCAN_IE_RF0FE (1 << 1)
302#define FDCAN_IE_RF0LE (1 << 2)
303#define FDCAN_IE_RF1NE (1 << 3)
304#define FDCAN_IE_RF1FE (1 << 4)
305#define FDCAN_IE_RF1LE (1 << 5)
306#define FDCAN_IE_HPME (1 << 6)
307#define FDCAN_IE_TCE (1 << 7)
308#define FDCAN_IE_TCFE (1 << 8)
309#define FDCAN_IE_TFEE (1 << 9)
310#define FDCAN_IE_TEFNE (1 << 10)
311#define FDCAN_IE_TEFFE (1 << 11)
312#define FDCAN_IE_TEFLE (1 << 12)
313#define FDCAN_IE_TSWE (1 << 13)
314#define FDCAN_IE_MRAFE (1 << 14)
315#define FDCAN_IE_TOOE (1 << 15)
316#define FDCAN_IE_ELOE (1 << 16)
317#define FDCAN_IE_EPE (1 << 17)
318#define FDCAN_IE_EWE (1 << 18)
319#define FDCAN_IE_BOE (1 << 19)
320#define FDCAN_IE_WDIE (1 << 20)
321#define FDCAN_IE_PEAE (1 << 21)
322#define FDCAN_IE_PEDE (1 << 22)
323#define FDCAN_IE_ARAE (1 << 23)
324/**@}*/
325
326/** @defgroup fdcan_ils FDCAN_ILS interrupt line select flags
327 * @{
328 */
329#define FDCAN_ILS_RxFIFO0 (1 << 0)
330#define FDCAN_ILS_RxFIFO1 (1 << 1)
331#define FDCAN_ILS_SMSG (1 << 2)
332#define FDCAN_ILS_TFERR (1 << 3)
333#define FDCAN_ILS_MISC (1 << 4)
334#define FDCAN_ILS_BERR (1 << 5)
335#define FDCAN_ILS_PERR (1 << 6)
336/**@}*/
337
338#define FDCAN_ILE_INT0 (1 << 0)
339#define FDCAN_ILE_INT1 (1 << 1)
340
341
342/** EIDM[28:0]: Extended ID mask for filtering */
343#define FDCAN_XIDAM_EIDM_SHIFT 0
344#define FDCAN_XIDAM_EIDM_MASK 0x1FFFFFFF
345
346
347/** BIDX[2:0]: Buffer index */
348#define FDCAN_HPMS_BIDX_SHIFT 0
349#define FDCAN_HPMS_BIDX_MASK 0x7
350
351/** MSI[1:0]: Message storage indicator */
352#define FDCAN_HPMS_MSI_SHIFT 6
353#define FDCAN_HPMS_MSI_MASK 0x3
354
355/** FIDX[4:0]: Filter index */
356#define FDCAN_HPMS_FIDX_SHIFT 8
357#define FDCAN_HPMS_FIDX_MASK 0x1F
358
359#define FDCAN_HPMS_FLS (1 << 15)
360
361/** Fill level of Rx FIFOs */
362#define FDCAN_RXFIFO_FL_SHIFT 0
363
364/** Get index of Rx FIFOs */
365#define FDCAN_RXFIFO_GI_SHIFT 8
366
367/** Put index of Rx FIFOs */
368#define FDCAN_RXFIFO_PI_SHIFT 16
369
370#define FDCAN_RXFIFO_FF (1 << 24)
371#define FDCAN_RXFIFO_RFL (1 << 25)
372
373/** F0FL[3:0]: Fill level of Rx FIFO 0 */
374#define FDCAN_RXF0S_F0FL_SHIFT FDCAN_RXFIFO_FL_SHIFT
375#define FDCAN_RXF0S_F0FL_MASK FDCAN_RXFIFO_FL_MASK
376
377/** F0GI[1:0]: Get index of Rx FIFO 0 */
378#define FDCAN_RXF0S_F0GI_SHIFT FDCAN_RXFIFO_GI_SHIFT
379#define FDCAN_RXF0S_F0GI_MASK FDCAN_RXFIFO_GI_MASK
380
381/** F0PI[1:0]: Put index of Rx FIFO 0 */
382#define FDCAN_RXF0S_F0PI_SHIFT FDCAN_RXFIFO_PI_SHIFT
383#define FDCAN_RXF0S_F0PI_MASK FDCAN_RXFIFO_PI_MASK
384
385#define FDCAN_RXF0S_F0F FDCAN_RXFIFO_FF
386#define FDCAN_RXF0S_RF0L FDCAN_RXFIFO_RFL
387
388/** Rx FIFOs acknowledge index */
389#define FDCAN_RXFIFO_AI_SHIFT 0
390
391/** R0AI[2:0]: Rx FIFO 0 acknowledge index */
392#define FDCAN_RXF0A_R0AI_SHIFT FDCAN_RXFIFO_AI_SHIFT
393#define FDCAN_RXF0A_R0AI_MASK FDCAN_RXFIFO_AI_MASK
394
395/** F1FL[3:1]: Fill level of Rx FIFO 1 */
396#define FDCAN_RXF1S_F1FL_SHIFT FDCAN_RXFIFO_FL_SHIFT
397#define FDCAN_RXF1S_F1FL_MASK FDCAN_RXFIFO_FL_MASK
398
399/** F1GI[1:1]: Get index of Rx FIFO 1 */
400#define FDCAN_RXF1S_F1GI_SHIFT FDCAN_RXFIFO_GI_SHIFT
401#define FDCAN_RXF1S_F1GI_MASK FDCAN_RXFIFO_GI_MASK
402
403/** F1PI[1:1]: Put index of Rx FIFO 1 */
404#define FDCAN_RXF1S_F1PI_SHIFT FDCAN_RXFIFO_PI_SHIFT
405#define FDCAN_RXF1S_F1PI_MASK FDCAN_RXFIFO_PI_MASK
406
407#define FDCAN_RXF1S_F1F FDCAN_RXFIFO_FF
408#define FDCAN_RXF1S_RF1L FDCAN_RXFIFO_RFL
409
410/** R1AI[2:0]: Rx FIFO 1 acknowledge index */
411#define FDCAN_RXF1A_R1AI_SHIFT FDCAN_RXFIFO_AI_SHIFT
412#define FDCAN_RXF1A_R1AI_MASK FDCAN_RXFIFO_AI_MASK
413
414#define FDCAN_TXBC_TFQM (1 << 24)
415
416/** TFFL[2:0]: Tx FIFO free level */
417#define FDCAN_TXFQS_TFFL_SHIFT 0
418
419/** TFGI[1:0]: Tx FIFO get index */
420#define FDCAN_TXFQS_TFGI_SHIFT 8
421
422/** TFQPI[1:0]: Tx FIFO put index */
423#define FDCAN_TXFQS_TFQPI_SHIFT 16
424
425#define FDCAN_TXFQS_TFQF (1 << 21)
426
427/** @defgroup fdcan_txbrp FDCAN_TXBRP Transmit request pending bits
428 * @{
429 */
430#define FDCAN_TXBRP_TRP0 (1 << 0)
431#define FDCAN_TXBRP_TRP1 (1 << 1)
432#define FDCAN_TXBRP_TRP2 (1 << 2)
433/**@}*/
434
435/** @defgroup fdcan_txbar FDCAN_TXBAR Transmit buffer add request bits
436 * @{
437 */
438#define FDCAN_TXBAR_AR0 (1 << 0)
439#define FDCAN_TXBAR_AR1 (1 << 1)
440#define FDCAN_TXBAR_AR2 (1 << 2)
441/**@}*/
442
443/** @defgroup fdcan_txbcr FDCAN_TXBCR Transmit buffer cancel request bits
444 * @{
445 */
446#define FDCAN_TXBCR_CR0 (1 << 0)
447#define FDCAN_TXBCR_CR1 (1 << 1)
448#define FDCAN_TXBCR_CR2 (1 << 2)
449/**@}*/
450
451/** @defgroup fdcan_txbto FDCAN_TXBTO Transmit buffer transfer occured bits
452 * @{
453 */
454#define FDCAN_TXBTO_TO0 (1 << 0)
455#define FDCAN_TXBTO_TO1 (1 << 1)
456#define FDCAN_TXBTO_TO2 (1 << 2)
457/**@}*/
458
459/** @defgroup fdcan_txbcf FDCAN_TXBCF Transmit buffer cancellation finished bits
460 * @{
461 */
462#define FDCAN_TXBCF_CF0 (1 << 0)
463#define FDCAN_TXBCF_CF1 (1 << 1)
464#define FDCAN_TXBCF_CF2 (1 << 2)
465/**@}*/
466
467/** @defgroup fdcan_txbtie FDCAN_TXBTIE Transmit interrupt enable bits
468 *
469 * Each bit enables or disables transmit interrupt for transmit buffer
470 * slot.
471 * @{
472 */
473#define FDCAN_TXBTIE_TIE0 (1 << 0)
474#define FDCAN_TXBTIE_TIE1 (1 << 1)
475#define FDCAN_TXBTIE_TIE2 (1 << 2)
476/**@}*/
477
478/** @defgroup fdcan_txbcie FDCAN_TXBCIE Transmit cancelled interrupt enable bits
479 *
480 * Each bit enables or disables transmit cancelled interrupt for transmit buffer
481 * slot.
482 * @{
483 */
484#define FDCAN_TXBCIE_CFIE0 (1 << 0)
485#define FDCAN_TXBCIE_CFIE1 (1 << 1)
486#define FDCAN_TXBCIE_CFIE2 (1 << 2)
487/**@}*/
488
489/** EFFL[2:0]: Event FIFO fill level*/
490#define FDCAN_TXEFS_EFFL_SHIFT 0
491
492/** EFG[1:0]: Event FIFO get index */
493#define FDCAN_TXEFS_EFGI_SHIFT 8
494
495/** EFPI[1:0]: Event FIFO put index */
496#define FDCAN_TXEFS_EFPI_SHIFT 16
497
498#define FDCAN_TXEFS_EFF (1 << 24)
499#define FDCAN_TXEFS_TEF (1 << 25)
500
501/** EFAI[1:0]: Event FIFO acknowledge index */
502#define FDCAN_TXEFA_EFAI_SHIFT 0
503#define FDCAN_TXEFA_EFAI_MASK 0x3
504
505
506/* --- FD-CAN memory block defines------------------------------------------ */
507
508/** Structure describing standard ID filter.
509 * Standard ID filter is composed of one 32bit value.
510 * This region of memory cannot be accessed in quantities less than 32bits.
511 */
513 /** Aggregate of filter type, filter action and two IDs */
515};
516
517#define FDCAN_SFT_SHIFT 30
518#define FDCAN_SFT_MASK 0x3
519
520/** @defgroup fdcan_sft Standard ID filter match type
521 *
522 * Matching strategy for standard ID filters.
523 * @{
524 */
525/** Filter matches all messages in range from id1 to id2. */
526#define FDCAN_SFT_RANGE 0x0
527
528/** Filter matches messages with id1 or id2 */
529#define FDCAN_SFT_DUAL 0x1
530
531/** Filter matches messages which match id1 after being unmasked
532 * using id2. */
533#define FDCAN_SFT_ID_MASK 0x2
534
535/** Disable this filter. */
536#define FDCAN_SFT_DISABLE 0x3
537/**@}*/
538
539#define FDCAN_SFEC_SHIFT 27
540#define FDCAN_SFEC_MASK 0x7
541
542/** @defgroup fdcan_sfec Standard ID filter action
543 *
544 * Defines possible actions for standard ID filters. All actions except
545 * of @ref FDCAN_SFEC_PRIO cause filter matching to terminate immediately
546 * with desired outcome. FDCAN_SFEC_PRIO sets priority flag for message
547 * and continues processing remaining filters.
548 * @{
549 */
550
551/** Filter is disabled. No matchin occurrs. */
552#define FDCAN_SFEC_DISABLE 0x0
553
554/** Put message into FIFO0 */
555#define FDCAN_SFEC_FIFO0 0x1
556
557/** Put message into FIFO1 */
558#define FDCAN_SFEC_FIFO1 0x2
559
560/** Reject message */
561#define FDCAN_SFEC_REJECT 0x3
562
563/** Treat message as priority message (and continue processing further rules) */
564#define FDCAN_SFEC_PRIO 0x4
565
566/** Treat message as priority and put it into FIFO0 */
567#define FDCAN_SFEC_PRIO_FIFO0 0x5
568
569/** Treat message as priority and put it into FIFO1 */
570#define FDCAN_SFEC_PRIO_FIFO1 0x6
571/**@}*/
572
573/* SFEC = 0x7 is unused */
574
575#define FDCAN_SFID1_SHIFT 16
576#define FDCAN_SFID1_MASK 0x7FF
577
578#define FDCAN_SFID2_SHIFT 0
579#define FDCAN_SFID2_MASK 0x7FF
580
581/** Structure describing extended ID filters.
582 * Extended ID filter is composed of two 32bit values.
583 * This region of memory cannot be accessed in quantities less than 32bits.
584 */
586 /** Aggregate of filter action and extended ID */
587 uint32_t conf_id1;
588 /** Aggregate of filter type and extended ID or mask */
589 uint32_t type_id2;
590};
591
592#define FDCAN_EFEC_SHIFT 29
593#define FDCAN_EFEC_MASK 0x7
594
595/** @defgroup fdcan_efec Extended ID filter action
596 *
597 * These are possible actions, extended filter can have. If filter is
598 * disabled, then no matching is performed. All other actions except of
599 * @ref FDCAN_EFEC_PRIO cause matching to terminate with required outcome.
600 * FDCAN_EFEC_PRIO marks message as priority and continues matching.
601 * @{
602 */
603
604/** Disable this filter. */
605#define FDCAN_EFEC_DISABLE 0x0
606
607/** Put message into FIFO0 */
608#define FDCAN_EFEC_FIFO0 0x1
609
610/** Put message into FIFO1 */
611#define FDCAN_EFEC_FIFO1 0x2
612
613/** Reject message */
614#define FDCAN_EFEC_REJECT 0x3
615
616/** Treat message as priority message (and continue processing further rules) */
617#define FDCAN_EFEC_PRIO 0x4
618
619/** Treat message as priority and put it into FIFO0 */
620#define FDCAN_EFEC_PRIO_FIFO0 0x5
621
622/** Treat message as priority and put it into FIFO1 */
623#define FDCAN_EFEC_PRIO_FIFO1 0x6
624/**@}*/
625
626#define FDCAN_EFID1_SHIFT 0
627#define FDCAN_EFID1_MASK 0x1FFFFFFF
628
629#define FDCAN_EFT_SHIFT 30
630#define FDCAN_EFT_MASK 0x3
631
632/** @defgroup fdcan_eft Extended ID filter match type
633 *
634 * Matching strategy for extended ID filters.
635 * @{
636 */
637/** Filter matches all messages in range from id1 to id2. */
638#define FDCAN_EFT_RANGE 0x0
639
640/** Filter matches messages with id1 or id2 */
641#define FDCAN_EFT_DUAL 0x1
642
643/** Filter matches messages which match id1 after being unmasked
644 * using id2. */
645#define FDCAN_EFT_ID_MASK 0x2
646
647/** Similar to @ref FDCAN_EFT_RANGE except of ignoring global mask
648 * set using @ref FDCAN_XIDAM register.
649 */
650#define FDCAN_EFT_RANGE_NOXIDAM 0x3
651/**@}*/
652
653#define FDCAN_EFID2_SHIFT 0
654#define FDCAN_EFID2_MASK 0x1FFFFFFF
655
656/** Structure describing receive FIFO element.
657 * Receive FIFO element consists of 2 32bit values for header
658 * and 16 32bit values for message payload.
659 * This area of memory can only be accessed in 32bit quantities
660 */
662 /** Aggregate of message identifier and flags. */
664 /** Aggregate of filter match ID, transfer format, DLC and timestamp */
666 /** Message payload data */
667 uint32_t data[64 / sizeof(uint32_t)];
668};
669
670/** Structure describing transmit event element.
671 * Transmit event element consists of 2 32bit values.
672 * This area of memory can only be accessed in 32bit quantities
673 */
675 /** Aggregate of message identifier and flags. */
677
678 /** Aggregate of event ID, transfer format, DLC and timestamp */
680};
681
682/** Structure describing transmit buffer element.
683 * Transmit buffer consists of 2 32bit values for header
684 * and 16 32bit values for message payload.
685 * This area of memory can only be accessed in 32bit quantities
686 */
688 /** Aggregate of message identifier and flags. */
690
691 /** Aggregate of event ID, transfer format and DLC */
693 /** Message payload data */
694 uint32_t data[64 / sizeof(uint32_t)];
695};
696
697/** @defgroup fdcan_fifo_flags FIFO / buffer flags
698 * @{
699 */
700#define FDCAN_FIFO_ESI (1 << 31)
701#define FDCAN_FIFO_XTD (1 << 30)
702#define FDCAN_FIFO_RTR (1 << 29)
703#define FDCAN_FIFO_EFC (1 << 23)
704#define FDCAN_FIFO_FDF (1 << 21)
705#define FDCAN_FIFO_BRS (1 << 20)
706/**@}*/
707
708#define FDCAN_FIFO_EID_SHIFT 0
709#define FDCAN_FIFO_EID_MASK 0x1FFFFFFF
710
711#define FDCAN_FIFO_SID_SHIFT 18
712#define FDCAN_FIFO_SID_MASK 0x7FF
713
714#define FDCAN_FIFO_DLC_SHIFT 16
715#define FDCAN_FIFO_DLC_MASK 0xF
716
717#define FDCAN_FIFO_MM_SHIFT 24
718#define FDCAN_FIFO_MM_MASK 0xFF
719
720#define FDCAN_FIFO_ANMF (1 << 31)
721#define FDCAN_FIFO_FIDX_SHIFT 24
722#define FDCAN_FIFO_FIDX_MASK 0x7F
723
724#define FDCAN_FIFO_RXTS_SHIFT 0
725#define FDCAN_FIFO_RXTS_MASK 0xFFFF
726
727
728/** @defgroup fdcan_error FDCAN error return values
729 * @{
730 */
731
732/** No error. Operation finished successfully */
733#define FDCAN_E_OK 0
734
735/** Value provided was out of range */
736#define FDCAN_E_OUTOFRANGE -1
737
738/** Timeout waiting for FDCAN block to accept INIT bit change */
739#define FDCAN_E_TIMEOUT -2
740
741/** Value provided was invalid (FIFO index, FDCAN block base address, length, etc.) */
742#define FDCAN_E_INVALID -3
743
744/** Device is busy: Transmit buffer is full, unable to queue additional message or device
745 * is outside of INIT mode and cannot perform desired operation. */
746#define FDCAN_E_BUSY -4
747
748/** Receive buffer is empty, unable to read any new message */
749#define FDCAN_E_NOTAVAIL -5
750
751/**@}*/
752
753/**@}*/
754
755
757
758int fdcan_init(uint32_t canport, uint32_t timeout);
759
760void fdcan_set_can(uint32_t canport, bool auto_retry_disable, bool rx_fifo_locked,
761 bool tx_queue_mode, bool silent, uint32_t n_sjw, uint32_t n_ts1, uint32_t n_ts2,
762 uint32_t n_br_presc);
763
764void fdcan_set_fdcan(uint32_t canport, bool brs_enable, bool fd_op_enable,
765 uint32_t f_sjw, uint32_t f_ts1, uint32_t f_ts2, uint32_t f_br_presc);
766
767void fdcan_set_test(uint32_t canport, bool testing, bool loopback);
768
769void fdcan_init_filter(uint32_t canport, uint8_t std_filt, uint8_t ext_filt);
770
771int fdcan_start(uint32_t canport, uint32_t timeout);
772
773int fdcan_get_init_state(uint32_t canport);
774
775void fdcan_set_std_filter(uint32_t canport, uint32_t nr,
776 uint8_t id_list_mode, uint32_t id1, uint32_t id2,
777 uint8_t action);
778
779void fdcan_set_ext_filter(uint32_t canport, uint32_t nr,
780 uint8_t id_list_mode, uint32_t id1, uint32_t id2,
781 uint8_t action);
782
783void fdcan_enable_irq(uint32_t canport, uint32_t irq);
784void fdcan_disable_irq(uint32_t canport, uint32_t irq);
785
786int fdcan_transmit(uint32_t canport, uint32_t id, bool ext, bool rtr,
787 bool fdcan_fmt, bool btr_switch, uint8_t length, const uint8_t *data);
788
789int fdcan_receive(uint32_t canport, uint8_t fifo, bool release, uint32_t *id,
790 bool *ext, bool *rtr, uint8_t *fmi, uint8_t *length,
791 uint8_t *data, uint16_t *timestamp);
792
793void fdcan_release_fifo(uint32_t canport, uint8_t fifo);
794
795bool fdcan_available_tx(uint32_t canport);
796bool fdcan_available_rx(uint32_t canport, uint8_t fifo);
797
798int fdcan_cccr_init_cfg(uint32_t canport, bool set, uint32_t timeout);
799struct fdcan_standard_filter *fdcan_get_flssa_addr(uint32_t canport);
800struct fdcan_extended_filter *fdcan_get_flesa_addr(uint32_t canport);
801
802struct fdcan_rx_fifo_element *fdcan_get_rxfifo_addr(uint32_t canport,
803 unsigned fifo_id, unsigned element_id);
804unsigned fdcan_get_fifo_element_size(uint32_t canport, unsigned fifo_id);
805
806struct fdcan_tx_event_element *fdcan_get_txevt_addr(uint32_t canport);
807struct fdcan_tx_buffer_element *fdcan_get_txbuf_addr(uint32_t canport, unsigned element_id);
808unsigned fdcan_get_txbuf_element_size(uint32_t canport);
809void fdcan_set_fifo_locked_mode(uint32_t canport, bool locked);
810uint32_t fdcan_length_to_dlc(uint8_t length);
811uint8_t fdcan_dlc_to_length(uint32_t dlc);
812
814
#define END_DECLS
Definition: common.h:34
#define BEGIN_DECLS
Definition: common.h:33
struct fdcan_tx_buffer_element * fdcan_get_txbuf_addr(uint32_t canport, unsigned element_id)
Returns a pointer to an TX FIFO element in message RAM.
Definition: fdcan_common.c:178
uint8_t fdcan_dlc_to_length(uint32_t dlc)
Converts DLC value into frame payload length.
Definition: fdcan_common.c:221
int fdcan_cccr_init_cfg(uint32_t canport, bool set, uint32_t timeout)
Routine implementing FDCAN_CCCR's INIT bit manipulation.
Definition: fdcan_common.c:41
struct fdcan_standard_filter * fdcan_get_flssa_addr(uint32_t canport)
Returns standard filter start address in message RAM.
Definition: fdcan_common.c:124
bool fdcan_available_tx(uint32_t canport)
Check if there is free transmit buffer.
Definition: fdcan_common.c:675
struct fdcan_rx_fifo_element * fdcan_get_rxfifo_addr(uint32_t canport, unsigned fifo_id, unsigned element_id)
Returns a pointer to an RX FIFO element in message RAM.
Definition: fdcan_common.c:150
void fdcan_disable_irq(uint32_t canport, uint32_t irq)
Disable IRQ from FDCAN block.
Definition: fdcan_common.c:664
void fdcan_set_std_filter(uint32_t canport, uint32_t nr, uint8_t id_list_mode, uint32_t id1, uint32_t id2, uint8_t action)
Configure filter rule for standard ID frames.
Definition: fdcan_common.c:418
void fdcan_set_test(uint32_t canport, bool testing, bool loopback)
Set FDCAN block testing features.
Definition: fdcan_common.c:374
void fdcan_release_fifo(uint32_t canport, uint8_t fifo)
Release receive oldest FIFO entry.
Definition: fdcan_common.c:628
void fdcan_set_fdcan(uint32_t canport, bool brs_enable, bool fd_op_enable, uint32_t f_sjw, uint32_t f_ts1, uint32_t f_ts2, uint32_t f_br_presc)
Set FDCAN block parameters for FDCAN transmission.
Definition: fdcan_common.c:343
bool fdcan_available_rx(uint32_t canport, uint8_t fifo)
Tell if there is message waiting in receive FIFO.
Definition: fdcan_common.c:687
int fdcan_receive(uint32_t canport, uint8_t fifo, bool release, uint32_t *id, bool *ext, bool *rtr, uint8_t *fmi, uint8_t *length, uint8_t *data, uint16_t *timestamp)
Receive Message from FDCAN FIFO.
Definition: fdcan_common.c:562
void fdcan_enable_irq(uint32_t canport, uint32_t irq)
Enable IRQ from FDCAN block.
Definition: fdcan_common.c:651
int fdcan_get_init_state(uint32_t canport)
Return current FDCAN block operation state.
Definition: fdcan_common.c:396
void fdcan_set_ext_filter(uint32_t canport, uint32_t nr, uint8_t id_list_mode, uint32_t id1, uint32_t id2, uint8_t action)
Configure filter rule for extended ID frames.
Definition: fdcan_common.c:458
struct fdcan_tx_event_element * fdcan_get_txevt_addr(uint32_t canport)
Returns transmit event start address in message RAM.
Definition: fdcan_common.c:165
int fdcan_transmit(uint32_t canport, uint32_t id, bool ext, bool rtr, bool fdcan_fmt, bool btr_switch, uint8_t length, const uint8_t *data)
Transmit Message using FDCAN.
Definition: fdcan_common.c:486
struct fdcan_extended_filter * fdcan_get_flesa_addr(uint32_t canport)
Returns extended filter start address in message RAM.
Definition: fdcan_common.c:136
uint32_t fdcan_length_to_dlc(uint8_t length)
Converts frame length to DLC value.
Definition: fdcan_common.c:197
void fdcan_set_can(uint32_t canport, bool auto_retry_disable, bool rx_fifo_locked, bool tx_queue_mode, bool silent, uint32_t n_sjw, uint32_t n_ts1, uint32_t n_ts2, uint32_t n_br_presc)
Set essential FDCAN block parameters for plain CAN operation.
Definition: fdcan_common.c:296
int fdcan_init(uint32_t canport, uint32_t timeout)
Put FDCAN block into INIT mode for setup.
Definition: fdcan_common.c:260
void fdcan_set_fifo_locked_mode(uint32_t canport, bool locked)
Configure FDCAN FIFO lock mode.
Definition: fdcan.c:460
int fdcan_start(uint32_t canport, uint32_t timeout)
Enable FDCAN operation after FDCAN block has been set up.
Definition: fdcan.c:359
unsigned fdcan_get_fifo_element_size(uint32_t canport, unsigned fifo_id)
Returns actual size of FIFO entry in FIFO for given CAN port and FIFO.
Definition: fdcan.c:72
void fdcan_init_filter(uint32_t canport, uint8_t std_filt, uint8_t ext_filt)
Configure amount of filters and initialize filtering block.
Definition: fdcan.c:300
unsigned fdcan_get_txbuf_element_size(uint32_t canport)
Returns actual size of transmit entry in transmit queue/FIFO for given CAN port.
Definition: fdcan.c:94
Structure describing extended ID filters.
Definition: fdcan.h:585
uint32_t type_id2
Aggregate of filter type and extended ID or mask.
Definition: fdcan.h:589
uint32_t conf_id1
Aggregate of filter action and extended ID.
Definition: fdcan.h:587
Structure describing receive FIFO element.
Definition: fdcan.h:661
uint32_t filt_fmt_dlc_ts
Aggregate of filter match ID, transfer format, DLC and timestamp.
Definition: fdcan.h:665
uint32_t identifier_flags
Aggregate of message identifier and flags.
Definition: fdcan.h:663
uint32_t data[64/sizeof(uint32_t)]
Message payload data.
Definition: fdcan.h:667
Structure describing standard ID filter.
Definition: fdcan.h:512
uint32_t type_id1_conf_id2
Aggregate of filter type, filter action and two IDs
Definition: fdcan.h:514
Structure describing transmit buffer element.
Definition: fdcan.h:687
uint32_t data[64/sizeof(uint32_t)]
Message payload data.
Definition: fdcan.h:694
uint32_t evt_fmt_dlc_res
Aggregate of event ID, transfer format and DLC.
Definition: fdcan.h:692
uint32_t identifier_flags
Aggregate of message identifier and flags.
Definition: fdcan.h:689
Structure describing transmit event element.
Definition: fdcan.h:674
uint32_t evt_fmt_dlc_ts
Aggregate of event ID, transfer format, DLC and timestamp.
Definition: fdcan.h:679
uint32_t identifier_flags
Aggregate of message identifier and flags.
Definition: fdcan.h:676