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