libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
can.h
Go to the documentation of this file.
1/**
2 * @brief CAN definitions for the Qorvo PAC55xx series of microcontrollers.
3 *
4 * @addtogroup PAC55xx_can CAN
5 * @ingroup PAC55xx_defines
6 * @author Kevin Stefanik <kevin@allocor.tech>
7 * LGPL License Terms @ref lgpl_license
8 * @date February 13, 2020
9 *
10 * Definitions in this file come from the PAC55XX Family User Guide Rev 1.23
11 * by Active-Semi dated November 19, 2019.
12 *
13 * Note: all memory-mapped writes must be performed using 32-bit registers.
14 * Any 8-bit memory-mapped registers below may only be used to read.
15 */
16/*
17 * This file is part of the libopencm3 project.
18 *
19 * Copyright (C) 2020 Kevin Stefanik <kevin@allocor.tech>
20 *
21 * This library is free software: you can redistribute it and/or modify
22 * it under the terms of the GNU Lesser General Public License as published by
23 * the Free Software Foundation, either version 3 of the License, or
24 * (at your option) any later version.
25 *
26 * This library is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU Lesser General Public License for more details.
30 *
31 * You should have received a copy of the GNU Lesser General Public License
32 * along with this library. If not, see <http://www.gnu.org/licenses/>.
33 */
34#ifndef LIBOPENCM3_PAC55XX_CAN_H_
35#define LIBOPENCM3_PAC55XX_CAN_H_
36
39
40/**@{*/
41
42/**
43 * @defgroup can_isr_sr_cmr_mr CAN ISR/SR/CMR/MR Registers
44 * @{*/
45/** This is the 32-bit memory mapped read/write accessor for:
46 * - ISR - bits 31:24 - Interrupt Status/ACK Register RW, default 00h
47 * - SR - bits 23:16 - Status Register RO, default 00h
48 * - CMR - bits 15:8 - Command RW, default 00h
49 * - MR - bits 7:0 - Mode RW, default 04h
50 * When writing, be sure to use CAN_ISR_SR_CMR_MR_SET and CAN_ISR_SR_CMR_MR_CLEAR
51 * so as to avoid inadvertently Acknowledging an ISR bit. Writing '1' to one
52 * of the ISR bits when it is triggered/set will ACK/clear the bit.
53 */
54#define CAN_ISR_SR_CMR_MR(can_base) MMIO32((can_base) + 0x0000)
55#define CAN_ISR_SR_CMR_MR_SET(can_base, bits) (CAN_ISR_SR_CMR_MR(can_base) = \
56 (CAN_ISR_SR_CMR_MR(can_base) & 0x00FFFFFF) | (bits))
57#define CAN_ISR_SR_CMR_MR_CLEAR(can_base, bits) (CAN_ISR_SR_CMR_MR(can_base) = \
58 (CAN_ISR_SR_CMR_MR(can_base) & 0x00FFFFFF) & ~(bits))
59/**@}*/
60
61/**
62 * @defgroup can_btr01_rmc_imr CAN BTR1/BTR0/RMC/IMR Registers
63 * @{*/
64/** This is the 32-bit memory mapped read/write accessor for:
65 * - BTR1 - bits 31:24 - Bus Timing 1 Register RW, default 00h
66 * - BTR0 - bits 23:16 - Bus Timing 0 Register RW, default 00h
67 * - RMC - bits 15:8 - Receive Message Counter RO, default 00h
68 * - IMR - bits 7:0 - Interrupt Mask Register RW, default 00h
69 */
70#define CAN_BTR1_BTR0_RMC_IMR(can_base) MMIO32((can_base) + 0x0004)
71/**@}*/
72
73/** CAN Transmit Buffer Register RW, default 00000000h */
74#define CAN_TXBUF(can_base) MMIO32((can_base) + 0x0008)
75/** CAN Receive Buffer Register RO, default 00000000h */
76#define CAN_RXBUF(can_base) MMIO32((can_base) + 0x000C)
77/** CAN Acceptance Code Register RW, default 00000000h */
78#define CAN_ACR(can_base) MMIO32((can_base) + 0x0010)
79/** CAN Acceptance Mask Register RW, default 00000000h */
80#define CAN_AMR(can_base) MMIO32((can_base) + 0x0014)
81
82/**
83 * @defgroup can_alc_txrxerr_ecc CAN ALC/TXERR/RXERR/ECC Registers
84 * @{*/
85#define CAN_ALC_TXERR_RXERR_ECC(can_base) MMIO32((can_base) + 0x0018)
86/** CAN Error Code Capture Register RO, default 00h */
87#define CAN_ECC(can_base) (CAN_ALC_TXERR_RXERR_ECC(can_base) & 0xFF)
88/** CAN RX Error Counter Register RO, default 00h */
89#define CAN_RXERR(can_base) ((CAN_ALC_TXERR_RXERR_ECC(can_base) >> 8) & 0xFF)
90/** CAN TX Error Counter Register RO, default 00h */
91#define CAN_TXERR(can_base) ((CAN_ALC_TXERR_RXERR_ECC(can_base) >> 16) & 0xFF)
92/** CAN Arbitration Lost Code Capture Register RO, default 00h */
93#define CAN_ALC(can_base) ((CAN_ALC_TXERR_RXERR_ECC(can_base) >> 24) & 0xFF)
94/**@}*/
95
96/** CAN Mode Register bit definitions. This register controls high level modes of the CAN peripheral.
97 * @defgroup can_mr_bits CAN Mode Register
98 * @{*/
99/** AFM: Acceptance Filter Mode */
100#define CAN_MR_AFM BIT0
101/** LOM: Listen only mode */
102#define CAN_MR_LOM BIT1
103/** RM: Reset Mode */
104#define CAN_MR_RM BIT2
105/**@}*/
106
107/** CAN Command Register. This register commands the CAN peripheral to either transmit or abort.
108 * @defgroup can_cmr_bits CAN Command Register
109 * @{*/
110/** AT: Abort transmission */
111#define CAN_CMR_AT BIT9
112/** TR: Transmit Request */
113#define CAN_CMR_TR BIT10
114/**@}*/
115
116/** CAN Status Register. This register provides read-only status of the CAN peripheral.
117 * @defgroup can_sr_bits CAN Status Register
118 * @{*/
119/** BS: Bus Off Status */
120#define CAN_SR_BS BIT16
121/** ES: Error Status */
122#define CAN_SR_ES BIT17
123/** TS: Transmit Status */
124#define CAN_SR_TS BIT18
125/** RS: Receive Status */
126#define CAN_SR_RS BIT19
127/** TBS: Transmit Buffer Status */
128#define CAN_SR_TBS BIT21
129/** DSO: Data Overrun Status */
130#define CAN_SR_DSO BIT22
131/** RBS: Receive Buffer Status */
132#define CAN_SR_RBS BIT23
133/**@}*/
134
135/** CAN Interrupt Status Register bit definitions.
136 * - 1: interrupt triggered
137 * - 0: no interrupt
138 * - Writing a 1 to a triggered interrupt clears the bit.
139 * @defgroup can_isr_bits CAN Interrupt Status Register
140 * @{*/
141/** DOI: Data Overflow Interrupt */
142#define CAN_ISR_DOI BIT24
143/** BEI: Bus Error Interrupt */
144#define CAN_ISR_BEI BIT25
145/** TI: Transmit Interrupt */
146#define CAN_ISR_TI BIT26
147/** RI: Receive Interrupt */
148#define CAN_ISR_RI BIT27
149/** EPI: Error Passive Interrupt */
150#define CAN_ISR_EPI BIT28
151/** EWI: Error Warning Interrupt */
152#define CAN_ISR_EWI BIT29
153/** ALI: Arbitration Lost Interrupt */
154#define CAN_ISR_ALI BIT30
155/** This is a helper to acknowledge an ISR */
156#define CAN_ISR_ACKNOWLEDGE(can_base, isr) CAN_ISR_SR_CMR_MR_SET(can_base, ((isr) & 0x7F000000))
157/**@}*/
158
159/** CAN Interrupt Mask Register bit definitions.
160 * 0: disables/masks interrupt
161 * 1: enables interrupt
162 * @defgroup can_imr_bits CAN Mask Register
163 * @{*/
164/** DOIM: DOI Interrupt Mask */
165#define CAN_IMR_DOIM BIT0
166/** BEIM: BEI Interrupt Mask */
167#define CAN_IMR_BEIM BIT1
168/** TIM: TI Interrupt Mask */
169#define CAN_IMR_TIM BIT2
170/** RIM: RI Interrupt Mask */
171#define CAN_IMR_RIM BIT3
172/** EPIM: EPI Interrupt Mask */
173#define CAN_IMR_EPIM BIT4
174/** EWIM: EWI Interrupt Mask */
175#define CAN_IMR_EWIM BIT5
176/** ALIM: ALI Interrupt Mask */
177#define CAN_IMR_ALIM BIT6
178/**@}*/
179
180/** CAN Receive Message Counter Register bit definitions.
181 * @defgroup can_rmc_bits CAN Receive Message Counter Register.
182 * @{*/
183#define CAN_RMC(can_base) ((CAN_BTR1_BTR0_RMC_IMR(can_base) >> 8) & 0x1F)
184/**@}*/
185
186/** CAN Bus Timing 0 Register bit definitions.
187 * @defgroup can_btr0_bits CAN Bus Timing 0 Register.
188 * @{*/
189#define CAN_BTR0_BRP_MASK (0x3F)
190#define CAN_BTR0_BRP_SHIFT 16
191#define CAN_BTR0_BRP(val) (((val) & CAN_BTR0_BRP_MASK) << CAN_BTR0_BRP_SHIFT)
192#define CAN_BTR0_SJW_MASK (0x03)
193#define CAN_BTR0_SJW_SHIFT 22
194#define CAN_BTR0_SJW(val) (((val) & CAN_BTR0_SJW_MASK) << CAN_BTR0_SJW_SHIFT)
195/**@}*/
196
197/** CAN Bus Timing 1 Register bit definitions.
198 * @defgroup can_btr1_bits CAN Bus Timing 1 Register
199 * @{*/
200#define CAN_BTR1_TSEG1_MASK (0x0F)
201#define CAN_BTR1_TSEG1_SHIFT 24
202#define CAN_BTR1_TSEG1(val) (((val) & CAN_BTR1_TSEG1_MASK) << CAN_BTR1_TSEG1_SHIFT)
203
204#define CAN_BTR1_TSEG2_MASK (0x07)
205#define CAN_BTR1_TSEG2_SHIFT 28
206#define CAN_BTR1_TSEG2(val) (((val) & CAN_BTR1_TSEG2_MASK) << CAN_BTR1_TSEG2_SHIFT)
207#define CAN_BTR1_SAM BIT31
208/**@}*/
209
210/** CAN Error Code Capture Register bit definitions.
211 * @defgroup can_ecc_bits CAN Error Code Capture Register
212 * @{*/
213/** BER: Bit error ocurred */
214#define CAN_ECC_BER BIT0
215/** STFER: Stuff error occurred */
216#define CAN_ECC_STFER BIT1
217/** CRCER: CRC error occurred */
218#define CAN_ECC_CRCER BIT2
219/** FRMER: Form error occurred */
220#define CAN_ECC_FRMER BIT3
221/** ACKER: ACK error occurred */
222#define CAN_ECC_ACKER BIT4
223/** EDIR: Direction of transfer 0:TX, 1:RX */
224#define CAN_ECC_EDIR BIT5
225/** TXWRN: set when CAN_TXERR >= 96 */
226#define CAN_ECC_TXWRN BIT6
227/** RXWRN: set when CAN_RXERR >= 96 */
228#define CAN_ECC_RXWRN BIT7
229/**@}*/
230
231/** CAN Acceptance Code/Mask Register. This is used for filtering messages.
232 * Mask value of 1 ignores the bit. Mask value of 0 checks the bit.
233 * @defgroup can_acr_bits CAN Acceptance Code Register
234 * @{*/
235#define CAN_ACR_DUAL_DB_UPPER 0x000F0000U /* 19:16 */
236#define CAN_ACR_DUAL_DB_LOWER 0x0000000FU /* 3:0 */
237#define CAN_ACR_DUAL_ID1 0xFFE00000U /* 31:21 */
238#define CAN_ACR_DUAL_ID2 0x0000FFE0U /* 15:5 */
239#define CAN_ACR_DUAL_RTR1 0x00100000U /* 20 */
240#define CAN_ACR_DUAL_RTR2 0x00000010U /* 4 */
241
242#define CAN_ACR_SINGLE_STD_ID 0xFFE00000U /* 31:21 */
243#define CAN_ACR_SINGLE_STD_RTR 0x00100000U /* 20 */
244#define CAN_ACR_SINGLE_STD_DB1 0x0000FF00U /* 15:8 */
245#define CAN_ACR_SINGLE_STD_DB2 0x000000FFU /* 7:0 */
246
247#define CAN_ACR_SINGLE_EXT_ID 0xFFFFFFF8U /* 31:3 */
248#define CAN_ACR_SINGLE_EXT_RTR 0x00000004U /* 2 */
249/**@}*/
250
251/**
252 * @defgroup can_bit_masks CAN Miscellaneous Bit Masks
253 * @{*/
254#define CAN_BITS_2_0 (0x07)
255#define CAN_BITS_3_0 (0x0F)
256#define CAN_BITS_4_0 (0x1F)
257#define CAN_BITS_7_3 (0xF8)
258#define CAN_BITS_10_3 (0x07F8)
259#define CAN_BITS_12_5 (0x00001FE0U)
260#define CAN_BITS_20_13 (0x001FE000U)
261#define CAN_BITS_28_21 (0x1FE00000U)
262#define CAN_BITS_15_8 (0x0000FF00U)
263#define CAN_BITS_23_16 (0x00FF0000U)
264#define CAN_BITS_31_24 (0xFF000000U)
265#define CAN_BITS_23_21 (0x00E00000U)
266/**@}*/
267
268/**@}*/
269
271/** CAN Application Programming Interface.
272 * @addtogroup can_api CAN Peripheral API
273 * @ingroup peripheral_apis
274 @{*/
275void can_enable(uint32_t canport);
276void can_disable(uint32_t canport);
277void can_init(uint32_t canport, bool listen_only, uint32_t sjw,
278 uint32_t tseg1, uint32_t tseg2,
279 bool sam3, uint32_t brp);
280void can_filter_clear(uint32_t canport);
281void can_filter_dual(uint32_t canport, uint32_t id1, uint32_t id1_mask,
282 uint32_t id2, uint32_t id2_mask,
283 uint8_t db, uint8_t db_mask);
284void can_filter_single_std(uint32_t canport, uint32_t id, uint32_t id_mask,
285 uint8_t db1, uint8_t db1_mask,
286 uint8_t db2, uint8_t db2_mask);
287void can_filter_single_std_rtr(uint32_t canport, uint32_t id, uint32_t id_mask,
288 uint8_t db1, uint8_t db1_mask,
289 uint8_t db2, uint8_t db2_mask);
290void can_filter_single_ext(uint32_t canport, uint32_t id, uint32_t id_mask);
291void can_filter_single_ext_rtr(uint32_t canport, uint32_t id, uint32_t id_mask);
292void can_enable_irq(uint32_t canport, uint8_t imr);
293void can_disable_irq(uint32_t canport, uint8_t imr);
294
295bool can_transmit_std(uint32_t canport, uint32_t id, bool rtr, uint8_t length,
296 const uint8_t *data);
297bool can_transmit_ext(uint32_t canport, uint32_t id, bool rtr, uint8_t length,
298 const uint8_t *data);
299void can_abort_transmit(uint32_t canport);
300
301void can_receive(uint32_t canport, uint32_t *id, bool *ext, bool *rtr, uint8_t *length,
302 uint8_t *data);
303/**@}*/
304
306
307#endif /* LIBOPENCM3_PAC55XX_CAN_H_ */
#define END_DECLS
Definition: common.h:34
#define BEGIN_DECLS
Definition: common.h:33
void can_filter_single_std(uint32_t canport, uint32_t id, uint32_t id_mask, uint8_t db1, uint8_t db1_mask, uint8_t db2, uint8_t db2_mask)
CAN Filter Single Standard Frame Notes:
Definition: can.c:160
void can_enable(uint32_t canport)
CAN Enable Enable the CAN peripheral and its associated FIFOs/counters/interrupts.
Definition: can.c:41
void can_filter_single_std_rtr(uint32_t canport, uint32_t id, uint32_t id_mask, uint8_t db1, uint8_t db1_mask, uint8_t db2, uint8_t db2_mask)
CAN Filter Single Standard Frame w/RTR set Notes:
Definition: can.c:196
void can_filter_single_ext(uint32_t canport, uint32_t id, uint32_t id_mask)
CAN Filter Single Extended Frame Notes:
Definition: can.c:227
void can_abort_transmit(uint32_t canport)
CAN Abort Transmit Aborts the current transmission.
Definition: can.c:369
void can_filter_single_ext_rtr(uint32_t canport, uint32_t id, uint32_t id_mask)
CAN Filter Single Extended Frame w/RTR set Notes:
Definition: can.c:252
void can_disable(uint32_t canport)
CAN Disable Disable the CAN peripheral and all associated FIFOs/counters/interrupts.
Definition: can.c:50
void can_filter_clear(uint32_t canport)
CAN Filter Clear Clear the message filters to receive all messages.
Definition: can.c:96
void can_filter_dual(uint32_t canport, uint32_t id1, uint32_t id1_mask, uint32_t id2, uint32_t id2_mask, uint8_t db, uint8_t db_mask)
CAN Dual Filter Standard Frame Notes:
Definition: can.c:123
void can_init(uint32_t canport, bool listen_only, uint32_t sjw, uint32_t tseg1, uint32_t tseg2, bool sam3, uint32_t brp)
CAN Init Initialize the selected CAN peripheral block.
Definition: can.c:65
void can_receive(uint32_t canport, uint32_t *id, bool *ext, bool *rtr, uint8_t *length, uint8_t *data)
CAN Receive Message If no data is in the RX buffer, id and length are set to 0.
Definition: can.c:384
void can_enable_irq(uint32_t canport, uint8_t imr)
CAN Enable IRQ.
Definition: can.c:268
void can_disable_irq(uint32_t canport, uint8_t imr)
CAN Disable IRQ.
Definition: can.c:278
bool can_transmit_ext(uint32_t canport, uint32_t id, bool rtr, uint8_t length, const uint8_t *data)
CAN Transmit Extended Frame.
Definition: can.c:330
bool can_transmit_std(uint32_t canport, uint32_t id, bool rtr, uint8_t length, const uint8_t *data)
CAN Transmit Standard Frame.
Definition: can.c:292