// Send a chunk of data to the micro. // static NeroStatus doSend( struct NeroHandle *handle, const uint8 *sendPtr, uint16 chunkSize, const char **error) { NeroStatus returnCode = NERO_SUCCESS; int uStatus = usbBulkWrite( handle->device, handle->outEndpoint, // write to out endpoint sendPtr, // write from send buffer chunkSize, // write this many bytes 5000, // timeout in milliseconds error ); CHECK_STATUS(uStatus, "doSend()", NERO_SEND); cleanup: return returnCode; }
// Write some raw bytes to the device. // FLStatus flWrite( struct FLContext *handle, const uint8 *bytes, uint32 count, uint32 timeout, const char **error) { FLStatus retVal = FL_SUCCESS; USBStatus uStatus = usbBulkWrite( handle->device, handle->commOutEP, // endpoint to write bytes, // data to send count, // number of bytes timeout, error ); CHECK_STATUS(uStatus, FL_USB_ERR, cleanup, "flWrite()"); cleanup: return retVal; }
void Sdr1kUsb::usbThread() { lock(); while (usbStatus >= 0) { waitUpdate(); if (usbStatus >= 0) { usbStatus = usbBulkWrite(outEP, outbuf, outcount); outcount = 0; if (usbStatus >= 0) { usbStatus = usbBulkRead(inEP, inbuf, 3); if (inbuf[0] != pstatus) { pstatus = inbuf[0]; signalInput(); } } } } signalInput(); unlock(); }
int main(int argc, char *argv[]) { struct arg_str *vpOpt = arg_str1("v", "vidpid", "<VID:PID>", " vendor ID and product ID (e.g 04B4:8613)"); struct arg_uint *toOpt = arg_uint0("t", "timeout", "<millis>", " timeout in milliseconds"); struct arg_int *epOpt = arg_int0("e", "endpoint", "<epNum>", " endpoint to write to"); struct arg_lit *benOpt = arg_lit0("b", "benchmark", " benchmark the operation"); struct arg_lit *chkOpt = arg_lit0("c", "checksum", " print 16-bit checksum"); struct arg_lit *helpOpt = arg_lit0("h", "help", " print this help and exit\n"); struct arg_file *fileOpt = arg_file1(NULL, NULL, "<fileName>", " the data to send"); struct arg_end *endOpt = arg_end(20); void* argTable[] = {vpOpt, toOpt, epOpt, benOpt, chkOpt, helpOpt, fileOpt, endOpt}; const char *progName = "bulk"; int numErrors; uint8 epNum = 0x06; FILE *inFile = NULL; uint8 *buffer = NULL; uint32 fileLen; struct USBDevice *deviceHandle = NULL; USBStatus uStatus; int retVal = 0; const char *error = NULL; double totalTime, speed; uint16 checksum = 0x0000; uint32 timeout = 5000; uint32 i; #ifdef WIN32 LARGE_INTEGER tvStart, tvEnd, freq; DWORD_PTR mask = 1; SetThreadAffinityMask(GetCurrentThread(), mask); QueryPerformanceFrequency(&freq); #else struct timeval tvStart, tvEnd; long long startTime, endTime; #endif if ( arg_nullcheck(argTable) != 0 ) { printf("%s: insufficient memory\n", progName); FAIL(1, cleanup); } numErrors = arg_parse(argc, argv, argTable); if ( helpOpt->count > 0 ) { printf("Bulk Write Tool Copyright (C) 2009-2011 Chris McClelland\n\nUsage: %s", progName); arg_print_syntax(stdout, argTable, "\n"); printf("\nWrite data to a bulk endpoint.\n\n"); arg_print_glossary(stdout, argTable," %-10s %s\n"); FAIL(0, cleanup); } if ( numErrors > 0 ) { arg_print_errors(stdout, endOpt, progName); printf("Try '%s --help' for more information.\n", progName); FAIL(2, cleanup); } if ( toOpt->count ) { timeout = toOpt->ival[0]; } inFile = fopen(fileOpt->filename[0], "rb"); if ( !inFile ) { fprintf(stderr, "Unable to open file %s\n", fileOpt->filename[0]); FAIL(3, cleanup); } fseek(inFile, 0, SEEK_END); fileLen = (uint32)ftell(inFile); fseek(inFile, 0, SEEK_SET); buffer = (uint8 *)malloc(fileLen); if ( !buffer ) { fprintf(stderr, "Unable to allocate memory for file %s\n", fileOpt->filename[0]); FAIL(4, cleanup); } if ( fread(buffer, 1, fileLen, inFile) != fileLen ) { fprintf(stderr, "Unable to read file %s\n", fileOpt->filename[0]); FAIL(5, cleanup); } if ( chkOpt->count ) { for ( i = 0; i < fileLen; i++ ) { checksum = (uint16)(checksum + buffer[i]); } printf("Checksum: 0x%04X\n", checksum); } if ( epOpt->count ) { epNum = (uint8)epOpt->ival[0]; } uStatus = usbInitialise(0, &error); CHECK_STATUS(uStatus, 6, cleanup); uStatus = usbOpenDevice(vpOpt->sval[0], 1, 0, 0, &deviceHandle, &error); CHECK_STATUS(uStatus, 7, cleanup); #ifdef WIN32 QueryPerformanceCounter(&tvStart); uStatus = usbBulkWrite(deviceHandle, epNum, buffer, fileLen, timeout, &error); QueryPerformanceCounter(&tvEnd); CHECK_STATUS(uStatus, 8, cleanup); totalTime = (double)(tvEnd.QuadPart - tvStart.QuadPart); totalTime /= freq.QuadPart; printf("Time: %fms\n", totalTime/1000.0); speed = (double)fileLen / (1024*1024*totalTime); #else gettimeofday(&tvStart, NULL); uStatus = usbBulkWrite(deviceHandle, epNum, buffer, fileLen, timeout, &error); gettimeofday(&tvEnd, NULL); CHECK_STATUS(uStatus, 8, cleanup); startTime = tvStart.tv_sec; startTime *= 1000000; startTime += tvStart.tv_usec; endTime = tvEnd.tv_sec; endTime *= 1000000; endTime += tvEnd.tv_usec; totalTime = (double)(endTime - startTime); totalTime /= 1000000; // convert from uS to S. speed = (double)fileLen / (1024*1024*totalTime); #endif if ( benOpt->count ) { printf("Speed: %f MB/s\n", speed); } cleanup: if ( buffer ) { free(buffer); } if ( inFile ) { fclose(inFile); } if ( deviceHandle ) { usbCloseDevice(deviceHandle, 0); } if ( error ) { fprintf(stderr, "%s\n", error); usbFreeError(error); } arg_freetable(argTable, sizeof(argTable)/sizeof(argTable[0])); return retVal; }