Esempio n. 1
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. 2
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;
}
Esempio n. 3
0
File: vcc.c Progetto: koheik/vcctool
void begin(struct ftdi_context *ftdi)
{
	ftdi_usb_reset(ftdi);
	ftdi_usb_purge_rx_buffer(ftdi);
	ftdi_usb_purge_tx_buffer(ftdi);

	ftdi_setrts(ftdi, 1);
}
Esempio n. 4
0
int reader_read_packet(struct reader *reader,
		       char *buf,
		       int maxlen)
{
  int rc;
  int cnt;
  if(maxlen > 0)
    buf[0] = '\0'; // in case of fail nul buffer

  for(cnt = 0; cnt < maxlen-1; cnt++){

    int timeout_cnt = 10;
    do {
      // why 1?
      rc = ftdi_read_data(reader->ftdic, (unsigned char *)buf, 1);
      if(rc < 0){
	// Error, return error code
	return -1;
      }else if( rc > 0){
	// Process it
	break;
      }
      usleep(1000);
      timeout_cnt--;
    } while(timeout_cnt);

    if(rc == 0) {
      ftdi_usb_purge_rx_buffer(reader->ftdic);
      return 0;
    }

    // EOP char reached, return
    if(*buf == RFID1_EOP_CHAR){
      *buf = '\0';
      return cnt;
    }

    buf++;
  }

  // Reached maxlen, purge buffers and return
  buf[maxlen-1] = '\0';
  ftdi_usb_purge_rx_buffer(reader->ftdic);
  return maxlen;
}
Esempio n. 5
0
int Context::flush(int mask)
{
    int ret = 1;

    if (mask & Input)
        ret &= ftdi_usb_purge_rx_buffer(d->ftdi);
    if (mask & Output)
        ret &= ftdi_usb_purge_tx_buffer(d->ftdi);

    return ret;
}
Esempio n. 6
0
int FtdiDevice::purgeBuffers( int bufType ) const
{
	if ( ! isOpen() ) return RV_DEVICE_NOT_OPEN;
	
	int rv;
	switch ( bufType ) {
		case RX_BUFFER: rv = ftdi_usb_purge_rx_buffer( context_ ); break;
		case TX_BUFFER: rv = ftdi_usb_purge_tx_buffer( context_ ); break;
		case RX_TX_BUFFER: rv = ftdi_usb_purge_buffers( context_ ); break;
		default: assert( false ); rv = 0; break; //illegal argument given
	}
	
	//if ( rv < 0 ) fprintf( stderr, "purgeBuffers: purging failed (%i)\n", rv ); //LOG
	
	return rv;
}
Esempio n. 7
0
static dc_status_t serial_ftdi_purge (void *io, dc_direction_t queue)
{
	ftdi_serial_t *device = io;

	if (device == NULL)
		return DC_STATUS_INVALIDARGS;

	size_t input;
	serial_ftdi_get_received (io, &input);
	INFO (device->context, "Flush: queue=%u, input=%lu, output=%i", queue, input,
	      serial_ftdi_get_transmitted (device));

	switch (queue) {
	case DC_DIRECTION_INPUT:  /**< Input direction */
		if (ftdi_usb_purge_tx_buffer(device->ftdi_ctx)) {
			ERROR (device->context, "%s", ftdi_get_error_string(device->ftdi_ctx));
			return DC_STATUS_IO;
		}
		break;
	case DC_DIRECTION_OUTPUT: /**< Output direction */
		if (ftdi_usb_purge_rx_buffer(device->ftdi_ctx)) {
			ERROR (device->context, "%s", ftdi_get_error_string(device->ftdi_ctx));
			return DC_STATUS_IO;
		}
		break;
	case DC_DIRECTION_ALL: /**< All directions */
	default:
		if (ftdi_usb_reset(device->ftdi_ctx)) {
			ERROR (device->context, "%s", ftdi_get_error_string(device->ftdi_ctx));
			return DC_STATUS_IO;
		}
		break;
	}

	return DC_STATUS_SUCCESS;
}
Esempio n. 8
0
/* prepare the FTDI USB device */
int do_init(struct ftdi2s88_t *fs88) {
    fs88->ftdic = ftdi_new();

    if (!fs88->ftdic) {
	fprintf(stderr, "ftdi_new failed\n");
	return -1;
    }

    if (ftdi_usb_open_desc(fs88->ftdic, 0x0403, 0x6001, NULL, NULL) < 0) {
    /* if (ftdi_usb_open_desc(ftdic, 0x0403, 0x6015, NULL, NULL) < 0) { */
	fprintf(stderr, "ftdi_usb_open_desc failed: %s\n", ftdi_get_error_string(fs88->ftdic));
	return -1;
    }

    if (ftdi_set_bitmode(fs88->ftdic, BITBANG_MASK, BITMODE_SYNCBB) < 0) {
	fprintf(stderr, "ftdi_set_bitmode failed: %s\n", ftdi_get_error_string(fs88->ftdic));
	return -1;
    }

    if (ftdi_set_baudrate(fs88->ftdic, fs88->baudrate) < 0) {
	fprintf(stderr, "ftdi_set_baudrate failed: %s\n", ftdi_get_error_string(fs88->ftdic));
	return -1;
    }

    if (ftdi_usb_purge_tx_buffer(fs88->ftdic)) {
	fprintf(stderr, "tx buffer flushing failed: %s\n", ftdi_get_error_string(fs88->ftdic));
	return -1;
    }

    if (ftdi_usb_purge_rx_buffer(fs88->ftdic)) {
	fprintf(stderr, "rx buffer flushing failed: %s\n", ftdi_get_error_string(fs88->ftdic));
	return -1;
    }

    return 0;
}
Esempio n. 9
0
int main(int argc, char *argv[]) {

	/** COMMS ************************************************************/ 

	struct ftdi_context *ftdic = bub_init(57600, 1, 0, BUF_SIZE);

	if(ftdic == NULL)
		abort(); // FIXME
	
	printf("Ready.\n");
	
	uint8_t buf[BUF_SIZE];
	memset(&buf, 0, BUF_SIZE);

	long rxb = -1;

	/** GFX **************************************************************/ 

	gam_screen screen = {
		.w       = 512,
		.h       = 512,
		.depth   = 32,
		.flags   = SDL_HWACCEL | SDL_DOUBLEBUF,
		.info    = 0,
		.surface = NULL
	};

	if(gam_init(GAM_ALL, 1) != EXIT_SUCCESS)
		abort();

	if(gam_video_init(&screen) != 0)
		return(EXIT_FAILURE);
	
	SDL_Surface *backdrop = 
			gam_get_surface_from_png_file("backdrop.png", SDL_HWSURFACE);
	if(backdrop == NULL){
		fprintf(stderr, "Can't allocate surface: %s\n", SDL_GetError());
		return(EXIT_FAILURE);
	}

	
	SDL_Surface *spot = gam_get_surface_from_png_file("spot.png", SDL_HWSURFACE);
	//SDL_Surface *spot = gam_get_surface_sphere(screen.surface, 100);
	if(spot == NULL){
		fprintf(stderr, "Can't allocate surface: %s\n", SDL_GetError());
		return(EXIT_FAILURE);
	}

	SDL_SetColorKey(spot, 
			SDL_SRCCOLORKEY,
			SDL_MapRGB(spot->format, 255, 255, 255));

	if(spot == NULL){
		fprintf(stderr, "Can't allocate surface: %s\n", SDL_GetError());
		return(EXIT_FAILURE);
	}


	TTF_Font *ttf = gam_init_sdlttf("StayPuft.ttf", 22);
	if(ttf == NULL)
		abort();
	Uint32 text_color = SDL_MapRGB(screen.surface->format, 0, 0, 0);
	
	gam_state->print_mode = GAM_PRINT_BLEND;
	gam_state->alpha = SDL_MapRGB(screen.surface->format, 140, 140, 160);
	/*********************************************************************/ 

	typedef struct { 
		int x, y, z;
	} point_t;
	
	point_t point[TAIL_COUNT];

	int i;
	for(i = 0; i < TAIL_COUNT; i++) {
		point[i].x = (.5 * screen.w - .5 * spot->w);
		point[i].y = (.5 * screen.h - .5 * spot->h);
		point[i].z = 0;
	}

	memset(&buf, 0, BUF_SIZE);
	ftdi_usb_purge_rx_buffer(ftdic);

	SDL_Event event;
	SDL_Rect rect;
	int busy = 1;
	while(busy) {

		while(SDL_PollEvent(&event))
			if(event.type == SDL_KEYDOWN)
				if(event.key.keysym.sym == SDLK_ESCAPE)
					busy = 0;
	
		SDL_BlitSurface(backdrop, NULL, screen.surface, NULL);

		rxb = ftdi_read_data(ftdic, buf, BUF_SIZE);

		if(rxb > 0) {
			if(rxb == BUF_SIZE) {

				if(buf[BUF_SIZE - 1] == 10 && 
				   buf[BUF_SIZE - 2] == 13) {

						int16_t x, y, z;

						x = (buf[1] << 8) ^ buf[0];
						y = (buf[3] << 8) ^ buf[2];
						z = (buf[5] << 8) ^ buf[4];


						printf("\e[32mGREAT SUCCESS\e[0m %4d %4d %4d\n", x, y, z);

						//gam_print_core(screen.surface, 0, 0, text_color, "X: %d", x);
						gam_print(screen.surface, 0, 0, ttf, text_color, "X: %1.2f", x / 256.0);
						gam_print(screen.surface, 0, 23, ttf, text_color, "Y: %1.2f", y / 256.0);
						gam_print(screen.surface, 0, 46, ttf, text_color, "Z: %1.2f", z / 256.0);

						//printf("%1.3f %1.3f %1.3f\n", 
						//       x / 256.0, 
						//       y / 256.0, 
						//       z / 256.0);

						point[0].x = (.5 * screen.w - .5 * spot->w) + x;
						point[0].y = (.5 * screen.h - .5 * spot->h) + y;

						for(i = TAIL_COUNT - 1; i > 0; i--) {
							point[i].x = point[i - 1].x;
							point[i].y = point[i - 1].y;
							point[i].z = point[i - 1].z;
						}

						for(i = TAIL_COUNT - 1; i >= 0; i--) {
							rect.x = point[i].x;
							rect.y = point[i].y;
							SDL_SetAlpha(spot, 
								     SDL_SRCALPHA, 
								     (255 / TAIL_COUNT) * (TAIL_COUNT - i));
							SDL_BlitSurface(spot, NULL, screen.surface, &rect);
						}

						SDL_Flip(screen.surface);

				} else {
					fprintf(stderr, "Packet of right length but wrong signature\n");
				}
			} else if(rxb < BUF_SIZE) { // Underrun? 
				fprintf(stderr, "Packet of unknown size %ld\n", rxb);
			} else {
				abort();
			}
		} else if(rxb < 0){