Unix reconnects on soft-driver reset. Doesn't like a hard disconnect.

This commit is contained in:
EspoTek 2017-05-16 14:45:00 +10:00
parent 54cdad44e5
commit c0e554e37f
11 changed files with 82 additions and 31 deletions

View File

@ -18,3 +18,5 @@ int MULTIMETER_PERIOD = 500;
double SERIAL_DELAY = 0.01; //100 baud?
QMutex tcBlockMutex;
QMutex unixDriverDeleteMutex;
struct timeval tv;

View File

@ -24,6 +24,8 @@ extern int MULTIMETER_PERIOD;
extern double SERIAL_DELAY;
extern QMutex tcBlockMutex;
extern QMutex unixDriverDeleteMutex;
extern struct timeval tv;
#define DEBUG_SETTINGSDOTSET

Binary file not shown.

View File

@ -17,7 +17,7 @@
#define ISO_PACKET_SIZE 125
#define ISO_PACKETS_PER_CTX 25
#define NUM_FUTURE_CTX 40
#define NUM_FUTURE_CTX 4
#define ISO_TIMER_PERIOD 1
#define NUM_ISO_ENDPOINTS 6
#define MAX_OVERLAP (NUM_FUTURE_CTX*NUM_ISO_ENDPOINTS + 1)

View File

@ -81,7 +81,6 @@ MainWindow::MainWindow(QWidget *parent) :
connect(ui->controller_iso->driver, SIGNAL(checkXY(bool)), ui->xyDisplayLabel, SLOT(setChecked(bool)));
connect(ui->controller_iso->driver, SIGNAL(disableWindow(bool)), ui->deviceConnected, SLOT(connectedStatusChanged(bool)));
connect(ui->controller_iso->driver, SIGNAL(upTick()), ui->controller_iso, SLOT(timerTick()));
connect(ui->controller_iso->driver, SIGNAL(killMe()), this, SLOT(reinitUsb()));
connect(ui->controller_iso->driver, SIGNAL(connectedStatus(bool)), ui->deviceConnected, SLOT(connectedStatusChanged(bool)));
connect(ui->controller_iso->driver, SIGNAL(initialConnectComplete(void)), ui->deviceConnected, SLOT(resetUsbState(bool)));
#endif
@ -105,7 +104,6 @@ MainWindow::MainWindow(QWidget *parent) :
connect(ui->controller_iso->driver, SIGNAL(checkXY(bool)), ui->xyDisplayLabel, SLOT(setChecked(bool)));
connect(ui->controller_iso->driver, SIGNAL(disableWindow(bool)), ui->deviceConnected, SLOT(connectedStatusChanged(bool)));
connect(ui->controller_iso->driver, SIGNAL(upTick()), ui->controller_iso, SLOT(timerTick()));
connect(ui->controller_iso->driver, SIGNAL(killMe()), this, SLOT(reinitUsb()));
connect(ui->controller_iso->driver, SIGNAL(connectedStatus(bool)), ui->deviceConnected, SLOT(connectedStatusChanged(bool)));
#endif
@ -1077,10 +1075,11 @@ void MainWindow::reinitUsb(void){
ui->controller_iso->driver->shutdownProcedure();
QTimer::singleShot(2000, this, SLOT(reinitUsbStage2()));
#endif
qDebug() << "ReinitUsb Stage 1 complete";
}
void MainWindow::reinitUsbStage2(void){
qDebug() << "ReinitUsb entering stage 2";
delete(ui->controller_iso->driver);
ui->controller_iso->driver = new _PLATFORM_DEPENDENT_USB_OBJECT();
@ -1109,7 +1108,7 @@ void MainWindow::reinitUsbStage2(void){
connect(ui->controller_iso->driver, SIGNAL(initialConnectComplete()), this, SLOT(resetUsbState()));
ui->controller_iso->driver->setGain(reinitScopeGain);
qDebug() << "ReinitUsb is returning";
qDebug() << "ReinitUsbStage2 is returning";
}
void MainWindow::resetUsbState(void){

Binary file not shown.

View File

@ -1,35 +1,62 @@
#include "unixusbdriver.h"
#include "platformspecific.h"
unixUsbDriver::unixUsbDriver(QWidget *parent) : genericUsbDriver(parent)
{
qDebug() << "unixUsbDriver created!";
tv.tv_sec = 0;
tv.tv_usec = 100000;
}
unixUsbDriver::~unixUsbDriver(void){
qDebug() << "\n\nunixUsbDriver destructor ran!";
workerThread->quit();
workerThread->deleteLater();
//unixDriverDeleteMutex.lock();
workerThread->terminate();
//workerThread->deleteLater();
delete(isoHandler);
delete(workerThread);
qDebug() << "THREAD Gone!";
for (int i=0; i<NUM_FUTURE_CTX; i++){
for (int k=0; k<NUM_ISO_ENDPOINTS; k++){
libusb_free_transfer(isoCtx[k][i]);
}
}
qDebug() << "Transfers freed.";
libusb_release_interface(handle, 0);
qDebug() << "Interface released";
libusb_close(handle);
qDebug() << "Device Closed";
libusb_exit(ctx);
qDebug() << "Libusb exited";
//unixDriverDeleteMutex.unlock();
qDebug() << "unixUsbDriver destructor completed!\n\n";
}
unsigned char unixUsbDriver::usbInit(unsigned long VIDin, unsigned long PIDin){
qDebug() << "Entering unixUsbDriver::usbInit";
int error = libusb_init(&ctx);
if(error){
qDebug() << "libusb_init FAILED";
return error;
} else qDebug() << "Libusb context initialised";
int error;
//Should only run once.
if(ctx == NULL){
error = libusb_init(&ctx);
if(error){
qDebug() << "libusb_init FAILED";
return 1;
} else qDebug() << "Libusb context initialised";
libusb_set_debug(ctx, 3);
handle = libusb_open_device_with_vid_pid(ctx, VIDin, PIDin);
if(handle==NULL){
qDebug() << "DEVICE NOT FOUND";
return -1;
libusb_set_debug(ctx, 3);
}
if(handle == NULL){
handle = libusb_open_device_with_vid_pid(ctx, VIDin, PIDin);
if(handle==NULL){
qDebug() << "DEVICE NOT FOUND";
return 1;
}
qDebug() << "Device found!!";
}
qDebug() << "Device found!!";
qDebug() << (libusb_kernel_driver_active(handle, 0) ? "KERNEL DRIVER ACTIVE" : "KERNEL DRIVER INACTIVE");
if(libusb_kernel_driver_active(handle, 0)){
@ -39,7 +66,7 @@ unsigned char unixUsbDriver::usbInit(unsigned long VIDin, unsigned long PIDin){
if(error){
qDebug() << "libusb_claim_interface FAILED";
qDebug() << "ERROR" << error << libusb_error_name(error);
return error;
return 1;
} else qDebug() << "Interface claimed!";
return 0;
@ -130,7 +157,7 @@ void unixUsbDriver::isoTimerTick(void){
tcBlockMutex.lock();
for (n=0; n<NUM_FUTURE_CTX; n++){
if(allEndpointsComplete(n)){
//qDebug("Transfer %d is complete!!", n);
qDebug("Transfer %d is complete!!", n);
if(transferCompleted[0][n].timeReceived < minFrame){
minFrame = transferCompleted[0][n].timeReceived;
earliest = n;
@ -143,6 +170,8 @@ void unixUsbDriver::isoTimerTick(void){
return;
}
qDebug() << "Processing Ctx" << earliest;
//Copy iso data into buffer
for(i=0;i<isoCtx[0][earliest]->num_iso_packets;i++){
for(unsigned char k=0; k<NUM_ISO_ENDPOINTS;k++){
@ -153,6 +182,8 @@ void unixUsbDriver::isoTimerTick(void){
}
}
qDebug() << "Data copy complete!";
//Control data for isoDriver
bufferLengths[currentWriteBuffer] = packetLength;
currentWriteBuffer = !currentWriteBuffer;
@ -160,9 +191,18 @@ void unixUsbDriver::isoTimerTick(void){
//Setup next transfer
for(unsigned char k=0; k<NUM_ISO_ENDPOINTS;k++){
transferCompleted[k][earliest].completed = false;
error = libusb_submit_transfer(isoCtx[k][earliest]);
if(shutdownMode){
error = libusb_cancel_transfer(isoCtx[k][earliest]);
numCancelled++;
qDebug() << "Cancelled" << earliest << k;
if(numCancelled == (NUM_FUTURE_CTX * NUM_ISO_ENDPOINTS)){
isoHandler->stopTime = true;
}
}else{
error = libusb_submit_transfer(isoCtx[k][earliest]);
}
if(error){
qDebug() << "libusb_submit_transfer FAILED";
qDebug() << (shutdownMode ? "libusb_cancel_transfer FAILED" : "libusb_submit_transfer FAILED");
qDebug() << "ERROR" << libusb_error_name(error);
} //else qDebug() << "isoCtx submitted successfully!";
}
@ -181,7 +221,10 @@ char *unixUsbDriver::isoRead(unsigned int *newLength){
}
void unixUsbDriver::recoveryTick(void){
avrDebug();
//This should not be called in shutdown mode since it cause double deletion!
if(!shutdownMode){
avrDebug();
}
}
bool unixUsbDriver::allEndpointsComplete(int n){

View File

@ -9,7 +9,6 @@
#include "genericusbdriver.h"
#include "libusb.h"
//tcBlock is fed to the callback in the libusb user data section.
typedef struct tcBlock{
int number;
@ -27,13 +26,16 @@ public:
worker(){};
~worker(){};
libusb_context *ctx;
bool stopTime = false;
public slots:
void handle(){
qDebug() << "SUB THREAD ID" << QThread::currentThreadId();
while(1){
if(libusb_event_handling_ok(ctx)){
libusb_handle_events(ctx);
//qDebug() << "HANDLED";
if(!stopTime){
if(libusb_event_handling_ok(ctx)){
libusb_handle_events_timeout(ctx, &tv);
//qDebug() << "HANDLED";
}
}
}
}
@ -52,7 +54,7 @@ public:
char *isoRead(unsigned int *newLength);
protected:
//USB Vars
libusb_context *ctx;
libusb_context *ctx = NULL;
libusb_device_handle *handle = NULL;
//USBIso Vars
libusb_transfer *isoCtx[NUM_ISO_ENDPOINTS][NUM_FUTURE_CTX];
@ -65,6 +67,7 @@ protected:
unsigned char usbIsoInit(void);
bool allEndpointsComplete(int n);
bool shutdownMode = false;
int numCancelled = 0;
signals:
public slots:
void isoTimerTick(void);
@ -81,8 +84,10 @@ static void LIBUSB_CALL isoCallback(struct libusb_transfer * transfer){
//qDebug() << "CALLBACK" << number;
//qDebug() << completed;
((tcBlock *)transfer->user_data)->completed = true;
((tcBlock *)transfer->user_data)->timeReceived = QDateTime::currentMSecsSinceEpoch();
if(transfer->status!=LIBUSB_TRANSFER_CANCELLED){
((tcBlock *)transfer->user_data)->completed = true;
((tcBlock *)transfer->user_data)->timeReceived = QDateTime::currentMSecsSinceEpoch();
}
//qDebug() << ((tcBlock *)transfer->user_data)->timeReceived;
tcBlockMutex.unlock();
return;

Binary file not shown.