static void Reset(int reset) { switch (board_type) { case BT_powermeter: if (reset) clear_cbus (2); else set_cbus (2); break; case BT_powermeterproto: if (reset) clear_cbus (3); else set_cbus (3); break; default: if (reset) Fail (FT_ClrDtr (handle)); else Fail (FT_SetDtr (handle)); break; } }
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_setDtr(FTClassicPort *self){ self->status = FT_SetDtr(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; }
bool CUsb3003DF2ETInterface::ResetDevice(void) { bool ok = false; FT_STATUS ftStatus; int len, i; char rxpacket[100]; std::cout << "Trying DF2ET-3003 soft reset" << std::endl; DWORD n, b; char txpacket[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; char reset_packet[7] = { PKT_HEADER, 0, 3, 0, PKT_RESET, PKT_PARITYBYTE, 3 ^ PKT_RESET ^ PKT_PARITYBYTE }; char *p; for (i = 0; i < 35; i++) { p = &txpacket[0]; n = 10; do { ftStatus = FT_Write( m_FtdiHandle, p, n, &b); if (FT_OK != ftStatus) { return 1; } n -= b; p += b; } while (n > 0); } p = &reset_packet[0]; n = 7; do { ftStatus = FT_Write( m_FtdiHandle, p, n, &b); if (FT_OK != ftStatus) { return 1; } n -= b; p += b; } while (n > 0); len = FTDI_read_packet( m_FtdiHandle, rxpacket, sizeof(rxpacket) ); ok = ((len == 7) && (rxpacket[4] == PKT_READY)); if ( ok ) { std::cout << "DF2ET-3003 soft reset succeeded" << std::endl; } else { std::cout << "DF2ET-3003 soft reset failed" << std::endl; std::cout << "Trying DF2ET-3003 hard reset" << std::endl; ftStatus = FT_ClrDtr( m_FtdiHandle ); CTimePoint::TaskSleepFor(10); ftStatus = FT_SetDtr( m_FtdiHandle ); CTimePoint::TaskSleepFor(10); len = FTDI_read_packet( m_FtdiHandle, rxpacket, sizeof(rxpacket) ); ok = ((len == 7) && (rxpacket[4] == PKT_READY)); if ( ok ) { std::cout << "DF2ET-3003 hard reset succeeded" << std::endl; } else { std::cout << "DF2ET-3003 hard reset failed" << std::endl; } } return ok; }
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 nifalcon_load_firmware(falcon_device* dev, const char* firmware_filename) { unsigned long bytes_written; unsigned char check_msg_1[3] = {0x0a, 0x43, 0x0d}; unsigned char check_msg_2[1] = "A"; unsigned char send_buf[128], receive_buf[128]; FILE* firmware_file; if(!dev->is_open) nifalcon_error_return(NIFALCON_DEVICE_NOT_FOUND_ERROR, "tried load firmware to an unopened device"); if((dev->falcon_status_code = FT_ResetDevice(dev->falcon)) != FT_OK) return -dev->falcon_status_code; //Set to: // 9600 baud // 8n1 // No Flow Control // RTS Low // DTR High if((dev->falcon_status_code = FT_SetBaudRate(dev->falcon, 9600)) != FT_OK) return -dev->falcon_status_code; if((dev->falcon_status_code = FT_SetDataCharacteristics(dev->falcon, FT_BITS_8, FT_STOP_BITS_1, FT_PARITY_NONE)) != FT_OK) return -dev->falcon_status_code; if((dev->falcon_status_code = FT_SetFlowControl(dev->falcon, FT_FLOW_NONE, 0, 0)) != FT_OK) return -dev->falcon_status_code; if((dev->falcon_status_code = FT_ClrRts(dev->falcon)) != FT_OK) return -dev->falcon_status_code; if((dev->falcon_status_code = FT_ClrDtr(dev->falcon)) != FT_OK) return -dev->falcon_status_code; if((dev->falcon_status_code = FT_SetDtr(dev->falcon)) != FT_OK) return -dev->falcon_status_code; //Send 3 bytes: 0x0a 0x43 0x0d if((dev->falcon_status_code = nifalcon_write(dev, check_msg_1, 3)) < 3) return -dev->falcon_status_code; //Expect 5 bytes back if((dev->falcon_status_code = nifalcon_read(dev, receive_buf, 5, 1000)) < 5) return -dev->falcon_status_code; //Set to: // DTR Low // 140000 baud (0x15 clock ticks per signal) if((dev->falcon_status_code = FT_ClrDtr(dev->falcon)) != FT_OK) return -dev->falcon_status_code; if((dev->falcon_status_code = FT_SetBaudRate(dev->falcon, 140000)) != FT_OK) return -dev->falcon_status_code; //Send "A" character if((dev->falcon_status_code = nifalcon_write(dev, check_msg_2, 1)) < 0) return -dev->falcon_status_code; //Expect back 2 bytes: // 0x13 0x41 if((dev->falcon_status_code = nifalcon_read(dev, receive_buf, 2, 1000)) < 0) return -dev->falcon_status_code; firmware_file = fopen(firmware_filename, "rb"); if(!firmware_file) { nifalcon_error_return(NIFALCON_FIRMWARE_NOT_FOUND_ERROR, "cannot find falcon firmware file"); } while(!feof(firmware_file)) { int firmware_bytes_read; int i; firmware_bytes_read = fread(send_buf, 1, 128, firmware_file); if((dev->falcon_status_code = nifalcon_write(dev, send_buf, firmware_bytes_read)) < 0) return dev->falcon_status_code; if((dev->falcon_status_code = nifalcon_read(dev, receive_buf, firmware_bytes_read, 1000)) < 0) return dev->falcon_status_code; if(firmware_bytes_read < 128) break; } fclose(firmware_file); if((dev->falcon_status_code = FT_SetBaudRate(dev->falcon, 1456312)) != FT_OK) return -dev->falcon_status_code; return FT_OK; }