1 # Copyright (c) 2019-2020 NVIDIA CORPORATION. All rights reserved.
3 @page canbus_usecase1 Receiving and Sending Data
5 @note SW Release Applicability: This tutorial is applicable to modules in both **NVIDIA DriveWorks** and **NVIDIA DRIVE Software** releases.
7 ## CAN Bus Sensor Module workflow
11 Initialization of the CAN bus sensor is achieved as follows:
14 dwSensorParams params{};
15 params.protocol = "can.socket";
16 params.parameters = "device=can0";
18 dwSensorHandle_t canSensor;
19 dwSAL_createSensor(&canSensor, params, ...);
23 The `dwSensorParams.protocol` expects one of the following can interfaces:
25 - `can.socket` interface for system CAN bus.
27 - `can.aurix` interface for CAN over Aurix.
29 - `can.virtual` interface for reading CAN data from binary logs.
31 #### Sensor parameters
32 The `dwSensorParams.parameters` accepts the following parameters:
36 `filter=id:mask[+id:mask]`:
38 - `filter` is a string of `+` seperated filters
40 - `mask` is a hex value
42 Example: `filter=123:FFF+60A:FFF`
44 See the [Setting message filters](#setting-message-filters) section to see how these values are used.
46 ##### When using `can.socket`:
48 `device=can0[,fifo-size=1024]`
50 - `device` CAN device interface, usually canx, x starting from 0
51 - on Linux, use `ifconfig` to list available CAN interfaces
52 - on QNX, use `pidin a | grep nvcan` to list available CAN interfaces
54 - `fifo-size` size of internal buffer, in number of `dwCANMessage` stored.
56 ##### When using `can.aurix`:
58 `ip=10.0.0.1,bus={a,b,c,d,e,f}[,aport=50000,bport=60395],[config-file=/path/to/EasyCanConfigFile.conf][,fifo-size=1024]`
60 - `ip` IP of Aurix (Drive AGX: `10.42.0.146`).
62 - `aport` port on which CAN messages are sent to Aurix.
63 - `bport` port on which CAN messages are received from Aurix.
64 - `config-file` path to EasyCanConfigFile.conf, when provided, configures
65 Aurix EasyCAN message routing when starting sensor. An example is provided
66 in the DriveWorks data folder, under /path/to/data/samples/sensors/can.
67 - `fifo-size` see above.
69 ##### When using `can.virtual`:
71 `file=/path/to/file.can[,create_seek,default_timeout_us,time-offset=0]`
73 - `file` path to CAN bus recording
74 - `create_seek` create seek table
75 - `default_timeout_us` period after which sensor times out
76 - `time-offset` offset applied to CAN messages timestamps
78 The CAN buses are setup with CAN FD if supported. If not supported sending
79 and receiving CAN FD messages will fail.
81 ### Starting, stopping and releasing sensor
83 The CAN bus sensor can be started, stopped and released as follows:
86 dwSensor_start(canSensor);
87 dwSensor_stop(canSensor);
88 dwSAL_releaseSensor(&canSensor);
91 ### Reading CAN messages
93 CAN messages can be read from the bus as follows:
97 dwStatus status = dwSensorCAN_readMessage(&msg, 10000, canSensor);
100 This will populate a `dwCANMessage` structure with the next CAN message
101 received on the bus, in this example waiting up to 10000us, or 10ms, for
102 a message to arrive. Timeout can be set to `::DW_TIMEOUT_INFINITE` to
103 indicate infinite waiting until new message arrives, or 0 to immediately
104 return and only read messages already received. The `dwCANMessage`
105 structure can hold up to 64 bytes to accommodate CAN FD messages.
107 To only read messages available in the internal queue, without reading
108 new messages on the CAN bus, the `dwSensorCAN_popMessage()` method can be
109 used. Together with `dwSensorCAN_processRawData()`, raw binary CAN data
110 can be read and parsed in `dwCANMessage` format, see @ref canbus_usecase2.
112 ### Sending CAN messages
114 CAN messages can be sent on the bus as follows:
124 dwSensorCAN_sendMessage(&msg, 10000, canSensor);
127 This sends a message over the CAN bus within a specified timeout. A message
128 is guaranteed to have been sent when this method returns `DW_SUCCESS`.
129 The method can block for up-to the specified amount of time if a bus is
130 blocked or previous `dwSensorCAN_readMessage()` operation has not yet
133 If the `msg.size` value is from 9 - 64, then the message will be sent as
136 #### Sending CAN messages over Aurix
138 As of software version 3.01.3, the Aurix is configured to cyclically send
139 CAN messages with IDs 0x10f to 0x114 on buses A through F. These message
140 IDs should be considered as reserved as this behavior cannot be disabled
141 at run-time. Please refer to the Elektrobit Aurix Software User Guide for
144 #### Sending CAN with FD (Flexible Data-Rate) and BRS (Bit Rate Switch)
146 As of software version 3.05.4, the Aurix can send CAN FD messages with BRS.
147 Note that this is configured to use timing sample points of 0.8. Devices
148 on the network must be setup using this timing or bus errors can occur.
149 Please refer to Drive Developer Guide, Flashing Aurix documentation for more information.
151 ### Setting message filters
153 CAN messages be filtered by ID as follows:
156 uint32_t ids[2] = {0x100, 0x6AB};
157 uint32_t masks[2] = {0xF00, 0xFFF};
159 dwSensorCAN_setMessageFilter(&ids, &masks, 2, canSensor);
162 This function takes an array of IDs and an array of masks to setup filters.
163 Once the filters are set, only messages with IDs matching the filters
164 will be read. Each call to the above will reset previously set filters. To
165 reset all filtering, call the above with 0 as number of filters.
167 Internally, only messages that meet the following condition for at least
171 (messageID & mask) == (id & mask)
174 Masks are optional; when passing `NULL`, a default mask of `1FFFFFFF`
177 #### Setting message routing on Aurix
179 A message routing configuration has to be sent to Aurix prior to running
180 the application in order to receive, over Aurix, select CAN messages on
181 the desired Xavier device(s). This can be done by providing an EasyCAN
182 configuration file when creating the CAN bus sensor for `can.aurix` protocol,
183 by specifying the path to an Aurix EasyCAN configuration file with the
184 `config-file` argument. The configuration file is a text file containing
185 the following structures, one per CAN message to be routed:
189 CtrlId : EC_CAN_BUS_E
190 FrmTyp : CAN_ID_STANDARD
191 DestId : TEGRA_DEVICE_A
192 DestSwc : SWC_ID_CANDATA
196 - `MsgId` is the message ID to be routed, in hexadecimal
197 - `CtrlId` is the EasyCAN bus on which the message is received, in range
198 `EC_CAN_BUS_A` to `EC_CAN_BUS_F`
199 - `FrmTyp` is the CAN ID type selector, either `CAN_ID_STANDARD` or `CAN_ID_EXTENDED`
200 - `DestId` is the destination device, either `TEGRA_DEVICE_A` or `TEGRA_DEVICE_B`
201 - `DestSwc` is set to `SWC_ID_CANDATA` for CAN data
203 Each structure, corresponding to one CAN message to be routed, is separated
204 by an empty line from the next. An example file is provided in the DriveWorks
205 data folder, under /path/to/data/samples/sensors/can.
207 Please refer to the Elektrobit Aurix Software User Guide provided as part of
208 PDK documentation for more information on the EasyCAN message routing feature
209 and on how to configure Aurix.
211 ### Setting hardware timestamping
213 The `dwSensorCAN_setUseHwTimestamps()` function allows the user to enable
214 or disable hardware timestamping of messages. By default, hardware timestamping
215 is enabled, but can be turned off if the default behavior is not working properly
216 with the current hardware stack.
219 The `dwSensorCAN_readMessage()` function reads the next available CAN message
220 with a given timeout. The function call is blocking, until a CAN message
221 is available or the timeout is exceeded. Filters can be set with
222 `dwSensorCAN_setMessageFilter()` so that only specific messages are read
223 from the bus. Messages can be sent with `dwSensorCAN_sendMessage()`.
225 The `dwCANMessage` structure holds a CAN message, consisting of a timestamp,
226 an ID, message data (payload) and size. The CAN bus module supports the
227 extended CAN frame format.
231 The following code shows the general structure of a program setting up
232 CAN message filters and receiving/sending messages on the bus. Only messages
233 with IDs `0x100`-`0x1FF` and `0x6AB` will be read.
236 dwSensorParams params{};
237 params.protocol = "can.socket";
238 params.parameters = "device=can0";
240 dwSensorHandle_t canSensor;
241 dwSAL_createSensor(&canSensor, params, ...);
243 uint32_t ids[2] = {0x100, 0x6AB};
244 uint32_t masks[2] = {0xF00, 0xFFF};
246 dwSensorCAN_setMessageFilter(&ids, &masks, 2, canSensor);
248 dwSensor_start(canSensor);
254 // read all messages until internal queue is empty
255 while(dwSensorCAN_readMessage(&msg, 0, canSensor) == DW_SUCCESS)
257 // use received can message
260 // send message, e.g. asking for more data
266 // wait for 10ms at most if bus is busy
267 dwSensorCAN_sendMessage(&msg, 10000, canSensor);
270 dwSensor_stop(canSensor);
271 dwSAL_releaseSensor(&canSensor);
275 For more information, refer to @ref dwx_canbus_logger_sample.