/
PeakCanInterface.cpp
295 lines (245 loc) · 7.86 KB
/
PeakCanInterface.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include "PeakCanInterface.h"
#include "PeakCanDriver.h"
#include <core/MeasurementInterface.h>
#include <windows.h>
#include "pcan-basic-api/Include/PCANBasic.h"
#include <QThread>
PeakCanInterface::PeakCanInterface(PeakCanDriver *driver, uint32_t handle)
: CanInterface(driver),
_handle(handle),
_hostTimestampStart(0),
_peakTimestampStart(0)
{
LARGE_INTEGER tps;
QueryPerformanceFrequency(&tps);
_perfTicksPerSecond = tps.QuadPart;
_autoResetEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
_config.autoRestart = true;
_config.listenOnly = false;
_config.bitrate = 100000;
}
PeakCanInterface::~PeakCanInterface()
{
CloseHandle(_autoResetEvent);
}
void PeakCanInterface::update()
{
// TODO: implement me
}
uint32_t PeakCanInterface::getHandle() const
{
return _handle;
}
QString PeakCanInterface::getName() const
{
switch (_handle) {
case PCAN_USBBUS1:
case PCAN_USBBUS2:
case PCAN_USBBUS3:
case PCAN_USBBUS4:
case PCAN_USBBUS5:
case PCAN_USBBUS6:
case PCAN_USBBUS7:
case PCAN_USBBUS8:
return QString("pusb%1").arg(_handle - PCAN_USBBUS1 + 1);
default:
return QString("pcan_%1").arg(_handle);
}
/*
char name[32];
if (CAN_GetValue(_handle, PCAN_HARDWARE_NAME, name, sizeof(name))==PCAN_ERROR_OK) {
return QString(name);
} else {
return "";
}
*/
}
void PeakCanInterface::applyConfig(const MeasurementInterface &mi)
{
_config.autoRestart = mi.doAutoRestart();
_config.listenOnly = mi.isListenOnlyMode();
_config.bitrate = mi.bitrate();
}
uint32_t PeakCanInterface::getCapabilities()
{
return CanInterface::capability_listen_only | CanInterface::capability_auto_restart;
}
int PeakCanInterface::getBitrate()
{
uint32_t speed = 0;
if (CAN_GetValue(_handle, PCAN_BUSSPEED_NOMINAL, &speed, sizeof(speed))==PCAN_ERROR_OK) {
return speed;
} else {
return 0;
}
}
uint16_t PeakCanInterface::calcBitrateMode(int bitrate)
{
switch (bitrate) {
case 5000: return PCAN_BAUD_5K;
case 10000: return PCAN_BAUD_10K;
case 20000: return PCAN_BAUD_20K;
case 33333: return PCAN_BAUD_33K;
case 47619: return PCAN_BAUD_47K;
case 50000: return PCAN_BAUD_50K;
case 83333: return PCAN_BAUD_83K;
case 95238: return PCAN_BAUD_95K;
case 100000: return PCAN_BAUD_100K;
case 125000: return PCAN_BAUD_125K;
case 250000: return PCAN_BAUD_250K;
case 500000: return PCAN_BAUD_500K;
case 800000: return PCAN_BAUD_800K;
case 1000000: return PCAN_BAUD_1M;
default: return 0;
}
}
QString PeakCanInterface::getErrorText(uint32_t status_code)
{
char s[256];
TPCANStatus result = CAN_GetErrorText(status_code, 9, s);
if (result==PCAN_ERROR_OK) {
return QString(s);
} else {
return "Unknown error";
}
}
QList<CanTiming> PeakCanInterface::getAvailableBitrates()
{
return CanInterface::getAvailableBitrates();
}
void PeakCanInterface::open()
{
TPCANStatus result;
uint32_t bitrate_adapt = PCAN_PARAMETER_ON;
result = CAN_SetValue(_handle, PCAN_BITRATE_ADAPTING, &bitrate_adapt, sizeof(bitrate_adapt));
if (result!=PCAN_ERROR_OK) {
log_error(QString("could not set bitrate adapt parameter for CAN channel %1: %2").arg(getName()).arg(getErrorText(result)));
}
uint32_t listen_only = _config.listenOnly ? PCAN_PARAMETER_ON : PCAN_PARAMETER_OFF;
result = CAN_SetValue(_handle, PCAN_LISTEN_ONLY, &listen_only, sizeof(listen_only));
if (result!=PCAN_ERROR_OK) {
log_error(QString("could not set listen only mode=%3 for CAN channel %1: %2").arg(getName()).arg(getErrorText(result)).arg(_config.listenOnly ? "on" : "off"));
}
uint16_t bitrate_mode = calcBitrateMode(_config.bitrate);
if (bitrate_mode==0) {
log_error(QString("CAN channel %1: cannot find bitrate settings for baud rate: %2").arg(getName()).arg(bitrate_mode));
return;
}
result = CAN_Initialize(_handle, bitrate_mode);
if ( (result!=PCAN_ERROR_OK) && (result != PCAN_ERROR_CAUTION) ) {
log_error(QString("could not initialize CAN channel %1: %2").arg(getName()).arg(getErrorText(result)));
return;
}
log_info(QString("CAN channel %1 initialized").arg(getName()));
if (getBitrate() == _config.bitrate) {
log_info(QString("setting bitrate of CAN channel %1 to %2").arg(getName()).arg(getBitrate()));
} else {
log_warning(QString("could not set correct bitrate for CAN channel %1: bitrate is now %2 instead of configured %3").arg(getName()).arg(getBitrate()).arg(_config.bitrate));
}
if (CAN_SetValue(_handle, PCAN_RECEIVE_EVENT, &_autoResetEvent, sizeof(_autoResetEvent))!=PCAN_ERROR_OK) {
log_error(QString("could not set read event for CAN channel: %1").arg(getName()));
}
uint32_t autoreset = _config.autoRestart ? PCAN_PARAMETER_ON : PCAN_PARAMETER_OFF;
result = CAN_SetValue(_handle, PCAN_BUSOFF_AUTORESET, &autoreset, sizeof(autoreset));
if (result!=PCAN_ERROR_OK) {
log_error(QString("could not set busoff auto reset mode=%3 for CAN channel %1: %2").arg(getName()).arg(getErrorText(result)).arg(_config.autoRestart ? "on" : "off"));
}
}
void PeakCanInterface::close()
{
CAN_Uninitialize(_handle);
log_info(QString("CAN channel %1 uninitialized").arg(getName()));
}
uint32_t PeakCanInterface::getState()
{
// TODO implement me
return state_unknown;
}
bool PeakCanInterface::readMessage(CanMessage &msg, unsigned int timeout_ms)
{
TPCANMsg buf;
TPCANTimestamp timestamp;
if (WaitForSingleObject(_autoResetEvent, timeout_ms) != WAIT_OBJECT_0) {
return false;
}
TPCANStatus result = CAN_Read(_handle, &buf, ×tamp);
if (result == PCAN_ERROR_OK) {
if ((buf.MSGTYPE & PCAN_MESSAGE_STATUS) != 0) {
// TODO handle status message?
return false;
} else {
msg.setErrorFrame(false);
}
msg.setInterfaceId(getId());
msg.setId(buf.ID);
msg.setExtended((buf.MSGTYPE & PCAN_MESSAGE_EXTENDED)!=0);
msg.setRTR((buf.MSGTYPE & PCAN_MESSAGE_RTR)!=0);
uint8_t len = (buf.LEN > 8) ? 8 : buf.LEN;
msg.setLength(len);
for (int i=0; i<len; i++) {
msg.setByte(i, buf.DATA[i]);
}
// Total Microseconds = micros + 1000 * millis + 0x100000000 * 1000 * millis_overflow
uint64_t ts = timestamp.millis;
ts += 0x100000000 * (uint64_t)timestamp.millis_overflow;
ts *= 1000;
ts += timestamp.micros;
if (_peakTimestampStart==0) {
_hostTimestampStart = 1000 * QDateTime::currentMSecsSinceEpoch();
_peakTimestampStart = ts;
}
ts -=_peakTimestampStart;
ts += _hostTimestampStart;
msg.setTimestamp(ts/1000000, ts % 1000000);
return true;
} else {
QThread::msleep(10);
return false;
}
}
void PeakCanInterface::sendMessage(const CanMessage &msg)
{
TPCANMsg buf;
buf.ID = msg.getId();
buf.MSGTYPE = 0;
if (msg.isExtended()) {
buf.MSGTYPE |= PCAN_MESSAGE_EXTENDED;
}
if (msg.isRTR()) {
buf.MSGTYPE |= PCAN_MESSAGE_EXTENDED;
}
buf.LEN = msg.getLength();
for (int i=0; i<8; i++) {
buf.DATA[i] = msg.getByte(i);
}
CAN_Write(_handle, &buf);
// TODO check error, update statistics (increment txerrors or txframes)
}
bool PeakCanInterface::updateStatistics()
{
return true;
}
int PeakCanInterface::getNumTxDropped()
{
return 0;
}
int PeakCanInterface::getNumRxOverruns()
{
return 0;
}
int PeakCanInterface::getNumTxErrors()
{
return 0;
}
int PeakCanInterface::getNumTxFrames()
{
return 0;
}
int PeakCanInterface::getNumRxErrors()
{
return 0;
}
int PeakCanInterface::getNumRxFrames()
{
return 0;
}