DriveWorks SDK Reference
3.0.4260 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`:
57 
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]`
59 
60 - `ip` IP of Aurix (Drive AGX: `10.42.0.146`).
61 - `bus` EasyCAN bus.
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.
68 
69 ##### When using `can.virtual`:
70 
71 `file=/path/to/file.can[,create_seek,default_timeout_us,time-offset=0]`
72 
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
77 
78 The CAN buses are setup with CAN FD if supported. If not supported sending
79 and receiving CAN FD messages will fail.
80 
81 ### Starting, stopping and releasing sensor
82 
83 The CAN bus sensor can be started, stopped and released as follows:
84 
85 ```{.cpp}
86 dwSensor_start(canSensor);
87 dwSensor_stop(canSensor);
88 dwSAL_releaseSensor(&canSensor);
89 ```
90 
91 ### Reading CAN messages
92 
93 CAN messages can be read from the bus as follows:
94 
95 ```{.cpp}
96 dwCANMessage msg{};
97 dwStatus status = dwSensorCAN_readMessage(&msg, 10000, canSensor);
98 ```
99 
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.
106 
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.
111 
112 ### Sending CAN messages
113 
114 CAN messages can be sent on the bus as follows:
115 
116 ```{.cpp}
117 dwCANMessage msg{};
118 
119 msg.id = 0x123;
120 msg.data[0] = 0xFF;
121 msg.size = 1;
122 msg.timestamp = 0;
123 
124 dwSensorCAN_sendMessage(&msg, 10000, canSensor);
125 ```
126 
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
131 finished.
132 
133 If the `msg.size` value is from 9 - 64, then the message will be sent as
134 a CAN FD message.
135 
136 #### Sending CAN messages over Aurix
137 
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
142 more details.
143 
144 #### Sending CAN with FD (Flexible Data-Rate) and BRS (Bit Rate Switch)
145 
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.
150 
151 ### Setting message filters
152 
153 CAN messages be filtered by ID as follows:
154 
155 ```{.cpp}
156 uint32_t ids[2] = {0x100, 0x6AB};
157 uint32_t masks[2] = {0xF00, 0xFFF};
158 
159 dwSensorCAN_setMessageFilter(&ids, &masks, 2, canSensor);
160 ```
161 
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.
166 
167 Internally, only messages that meet the following condition for at least
168 one filter are read:
169 
170 ```{.cpp}
171 (messageID & mask) == (id & mask)
172 ```
173 
174 Masks are optional; when passing `NULL`, a default mask of `1FFFFFFF`
175 is used.
176 
177 #### Setting message routing on Aurix
178 
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:
186 
187 ```
188 MsgId : 0x140
189 CtrlId : EC_CAN_BUS_E
190 FrmTyp : CAN_ID_STANDARD
191 DestId : TEGRA_DEVICE_A
192 DestSwc : SWC_ID_CANDATA
193 ```
194 
195 Where:
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
202 
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.
206 
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.
210 
211 ### Setting hardware timestamping
212 
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.
217 
218 
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()`.
224 
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.
228 
229 ## Example
230 
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.
234 
235 ```{.cpp}
236 dwSensorParams params{};
237 params.protocol = "can.socket";
238 params.parameters = "device=can0";
239 
240 dwSensorHandle_t canSensor;
241 dwSAL_createSensor(&canSensor, params, ...);
242 
243 uint32_t ids[2] = {0x100, 0x6AB};
244 uint32_t masks[2] = {0xF00, 0xFFF};
245 
246 dwSensorCAN_setMessageFilter(&ids, &masks, 2, canSensor);
247 
248 dwSensor_start(canSensor);
249 
250 while(loop)
251 {
252  dwCANMessage msg{};
253 
254  // read all messages until internal queue is empty
255  while(dwSensorCAN_readMessage(&msg, 0, canSensor) == DW_SUCCESS)
256  {
257  // use received can message
258  }
259 
260  // send message, e.g. asking for more data
261  msg.id = 0xAAA;
262  msg.data[0] = 0xFF;
263  msg.size = 1;
264  msg.timestamp = 0;
265 
266  // wait for 10ms at most if bus is busy
267  dwSensorCAN_sendMessage(&msg, 10000, canSensor);
268 }
269 
270 dwSensor_stop(canSensor);
271 dwSAL_releaseSensor(&canSensor);
272 
273 ```
274 
275 For more information, refer to @ref dwx_canbus_logger_sample.