int main(int argc, char *argv[]) { char * pcBufRead; DWORD dwBytesRead; FILE * fh; FT_HANDLE ftHandle; FT_STATUS ftStatus; int iport; if(argc > 1) { sscanf(argv[1], "%d", &iport); } else { iport = 0; } fh = fopen("target.bin", "wb+"); if(fh == NULL) { printf("Cant open source file\n"); return 1; } ftStatus = FT_Open(iport, &ftHandle); if(ftStatus != FT_OK) { /* This can fail if the ftdi_sio driver is loaded use lsmod to check this and rmmod ftdi_sio to remove also rmmod usbserial */ printf("FT_Open(%d) failed\n", iport); return 1; } pcBufRead = (char *)malloc(BUF_SIZE); FT_ResetDevice(ftHandle); FT_SetBaudRate(ftHandle, 115200); FT_SetDtr(ftHandle); FT_SetRts(ftHandle); FT_SetFlowControl(ftHandle, FT_FLOW_RTS_CTS, 0, 0); FT_SetTimeouts(ftHandle, 0, 0); // infinite timeouts FT_SetBitMode(ftHandle, 0xFF, 0x01); FT_Read(ftHandle, pcBufRead, BUF_SIZE, &dwBytesRead); fwrite(pcBufRead, 1, dwBytesRead, fh); fclose(fh); free(pcBufRead); FT_Close(ftHandle); return 0; }
void FtdiDevices::setDtrRts() { // if (true) { FT_SetDtr(m_ftHandleA); // } // else { // FT_ClrDtr(dev.ftHandle); // } // if (true) { FT_SetRts(m_ftHandleA); // } // else { // FT_ClrRts(dev.ftHandle); // } }
bool CUsb3003DF2ETInterface::OpenDevice(void) { FT_STATUS ftStatus; int baudrate = 921600; //sets serial VID/PID for a Standard Device NOTE: This is for legacy purposes only. This can be ommitted. ftStatus = FT_SetVIDPID(m_uiVid, m_uiPid); if (ftStatus != FT_OK) {FTDI_Error((char *)"FT_SetVIDPID", ftStatus ); return false; } ftStatus = FT_OpenEx((PVOID)m_szDeviceSerial, FT_OPEN_BY_SERIAL_NUMBER, &m_FtdiHandle); if (ftStatus != FT_OK) { FTDI_Error((char *)"FT_OpenEx", ftStatus ); return false; } CTimePoint::TaskSleepFor(50); FT_Purge(m_FtdiHandle, FT_PURGE_RX | FT_PURGE_TX ); CTimePoint::TaskSleepFor(50); ftStatus = FT_SetDataCharacteristics(m_FtdiHandle, FT_BITS_8, FT_STOP_BITS_1, FT_PARITY_NONE); if ( ftStatus != FT_OK ) { FTDI_Error((char *)"FT_SetDataCharacteristics", ftStatus ); return false; } ftStatus = FT_SetFlowControl(m_FtdiHandle, FT_FLOW_RTS_CTS, 0x11, 0x13); if (ftStatus != FT_OK) { FTDI_Error((char *)"FT_SetFlowControl", ftStatus ); return false; } ftStatus = FT_SetRts (m_FtdiHandle); if (ftStatus != FT_OK) { FTDI_Error((char *)"FT_SetRts", ftStatus ); return false; } // for DF2ET-3003 interface pull DTR low to take AMBE3003 out of reset. ftStatus = FT_SetDtr( m_FtdiHandle ); CTimePoint::TaskSleepFor(50); if (ftStatus != FT_OK) { FTDI_Error((char *)"FT_SetDtr", ftStatus); return false; } ftStatus = FT_SetBaudRate(m_FtdiHandle, baudrate ); if (ftStatus != FT_OK) { FTDI_Error((char *)"FT_SetBaudRate", ftStatus ); return false; } ftStatus = FT_SetLatencyTimer(m_FtdiHandle, 4); if (ftStatus != FT_OK) { FTDI_Error((char *)"FT_SetLatencyTimer", ftStatus ); return false; } ftStatus = FT_SetUSBParameters(m_FtdiHandle, USB3XXX_MAXPACKETSIZE, 0); if (ftStatus != FT_OK) { FTDI_Error((char *)"FT_SetUSBParameters", ftStatus ); return false; } ftStatus = FT_SetTimeouts(m_FtdiHandle, 200, 200 ); if (ftStatus != FT_OK) { FTDI_Error((char *)"FT_SetTimeouts", ftStatus ); return false; } // done return true; }
static void FTClassicPort_setRts(FTClassicPort *self){ self->status = FT_SetRts(self->handle); }
int main(int argc, char *argv[]) { int retCode = -1; // Assume failure int f = 0; FT_STATUS ftStatus = FT_OK; FT_HANDLE ftHandle = NULL; int portNum = -1; // Deliberately invalid DWORD bytesToWrite = 0; DWORD bytesWritten = 0; int inputRate = -1; // Entered on command line int baudRate = 38400; // Rate to actually use int rates[] = {50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800, 576000, 921600}; if (argc > 1) { sscanf(argv[1], "%d", &portNum); } if (portNum < 0) { // Missing, or invalid. Just use first port. portNum = 0; } if (portNum > 16) { // User probably specified a baud rate without a port number printf("Syntax: %s [port number] [baud rate]\n", argv[0]); portNum = 0; } if (argc > 2) { sscanf(argv[2], "%d", &inputRate); for (f = 0; f < (int)(ARRAY_SIZE(rates)); f++) { if (inputRate == rates[f]) { // User entered a rate we support, so we'll use it. baudRate = inputRate; break; } } } if (baudRate < 0) baudRate = 9600; printf("Trying FTDI device %d at %d baud.\n", portNum, baudRate); ftStatus = FT_Open(portNum, &ftHandle); if (ftStatus != FT_OK) { printf("FT_Open(%d) failed, with error %d.\n", portNum, (int)ftStatus); printf("Use lsmod to check if ftdi_sio (and usbserial) are present.\n"); printf("If so, unload them using rmmod, as they conflict with ftd2xx.\n"); goto exit; } assert(ftHandle != NULL); ftStatus = FT_ResetDevice(ftHandle); if (ftStatus != FT_OK) { printf("Failure. FT_ResetDevice returned %d.\n", (int)ftStatus); goto exit; } ftStatus = FT_SetBaudRate(ftHandle, (ULONG)baudRate); if (ftStatus != FT_OK) { printf("Failure. FT_SetBaudRate(%d) returned %d.\n", baudRate, (int)ftStatus); goto exit; } ftStatus = FT_SetDataCharacteristics(ftHandle, FT_BITS_8, FT_STOP_BITS_1, FT_PARITY_NONE); if (ftStatus != FT_OK) { printf("Failure. FT_SetDataCharacteristics returned %d.\n", (int)ftStatus); goto exit; } // Indicate our presence to remote computer ftStatus = FT_SetDtr(ftHandle); if (ftStatus != FT_OK) { printf("Failure. FT_SetDtr returned %d.\n", (int)ftStatus); goto exit; } // Flow control is needed for higher baud rates ftStatus = FT_SetFlowControl(ftHandle, FT_FLOW_RTS_CTS, 0, 0); if (ftStatus != FT_OK) { printf("Failure. FT_SetFlowControl returned %d.\n", (int)ftStatus); goto exit; } // Assert Request-To-Send to prepare remote computer ftStatus = FT_SetRts(ftHandle); if (ftStatus != FT_OK) { printf("Failure. FT_SetRts returned %d.\n", (int)ftStatus); goto exit; } ftStatus = FT_SetTimeouts(ftHandle, 3000, 3000); // 3 seconds if (ftStatus != FT_OK) { printf("Failure. FT_SetTimeouts returned %d\n", (int)ftStatus); goto exit; } bytesToWrite = (DWORD)(sizeof(testPattern) - 1); // Don't write string terminator ftStatus = FT_Write(ftHandle, testPattern, bytesToWrite, &bytesWritten); if (ftStatus != FT_OK) { printf("Failure. FT_Write returned %d\n", (int)ftStatus); goto exit; } if (bytesWritten != bytesToWrite) { printf("Failure. FT_Write wrote %d bytes instead of %d.\n", (int)bytesWritten, (int)bytesToWrite); goto exit; } // Success retCode = 0; printf("Successfully wrote %d bytes\n", (int)bytesWritten); exit: if (ftHandle != NULL) FT_Close(ftHandle); return retCode; }
void ResetNES (int rtype) { if (ParPort == -1) { if (rtype & RESET_PLAYMODE) { //clr /RTS=1 ftStatus = FT_ClrRts(ftHandleB); if (ftStatus != FT_OK) { MessageBox(topHWnd, "USB Error: ClrRts Failed!", "ResetNES", MB_OK | MB_ICONERROR); return; } } else { //set /RTS=0 ftStatus = FT_SetRts(ftHandleB); if (ftStatus != FT_OK) { MessageBox(topHWnd, "USB Error: SetRts Failed!", "ResetNES", MB_OK | MB_ICONERROR); return; } } if (!(rtype & RESET_NORESET)) { // pull /RESET low clear D2 //set /dtr=0 ftStatus = FT_ClrDtr(ftHandleB); if (ftStatus != FT_OK) { MessageBox(topHWnd, "USB Error: ClrDtr Failed!", "ResetNES", MB_OK | MB_ICONERROR); return; } Sleep(SLEEP_SHORT); } // pull /RESET high set D2 //clr /dtr=1 ftStatus = FT_SetDtr(ftHandleB); if (ftStatus != FT_OK) { MessageBox(topHWnd, "USB Error: SetDtr Failed!", "ResetNES", MB_OK | MB_ICONERROR); return; } Sleep(SLEEP_SHORT); InitPort(); Sleep(SLEEP_SHORT); } else { if (rtype & RESET_ALTPORT) shadow &= 0xF7; else shadow |= 0x08; if (rtype & RESET_PLAYMODE) shadow &= 0xFE; else shadow |= 0x01; if (!(rtype & RESET_NORESET)) { shadow &= 0xFB; pwControl(shadow); // pull /RESET low Sleep(SLEEP_SHORT); } shadow |= 0x04; // pull /RESET high pwControl(shadow); Sleep(SLEEP_SHORT); } }
int main(int argc, char *argv[]) { int retCode = -1; // Assume failure int f = 0; DWORD driverVersion = 0; FT_STATUS ftStatus = FT_OK; FT_HANDLE ftHandle = NULL; int portNum = 0; // First device found size_t bufferSize = 64 * 1024; DWORD bytesToWrite; DWORD bytesWritten = 0; DWORD bytesReceived = 0; DWORD bytesRead = 0; struct timeval startTime; int journeyDuration; unsigned char *writeBuffer = NULL; unsigned char *readBuffer = NULL; int queueChecks = 0; ULONG rates[] = {300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800, 576000, 921600, 1500000, 2000000, 3000000}; // TODO: detect high-speed device and use 8240000 UNUSED_PARAMETER(argc); UNUSED_PARAMETER(argv); // Make printfs immediate (no buffer) setvbuf(stdout, NULL, _IONBF, 0); writeBuffer = (unsigned char *)malloc((size_t)bufferSize); if (writeBuffer == NULL) goto exit; // Fill write buffer with consecutive values for (f = 0; f < (int)bufferSize; f++) { writeBuffer[f] = (unsigned char)f + 64; } printf("Opening FTDI device %d.\n", portNum); ftStatus = FT_Open(portNum, &ftHandle); if (ftStatus != FT_OK) { printf("FT_Open(%d) failed, with error %d.\n", portNum, (int)ftStatus); printf("On Linux, lsmod can check if ftdi_sio (and usbserial) are present.\n"); printf("If so, unload them using rmmod, as they conflict with ftd2xx.\n"); goto exit; } assert(ftHandle != NULL); ftStatus = FT_GetDriverVersion(ftHandle, &driverVersion); if (ftStatus != FT_OK) { printf("Failure. FT_GetDriverVersion returned %d.\n", (int)ftStatus); goto exit; } printf("Using D2XX version %08x\n", driverVersion); ftStatus = FT_ResetDevice(ftHandle); if (ftStatus != FT_OK) { printf("Failure. FT_ResetDevice returned %d.\n", (int)ftStatus); goto exit; } // Flow control is needed for higher baud rates ftStatus = FT_SetFlowControl(ftHandle, FT_FLOW_RTS_CTS, 0, 0); if (ftStatus != FT_OK) { printf("Failure. FT_SetFlowControl returned %d.\n", (int)ftStatus); goto exit; } ftStatus = FT_SetDataCharacteristics(ftHandle, FT_BITS_8, FT_STOP_BITS_1, FT_PARITY_NONE); if (ftStatus != FT_OK) { printf("Failure. FT_SetDataCharacteristics returned %d.\n", (int)ftStatus); goto exit; } for (f = 0; f < (int)ARRAY_SIZE(rates); f++) { ftStatus = FT_SetBaudRate(ftHandle, rates[f]); if (ftStatus != FT_OK) { printf("Failure. FT_SetBaudRate(%d) returned %d.\n", (int)rates[f], (int)ftStatus); goto exit; } // Assert Request-To-Send to prepare receiver ftStatus = FT_SetRts(ftHandle); if (ftStatus != FT_OK) { printf("Failure. FT_SetRts returned %d.\n", (int)ftStatus); goto exit; } if (rates[f] < 57600) { // Keep test duration reasonable by transferring fewer // bytes at low baud rates. bytesToWrite = rates[f] / 4; } else { bytesToWrite = bufferSize; } printf("\nBaud rate %d. Writing %d bytes to loopback device...\n", (int)rates[f], (int)bytesToWrite); ftStatus = FT_Write(ftHandle, writeBuffer, bytesToWrite, &bytesWritten); if (ftStatus != FT_OK) { printf("Failure. FT_Write returned %d\n", (int)ftStatus); goto exit; } if (bytesWritten != bytesToWrite) { printf("Failure. FT_Write wrote %d bytes instead of %d.\n", (int)bytesWritten, (int)bytesToWrite); goto exit; } printf("%d bytes written.\n", (int)bytesWritten); // Keep checking queue until D2XX has received all the bytes we wrote. // Estimate total time to write and read, so we can time-out. // Each byte has 8 data bits plus a stop bit and perhaps a 1-bit gap. journeyDuration = bytesWritten * (8 + 1 + 1) / (int)rates[f]; journeyDuration += 1; // Round up journeyDuration *= 2; // It's a return journey printf("Estimate %d seconds remain.\n", journeyDuration); gettimeofday(&startTime, NULL); for (bytesReceived = 0, queueChecks = 0; bytesReceived < bytesWritten; queueChecks++) { // Periodically check for time-out if (queueChecks % 32 == 0) { struct timeval now; struct timeval elapsed; gettimeofday(&now, NULL); timersub(&now, &startTime, &elapsed); if (elapsed.tv_sec > (long int)journeyDuration) { // We've waited too long. Give up. printf("\nTimed out after %ld seconds\n", elapsed.tv_sec); break; } // Display number of bytes D2XX has received printf("%s%d", queueChecks == 0 ? "Number of bytes in D2XX receive-queue: " : ", ", (int)bytesReceived); } ftStatus = FT_GetQueueStatus(ftHandle, &bytesReceived); if (ftStatus != FT_OK) { printf("\nFailure. FT_GetQueueStatus returned %d.\n", (int)ftStatus); goto exit; } } printf("\nGot %d (of %d) bytes.\n", (int)bytesReceived, (int)bytesWritten); // Even if D2XX has the wrong number of bytes, create our // own buffer so we can read and display them. free(readBuffer); // Free previous iteration's buffer. readBuffer = (unsigned char *)calloc(bytesReceived, sizeof(unsigned char)); if (readBuffer == NULL) { printf("Failed to allocate %d bytes.\n", bytesReceived); goto exit; } // Then copy D2XX's buffer to ours. ftStatus = FT_Read(ftHandle, readBuffer, bytesReceived, &bytesRead); if (ftStatus != FT_OK) { printf("Failure. FT_Read returned %d.\n", (int)ftStatus); goto exit; } if (bytesRead != bytesReceived) { printf("Failure. FT_Read only read %d (of %d) bytes.\n", (int)bytesRead, (int)bytesReceived); goto exit; } if (0 != memcmp(writeBuffer, readBuffer, bytesRead)) { printf("Failure. Read-buffer does not match write-buffer.\n"); printf("Write buffer:\n"); dumpBuffer(writeBuffer, bytesReceived); printf("Read buffer:\n"); dumpBuffer(readBuffer, bytesReceived); goto exit; } // Fail if D2XX's queue lacked (or had surplus) bytes. if (bytesReceived != bytesWritten) { printf("Failure. D2XX received %d bytes but we expected %d.\n", (int)bytesReceived, (int)bytesWritten); dumpBuffer(readBuffer, bytesReceived); goto exit; } // Check that queue hasn't gathered any additional unexpected bytes bytesReceived = 4242; // deliberately junk ftStatus = FT_GetQueueStatus(ftHandle, &bytesReceived); if (ftStatus != FT_OK) { printf("Failure. FT_GetQueueStatus returned %d.\n", (int)ftStatus); goto exit; } if (bytesReceived != 0) { printf("Failure. %d bytes in input queue -- expected none.\n", (int)bytesReceived); goto exit; } } // Success printf("\nTest PASSED.\n"); retCode = 0; exit: free(readBuffer); free(writeBuffer); if (ftHandle != NULL) FT_Close(ftHandle); return retCode; }