Reworked driver code, again.
This commit is contained in:
@@ -36,44 +36,56 @@
|
||||
#include <can_drv.h>
|
||||
|
||||
|
||||
/**
|
||||
* @brief Destruktor. Entlädt den CAN-Bus Treiber wieder.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
BCDriverTinyCan::BCDriverTinyCan( QObject* parent )
|
||||
: BCDriver(parent )
|
||||
BCDriverTinyCan::~BCDriverTinyCan()
|
||||
{
|
||||
|
||||
resetDriver();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief BCDriverTinyCan::loadAndStartDriver
|
||||
* @return
|
||||
*/
|
||||
|
||||
void BCDriverTinyCan::onStartDriver()
|
||||
BCDriver::DriverStateResult BCDriverTinyCan::loadAndStartDriver()
|
||||
{
|
||||
DriverStateResult result;
|
||||
if( _driverState < DriverState::Opened)
|
||||
loadDriver();
|
||||
result = loadDriver();
|
||||
if( _driverState == DriverState::Opened)
|
||||
_driverState = connectDriver();
|
||||
result = setConsoleSlaveMode();
|
||||
return result;
|
||||
}
|
||||
|
||||
void BCDriverTinyCan::loadDriver()
|
||||
/**
|
||||
* @brief BCDriverTinyCan::loadDriver
|
||||
* @return
|
||||
*/
|
||||
|
||||
BCDriver::DriverStateResult BCDriverTinyCan::loadDriver()
|
||||
{
|
||||
|
||||
|
||||
auto callLoadDriver = []() -> DriverStateResult
|
||||
auto callLoadDriver = [&]() -> DriverStateResult
|
||||
{
|
||||
if( ::LoadDriver( NULL ) < 0 )
|
||||
return std::unexpected(QString("Driver Error: 'LoadDriver'"));
|
||||
return DriverState::Loaded;
|
||||
_driverState = DriverState::Loaded;
|
||||
return _driverState;
|
||||
};
|
||||
|
||||
auto callInitDriver = [](DriverState state) -> DriverStateResult
|
||||
auto callInitDriver = [&](DriverState state) -> DriverStateResult
|
||||
{
|
||||
Q_UNUSED(state)
|
||||
if( ::CanInitDriver( NULL ) < 0 )
|
||||
return std::unexpected(QString("Driver Error: 'InitDriver'"));
|
||||
return DriverState::Initialized;
|
||||
_driverState = DriverState::Initialized;
|
||||
return _driverState;
|
||||
};
|
||||
|
||||
auto callOpenDevice = [](DriverState state) -> DriverStateResult
|
||||
auto callOpenDevice = [&](DriverState state) -> DriverStateResult
|
||||
{
|
||||
Q_UNUSED(state)
|
||||
if( ::CanDeviceOpen( 0, NULL ) < 0 )
|
||||
@@ -90,30 +102,67 @@ void BCDriverTinyCan::loadDriver()
|
||||
|
||||
if( deviceStatus.CanStatus == CAN_STATUS_BUS_OFF )
|
||||
{
|
||||
::CanSetMode( 0, OP_CAN_RESET, CAN_CMD_NONE );
|
||||
return std::unexpected(QString("Driver Error: CAN Status 'BusOff'" ));
|
||||
::CanSetMode( 0, OP_CAN_RESET, CAN_CMD_NONE );
|
||||
return std::unexpected(QString("Driver Error: CAN Status 'BusOff'" ));
|
||||
}
|
||||
return DriverState::Opened;
|
||||
_driverState = DriverState::Opened;
|
||||
return _driverState;
|
||||
};
|
||||
|
||||
|
||||
// #1. erstmal komplett zurücksetzen
|
||||
resetDriver();
|
||||
// #2. Treiber laden, initialisieren und
|
||||
// mit dem tinyCan Interface verbinden.
|
||||
auto newDriverState = callLoadDriver()
|
||||
.and_then( callInitDriver )
|
||||
.and_then( callOpenDevice );
|
||||
|
||||
_driverState = DriverState::Error;
|
||||
QString message("Driver Ready.");
|
||||
|
||||
// success:
|
||||
if(newDriverState)
|
||||
_driverState = *newDriverState;
|
||||
else
|
||||
message = newDriverState.error();
|
||||
{
|
||||
// return 'DriverState::Opened'
|
||||
return _driverState;
|
||||
}
|
||||
// return driver error message,
|
||||
// _driverState ist irgendwo unter DriverState::Opened
|
||||
return newDriverState;
|
||||
|
||||
emit driverStateChanged( _driverState, message );
|
||||
}
|
||||
|
||||
// __fix
|
||||
BCDriver::DriverState BCDriverTinyCan::connectDriver()
|
||||
BCDriver::DriverStateResult BCDriverTinyCan::setConsoleSlaveMode()
|
||||
{
|
||||
/*
|
||||
consoleInSlaveMode = getValue(CONSOLE, CONSOLE_STATUS_SLAVE);
|
||||
|
||||
if (consoleInSlaveMode)
|
||||
{
|
||||
printf("console already in salve mode. good!" _NL _NL);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (gConsoleSetSlaveMode)
|
||||
{
|
||||
int retry = 20;
|
||||
|
||||
printf("putting console in salve mode ... ");
|
||||
do {
|
||||
setValue(CONSOLE, CONSOLE_STATUS_SLAVE, 1);
|
||||
consoleInSlaveMode = getValue(CONSOLE, CONSOLE_STATUS_SLAVE);
|
||||
usleep(200000);
|
||||
} while(retry-- && !consoleInSlaveMode);
|
||||
|
||||
doSleep(500); // give the console some time to settle
|
||||
printf("%s" _NL _NL, consoleInSlaveMode ? "done" : "failed");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("console not in slave mode" _NL _NL);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
uint32_t console = static_cast<uint32_t>(BCDevice::ID::Console);
|
||||
uint8_t slaveFlag = static_cast<uint8_t>(BC::ID::Cons_Status_Slave);
|
||||
|
||||
@@ -126,12 +175,12 @@ BCDriver::DriverState BCDriverTinyCan::connectDriver()
|
||||
qDebug() << "BCDriverTinyCan::BCDriverTinyCan::XXX Driver Init: putting Console in slave mode ... ";
|
||||
|
||||
unsigned int retry = cTimeOuts;
|
||||
emit driverStateChanged( _driverState, "Driver Init: putting Console in slave mode ... " );
|
||||
|
||||
TransmitResult isSlave = 0;
|
||||
do
|
||||
{
|
||||
writeRawByte( console, slaveFlag, 1 );
|
||||
isSlave = readRawByte( console, slaveFlag );
|
||||
isSlave = readRawByte( console, slaveFlag );
|
||||
bc::delay_millis( 200 );
|
||||
|
||||
} while( retry-- && !(*isSlave) );
|
||||
@@ -145,20 +194,15 @@ BCDriver::DriverState BCDriverTinyCan::connectDriver()
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
try
|
||||
{
|
||||
initBCDevice::ID::Console();
|
||||
}
|
||||
|
||||
catch( std::exception& except )
|
||||
{
|
||||
::CanDownDriver();
|
||||
::UnloadDriver();
|
||||
}
|
||||
*/
|
||||
|
||||
void BCDriverTinyCan::resetDriver()
|
||||
{
|
||||
if( _driverState > DriverState::NotPresent )
|
||||
{
|
||||
::CanDownDriver();
|
||||
::UnloadDriver();
|
||||
_driverState = DriverState::NotPresent;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief BCDriverTinyCan::readRawByte
|
||||
@@ -173,7 +217,7 @@ TransmitResult BCDriverTinyCan::readRawByte( uint32_t deviceID, uint8_t register
|
||||
//TransmitResult
|
||||
qDebug() << " --- BCDriverTinyCan::readRawByte DriverState: " << getDriverState();
|
||||
|
||||
if( getDriverState() != DriverState::DeviceReady)
|
||||
if( _driverState != DriverState::DeviceReady)
|
||||
return std::unexpected(QString("readRawValue error: driver not loaded." ) );
|
||||
|
||||
::TCanMsg msg;
|
||||
@@ -238,7 +282,7 @@ retry:
|
||||
TransmitResult BCDriverTinyCan::writeRawByte( uint32_t deviceID, uint8_t registerID ,uint8_t value ) const
|
||||
{
|
||||
|
||||
if( getDriverState() != DriverState::DeviceReady)
|
||||
if( _driverState != DriverState::DeviceReady)
|
||||
return std::unexpected(QString("writeRawValue error: driver not loaded." ) );
|
||||
|
||||
qDebug() << " --- BCDriverTinyCan writeRawValue: " << value;
|
||||
|
||||
Reference in New Issue
Block a user