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