Exemplo n.º 1
0
int main()
{
    unsigned char d = 0;
    unsigned char i;
    struct ftdi_context ftdic;

    /* 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);

    ftdi_write_data(&ftdic, &d, 1);
    sleep(1);
    for(i=0;i<8;i++) {
        printf("%d\n",d); 
        d |= d<<1;
    	ftdi_write_data(&ftdic, &d, 1);
	usleep(200 * 1000);
    }
    sleep(2);
    d = 0;
    ftdi_write_data(&ftdic, &d, 1);
   
    return 0;
}
Exemplo n.º 2
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;
}
Exemplo n.º 3
0
Arquivo: lm60.c Projeto: 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;
}
Exemplo n.º 4
0
void set_led_state (unsigned char state)
{
    if (!ftdi_ok)
        return;
    if (ftdi_write_data (&ctx, &state, 1) < 0) {
        init_hw ();
        if (ftdi_ok)
            ftdi_write_data (&ctx, &state, 1);
    }
}
Exemplo n.º 5
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;
}
Exemplo n.º 6
0
/**
 * Write data of a certain length to 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 containing the data to write. Must not be NULL.
 * @param size The number of bytes to write. Must be >= 0.
 * @return The number of bytes written, or a negative value upon errors.
 */
SR_PRIV int la8_write(struct dev_context *devc, uint8_t *buf, int size)
{
	int bytes_written;

	/* 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_written = ftdi_write_data(devc->ftdic, buf, size);

	if (bytes_written < 0) {
		sr_err("%s: ftdi_write_data: (%d) %s.", __func__,
		       bytes_written, ftdi_get_error_string(devc->ftdic));
		(void) la8_close_usb_reset_sequencer(devc); /* Ignore errors. */
	} else if (bytes_written != size) {
		sr_err("%s: bytes to write: %d, bytes written: %d.",
		       __func__, size, bytes_written);
		(void) la8_close_usb_reset_sequencer(devc); /* Ignore errors. */
	}

	return bytes_written;
}
Exemplo n.º 7
0
	bool FalconCommLibFTDI::write(uint8_t* str, uint32_t size)
	{
		LOG_DEBUG("Writing " << size << " bytes of data");
		if(!m_isCommOpen)
		{
			LOG_ERROR("Device not open");
			m_errorCode = FALCON_COMM_DEVICE_NOT_VALID_ERROR;
			return false;
		}

		m_lastBytesWritten = ftdi_write_data((m_falconDevice), str, size);
		if(m_lastBytesWritten < 0)
		{
			LOG_ERROR("Device error " << m_deviceErrorCode);
			m_deviceErrorCode = m_lastBytesRead;
			m_errorCode = FALCON_COMM_DEVICE_ERROR;
			return false;
		}
		if(m_lastBytesWritten < size)
		{
			LOG_ERROR("Write amount " << m_lastBytesWritten << " less than requested size " << size);
			m_errorCode = FALCON_COMM_WRITE_ERROR;
			return false;
		}
		return true;
	}
Exemplo n.º 8
0
static int ft245r_send(PROGRAMMER * pgm, unsigned char * buf, size_t len) {
    int rv;

    rv = ftdi_write_data(handle, buf, len);
    if (len != rv) return -1;
    return 0;
}
Exemplo n.º 9
0
int Write_JTAG(struct ftdi_context * handle, unsigned char value, unsigned char direction, unsigned int len)
{
        unsigned char cmd [3] = { 0x00 };
        int ret = 0;
        int i = 0;

        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;
        }

        for(i = 0; i < len; i ++)
        {
                cmd[0] = 0x82;
                cmd[1] = value;
                cmd[2] = direction;

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

        return ret;
}
Exemplo n.º 10
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;
}
Exemplo n.º 11
0
int Communication::syncFlush()
{
	if(syncPacketSize == 0)
	{
		printf("Nothing to flush!\n");
		return EXIT_FAILURE;
	}
	else
	{
		#ifdef DEBUG
		for (int i = 0; i < syncPacketSize+1; ++i)
		{
		 	printf("%x\t", syncPacket[i]);
		}
		printf("\n\n\n\n");
		#endif
		
		syncPacket[3] = syncPacketSize - 3;
		syncPacket[syncPacketSize] = checksum(syncPacket);
		syncPacketSize = 0;	//Resetting size to 0 regardless of whether flush was successful or not
		if(ftdi_write_data(&bodyFTDI, syncPacket, syncPacket[3]+4)>0)
			return EXIT_SUCCESS;
		else 
			return EXIT_FAILURE;
	}
}
Exemplo n.º 12
0
int iic_set_bits (struct ftiic_data* iic_data, unsigned char scl_val,
					       unsigned char sda_val,
					       unsigned char scl_dir,
					       unsigned char sda_dir,
					       unsigned char sync) {
	struct ftdi_context *fc = iic_data->ftdic;
	unsigned char data[4] = {SET_BITS_LOW, 0x0, 0x0, SEND_IMMEDIATE};
	int ret;

	data[2] = (scl_dir ? 0x1 : 0x0) | (sda_dir ? 0x2 : 0x0);
	data[1] = (scl_val ? 0x1 : 0x0) | (sda_val ? 0x2 : 0x0);
	ret = ftdi_write_data(fc, data, sync ? 4 : 3);
	if (ret < 0) {
		fprintf(stderr, "write error: %s\n", 
						ftdi_get_error_string(fc));
		return -1;
	} else if (ret < (sync ? 4 : 3)) {
		fprintf(stderr, "write error: wrote %d bytes, expected %d\n", 
						ret, sync ? 4 : 3);
		return -1;
	}
	iic_data->sda = sda_val;
	iic_data->scl = scl_val;
	/* if sync, make sure you delay one cycle after */
	if (sync && iic_data->freq < 1000000)
		usleep(1000000/iic_data->freq);

	//printf("sda == %d, scl == %d ? ", sda_dir, scl_dir);
	dump_pins(iic_data, 0);
	return 0;
}
Exemplo n.º 13
0
//Write bytes to the nand, with al/cl set as indicated.
int FtdiNand::nandWrite(int cl, int al, char *buf, int count) {
	unsigned char *cmds=new unsigned char[count*3+1];
	int x, i;
	i=0;

	//Construct write commands. First one sets the cl and al lines too, rest just reads.
	for (x=0; x<count; x++) {
		if (x==0) {
			cmds[i++]=WRITE_EXTENDED;
			cmds[i++] = (cl ? ADR_CL : 0) | (al ? ADR_AL : 0) | ADR_WP;
			cmds[i++]=0;
		} else {
			cmds[i++]=WRITE_SHORT;
			cmds[i++]=0;
		}
		cmds[i++]=buf[x];
	}

//	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");
	delete[] cmds;
	return count;
}
Exemplo n.º 14
0
static int scanaplus_write(struct dev_context *devc, uint8_t *buf, int size)
{
	int i, bytes_written;
	GString *s;

	/* Note: Caller checks devc, devc->ftdic, buf, size. */

	s = g_string_sized_new(100);
	g_string_printf(s, "Writing %d bytes: ", size);
	for (i = 0; i < size; i++)
		g_string_append_printf(s, "0x%02x ", buf[i]);
	sr_spew("%s", s->str);
	g_string_free(s, TRUE);

	bytes_written = ftdi_write_data(devc->ftdic, buf, size);
	if (bytes_written < 0) {
		sr_err("Failed to write FTDI data (%d): %s.",
		       bytes_written, ftdi_get_error_string(devc->ftdic));
	} else if (bytes_written != size) {
		sr_err("FTDI write error, only %d/%d bytes written: %s.",
		       bytes_written, size, ftdi_get_error_string(devc->ftdic));
	}

	return bytes_written;
}
Exemplo n.º 15
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;
	}
}
Exemplo n.º 16
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);

}
Exemplo n.º 17
0
static int presto_write(uint8_t *buf, uint32_t size)
{
#if BUILD_PRESTO_FTD2XX == 1
	DWORD ftbytes;
	presto->status = FT_Write(presto->handle, buf, size, &ftbytes);
	if (presto->status != FT_OK) {
		LOG_ERROR("FT_Write returned: %s", ftd2xx_status_string(presto->status));
		return ERROR_JTAG_DEVICE_ERROR;
	}

#elif BUILD_PRESTO_LIBFTDI == 1
	uint32_t ftbytes;
	presto->retval = ftdi_write_data(&presto->ftdic, buf, size);
	if (presto->retval < 0) {
		LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&presto->ftdic));
		return ERROR_JTAG_DEVICE_ERROR;
	}
	ftbytes = presto->retval;
#endif

	if (ftbytes != size) {
		LOG_ERROR("couldn't write the requested number of bytes to PRESTO (%u < %u)",
			(unsigned)ftbytes, (unsigned)size);
		return ERROR_JTAG_DEVICE_ERROR;
	}

	return ERROR_OK;
}
Exemplo n.º 18
0
int FtdiWrapper::put(const uint8_t *buffer, unsigned int bufSize, unsigned int minPut)
{
    unsigned int bytesWritten = 0;
//    unsigned int trys = 0;
    int status;

    while(bytesWritten < minPut)
    {
        //TODO: casting away const is BAD
        //usb write
        status = ftdi_write_data(ftdiContext, const_cast<uint8_t*>(buffer), bufSize);

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

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

        //TODO: progressively sleep?

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

    }

    return bytesWritten;
}
Exemplo n.º 19
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;
}
Exemplo n.º 20
0
int Write_bus(struct ftdi_context * handle, unsigned char haddr, unsigned char laddr, unsigned char * buff, unsigned int len)
{
	unsigned char cmd [4] = { 0x00 };
	int ret = 0;
	int i = 0;

	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;
	}

	for(i = 0; i < len; i ++)
	{
		cmd[0] = 0x93;
		cmd[1] = haddr;
		cmd[2] = laddr;
		cmd[3] = buff[i];

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

	return ret;
}
Exemplo n.º 21
0
void
setPins(int pins) {
	if (pins < 0 || pins > 15) {
		printf("Set value must be 0-15 (0b0000-0b1111).\n%i is an invalid entry\n\n", pins);
		return;
	}
	/*
	1) The type must be a char, so cast it
	2) 0xF0 is the base.
	3) On=0, Off=1, so subtract that from 0xF (15) to get the bits to write
	*/
	bits[0] = (char) (0xF0 + (0xF - pins));
	//Written twice because of the buffer
	ret = ftdi_write_data(&ftdic, bits, 1);
	ret = ftdi_write_data(&ftdic, bits, 1);
}
Exemplo n.º 22
0
int nifalcon_write(falcon_device* dev, unsigned char* str, unsigned int size)
{
	if(!dev->is_initialized) nifalcon_error_return(NIFALCON_DEVICE_NOT_VALID_ERROR, "tried to write to an uninitialized device");
	if(!dev->is_open) nifalcon_error_return(NIFALCON_DEVICE_NOT_FOUND_ERROR, "tried to write to an unopened device");
	dev->falcon_status_code = ftdi_write_data(&(dev->falcon), str, size);
	return dev->falcon_status_code;
}
Exemplo n.º 23
0
/*
 * use libftdi and libusb to send command
 * no kernel driver needed
 */
int usbWriteFtdi(char *cmdstr)
{
    struct ftdi_context ctx;
    int device=0x0c30, vendor=0x1781;

    if (ftdi_init( &ctx )) {
        fprintf(stderr,  "usb - init error !\n");
        return 1;
    }

    if (ftdi_usb_open(&ctx, vendor, device)) {
        fprintf(stderr,  "usb - open error (cannot find?) !\n");
        ftdi_deinit( &ctx );
        return 2;
    }

    if (ftdi_usb_reset( &ctx )) {
        fprintf(stderr,  "usb - reset error !\n");
        ftdi_usb_close( &ctx );
        ftdi_deinit( &ctx );
        return 3;
    }

    ftdi_disable_bitbang( &ctx );
    ftdi_set_baudrate(&ctx, BAUD);

    ftdi_write_data( &ctx, cmdstr, strlen(cmdstr) );
    sleep(1); /* just for sure */
    ftdi_usb_close( &ctx );
    ftdi_deinit( &ctx );

    return 0;
}
Exemplo n.º 24
0
void writeFTDIdata (char data, struct ftdi_context ftdic)
{
	char x;
	x = data; 
	ftdi_write_data(&ftdic, &x, 1);

}
Exemplo n.º 25
0
static int spi_buf_w(const 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);

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

		// most significant bit first
		for (bit = (1 << 7); bit > 0; bit >>= 1) {
			if (b[pos] & bit) {
				pin_state |= PIN_FMOSI;
				buf[j++] = pin_state;
			} else {
				pin_state &= ~PIN_FMOSI;
				buf[j++] = pin_state;
			}
			pin_state |= PIN_FSCK;
			buf[j++] = pin_state;

			pin_state &= ~PIN_FSCK;
			buf[j++] = pin_state;
		}
	}
	j = ftdi_write_data(ftdi, buf, j);
	free(buf);

	return j / 8 / BYTES_PER_BIT;
}
Exemplo n.º 26
0
static int config_i2c(struct ftdi_context *ftdi)
{
	int ret;
	uint8_t buf[5];
	uint16_t divisor;

	ret = ftdi_set_latency_timer(ftdi, 16 /* ms */);
	if (ret < 0)
		fprintf(stderr, "Cannot set latency\n");

	ret = ftdi_set_bitmode(ftdi, 0, BITMODE_RESET);
	if (ret < 0) {
		fprintf(stderr, "Cannot reset MPSSE\n");
		return -EIO;
	}
	ret = ftdi_set_bitmode(ftdi, 0, BITMODE_MPSSE);
	if (ret < 0) {
		fprintf(stderr, "Cannot enable MPSSE\n");
		return -EIO;
	}

	ret = ftdi_usb_purge_buffers(ftdi);
	if (ret < 0)
		fprintf(stderr, "Cannot purge buffers\n");

	/* configure the clock */
	divisor = (60000000 / (2 * I2C_FREQ * 3 / 2 /* 3-phase CLK */) - 1);
	buf[0] = EN_3_PHASE;
	buf[1] = DIS_DIV_5;
	buf[2] = TCK_DIVISOR;
	buf[3] = divisor & 0xff;
	buf[4] = divisor >> 8;
	ret = ftdi_write_data(ftdi, buf, sizeof(buf));
	return ret;
}
Exemplo n.º 27
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;
}
Exemplo n.º 28
0
static dc_status_t serial_ftdi_write (void *io, const void *data, size_t size, size_t *actual)
{
	ftdi_serial_t *device = io;

	if (device == NULL)
		return DC_STATUS_INVALIDARGS;

	unsigned int nbytes = 0;
	while (nbytes < size) {

		int n = ftdi_write_data (device->ftdi_ctx, (unsigned char *) data + nbytes, size - nbytes);
		if (n < 0) {
			if (n == LIBUSB_ERROR_INTERRUPTED)
				continue; // Retry.
			ERROR (device->context, "%s", ftdi_get_error_string(device->ftdi_ctx));
			return DC_STATUS_IO; // Error during write call.
		} else if (n == 0) {
			break; // EOF.
		}

		nbytes += n;
	}

	INFO (device->context, "Wrote %d bytes", nbytes);

	if (actual)
		*actual = nbytes;

	return DC_STATUS_SUCCESS;
}
Exemplo n.º 29
0
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;
}
Exemplo n.º 30
0
/*
 * Attempts to write the given buffer of given length to the device.
 *
 * Returns: the total number of bytes written, DEVICE_NOT_OPEN if the ftdi device
 * is not open, or another value < 0 representing an error code from ftdi_write_data.
 */
int FtdiDevice::writeData( const unsigned char* data, int length ) const
{
	//fprintf( stderr, "writeData: about to write %i bytes.\n", length ); //LOG
	
	if ( ! isOpen() ) return RV_DEVICE_NOT_OPEN;
	
	return ftdi_write_data( context_, const_cast<unsigned char*>( data ), length );
}