DriveWorks SDK Reference
3.5.78 Release
For Test and Development only

sensors/canbus/docs/usecase1.md
Go to the documentation of this file.
1 # Copyright (c) 2019-2020 NVIDIA CORPORATION. All rights reserved.
2 
3 @page canbus_usecase1 Receiving and Sending Data
4 
5 @note SW Release Applicability: This tutorial is applicable to modules in both **NVIDIA DriveWorks** and **NVIDIA DRIVE Software** releases.
6 
7 ## CAN Bus Sensor Module workflow
8 
9 ### Initialization
10 
11 Initialization of the CAN bus sensor is achieved as follows:
12 
13 ```{.cpp}
14  dwSensorParams params{};
15  params.protocol = "can.socket";
16  params.parameters = "device=can0";
17 
18  dwSensorHandle_t canSensor;
19  dwSAL_createSensor(&canSensor, params, ...);
20 ```
21 
22 #### Protocol
23 The `dwSensorParams.protocol` expects one of the following can interfaces:
24 
25 - `can.socket` interface for system CAN bus.
26 
27 - `can.aurix` interface for CAN over Aurix.
28 
29 - `can.virtual` interface for reading CAN data from binary logs.
30 
31 #### Sensor parameters
32 The `dwSensorParams.parameters` accepts the following parameters:
33 
34 ##### For all:
35 
36 `filter=id:mask[+id:mask]`:
37 
38 - `filter` is a string of `+` seperated filters
39 - `id` is a hex value
40 - `mask` is a hex value
41 
42 Example: `filter=123:FFF+60A:FFF`
43 
44 See the [Setting message filters](#setting-message-filters) section to see how these values are used.
45 
46 ##### When using `can.socket`:
47 
48 `device=can0[,fifo-size=1024]`
49 
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
53 
54 - `fifo-size` size of internal buffer, in number of `dwCANMessage` stored.
55 
56 ##### When using `can.aurix` in driveworks 3.5 and above (Aurix version 4.0 and above):
57 
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]`
59 
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.
66 
67 ##### When using `can.aurix` in driveworks 3.0 and below (Aurix version 3.X and below)
68 
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]`
70 
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.
75 - `bus` EasyCAN bus.
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.
79 
80 ##### When using `can.virtual`:
81 
82 `file=/path/to/file.can[,create_seek,default_timeout_us,time-offset=0]`
83 
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
88 
89 The CAN buses are setup with CAN FD if supported. If not supported sending
90 and receiving CAN FD messages will fail.
91 
92 ### Starting, stopping and releasing sensor
93 
94 The CAN bus sensor can be started, stopped and released as follows:
95 
96 ```{.cpp}
97 dwSensor_start(canSensor);
98 dwSensor_stop(canSensor);
99 dwSAL_releaseSensor(&canSensor);
100 ```
101 
102 ### Reading CAN messages
103 
104 CAN messages can be read from the bus as follows:
105 
106 ```{.cpp}
107 dwCANMessage msg{};
108 dwStatus status = dwSensorCAN_readMessage(&msg, 10000, canSensor);
109 ```
110 
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.
117 
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.
122 
123 ### Sending CAN messages
124 
125 CAN messages can be sent on the bus as follows:
126 
127 ```{.cpp}
128 dwCANMessage msg{};
129 
130 msg.id = 0x123;
131 msg.data[0] = 0xFF;
132 msg.size = 1;
133 msg.timestamp = 0;
134 
135 dwSensorCAN_sendMessage(&msg, 10000, canSensor);
136 ```
137 
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
142 finished.
143 
144 If the `msg.size` value is from 9 - 64, then the message will be sent as
145 a CAN FD message.
146 
147 #### Sending CAN messages over Aurix
148 
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
153 more details.
154 
155 #### Sending CAN with FD (Flexible Data-Rate) and BRS (Bit Rate Switch)
156 
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.
161 
162 ### Setting message filters
163 
164 CAN messages be filtered by ID as follows:
165 
166 ```{.cpp}
167 uint32_t ids[2] = {0x100, 0x6AB};
168 uint32_t masks[2] = {0xF00, 0xFFF};
169 
170 dwSensorCAN_setMessageFilter(&ids, &masks, 2, canSensor);
171 ```
172 
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.
177 
178 Internally, only messages that meet the following condition for at least
179 one filter are read:
180 
181 ```{.cpp}
182 (messageID & mask) == (id & mask)
183 ```
184 
185 Masks are optional; when passing `NULL`, a default mask of `1FFFFFFF`
186 is used.
187 
188 #### Setting message routing on Aurix
189 
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:
197 
198 ```
199 MsgId : 0x140
200 CtrlId : EC_CAN_BUS_E
201 FrmTyp : CAN_ID_STANDARD
202 DestId : TEGRA_DEVICE_A
203 DestSwc : SWC_ID_CANDATA
204 ```
205 
206 Where:
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
213 
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.
217 
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.
221 
222 ### Setting hardware timestamping
223 
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.
228 
229 
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()`.
235 
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.
239 
240 ## Example
241 
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.
245 
246 ```{.cpp}
247 dwSensorParams params{};
248 params.protocol = "can.socket";
249 params.parameters = "device=can0";
250 
251 dwSensorHandle_t canSensor;
252 dwSAL_createSensor(&canSensor, params, ...);
253 
254 uint32_t ids[2] = {0x100, 0x6AB};
255 uint32_t masks[2] = {0xF00, 0xFFF};
256 
257 dwSensorCAN_setMessageFilter(&ids, &masks, 2, canSensor);
258 
259 dwSensor_start(canSensor);
260 
261 while(loop)
262 {
263  dwCANMessage msg{};
264 
265  // read all messages until internal queue is empty
266  while(dwSensorCAN_readMessage(&msg, 0, canSensor) == DW_SUCCESS)
267  {
268  // use received can message
269  }
270 
271  // send message, e.g. asking for more data
272  msg.id = 0xAAA;
273  msg.data[0] = 0xFF;
274  msg.size = 1;
275  msg.timestamp = 0;
276 
277  // wait for 10ms at most if bus is busy
278  dwSensorCAN_sendMessage(&msg, 10000, canSensor);
279 }
280 
281 dwSensor_stop(canSensor);
282 dwSAL_releaseSensor(&canSensor);
283 
284 ```
285 
286 For more information, refer to @ref dwx_canbus_logger_sample.