1 /**
2  * @file    can.h
3  * @brief   CAN function prototypes and data types.
4  */
5 
6 /******************************************************************************
7  *
8  * Copyright (C) 2022-2023 Maxim Integrated Products, Inc. (now owned by
9  * Analog Devices, Inc.),
10  * Copyright (C) 2023-2024 Analog Devices, Inc.
11  *
12  * Licensed under the Apache License, Version 2.0 (the "License");
13  * you may not use this file except in compliance with the License.
14  * You may obtain a copy of the License at
15  *
16  *     http://www.apache.org/licenses/LICENSE-2.0
17  *
18  * Unless required by applicable law or agreed to in writing, software
19  * distributed under the License is distributed on an "AS IS" BASIS,
20  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  * See the License for the specific language governing permissions and
22  * limitations under the License.
23  *
24  ******************************************************************************/
25 
26 /* Define to prevent redundant inclusion */
27 #ifndef LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32662_CAN_H_
28 #define LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32662_CAN_H_
29 
30 /* **** Includes **** */
31 #include <stdint.h>
32 #include <stdbool.h>
33 #include "can_regs.h"
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
39 /**
40  * @defgroup can CAN
41  * @ingroup periphlibs
42  * @{
43  */
44 
45 #define MXC_CAN_FILT_PER_OBJ 2
46 
47 // SEG1 = PROPAGATION_SEGMENT + PHASE_SEG1
48 #define MXC_CAN_SEG1_SHIFT 16
49 #define MXC_CAN_SEG1(seg1_tq) ((seg1_tq & 0xFF) << MXC_CAN_SEG1_SHIFT)
50 #define MXC_CAN_SEG2_SHIFT 8
51 #define MXC_CAN_SEG2(seg2_tq) ((seg2_tq & 0xFF) << MXC_CAN_SEG2_SHIFT)
52 #define MXC_CAN_SJW_SHIFT 0
53 #define MXC_CAN_SJW(sjw_tq) ((sjw_tq & 0xFF) << MXC_CAN_SJW_SHIFT)
54 #define MXC_CAN_BIT_SEGMENTS(seg1_tq, seg2_tq, sjw_tq) \
55     (MXC_CAN_SEG1(seg1_tq) | MXC_CAN_SEG2(seg2_tq) | MXC_CAN_SJW(sjw_tq))
56 
57 #define MXC_CAN_NOMINAL_MAX_SEG1TQ 16
58 #define MXC_CAN_NOMINAL_MAX_SEG2TQ 8
59 #define MXC_CAN_NOMINAL_MAX_SJWTQ 4
60 #define MXC_CAN_NOMINAL_MAX_PRESCALER 0x400
61 #define MXC_CAN_FD_DATA_MAX_SEG1TQ 64
62 #define MXC_CAN_FD_DATA_MAX_SEG2TQ 16
63 #define MXC_CAN_FD_DATA_MAX_SJWTQ 16
64 #define MXC_CAN_FD_DATA_MAX_PRESCALER 0x400
65 
66 #define MXC_CAN_FILT_OP_TYPE_SHIFT 0
67 #define MXC_CAN_FILT_OP_TYPE_MASK 0x000F
68 #define MXC_CAN_FILT_SEL_SHIFT 4
69 #define MXC_CAN_FILT_SEL_MASK 0x00F0
70 
71 #define MXC_CAN_MSG_INFO_IDE_BIT 0x80000000
72 #define MXC_CAN_STANDARD_ID(id) (id & 0x7FFUL)
73 #define MXC_CAN_EXTENDED_ID(id) ((id & 0x1FFFFFFFUL) | MXC_CAN_MSG_INFO_IDE_BIT)
74 
75 #define MXC_CAN_BUF_CFG_IDE 0x80
76 #define MXC_CAN_BUF_CFG_SRR 0x40
77 #define MXC_CAN_BUF_CFG_RTR(rtr) ((!!rtr) << 6)
78 #define MXC_CAN_BUF_CFG_FDF(fdf) ((!!fdf) << 5)
79 #define MXC_CAN_BUF_CFG_BRS(brs) ((!!brs) << 4)
80 #define MXC_CAN_BUF_CFG_DLC(dlc) (dlc & 0xF)
81 
82 #define MXC_CAN_BUF_CFG_EXT_ID_TX1(id) ((id & (0xFF << 21)) >> 21)
83 #define MXC_CAN_BUF_CFG_EXT_ID_TX2(id) ((id & (0xFF << 13)) >> 13)
84 #define MXC_CAN_BUF_CFG_EXT_ID_TX3(id) ((id & (0xFF << 5)) >> 5)
85 #define MXC_CAN_BUF_CFG_EXT_ID_TX4(id) ((id & 0x1F) << 3)
86 #define MXC_CAN_BUF_CFG_EXT_ID_RX1(rxdata) (rxdata << 21)
87 #define MXC_CAN_BUF_CFG_EXT_ID_RX2(rxdata) (rxdata << 13)
88 #define MXC_CAN_BUF_CFG_EXT_ID_RX3(rxdata) (rxdata << 5)
89 #define MXC_CAN_BUF_CFG_EXT_ID_RX4(rxdata) ((rxdata & 0xF8) >> 3)
90 #define MXC_CAN_BUF_CFG_EXT_ID_RTR(rtr) ((!!rtr) << 2)
91 #define MXC_CAN_BUF_CFG_EXT_ID_ESI(esi) ((!!esi) << 1)
92 
93 #define MXC_CAN_BUF_CFG_STD_ID_TX1(id) ((id & (0xFF << 3)) >> 3)
94 #define MXC_CAN_BUF_CFG_STD_ID_TX2(id) ((id & 0x7) << 5)
95 #define MXC_CAN_BUF_CFG_STD_ID_RX1(rxdata) (rxdata << 3)
96 #define MXC_CAN_BUF_CFG_STD_ID_RX2(rxdata) ((rxdata & 0xE0) >> 5)
97 #define MXC_CAN_BUF_CFG_STD_ID_RTR(rtr) ((!!rtr) << 6)
98 #define MXC_CAN_BUF_CFG_STD_ID_ESI(esi) ((!!esi) << 3)
99 
100 #define MXC_CAN_LEC_NO_ERR 0U
101 #define MXC_CAN_LEC_BIT_ERR 1U
102 #define MXC_CAN_LEC_STUFF_ERR 2U
103 #define MXC_CAN_LEC_CRC_ERR 3U
104 #define MXC_CAN_LEC_FORM_ERR 4U
105 #define MXC_CAN_LEC_ACK_ERR 5U
106 #define MXC_CAN_ECC_ERROR_CODE_MASK                                                   \
107     (MXC_F_CAN_REVA_ECC_ACKER | MXC_F_CAN_REVA_ECC_FRMER | MXC_F_CAN_REVA_ECC_CRCER | \
108      MXC_F_CAN_REVA_ECC_STFER | MXC_F_CAN_REVA_ECC_BER)
109 
110 #define MXC_CAN_UNIT_STATE_INACTIVE 0U
111 #define MXC_CAN_UNIT_STATE_ACTIVE 1U
112 #define MXC_CAN_UNIT_STATE_PASSIVE 2U
113 #define MXC_CAN_UNIT_STATE_BUS_OFF 3U
114 
115 #define MXC_CAN_DMA_LEN(msg_id) (msg_id & MXC_CAN_MSG_INFO_IDE_BIT ? 5 : 3)
116 
117 #define MXC_CAN_TXSCNT_MAX 0x80
118 #define MXC_CAN_ERRPSV_THRESH 0x80
119 
120 /**
121  * @brief  Struct containing information about the version of the CAN library
122  */
123 typedef struct {
124     uint16_t api; ///< CMSIS API Version
125     uint16_t drv; ///< Maxim CAN SDK Version
126 } mxc_can_drv_version_t;
127 
128 /**
129  * @brief  Struct containing the capabilities of the CAN driver.
130  */
131 typedef struct {
132     uint32_t num_objects; ///< Number of objects available
133     uint32_t
134         reentrant_operation; ///< Reentrant calls to MessageSend/Read, ObjectConfigure, and CAN_Control supported
135     uint32_t fd_mode; ///< CAN FD supported
136     uint32_t restricted_mode; ///< Restricted mode supported
137     uint32_t monitor_mode; ///< Monitor mode supported
138     uint32_t internal_loopback; ///< Internal loopback supported
139     uint32_t external_loopback; ///< External loopback supported
140     uint32_t rsv; ///< Reserved for future use
141 } mxc_can_capabilities_t;
142 
143 /**
144  * @brief  Selects power state of the CAN peripherals
145  */
146 typedef enum {
147     MXC_CAN_PWR_CTRL_OFF, ///< Shut off power to peripherals
148     MXC_CAN_PWR_CTRL_SLEEP, ///< Put peripherals to sleep
149     MXC_CAN_PWR_CTRL_FULL, ///< Peripherals fully awake
150 } mxc_can_pwr_ctrl_t;
151 
152 /**
153  * @brief  Selects which bitrate to perform operation on
154  */
155 typedef enum {
156     MXC_CAN_BITRATE_SEL_NOMINAL, ///< Set bitrate for classic CAN frames
157     MXC_CAN_BITRATE_SEL_FD_DATA, ///< Reserved for future use. Not supported on MAX32662, included to prevent build errors.
158 } mxc_can_bitrate_sel_t;
159 
160 /**
161  * @brief  Selects the CAN driver's mode of operation
162  */
163 typedef enum {
164     MXC_CAN_MODE_INITIALIZATION, ///< Reset mode
165     MXC_CAN_MODE_NORMAL, ///< Normal operating mode
166     MXC_CAN_MODE_RESTRICTED, ///< Restricted mode
167     MXC_CAN_MODE_MONITOR, ///< Listen-only mode
168     MXC_CAN_MODE_LOOPBACK, ///< Loopback mode
169     MXC_CAN_MODE_RSV, ///< Reserved for future use
170     MXC_CAN_MODE_LOOPBACK_W_TXD, ///< Loopback mode with transmit pin disconnected
171 } mxc_can_mode_t;
172 
173 /**
174  * @brief  Struct containing information about the objects associated with a particular CAN driver.
175  */
176 typedef struct {
177     int32_t tx; ///< Object supports transmission
178     int32_t rx; ///< Object supports receive
179     int32_t rx_rtr_tx_data; ///< Object supports RTR reception and automatic data frame transmission
180     int32_t tx_rtr_rx_data; ///< Object supports RTR transmission and automatic data fram reception
181     int32_t multiple_filters; ///< Number of filters supported by the object
182     int32_t exact_filtering; ///< Object can support exact message ID filters
183     int32_t mask_filtering; ///< Object can support mask message ID filters
184     int32_t range_filtering; ///< Object can support range message ID filters
185     int32_t message_depth; ///< Message depth of transmit and receive buffers
186     int32_t reserved; ///< Reserved for future use
187 } mxc_can_obj_capabilities_t;
188 
189 /**
190  * @brief  Type used to select which operation to perform on message ID filter. Mask together one choice from each group to make configuration selection
191  */
192 typedef enum {
193     // Select one from Group 1 {
194     MXC_CAN_FILT_CFG_EXACT_ADD = 0, ///< Add exact filter
195     MXC_CAN_FILT_CFG_EXACT_DEL = 1, ///< Remove exact filter
196     MXC_CAN_FILT_CFG_RSV1 = 2, ///< NOT SUPPORTED ON MAX32690
197     MXC_CAN_FILT_CFG_RSV2 = 3, ///< NOT SUPPORTED ON MAX32690
198     MXC_CAN_FILT_CFG_MASK_ADD = 4, ///< Add maskable filter
199     MXC_CAN_FILT_CFG_MASK_DEL = 5, ///< Remove maskable filter
200     // } end group 1
201 
202     // Select one from group 2 {
203     MXC_CAN_FILT_CFG_DUAL_GEN = 0
204                                 << MXC_CAN_FILT_SEL_SHIFT, ///< Reccomended only for middleware use
205     MXC_CAN_FILT_CFG_DUAL1_STD_ID =
206         1
207         << MXC_CAN_FILT_SEL_SHIFT, ///< Perform operation on dual filter 1, for 11-bit message ID's
208     MXC_CAN_FILT_CFG_DUAL1_EXT_ID =
209         2
210         << MXC_CAN_FILT_SEL_SHIFT, ///< Perform operation on dual filter 1, for 29-bit message ID's
211     MXC_CAN_FILT_CFG_DUAL2_STD_ID =
212         3
213         << MXC_CAN_FILT_SEL_SHIFT, ///< Perform operation on dual filter 2, for 11-bit message ID's
214     MXC_CAN_FILT_CFG_DUAL2_EXT_ID =
215         4
216         << MXC_CAN_FILT_SEL_SHIFT, ///< Perform operation on dual filter 2, for 29-bit message ID's
217     MXC_CAN_FILT_CFG_SINGLE_STD_ID =
218         5
219         << MXC_CAN_FILT_SEL_SHIFT, ///< Perform operation on single filter, for 11-bit message ID's
220     MXC_CAN_FILT_CFG_SINGLE_EXT_ID =
221         6
222         << MXC_CAN_FILT_SEL_SHIFT, ///< Perform operation on single filter, for 29-bit message ID's
223     // } end group 2
224 } mxc_can_filt_cfg_t;
225 
226 /**
227  * @brief  Struct detailing the current status of the CAN driver.
228  */
229 typedef struct {
230     uint32_t unit_state; ///< State of the CAN bus
231     uint32_t last_error_code; ///< Last error code recorded
232     uint32_t tx_err_cnt; ///< Number of transmission errors
233     uint32_t rx_err_cnt; ///< Number of receive errors
234     uint32_t can_idx; ///< Index of CAN peripheral status was retrieved for
235 } mxc_can_stat_t;
236 
237 /**
238  * @brief  Contains information about the message to be sent or the message received.
239  */
240 typedef struct {
241     uint32_t msg_id; ///< Message ID
242     uint32_t rtr; ///< Remote transmit request frame
243     uint32_t fdf; ///< FD frame
244     uint32_t brs; ///< FD format bit rate switch
245     uint32_t esi; ///< FD format error state indicator
246     uint32_t dlc; ///< Data length code
247     uint32_t rsv; ///< Reserved for future use
248 } mxc_can_msg_info_t;
249 
250 /**
251  * @brief  Used to set the features available to a CAN object
252  */
253 typedef enum {
254     MXC_CAN_OBJ_CFG_INACTIVE, ///< Object disabled
255     MXC_CAN_OBJ_CFG_TXRX, ///< Object can transmit and/or receive messages
256     MXC_CAN_OBJ_CFG_RSV, ///< Reserved for future use
257     MXC_CAN_OBJ_CFG_RX_RTR_TX_DATA, ///< NOT SUPPORTED ON MAX32690
258     MXC_CAN_OBJ_CFG_TX_RTR_RX_DATA, ///< NOT SUPPORTED ON MAX32690
259 } mxc_can_obj_cfg_t;
260 
261 /**
262  * @brief  Selects the control operation for the CAN driver to perform
263  */
264 typedef enum {
265     MXC_CAN_CTRL_SET_FD_MODE, ///< No effect on MAX32690 (FD mode always enabled when CAN active)
266     MXC_CAN_CTRL_ABORT_TX, ///< Abort transmission
267     MXC_CAN_CTRL_RETRANSMISSION, ///< Enable/disable auto retransmission on error
268     MXC_CAN_CTRL_TRANSCEIVER_DLY, ///< Set transceiver delay
269 } mxc_can_ctrl_t;
270 
271 /**
272  * @brief  State which bus has entered to trigger unit event
273  */
274 typedef enum {
275     MXC_CAN_UNIT_EVT_INACTIVE, ///< Peripherals entered inactive state (sleep, shutdown)
276     MXC_CAN_UNIT_EVT_ACTIVE, ///< Peripherals entered active state
277     MXC_CAN_UNIT_EVT_WARNING, ///< Peripheral received error warning
278     MXC_CAN_UNIT_EVT_PASSIVE, ///< Peripheral entered passive state
279     MXC_CAN_UNIT_EVT_BUS_OFF, ///< Bus turned off
280 } mxc_can_unit_evt_t;
281 
282 /**
283  * @brief  Selects which object to notify/handle
284  */
285 typedef enum {
286     MXC_CAN_OBJ_EVT_TX_COMPLETE, ///< Transmission complete
287     MXC_CAN_OBJ_EVT_RX, ///< Message received
288     MXC_CAN_OBJ_EVT_RX_OVERRUN, ///< RXFIFO overflow
289 } mxc_can_obj_evt_t;
290 
291 /**
292  * @brief  Struct containing information about CAN message
293  */
294 typedef struct {
295     mxc_can_msg_info_t
296         *msg_info; ///< Pointer to struct containing information about the format of the message
297     uint8_t *
298         data; ///< Pointer to array of data bytes (either data to transmit or where to store data received)
299     uint8_t
300         data_sz; ///< MessageSend - number of data bytes to transmit, MessageRead - maximum number of data bytes that can be stored in "data"
301 } mxc_can_req_t;
302 
303 ///< Callback used when a bus event occurs
304 typedef void (*mxc_can_unit_event_cb_t)(uint32_t can_idx, uint32_t event);
305 
306 ///< Callback used when a transmission event occurs
307 typedef void (*mxc_can_object_event_cb_t)(uint32_t can_idx, uint32_t event);
308 
309 /**
310  * @brief   Get information about the version of the CMSIS API and Maxim CAN SDK
311  *
312  * @return  Struct containing version information
313  */
314 mxc_can_drv_version_t MXC_CAN_GetVersion(void);
315 
316 /**
317  * @brief   Get information about the capabilities of the Maxim CAN SDK
318  *
319  * @return  Struct containing capabilities information
320  */
321 mxc_can_capabilities_t MXC_CAN_GetCapabilities(void);
322 
323 /**
324  * @brief   Initializes CAN event callbacks
325  * @note    On default this function enables CAN peripheral clock and CAN gpio pins.
326  *          if you wish to manage clock and gpio related things in upper level instead of here.
327  *          Define MSDK_NO_GPIO_CLK_INIT flag in project.mk file.
328  *          By this flag this function will remove clock and gpio related codes from file.
329  *
330  * @param can_idx   Index of the CAN peripheral to initialize
331  * @param cfg       Specifies how to configure CAN peripheral (see MXC_CAN_ObjectConfigure)
332  * @param unit_cb   Pointer to unit event callback function
333  * @param obj_cb    Pointer to object event callback function
334  * @param map       Selects pin mapping (0 - CANA Pins; 1 - CANB Pins)
335  *
336  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
337  */
338 int MXC_CAN_Init(uint32_t can_idx, mxc_can_obj_cfg_t cfg, mxc_can_unit_event_cb_t unit_cb,
339                  mxc_can_object_event_cb_t obj_cb, uint8_t map);
340 
341 /**
342  * @brief   Free CAN resources (does not reset or disable CAN peripherals)
343  * @note    On default this function enables CAN peripheral clock.
344  *          if you wish to manage clock related things in upper level instead of here.
345  *          Define MSDK_NO_GPIO_CLK_INIT flag in project.mk file.
346  *          By this flag this function will remove clock related codes from file.
347  *
348  * @param can_idx   Index of CAN peripheral to un-initialize (shutdown)
349  *
350  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
351  */
352 int MXC_CAN_UnInit(uint32_t can_idx);
353 
354 /**
355  * @brief   Change Power state of the CAN peripherals
356  * @note    On default this function enables CAN peripheral clock.
357  *          if you wish to manage clock related things in upper level instead of here.
358  *          Define MSDK_NO_GPIO_CLK_INIT flag in project.mk file.
359  *          By this flag this function will remove clock related codes from file.
360  *
361  * @param can_idx   Index of CAN peripheral to alter power settings for
362  * @param pwr       Desired power state of the CAN peripherals
363  *
364  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
365  */
366 int MXC_CAN_PowerControl(uint32_t can_idx, mxc_can_pwr_ctrl_t pwr);
367 
368 /**
369  * @brief   Enables interrupts in the interrupt and extended interrupt enable registetrs
370  *
371  * @param can_idx   Index of the CAN peripheral to enable interrupts for (0 - CAN0, 1 - CAN1)
372  * @param en        Mask of interrupts to enable in the interrupt enable register
373  * @param ext_en    Mask of interrupts to enable in the extended interrupt enable register
374  *
375  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
376  */
377 int MXC_CAN_EnableInt(uint32_t can_idx, uint8_t en, uint8_t ext_en);
378 
379 /**
380  * @brief   Disables interrupts in the interrupt and extended interrupt enable registers
381  *
382  * @param can_idx   Index of the CAN peripheral to disable interrupts for (0 - CAN0, 1 - CAN1)
383  * @param dis       Mask of interrupts to disable in the interrupt enable register
384  * @param ext_dis   Mask of interrupts to disable in the extended interrupt enable register
385  *
386  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
387  */
388 int MXC_CAN_DisableInt(uint32_t can_idx, uint8_t dis, uint8_t ext_dis);
389 
390 /**
391  * @brief   Reads interrupt status flags
392  *
393  * @param can_idx   Retrieve interrupt flags for CAN peripheral specified by this parameter (0 - CAN0, 1 - CAN1)
394  * @param flags     Interrupt status flags in the INTFL register
395  * @param ext_flags Interrupt status flags in the EINTFL register
396  *
397  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
398  */
399 int MXC_CAN_GetFlags(uint32_t can_idx, uint8_t *flags, uint8_t *ext_flags);
400 
401 /**
402  * @brief   Clears interrupts flags
403  *
404  * @param can_idx   Clear interrupt flags for CAN peripheral specified by this parameter (0 - CAN0, 1 - CAN1)
405  * @param flags     Mask of interrupt flags to clear in INTFL register
406  * @param ext_flags Mask of interrupt flags to clear in EINTFL register
407  *
408  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
409  */
410 int MXC_CAN_ClearFlags(uint32_t can_idx, uint8_t flags, uint8_t ext_flags);
411 
412 /**
413  * @brief   Returns the bit rate of the CAN clock
414  *
415  * @param can_idx   Index of CAN peripheral to get clock rate of
416  *
417  * @return  Frequency of CAN clock
418  */
419 int MXC_CAN_GetClock(uint32_t can_idx);
420 
421 /**
422  * @brief   Returns the bit rate of the CAN clock
423  *
424  * @param can_idx   Selects CAN peripheral (0 - CAN0, 1 - CAN1) to retrieve bit rate for
425  * @param sel       Select which bitrate to return
426  *
427  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes. If successful, returns value of bit rate.
428  *
429  * @warning MAX32662 does not support CAN FD, passing MXC_CAN_BITRATE_SEL_FD_DATA will return an error.
430  */
431 int MXC_CAN_GetBitRate(uint32_t can_idx, mxc_can_bitrate_sel_t sel);
432 
433 /**
434  * @brief   Sets CAN clock frequency and sets time quanta values
435  *
436  * @param can_idx       Index of CAN peripheral to set bitrate for
437  * @param sel           Selects which bitrate to set (nominal/FD arbitration phase or FD data phase)
438  * @param bitrate       Desired bitrate
439  * @param bit_segments  Mask of number of time quanta in each bit segment see MXC_CAN_BIT_SEGMENTS(seg1_tq, seg2_tq, sjw_tq) defined above
440  *
441  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
442  *
443  * @warning MAX32662 does not support CAN FD, passing MXC_CAN_BITRATE_SEL_FD_DATA will return an error.
444  */
445 int MXC_CAN_SetBitRate(uint32_t can_idx, mxc_can_bitrate_sel_t sel, uint32_t bitrate,
446                        uint32_t bit_segments);
447 
448 /**
449  * @brief   Sets the operating mode of the CAN peripherals
450  *
451  * @param can_idx   Index of CAN peripheral
452  * @param mode      Selects the mode of the CAN peripherals
453  *
454  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
455  */
456 int MXC_CAN_SetMode(uint32_t can_idx, mxc_can_mode_t mode);
457 
458 /**
459  * @brief   Get the capabilities of the CAN object specified by can_idx
460  *
461  * @param can_idx   Index of the CAN peripheral to get capabilities of
462  *
463  * @return  Object capabilities information
464  */
465 mxc_can_obj_capabilities_t MXC_CAN_ObjectGetCapabilities(uint32_t can_idx);
466 
467 /**
468  * @brief   Setup message ID filter on CAN peripheral
469  *
470  * @param can_idx   Pointer to CAN instance
471  * @param cfg       Specifies how the filter should be configured
472  * @param id        Exact ID for exact filter type. Base ID for maskable filter type.
473  * @param arg       Mask for maskable filter type. Bits set to 0 are "don't care" bits and will always be accepted regardless of value, 1's are compared with ID. (Inverse of AMR register function.)
474  *
475  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
476  */
477 int MXC_CAN_ObjectSetFilter(uint32_t can_idx, mxc_can_filt_cfg_t cfg, uint32_t id, uint32_t arg);
478 
479 /**
480  * @brief   Configure CAN object
481  * @note    On default this function enables CAN gpio pins.
482  *          if you wish to manage gpio related things in upper level instead of here.
483  *          Define MSDK_NO_GPIO_CLK_INIT flag in project.mk file.
484  *          By this flag this function will remove gpio related codes from file.
485  *
486  * @param can_idx   Index of CAN peripheral instance
487  * @param cfg       Specifies how the filter should be configured
488  * @param map       Selects pin mapping (0 - CANA Pins; 1 - CANB Pins)
489  *
490  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
491  */
492 int MXC_CAN_ObjectConfigure(uint32_t can_idx, mxc_can_obj_cfg_t cfg, uint8_t map);
493 
494 /**
495  * @brief   Write data to be transmitted to TX FIFO (does not need to be called before message send)
496  *
497  * @param can_idx   Index of CAN peripheral to read RX data for
498  * @param info      Pointer to struct containing information about the type of CAN message to send
499  * @param data      Buffer of data bytes to be transmitted
500  * @param size      Number of data bytes in "data"
501  *
502  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
503  *
504  * @warning MAX32662 does not support CAN FD, setting info->fdf will return an error.
505  */
506 int MXC_CAN_WriteTXFIFO(uint32_t can_idx, mxc_can_msg_info_t *info, const uint8_t *data,
507                         uint8_t size);
508 
509 /**
510  * @brief   Reads data from RX FIFO if data available
511  *
512  * @param can_idx   Index of CAN peripheral to read RX data for
513  * @param info      Pointer to struct to store message information in
514  * @param data      Buffer to store received data bytes
515  * @param size      Maximum number of data bytes that can be stored in "data"
516  *
517  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
518  *
519  * @warning MAX32662 does not support CAN FD, setting info->fdf will return an error.
520  */
521 int MXC_CAN_ReadRXFIFO(uint32_t can_idx, mxc_can_msg_info_t *info, uint8_t *data, uint8_t size);
522 
523 /**
524  * @brief   Send message (this is a blocking function).
525  *
526  * @param can_idx   Index of the CAN peripheral to send the message from
527  * @param req       Contains information about the format and data of message to send
528  *
529  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
530  *
531  * @warning MAX32662 does not support CAN FD, setting req->msg_info->fdf will return an error.
532  */
533 int MXC_CAN_MessageSend(uint32_t can_idx, mxc_can_req_t *req);
534 
535 /**
536  * @brief   Send message (non-blocking).
537  *
538  * @param can_idx   Index of the CAN peripheral to send the message from
539  * @param req       Contains information about the format and data of message to send
540  *
541  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
542  *
543  * @warning MAX32662 does not support CAN FD, setting req->msg_info->fdf will return an error.
544  * @warning The structure pointed to by 'req' must remain unchanged and in scope until the
545  *          TX complete event has been signaled.
546  */
547 int MXC_CAN_MessageSendAsync(uint32_t can_idx, mxc_can_req_t *req);
548 
549 /**
550  * @brief   Send message (non-blocking DMA).
551  *
552  * @param can_idx   Index of the CAN peripheral to send the message from
553  * @param req       Contains information about the format and data of message to send
554  *
555  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
556  *
557  * @warning MAX32662 does not support CAN FD, setting req->msg_info->fdf will return an error.
558  * @warning The structure pointed to by 'req' must remain unchanged and in scope until the
559  *          TX complete event has been signaled.
560  */
561 int MXC_CAN_MessageSendDMA(uint32_t can_idx, mxc_can_req_t *req);
562 
563 /**
564  * @brief   Read received message if any. (this is a blocking function)
565  *
566  * @param can_idx   Index of the CAN peripheral to read the message for
567  * @param req       Pointer to struct that stores CAN message information
568  *
569  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
570  *
571  * @warning MAX32662 does not support CAN FD, setting req->msg_info->fdf will return an error.
572  */
573 int MXC_CAN_MessageRead(uint32_t can_idx, mxc_can_req_t *req);
574 
575 /**
576  * @brief   Set up CAN device for asynchronus data receive (non-blocking)
577  *
578  * @param can_idx   Index of the CAN peripheral to set up asynchronus reads for
579  * @param req       Pointer to struct that stores CAN message information
580  *
581  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
582  *
583  * @warning MAX32662 does not support CAN FD, setting req->msg_info->fdf will return an error.
584  * @warning The structure pointed to by 'req' must remain unchanged and in scope until the
585  *          RX complete event has been signaled.
586  */
587 int MXC_CAN_MessageReadAsync(uint32_t can_idx, mxc_can_req_t *req);
588 
589 /**
590  * @brief   Set up CAN device for DMA data receive
591  *
592  * @param can_idx   Index of the CAN peripheral to receive data
593  * @param req       Pointer to struct that stores CAN message information, initialize "msg_info" to expected configuration of the message to be received (Needed to ensure proper DMA length is set, if these are not known use MXC_CAN_MessageReadAsync instead.)
594  * @param dma_cb    Pointer to DMA callback function.
595  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
596  *
597  * @warning MAX32662 does not support CAN FD, setting req->msg_info->fdf will return an error.
598  * @warning The structure pointed to by 'req' must remain unchanged and in scope until the
599  *          RX complete event has been signaled.
600  */
601 int MXC_CAN_MessageReadDMA(uint32_t can_idx, mxc_can_req_t *req, void (*dma_cb)(int, int));
602 
603 /**
604  * @brief   General interrupt handler for MessageSendAsync and MessageReadAsync
605  *
606  * @param can_idx   Index of the CAN peripheral to handle interrupts for
607  *
608  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
609  */
610 int MXC_CAN_Handler(uint32_t can_idx);
611 
612 /**
613  * @brief   Perform control operation on CAN peripheral(s)
614  *
615  * @param can_idx   Index of CAN peripheral to perform control function on
616  * @param ctrl      Operation to perform on the CAN peripherals
617  * @param ctrl_arg  Depends on ctrl. RETRANSMISSION: 1-Enable, 0-Disable; TRANSCEIVER_DELAY: number of time quanta to delay; any other value of control this parameter is ignored
618  *
619  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
620  *
621  * @warning MAX32662 does not support CAN FD, passing MXC_CAN_CTRL_SET_FD_MODE will return an error.
622  */
623 int MXC_CAN_Control(uint32_t can_idx, mxc_can_ctrl_t ctrl, uint32_t ctrl_arg);
624 
625 /**
626  * @brief   Configure wakeup timer settings (must be called before entering sleep mode)
627  *
628  * @param can_idx           Index of CAN peripheral to configure wakeup timer for
629  * @param prescaler         Value to scale the CAN clock by to generate the wakup clock signal
630  * @param wup_filter_tm     Value to set Wake-up filter time register to
631  * @param wup_expire_tm     Value to set wake-up expire time register to
632  *
633  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
634  */
635 int MXC_CAN_SetWakeupTimer(uint32_t can_idx, uint8_t prescaler, uint16_t wup_filter_tm,
636                            uint32_t wup_expire_tm);
637 
638 /*
639  * @brief   Get status of the bus unit
640  *
641  * @param can_idx   Index of CAN peripheralto get status of
642  *
643  * @return  Information about the bus and error status
644  */
645 mxc_can_stat_t MXC_CAN_GetStatus(uint32_t can_idx);
646 
647 /**
648  * @brief   Notify unit event handler of event that transpired
649  *
650  * @param can_idx   Index of CAN peripheral which the event transpired on
651  *
652  * @param event     Event that occured
653  */
654 void MXC_CAN_SignalUnitEvent(uint32_t can_idx, mxc_can_unit_evt_t event);
655 
656 /**
657  * @brief   Notify object event handler of event that transpired
658  *
659  * @param can_idx   Index of the CAN peripheral which had the event
660  * @param event     Event that occured
661  */
662 void MXC_CAN_SignalObjectEvent(uint32_t can_idx, mxc_can_obj_evt_t event);
663 
664 /**@} end of group can */
665 
666 #ifdef __cplusplus
667 }
668 #endif
669 
670 #endif // LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32662_CAN_H_
671