Skip to content
Permalink
Browse files

implement configuration for pcan

  • Loading branch information...
HubertD committed Jan 21, 2016
1 parent eb926c9 commit fa3174e80337f4a0032dff26a920cc6fcea9149e
Showing with 85 additions and 6 deletions.
  1. +77 −6 src/driver/PeakCanDriver/PeakCanInterface.cpp
  2. +8 −0 src/driver/PeakCanDriver/PeakCanInterface.h
@@ -1,6 +1,7 @@
#include "PeakCanInterface.h"
#include "PeakCanDriver.h"

#include <model/MeasurementInterface.h>
#include <windows.h>
#include "pcan-basic-api/Include/PCANBasic.h"

@@ -12,6 +13,10 @@ PeakCanInterface::PeakCanInterface(PeakCanDriver *driver, uint32_t handle)
{
_timestampOffset = QDateTime::currentMSecsSinceEpoch() - GetTickCount();
_autoResetEvent = CreateEvent(NULL, FALSE, FALSE, NULL);

_config.autoRestart = true;
_config.listenOnly = false;
_config.bitrate = 100000;
}

PeakCanInterface::~PeakCanInterface()
@@ -57,7 +62,10 @@ QString PeakCanInterface::getName() const

void PeakCanInterface::applyConfig(const MeasurementInterface &mi)
{
// TODO: implement me
_config.autoRestart = mi.doAutoRestart();
_config.listenOnly = mi.isListenOnlyMode();
_config.bitrate = mi.bitrate();

}

int PeakCanInterface::getBitrate()
@@ -70,6 +78,38 @@ int PeakCanInterface::getBitrate()
}
}

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<int> PeakCanInterface::getAvailableBitrates()
{
QList<int> retval;
@@ -84,20 +124,51 @@ QList<int> PeakCanInterface::getAvailableFdBitrates()

void PeakCanInterface::open()
{
TPCANStatus result;

uint32_t bitrate_adapt = PCAN_PARAMETER_ON;
if (CAN_SetValue(_handle, PCAN_BITRATE_ADAPTING, &bitrate_adapt, sizeof(bitrate_adapt))!=PCAN_ERROR_OK) {
Backend::instance().logMessage(log_level_error, QString("could not set bitrate adapt parameter for CAN channel: %1").arg(getName()));

result = CAN_SetValue(_handle, PCAN_BITRATE_ADAPTING, &bitrate_adapt, sizeof(bitrate_adapt));
if (result!=PCAN_ERROR_OK) {
Backend::instance().logMessage(log_level_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) {
Backend::instance().logMessage(log_level_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) {
Backend::instance().logMessage(log_level_error, QString("CAN channel %1: cannot find bitrate settings for baud rate: %2").arg(getName()).arg(bitrate_mode));
return;
}

if (CAN_Initialize(_handle, PCAN_BAUD_500K)==PCAN_ERROR_OK) { // TODO check if bitrate could not be changed...
Backend::instance().logMessage(log_level_info, QString("CAN channel %1 initialized").arg(getName()));
result = CAN_Initialize(_handle, bitrate_mode);

if ( (result!=PCAN_ERROR_OK) && (result != PCAN_ERROR_CAUTION) ) {
Backend::instance().logMessage(log_level_error, QString("could not initialize CAN channel %1: %2").arg(getName()).arg(getErrorText(result)));
return;
}

Backend::instance().logMessage(log_level_info, QString("CAN channel %1 initialized").arg(getName()));
if (getBitrate() == _config.bitrate) {
Backend::instance().logMessage(log_level_info, QString("setting bitrate of CAN channel %1 to %2").arg(getName()).arg(getBitrate()));
} else {
Backend::instance().logMessage(log_level_error, QString("could not initialize CAN channel: %1").arg(getName()));
Backend::instance().logMessage(log_level_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) {
Backend::instance().logMessage(log_level_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) {
Backend::instance().logMessage(log_level_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()
@@ -42,6 +42,14 @@ class PeakCanInterface : public CanInterface
int64_t _timestampOffset;
void *_autoResetEvent;

struct {
bool autoRestart;
bool listenOnly;
int bitrate;
} _config;

uint16_t calcBitrateMode(int bitrate);
QString getErrorText(uint32_t status_code);

};

0 comments on commit fa3174e

Please sign in to comment.
You can’t perform that action at this time.