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