/* * Async Write and Read Loopback test */ #include #include #include #include #include "ftd3xx.h" #define MULTI_ASYNC_BUFFER_SIZE 1048576 //32768 // 8388608 // #define MULTI_ASYNC_NUM 8 #define NUM_ITERATIONS 1 pthread_t writeThread; pthread_t readThread; int writerRunning = 0; int readerRunning = 0; int r = 0; int exitWriter = 0; int exitReader = 0; UCHAR *acWriteBuf = NULL; UCHAR *acReadBuf = NULL; ULONG ulBytesToWrite = MULTI_ASYNC_BUFFER_SIZE; ULONG ulBytesToRead = MULTI_ASYNC_BUFFER_SIZE; ULONG ulBytesWritten[MULTI_ASYNC_NUM] = {0}; ULONG ulBytesRead[MULTI_ASYNC_NUM] = {0}; OVERLAPPED vOverlappedWrite[MULTI_ASYNC_NUM] = {{0}}; OVERLAPPED vOverlappedRead[MULTI_ASYNC_NUM] = {{0}}; FT_HANDLE ftHandle; static void* asyncRead(void *arg) { int i,j; FT_STATUS ftStatus = FT_OK; BOOL bResult = TRUE; UCHAR *acReadBuf = calloc(MULTI_ASYNC_NUM * MULTI_ASYNC_BUFFER_SIZE, sizeof(UCHAR)); printf("Starting %s.\n", __FUNCTION__); /*Create the overlapped io event for asynchronous transfer*/ for (j=0; j FT_AbortPipe return =%d \n",ftStatus); bResult = FALSE; goto exit; } if (ulBytesRead[j] != ulBytesToRead) { printf("FT_GetOverlappedResult >> ulBytesRead[j=%d]=%d != %d ulBytesToRead \n",j,ulBytesRead[j],ulBytesToRead); continue; } break; } #endif } exit: if(bResult == FALSE) { break; } } /*Delete the overlapped io event for asynchronous transfer*/ for (j=0; jWrite\n"); bResult = FALSE; ftStatus = FT_AbortPipe(ftHandle, 0x82); printf("Write -> FT_AbortPipe return =%d \n",ftStatus); goto exit; } if (ulBytesWritten[j] != ulBytesToWrite) { //printf("FT_GetOverlappedResult >> ulBytesWritten[j=%d]=%d != %d ulBytesToWrite\n",j,ulBytesWritten[j],ulBytesToWrite); bResult = FALSE; goto exit; } } } exit: if(bResult == FALSE) { break; } } /*Delete the overlapped io event for asynchronous transfer*/ for (j=0; j FT_SetStreamPipe failed!\n"); bResult = FALSE; goto exit; } ftStatus = FT_SetStreamPipe(ftHandle, FALSE, FALSE, 0x82, ulBytesToRead); if (FT_FAILED(ftStatus)) { printf("0x82 --> FT_SetStreamPipe failed!\n"); bResult = FALSE; goto exit; } FT_SetPipeTimeout(ftHandle,0x02,0); FT_SetPipeTimeout(ftHandle,0x82,0); // Start thread to read bytes r = pthread_create(&readThread, NULL, &asyncRead,NULL); if (r != 0) { printf("Failed to create read thread (%d)\n", r); goto exit; } readerRunning = 1; // Start thread to write bytes r = pthread_create(&writeThread, NULL, &asyncWrite, NULL); if (r != 0) { printf("Failed to create write thread (%d)\n", r); goto exit; } writerRunning = 1; (void)pthread_join(readThread, NULL); readerRunning = 0; // Wait for threads to finish (void)pthread_join(writeThread, NULL); writerRunning = 0; exit: if (readerRunning) { exitReader = 1; (void)pthread_join(readThread, NULL); } if (writerRunning) { exitWriter = 1; (void)pthread_join(writeThread, NULL); } /*Close device*/ FT_Close(ftHandle); /*Stop stream transfer*/ FT_ClearStreamPipe(ftHandle, FALSE, FALSE, 0x02); FT_ClearStreamPipe(ftHandle, FALSE, FALSE, 0x82); if(acReadBuf != NULL) free(acReadBuf); if(acWriteBuf != NULL) free(acWriteBuf); return 0; }