void D2xxSerial::setLatencyTimer(int ms) { UCHAR latency = ms; FT_STATUS err = FT_SetLatencyTimer(handle, latency); if(err!=FT_OK) { printf("Couldn't get latency timer, error: %s\n", getError(err)); } }
bool FTDXXDevice::connect (int baudrate) throw () { #ifdef FOUND_ftd2xx disconnect(); _status = FT_SetVIDPID(0x0403, 0x6001); FXXCHECK("Could not set IDs."); _status = FT_Open(0, &_handle); FXXCHECK("Could not open handle."); _status = FT_SetBaudRate(_handle, baudrate); FXXCHECK("Could not set baudrate."); _status = FT_SetLatencyTimer(_handle, 0); FXXCHECK("Unable to set latency timer."); _status = FT_Purge(_handle, FT_PURGE_RX | FT_PURGE_TX); FXXCHECK("Unable to purge buffers."); _initialized = true; FTDLOG("Connection successful."); return true; #else _initialized = false; FTDERROR("cannot connect FTD2xx device: compiled without required libraries"); return false; #endif }
FT_STATUS ftdimut_setup() { FT_STATUS ftStatus; unsigned char timer; ftStatus = FT_SetVIDPID(USB_VID, USB_PID); if(ftStatus != FT_OK) return ftStatus; ftStatus = FT_Open(0, &ftHandle); if(ftStatus != FT_OK) return ftStatus; ftStatus = FT_ResetDevice(ftHandle); if(ftStatus != FT_OK) return ftStatus; ftStatus = FT_Purge(ftHandle, FT_PURGE_RX | FT_PURGE_TX); if(ftStatus != FT_OK) return ftStatus; ftStatus = FT_SetBaudRate(ftHandle, 15625); if(ftStatus != FT_OK) return ftStatus; ftStatus = FT_SetDataCharacteristics(ftHandle, FT_BITS_8, FT_STOP_BITS_1, FT_PARITY_NONE); if(ftStatus != FT_OK) return ftStatus; ftStatus = FT_SetFlowControl(ftHandle, FT_FLOW_NONE, 0, 0); if(ftStatus != FT_OK) return ftStatus; ftStatus = FT_SetTimeouts(ftHandle, 1000, 1000); if(ftStatus != FT_OK) return ftStatus; ftStatus = FT_GetLatencyTimer(ftHandle, &timer); if(ftStatus != FT_OK) return ftStatus; ftStatus = FT_SetLatencyTimer(ftHandle, 1); if(ftStatus != FT_OK) return ftStatus; return FT_OK; }
bool CMmcUsbHndlBase::Configure() { if( !AreLibraryFunctionsLoaded() ) { perror("Library not loaded"); return false; } bool oResult = true; const BYTE LATENCY_TIMER(2); const DWORD INPUT_TRANSFER_SIZE(128); const DWORD OUTPUT_TRANSFER_SIZE(128); FT_STATUS ftStatus = FT_OK; //Data Characteristics:ByteSize, StopBit, Parity if(FT_OK != (ftStatus = FT_SetDataCharacteristics(m_Handle, FT_BITS_8, FT_STOP_BITS_1, FT_PARITY_NONE))) { std::string errormsg = GetFtStatusDescription(ftStatus); errormsg += ":FT_SetDataCharacteristics"; perror(errormsg.c_str()); oResult = false; } //FlowControl: Dtr, Rts, Xon, Xoff if(FT_OK != (ftStatus = FT_SetFlowControl(m_Handle, FT_FLOW_NONE, 0, 0))) { std::string errormsg = GetFtStatusDescription(ftStatus); errormsg += ":FT_SetFlowControl"; perror(errormsg.c_str()); oResult = false; } //LatencyTimer if(FT_OK != (ftStatus = FT_SetLatencyTimer(m_Handle, LATENCY_TIMER))) { std::string errormsg = GetFtStatusDescription(ftStatus); errormsg += ":FT_SetLatencyTimer"; perror(errormsg.c_str()); oResult = false; } //TransferSize if(FT_OK != (ftStatus = FT_SetUSBParameters(m_Handle, INPUT_TRANSFER_SIZE, OUTPUT_TRANSFER_SIZE))) { std::string errormsg = GetFtStatusDescription(ftStatus); errormsg += ":FT_SetUSBParameters"; perror(errormsg.c_str()); oResult = false; } return oResult; }
static int presto_close(void) { int result = ERROR_OK; #if BUILD_PRESTO_FTD2XX == 1 DWORD ftbytes; if (presto->handle == (FT_HANDLE)INVALID_HANDLE_VALUE) return result; presto->status = FT_Purge(presto->handle, FT_PURGE_TX | FT_PURGE_RX); if (presto->status != FT_OK) result = ERROR_JTAG_DEVICE_ERROR; presto->status = FT_Write(presto->handle, &presto_init_seq, sizeof(presto_init_seq), &ftbytes); if (presto->status != FT_OK || ftbytes != sizeof(presto_init_seq)) result = ERROR_JTAG_DEVICE_ERROR; presto->status = FT_SetLatencyTimer(presto->handle, 16); if (presto->status != FT_OK) result = ERROR_JTAG_DEVICE_ERROR; presto->status = FT_Close(presto->handle); if (presto->status != FT_OK) result = ERROR_JTAG_DEVICE_ERROR; else presto->handle = (FT_HANDLE)INVALID_HANDLE_VALUE; #elif BUILD_PRESTO_LIBFTDI == 1 presto->retval = ftdi_write_data(&presto->ftdic, presto_init_seq, sizeof(presto_init_seq)); if (presto->retval != sizeof(presto_init_seq)) result = ERROR_JTAG_DEVICE_ERROR; presto->retval = ftdi_set_latency_timer(&presto->ftdic, 16); if (presto->retval < 0) result = ERROR_JTAG_DEVICE_ERROR; presto->retval = ftdi_usb_close(&presto->ftdic); if (presto->retval < 0) result = ERROR_JTAG_DEVICE_ERROR; else ftdi_deinit(&presto->ftdic); #endif return result; }
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; }
int CKMotionIO::SetLatency(UCHAR LatencyTimer) { FT_STATUS ftStatus; unsigned char c; Mutex->Lock(); ftStatus = FT_GetLatencyTimer (ftHandle, &c); ftStatus = FT_SetLatencyTimer(ftHandle,LatencyTimer); if (ftStatus == FT_OK) { ftStatus = FT_GetLatencyTimer (ftHandle, &c); // LatencyTimer set ftStatus = FT_SetChars (ftHandle, '\n', 1, 0,0); if (ftStatus == FT_OK) { // Event set Mutex->Unlock(); return 0; } else { // FT_SetLatencyTimer FAILED! ErrorMessageBox("Unable to set USB Event Character"); Mutex->Unlock(); return 1; } } else { // FT_SetLatencyTimer FAILED! ErrorMessageBox("Unable to set USB Latency timer"); Mutex->Unlock(); return 1; } }
int dxl_hal_open() { FT_STATUS ft_status; dxl_hal_close(); ft_status = FT_Open( 1, &ghFt_Handle ); if( ft_status != FT_OK ) goto DXL_HAL_OPEN_ERROR; ft_status = FT_ResetDevice( ghFt_Handle ); if( ft_status != FT_OK ) goto DXL_HAL_OPEN_ERROR; ft_status = FT_SetDataCharacteristics( ghFt_Handle, FT_BITS_8, FT_STOP_BITS_1, FT_PARITY_NONE ); if( ft_status != FT_OK ) goto DXL_HAL_OPEN_ERROR; ft_status = FT_SetFlowControl( ghFt_Handle, FT_FLOW_NONE, (UCHAR)0, (UCHAR)0 ); if( ft_status != FT_OK ) goto DXL_HAL_OPEN_ERROR; ft_status = FT_SetLatencyTimer( ghFt_Handle, LATENCY_TIME ); if( ft_status != FT_OK ) goto DXL_HAL_OPEN_ERROR; ft_status = FT_SetUSBParameters( ghFt_Handle, IN_TRASFER_SIZE, 0 ); if( ft_status != FT_OK ) goto DXL_HAL_OPEN_ERROR; ft_status = FT_SetTimeouts( ghFt_Handle, 0, 0 ); if( ft_status != FT_OK ) goto DXL_HAL_OPEN_ERROR; ft_status = FT_Purge( ghFt_Handle, FT_PURGE_RX|FT_PURGE_TX ); if( ft_status != FT_OK ) goto DXL_HAL_OPEN_ERROR; return 1; DXL_HAL_OPEN_ERROR: dxl_hal_close(); return 0; }
bool DYNA_initialize(FT_HANDLE ftHandleDYNA) { FT_STATUS ft_status; ft_status = FT_ResetDevice(ftHandleDYNA); if( ft_status != FT_OK ) goto DYNA_init_error; ft_status = FT_SetDataCharacteristics(ftHandleDYNA, FT_BITS_8, FT_STOP_BITS_1, FT_PARITY_NONE); if( ft_status != FT_OK ) goto DYNA_init_error; ft_status = FT_SetFlowControl(ftHandleDYNA, FT_FLOW_NONE, (UCHAR)0, (UCHAR)0); if( ft_status != FT_OK ) goto DYNA_init_error; ft_status = FT_SetLatencyTimer(ftHandleDYNA, LATENCY_TIME); if( ft_status != FT_OK ) goto DYNA_init_error; ft_status = FT_SetUSBParameters(ftHandleDYNA, IN_TRASFER_SIZE, 0); if( ft_status != FT_OK ) goto DYNA_init_error; ft_status = FT_SetTimeouts(ftHandleDYNA, 0, 0); if( ft_status != FT_OK ) goto DYNA_init_error; ft_status = FT_Purge(ftHandleDYNA, FT_PURGE_RX|FT_PURGE_TX); if( ft_status != FT_OK ) goto DYNA_init_error; ft_status = FT_SetBaudRate(ftHandleDYNA, 1000000); if( ft_status != FT_OK ) goto DYNA_init_error; return true; DYNA_init_error: qDebug() << "\n\nUSB2Dynamixel Initialization Error!"; FT_Close(ftHandleDYNA); return false; }
static FT_HANDLE OpenUSBDevice( int nType, int nId ) { FT_HANDLE hndl = 0; unsigned i; DWORD dwDevs; FT_DEVICE_LIST_INFO_NODE *pNodes = 0; if( FT_CreateDeviceInfoList( &dwDevs) == FT_OK ) { pNodes = (FT_DEVICE_LIST_INFO_NODE *)calloc( sizeof(FT_DEVICE_LIST_INFO_NODE) , dwDevs ); if( FT_GetDeviceInfoList( pNodes, &dwDevs ) == FT_OK ) { for( i = 0; i < dwDevs; i++ ) { if( pNodes[i].Type == nType && pNodes[i].ID == nId ) { if( FT_OpenEx( pNodes[i].Description, FT_OPEN_BY_DESCRIPTION, &hndl ) != FT_OK ) { hndl = 0; goto cleanup; } /* if */ if( FT_SetLatencyTimer( hndl, s_nUsbLatency ) != FT_OK || FT_SetTimeouts( hndl, s_nReadTimeout, s_nWriteTimeout ) != FT_OK ) { FT_Close( s_hndl ); hndl = 0; goto cleanup; } break; } /* if */ } /* for */ } /* if */ } /* if */ cleanup: if( pNodes ) free( pNodes ); return hndl; }
static int ublast_ftd2xx_init(struct ublast_lowlevel *low) { FT_STATUS status; FT_HANDLE *ftdih = ublast_getftdih(low); uint8_t latency_timer; LOG_INFO("usb blaster interface using FTD2XX"); /* Open by device description */ if (low->ublast_device_desc == NULL) { LOG_WARNING("no usb blaster device description specified, " "using default 'USB-Blaster'"); low->ublast_device_desc = "USB-Blaster"; } #if IS_WIN32 == 0 /* Add non-standard Vid/Pid to the linux driver */ status = FT_SetVIDPID(low->ublast_vid, low->ublast_pid); if (status != FT_OK) { LOG_WARNING("couldn't add %4.4x:%4.4x", low->ublast_vid, low->ublast_pid); } #endif status = FT_OpenEx(low->ublast_device_desc, FT_OPEN_BY_DESCRIPTION, ftdih); if (status != FT_OK) { DWORD num_devices; LOG_ERROR("unable to open ftdi device: %s", ftd2xx_status_string(status)); status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY); if (status == FT_OK) { char **desc_array = malloc(sizeof(char *) * (num_devices + 1)); unsigned int i; for (i = 0; i < num_devices; i++) desc_array[i] = malloc(64); desc_array[num_devices] = NULL; status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | FT_OPEN_BY_DESCRIPTION); if (status == FT_OK) { LOG_ERROR("ListDevices: %" PRIu32, (uint32_t)num_devices); for (i = 0; i < num_devices; i++) LOG_ERROR("%i: %s", i, desc_array[i]); } for (i = 0; i < num_devices; i++) free(desc_array[i]); free(desc_array); } else { printf("ListDevices: NONE\n"); } return ERROR_JTAG_INIT_FAILED; } status = FT_SetLatencyTimer(*ftdih, 2); if (status != FT_OK) { LOG_ERROR("unable to set latency timer: %s", ftd2xx_status_string(status)); return ERROR_JTAG_INIT_FAILED; } status = FT_GetLatencyTimer(*ftdih, &latency_timer); if (status != FT_OK) { LOG_ERROR("unable to get latency timer: %s", ftd2xx_status_string(status)); return ERROR_JTAG_INIT_FAILED; } LOG_DEBUG("current latency timer: %i", latency_timer); status = FT_SetBitMode(*ftdih, 0x00, 0); if (status != FT_OK) { LOG_ERROR("unable to disable bit i/o mode: %s", ftd2xx_status_string(status)); return ERROR_JTAG_INIT_FAILED; } return ERROR_OK; }
int main(int argc, char* argv[]) { FT_HANDLE fthandle; FT_STATUS status; DWORD numdev = 0; // Open the first device connected to the system (which has index 0). You can use the various other // functions such as open_by_description to make this much more flexible and user friendly to let the user // choose which device to open. See the D2xx Programmers Guide for more information // Check how many FTDI devices are connected and installed. If one or more connected, open the first one status = FT_CreateDeviceInfoList(&numdev); if ((status == FT_OK) && (numdev > 0) ) { // Open the device now status = FT_Open(0, &fthandle); if(status != FT_OK) printf("status not ok %d\n", status); // Set the In transfer size. You can set up to 64K if required. Ideally, use a small value like this for // receiving a few bytes at a time or a larger value if you will be transferring large amounts of data status = FT_SetUSBParameters(fthandle, 256, 0); if(status != FT_OK) printf("status not ok %d\n", status); // Reset the device status = FT_ResetDevice(fthandle); if(status != FT_OK) printf("status not ok %d\n", status); // Set the handshaking mode in the driver, for I2C chips this has no affect on the external I2C interface // since it does not have handshake lines but this enables internal handshake in the driver status = FT_SetFlowControl(fthandle, FT_FLOW_RTS_CTS, FT_STOP_BITS_1, FT_PARITY_NONE); if(status != FT_OK) printf("status not ok %d\n", status); // Set Timeouts to ensure a Read or Write will return if unable to be completed // Setting both read and write timeouts to 5 seconds status = FT_SetTimeouts(fthandle, 5000, 5000); if(status != FT_OK) printf("status not ok %d\n", status); // Set Latency Timer (keeping it at default of 16ms here) status = FT_SetLatencyTimer(fthandle, 16); if(status != FT_OK) printf("status not ok %d\n", status); // Now write some data to the chips buffer char data_out[12] = "HELLO WORLD"; DWORD w_data_len = 12; DWORD data_written; status = FT_Write(fthandle, data_out, w_data_len, &data_written); if(status != FT_OK) printf("status not ok %d\n", status); else printf("12 Bytes Sent, waiting for bytes to come back\n"); /**********************************************************************/ // The I2C Master should now be able to read these 12 bytes from the FT-X over I2C. This example expects the // I2C Master to send the bytes back over I2C to the FT-X /**********************************************************************/ // Now read the data which the I2C master has written to the FT-X char data_in[12]; DWORD data_read; DWORD MyBytesReceived = 0; DWORD SoftwareTimeout = 0; // Wait for the FT-X to send our 12 bytes back to the PC while((MyBytesReceived <12) && (SoftwareTimeout < 500)) { FT_GetQueueStatus(fthandle, &MyBytesReceived); Sleep(1); SoftwareTimeout ++; } // Check if the loop exited due to timing out or receiving 12 bytes if(SoftwareTimeout == 500) { printf("Timed out waiting for data\n"); } else { // Now read the received bytes status = FT_Read(fthandle, data_in, MyBytesReceived, &data_read); if(status != FT_OK) printf("status not ok %d\n", status); else printf("data read %s\n", data_in); } // Close the device status = FT_Close(fthandle); } else { printf("No FTDI devices connected to the computer \n"); } printf("Press Return To End Program"); getchar(); printf("closed \n"); return 0; }
int dev_open_uart (int n_dev_indx, FT_HANDLE *ph_device) { FT_STATUS ft_status; DWORD dw_num_devs; LONG devLocation; ft_status = FT_ListDevices(&dw_num_devs, NULL, FT_LIST_NUMBER_ONLY); if (ft_status != FT_OK) return FALSE; if (dw_num_devs == 0){ // No devices were found return FALSE; } ft_status = FT_ListDevices((void*)n_dev_indx, &devLocation, FT_LIST_BY_INDEX | FT_OPEN_BY_LOCATION); if (ft_status != FT_OK) { return FALSE; } ft_status |= FT_ListDevices((void*)n_dev_indx, &devDescriptor, FT_LIST_BY_INDEX | FT_OPEN_BY_DESCRIPTION); ft_status |= FT_ListDevices((void*)n_dev_indx, &devSerial, FT_LIST_BY_INDEX | FT_OPEN_BY_SERIAL_NUMBER); if (ft_status != FT_OK){ return FALSE; } #define FT_Classic 0 #if FT_Classic ft_status |= FT_OpenEx((void*)devLocation, FT_OPEN_BY_LOCATION, ph_device); ft_status |= FT_SetTimeouts(*ph_device, 500, 500); ft_status |= FT_SetLatencyTimer(*ph_device, 2); // Divisor selection // BAUD = 3000000 / Divisor // Divisor = (N + 0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875) // Divisor = 24 ==> Baud 125000 ft_status |= FT_SetDivisor(*ph_device, 3000000 / 125000); // Set UART format 8N1 ft_status |= FT_SetDataCharacteristics(*ph_device, FT_BITS_8, FT_STOP_BITS_1, FT_PARITY_NONE); if (ft_status != FT_OK){ return FALSE; } // Just in case FT_Purge(*ph_device, FT_PURGE_TX | FT_PURGE_RX); #else // Open a device for overlapped I/O using its serial number *ph_device = FT_W32_CreateFile( (LPCTSTR)devLocation, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED | FT_OPEN_BY_LOCATION, 0); if (*ph_device == INVALID_HANDLE_VALUE) { // FT_W32_CreateDevice failed return FALSE; } // ---------------------------------------- // --- Set comm parameters // ---------------------------------------- FTDCB ftDCB; FTTIMEOUTS ftTimeouts; FTCOMSTAT ftPortStatus; DWORD dw_port_error; if (!FT_W32_GetCommState(*ph_device, &ftDCB)) { return FALSE; } // Divisor selection // BAUD = 3000000 / Divisor // Divisor = (N + 0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875) // Divisor = 24 ==> Baud 125000 ftDCB.BaudRate = 38400; ftDCB.fBinary = TRUE; /* Binary Mode (skip EOF check) */ ftDCB.fParity = FALSE; /* Enable parity checking */ ftDCB.fOutxCtsFlow = FALSE; /* CTS handshaking on output */ ftDCB.fOutxDsrFlow = FALSE; /* DSR handshaking on output */ ftDCB.fDtrControl = DTR_CONTROL_DISABLE; /* DTR Flow control */ ftDCB.fTXContinueOnXoff = FALSE; ftDCB.fErrorChar = FALSE; // enable error replacement ftDCB.fNull = FALSE; // enable null stripping ftDCB.fRtsControl = RTS_CONTROL_DISABLE; // RTS flow control ftDCB.fAbortOnError = TRUE; // abort reads/writes on error ftDCB.fOutX = FALSE; /* Enable output X-ON/X-OFF */ ftDCB.fInX = FALSE; /* Enable input X-ON/X-OFF */ ftDCB.fNull = FALSE; /* Enable Null stripping */ ftDCB.fRtsControl = RTS_CONTROL_DISABLE; /* Rts Flow control */ ftDCB.fAbortOnError = TRUE; /* Abort all reads and writes on Error */ // 8N1 ftDCB.ByteSize = 8; /* Number of bits/byte, 4-8 */ ftDCB.Parity = NOPARITY; /* 0-4=None,Odd,Even,Mark,Space */ ftDCB.StopBits = ONESTOPBIT; /* 0,1,2 = 1, 1.5, 2 */ if (!FT_W32_SetCommState(*ph_device, &ftDCB)) { return FALSE; } FT_W32_GetCommState(*ph_device, &ftDCB); // Set serial port Timeout values FT_W32_GetCommTimeouts(*ph_device, &ftTimeouts); ftTimeouts.ReadIntervalTimeout = 0; ftTimeouts.ReadTotalTimeoutMultiplier = 0; ftTimeouts.ReadTotalTimeoutConstant = 200; ftTimeouts.WriteTotalTimeoutConstant = 0; ftTimeouts.WriteTotalTimeoutMultiplier = 0; FT_W32_SetCommTimeouts(*ph_device, &ftTimeouts); FT_W32_ClearCommError(*ph_device, &dw_port_error, &ftPortStatus); FT_W32_PurgeComm(*ph_device, PURGE_TXCLEAR | PURGE_RXCLEAR | PURGE_RXABORT | PURGE_TXABORT); #endif // End of W32 device init return TRUE; }
static int presto_open_ftd2xx(char *req_serial) { uint32_t i; DWORD numdevs; DWORD vidpid; char devname[FT_DEVICE_NAME_LEN]; FT_DEVICE device; BYTE presto_data; DWORD ftbytes; presto->handle = (FT_HANDLE)INVALID_HANDLE_VALUE; #if IS_WIN32 == 0 /* Add non-standard Vid/Pid to the linux driver */ presto->status = FT_SetVIDPID(PRESTO_VID, PRESTO_PID); if (presto->status != FT_OK) { LOG_ERROR("couldn't add PRESTO VID/PID"); exit(-1); } #endif presto->status = FT_ListDevices(&numdevs, NULL, FT_LIST_NUMBER_ONLY); if (presto->status != FT_OK) { LOG_ERROR("FT_ListDevices failed: %s", ftd2xx_status_string(presto->status)); return ERROR_JTAG_DEVICE_ERROR; } LOG_DEBUG("FTDI devices available: %" PRIu32, (uint32_t)numdevs); for (i = 0; i < numdevs; i++) { presto->status = FT_Open(i, &(presto->handle)); if (presto->status != FT_OK) { /* this is not fatal, the device may be legitimately open by other process, *hence debug message only */ LOG_DEBUG("FT_Open failed: %s", ftd2xx_status_string(presto->status)); continue; } LOG_DEBUG("FTDI device %i open", (int)i); presto->status = FT_GetDeviceInfo(presto->handle, &device, &vidpid, presto->serial, devname, NULL); if (presto->status == FT_OK) { if (vidpid == PRESTO_VID_PID && (req_serial == NULL || !strcmp(presto->serial, req_serial))) break; } else LOG_DEBUG("FT_GetDeviceInfo failed: %s", ftd2xx_status_string( presto->status)); LOG_DEBUG("FTDI device %i does not match, closing", (int)i); FT_Close(presto->handle); presto->handle = (FT_HANDLE)INVALID_HANDLE_VALUE; } if (presto->handle == (FT_HANDLE)INVALID_HANDLE_VALUE) return ERROR_JTAG_DEVICE_ERROR; /* presto not open, return */ presto->status = FT_SetLatencyTimer(presto->handle, 1); if (presto->status != FT_OK) return ERROR_JTAG_DEVICE_ERROR; presto->status = FT_SetTimeouts(presto->handle, 100, 0); if (presto->status != FT_OK) return ERROR_JTAG_DEVICE_ERROR; presto->status = FT_Purge(presto->handle, FT_PURGE_TX | FT_PURGE_RX); if (presto->status != FT_OK) return ERROR_JTAG_DEVICE_ERROR; presto_data = 0xD0; presto->status = FT_Write(presto->handle, &presto_data, 1, &ftbytes); if (presto->status != FT_OK) return ERROR_JTAG_DEVICE_ERROR; /* delay between first write/read turnaround (after purge?) necessary * under Linux for unknown reason, * probably a bug in library threading */ usleep(100000); presto->status = FT_Read(presto->handle, &presto_data, 1, &ftbytes); if (presto->status != FT_OK) return ERROR_JTAG_DEVICE_ERROR; if (ftbytes != 1) { LOG_DEBUG("PRESTO reset"); presto->status = FT_Purge(presto->handle, FT_PURGE_TX | FT_PURGE_RX); if (presto->status != FT_OK) return ERROR_JTAG_DEVICE_ERROR; presto->status = FT_SetBitMode(presto->handle, 0x80, 1); if (presto->status != FT_OK) return ERROR_JTAG_DEVICE_ERROR; presto->status = FT_SetBaudRate(presto->handle, 9600); if (presto->status != FT_OK) return ERROR_JTAG_DEVICE_ERROR; presto_data = 0; for (i = 0; i < 4 * 62; i++) { presto->status = FT_Write(presto->handle, &presto_data, 1, &ftbytes); if (presto->status != FT_OK) return ERROR_JTAG_DEVICE_ERROR; } usleep(100000); presto->status = FT_SetBitMode(presto->handle, 0x00, 0); if (presto->status != FT_OK) return ERROR_JTAG_DEVICE_ERROR; presto->status = FT_Purge(presto->handle, FT_PURGE_TX | FT_PURGE_RX); if (presto->status != FT_OK) return ERROR_JTAG_DEVICE_ERROR; presto_data = 0xD0; presto->status = FT_Write(presto->handle, &presto_data, 1, &ftbytes); if (presto->status != FT_OK) return ERROR_JTAG_DEVICE_ERROR; /* delay between first write/read turnaround (after purge?) necessary under Linux for unknown reason, probably a bug in library threading */ usleep(100000); presto->status = FT_Read(presto->handle, &presto_data, 1, &ftbytes); if (presto->status != FT_OK) return ERROR_JTAG_DEVICE_ERROR; if (ftbytes != 1) { LOG_DEBUG("PRESTO not responding"); return ERROR_JTAG_DEVICE_ERROR; } } presto->status = FT_SetTimeouts(presto->handle, 0, 0); if (presto->status != FT_OK) return ERROR_JTAG_DEVICE_ERROR; presto->status = FT_Write(presto->handle, &presto_init_seq, sizeof(presto_init_seq), &ftbytes); if (presto->status != FT_OK || ftbytes != sizeof(presto_init_seq)) return ERROR_JTAG_DEVICE_ERROR; return ERROR_OK; }
int ftdi_init() { FT_DEVICE ftDevice; DWORD deviceID; char SerialNumber[16+1]; char Description[64+1]; // Does an FTDI device exist? printf("Checking for FTDI devices...\n"); ftStatus = FT_CreateDeviceInfoList(&dwNumDevs); // Get the number of FTDI devices if (ftStatus != FT_OK) // Did the command execute OK? { printf("Error in getting the number of devices\n"); return 1; // Exit with error } if (dwNumDevs < 1) // Exit if we don't see any { printf("There are no FTDI devices installed\n"); return 1; // Exist with error } printf("%d FTDI devices found - the count includes individual ports on a single chip\n", dwNumDevs); ftHandle=NULL; //go thru' list of devices for(int i=0; i<dwNumDevs; i++) { printf("Open port %d\n",i); ftStatus = FT_Open(i, &ftHandle); if (ftStatus != FT_OK) { printf("Open Failed with error %d\n", ftStatus); printf("If runing on Linux then try <rmmod ftdi_sio> first\n"); continue; } FT_PROGRAM_DATA ftData; char ManufacturerBuf[32]; char ManufacturerIdBuf[16]; char DescriptionBuf[64]; char SerialNumberBuf[16]; ftData.Signature1 = 0x00000000; ftData.Signature2 = 0xffffffff; ftData.Version = 0x00000003; //3 = FT2232H extensions ftData.Manufacturer = ManufacturerBuf; ftData.ManufacturerId = ManufacturerIdBuf; ftData.Description = DescriptionBuf; ftData.SerialNumber = SerialNumberBuf; ftStatus = FT_EE_Read(ftHandle,&ftData); if (ftStatus == FT_OK) { printf("\tDevice: %s\n\tSerial: %s\n", ftData.Description, ftData.SerialNumber); printf("\tDevice Type: %02X\n", ftData.IFAIsFifo7 ); break; } else { printf("\tCannot read ext flash\n"); } } if(ftHandle==NULL) { printf("NO FTDI chip with FIFO function\n"); return -1; } //ENABLE SYNC FIFO MODE ftStatus |= FT_SetBitMode(ftHandle, 0xFF, 0x00); ftStatus |= FT_SetBitMode(ftHandle, 0xFF, 0x40); if (ftStatus != FT_OK) { printf("Error in initializing1 %d\n", ftStatus); FT_Close(ftHandle); return 1; // Exit with error } UCHAR LatencyTimer = 2; //our default setting is 2 ftStatus |= FT_SetLatencyTimer(ftHandle, LatencyTimer); ftStatus |= FT_SetUSBParameters(ftHandle,0x10000,0x10000); ftStatus |= FT_SetFlowControl(ftHandle,FT_FLOW_RTS_CTS,0x10,0x13); if (ftStatus != FT_OK) { printf("Error in initializing2 %d\n", ftStatus); FT_Close(ftHandle); return 1; // Exit with error } //return with success return 0; }
bool CApoxObj::open( const char *szFileName, unsigned long flags ) { const char *p; unsigned long busspeed = 125; m_emergencyInfo = 0; char szDrvParams[ MAX_PATH ]; m_initFlag = flags; m_RxMsgState = USB_IDLE; // save parameter string and conbert to upper case strncpy( szDrvParams, szFileName, MAX_PATH ); _strupr( szDrvParams ); // Initiate statistics m_stat.cntReceiveData = 0; m_stat.cntReceiveFrames = 0; m_stat.cntTransmitData = 0; m_stat.cntTransmitFrames = 0; m_stat.cntBusOff = 0; m_stat.cntBusWarnings = 0; m_stat.cntOverruns = 0; // if open we have noting to do if ( m_bRun ) return true; // serial p = strtok( szDrvParams, ";" ); if ( NULL != p ) { strcpy( m_SerialNumber, p ); } // Bus-Speed p = strtok( NULL, ";" ); if ( NULL != p ) { if ( ( NULL != strstr( p, "0x" ) ) || ( NULL != strstr( p, "0X" ) ) ) { sscanf( p + 2, "%x", &busspeed ); } else { busspeed = atol( p ); } } // Handle busspeed uint8_t nSpeed = CAN_BAUD_1000; switch ( busspeed ) { case 125: nSpeed = CAN_BAUD_125; break; case 250: nSpeed = CAN_BAUD_250; break; case 500: nSpeed = CAN_BAUD_500; break; case 1000: nSpeed = CAN_BAUD_1000; break; default: nSpeed = CAN_BAUD_125; break; } FT_STATUS ftStatus; ftStatus = FT_OpenEx( (void *)m_SerialNumber, FT_OPEN_BY_SERIAL_NUMBER, &m_ftHandle ); if ( !FT_SUCCESS( ftStatus ) ) return false; ftStatus = FT_ResetDevice( m_ftHandle ); ftStatus = FT_Purge( m_ftHandle, FT_PURGE_RX | FT_PURGE_TX ); ftStatus = FT_SetTimeouts(m_ftHandle, 378, 128); ftStatus = FT_SetUSBParameters ( m_ftHandle, 2048, 2048 ); ftStatus = FT_SetLatencyTimer( m_ftHandle, 3 ); // Run run run ..... m_bRun = true; #ifdef WIN32 // Start write thread DWORD threadId; if ( NULL == ( m_hTreadTransmit = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE) workThreadTransmit, this, 0, &threadId ) ) ) { // Failure close(); return false; } // Start read thread if ( NULL == ( m_hTreadReceive = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE) workThreadReceive, this, 0, &threadId ) ) ) { // Failure close(); return false; } // Release the mutex UNLOCK_MUTEX( m_apoxMutex ); UNLOCK_MUTEX( m_receiveMutex ); UNLOCK_MUTEX( m_transmitMutex ); UNLOCK_MUTEX( m_responseMutex ); #else // LINUX pthread_attr_t thread_attr; pthread_attr_init( &thread_attr ); // Create the log write thread. if ( pthread_create( &m_threadId, &thread_attr, workThreadTransmit, this ) ) { syslog( LOG_CRIT, "canallogger: Unable to create apoxdrv write thread."); rv = false; fclose( m_flog ); } // Create the log write thread. if ( pthread_create( &m_threadId, &thread_attr, workThreadReceive, this ) ) { syslog( LOG_CRIT, "canallogger: Unable to create apoxdrv receive thread."); rv = false; fclose( m_flog ); } // We are open m_bOpen = true; // Release the mutex pthread_mutex_unlock( &m_apoxMutex ); #endif // Switch to main mode if in boot mode if ( RUNMODE_BOOT == getAdapterRunMode() ) { setAdapterRunMode( RUNMODE_MAIN ); } // Set baudrate setBaudrate( nSpeed ); // Set initial filter/mask switch( m_initFlag & 0x03 ) { case 0: case 3: m_filtermask.rx_buff1_ext = 0; m_filtermask.rx_buff2_ext = 1; break; case 1: m_filtermask.rx_buff1_ext = 0; m_filtermask.rx_buff2_ext = 0; break; case 2: m_filtermask.rx_buff1_ext = 1; m_filtermask.rx_buff2_ext = 1; break; } m_filtermask.RXM0 = 0; m_filtermask.RXM1 = 0; m_filtermask.RXF0 = 0; m_filtermask.RXF1 = 0; m_filtermask.RXF2 = 0; m_filtermask.RXF3 = 0; m_filtermask.RXF4 = 0; m_filtermask.RXF5 = 0; setAdapterFilterMask( &m_filtermask ); // Set transmission mode short trmode = ( (short)( m_initFlag & 0x0c ) >> 2 ); if ( 0 == trmode ) trmode = TRMODE_NORMAL; setTransmissionMode( trmode ); return true; }