CAN.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2019, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 /*!*****************************************************************************
33  * @file CAN.h
34  * @brief <b>PRELIMINARY</b> CAN driver interface
35  *
36  * <b>WARNING</b> These APIs are <b>PRELIMINARY</b>, and subject to
37  * change in the next few months.
38  *
39  * To use the CAN driver, ensure that the correct driver library for your
40  * device is linked in and include this header file as follows:
41  * @code
42  * #include <ti/drivers/CAN.h>
43  * @endcode
44  *
45  * This module serves as the main interface for applications. Its purpose
46  * is to redirect the CAN APIs to specific driver implementations
47  * which are specified using a pointer to a #CAN_FxnTable.
48  *
49  * @anchor ti_drivers_CAN_Overview
50  * # Overview #
51  * The Controller Area Network (CAN) driver is a generic driver that allows
52  * for communication on a CAN bus. It is a two-wire, half-duplex, LAN system
53  * that is collision free. The main method of transfer is by broadcasting.
54  * The CAN protocol defines the format of data transfer, and this CAN driver
55  * allows full functionality as a transmitting and receiving node on a bus.
56  * However, there can be higher-level software layers and stacks that use this
57  * driver to enable more advanced features.
58  * Functional modes available in this driver include blocking and non-blocking.
59  *
60  * The APIs in this driver serve as an interface to a typical RTOS
61  * application. The specific peripheral implementations are responsible for
62  * creating all the RTOS specific primitives to allow for thread-safe
63  * operation.
64  *
65  * @anchor ti_drivers_CAN_Usage
66  * # Usage #
67  *
68  * The CAN driver interface provides device independent APIs, data types,
69  * and macros.
70  *
71  * @anchor ti_drivers_CAN_Synopsis
72  * ## Synopsis #
73  * The following code example opens a CAN instance, creates
74  * an incrementing CAN frame, and continually writes them to the CAN bus.
75  * NOTE: a CAN receiver on this bus is needed, or else this transmitter will
76  * continually throw an error if it does not detect an ACK.
77  *
78  * @code
79  * uint8_t i;
80  * // Initialize the CAN driver
81  * CAN_init();
82  *
83  * CAN_Handle canHandle;
84  * CAN_Params canParams;
85  * CAN_Params_init(&canParams);
86  * canHandle = CAN_open(CONFIG_CAN0, &canParams);
87  *
88  * if (canHandle == NULL) {
89  * // CAN_open() failed
90  * while (1);
91  * }
92  *
93  * for (i = 0; ; ++i) {
94  * CAN_Frame canFrame[1];
95  * canFrame[0].can_id = i;
96  * canFrame[0].err = 0;
97  * canFrame[0].rtr = 0;
98  * canFrame[0].eff = 1;
99  * canFrame[0].dlc = i % 9;
100  * canFrame[0].data[0] = i;
101  * canFrame[0].data[1] = i + 1;
102  * canFrame[0].data[2] = i + 2;
103  * canFrame[0].data[3] = i + 3;
104  * canFrame[0].data[4] = i + 4;
105  * canFrame[0].data[5] = i + 5;
106  * canFrame[0].data[6] = i + 6;
107  * canFrame[0].data[7] = i + 7;
108  *
109  * CAN_write(canHandle, canFrame, sizeof(canFrame));
110  * }
111  * @endcode
112  *
113  * Details for the example code above are described in the following
114  * subsections.
115  *
116  *
117  * @anchor ti_drivers_CAN_Configuration
118  * ### CAN Driver Configuration #
119  *
120  * In order to use the CAN APIs, the application is required to
121  * provide device-specific CAN configuration in the ti_drivers_config.c file.
122  * The CAN driver interface defines a configuration data structure:
123  *
124  * @code
125  * typedef struct {
126  * CAN_FxnTable const *fxnTablePtr;
127  * void *object;
128  * void const *hwAttrs;
129  * CAN_Frame *rxBufPtr;
130  * CAN_frame *txBufPtr;
131  * size_t rxBufSize;
132  * size_t txBufSize;
133  * } CAN_Config;
134  * @endcode
135  *
136  * You will need to check the device-specific CAN driver implementation's
137  * header file for example configuration. Please also refer to the
138  * ti_drivers_config.c file to see the CAN configuration.
139  *
140  * ### Initializing the CAN Driver #
141  *
142  * CAN_init() must be called before any other CAN APIs. This function
143  * calls the device implementation's CAN initialization function, for each
144  * element of CAN_config[].
145  *
146  * ### Opening the CAN Driver #
147  *
148  * Opening a CAN requires four steps:
149  * 1. Create and initialize a CAN_Params structure.
150  * 2. Fill in the desired parameters.
151  * 3. Call CAN_open(), passing the index of the CAN in the CAN_config
152  * structure, and the address of the CAN_Params structure. The
153  * CAN instance is specified by the index in the CAN_config structure.
154  * 4. Check that the CAN handle returned by CAN_open() is non-NULL,
155  * and save it. The handle will be used to read and write to the
156  * CAN you just opened.
157  *
158  * Only one CAN index can be used at a time; calling CAN_open() a second
159  * time with the same index previously passed to CAN_open() will result in
160  * an error. You can, though, re-use the index if the instance is closed
161  * via CAN_close().
162  * In the example code, CONFIG_CAN0 is passed to CAN_open(). This macro
163  * is defined in the applications "ti_drivers_config.h" file.
164  *
165  *
166  * ### Modes of Operation #
167  *
168  * The CAN driver can operate in blocking mode or nonblocking mode, by
169  * setting the mode parameters passed to CAN_open().
170  * If these parameters are not set, as in the example code, the CAN
171  * driver defaults to blocking mode. Options for the mode parameter are
172  * #CAN_MODE_BLOCKING and #CAN_MODE_NONBLOCKING:
173  *
174  * - #CAN_MODE_BLOCKING uses a semaphore to block while data is being sent
175  * or read. The context of calling CAN_read() or CAN_write() must be a Task
176  * when using #CAN_MODE_BLOCKING. The CAN_write() or CAN_read() call
177  * will block until all data is sent or received, or the write timeout or
178  * read timeout expires, whichever happens first.
179  *
180  * - #CAN_MODE_NONBLOCKING is non-blocking and CAN_read() and CAN_write()
181  * will return either with the number of bytes successfully read/written,
182  * or a negative error number.
183  *
184  * ### Reading and Writing data #
185  *
186  * The example code reads one CAN frame from the CAN instance, and then writes
187  * one CAN frame back to the same instance:
188  *
189  * @code
190  * CAN_read(can, &canFrame, sizeof(canFrame));
191  * CAN_write(can, &canFrame, sizeof(canFrame));
192  * @endcode
193  *
194  * The CAN driver allows CAN_read() and CAN_write() calls to happen for any
195  * node at any time from the CAN bus. Please see the CAN protocol for how it
196  * handles collisions. The ability to filter incoming messages are also
197  * available through CAN_Params.
198  *
199  * # Implementation #
200  *
201  * The CAN driver interface module is joined (at link time) to an
202  * array of CAN_Config data structures named *CAN_config*.
203  * CAN_config is implemented in the application with each entry being an
204  * instance of a CAN peripheral. Each entry in *CAN_config* contains a:
205  * - (CAN_FxnTable *) to a set of functions that implement a CAN peripheral
206  * - (void *) data object that is associated with the CAN_FxnTable
207  * - (void *) hardware attributes that are associated with the CAN_FxnTable
208  *
209  * The CAN APIs are redirected to the device specific implementations
210  * using the CAN_FxnTable pointer of the CAN_config entry.
211  * In order to use device specific functions of the CAN driver directly,
212  * link in the correct driver library for your device and include the
213  * device specific CAN driver header file (which in turn includes CAN.h).
214  * For example, for the MSP432 family of devices, you would include the
215  * following header file:
216  * @code
217  * #include <ti/drivers/can/CANMSP432.h>
218  * @endcode
219  *
220  * ============================================================================
221  */
222 
223 #ifndef ti_drivers_CAN__include
224 #define ti_drivers_CAN__include
225 
226 #include <stddef.h>
227 #include <stdint.h>
228 
229 #include <ti/drivers/can/types.h>
230 
231 #ifdef __cplusplus
232 extern "C" {
233 #endif
234 
252 #define CAN_CMD_RESERVED (32)
253 
266 #define CAN_STATUS_RESERVED (-32)
267 
281 #define CAN_STATUS_SUCCESS (0)
282 
289 #define CAN_STATUS_ERROR (-1)
290 
298 #define CAN_STATUS_UNDEFINEDCMD (-2)
299 
305 #define CAN_WAIT_FOREVER (~(0U))
306 
310 typedef struct CAN_Config_ *CAN_Handle;
311 
318 typedef enum {
331 } CAN_Mode;
332 
339 typedef enum {
349 } CAN_Direction;
350 
359 typedef struct {
360  CAN_Mode mode;
362  uint32_t filterID;
363  uint32_t filterMask;
364  uint32_t readTimeout;
365  uint32_t writeTimeout;
366 } CAN_Params;
367 
380 typedef struct can_frame CAN_Frame;
381 
386 typedef void (*CAN_CloseFxn) (CAN_Handle handle);
387 
392 typedef int_fast16_t (*CAN_ControlFxn) (CAN_Handle handle, uint_fast16_t cmd, void *arg);
393 
398 typedef void (*CAN_InitFxn) (CAN_Handle handle);
399 
404 typedef CAN_Handle (*CAN_OpenFxn) (CAN_Handle handle, CAN_Params *params);
405 
410 typedef int_fast32_t (*CAN_ReadFxn) (CAN_Handle handle, void *buffer,
411  size_t size);
412 
417 typedef int_fast32_t (*CAN_WriteFxn) (CAN_Handle handle, const void *buffer,
418  size_t size);
419 
424 typedef void (*CAN_TxMsgFxn) (CAN_Handle handle);
425 
431 typedef struct {
434 
437 
440 
443 
446 
449 
452 } CAN_FxnTable;
453 
465 typedef struct CAN_Config_ {
468 
470  void *object;
471 
473  void const *hwAttrs;
474 
477 
480 
482  size_t rxBufSize;
483 
485  size_t txBufSize;
486 } CAN_Config;
487 
498 extern void CAN_close(CAN_Handle handle);
499 
507 extern void CAN_init(void);
508 
542 extern int_fast16_t CAN_control(CAN_Handle handle, uint_fast16_t cmd, void *arg);
543 
565 extern CAN_Handle CAN_open(uint_least8_t index, CAN_Params *params);
566 
580 extern void CAN_Params_init(CAN_Params *params);
581 
610 extern int_fast32_t CAN_write(CAN_Handle handle, const void *buffer, size_t size);
611 
641 extern int_fast32_t CAN_read(CAN_Handle handle, void *buffer, size_t size);
642 
643 #ifdef __cplusplus
644 }
645 #endif
646 
647 #endif /* ti_drivers_CAN__include */
CAN_WriteFxn writeFxn
Definition: CAN.h:448
CAN_ReadFxn readFxn
Definition: CAN.h:445
int_fast32_t CAN_write(CAN_Handle handle, const void *buffer, size_t size)
Function that writes data to a CAN with interrupts enabled.
int_fast32_t(* CAN_ReadFxn)(CAN_Handle handle, void *buffer, size_t size)
A function pointer to a driver specific implementation of CAN_ReadFxn().
Definition: CAN.h:410
int_fast32_t(* CAN_WriteFxn)(CAN_Handle handle, const void *buffer, size_t size)
A function pointer to a driver specific implementation of CAN_WriteFxn().
Definition: CAN.h:417
int_fast16_t CAN_control(CAN_Handle handle, uint_fast16_t cmd, void *arg)
Function performs implementation specific features on a given CAN_Handle.
CAN_ControlFxn controlFxn
Definition: CAN.h:436
void(* CAN_InitFxn)(CAN_Handle handle)
A function pointer to a driver specific implementation of CAN_InitFxn().
Definition: CAN.h:398
void CAN_init(void)
Function to initialize the CAN module.
struct CAN_Config_ * CAN_Handle
A handle that is returned from a CAN_open() call.
Definition: CAN.h:310
CAN_InitFxn initFxn
Definition: CAN.h:439
CAN_TxMsgFxn txMsgFxn
Definition: CAN.h:451
uint32_t readTimeout
Definition: CAN.h:364
int_fast16_t(* CAN_ControlFxn)(CAN_Handle handle, uint_fast16_t cmd, void *arg)
A function pointer to a driver specific implementation of CAN_ControlFxn().
Definition: CAN.h:392
uint32_t filterID
Definition: CAN.h:362
size_t txBufSize
Definition: CAN.h:485
struct CAN_Config_ CAN_Config
CAN Global configuration.
CAN_Handle CAN_open(uint_least8_t index, CAN_Params *params)
Function to initialize a given CAN peripheral.
void * object
Definition: CAN.h:470
CAN Global configuration.
Definition: CAN.h:465
CAN_Direction
CAN communication mode.
Definition: CAN.h:339
void(* CAN_TxMsgFxn)(CAN_Handle handle)
A function pointer to a driver specific implementation of CAN_TxMsgFxn().
Definition: CAN.h:424
void const * hwAttrs
Definition: CAN.h:473
uint32_t writeTimeout
Definition: CAN.h:365
CAN_OpenFxn openFxn
Definition: CAN.h:442
CAN_Frame * txBufPtr
Definition: CAN.h:479
CAN frame structure.
Definition: source/ti/drivers/can/types.h:65
Definition: CAN.h:348
int_fast32_t CAN_read(CAN_Handle handle, void *buffer, size_t size)
Function that reads data from a CAN with interrupt enabled.
size_t rxBufSize
Definition: CAN.h:482
CAN_Mode mode
Definition: CAN.h:360
uint32_t filterMask
Definition: CAN.h:363
CAN_CloseFxn closeFxn
Definition: CAN.h:433
Definition: CAN.h:324
void CAN_Params_init(CAN_Params *params)
Function to initialize the CAN_Params struct to its defaults.
CAN_Frame * rxBufPtr
Definition: CAN.h:476
CAN_Mode
CAN mode settings.
Definition: CAN.h:318
Definition: CAN.h:330
The definition of a CAN function table that contains the required set of functions to control a speci...
Definition: CAN.h:431
void CAN_close(CAN_Handle handle)
Function to close a CAN peripheral specified by the CAN handle.
Definition: CAN.h:343
CAN_Direction direction
Definition: CAN.h:361
CAN_Handle(* CAN_OpenFxn)(CAN_Handle handle, CAN_Params *params)
A function pointer to a driver specific implementation of CAN_OpenFxn().
Definition: CAN.h:404
Definition: CAN.h:341
CAN Parameters.
Definition: CAN.h:359
void(* CAN_CloseFxn)(CAN_Handle handle)
A function pointer to a driver specific implementation of CAN_CloseFxn().
Definition: CAN.h:386
CAN_FxnTable const * fxnTablePtr
Definition: CAN.h:467
© Copyright 1995-2019, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale