Esempio n. 1
0
void OBDDevice::setupOBD() {
    int offset = 0;
    unsigned char buf[32];    // just picked a power of two for whatever reason

    /* Each of the commands needs to be pre-allocated WITHOUT a null-terminating character */
    unsigned char cmd_ATZ[] = { 'A', 'T', 'Z', '\r' };
    unsigned char cmd_ATE0[] = { 'A', 'T', 'E', '0', '\r' };
    unsigned char cmd_ATL0[] = { 'A', 'T', 'L', '0', '\r' };
    unsigned char cmd_ATS0[] = { 'A', 'T', 'S', '0', '\r' };
    unsigned char cmd_ATSPA3[] = { 'A', 'T', 'S', 'P', 'A', '3', '\r' };
    unsigned char cmd_010D1[] = { '0', '1', '0', 'D', '1', '\r' };

    /* Stop any previous OBD queries and clear the read buffer */
    ftdi_write_data( ftdi, (unsigned char*) "", sizeof( "" ) );
    usleep( 10000 );
    ftdi_read_data( ftdi, buf, sizeof( buf ) );

    /* Send "AT Z\r" to reset the device */
    ftdi_write_data( ftdi, cmd_ATZ, sizeof( cmd_ATZ ) );

    usleep( 800000 );
    
    do {
        usleep( 100000 );
        offset += ftdi_read_data( ftdi, (buf + offset),
                                  (sizeof( buf ) - offset) );
    } while ( offset < 21 );

    offset = 0;

    /* Send "AT E 0\r" to disable command echo */
    ftdi_write_data( ftdi, cmd_ATE0, sizeof( cmd_ATE0 ) );
    /* Send "AT L 0\r" to disable linefeeds */
    ftdi_write_data( ftdi, cmd_ATL0, sizeof( cmd_ATL0 ) );
    /* Send "AT S 0\r" to disable spaces */
    ftdi_write_data( ftdi, cmd_ATS0, sizeof( cmd_ATS0 ) );
    /* Send "AT SP A 3\r" to try ELM237 protocol 3 */
    ftdi_write_data( ftdi, cmd_ATSPA3, sizeof( cmd_ATSPA3 ) );
    
    /* Clear the read buffer of the output from previous comands */
    do {
        usleep( 1 );
        offset += ftdi_read_data( ftdi, (buf + offset),
                                  (sizeof( buf ) - offset) );
    } while ( offset < 24 );

    offset = 0;

    /* Send "010D1\r" (arbitrary) to confirm the protocol selection */
    ftdi_write_data( ftdi, cmd_010D1, sizeof( cmd_010D1 ) );

    do {
        usleep( 5000 );
        offset += ftdi_read_data( ftdi, (buf + offset),
                                  (sizeof( buf ) - offset ) );
    } while ( offset < 9 );
}
Esempio n. 2
0
int
USBPort::receive_packet (unsigned char *packet)
{
  time_t tp = time (NULL);
  int bytesReceived = 0, bytesLeft;
  do
    {
      bytesReceived = ftdi_read_data (&ftdic, packet, 1);
    }
  while (time (NULL) - tp < SLEEPTIME && bytesReceived == 0);
  if (bytesReceived == 0)
    return TIMEOUT;
  else
    {
      if (packet[0] != DATA)
	{
	  if (packet[0] == ACK)
	    return ACK;
	  else if (packet[0] == END)
	    return END;
	  else
	    return NAK;
	}
      else
	{
	  unsigned int remaining = PACKETSIZE - 1;
	  tp = time (NULL);
	  bytesReceived = 0;
	  do
	    {
	      bytesLeft = remaining;

	      bytesReceived =
		ftdi_read_data (&ftdic, &packet[PACKETSIZE - remaining],
				bytesLeft);
	      remaining -= bytesReceived;
	      tp = time (NULL);



	    }
	  while (time (NULL) - tp < SLEEPTIME && remaining != 0);
	  if (remaining > 0)
	    return TIMEOUT;
	  else
	    return DATA;
	}
    }
}
Esempio n. 3
0
void FT232R_SPI::ftdi_spi_rw(unsigned char *write, unsigned char *read, int size){

    unsigned char * buffWrite = (unsigned char *)malloc((size+2)*((8*3)+3));
    unsigned char * buffRead = (unsigned char *)malloc((size+2)*((8*3)+3));
    int index = 0;


    state &= ~CSN;
    buffWrite[index++] = state;

    for(int i = 0; i < size; i++){
        index += ftdi_byte_encode(buffWrite+index, write[i]);
    }

    state |= CSN;
    buffWrite[index++] = state;


#ifdef _WIN32
    DWORD written = 0;
    FT_Write(ftHandle,(void*)buffWrite, index, &written);
    written = 0;
    FT_Read(ftHandle, (void*) buffRead, index, &written);
#elif __linux__
    unsigned int written = 0;
    written = ftdi_write_data(ftdi, (unsigned char*)buffWrite, index);
    ftdi_read_data(ftdi, (unsigned char*) buffRead, index);
#endif

    ftdi_spi_decode(buffRead, written, read);

    free(buffWrite);
    free(buffRead);

}
Esempio n. 4
0
static int presto_read(uint8_t *buf, uint32_t size)
{
	uint32_t ftbytes = 0;

	struct timeval timeout, now;
	gettimeofday(&timeout, NULL);
	timeval_add_time(&timeout, 1, 0);	/* one second timeout */

	while (ftbytes < size) {
		presto->retval = ftdi_read_data(&presto->ftdic, buf + ftbytes, size - ftbytes);
		if (presto->retval < 0) {
			LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&presto->ftdic));
			return ERROR_JTAG_DEVICE_ERROR;
		}
		ftbytes += presto->retval;

		gettimeofday(&now, NULL);
		if ((now.tv_sec > timeout.tv_sec) ||
				((now.tv_sec == timeout.tv_sec) && (now.tv_usec > timeout.tv_usec)))
			break;
	}

	if (ftbytes != size) {
		/* this is just a warning, there might have been timeout when detecting PRESTO,
		 *which is not fatal */
		LOG_WARNING("couldn't read the requested number of bytes from PRESTO (%u < %u)",
			(unsigned)ftbytes, (unsigned)size);
		return ERROR_JTAG_DEVICE_ERROR;
	}

	return ERROR_OK;
}
Esempio n. 5
0
int FtdiWrapper::get(uint8_t *buffer, unsigned int bufSize, unsigned int minGet)
{
    unsigned int bytesRead = 0;
//    unsigned int trys = 0;
    int status;

    while(bytesRead < minGet)
    {
        //usb read
        status = ftdi_read_data(ftdiContext, buffer, bufSize);

        //check for usb errors
        if(status < 0)
            throw runtime_error( ftdi_get_error_string(ftdiContext) );

        bytesRead += status;//keep track of how much we got
        buffer += status;//move buffer pointer
        bufSize -= status;//adjust buffer size

        //TODO: progressively sleep?

//        trys ++;
//        if(trys > 10)
//            throw runtime_error("timeout occured durring read to usb");

    }

    return bytesRead;
}
Esempio n. 6
0
int platform_buffer_read(uint8_t *data, int size)
{
	int index = 0;
	platform_buffer_flush();
	while((index += ftdi_read_data(ftdic, data + index, size-index)) != size);
	return size;
}
Esempio n. 7
0
int Read_JTAG(struct ftdi_context * handle, unsigned char * buff, unsigned int len)
{
        unsigned char cmd [2] = { 0x00 };
        int ret = 0;

        cmd[0] = 0x83;
        cmd[1] = 0x87;

        if((ret = ftdi_usb_purge_rx_buffer (handle)) < 0)
        {
            fprintf(stderr, "ftdi_usb_purge_rx_buffer failed: %d (%s)\n", ret, ftdi_get_error_string(&ux400_ftdic));
            return EXIT_FAILURE;
        }

        if((ret = ftdi_usb_purge_tx_buffer (handle)) < 0)
        {
            fprintf(stderr, "ftdi_usb_purge_tx_buffer failed: %d (%s)\n", ret, ftdi_get_error_string(&ux400_ftdic));
            return EXIT_FAILURE;
        }

        if((ret = ftdi_write_data (handle, cmd, 2)) < 0)
        {
            fprintf(stderr, "ftdi_write_data failed: %d (%s)\n", ret, ftdi_get_error_string(&ux400_ftdic));
            return EXIT_FAILURE;
        }

        if((ret = ftdi_read_data (handle, buff, len)) < 0)
        {
            fprintf(stderr, "ftdi_read_data failed: %d (%s)\n", ret, ftdi_get_error_string(&ux400_ftdic));
            return EXIT_FAILURE;
        }

        return ret;
}
	bool FalconCommLibFTDI::read(uint8_t* str, uint32_t size)
	{
		LOG_DEBUG("Reading " << size << " bytes of data");
		unsigned long bytes_rx;

		if(!m_isCommOpen)
		{
			LOG_ERROR("Device not open");
			m_errorCode = FALCON_COMM_DEVICE_NOT_VALID_ERROR;
			return false;
		}

		m_lastBytesRead = ftdi_read_data((m_falconDevice), str, size);
		if(m_lastBytesRead < 0)
		{
			LOG_ERROR("Device error " << m_deviceErrorCode);
			m_deviceErrorCode = m_lastBytesRead;
			m_errorCode = FALCON_COMM_DEVICE_ERROR;
			return false;
		}
		if(m_lastBytesRead < size)
		{
			LOG_ERROR("Read amount " << m_lastBytesRead << " less than requested size " << size);
			m_errorCode = FALCON_COMM_READ_ERROR;
			return false;
		}
		return true;
	}
Esempio n. 9
0
/**
 * Read a certain amount of bytes from the LA8's FTDI device.
 *
 * @param devc The struct containing private per-device-instance data. Must not
 *            be NULL. devc->ftdic must not be NULL either.
 * @param buf The buffer where the received data will be stored. Must not
 *            be NULL.
 * @param size The number of bytes to read. Must be >= 1.
 * @return The number of bytes read, or a negative value upon errors.
 */
SR_PRIV int la8_read(struct dev_context *devc, uint8_t *buf, int size)
{
	int bytes_read;

	/* Note: Caller checked that devc and devc->ftdic != NULL. */

	if (!buf) {
		sr_err("%s: buf was NULL.", __func__);
		return SR_ERR_ARG;
	}

	if (size <= 0) {
		sr_err("%s: size was <= 0.", __func__);
		return SR_ERR_ARG;
	}

	bytes_read = ftdi_read_data(devc->ftdic, buf, size);

	if (bytes_read < 0) {
		sr_err("%s: ftdi_read_data: (%d) %s.", __func__,
		       bytes_read, ftdi_get_error_string(devc->ftdic));
	} else if (bytes_read != size) {
		// sr_err("%s: Bytes to read: %d, bytes read: %d.",
		//        __func__, size, bytes_read);
	}

	return bytes_read;
}
Esempio n. 10
0
int Ft245sync::read(unsigned char * buf)
{
    int res = ftdi_read_data(ftdic, buf, getReadChunkSize());
    std::cerr << "res rd: " << res << std::endl;
    std::cerr << "rd data[0]: " << (int)buf[0] << std::endl;
    return res; 
}
Esempio n. 11
0
/* FIXME: trouble compiling gettimeofday() in windows? add this:
 * http://www.suacommunity.com/dictionary/gettimeofday-entry.php
 */
int FtdiDevice::readData( const unsigned char* data, int length, int timeout ) const
{
	//fprintf( stderr, "readData: about to read %i bytes.\n", length ); //LOG
	
	if ( ! isOpen() ) return RV_DEVICE_NOT_OPEN;
	struct timeval tNow, tEnd, tOut;
	
	tOut.tv_sec = timeout / 1000; tOut.tv_usec = ( timeout % 1000 ) * 1000;
	gettimeofday( &tNow, 0 );
	timeradd( &tNow, &tOut, &tEnd );
	
	int readTotal = 0, readLast = 0;
	bool reread = ( length > 0 );
	
	while ( reread ) {
		readTotal += readLast;
		if ( readTotal == length ) break;
		readLast = ftdi_read_data( context_,
															const_cast<unsigned char*>( data ) + readTotal,
															length - readTotal );
		
		gettimeofday( &tNow, 0 );
		reread = ( readLast >= 0 );
		if ( reread ) reread = ( readLast > 0 || timercmp( &tNow, &tEnd, < ) );
	}
	
	return readLast < 0 ? readLast : readTotal;
}
Esempio n. 12
0
static int spi_buf_r(uint8_t *b, size_t s)
{
	const size_t total_size = s * 8 * BYTES_PER_BIT;
	int j = 0, pos;
	uint8_t *buf = calloc(1, total_size);

	if (ftdi_read_data(ftdi, buf, total_size) != total_size) {
		fprintf(stderr, "problem reading device\n");
		free(buf);
		return 0;
	}

	for (pos = 0; pos < s; pos++) {
		uint8_t v = 0;
		uint8_t bit;

		// most significant bit first
		for (bit = (1 << 7); bit > 0; bit >>= 1) {
			j += (BYTES_PER_BIT + 1) / 2;
			if (buf[j++] & PIN_FMISO)
				v |= bit;
		}

		b[pos] = v;
	}

	free(buf);
	return j / 8 / BYTES_PER_BIT;
}
Esempio n. 13
0
/* Read data from the FTDI chip */
int raw_read(struct mpsse_context *mpsse, unsigned char *buf, int size)
{
	int n = 0, r = 0;

	if(mpsse->mode)
	{
		while(n < size)
		{
			r = ftdi_read_data(&mpsse->ftdi, buf, size);
			if(r < 0) break;
			n += r;
		}

		if(mpsse->flush_after_read)
		{
			/* 
			 * Make sure the buffers are cleared after a read or subsequent reads may fail.
			 * 
			 * Is this needed anymore? It slows down repetitive read operations by ~8%.
			 */
			ftdi_usb_purge_rx_buffer(&mpsse->ftdi);
		}
	}

	return n;
}
Esempio n. 14
0
int ftdi_device_read(ftdi_device_t* dev, unsigned char* data, size_t num) {
  ssize_t result = 0, num_read = 0;
  double time, period = 0.0;

  error_clear(&dev->error);
  
  timer_start(&time);
  while ((num_read < num) &&
    ((period = timer_stop(time)) <= dev->timeout) &&
    ((result = ftdi_read_data(dev->libftdi_context, &data[num_read],
      num-num_read)) >= 0)) {
    if (result)
      timer_start(&time);
    num_read += result;
  }
  dev->num_read += num_read;
    
  if (result < 0) {
    error_setf(&dev->error, FTDI_ERROR_READ, "%03:%03", dev->bus,
      dev->address);
    return -error_get(&dev->error);
  }
  else if (!num_read && (period > dev->timeout)) {
    error_setf(&dev->error, FTDI_ERROR_TIMEOUT, "%03:%03", dev->bus,
      dev->address);
    return -error_get(&dev->error);
  }

  return num_read;
}
Esempio n. 15
0
int tx(int d)
{
uint8_t cmd[] = { 0x37, 7, d };
uint8_t ans = 0;
int i,f;
    f = ftdi_write_data(ftdi, cmd, 3);
    if(f != 3) {
	fprintf(stderr, "unable to write command3 to ftdi device: %d (%s)\n", f, ftdi_get_error_string(ftdi));
	err = 1;
	return 0;
    }
    
    for(i=0; i < 10; i++) {    
	f = ftdi_read_data(ftdi,&ans,1);
	if(f == 0) {
	    usleep(1);
	    continue;
	}
        if(f == 1) break;
	else {
    	    fprintf(stderr, "unable to read data from ftdi device: %d (%s)\n", f, ftdi_get_error_string(ftdi));
	    err = 1;
	    return 0;
	}
    }

    return ans & 0xff;
}
Esempio n. 16
0
int FTDIDevice::communicate (unsigned char* packet, unsigned char* buffer, int send, int receive, bool twice) throw ()
{
  if (!_initialized) return false;

  _t = _timeout;

  // if expected_bytes==6 we are waiting for ping; why does FTDI never send one back? FTD2xx does!
  if (receive == 6) receive = 0;

#ifdef FOUND_ftdi
  while (receive > 0 && _bytes == 0 && _t > 0) {
    ftdi_write_data(&_ftdic, &packet[0], send);
    if (twice)
      ftdi_write_data(&_ftdic, &packet[0], send);
    usleep(_latency);
    _bytes = ftdi_read_data(&_ftdic, &buffer[0], 100);
    _t--;
  }
#endif
  if (receive > 0 && (_bytes == 0 || _bytes % receive != 0)) {
    FTDERROR("communication failure: " << _bytes << " bytes received, should be a multiple of " << receive << ".");
    return 0;
  }
  return _bytes;
}
Esempio n. 17
0
QByteArray QLCFTDI::read(int size, uchar* userBuffer)
{
    uchar* buffer = NULL;

    if (userBuffer == NULL)
        buffer = (uchar*) malloc(sizeof(uchar) * size);
    else
        buffer = userBuffer;
    Q_ASSERT(buffer != NULL);

    QByteArray array;
    int read = ftdi_read_data(&m_handle, buffer, size);
    if (userBuffer != NULL)
    {
        for (int i = 0; i < read; i++)
            array.append((char) buffer[i]);
    }
    else
    {
        array = QByteArray::fromRawData((char*) buffer, read);
    }

    if (userBuffer == NULL)
        free(buffer);

    return array;
}
Esempio n. 18
0
File: lm60.c Progetto: jatocode/ftdi
int main(int argc, char *argv[])
{
    struct ftdi_context ftdic;
    int portrelay = MOTOR_RELAY;  
    unsigned char relay = 0;

    /* Initialize context for subsequent function calls */
    ftdi_init(&ftdic);

    /* Open FTDI device based on FT232R vendor & product IDs */
    if(ftdi_usb_open(&ftdic, 0x0403, 0x6001) < 0) {
        puts("Can't open device");
        fprintf(stderr, "unable to open ftdi device: (%s)\n", ftdi_get_error_string(&ftdic));
        return EXIT_FAILURE;
    }
    ftdi_set_bitmode(&ftdic, 0xFF, BITMODE_BITBANG);

    /* Reading existing state */
    relay = 0;     
    ftdi_read_data(&ftdic, &relay, 1);

//    printf("Activating relay %d\n", MOTOR_RELAY);
    relay |= (1 << portrelay);
    ftdi_write_data(&ftdic, &relay, 1);

//    printf("Sleeping for %dms\n", SLEEP_TIME);
    usleep(SLEEP_TIME * 1000); // usleep want microsecs 

//    printf("Dectivating relay %d\n", MOTOR_RELAY);
    relay &= ~(1 << portrelay);
    ftdi_write_data(&ftdic, &relay, 1);

    return 0;
}
static int i2c_add_recv_bytes(struct ftdi_context *ftdi, uint8_t *buf,
			     uint8_t *ptr, uint8_t *rbuf, int rcnt)
{
	int ret, i;
	uint8_t *b = ptr;

	for (i = 0; i < rcnt; i++) {
		/* set SCL low */
		*b++ = SET_BITS_LOW; *b++ = 0; *b++ = SCL_BIT;
		/* read the byte on the wire */
		*b++ = MPSSE_DO_READ; *b++ = 0; *b++ = 0;

		if (i == rcnt - 1) {
			/* NACK last byte */
			*b++ = SET_BITS_LOW; *b++ = 0; *b++ = SCL_BIT;
			*b++ = MPSSE_DO_WRITE | MPSSE_BITMODE | MPSSE_WRITE_NEG;
			*b++ = 0; *b++ = 0xff; *b++ = SEND_IMMEDIATE;
		} else {
			/* ACK all other bytes */
			*b++ = SET_BITS_LOW; *b++ = 0; *b++ = SCL_BIT | SDA_BIT;
			*b++ = MPSSE_DO_WRITE | MPSSE_BITMODE | MPSSE_WRITE_NEG;
			*b++ = 0; *b++ = 0; *b++ = SEND_IMMEDIATE;
		}
	}

	ret = ftdi_write_data(ftdi, buf, b - buf);
	if (ret < 0) {
		fprintf(stderr, "failed to prepare read\n");
		return ret;
	}
	ret = ftdi_read_data(ftdi, rbuf, rcnt);
	if (ret < 0)
		fprintf(stderr, "read byte failed\n");
	return ret;
}
Esempio n. 20
0
//Waits till the R-/B-line to go high
int FtdiNand::waitReady() {
	unsigned char cmd=GET_BITS_HIGH;
	unsigned char resp;
	int x;
	if (m_rbErrorCount==-1) return 1; //Too many timeouts -> don't check R/B-pin

	for (x=0; x<TIMEOUT_MSEC; x++) {
		//Send the command to read the IO-lines
		if (ftdi_write_data(&m_ftdi, &cmd, 1)<0) return error("writing cmd");
		//Read response
		if (ftdi_read_data(&m_ftdi, &resp, 1)<=0) return error("writing cmd");
		//Return if R/B-line is high (=ready)
		if (resp&2) return 1;
	#ifdef WIN32
		Sleep(1);
	#else
		usleep(1000);
	#endif
	}
	printf("Timeout on R/B-pin; chip seems busy for too long!\n");
	m_rbErrorCount++;
	if (m_rbErrorCount>TIMEOUT_RETRIES) {
		printf("WARNING: Too many R/B-pin timeouts. Going to ignore this pin for now.\n");
		printf("DOUBLE CHECK IF THE CHIP IS READ OR WRITTEN CORRECTLY!\n");
		m_rbErrorCount=-1;
	}
}
Esempio n. 21
0
QByteArray LibFTDIInterface::read(int size, uchar* userBuffer)
{
    uchar* buffer = NULL;

    if (userBuffer == NULL)
        buffer = (uchar*) malloc(sizeof(uchar) * size);
    else
        buffer = userBuffer;
    Q_ASSERT(buffer != NULL);

    QByteArray array;
    int read = ftdi_read_data(&m_handle, buffer, size);
    if (userBuffer != NULL)
    {
        qDebug() << "using user buffer";
        for (int i = 0; i < read; i++)
            array.append((char) buffer[i]);
    }
    else
    {
        array = QByteArray((char*)buffer, read);
    }

    if (userBuffer == NULL)
        free(buffer);

    return array;
}
Esempio n. 22
0
bool IntanThread::stopAcquisition()
{

    isTransmitting = false;

    std::cout << "Received signal to terminate thread." << std::endl;

    if (isThreadRunning())
    {
        signalThreadShouldExit();
    }

    std::cout << "Thread stopped successfully, stopping Intan Board." << std::endl;

    if (!simulateDataStream)
    {
        int return_value;

        if ((return_value = ftdi_write_data(&ftdic, &stopCode, 1)) > 0)
        {
            unsigned char buf[4097]; // has to be bigger than the on-chip buffer
            ftdi_read_data(&ftdic, buf, sizeof(buf));
            //closeUSB();
        }
        else
        {
            std::cout << "No device found." << std::endl;
            deviceFound = false;
        }
    }
   
    return true;
}
static int i2c_add_send_byte(struct ftdi_context *ftdi, uint8_t *buf,
			     uint8_t *ptr, uint8_t *tbuf, int tcnt)
{
	int ret, i, j;
	int tx_buffered = 0;
	static uint8_t ack[TX_BUFFER_LIMIT];
	uint8_t *b = ptr;
	uint8_t failed_ack = 0;

	for (i = 0; i < tcnt; i++) {
		/* WORKAROUND: force SDA before sending the next byte */
		*b++ = SET_BITS_LOW; *b++ = SDA_BIT; *b++ = SCL_BIT | SDA_BIT;
		/* write byte */
		*b++ = MPSSE_DO_WRITE | MPSSE_BITMODE | MPSSE_WRITE_NEG;
		*b++ = 0x07; *b++ = *tbuf++;
		/* prepare for ACK */
		*b++ = SET_BITS_LOW; *b++ = 0; *b++ = SCL_BIT;
		/* read ACK */
		*b++ = MPSSE_DO_READ | MPSSE_BITMODE | MPSSE_LSB;
		*b++ = 0;
		*b++ = SEND_IMMEDIATE;

		tx_buffered++;

		/*
		 * On the last byte, or every TX_BUFFER_LIMIT bytes, read the
		 * ACK bits.
		 */
		if (i == tcnt-1 || (tx_buffered == TX_BUFFER_LIMIT)) {
			/* write data */
			ret = ftdi_write_data(ftdi, buf, b - buf);
			if (ret < 0) {
				fprintf(stderr, "failed to write byte\n");
				return ret;
			}

			/* read ACK bits */
			ret = ftdi_read_data(ftdi, &ack[0], tx_buffered);
			for (j = 0; j < tx_buffered; j++) {
				if ((ack[j] & 0x80) != 0)
					failed_ack = ack[j];
			}

			/* check ACK bits */
			if (ret < 0 || failed_ack) {
				if (debug)
					fprintf(stderr,
						"write ACK fail: %d, 0x%02x\n",
						ret, failed_ack);
				return  -ENXIO;
			}

			/* reset for next set of transactions */
			b = ptr;
			tx_buffered = 0;
		}
	}
	return 0;
}
Esempio n. 24
0
static int ux400_gps_read_data(unsigned char * buf, int size)
{
	int ret = 0;

	ret = ftdi_read_data(&ux400_gps_ftdic, buf, size);

	return ret;
}
Esempio n. 25
0
int platform_buffer_read(uint8_t *data, int size)
{
	int index = 0;
	outbuf[bufptr++] = SEND_IMMEDIATE;
	platform_buffer_flush();
	while((index += ftdi_read_data(ftdic, data + index, size-index)) != size);
	return size;
}
Esempio n. 26
0
/**
 * attempts to read from ftdi object.
 * If a string is read, it calls FindIncrements().
 */
void IncrementParser::ReadFromRS232() {
	int f;
	unsigned char buf[1024];
	f = ftdi_read_data(ftdi_, buf, sizeof(buf));
		if(f<0)
			fprintf(stderr,"size < 0\n");
		else if(f>0)
			FindIncrements(buf, f);
}
Esempio n. 27
0
/*
 * Read utility functions
 */
uint8_t *read_data(void)
{
    static uint8_t last_read_data[10000];
    int i, j, expected_len = 0, extra_bytes = 0;

    if (trace)
        printf("[%s]\n", __FUNCTION__);
    if (buffer_current_size())
        *usbreadbuffer_ptr++ = SEND_IMMEDIATE; /* tell the FTDI that we are waiting... */
    flush_write(NULL);
    last_read_data_length = 0;
    for (i = 0; i < read_size_ptr; i++) {
        if (read_size[i] > 0)
            expected_len += read_size[i];
        else {
            if (i > 0 && read_size[i-1] < 0)
                extra_bytes++; /* we will squeeze out partial bytes in the processing below */
            else
                expected_len++;
        /* When there are 2 bit operations in a row, this is just accumulating
         * shifted bits into a register for return to user.  When exiting
         * Shift-DR/IR state, the last bit shifted is not performed with DATAWBITS,
         * but with a TMS operation.  For this reason, the combo of 2 bit ops
         * in a row is quite common.
         */
        }
    }
    if (expected_len + extra_bytes)
        ftdi_read_data(global_ftdi, last_read_data, expected_len + extra_bytes);
    last_read_data_length = expected_len;
    if (expected_len) {
        uint8_t *p = last_read_data;
        int validbits = 0;
        for (i = 0; i < read_size_ptr; i++) {
            if (read_size[i] < 0) {
                validbits -= read_size[i];
                if (validbits < 0 || validbits > 8) {
                    printf("[%s] validbits %d big\n", __FUNCTION__, validbits);
                    validbits = 8;
                    //exit(-1);
                }
                *p &= (0xff << (8-validbits));
                /* NOTE: when trying to combine back data bits that result from a TMS
                 * shift with > 1 bit, be aware that the number of bits shifted in reflects the
                 * _number of TMS bits shifted_, not the number of valid data bits (which is
                 * always only 1).
                 */
                if (i > 0 && read_size[i-1] < 0) {
                    *(p-1) = *p >> (8-validbits);    /* put result into LSBs */
                    /* Note: union datatypes work correctly, but int needs the data as MSBs! */
                    for (j = 0; j < expected_len; j++)  /* copies too much, but... */
                        *(p+j) = *(p+j+1);  /* move the data down in the buffer 1 byte */
                }
                else
                    p++;
            }
Esempio n. 28
0
//Read bytes from the nand, with the cl and al lines set as indicated.
int FtdiNand::nandRead(int cl, int al, char *buf, int count) {
	unsigned char *cmds=new unsigned char[count*2+2];
	unsigned char *ftdata=new unsigned char[count*2];
	int x, i, ret;
	i=0;
	//Construct read commands. First one sets the cl and al lines too, rest just reads.
	for (x=0; x<count; x++) {
		if (x==0) {
			cmds[i++]=READ_EXTENDED;
			cmds[i++]=(cl?ADR_CL:0)|(al?ADR_AL:0)|ADR_WP;
			cmds[i++]=0;
		} else {
			cmds[i++]=READ_SHORT;
			cmds[i++]=0;
		}
	}
	cmds[i++]=SEND_IMMEDIATE;

//	printf("Cmd:\n");
//	for (x=0; x<i; x++) printf("%02hhx %s", cmds[x], ((x&15)==15)?"\n":"");
//	printf("\n\n");


	if (ftdi_write_data(&m_ftdi, cmds, i)<0) return error("writing cmd");
	if (m_slowAccess) {
		//Div by 5 mode makes the ftdi-chip return all databytes double. Compensate for that.
		ret=ftdi_read_data(&m_ftdi, ftdata, count*2);
		for (x=0; x<count; x++) buf[x]=ftdata[x*2];
		ret/=2;
	} else {
		ret=ftdi_read_data(&m_ftdi, ftdata, count);
		for (x=0; x<count; x++) buf[x]=ftdata[x];
	}

	if (ret<0) return error("reading data");
	if (ret<count) return error("short read");
//	printf("%i bytes read.\n", ret);

	delete[] cmds;
	delete[] ftdata;
	return ret;
}
Esempio n. 29
0
void spi_csn(uint8_t value) {
        uint8_t v;

        if (value)
                pin_state |= PIN_CSN;
        else
                pin_state &= ~PIN_CSN;

        ftdi_write_data(ftdi, &pin_state, sizeof(pin_state));
        ftdi_read_data(ftdi, &v, sizeof(v));
}
Esempio n. 30
0
//Sets the USB timeout for reads then reads
int nifalcon_read(falcon_device* dev, unsigned char* str, unsigned int size, unsigned int timeout_ms)
{
	unsigned long bytes_rx, bytes_read = 0;
	clock_t timeout;

	if(!dev->is_initialized) nifalcon_error_return(NIFALCON_DEVICE_NOT_VALID_ERROR, "tried to read from an uninitialized device");
	if(!dev->is_open) nifalcon_error_return(NIFALCON_DEVICE_NOT_FOUND_ERROR, "tried to read from an unopened device");
	(dev->falcon).usb_read_timeout = timeout_ms;
	dev->falcon_status_code = ftdi_read_data(&(dev->falcon), str, size);
	return dev->falcon_status_code;
}