1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2017 Denis Shienkov <denis.shienkov@gmail.com> |
4 | ** Copyright (C) 2017 The Qt Company Ltd. |
5 | ** Contact: http://www.qt.io/licensing/ |
6 | ** |
7 | ** This file is part of the QtSerialBus module of the Qt Toolkit. |
8 | ** |
9 | ** $QT_BEGIN_LICENSE:LGPL3$ |
10 | ** Commercial License Usage |
11 | ** Licensees holding valid commercial Qt licenses may use this file in |
12 | ** accordance with the commercial license agreement provided with the |
13 | ** Software or, alternatively, in accordance with the terms contained in |
14 | ** a written agreement between you and The Qt Company. For licensing terms |
15 | ** and conditions see http://www.qt.io/terms-conditions. For further |
16 | ** information use the contact form at http://www.qt.io/contact-us. |
17 | ** |
18 | ** GNU Lesser General Public License Usage |
19 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
20 | ** General Public License version 3 as published by the Free Software |
21 | ** Foundation and appearing in the file LICENSE.LGPLv3 included in the |
22 | ** packaging of this file. Please review the following information to |
23 | ** ensure the GNU Lesser General Public License version 3 requirements |
24 | ** will be met: https://www.gnu.org/licenses/lgpl.html. |
25 | ** |
26 | ** GNU General Public License Usage |
27 | ** Alternatively, this file may be used under the terms of the GNU |
28 | ** General Public License version 2.0 or later as published by the Free |
29 | ** Software Foundation and appearing in the file LICENSE.GPL included in |
30 | ** the packaging of this file. Please review the following information to |
31 | ** ensure the GNU General Public License version 2.0 requirements will be |
32 | ** met: http://www.gnu.org/licenses/gpl-2.0.html. |
33 | ** |
34 | ** $QT_END_LICENSE$ |
35 | ** |
36 | ****************************************************************************/ |
37 | |
38 | #ifndef TINYCAN_SYMBOLS_P_H |
39 | #define TINYCAN_SYMBOLS_P_H |
40 | |
41 | // |
42 | // W A R N I N G |
43 | // ------------- |
44 | // |
45 | // This file is not part of the Qt API. It exists purely as an |
46 | // implementation detail. This header file may change from version to |
47 | // version without notice, or even be removed. |
48 | // |
49 | // We mean it. |
50 | // |
51 | |
52 | #ifdef LINK_LIBMHSTCAN |
53 | |
54 | extern "C" |
55 | { |
56 | #include <can_types.h> |
57 | #include <can_drv.h> |
58 | #include <can_drv_ex.h> |
59 | } |
60 | |
61 | #else |
62 | |
63 | #include <QtCore/qlibrary.h> |
64 | #include <QtCore/qstring.h> |
65 | #include <QtCore/qdebug.h> |
66 | |
67 | #ifdef Q_OS_WIN32 |
68 | #include <windows.h> |
69 | #define DRV_CALLBACK_TYPE WINAPI |
70 | #else |
71 | #define DRV_CALLBACK_TYPE |
72 | #endif |
73 | |
74 | #define INDEX_INVALID 0xFFFFFFFF |
75 | |
76 | #define INDEX_FIFO_PUFFER_MASK 0x0000FFFF |
77 | #define INDEX_SOFT_FLAG 0x02000000 |
78 | #define INDEX_RXD_TXT_FLAG 0x01000000 |
79 | #define INDEX_CAN_KANAL_MASK 0x000F0000 |
80 | #define INDEX_CAN_DEVICE_MASK 0x00F00000 |
81 | |
82 | #define INDEX_CAN_KANAL_A 0x00000000 |
83 | #define INDEX_CAN_KANAL_B 0x00010000 |
84 | |
85 | // CAN Message Type |
86 | #define MsgFlags Flags.Long |
87 | #define MsgLen Flags.Flag.Len |
88 | #define MsgRTR Flags.Flag.RTR |
89 | #define MsgEFF Flags.Flag.EFF |
90 | #define MsgTxD Flags.Flag.TxD |
91 | #define MsgErr Flags.Flag.Error |
92 | #define MsgSource Flags.Flag.Source |
93 | #define MsgData Data.Bytes |
94 | |
95 | struct TCanFlagsBits |
96 | { |
97 | unsigned Len:4; // DLC -> data length 0 - 8 byte |
98 | unsigned TxD:1; // TxD -> 1 = Tx CAN Message, 0 = Rx CAN Message |
99 | unsigned Error:1; // Error -> 1 = bus CAN error message |
100 | unsigned RTR:1; // Remote Transmission Request bit -> If message RTR marks |
101 | unsigned EFF:1; // Extended Frame Format bit -> 1 = 29 Bit Id's, 0 = 11 Bit Id's |
102 | unsigned Source:8; // Source of the message (Device) |
103 | }; |
104 | |
105 | #pragma pack(push, 1) |
106 | union TCanFlags |
107 | { |
108 | struct TCanFlagsBits Flag; |
109 | quint32 Long; |
110 | }; |
111 | #pragma pack(pop) |
112 | |
113 | #pragma pack(push, 1) |
114 | union TCanData |
115 | { |
116 | char Chars[8]; |
117 | quint8 Bytes[8]; |
118 | quint16 Words[4]; |
119 | quint32 Longs[2]; |
120 | }; |
121 | #pragma pack(pop) |
122 | |
123 | #pragma pack(push, 1) |
124 | struct TTime |
125 | { |
126 | quint32 Sec; |
127 | quint32 USec; |
128 | }; |
129 | #pragma pack(pop) |
130 | |
131 | #pragma pack(push, 1) |
132 | struct TCanMsg |
133 | { |
134 | quint32 Id; |
135 | union TCanFlags Flags; |
136 | union TCanData Data; |
137 | struct TTime Time; |
138 | }; |
139 | #pragma pack(pop) |
140 | |
141 | // CAN Message Filter Type |
142 | #define FilFlags Flags.Long |
143 | #define FilRTR Flags.Flag.RTR |
144 | #define FilEFF Flags.Flag.EFF |
145 | #define FilMode Flags.Flag.Mode |
146 | #define FilIdMode Flags.Flag.IdMode |
147 | #define FilEnable Flags.Flag.Enable |
148 | |
149 | struct TMsgFilterFlagsBits |
150 | { |
151 | // 1. byte |
152 | unsigned Len:4; // DLC |
153 | unsigned Res:2; // Reserved |
154 | unsigned RTR:1; // Remote Transmit Request bit |
155 | unsigned EFF:1; // Extended Frame Format bit |
156 | // 2. byte |
157 | unsigned IdMode:2; // 0 = Mask & Code; 1 = Start & Sstop; 2 = Single Id |
158 | unsigned DLCCheck:1; |
159 | unsigned DataCheck:1; |
160 | unsigned Res1:4; |
161 | // 3. byte |
162 | unsigned Res2:8; |
163 | // 4. byte |
164 | unsigned Type:4; // 0 = Single buffer |
165 | unsigned Res3:2; |
166 | unsigned Mode:1; // 0 = Delete message; 1 = Don't delete message |
167 | unsigned Enable:1; // 0 = Close filter; 1 = Release filter |
168 | }; |
169 | |
170 | #pragma pack(push, 1) |
171 | union TMsgFilterFlags |
172 | { |
173 | struct TMsgFilterFlagsBits Flag; |
174 | quint32 Long; |
175 | }; |
176 | #pragma pack(pop) |
177 | |
178 | #pragma pack(push, 1) |
179 | struct TMsgFilter |
180 | { |
181 | quint32 Maske; |
182 | quint32 Code; |
183 | union TMsgFilterFlags Flags; |
184 | union TCanData Data; |
185 | }; |
186 | #pragma pack(pop) |
187 | |
188 | // CAN speed |
189 | #define CAN_10K_BIT 10 // 10 kBit/s |
190 | #define CAN_20K_BIT 20 // 20 kBit/s |
191 | #define CAN_50K_BIT 50 // 50 kBit/s |
192 | #define CAN_100K_BIT 100 // 100 kBit/s |
193 | #define CAN_125K_BIT 125 // 125 kBit/s |
194 | #define CAN_250K_BIT 250 // 250 kBit/s |
195 | #define CAN_500K_BIT 500 // 500 kBit/s |
196 | #define CAN_800K_BIT 800 // 800 kBit/s |
197 | #define CAN_1M_BIT 1000 // 1 MBit/s |
198 | |
199 | // CAN bus mode |
200 | #define OP_CAN_NO_CHANGE 0 // Current state no change |
201 | #define OP_CAN_START 1 // Starts CAN-Bus |
202 | #define OP_CAN_STOP 2 // Stops CAN-Bus |
203 | #define OP_CAN_RESET 3 // Reset CAN Controller (clearing BusOff) |
204 | #define OP_CAN_START_LOM 4 // Starts the CAN-Bus in Silent Mode (Listen Only Mode) |
205 | #define OP_CAN_START_NO_RETRANS 5 // Starts the CAN-Bus in Automatic Retransmission disable Mode |
206 | |
207 | |
208 | #define CAN_CMD_NONE 0x0000 |
209 | #define CAN_CMD_RXD_OVERRUN_CLEAR 0x0001 |
210 | #define CAN_CMD_RXD_FIFOS_CLEAR 0x0002 |
211 | #define CAN_CMD_TXD_OVERRUN_CLEAR 0x0004 |
212 | #define CAN_CMD_TXD_FIFOS_CLEAR 0x0008 |
213 | #define CAN_CMD_HW_FILTER_CLEAR 0x0010 |
214 | #define CAN_CMD_SW_FILTER_CLEAR 0x0020 |
215 | #define CAN_CMD_TXD_PUFFERS_CLEAR 0x0040 |
216 | |
217 | #define CAN_CMD_ALL_CLEAR 0x0FFF |
218 | |
219 | // Driver status |
220 | #define DRV_NOT_LOAD 0 // Driver DLL was not loaded |
221 | #define DRV_STATUS_NOT_INIT 1 // Driver was not initialised |
222 | #define DRV_STATUS_INIT 2 // Driver was successfully initialised |
223 | #define DRV_STATUS_PORT_NOT_OPEN 3 // The port is not open |
224 | #define DRV_STATUS_PORT_OPEN 4 // The port was opened |
225 | #define DRV_STATUS_DEVICE_FOUND 5 // The connection to the hardware was established |
226 | #define DRV_STATUS_CAN_OPEN 6 // The device was opened and initialised |
227 | #define DRV_STATUS_CAN_RUN_TX 7 // CAN Bus RUN Transmitter (not used) |
228 | #define DRV_STATUS_CAN_RUN 8 // CAN Bus RUN |
229 | |
230 | // CAN status |
231 | #define CAN_STATUS_OK 0 // CAN-Controller: Ok |
232 | #define CAN_STATUS_ERROR 1 // CAN-Controller: CAN Error |
233 | #define CAN_STATUS_WARNING 2 // CAN-Controller: Error warning |
234 | #define CAN_STATUS_PASSIV 3 // CAN-Controller: Error passive |
235 | #define CAN_STATUS_BUS_OFF 4 // CAN-Controller: Bus Off |
236 | #define CAN_STATUS_UNBEKANNT 5 // CAN-Controller: State unknown |
237 | |
238 | // FIFO status |
239 | #define FIFO_OK 0 // Fifo-Status: Ok |
240 | #define FIFO_HW_OVERRUN 1 // Fifo-Status: Hardware Fifo overflow |
241 | #define FIFO_SW_OVERRUN 2 // Fifo-Status: Software Fifo overflow |
242 | #define FIFO_HW_SW_OVERRUN 3 // Fifo-Status: Hardware & Software Fifo overflow |
243 | #define FIFO_STATUS_UNBEKANNT 4 // Fifo-Status: Unknown |
244 | |
245 | // Macro for SetEvent |
246 | #define EVENT_ENABLE_PNP_CHANGE 0x0001 |
247 | #define EVENT_ENABLE_STATUS_CHANGE 0x0002 |
248 | #define EVENT_ENABLE_RX_FILTER_MESSAGES 0x0004 |
249 | #define EVENT_ENABLE_RX_MESSAGES 0x0008 |
250 | #define EVENT_ENABLE_ALL 0x00FF |
251 | |
252 | #define EVENT_DISABLE_PNP_CHANGE 0x0100 |
253 | #define EVENT_DISABLE_STATUS_CHANGE 0x0200 |
254 | #define EVENT_DISABLE_RX_FILTER_MESSAGES 0x0400 |
255 | #define EVENT_DISABLE_RX_MESSAGES 0x0800 |
256 | #define EVENT_DISABLE_ALL 0xFF00 |
257 | |
258 | #pragma pack(push, 1) |
259 | struct TDeviceStatus |
260 | { |
261 | quint32 DrvStatus; |
262 | quint8 CanStatus; |
263 | quint8 FifoStatus; |
264 | }; |
265 | #pragma pack(pop) |
266 | |
267 | typedef void (DRV_CALLBACK_TYPE *CanPnPEventCallback)( |
268 | quint32 index, |
269 | qint32 status |
270 | ); |
271 | |
272 | typedef void (DRV_CALLBACK_TYPE *CanStatusEventCallback)( |
273 | quint32 index, |
274 | TDeviceStatus *device_status |
275 | ); |
276 | |
277 | typedef void (DRV_CALLBACK_TYPE *CanRxEventCallback)( |
278 | quint32 index, |
279 | TCanMsg *msg, |
280 | qint32 count |
281 | ); |
282 | |
283 | #define GENERATE_SYMBOL_VARIABLE(returnType, symbolName, ...) \ |
284 | typedef returnType (DRV_CALLBACK_TYPE *fp_##symbolName)(__VA_ARGS__); \ |
285 | static fp_##symbolName symbolName; |
286 | |
287 | #define RESOLVE_SYMBOL(symbolName) \ |
288 | symbolName = reinterpret_cast<fp_##symbolName>(mhstcanLibrary->resolve(#symbolName)); \ |
289 | if (!symbolName) \ |
290 | return false; |
291 | |
292 | GENERATE_SYMBOL_VARIABLE(qint32, CanInitDriver, char *) |
293 | GENERATE_SYMBOL_VARIABLE(void, CanDownDriver, void) |
294 | GENERATE_SYMBOL_VARIABLE(qint32, CanSetOptions, char *) |
295 | GENERATE_SYMBOL_VARIABLE(qint32, CanDeviceOpen, quint32, char *) |
296 | GENERATE_SYMBOL_VARIABLE(qint32, CanDeviceClose, quint32) |
297 | GENERATE_SYMBOL_VARIABLE(qint32, CanApplaySettings, quint32) |
298 | GENERATE_SYMBOL_VARIABLE(qint32, CanSetMode, quint32, quint8, quint16) |
299 | GENERATE_SYMBOL_VARIABLE(qint32, CanSet, quint32, quint16, quint16, void *, qint32) |
300 | GENERATE_SYMBOL_VARIABLE(qint32, CanGet, quint32, quint16, quint16, void *, qint32) |
301 | GENERATE_SYMBOL_VARIABLE(qint32, CanTransmit, quint32, TCanMsg *, qint32) |
302 | GENERATE_SYMBOL_VARIABLE(void, CanTransmitClear, quint32) |
303 | GENERATE_SYMBOL_VARIABLE(quint32, CanTransmitGetCount, quint32) |
304 | GENERATE_SYMBOL_VARIABLE(qint32, CanTransmitSet, quint32, quint16, quint32) |
305 | GENERATE_SYMBOL_VARIABLE(qint32, CanReceive, quint32, TCanMsg *, qint32) |
306 | GENERATE_SYMBOL_VARIABLE(void, CanReceiveClear, quint32) |
307 | GENERATE_SYMBOL_VARIABLE(quint32, CanReceiveGetCount, quint32) |
308 | GENERATE_SYMBOL_VARIABLE(qint32, CanSetSpeed, quint32, quint16) |
309 | GENERATE_SYMBOL_VARIABLE(qint32, CanSetSpeedUser, quint32, quint32) |
310 | GENERATE_SYMBOL_VARIABLE(char *, CanDrvInfo, void) |
311 | GENERATE_SYMBOL_VARIABLE(char *, CanDrvHwInfo, quint32) |
312 | GENERATE_SYMBOL_VARIABLE(qint32, CanSetFilter, quint32, TMsgFilter *) |
313 | GENERATE_SYMBOL_VARIABLE(qint32, CanGetDeviceStatus, quint32, TDeviceStatus *) |
314 | GENERATE_SYMBOL_VARIABLE(void, CanSetPnPEventCallback, CanPnPEventCallback) |
315 | GENERATE_SYMBOL_VARIABLE(void, CanSetStatusEventCallback, CanStatusEventCallback) |
316 | GENERATE_SYMBOL_VARIABLE(void, CanSetRxEventCallback, CanRxEventCallback) |
317 | GENERATE_SYMBOL_VARIABLE(void, CanSetEvents, quint16) |
318 | GENERATE_SYMBOL_VARIABLE(quint32, CanEventStatus, void) |
319 | |
320 | inline bool resolveTinyCanSymbols(QLibrary *mhstcanLibrary) |
321 | { |
322 | if (!mhstcanLibrary->isLoaded()) { |
323 | mhstcanLibrary->setFileName(QStringLiteral("mhstcan" )); |
324 | if (!mhstcanLibrary->load()) |
325 | return false; |
326 | } |
327 | |
328 | RESOLVE_SYMBOL(CanInitDriver) |
329 | RESOLVE_SYMBOL(CanDownDriver) |
330 | RESOLVE_SYMBOL(CanSetOptions) |
331 | RESOLVE_SYMBOL(CanDeviceOpen) |
332 | RESOLVE_SYMBOL(CanDeviceClose) |
333 | RESOLVE_SYMBOL(CanApplaySettings) |
334 | RESOLVE_SYMBOL(CanSetMode) |
335 | RESOLVE_SYMBOL(CanSet) |
336 | RESOLVE_SYMBOL(CanGet) |
337 | RESOLVE_SYMBOL(CanTransmit) |
338 | RESOLVE_SYMBOL(CanTransmitClear) |
339 | RESOLVE_SYMBOL(CanTransmitGetCount) |
340 | RESOLVE_SYMBOL(CanTransmitSet) |
341 | RESOLVE_SYMBOL(CanReceive) |
342 | RESOLVE_SYMBOL(CanReceiveClear) |
343 | RESOLVE_SYMBOL(CanReceiveGetCount) |
344 | RESOLVE_SYMBOL(CanSetSpeed) |
345 | RESOLVE_SYMBOL(CanSetSpeedUser) |
346 | RESOLVE_SYMBOL(CanDrvInfo) |
347 | RESOLVE_SYMBOL(CanDrvHwInfo) |
348 | RESOLVE_SYMBOL(CanSetFilter) |
349 | RESOLVE_SYMBOL(CanGetDeviceStatus) |
350 | RESOLVE_SYMBOL(CanSetPnPEventCallback) |
351 | RESOLVE_SYMBOL(CanSetStatusEventCallback) |
352 | RESOLVE_SYMBOL(CanSetRxEventCallback) |
353 | RESOLVE_SYMBOL(CanSetEvents) |
354 | RESOLVE_SYMBOL(CanEventStatus) |
355 | |
356 | return true; |
357 | } |
358 | |
359 | #endif |
360 | |
361 | #endif // TINYCAN_SYMBOLS_P_H |
362 | |