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` in driveworks 3.5 and above (Aurix version 4.0 and above):
58 `ip=10.0.0.1,bus={a,b,c,d,e,f}[,aport=50000,bport=60395],[inIDMap=1:0x106+2:0x206][6,outIDMap=0x105:17+0x205:18][,fifo-size=1024]`
60 - `ip` IP of Aurix (Drive AGX: `10.42.0.146`).
61 - `aport` port on which CAN messages are sent to Aurix.
62 - `bport` port on which CAN messages are received from Aurix.
63 - `fifo-size` see above.
64 - `inIDMap` a mapping of PDU ID to CAN ID in the format pduID:canID seperated by '+'. This should match the security model compiled onto the Aurix. ID values in decimal or hex format with leading 0x. Messages recieved from Aurix with pduID will be mapped to matched canID.
65 - `outIDMap` a mapping of CAN ID to PDU ID in the format canID:pduUD seperated by '+'. This should match the security model compiled onto the Aurix. ID values in decimal or hex format with leading 0x. Messages with canID will be send to Aurix mapped to pduID.
67 ##### When using `can.aurix` in driveworks 3.0 and below (Aurix version 3.X and below)
69 `ip=10.42.0.146,bus={a,b,c,d,e,f}[,aport=50000,bport=60395],[config-file=/path/to/EasyCanConfigFile.conf][,fifo-size=1024]`
71 - `ip` IP of Aurix (Drive AGX: `10.42.0.146`).
72 - `aport` port on which CAN messages are sent to Aurix.
73 - `bport` port on which CAN messages are received from Aurix.
74 - `fifo-size` see above.
76 - `config-file` path to EasyCanConfigFile.conf, when provided, configures
77 Aurix EasyCAN message routing when starting sensor. An example is provided
78 in the DriveWorks data folder, under /path/to/data/samples/sensors/can.
80 ##### When using `can.virtual`:
82 `file=/path/to/file.can[,create_seek,default_timeout_us,time-offset=0]`
84 - `file` path to CAN bus recording
85 - `create_seek` create seek table
86 - `default_timeout_us` period after which sensor times out
87 - `time-offset` offset applied to CAN messages timestamps
89 The CAN buses are setup with CAN FD if supported. If not supported sending
90 and receiving CAN FD messages will fail.
92 ### Starting, stopping and releasing sensor
94 The CAN bus sensor can be started, stopped and released as follows:
97 dwSensor_start(canSensor);
98 dwSensor_stop(canSensor);
99 dwSAL_releaseSensor(&canSensor);
102 ### Reading CAN messages
104 CAN messages can be read from the bus as follows:
108 dwStatus status = dwSensorCAN_readMessage(&msg, 10000, canSensor);
111 This will populate a `dwCANMessage` structure with the next CAN message
112 received on the bus, in this example waiting up to 10000us, or 10ms, for
113 a message to arrive. Timeout can be set to `::DW_TIMEOUT_INFINITE` to
114 indicate infinite waiting until new message arrives, or 0 to immediately
115 return and only read messages already received. The `dwCANMessage`
116 structure can hold up to 64 bytes to accommodate CAN FD messages.
118 To only read messages available in the internal queue, without reading
119 new messages on the CAN bus, the `dwSensorCAN_popMessage()` method can be
120 used. Together with `dwSensorCAN_processRawData()`, raw binary CAN data
121 can be read and parsed in `dwCANMessage` format, see @ref canbus_usecase2.
123 ### Sending CAN messages
125 CAN messages can be sent on the bus as follows:
135 dwSensorCAN_sendMessage(&msg, 10000, canSensor);
138 This sends a message over the CAN bus within a specified timeout. A message
139 is guaranteed to have been sent when this method returns `DW_SUCCESS`.
140 The method can block for up-to the specified amount of time if a bus is
141 blocked or previous `dwSensorCAN_readMessage()` operation has not yet
144 If the `msg.size` value is from 9 - 64, then the message will be sent as
147 #### Sending CAN messages over Aurix
149 As of software version 3.01.3, the Aurix is configured to cyclically send
150 CAN messages with IDs 0x10f to 0x114 on buses A through F. These message
151 IDs should be considered as reserved as this behavior cannot be disabled
152 at run-time. Please refer to the Elektrobit Aurix Software User Guide for
155 #### Sending CAN with FD (Flexible Data-Rate) and BRS (Bit Rate Switch)
157 As of software version 3.05.4, the Aurix can send CAN FD messages with BRS.
158 Note that this is configured to use timing sample points of 0.8. Devices
159 on the network must be setup using this timing or bus errors can occur.
160 Please refer to Drive Developer Guide, Flashing Aurix documentation for more information.
162 ### Setting message filters
164 CAN messages be filtered by ID as follows:
167 uint32_t ids[2] = {0x100, 0x6AB};
168 uint32_t masks[2] = {0xF00, 0xFFF};
170 dwSensorCAN_setMessageFilter(&ids, &masks, 2, canSensor);
173 This function takes an array of IDs and an array of masks to setup filters.
174 Once the filters are set, only messages with IDs matching the filters
175 will be read. Each call to the above will reset previously set filters. To
176 reset all filtering, call the above with 0 as number of filters.
178 Internally, only messages that meet the following condition for at least
182 (messageID & mask) == (id & mask)
185 Masks are optional; when passing `NULL`, a default mask of `1FFFFFFF`
188 #### Setting message routing on Aurix
190 A message routing configuration has to be sent to Aurix prior to running
191 the application in order to receive, over Aurix, select CAN messages on
192 the desired Xavier device(s). This can be done by providing an EasyCAN
193 configuration file when creating the CAN bus sensor for `can.aurix` protocol,
194 by specifying the path to an Aurix EasyCAN configuration file with the
195 `config-file` argument. The configuration file is a text file containing
196 the following structures, one per CAN message to be routed:
200 CtrlId : EC_CAN_BUS_E
201 FrmTyp : CAN_ID_STANDARD
202 DestId : TEGRA_DEVICE_A
203 DestSwc : SWC_ID_CANDATA
207 - `MsgId` is the message ID to be routed, in hexadecimal
208 - `CtrlId` is the EasyCAN bus on which the message is received, in range
209 `EC_CAN_BUS_A` to `EC_CAN_BUS_F`
210 - `FrmTyp` is the CAN ID type selector, either `CAN_ID_STANDARD` or `CAN_ID_EXTENDED`
211 - `DestId` is the destination device, either `TEGRA_DEVICE_A` or `TEGRA_DEVICE_B`
212 - `DestSwc` is set to `SWC_ID_CANDATA` for CAN data
214 Each structure, corresponding to one CAN message to be routed, is separated
215 by an empty line from the next. An example file is provided in the DriveWorks
216 data folder, under /path/to/data/samples/sensors/can.
218 Please refer to the Elektrobit Aurix Software User Guide provided as part of
219 PDK documentation for more information on the EasyCAN message routing feature
220 and on how to configure Aurix.
222 ### Setting hardware timestamping
224 The `dwSensorCAN_setUseHwTimestamps()` function allows the user to enable
225 or disable hardware timestamping of messages. By default, hardware timestamping
226 is enabled, but can be turned off if the default behavior is not working properly
227 with the current hardware stack.
230 The `dwSensorCAN_readMessage()` function reads the next available CAN message
231 with a given timeout. The function call is blocking, until a CAN message
232 is available or the timeout is exceeded. Filters can be set with
233 `dwSensorCAN_setMessageFilter()` so that only specific messages are read
234 from the bus. Messages can be sent with `dwSensorCAN_sendMessage()`.
236 The `dwCANMessage` structure holds a CAN message, consisting of a timestamp,
237 an ID, message data (payload) and size. The CAN bus module supports the
238 extended CAN frame format.
242 The following code shows the general structure of a program setting up
243 CAN message filters and receiving/sending messages on the bus. Only messages
244 with IDs `0x100`-`0x1FF` and `0x6AB` will be read.
247 dwSensorParams params{};
248 params.protocol = "can.socket";
249 params.parameters = "device=can0";
251 dwSensorHandle_t canSensor;
252 dwSAL_createSensor(&canSensor, params, ...);
254 uint32_t ids[2] = {0x100, 0x6AB};
255 uint32_t masks[2] = {0xF00, 0xFFF};
257 dwSensorCAN_setMessageFilter(&ids, &masks, 2, canSensor);
259 dwSensor_start(canSensor);
265 // read all messages until internal queue is empty
266 while(dwSensorCAN_readMessage(&msg, 0, canSensor) == DW_SUCCESS)
268 // use received can message
271 // send message, e.g. asking for more data
277 // wait for 10ms at most if bus is busy
278 dwSensorCAN_sendMessage(&msg, 10000, canSensor);
281 dwSensor_stop(canSensor);
282 dwSAL_releaseSensor(&canSensor);
286 For more information, refer to @ref dwx_canbus_logger_sample.