1 //*****************************************************************************
2 //
3 // can.h - Defines and Macros for the CAN controller.
4 //
5 // Copyright (c) 2006-2020 Texas Instruments Incorporated.  All rights reserved.
6 // Software License Agreement
7 //
8 //   Redistribution and use in source and binary forms, with or without
9 //   modification, are permitted provided that the following conditions
10 //   are met:
11 //
12 //   Redistributions of source code must retain the above copyright
13 //   notice, this list of conditions and the following disclaimer.
14 //
15 //   Redistributions in binary form must reproduce the above copyright
16 //   notice, this list of conditions and the following disclaimer in the
17 //   documentation and/or other materials provided with the
18 //   distribution.
19 //
20 //   Neither the name of Texas Instruments Incorporated nor the names of
21 //   its contributors may be used to endorse or promote products derived
22 //   from this software without specific prior written permission.
23 //
24 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 //
36 // This is part of revision 2.2.0.295 of the Tiva Peripheral Driver Library.
37 //
38 //*****************************************************************************
39 
40 #ifndef __DRIVERLIB_CAN_H__
41 #define __DRIVERLIB_CAN_H__
42 
43 //*****************************************************************************
44 //
45 //! \addtogroup can_api
46 //! @{
47 //
48 //*****************************************************************************
49 
50 //*****************************************************************************
51 //
52 // If building with a C++ compiler, make all of the definitions in this header
53 // have a C binding.
54 //
55 //*****************************************************************************
56 #ifdef __cplusplus
57 extern "C"
58 {
59 #endif
60 
61 //*****************************************************************************
62 //
63 // Miscellaneous defines for Message ID Types
64 //
65 //*****************************************************************************
66 
67 //*****************************************************************************
68 //
69 // These are the flags used by the tCANMsgObject.ui32Flags value when calling
70 // the CANMessageSet() and CANMessageGet() functions.
71 //
72 //*****************************************************************************
73 
74 //
75 //! This indicates that transmit interrupts are enabled.
76 //
77 #define MSG_OBJ_TX_INT_ENABLE   0x00000001
78 
79 //
80 //! This indicates that receive interrupts are enabled.
81 //
82 #define MSG_OBJ_RX_INT_ENABLE   0x00000002
83 
84 //
85 //! This indicates that a message object is using an extended identifier.
86 //
87 #define MSG_OBJ_EXTENDED_ID     0x00000004
88 
89 //
90 //! This indicates that a message object is using filtering based on the
91 //! object's message identifier.
92 //
93 #define MSG_OBJ_USE_ID_FILTER   0x00000008
94 
95 //
96 //! This indicates that new data was available in the message object.
97 //
98 #define MSG_OBJ_NEW_DATA        0x00000080
99 
100 //
101 //! This indicates that data was lost since this message object was last
102 //! read.
103 //
104 #define MSG_OBJ_DATA_LOST       0x00000100
105 
106 //
107 //! This indicates that a message object uses or is using filtering
108 //! based on the direction of the transfer.  If the direction filtering is
109 //! used, then ID filtering must also be enabled.
110 //
111 #define MSG_OBJ_USE_DIR_FILTER  (0x00000010 | MSG_OBJ_USE_ID_FILTER)
112 
113 //
114 //! This indicates that a message object uses or is using message
115 //! identifier filtering based on the extended identifier.  If the extended
116 //! identifier filtering is used, then ID filtering must also be enabled.
117 //
118 #define MSG_OBJ_USE_EXT_FILTER  (0x00000020 | MSG_OBJ_USE_ID_FILTER)
119 
120 //
121 //! This indicates that a message object is a remote frame.
122 //
123 #define MSG_OBJ_REMOTE_FRAME    0x00000040
124 
125 //
126 //! This indicates that this message object is part of a FIFO structure and
127 //! not the final message object in a FIFO.
128 //
129 #define MSG_OBJ_FIFO            0x00000200
130 
131 //
132 //! This indicates that a message object has no flags set.
133 //
134 #define MSG_OBJ_NO_FLAGS        0x00000000
135 
136 //*****************************************************************************
137 //
138 //! This define is used with the flag values to allow checking only status
139 //! flags and not configuration flags.
140 //
141 //*****************************************************************************
142 #define MSG_OBJ_STATUS_MASK     (MSG_OBJ_NEW_DATA | MSG_OBJ_DATA_LOST)
143 
144 //*****************************************************************************
145 //
146 //! The structure used for encapsulating all the items associated with a CAN
147 //! message object in the CAN controller.
148 //
149 //*****************************************************************************
150 typedef struct
151 {
152     //
153     //! The CAN message identifier used for 11 or 29 bit identifiers.
154     //
155     uint32_t ui32MsgID;
156 
157     //
158     //! The message identifier mask used when identifier filtering is enabled.
159     //
160     uint32_t ui32MsgIDMask;
161 
162     //
163     //! This value holds various status flags and settings specified by
164     //! tCANObjFlags.
165     //
166     uint32_t ui32Flags;
167 
168     //
169     //! This value is the number of bytes of data in the message object.
170     //
171     uint32_t ui32MsgLen;
172 
173     //
174     //! This is a pointer to the message object's data.
175     //
176     uint8_t *pui8MsgData;
177 }
178 tCANMsgObject;
179 
180 //*****************************************************************************
181 //
182 //! This structure is used for encapsulating the values associated with setting
183 //! up the bit timing for a CAN controller.  The structure is used when calling
184 //! the CANGetBitTiming and CANSetBitTiming functions.
185 //
186 //*****************************************************************************
187 typedef struct
188 {
189     //
190     //! This value holds the sum of the Synchronization, Propagation, and Phase
191     //! Buffer 1 segments, measured in time quanta.  The valid values for this
192     //! setting range from 2 to 16.
193     //
194     uint32_t ui32SyncPropPhase1Seg;
195 
196     //
197     //! This value holds the Phase Buffer 2 segment in time quanta.  The valid
198     //! values for this setting range from 1 to 8.
199     //
200     uint32_t ui32Phase2Seg;
201 
202     //
203     //! This value holds the Resynchronization Jump Width in time quanta.  The
204     //! valid values for this setting range from 1 to 4.
205     //
206     uint32_t ui32SJW;
207 
208     //
209     //! This value holds the CAN_CLK divider used to determine time quanta.
210     //! The valid values for this setting range from 1 to 1023.
211     //
212     uint32_t ui32QuantumPrescaler;
213 }
214 tCANBitClkParms;
215 
216 //*****************************************************************************
217 //
218 //! This data type is used to identify the interrupt status register.  This is
219 //! used when calling the CANIntStatus() function.
220 //
221 //*****************************************************************************
222 typedef enum
223 {
224     //
225     //! Read the CAN interrupt status information.
226     //
227     CAN_INT_STS_CAUSE,
228 
229     //
230     //! Read a message object's interrupt status.
231     //
232     CAN_INT_STS_OBJECT
233 }
234 tCANIntStsReg;
235 
236 //*****************************************************************************
237 //
238 //! This data type is used to identify which of several status registers to
239 //! read when calling the CANStatusGet() function.
240 //
241 //*****************************************************************************
242 typedef enum
243 {
244     //
245     //! Read the full CAN controller status.
246     //
247     CAN_STS_CONTROL,
248 
249     //
250     //! Read the full 32-bit mask of message objects with a transmit request
251     //! set.
252     //
253     CAN_STS_TXREQUEST,
254 
255     //
256     //! Read the full 32-bit mask of message objects with new data available.
257     //
258     CAN_STS_NEWDAT,
259 
260     //
261     //! Read the full 32-bit mask of message objects that are enabled.
262     //
263     CAN_STS_MSGVAL
264 }
265 tCANStsReg;
266 
267 //*****************************************************************************
268 //
269 // These definitions are used to specify interrupt sources to CANIntEnable()
270 // and CANIntDisable().
271 //
272 //*****************************************************************************
273 //
274 //! This flag is used to allow a CAN controller to generate error
275 //! interrupts.
276 //
277 #define CAN_INT_ERROR           0x00000008
278 
279 //
280 //! This flag is used to allow a CAN controller to generate status
281 //! interrupts.
282 //
283 #define CAN_INT_STATUS          0x00000004
284 
285 //
286 //! This flag is used to allow a CAN controller to generate any CAN
287 //! interrupts.  If this is not set, then no interrupts are generated
288 //! by the CAN controller.
289 //
290 #define CAN_INT_MASTER          0x00000002
291 
292 //*****************************************************************************
293 //
294 //! This definition is used to determine the type of message object that is
295 //! set up via a call to the CANMessageSet() API.
296 //
297 //*****************************************************************************
298 typedef enum
299 {
300     //
301     //! Transmit message object.
302     //
303     MSG_OBJ_TYPE_TX,
304 
305     //
306     //! Transmit remote request message object
307     //
308     MSG_OBJ_TYPE_TX_REMOTE,
309 
310     //
311     //! Receive message object.
312     //
313     MSG_OBJ_TYPE_RX,
314 
315     //
316     //! Receive remote request message object.
317     //
318     MSG_OBJ_TYPE_RX_REMOTE,
319 
320     //
321     //! Remote frame receive remote, with auto-transmit message object.
322     //
323     MSG_OBJ_TYPE_RXTX_REMOTE
324 }
325 tMsgObjType;
326 
327 //*****************************************************************************
328 //
329 // The following enumeration contains all error or status indicators that can
330 // be returned when calling the CANStatusGet() function.
331 //
332 //*****************************************************************************
333 //
334 //! CAN controller has entered a Bus Off state.
335 //
336 #define CAN_STATUS_BUS_OFF      0x00000080
337 
338 //
339 //! CAN controller error level has reached warning level.
340 //
341 #define CAN_STATUS_EWARN        0x00000040
342 
343 //
344 //! CAN controller error level has reached error passive level.
345 //
346 #define CAN_STATUS_EPASS        0x00000020
347 
348 //
349 //! A message was received successfully since the last read of this status.
350 //
351 #define CAN_STATUS_RXOK         0x00000010
352 
353 //
354 //! A message was transmitted successfully since the last read of this
355 //! status.
356 //
357 #define CAN_STATUS_TXOK         0x00000008
358 
359 //
360 //! This is the mask for the last error code field.
361 //
362 #define CAN_STATUS_LEC_MSK      0x00000007
363 
364 //
365 //! There was no error.
366 //
367 #define CAN_STATUS_LEC_NONE     0x00000000
368 
369 //
370 //! A bit stuffing error has occurred.
371 //
372 #define CAN_STATUS_LEC_STUFF    0x00000001
373 
374 //
375 //! A formatting error has occurred.
376 //
377 #define CAN_STATUS_LEC_FORM     0x00000002
378 
379 //
380 //! An acknowledge error has occurred.
381 //
382 #define CAN_STATUS_LEC_ACK      0x00000003
383 
384 //
385 //! The bus remained a bit level of 1 for longer than is allowed.
386 //
387 #define CAN_STATUS_LEC_BIT1     0x00000004
388 
389 //
390 //! The bus remained a bit level of 0 for longer than is allowed.
391 //
392 #define CAN_STATUS_LEC_BIT0     0x00000005
393 
394 //
395 //! A CRC error has occurred.
396 //
397 #define CAN_STATUS_LEC_CRC      0x00000006
398 
399 //
400 //! This is the mask for the CAN Last Error Code (LEC).
401 //
402 #define CAN_STATUS_LEC_MASK     0x00000007
403 
404 //*****************************************************************************
405 //
406 // Close the Doxygen group.
407 //! @}
408 //
409 //*****************************************************************************
410 
411 //*****************************************************************************
412 //
413 // API Function prototypes
414 //
415 //*****************************************************************************
416 extern void CANBitTimingGet(uint32_t ui32Base, tCANBitClkParms *psClkParms);
417 extern void CANBitTimingSet(uint32_t ui32Base, tCANBitClkParms *psClkParms);
418 extern uint32_t CANBitRateSet(uint32_t ui32Base, uint32_t ui32SourceClock,
419                               uint32_t ui32BitRate);
420 extern void CANDisable(uint32_t ui32Base);
421 extern void CANEnable(uint32_t ui32Base);
422 extern bool CANErrCntrGet(uint32_t ui32Base, uint32_t *pui32RxCount,
423                           uint32_t *pui32TxCount);
424 extern void CANInit(uint32_t ui32Base);
425 extern void CANIntClear(uint32_t ui32Base, uint32_t ui32IntClr);
426 extern void CANIntDisable(uint32_t ui32Base, uint32_t ui32IntFlags);
427 extern void CANIntEnable(uint32_t ui32Base, uint32_t ui32IntFlags);
428 extern void CANIntRegister(uint32_t ui32Base, void (*pfnHandler)(void));
429 extern uint32_t CANIntStatus(uint32_t ui32Base, tCANIntStsReg eIntStsReg);
430 extern void CANIntUnregister(uint32_t ui32Base);
431 extern void CANMessageClear(uint32_t ui32Base, uint32_t ui32ObjID);
432 extern void CANMessageGet(uint32_t ui32Base, uint32_t ui32ObjID,
433                           tCANMsgObject *psMsgObject, bool bClrPendingInt);
434 extern void CANMessageSet(uint32_t ui32Base, uint32_t ui32ObjID,
435                           tCANMsgObject *psMsgObject, tMsgObjType eMsgType);
436 extern bool CANRetryGet(uint32_t ui32Base);
437 extern void CANRetrySet(uint32_t ui32Base, bool bAutoRetry);
438 extern uint32_t CANStatusGet(uint32_t ui32Base, tCANStsReg eStatusReg);
439 
440 //*****************************************************************************
441 //
442 // Mark the end of the C bindings section for C++ compilers.
443 //
444 //*****************************************************************************
445 #ifdef __cplusplus
446 }
447 #endif
448 
449 #endif // __DRIVERLIB_CAN_H__
450