Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Desktop_Interface/genericusbdriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ genericUsbDriver::genericUsbDriver(QWidget *parent) : QLabel(parent)
//qDebug() << k;
//qDebug() << NUM_ISO_ENDPOINTS;
pipeID[k] = 0x81+k;
#ifdef PLATFORM_WINDOWS
qDebug() << "pipeID" << k << "=" << static_cast<int>(pipeID[k]);
#else
qDebug() << "pipeID" << k << "=" << pipeID[k];
#endif
}

connectTimer = new QTimer();
Expand Down
4 changes: 4 additions & 0 deletions Desktop_Interface/genericusbdriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,11 @@ class genericUsbDriver : public QLabel
//Generic Vars
//bufferControl *bufferPtr = NULL;
QTimer *psuTimer = nullptr;
#ifdef PLATFORM_WINDOWS
unsigned char pipeID[NUM_ISO_ENDPOINTS];
#else
unsigned char pipeID[3];
#endif
QTimer *isoTimer = nullptr;
QTimer *connectTimer = nullptr;
QTimer *recoveryTimer;
Expand Down
8 changes: 7 additions & 1 deletion Desktop_Interface/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1575,7 +1575,13 @@ void MainWindow::reinitUsb(void){
ui->controller_iso->driver->saveState(&reinitdeviceMode, &reinitScopeGain, &reinitCurrentPsuVoltage, &reinitDigitalPinState);

#ifdef PLATFORM_WINDOWS
reinitUsbStage2();
if(!(ui->controller_iso->driver->connected)){
reinitUsbStage2();
} else{
connect(ui->controller_iso->driver, SIGNAL(shutdownComplete()),
this, SLOT(reinitUsbStage2()), Qt::UniqueConnection);
ui->controller_iso->driver->shutdownProcedure();
}
#else
if(!(ui->controller_iso->driver->connected)){
reinitUsbStage2();
Expand Down
78 changes: 73 additions & 5 deletions Desktop_Interface/winusbdriver.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "winusbdriver.h"
#include <QApplication>
#include <QElapsedTimer>
#include <qprocess.h>
#include <QMessageBox>

Expand All @@ -18,7 +19,7 @@ winUsbDriver::~winUsbDriver(void){
for(int n=0;n<NUM_FUTURE_CTX;n++){
IsoK_Free(isoCtx[k][n]);
}
for(int i=0;i<MAX_OVERLAP;i++){
for(int i=0;i<NUM_FUTURE_CTX;i++){
OvlK_Release(ovlkHandle[k][i]);
}
UsbK_FlushPipe(handle, pipeID[k]);
Expand Down Expand Up @@ -60,17 +61,19 @@ unsigned char winUsbDriver::usbInit(unsigned long VIDin, unsigned long PIDin){
LstK_FindByVidPid(deviceList, VIDin, PIDin, &deviceInfo);
LstK_Free(deviceList);
if (deviceInfo == NULL){
qDebug("Could not find device VID = %04X, PID = %04X", VIDin, PIDin);
//I nitpicked this formatting so the compiler wont whine about the qDebug format type mismatch
qDebug() << "Could not find device VID = " << QString::number(VIDin, 16) << ", PID = " << QString::number(PIDin, 16);
return 2;
}

//Open Labrador!!
success = UsbK_Init(&handle, deviceInfo);
if (!success){
ec = GetLastError();
qDebug("UsbK_Init failed. ErrorCode: %08Xh", ec);
//I nitpicked this formatting so the compiler wont whine about the qDebug format type mismatch
qDebug() << "UsbK_Init failed. ErrorCode: " << QString::number(ec, 16);
return 3;
} else qDebug("Device opened successfully!");
} else qDebug() << "Device opened successfully!";

if (handle == NULL){
return 4; //Unkown error, only exists on 32 bit???
Expand Down Expand Up @@ -212,6 +215,11 @@ void winUsbDriver::isoTimerTick(void){
//Finally, it should signal upTick() so that isoDriver knows it can draw a new frame.

timerCount++;

if(shutdownRequested){
return;
}

char subString[3] = "th";
if(timerCount%10 == 1) strcpy(subString, "st");
if(timerCount%10 == 2) strcpy(subString, "nd");
Expand Down Expand Up @@ -263,13 +271,15 @@ void winUsbDriver::isoTimerTick(void){
if(framePhaseError){
#ifndef WINDOWS_32_BIT
qDebug("Frame phase error of %d", framePhaseError);
killMe();
#endif
}


//Setup transfer for resubmission
for(unsigned char k=0; k<NUM_ISO_ENDPOINTS; k++){
if(shutdownRequested){
continue;
}
//Apparently reusing before resubmitting is a bad idea???

/*UINT oldStart = isoCtx[k][earliest]->StartFrame;
Expand Down Expand Up @@ -321,6 +331,64 @@ void winUsbDriver::recoveryTick(){
}

void winUsbDriver::shutdownProcedure(){
if(shutdownRequested){
return;
}
shutdownRequested = true;

// 1) Stop timers so no new work is scheduled.
// 2) Abort + flush pipes to force outstanding overlapped requests to complete.
// 3) Poll for completion with a timeout.
// 4) Emit shutdownComplete once it's safe for MainWindow to delete this driver.
qDebug() << "winUsbDriver::shutdownProcedure begin";

if(isoTimer){
isoTimer->stop();
}
if(recoveryTimer){
recoveryTimer->stop();
}

if(handle){
for (unsigned char k=0; k<NUM_ISO_ENDPOINTS; k++){
UsbK_AbortPipe(handle, pipeID[k]);
UsbK_FlushPipe(handle, pipeID[k]);
}
}

// Drain outstanding overlapped requests.
QElapsedTimer drainTimer;
drainTimer.start();
constexpr qint64 kDrainTimeoutMs = 3000;
bool allComplete = false;
while(!allComplete && (drainTimer.elapsed() < kDrainTimeoutMs)){
allComplete = true;
for (unsigned char k=0; k<NUM_ISO_ENDPOINTS; k++){
for(int n=0; n<NUM_FUTURE_CTX; n++){
if(ovlkHandle[k][n] && !OvlK_IsComplete(ovlkHandle[k][n])){
allComplete = false;
break;
}
}
if(!allComplete){
break;
}
}
if(!allComplete){
QThread::msleep(10);
}
}

if(!allComplete){
qDebug() << "winUsbDriver::shutdownProcedure timed out waiting for overlapped completion";
}

if(!shutdownCompleteSent){
shutdownCompleteSent = true;
connected = false;
connectedStatus(false);
emit shutdownComplete();
}
return;
}

Expand Down
8 changes: 8 additions & 0 deletions Desktop_Interface/winusbdriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,15 @@ class winUsbDriver : public genericUsbDriver
int usbIsoInit(void);
int flashFirmware(void);
bool allEndpointsComplete(int n);
bool shutdownRequested = false;
bool shutdownCompleteSent = false;
bool transferPending[NUM_ISO_ENDPOINTS][NUM_FUTURE_CTX] = {};
QElapsedTimer shutdownElapsed;

signals:
void shutdownComplete();


public slots:
void isoTimerTick(void);
void recoveryTick();
Expand Down
Loading