Iso threading implemented. No actual data I/O yet.

This commit is contained in:
Chris Esposito 2018-03-31 17:11:33 +11:00
parent a846895e53
commit e9d84cd963
2 changed files with 68 additions and 0 deletions

View File

@ -1,10 +1,48 @@
#include "usbcallhandler.h" #include "usbcallhandler.h"
#include <stdio.h> #include <stdio.h>
static void LIBUSB_CALL isoCallback(struct libusb_transfer * transfer){
//Thread mutex??
if(transfer->status!=LIBUSB_TRANSFER_CANCELLED){
printf("Copy the data...\n");
printf("Re-arm the endpoint...\n");
int error = libusb_submit_transfer(transfer);
if(error){
printf("Error re-arming the endpoint!\n");
}
}
return;
}
void usb_polling_function(libusb_context *ctx){
printf("usb_polling_function thread spawned\n");
struct timeval tv;
tv.tv_sec = 2;
tv.tv_usec = 0;
while(1){
printf("usb_polling_function begin loop\n");
if(libusb_event_handling_ok(ctx)){
libusb_handle_events_timeout(ctx, &tv);
}
}
}
usbCallHandler::usbCallHandler(unsigned short VID_in, unsigned short PID_in) usbCallHandler::usbCallHandler(unsigned short VID_in, unsigned short PID_in)
{ {
VID = VID_in; VID = VID_in;
PID = PID_in; PID = PID_in;
for(int k=0; k<NUM_ISO_ENDPOINTS; k++){
pipeID[k] = 0x81+k;
printf("pipeID %d = %d\n", k, pipeID[k]);
}
}
usbCallHandler::~usbCallHandler(){
//Kill off usb_polling_thread. Maybe join then get it to detect its own timeout condition.
} }
@ -49,6 +87,23 @@ int usbCallHandler::setup_usb_control(){
} }
int usbCallHandler::setup_usb_iso(){ int usbCallHandler::setup_usb_iso(){
int error;
printf("usbCallHandler::setup_usb_iso()\n");
for(int n=0;n<NUM_FUTURE_CTX;n++){
for (unsigned char k=0;k<NUM_ISO_ENDPOINTS;k++){
isoCtx[k][n] = libusb_alloc_transfer(ISO_PACKETS_PER_CTX);
libusb_fill_iso_transfer(isoCtx[k][n], handle, pipeID[k], dataBuffer[k][n], ISO_PACKET_SIZE*ISO_PACKETS_PER_CTX, ISO_PACKETS_PER_CTX, isoCallback, NULL, 4000);
libusb_set_iso_packet_lengths(isoCtx[k][n], ISO_PACKET_SIZE);
error = libusb_submit_transfer(isoCtx[k][n]);
if(error){
printf("libusb_submit_transfer #%d:%d FAILED with error %d %s\n", n, k, error, libusb_error_name(error));
return error;
}
}
}
usb_polling_thread = new std::thread(usb_polling_function, ctx);
return 0; return 0;
} }

View File

@ -2,6 +2,12 @@
#define USBCALLHANDLER_H #define USBCALLHANDLER_H
#include "libusb.h" #include "libusb.h"
#include <thread>
#define NUM_ISO_ENDPOINTS 1
#define NUM_FUTURE_CTX 4
#define ISO_PACKET_SIZE 750
#define ISO_PACKETS_PER_CTX 33
//EVERYTHING MUST BE SENT ONE BYTE AT A TIME, HIGH AND LOW BYTES SEPARATE, IN ORDER TO AVOID ISSUES WITH ENDIANNESS. //EVERYTHING MUST BE SENT ONE BYTE AT A TIME, HIGH AND LOW BYTES SEPARATE, IN ORDER TO AVOID ISSUES WITH ENDIANNESS.
typedef struct uds{ typedef struct uds{
@ -31,6 +37,7 @@ class usbCallHandler
{ {
public: public:
usbCallHandler(unsigned short VID_in, unsigned short PID_in); usbCallHandler(unsigned short VID_in, unsigned short PID_in);
~usbCallHandler();
int setup_usb_control(); int setup_usb_control();
int setup_usb_iso(); int setup_usb_iso();
int send_control_transfer(uint8_t RequestType, uint8_t Request, uint16_t Value, uint16_t Index, uint16_t Length, unsigned char *LDATA); int send_control_transfer(uint8_t RequestType, uint8_t Request, uint16_t Value, uint16_t Index, uint16_t Length, unsigned char *LDATA);
@ -41,6 +48,12 @@ private:
libusb_context *ctx = NULL; libusb_context *ctx = NULL;
libusb_device_handle *handle = NULL; libusb_device_handle *handle = NULL;
unsigned char inBuffer[256]; unsigned char inBuffer[256];
//USBIso Vars
unsigned char pipeID[NUM_ISO_ENDPOINTS];
libusb_transfer *isoCtx[NUM_ISO_ENDPOINTS][NUM_FUTURE_CTX];
unsigned char dataBuffer[NUM_ISO_ENDPOINTS][NUM_FUTURE_CTX][ISO_PACKET_SIZE*ISO_PACKETS_PER_CTX];
std::thread *usb_polling_thread;
}; };
#endif // USBCALLHANDLER_H #endif // USBCALLHANDLER_H