diff --git a/Desktop_Interface/genericusbdriver.h b/Desktop_Interface/genericusbdriver.h index 8ddc3eb2..84d217b5 100644 --- a/Desktop_Interface/genericusbdriver.h +++ b/Desktop_Interface/genericusbdriver.h @@ -95,7 +95,7 @@ protected: void requestFirmwareVersion(void); void requestFirmwareVariant(void); virtual unsigned char usbInit(unsigned long VIDin, unsigned long PIDin) = 0; - virtual unsigned char usbIsoInit(void) = 0; + virtual int usbIsoInit(void) = 0; virtual int flashFirmware(void) = 0; uint8_t numero_uno = 1; signals: diff --git a/Desktop_Interface/macusbdriver.cpp b/Desktop_Interface/macusbdriver.cpp deleted file mode 100644 index 818ee66f..00000000 --- a/Desktop_Interface/macusbdriver.cpp +++ /dev/null @@ -1,203 +0,0 @@ -#include "macUsbDriver.h" - -QMutex tcBlockMutex; - -macUsbDriver::macUsbDriver(QWidget *parent) : genericUsbDriver(parent) -{ - unsigned char error = 1; - while(error){ - error = usbInit(0x03eb, 0xa000); - if(error)QThread::msleep(64); - } - setDeviceMode(deviceMode); - newDig(digitalPinState); - usbIsoInit(); - - psuTimer = new QTimer(); - psuTimer->setTimerType(Qt::PreciseTimer); - psuTimer->start(PSU_PERIOD); - - recoveryTimer = new QTimer(); - recoveryTimer->setTimerType(Qt::PreciseTimer); - recoveryTimer->start(RECOVERY_PERIOD); - connect(recoveryTimer, SIGNAL(timeout()), this, SLOT(recoveryTick())); -} - -macUsbDriver::~macUsbDriver(void){ - qDebug() << "\n\nmacUsbDriver destructor ran!"; - workerThread->quit(); - workerThread->deleteLater(); - delete(isoHandler); - delete(psuTimer); - delete(recoveryTimer); - delete(isoTimer); - libusb_release_interface(handle, 0); - libusb_exit(ctx); -} - -unsigned char macUsbDriver::usbInit(unsigned long VIDin, unsigned long PIDin){ - qDebug() << "Entering macUsbDriver::usbInit"; - - int error = libusb_init(&ctx); - if(error){ - qDebug() << "libusb_init FAILED"; - return error; - } 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; - } - qDebug() << "Device found!!"; - - qDebug() << (libusb_kernel_driver_active(handle, 0) ? "KERNEL DRIVER ACTIVE" : "KERNEL DRIVER INACTIVE"); - if(libusb_kernel_driver_active(handle, 0)){ - libusb_detach_kernel_driver(handle, 0); - } - error = libusb_claim_interface(handle, 0); - if(error){ - qDebug() << "libusb_claim_interface FAILED"; - qDebug() << "ERROR" << error << libusb_error_name(error); - return error; - } else qDebug() << "Interface claimed!"; - - return 0; -} - -void macUsbDriver::usbSendControl(uint8_t RequestType, uint8_t Request, uint16_t Value, uint16_t Index, uint16_t Length, unsigned char *LDATA){ - qDebug("Sending Control packet! 0x%x,\t0x%x,\t%u,\t%u,\t%d,\t%u", RequestType, Request, Value, Index, LDATA, Length); - int error = libusb_control_transfer(handle, RequestType, Request, Value, Index, LDATA, Length, 4000); - if(error){ - qDebug("macUsbDriver::usbSendControl FAILED with error %s", libusb_error_name(error)); - } else qDebug() << "macUsbDriver::usbSendControl SUCCESS"; - if(error == LIBUSB_ERROR_NO_DEVICE){ - qDebug() << "Device not found. Becoming an hero."; - killMe(); - } - return; -} - -unsigned char macUsbDriver::usbIsoInit(void){ - int error; - - for(int n=0;nsetTimerType(Qt::PreciseTimer); - isoTimer->start(ISO_TIMER_PERIOD); - connect(isoTimer, SIGNAL(timeout()), this, SLOT(isoTimerTick())); - - qDebug() << "Setup successful!"; - - isoHandler = new worker(); - workerThread = new QThread(); - - isoHandler->ctx = ctx; - isoHandler->moveToThread(workerThread); - connect(workerThread, SIGNAL(started()), isoHandler, SLOT(handle())); - - workerThread->start(); - - qDebug() << "MAIN THREAD ID" << QThread::currentThreadId(); - //QThread::sleep(1); - qDebug() << "Iso Stack initialised!"; - return 1; -} - -void macUsbDriver::isoTimerTick(void){ - timerCount++; - - char subString[3] = "th"; - if(timerCount%10 == 1) strcpy(subString, "st"); - if(timerCount%10 == 2) strcpy(subString, "nd"); - if(timerCount%10 == 3) strcpy(subString, "rd"); - if((timerCount<20) && (timerCount > 10)) strcpy(subString, "th"); - - //qDebug("\n\nThis is the %d%s Tick!", timerCount, subString); - - int n, error, earliest = MAX_OVERLAP; - qint64 minFrame = 9223372036854775807; //max value for 64 bit signed - - unsigned int i, packetLength = 0; - unsigned char* packetPointer; - - tcBlockMutex.lock(); - for (n=0; nnum_iso_packets;i++){ - packetPointer = libusb_get_iso_packet_buffer_simple(isoCtx[earliest], i); - //qDebug() << packetLength; - memcpy(&(outBuffers[currentWriteBuffer][packetLength]), packetPointer, isoCtx[earliest]->iso_packet_desc[i].actual_length); - packetLength += isoCtx[earliest]->iso_packet_desc[i].actual_length; - } - - //Control data for isoDriver - bufferLengths[currentWriteBuffer] = packetLength; - currentWriteBuffer = !currentWriteBuffer; - - //Setup next transfer - transferCompleted[earliest].completed = false; - error = libusb_submit_transfer(isoCtx[earliest]); - if(error){ - qDebug() << "libusb_submit_transfer FAILED"; - qDebug() << "ERROR" << libusb_error_name(error); - } //else qDebug() << "isoCtx submitted successfully!"; - tcBlockMutex.unlock(); - qDebug() << "Returning at Uptick()"; - upTick(); - return; -} - -char *macUsbDriver::isoRead(unsigned int *newLength){ - //*(newLength) = 0; - //return (char*) NULL; - qDebug() << "macUsbDriver::isoRead"; - *(newLength) = bufferLengths[!currentWriteBuffer]; - return (char*) outBuffers[(unsigned char) !currentWriteBuffer]; -} - -void macUsbDriver::recoveryTick(void){ - //avrDebug(); -} - -static void LIBUSB_CALL isoCallback(struct libusb_transfer * transfer){ - tcBlockMutex.lock(); - //int number = ((tcBlock *)transfer->user_data)->number; - //bool completed = ((tcBlock *)transfer->user_data)->completed; - - //qDebug() << "CALLBACK"; - //qDebug() << completed; - - ((tcBlock *)transfer->user_data)->completed = true; - ((tcBlock *)transfer->user_data)->timeReceived = QDateTime::currentMSecsSinceEpoch(); - //qDebug() << ((tcBlock *)transfer->user_data)->timeReceived; - tcBlockMutex.unlock(); - return; -} diff --git a/Desktop_Interface/macusbdriver.h b/Desktop_Interface/macusbdriver.h deleted file mode 100644 index a3678b32..00000000 --- a/Desktop_Interface/macusbdriver.h +++ /dev/null @@ -1,72 +0,0 @@ -#ifndef macUsbDriver_H -#define macUsbDriver_H - -#include -#include -#include -#include - -#include "genericusbdriver.h" -#include "libusb.h" - -#define RECOVERY_PERIOD 250 - -typedef struct tcBlock{ - int number; - bool completed; - qint64 timeReceived; -} tcBlock; - -extern QMutex tcBlockMutex; - -class worker : public QObject -{ - Q_OBJECT - -public: - worker(){}; - ~worker(){}; - libusb_context *ctx; -public slots: - void handle(){ - qDebug() << "SUB THREAD ID" << QThread::currentThreadId(); - while(1){ - if(libusb_event_handling_ok(ctx)){ - libusb_handle_events(ctx); - //qDebug() << "HANDLED"; - } - } - } -}; - -class macUsbDriver : public genericUsbDriver -{ - Q_OBJECT -public: - explicit macUsbDriver(QWidget *parent = 0); - ~macUsbDriver(); - void usbSendControl(uint8_t RequestType, uint8_t Request, uint16_t Value, uint16_t Index, uint16_t Length, unsigned char *LDATA); - char *isoRead(unsigned int *newLength); -private: - //USB Vars - libusb_context *ctx; - libusb_device_handle *handle = NULL; - QTimer *recoveryTimer; - //USBIso Vars - libusb_transfer *isoCtx[NUM_FUTURE_CTX]; - tcBlock transferCompleted[NUM_FUTURE_CTX]; - unsigned char dataBuffer[NUM_FUTURE_CTX][ISO_PACKET_SIZE*ISO_PACKETS_PER_CTX]; - worker *isoHandler; - QThread *workerThread; - //Generic Functions - unsigned char usbInit(unsigned long VIDin, unsigned long PIDin); - unsigned char usbIsoInit(void); -signals: -public slots: - void isoTimerTick(void); - void recoveryTick(void); -}; - -static void LIBUSB_CALL isoCallback(struct libusb_transfer *transfer); - -#endif // macUsbDriver_H diff --git a/Desktop_Interface/unixusbdriver.cpp b/Desktop_Interface/unixusbdriver.cpp index d91121cb..f54d4c4a 100644 --- a/Desktop_Interface/unixusbdriver.cpp +++ b/Desktop_Interface/unixusbdriver.cpp @@ -118,7 +118,7 @@ void unixUsbDriver::usbSendControl(uint8_t RequestType, uint8_t Request, uint16_ return; } -unsigned char unixUsbDriver::usbIsoInit(void){ +int unixUsbDriver::usbIsoInit(void){ int error; for(int n=0;n