Exemplo n.º 1
0
/* reinit libhid to get update the list of attached devices */
static void usbhid_refresh(t_usbhid *x)
{
	x->x_hid_return = hid_cleanup();
	if (x->x_hid_return != HID_RET_SUCCESS) 
		error("[usbhid] hid_cleanup failed with return code %d\n", 
			  x->x_hid_return);
	if( init_libhid(x) != HID_RET_SUCCESS ) return;
}
Exemplo n.º 2
0
int main(void){
  HIDInterface* hid;
  hid_return ret;
  HIDInterfaceMatcher matcher = { VENDOR, PRODUCT, NULL, NULL, 0 };
  char packet[PACKET_LEN];

  hid_set_debug(HID_DEBUG_NONE);
  hid_set_debug_stream(stderr);
  hid_set_usb_debug(0);
  
  hid_init();
  hid = hid_new_HIDInterface();
  ret = hid_force_open(hid, 0, &matcher, 3);

  if (ret == HID_RET_FAIL_DETACH_DRIVER) {
      printf("Failed to detach Driver for Device %04x:%04x; Permission?\n", VENDOR, PRODUCT);
      return 1;
  }

  if (ret != HID_RET_SUCCESS) {
      printf("Could not open HID device %04x:%04x (ret was %d)\n", VENDOR, PRODUCT, ret);
      return 1;
  }

  // printf("hid_force_open ret=%d\n", ret);

  // hid_set_idle(hid,0,0);

  // Discard till first '\n'
  do {
    memset(packet,0,sizeof(packet));
    ret = hid_interrupt_read(hid,0x81,packet,PACKET_LEN,1000);
  } while (getTheChar(packet[1]) != '\n');

  while (1) {
    memset(packet,0,sizeof(packet));

    ret = hid_interrupt_read(hid,0x81,packet,PACKET_LEN,1000);

    /*
    if (ret == HID_RET_FAIL_INT_READ) {
        printf("Fail hid_interrupt_read\n");
    } else {
        printf("hid_interrupt_read ret=%d\n", ret);
    }
    */

    if( ret == HID_RET_SUCCESS ) {
      //printPacket(packet,PACKET_LEN);
      showTemperature(packet);
    }
  }

  hid_close(hid);
  hid_delete_HIDInterface(&hid);
  hid_cleanup();
  return 0;
}
Exemplo n.º 3
0
int main(int argc, unsigned char**argv)
{
	int y=device_init();
	char AttValue1[3],AttValue2[3];
	float LastAtt=0.0;
	char PNreceive[SEND_PACKET_LEN];
	char SNreceive[SEND_PACKET_LEN];
	char RFpower[SEND_PACKET_LEN];
	int StrLen1;
	float Att1=0.0;
	float Att2=0.0;
	Get_PN(PNreceive,hid1);
	fprintf(stderr," PN1=  %s .\n",PNreceive);
	/* Get_PN(PNreceive,hid2); */
	/* fprintf(stderr," PN2=  %s .\n",PNreceive); */
	Get_SN(SNreceive,hid1);
	fprintf(stderr," SN1=  %s .\n",SNreceive);
	/* Get_SN(SNreceive,hid2); */
	/* fprintf(stderr," SN2=  %s .\n",SNreceive); */
	Att1=(float)(atof(argv[1])); 
	Set_Attenuation(Att1,hid1); // set attenuation 
	ReadAtt(AttValue1,hid1);
	LastAtt=(int)(AttValue1[0])+(float)(AttValue1[1])/4;
	fprintf(stderr," Attenuation1=  %f \n",LastAtt);
	/* Att2=(float)(atof(argv[2])); 
	Set_Attenuation(Att2,hid2);  // set attenuation 
	ReadAtt(AttValue2,hid2);
	LastAtt=(int)(AttValue2[0])+(float)(AttValue2[1])/4;
	fprintf(stderr," Attenuation2=  %f \n",LastAtt); 
	*/
	ret = hid_close(hid1);
	/* ret = hid_close(hid2); */
	if (ret != HID_RET_SUCCESS) {
		fprintf(stderr, "hid_close failed with return code %d\n", ret);
		return 1;
}
	hid_delete_HIDInterface(&hid1);
	hid_delete_HIDInterface(&hid2);
	ret = hid_cleanup();
	if (ret != HID_RET_SUCCESS) {
		fprintf(stderr, "hid_cleanup failed with return code %d\n", ret);
		return 1;
}
	return 0;
}
Exemplo n.º 4
0
int main(void)
{
  int i;
  hid_return ret;
  HIDInterface* hid;
  HIDInterfaceMatcher matcher;

  /* hid_write_library_config(stdout); */
  
  /* hid_set_debug(HID_DEBUG_NOTRACES); */
  // hid_set_debug(HID_DEBUG_NONE);
  hid_set_debug(HID_DEBUG_ALL);
  hid_set_debug_stream(stderr);
  hid_set_usb_debug(0);

  /* data init */
  for (i = 0 ; i < 32 ; i++)
	hid_id[i] = NULL;

  ret = hid_init();

  hid = hid_new_HIDInterface();
  matcher.vendor_id = HID_ID_MATCH_ANY;
  matcher.product_id = HID_ID_MATCH_ANY;
  matcher.matcher_fn = device_iterator;

  /* open recursively all HID devices found */
  while ( (ret = hid_force_open(hid, 0, 0, &matcher, 2)) != HID_RET_DEVICE_NOT_FOUND)
	{
	  printf("************************************************************************\n");
	  
	  hid_write_identification(stdout, hid);
	  
	  /* Only dump HID tree if asked */
	  /* hid_dump_tree(stdout, hid); */
	  
	  hid_close(hid);
	}
	 
  hid_delete_HIDInterface(&hid);
  ret = hid_cleanup();
  
  return 0;
}
Exemplo n.º 5
0
Arquivo: core.c Projeto: axlrose/cells
int close_usb_device()
{
	hid_return ret; 
	/*step 1*/
	ret = hid_close(hid);
	if (ret != HID_RET_SUCCESS) {
		fprintf(stderr, "hid_close failed with return code %d\n", ret);
		return -1;
	}

	/*step 2*/
	hid_delete_HIDInterface(&hid);

	/*step 3*/
	ret = hid_cleanup();
	if (ret != HID_RET_SUCCESS) {
		fprintf(stderr, "hid_cleanup failed with return code %d\n", ret);
		return -1;
	}
	return 0;
}
Exemplo n.º 6
0
/* -------------------------------------------------------------------------- */
static void usbhid_free(t_usbhid* x) 
{
	if(x->debug_level) post("usbhid_free");
		
	usbhid_close(x);

	freebytes(x->x_read_elements,sizeof(t_int) * x->x_read_element_count);
	freebytes(x->x_write_paths,sizeof(t_int) * x->x_write_path_count);

	if(x->debug_level) 
		post("[usbhid] freeing instance %d",usbhid_instance_count);
	hid_delete_HIDInterface(&(x->x_hidinterface));
	if(usbhid_instance_count <= 1)
	{
		post("[usbhid] freeing last instance");
		x->x_hid_return = hid_cleanup();
		if (x->x_hid_return != HID_RET_SUCCESS) 
			error("[usbhid] hid_cleanup failed with return code %d\n", 
				  x->x_hid_return);
	}

	usbhid_instance_count--;
}
Exemplo n.º 7
0
int main(int argc, char *argv[])
{
  HIDInterface* hid;
  int iface_num = 0;
  hid_return ret;

  unsigned short vendor_id  = 0x1325;
  unsigned short product_id = 0xc029;
  char *vendor, *product;
  
  // Match USB by VID and PID
  HIDInterfaceMatcher matcher = { vendor_id, product_id, NULL, NULL, 0 };

  // Enable debugging
//  hid_set_debug(HID_DEBUG_ALL);
//  hid_set_debug_stream(stderr);
//  hid_set_usb_debug(0);
  

  // Initialize HID
  ret = hid_init();
  if (ret != HID_RET_SUCCESS) {
    fprintf(stderr, "hid_init failed with return code %d\n", ret);
    return 1;
  } else {
    fprintf(stderr, "*** hid_init success! ***\n");
  }

  hid = hid_new_HIDInterface();
  if (hid == 0) {
    fprintf(stderr, "hid_new_HIDInterface() failed, out of memory?\n");
    return 1;
  } else {
    fprintf(stderr, "*** hid_new_HIDInterface success! ***\n");
  }
 
  //TODO: Use hotplug or give permission to USB without sudo
  /* How to detach a device from the kernel HID driver:
   *
   * The hid.o or usbhid.ko kernel modules claim a HID device on insertion,
   * usually. To be able to use it with libhid, you need to blacklist the
   * device (which requires a kernel recompilation), or simply tell libhid to
   * detach it for you. hid_open just opens the device, hid_force_open will
   * try n times to detach the device before failing.
   *
   * To open the HID, you need permission to the file in the /proc usbfs
   * (which must be mounted -- most distros do that by default):
   *   mount -t usbfs none /proc/bus/usb
   * You can use hotplug to automatically give permissions to the device on
   * connection. Please see
   *   http://cvs.ailab.ch/cgi-bin/viewcvs.cgi/external/libphidgets/hotplug/
   * for an example. Try NOT to work as root!
   */
 

  // Detach kernel 
  ret = hid_force_open(hid, iface_num, &matcher, 25);
  if (ret != HID_RET_SUCCESS) {
    fprintf(stderr, "hid_force_open failed with return code %d\n", ret);
    return 1;
  } else {
    fprintf(stderr, "*** hid_forcE_open success! ***\n");
  }
 
  ret = hid_write_identification(stdout, hid);
  if (ret != HID_RET_SUCCESS) {
    fprintf(stderr, "hid_write_identification failed with return code %d\n", ret);
    return 1;
  } else {
    fprintf(stderr, "*** hid_write_identification success! ***\n");
  }
 
  ret = hid_dump_tree(stdout, hid);
  if (ret != HID_RET_SUCCESS) {
    fprintf(stderr, "hid_dump_tree failed with return code %d\n", ret);
    return 1;
  } else {
    fprintf(stderr, "*** hid_dump_tree success! ***\n");
  }
 
  RFIDCommunication(ret, &hid);
  printf("\n\n/* * * * * * * * END COMMUNICATION WITH RFID DEVICE * * * * * * * */\n\n");

  ret = hid_close(hid);
  if (ret != HID_RET_SUCCESS) {
    fprintf(stderr, "hid_close failed with return code %d\n", ret);
    return 1;
  } else {
    fprintf(stderr, "*** hid_close successful! ***\n");
  }
 
  hid_delete_HIDInterface(&hid);
 
  ret = hid_cleanup();
  if (ret != HID_RET_SUCCESS) {
    fprintf(stderr, "hid_cleanup failed with return code %d\n", ret);
    return 1;
  } else {
    fprintf(stderr, "*** hid_cleanup successful! ***\n");
  }
  
  return 0;
}
Exemplo n.º 8
0
int main (int argc, char **argv)
{
  int flag;
  signed short svalue;
  __u8 input, channel, gain, options;
  __u8 gains[8], channels[8];
  __u16 value;
  __u16 out_data[512];
  signed short in_data[1024];
  int count;
  int temp, i, j;
  int ch;
  float freq;
  time_t startTime, endTime;
  getAllValues allValue;

  HIDInterface*  hid[4];  // Composite device with 4 interfaces.
  hid_return ret;
  int interface;
  
  // Debug information.  Delete when not needed    
  //  hid_set_debug(HID_DEBUG_ALL);
  //  hid_set_debug_stream(stderr);
  //  hid_set_usb_debug(2);

  ret = hid_init();
  if (ret != HID_RET_SUCCESS) {
    fprintf(stderr, "hid_init failed with return code %d\n", ret);
    return -1;
  }

  for ( i = 0; i <= 3; i++ ) {
    if ((interface = PMD_Find_Interface(&hid[i], i, USB1408FS_PID)) < 0 ) {
      fprintf(stderr, "USB 1408FS not found.\n");
      exit(1);
    } else {
      printf("USB 1408FS Device is found! Interface = %d\n", interface);
    }
  }

  /* config mask 0x01 means all inputs */
  usbDConfigPort_USB1408FS(hid[0], DIO_PORTA, DIO_DIR_OUT);
  usbDConfigPort_USB1408FS(hid[0], DIO_PORTB, DIO_DIR_IN);
  usbDOut_USB1408FS(hid[0], DIO_PORTA, 0);
  usbDOut_USB1408FS(hid[0], DIO_PORTA, 0);

  while(1) {
    printf("\nUSB 1408FS Testing\n");
    printf("----------------\n");
    printf("Hit 'a' to test analog output scan.\n");    
    printf("Hit 'b' to blink LED.\n");
    printf("Hit 'c' to test counter.\n");
    printf("Hit 'd' to test digital I/O.\n");
    printf("Hit 'e' to exit.\n");
    printf("Hit 'f' to get all values.\n");
    printf("Hit 'g' to test analog input scan (differential).\n");
    printf("Hit 'h' to test analog input (single ended).\n");
    printf("Hit 'i' to test analog input (differential mode).\n");
    printf("Hit 'j' to test analog input scan (single ended).\n");
    printf("Hit 'o' to test analog output.\n");
    printf("Hit 'r' to reset.\n");
    printf("Hit 's' to get status.\n");

    while((ch = getchar()) == '\0' || ch == '\n');

    switch(tolower(ch)) {
      case 'b': /* test to see if led blinks */
        usbBlink_USB1408FS(hid[0]);
        break;
      case 'c':
        printf("connect pin 20 and 21\n");
        usbInitCounter_USB1408FS(hid[0]);
        sleep(1);
        flag = fcntl(fileno(stdin), F_GETFL);
        fcntl(0, F_SETFL, flag | O_NONBLOCK);
        do {
          usbDOut_USB1408FS(hid[0], DIO_PORTA, 1);
	  sleep(1);
          usbDOut_USB1408FS(hid[0], DIO_PORTA, 0);
	  printf("Counter = %d\n",usbReadCounter_USB1408FS(hid[0]));
        } while (!isalpha(getchar()));
        fcntl(fileno(stdin), F_SETFL, flag);
        break;
      case 'd':
        printf("\nTesting Digital I/O....\n");
        printf("connect pins 21 through 28 <=> 32 through 39\n");
        do {
          printf("Enter a byte number [0-0xff]: " );
          scanf("%x", &temp);
          usbDOut_USB1408FS(hid[0], DIO_PORTA, (__u8)temp);
          usbDIn_USB1408FS(hid[0], DIO_PORTB, &input);
          printf("The number you entered = %#x\n",input);
        } while (toContinue());
        break;
      case 'o': /* test the analog output */
        printf("Testing the analog output...\n");
        printf("Enter channel [0-1] => (pin 13-14):");
        scanf("%d", &temp);
        channel = (__u8) temp;
        printf("Enter a value: ");
        scanf("%hx", &value);
        usbAOut_USB1408FS(hid[0], channel, value);
        break;
      case 'a': /* test Analog Output Scan */
        printf("Enter desired frequency [Hz]: ");
        scanf("%f", &freq);
        for ( j = 0; j <  2; j++ ) {
	  for (i = 0; i < 512; i++) {
	    out_data[i] = i%2 ? 0 : 0xfff;
	  }
	  usbAOutScan_USB1408FS(hid, 0, 0, 512, &freq, out_data, 1);
	}
	break;
      case 'g':
        printf("Enter desired frequency [Hz]: ");
        scanf("%f", &freq);
        printf("Enter number of samples [1-1024]: ");
        scanf("%d", &count);
        printf("\t\t1. +/- 20.V\n");
        printf("\t\t2. +/- 10.V\n");
        printf("\t\t3. +/- 5.V\n");
        printf("\t\t4. +/- 4.V\n");
        printf("\t\t5. +/- 2.5V\n");
        printf("\t\t6. +/- 2.0V\n");
        printf("\t\t7. +/- 1.25V\n");
        printf("\t\t8. +/- 1.0V\n");
        printf("Select gain: [1-8]\n");
        scanf("%d", &temp);
        switch(temp) {
          case 1: gain = BP_20_00V;
            break;
          case 2: gain = BP_10_00V;
            break;
          case 3: gain = BP_5_00V;
            break;
          case 4: gain = BP_4_00V;
            break;
          case 5: gain = BP_2_50V;
            break;
          case 6: gain = BP_2_00V;
            break;
          case 7: gain = BP_1_25V;
            break;
          case 8: gain = BP_1_00V;
            break;
          default:
            break;
	}
	for (i = 0; i < 8; i++) {
	  channels[i] = i;
	  gains[i] = gain;
	}
	usbALoadQueue_USB1408FS(hid[0], 1, channels, gains);
	options = AIN_EXECUTION | AIN_GAIN_QUEUE;
        for ( i = 0; i < 1024; i++ ) {  // load data with known value
	  in_data[i] = 0xbeef;
	}
        usbAInScan_USB1408FS(hid, 0, 0, count, &freq, options, in_data);
	for ( i = 0; i < count; i++ ) {
	  printf("data[%d] = %#hx  %.2fV\n", i, in_data[i], volts_1408FS(gain, in_data[i]));
	}
	usbAInStop_USB1408FS(hid[0]);
	break;
      case 'h':
        printf("Testing Analog Input Single Ended Mode\n");
        printf("Select channel [0-7]: ");
        scanf("%d", &temp);
	channel = (__u8) (temp);
	gain =  SE_10_00V;
        flag = fcntl(fileno(stdin), F_GETFL);
        fcntl(0, F_SETFL, flag | O_NONBLOCK);
        do {
          sleep(1);
	  svalue = usbAIn_USB1408FS(hid[0], channel, gain);
	  printf("Channel: %d: value = %#hx, %.2fV\n",
		 channel, svalue, volts_1408FS_SE(svalue));
	} while (!isalpha(getchar()));
	fcntl(fileno(stdin), F_SETFL, flag);
	break;
      case 'i':
        printf("Connect pin 1 - pin 21  and pin 2 - pin 3\n");
        printf("Select channel [0-3]: ");
        scanf("%d", &temp);
        if ( temp < 0 || temp > 3 ) break;
        channel = (__u8) temp;
        printf("\t\t1. +/- 20.V\n");
        printf("\t\t2. +/- 10.V\n");
        printf("\t\t3. +/- 5.V\n");
        printf("\t\t4. +/- 4.V\n");
        printf("\t\t5. +/- 2.5V\n");
        printf("\t\t6. +/- 2.0V\n");
        printf("\t\t7. +/- 1.25V\n");
        printf("\t\t8. +/- 1.0V\n");
        printf("Select gain: [1-8]\n");
        scanf("%d", &temp);
        switch(temp) {
          case 1: gain = BP_20_00V;
            break;
          case 2: gain = BP_10_00V;
            break;
          case 3: gain = BP_5_00V;
            break;
          case 4: gain = BP_4_00V;
            break;
          case 5: gain = BP_2_50V;
            break;
          case 6: gain = BP_2_00V;
            break;
          case 7: gain = BP_1_25V;
            break;
          case 8: gain = BP_1_00V;
            break;
          default:
            break;
	}
        flag = fcntl(fileno(stdin), F_GETFL);
        fcntl(0, F_SETFL, flag | O_NONBLOCK);
        do {
          usbDOut_USB1408FS(hid[0], DIO_PORTA, 0);
	  sleep(1);
	  svalue = usbAIn_USB1408FS(hid[0], channel, gain);
	  printf("Channel: %d: value = %#hx, %.2fV\n",
		 channel, svalue, volts_1408FS(gain, svalue));
          usbDOut_USB1408FS(hid[0], DIO_PORTA, 0x1);
	  sleep(1);
	  svalue = usbAIn_USB1408FS(hid[0], channel, gain);
	  printf("Channel: %d: value = %#hx, %.2fV\n",
		 channel, svalue, volts_1408FS(gain, svalue));
	} while (!isalpha(getchar()));
	fcntl(fileno(stdin), F_SETFL, flag);
        printf("Doing a timing test.  Please wait ...\n");
        time(&startTime);
        for (count = 0; count < 500; count++) {
	  svalue = usbAIn_USB1408FS(hid[0], channel, gain);
	}
        time(&endTime);
        printf("Sampling speed is %ld Hz.\n", 500/(endTime - startTime));
	break;
      case 'j':
        printf("Test of scan mode (single ended).\n");
        printf("Enter desired frequency [Hz]: ");
        scanf("%f", &freq);
        printf("Enter number of samples [1-1024]: ");
        scanf("%d", &count);
	options = AIN_EXECUTION | AIN_GAIN_QUEUE;
        for ( i = 0; i < 1024; i++ ) {  // load data with known value
	  in_data[i] = 0xbeef;
	}
        usbAInScan_USB1408FS_SE(hid, 0, 0, count, &freq, options, in_data);
	for ( i = 0; i < count; i++ ) {
	  printf("data[%d] = %#hx  %.2fV\n", i, in_data[i], volts_1408FS_SE(in_data[i]));
	}
	break;
      case 'f':
        usbGetAll_USB1408FS(hid[0], &allValue);
	for (i = 0; i < 4; i++) {
	  printf("Differential Reference Low channel[%d] = %hd \n", i, allValue.ref_Low[i]);
	}
	for (i = 0; i < 4; i++) {
	  printf("Differential Reference High channel[%d] = %hd \n", i, allValue.ref_High[i]);
	}
	for (i = 0; i < 8; i++) {
	  printf("Single Ended Input channel[%d] = %hd \n", i, allValue.se[i]);
	}
	printf("DIO Port A = %#x\n", allValue.dio_portA);
	printf("DIO Port B = %#x\n", allValue.dio_portB);
	printf("\n\n\n");
	break;
      case 's':
        printf("Status = %#x\n", usbGetStatus_USB1408FS(hid[0]));
	break;
      case 'r':
        usbReset_USB1408FS(hid[0]);
        return 0;
	break;
      case 'e':
        for ( i = 0; i <= 3; i++ ) {
	  ret = hid_close(hid[i]);
	  if (ret != HID_RET_SUCCESS) {
	    fprintf(stderr, "hid_close failed with return code %d\n", ret);
	    return 1;
	  }
	  hid_delete_HIDInterface(&hid[i]);
	}

	ret = hid_cleanup();
	if (ret != HID_RET_SUCCESS) {
	  fprintf(stderr, "hid_cleanup failed with return code %d\n", ret);
	  return 1;
	}
        return 0;
	break;
      default:
        break;
    }
  }
}
Exemplo n.º 9
0
int main (int argc, char **argv) {

  int flag;
  signed short svalue;
  __u8 channel, gain;
  int temp, i;
  int ch;
  __u8 gainArray[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0, 0, 0, 0, 0};
  signed short in_data[1024];
  int count;
  int options;
  float freq;
  char serial[9];
  HIDInterface*  hid[7];  // Composite device with 7 interfaces.
  hid_return ret;
  int interface;
  
  // Debug information.  Delete when not needed    
  //  hid_set_debug(HID_DEBUG_ALL);
  //  hid_set_debug_stream(stderr);
  //  hid_set_usb_debug(2);

  ret = hid_init();
  if (ret != HID_RET_SUCCESS) {
    fprintf(stderr, "hid_init failed with return code %d\n", ret);
    return -1;
  }

  for ( i = 0; i <= 6; i++ ) {
    if ((interface = PMD_Find_Interface(&hid[i], i, USB1616FS_PID)) < 0) {
      fprintf(stderr, "USB 1616FS not found.\n");
      exit(1);
    } else {
      printf("USB 1616FS Device is found! Interface = %d\n", interface);
    }
  }

  /* config mask 0x01 means all inputs */
  usbDConfigPort_USB1616FS(hid[0], DIO_DIR_OUT);
  usbDOut_USB1616FS(hid[0], 0);

  while(1) {
    printf("\nUSB 1616FS Testing\n");
    printf("----------------\n");
    printf("Hit 'b' to blink LED\n");
    printf("Hit 'c' to test counter\n");
    printf("Hit 'd' to test digital output\n");
    printf("Hit 'e' to exit\n");
    printf("Hit 'g' to get serial number\n");
    printf("Hit 'I' to test analog input scan\n");    
    printf("Hit 'i' to test analog input\n");
    printf("Hit 'r' to reset\n");
    printf("Hit 's' to get status\n");
    printf("Hit 't' to test digital bit I/O\n");

    while((ch = getchar()) == '\0' ||
      ch == '\n');

    switch(ch) {
      case 'b': /* test to see if led blinks */
        usbBlink_USB1616FS(hid[0]);
        break;
      case 'c':
        printf("connect pin 38 and 21\n");
        usbInitCounter_USB1616FS(hid[0]);
        sleep(1);
        flag = fcntl(fileno(stdin), F_GETFL);
        fcntl(0, F_SETFL, flag | O_NONBLOCK);
        do {
          usbDOut_USB1616FS(hid[0], 1);
	  usleep(300000);
          usbDOut_USB1616FS(hid[0], 0);
	  printf("Counter = %d\n",usbReadCounter_USB1616FS(hid[0]));
        } while (!isalpha(getchar()));
        fcntl(fileno(stdin), F_SETFL, flag);
        break;
      case 'g':
        usbReadCode_USB1616FS(hid[0], 0x200000, 8, (__u8 *) serial);
        serial[8] = '\0';
        printf("Serial Number = %s\n", serial);
        break;
      case 'I':
        printf("Enter desired frequency [Hz]: ");
        scanf("%f", &freq);
        printf("Enter number of samples [1-1024]: ");
        scanf("%d", &count);
	printf("\t\t1. +/- 10.V\n");
        printf("\t\t2. +/- 5.V\n");
        printf("\t\t3. +/- 2.5V\n");
        printf("\t\t4. +/- 2.V\n");
        printf("\t\t5. +/- 1.25V\n");
        printf("\t\t6. +/- 1.0V\n");
        printf("\t\t7. +/- 0.625V\n");
        printf("\t\t8. +/- 0.3125V\n");
        printf("Select gain: [1-8]\n");
        scanf("%d", &temp);
        switch(temp) {
          case 1: gain = BP_10_00V;
            break;
          case 2: gain = BP_5_00V;
            break;
          case 3: gain = BP_2_50V;
            break;
          case 4: gain = BP_2_00V;
            break;
          case 5: gain = BP_1_25V;
            break;
          case 6: gain = BP_1_00V;
            break;
          case 7: gain = BP_0_625V;
            break;
          case 8: gain = BP_0_3125V;
            break;
          default:
            break;
	}
	// Load the gain queue
	gainArray[0] = gain;
	usbAInLoadQueue_USB1616FS(hid[0], gainArray);

	// configure options
	options = AIN_EXECUTION | AIN_DEBUG_MODE;
	//options = AIN_EXECUTION ;
	for ( i = 0; i < 1024; i++ ) {  // load data with known value
	  in_data[i] = 0xbeef;
	}
        usbAInScan_USB1616FS(hid, 0, 0, count, &freq, options, in_data);
	printf("Actual frequency = %f\n", freq);
	for ( i = 0; i < count; i++ ) {
	  printf("data[%d] = %#hx  %.2fV\n", i, in_data[i], volts_USB1616FS(gain, in_data[i]));
	}
	break;
      case 'i':
        printf("Connect pin 1 - pin 23\n");
        printf("Select channel [0-15]: ");
        scanf("%d", &temp);
        if ( temp < 0 || temp > 15 ) break;
        channel = (__u8) temp;
        printf("\t\t1. +/- 10.V\n");
        printf("\t\t2. +/- 5.V\n");
        printf("\t\t3. +/- 2.5V\n");
        printf("\t\t4. +/- 2.V\n");
        printf("\t\t5. +/- 1.25V\n");
        printf("\t\t6. +/- 1.0V\n");
        printf("\t\t7. +/- 0.625V\n");
        printf("\t\t8. +/- 0.3125V\n");
        printf("Select gain: [1-8]\n");
        scanf("%d", &temp);
        switch(temp) {
          case 1: gain = BP_10_00V;
            break;
          case 2: gain = BP_5_00V;
            break;
          case 3: gain = BP_2_50V;
            break;
          case 4: gain = BP_2_00V;
            break;
          case 5: gain = BP_1_25V;
            break;
          case 6: gain = BP_1_00V;
            break;
          case 7: gain = BP_0_625V;
            break;
          case 8: gain = BP_0_3125V;
            break;
          default:
            break;
	}
        flag = fcntl(fileno(stdin), F_GETFL);
        fcntl(0, F_SETFL, flag | O_NONBLOCK);
        do {
	  svalue = usbAIn_USB1616FS(hid[0], channel, gain);
	  printf("Channel: %d: value = %#hx, %.2fV\n",
		 channel, svalue, volts_USB1616FS(gain, svalue));
	} while (!isalpha(getchar()));
	fcntl(fileno(stdin), F_SETFL, flag);
	break;
      case 's':
        printf("Status = %#x\n", usbGetStatus_USB1616FS(hid[0]));
	break;
      case 'r':
        usbReset_USB1616FS(hid[0]);
        return 0;
	break;
      case 'e':
        for ( i = 0; i <= 6; i++ ) {
          ret = hid_close(hid[i]);
          if (ret != HID_RET_SUCCESS) {
	    fprintf(stderr, "hid_close failed with return code %d\n", ret);
	    return 1;
          }
	  hid_delete_HIDInterface(&hid[i]);
        }
        ret = hid_cleanup();
        if (ret != HID_RET_SUCCESS) {
	  fprintf(stderr, "hid_cleanup failed with return code %d\n", ret);
	  return 1;
        }
        return 0;
        break;
      default:
        break;
    }
  }
}
Exemplo n.º 10
0
Arquivo: main.c Projeto: patzy/rcd
int main(int argc,char **argv)
{
  ir_receiver_t* receiver;

  // parse command line args
  if (argc!=2) {
    printf("You must specify the receiver type: %s type\n",argv[0]);
    printf("Valid values for type are:\n");
    unsigned int i=0;
    for (i=0;i<NB_IRR;i++)
      {
        printf("\t- %s\n",receivers[i].description);
      }
    return 1;
  }
  else {
    unsigned int i=0;
    for (i=0;i<NB_IRR;i++)
      {
        if (strncmp(receivers[i].description,argv[1],256)==0) {
          receiver=&receivers[i];
          break;
        }
      }
  }
  printf("Running for type: %s\n",receiver->description);

  HIDInterface* hid;
  hid_return ret;
  hid_set_debug(0/*HID_DEBUG_ALL*/);
  hid_set_debug_stream(stderr);
  /* passed directly to libusb */
  hid_set_usb_debug(0);
  ret = hid_init();
  if (ret != HID_RET_SUCCESS) {
    fprintf(stderr, "hid_init failed with return code %d\n", ret);
    return 1;
  }

  hid = hid_new_HIDInterface();
  if (hid == 0) {
    fprintf(stderr, "hid_new_HIDInterface() failed, out of memory?\n");
    return 1;
  }

  ret = hid_force_open(hid, 0, &receiver->matcher, 3);
  if (ret != HID_RET_SUCCESS) {
    fprintf(stderr, "hid_force_open failed with return code %d\n", ret);
    return 1;
  }

  ret = hid_write_identification(stderr, hid);
  if (ret != HID_RET_SUCCESS) {
    fprintf(stderr, "hid_write_identification failed with return code %d\n",
            ret);
    return 1;
  }

  ret = hid_dump_tree(stderr, hid);
  if (ret != HID_RET_SUCCESS) {
    fprintf(stderr, "hid_dump_tree failed with return code %d\n", ret);
    return 1;
  }

  /* Infinite parsing loop use C-c to stop the program */

  while (1)
    {
      char packet[receiver->packetSize];
      ret = hid_interrupt_read(hid, 0x81,  packet, receiver->packetSize,10000);
      if (ret == HID_RET_SUCCESS) {
          fflush(stdout);
       }
    }
  ret = hid_close(hid);
  if (ret != HID_RET_SUCCESS) {
    fprintf(stderr, "hid_close failed with return code %d\n", ret);
    return 1;
  }

  hid_delete_HIDInterface(&hid);

  ret = hid_cleanup();
  if (ret != HID_RET_SUCCESS) {
    fprintf(stderr, "hid_cleanup failed with return code %d\n", ret);
    return 1;
  }

  return 0;
}
Exemplo n.º 11
0
int main(int argc, char *argv[])
{
  HIDInterface* hid;
  int iface_num = 2;
  hid_return ret;
  int index=0;
  char buff[42];
  unsigned short vendor_id  = 0x0451;
  unsigned short product_id = 0x2100;
  int flag;
  uint16_t addr=0;

  uint8_t eepromBuff[512];
  memset(eepromBuff, 0x00, 512);

  // HIDInterfaceMatcher matcher = { 0x0925, 0x1237, NULL, NULL, 0 };
  HIDInterfaceMatcher matcher = { vendor_id, product_id, NULL, NULL, 0 };

#if 1
  /* see include/debug.h for possible values */
  hid_set_debug(HID_DEBUG_ALL);
  hid_set_debug_stream(stderr);
  /* passed directly to libusb */
  hid_set_usb_debug(0);
  
  ret = hid_init();
  if (ret != HID_RET_SUCCESS) {
    fprintf(stderr, "hid_init failed with return code %d\n", ret);
    return 1;
  }

  hid = hid_new_HIDInterface();
  if (hid == 0) {
    fprintf(stderr, "hid_new_HIDInterface() failed, out of memory?\n");
    return 1;
  }

  ret = hid_force_open(hid, iface_num, &matcher, 3);
  if (ret != HID_RET_SUCCESS) {
    fprintf(stderr, "hid_force_open failed with return code %d\n", ret);
    return 1;
  }

  ret = hid_write_identification(stdout, hid);
  if (ret != HID_RET_SUCCESS) {
    fprintf(stderr, "hid_write_identification failed with return code %d\n", ret);
    return 1;
  }

  ret = hid_dump_tree(stdout, hid);
  if (ret != HID_RET_SUCCESS) {
    fprintf(stderr, "hid_dump_tree failed with return code %d\n", ret);
    return 1;
  }

//#define _READ
#ifdef _READ
  memset(eepromBuff, 0x00, 512);
  for (index=0;index < 64;index++) {
      memset(buff, 0x00, 42);
      addr = 8*index;
      buff[0] = PCTransfer;
      buff[1] = RequestEepromSectionRead;
      buff[2] = (0xFF00 & addr) >> 8; //M:offset
      buff[3] = 0x00FF & addr; //L:offset
      buff[4] = 8; //bytes

      fprintf(stderr, "sending:\n");
      print_bytes(LOG_ERROR, buff, 42);

      ret = hid_interrupt_write(hid, 0x04, buff, 42, 1000);
      if (ret != HID_RET_SUCCESS) {
        fprintf(stderr, "hid_set_output_report failed with return code %d\n", ret);
      }
      ret = hid_interrupt_read(hid, 0x03, buff, 42, 1000);
      if (ret != HID_RET_SUCCESS) {
        fprintf(stderr, "hid_set_output_report failed with return code %d\n", ret);
      }

      fprintf(stderr, "readed:\n");
      print_bytes(LOG_ERROR, buff, 42);

      if(buff[0] & PCRequestError) {
        logwrite(LOG_ERROR, "USB: request error");
      }

      if(!(buff[1] & RequestDone)) {
        logwrite(LOG_ERROR, "USB: request is not done!");
      }

      logwrite(LOG_ERROR, "USB: retval: %02x", buff[2]);
      memcpy(&eepromBuff[8*index], &buff[3], 8);
  }
#endif

#define _WRITE
#ifdef _WRITE
  memset(eepromBuff, 0xFF, 512);
  int commands = eeprom_data_size()/8;
  if(commands < 0 || commands > 62) {
    logwrite(LOG_ERROR, "WRONG COMMANDS NUMBER: %d", commands);
    exit(1);
  }
  logwrite(LOG_ERROR, "total commands: %d", commands);
  /* 2 bytes of sync + number of commands! */
  eepromBuff[0] = 0xb0;
  eepromBuff[1] = 0x0b;
  eepromBuff[2] = commands;

  for (index=0;index < commands;index++) {
      fprintf(stderr, "w%d: eepromBuff[%d], eepromData[%d]\n", index, (index+1)*8, index*8);
      memcpy(&eepromBuff[(index+1)*8], &eepromData[8*index], 8);
  }

  for (index=0;index <= commands;index++) {
      addr = 8*index;
      memset(buff, 0x00, 42);
      buff[0] = PCTransfer;
      buff[1] = RequestEepromSectionWrite;
      buff[2] = (0xFF00 & addr) >> 8; //M:offset
      buff[3] = 0x00FF & addr; //L:offset
      buff[4] = 8; //bytes
      memcpy(&buff[5], &eepromBuff[8*index], 8);

      fprintf(stderr, "sending:\n");
      print_bytes(LOG_ERROR, buff, 42);

      ret = hid_interrupt_write(hid, 0x04, buff, 42, 1000);
      if (ret != HID_RET_SUCCESS) {
        fprintf(stderr, "hid_set_output_report failed with return code %d\n", ret);
      }
      ret = hid_interrupt_read(hid, 0x03, buff, 42, 1000);
      if (ret != HID_RET_SUCCESS) {
        fprintf(stderr, "hid_set_output_report failed with return code %d\n", ret);
      }

      fprintf(stderr, "readed:\n");
      print_bytes(LOG_ERROR, buff, 42);

      if(buff[0] & PCRequestError) {
        logwrite(LOG_ERROR, "USB: request error");
      }

      if(!(buff[1] & RequestDone)) {
        logwrite(LOG_ERROR, "USB: request is not done!");
      }

      logwrite(LOG_ERROR, "USB: retval: %02x", buff[2]);
  }
#endif

  ret = hid_close(hid);
  if (ret != HID_RET_SUCCESS) {
    fprintf(stderr, "hid_close failed with return code %d\n", ret);
    return 1;
  }

  hid_delete_HIDInterface(&hid);

  ret = hid_cleanup();
  if (ret != HID_RET_SUCCESS) {
    fprintf(stderr, "hid_cleanup failed with return code %d\n", ret);
    return 1;
  }
#endif

  fprintf(stderr, "EEPROM DATA:\n");
  for (index=0;index < 64;index++) {
    print_bytes(LOG_ERROR, &eepromBuff[8*index], 8);
  }


  return 0;
}
Exemplo n.º 12
0
int main(int argc, char **argv) {

  int ch;
  int i;
  int nchan;
  __u8 bIReg, bOReg;
  __u8 in_options, out_options;
  float temperature;
  float volts;
  float temperature_array[8];
  float value_1, value_2;
  int flag;
  char type[10];

  HIDInterface*  hid = 0x0;  // Composite device with 1 interface.
  hid_return ret;
  int interface;
  char serial[9];
  
  // Debug information.  Delete when not needed    
  //  hid_set_debug(HID_DEBUG_ALL);
  //  hid_set_debug_stream(stderr);
  //  hid_set_usb_debug(2);

  ret = hid_init();
  if (ret != HID_RET_SUCCESS) {
    fprintf(stderr, "hid_init failed with return code %d\n", ret);
    return -1;
  }

  if ((interface = PMD_Find_Interface(&hid, 0, USBTC_AI_PID)) < 0) {
      fprintf(stderr, "USB TC-AI not found.\n");
      exit(1);
  } else {
      printf("USB TC-AI Device is found! Interface = %d\n", interface);
  }

  /* config mask 0x01 means all inputs */
  usbDConfigPort_USBTC_AI(hid, DIO_DIR_OUT);
  usbDOut_USBTC_AI(hid, 0x0);

  while(1) {
    printf("\nUSB TC-AI Testing\n");
    printf("----------------\n");
    printf("Hit 'a' to get alarm status\n");
    printf("Hit 'b' to blink LED\n");
    printf("Hit 'c' to calibrate\n");
    printf("Hit 'd' to test DIO\n");
    printf("Hit 'e' to exit\n");
    printf("Hit 'f' for burnout status\n");
    printf("Hit 'g' to get serial number\n");
    printf("Hit 'h' to run the counter\n");
    printf("Hit 'r' to reset\n");
    printf("Hit 'p' read the CJC\n");
    printf("Hit 's' to get status\n");
    printf("Hit 't' to measure temperature\n");
    printf("Hit 'x' to measure temperature (Thermocouple) multiple channels\n");
    printf("Hit 'v' to measure voltage (channels 4-7)\n");

    while((ch = getchar()) == '\0' ||
      ch == '\n');

    switch(tolower(ch)) {
      case 'a':
        for ( i = 0; i < 8; i++ ) {
	  value_1 = 1.0 + i;
	  value_2 = 0.0;
	  in_options = 0x40 | i;
	  out_options = 0x0;  // disable alarm
          usleep(10000);
          usbConfigAlarm_USBTC_AI(hid, i, in_options, out_options, value_1, value_2);
          usleep(10000);
    	  usbGetAlarmConfig_USBTC_AI(hid, i, &in_options, &out_options, &value_1, &value_2);
	  printf("Alarm %d: input options = %#x  output options = %#x  value_1 = %f  value_2 = %f\n",
		 i, in_options, out_options, value_1, value_2);
	}
        printf("\n");
	break;
      case 'b': /* test to see if led blinks */
        usbBlink_USBTC_AI(hid);
        break;
      case 'c': /* calibration */
        usbCalibrate_USBTC_AI(hid, 0);
        usbCalibrate_USBTC_AI(hid, 1);
        break;
      case 'f':  /* Get status of thermocouple burnout detection */
	 printf("Burnout status = %#x\n", usbGetBurnoutStatus_USBTC_AI(hid, 0xf));
	 break;
      case 'g':
        strncpy(serial, PMD_GetSerialNumber(hid), 9);
        printf("Serial Number = %s\n", serial);
        break;
      case 'p':  /* read the CJC */
        usbAin_USBTC_AI(hid, CJC0, 0, &temperature);
	printf("CJC 0 = %.2f degress Celsius or %.2f degrees Fahrenheit.\n", temperature,
	        celsius2fahr(temperature));
        break;
      case 'd': /* test to see if led blinks */
        printf("conect DIO0 - DIO4\n");
	printf("conect DIO1 - DIO5\n");
	printf("conect DIO2 - DIO6\n");
	printf("conect DIO3 - DIO7\n");
	usbDConfigBit_USBTC_AI(hid, 0,  DIO_DIR_OUT);
	usbDConfigBit_USBTC_AI(hid, 1,  DIO_DIR_OUT);
	usbDConfigBit_USBTC_AI(hid, 2,  DIO_DIR_OUT);
	usbDConfigBit_USBTC_AI(hid, 3,  DIO_DIR_OUT);
	usbDConfigBit_USBTC_AI(hid, 4,  DIO_DIR_IN);
	usbDConfigBit_USBTC_AI(hid, 5,  DIO_DIR_IN);
	usbDConfigBit_USBTC_AI(hid, 6,  DIO_DIR_IN);
	usbDConfigBit_USBTC_AI(hid, 7,  DIO_DIR_IN);
	do {
  	  printf("Enter value [0-f]: ");
	  scanf("%hhx", &bIReg);
	  bIReg &= 0xf;
  	  usbDOut_USBTC_AI(hid, bIReg);
	  usbDIn_USBTC_AI(hid, &bOReg);
	  printf("value = %#hhx\n", bOReg);
	} while (toContinue());
	break;
      case 'h':
	printf("connect CTR to DIO0.\n");
	usbDConfigBit_USBTC_AI(hid, 0,  DIO_DIR_OUT);
	usbInitCounter_USBTC_AI(hid);
        sleep(1);
        flag = fcntl(fileno(stdin), F_GETFL);
        fcntl(0, F_SETFL, flag | O_NONBLOCK);
        do {
          usbDOutBit_USBTC_AI(hid, 0, 1);
	  usleep(200000);
          usbDOutBit_USBTC_AI(hid, 0, 0);
	  printf("Counter = %d\n",usbReadCounter_USBTC_AI(hid));
        } while (!isalpha(getchar()));
        fcntl(fileno(stdin), F_SETFL, flag);
	break;
      case 's':
        printf("Status = %#x\n", usbGetStatus_USBTC_AI(hid));
	break;
      case 'r':
        usbReset_USBTC_AI(hid);
        return 0;
	break;
      case 'e':
	ret = hid_close(hid);
        if (ret != HID_RET_SUCCESS) {
	  fprintf(stderr, "hid_close failed with return code %d\n", ret);
	  return 1;
        }
        hid_delete_HIDInterface(&hid);
        ret = hid_cleanup();
        if (ret != HID_RET_SUCCESS) {
	  fprintf(stderr, "hid_cleanup failed with return code %d\n", ret);
	  return 1;
        }
        return 0;
	break;
      case 't':
        printf("Select Channel [0-3]: ");
	scanf("%d", &i);
        if ( i > 3 ) {
          printf("Channel must be between 0-3.\n");
          i = 0;
	}
        printf("Connect thermocouple to channel %d\n", i);
	printf(" Select Thermocouple Type [JKSRBETN]: ");
	scanf("%s", type);
	switch(type[0]) {
	case 'J':
	  bIReg = TYPE_J;
	  printf("Type J Thermocouple Selected: \n");
	  break;
	case 'K':
	  bIReg = TYPE_K;
  	  printf("Type K Thermocouple Selected: \n");
	  break;
	case 'T':
	  bIReg = TYPE_T;
    	  printf("Type T Thermocouple Selected: \n");
	  break;
	case 'E':
	  bIReg = TYPE_E;
    	  printf("Type E Thermocouple Selected: \n");
	  break;
	case 'R':
	  bIReg = TYPE_R;
      	  printf("Type R Thermocouple Selected: \n");
	  break;
	case 'S':
	  bIReg = TYPE_S;
       	  printf("Type S Thermocouple Selected: \n");
	  break;
	case 'B':
	  bIReg = TYPE_B;
       	  printf("Type B Thermocouple Selected: \n");
	  break;
	case 'N':
	  bIReg = TYPE_N;
       	  printf("Type N Thermocouple Selected: \n");
	  break;
        default:
	  printf("Unknown or unsupported thermocopule type.\n");
	  break;
	}
        usbSetItem_USBTC_AI(hid, i, i%2+CH_0_TC, bIReg);
        flag = fcntl(fileno(stdin), F_GETFL);
        fcntl(0, F_SETFL, flag | O_NONBLOCK);
	do {
          usbAin_USBTC_AI(hid, i, 0, &temperature);
    	  printf("Channel: %d  %.2f degress Celsius or %.2f degrees Fahrenheit.\n",
		 i, temperature, celsius2fahr(temperature));
  	  sleep(1);
	} while (!isalpha(getchar()));
	fcntl(fileno(stdin), F_SETFL, flag);
	break;
      case 'x':
	printf("Enter number of Channels (1-4): ");
	scanf("%d", &nchan);
        if (nchan > 4) nchan = 4;
	for ( i = 0; i < nchan; i++ ) {
          printf("Connect thermocouple to channel %d\n", i);
	  printf(" Select Thermocouple Type [JKSRBETN]: ");
	  scanf("%s", type);
	  switch(type[0]) {
	  case 'J':
	    bIReg = TYPE_J;
	    printf("Type J Thermocouple Selected: \n");
	    break;
	  case 'K':
	    bIReg = TYPE_K;
  	    printf("Type K Thermocouple Selected: \n");
	    break;
	  case 'T':
	    bIReg = TYPE_T;
    	    printf("Type T Thermocouple Selected: \n");
	    break;
	  case 'E':
	    bIReg = TYPE_E;
    	    printf("Type E Thermocouple Selected: \n");
	    break;
	  case 'R':
	    bIReg = TYPE_R;
      	    printf("Type R Thermocouple Selected: \n");
	    break;
	  case 'S':
	    bIReg = TYPE_S;
       	    printf("Type S Thermocouple Selected: \n");
	    break;
	  case 'B':
	    bIReg = TYPE_B;
       	    printf("Type B Thermocouple Selected: \n");
	    break;
	  case 'N':
	    bIReg = TYPE_N;
       	    printf("Type N Thermocouple Selected: \n");
	    break;
          default:
	    printf("Unknown or unsupported thermocopule type.\n");
	    break;
	  }
          usbSetItem_USBTC_AI(hid, i/2, i%2+CH_0_TC, bIReg);
	}
        flag = fcntl(fileno(stdin), F_GETFL);
        fcntl(0, F_SETFL, flag | O_NONBLOCK);
	do {
          usbAinScan_USBTC_AI(hid, CH0, nchan-1, 0, temperature_array);
	  for ( i = 0; i < nchan; i++ ) {
  	    printf("Channel %d:  %.2f degress Celsius or %.2f degrees Fahrenheit.\n",
		   i, temperature_array[i], celsius2fahr(temperature_array[i]));
	  }
	  printf("\n");
	  sleep(1);
	} while (!isalpha(getchar()));
	fcntl(fileno(stdin), F_SETFL, flag);
	break;
      case 'v':
      	printf("Select Channel [4-7]: ");
        scanf("%d", &i);
        if ( i < 4 || i > 7 ) {
	  printf("Channel must be between 4-7 inclusive.  Setting to 4\n");
	i = 4;
    	}
	//usbSetItem_USBTC_AI(hid, i/2, SENSOR_TYPE, VOLTAGE);
	usbSetItem_USBTC_AI(hid, i/2, i%2+CH_0_VOLT_CONN, DIFFERENTIAL);
	usbSetItem_USBTC_AI(hid, i/2, i%2+CH_0_GAIN, GAIN_4X);  // +/- 10V.
        flag = fcntl(fileno(stdin), F_GETFL);
        fcntl(0, F_SETFL, flag | O_NONBLOCK);
	do {
          usbAin_USBTC_AI(hid, i, 0, &volts);
    	  printf("Channel: %d  %.2f Volts.\n", i, volts);
  	  sleep(1);
	} while (!isalpha(getchar()));
	fcntl(fileno(stdin), F_SETFL, flag);
        break;
      default:
        break;
    }
  }
}
Exemplo n.º 13
0
int main (int argc, char **argv)
{
  __u8 input;
  int temp;
  int ch;
  char serial[9];
  
  HIDInterface*  hid = 0x0;
  hid_return ret;
  int interface;
  __u8 port;
  __u8 pin = 0;
  __u8 bit_value;
  __u16 status;
  int device = 0;  // either USBSSR24 or USBSSR08
  
  // Debug information.  Delete when not needed    
  //  hid_set_debug(HID_DEBUG_ALL);
  //  hid_set_debug_stream(stderr);
  //  hid_set_usb_debug(2);

  ret = hid_init();
  if (ret != HID_RET_SUCCESS) {
    fprintf(stderr, "hid_init failed with return code %d\n", ret);
    return -1;
  }

  if ((interface = PMD_Find_Interface(&hid, 0, USBSSR24_PID)) >= 0) {
    printf("USB SSR24 Device is found! Interface = %d\n", interface);
    device = USBSSR24_PID;
  } else if ((interface = PMD_Find_Interface(&hid, 0, USBSSR08_PID)) >= 0) {
    printf("USB SSR08 Device is found! Interface = %d\n", interface);
    device = USBSSR08_PID;
  } else {
    fprintf(stderr, "USB SSR24 or SSR08 not found.\n");
    exit(-1);
  }

  while(1) {
    printf("\nUSB SSR24 & SSR08 Testing\n");
    printf("----------------\n");
    printf("Hit 'b' to blink LED\n");
    printf("Hit 'd' to test digital I/O \n");
    printf("Hit 'e' to exit\n");
    printf("Hit 'g' to get serial number\n");
    printf("Hit 's' to get status\n");
    printf("Hit 'r' to reset\n");
    printf("Hit 't' to test digital bit I/O\n");
    
    while((ch = getchar()) == '\0' || ch == '\n');
    
    switch(ch) {
    case 'b': /* test to see if led blinks */
      usbBlink_USBSSR(hid);
      break;
    case 'd':
      printf("\nTesting Digital I/O....\n");
      do {
	if (device == USBSSR24_PID) {
	  printf("Enter a port number [0-3]: ");
	  scanf("%hhd", &port);
	  if (port > 3) break;
	} else {
	  printf("Enter a port number [2-3]: ");  // only CL and CH on SSR08
	  scanf("%hhd", &port);
	  printf("port = %d\n", port);
	  if (port != 2 &&  port != 3) continue;
	}
        status = usbGetStatus_USBSSR(hid);
        switch (port) {
          case 0:   /* Port A */
            if (status & 0x1<<0) {
              printf("    Port A direction = input\n");
              input = usbDIn_USBSSR(hid, port);
              printf("    Port A = %#x\n", input);
            } else {
              printf("    Port A direction = output\n");
	      printf("Enter a byte number [0-0xff] : " );
	      scanf("%x", &temp);
              usbDOut_USBSSR(hid, port, (__u8)temp);
            }
            if (status & 0x1<<4) {
              printf("    Port A polarity = normal\n");
            } else {
              printf("    Port A polarity = inverted\n");
            }
            if (status & 0x1<<8) {
              printf("    Port A  = pull up\n");
            } else {
              printf("    Port A  = pull down\n");
            }
            break;
          case 1:   /* Port B */
            if (status & 0x1<<1) {
              printf("    Port B direction = input\n");
              input = usbDIn_USBSSR(hid, port);
              printf("    Port B = %#x\n", input);
            } else {
              printf("    Port B direction = output\n");
	      printf("Enter a byte number [0-0xff] : " );
	      scanf("%x", &temp);
              usbDOut_USBSSR(hid, port, (__u8)temp);
            }
            if (status & 0x1<<5) {
              printf("    Port B polarity = normal\n");
            } else {
              printf("    Port B polarity = inverted\n");
            }
            if (status & 0x1<<9) {
              printf("    Port B  = pull up\n");
            } else {
              printf("    Port B  = pull down\n");
            }
            break;
          case 2:   /* Port C Low */
            if (status & 0x1<<2) {
              printf("    Port C Low direction = input\n");
              input = usbDIn_USBSSR(hid, port);
              printf("    Port C Low = %#x\n", input);
            } else {
              printf("    Port C Low direction = output\n");
	      printf("Enter a byte number [0-0xff] : " );
	      scanf("%x", &temp);
              usbDOut_USBSSR(hid, port, (__u8)temp);
            }
            if (status & 0x1<<6) {
              printf("    Port C Low polarity = normal\n");
            } else {
              printf("    Port C Low polarity = inverted\n");
            }
            if (status & 0x1<<10) {
              printf("    Port C Low  = pull up\n");
            } else {
              printf("    Port C Low  = pull down\n");
            }
            break;
          case 3:   /* Port C High */
            if (status & 0x1<<3) {
              printf("    Port C High direction = input\n");
              input = usbDIn_USBSSR(hid, port);
              printf("    Port C High = %#x\n", input);
            } else {
              printf("    Port C High direction = output\n");
	      printf("Enter a byte number [0-0xff] : " );
	      scanf("%x", &temp);
              usbDOut_USBSSR(hid, port, (__u8)temp);

              usbDOut_USBSSR(hid, port, (__u8)temp);
            }
            if (status & 0x1<<7) {
              printf("    Port C High polarity = normal\n");
            } else {
              printf("    Port C High polarity = inverted\n");
            }
            if (status & 0x1<<11) {
              printf("    Port C High  = pull up\n");
            } else {
              printf("    Port C High  = pull down\n");
            }
            break;
        }
      } while (toContinue());
      break;
    case 'g':
      usbReadCode_USBSSR(hid, 0x200000, 8, (__u8 *) serial);
      serial[8] = '\0';
      printf("Serial Number = %s\n", serial);
      break;
    case 't':
      printf("\nTesting Digital Bit I/O....\n");
      do {
	if (device == USBSSR24_PID) {
	  printf("Enter a port number [0-3]: ");
	  scanf("%hhd", &port);
	  if (port > 3) break;
	} else {
	  printf("Enter a port number [2-3]: ");  // only CL and CH on SSR08
	  scanf("%hhd", &port);
	  printf("port = %d\n", port);
	  if (port != 2 &&  port != 3) continue;
	}
        printf("Select the Pin in port  %d  [0-7] :", port);
        scanf("%hhd", &pin);
        status = usbGetStatus_USBSSR(hid);
        switch (port) {
          case 0:   /* Port A */
            if (status & 0x1<<0) {
              printf("    Port A direction = input\n");
              input = usbDBitIn_USBSSR(hid, port, pin);
              printf("    Port %d  Pin %d = %#x\n", port, pin, input);
            } else {
              printf("    Port A direction = output\n");
              printf("Enter a bit value for output (0 | 1) : ");
	      scanf("%hhd", &bit_value);
              usbDBitOut_USBSSR(hid, port, pin, bit_value);
            }
            if (status & 0x1<<4) {
              printf("    Port A polarity = normal\n");
            } else {
              printf("    Port A polarity = inverted\n");
            }
            if (status & 0x1<<8) {
              printf("    Port A  = pull up\n");
            } else {
              printf("    Port A  = pull down\n");
            }
            break;
          case 1:   /* Port B */
            if (status & 0x1<<1) {
              printf("    Port B direction = input\n");
              input = usbDBitIn_USBSSR(hid, port, pin);
              printf("    Port %d  Pin %d = %#x\n", port, pin, input);
            } else {
              printf("    Port B direction = output\n");
	      printf("Enter a bit value for output (0 | 1) : ");
	      scanf("%hhd", &bit_value);
              usbDBitOut_USBSSR(hid, port, pin, bit_value);
            }
            if (status & 0x1<<5) {
              printf("    Port B polarity = normal\n");
            } else {
              printf("    Port B polarity = inverted\n");
            }
            if (status & 0x1<<9) {
              printf("    Port B  = pull up\n");
            } else {
              printf("    Port B  = pull down\n");
            }
            break;
          case 2:   /* Port C Low */
            if (status & 0x1<<2) {
              printf("    Port C Low direction = input\n");
              input = usbDBitIn_USBSSR(hid, port, pin);
              printf("    Port %d  Pin %d = %#x\n", port, pin, input);
            } else {
              printf("    Port C Low direction = output\n");
              printf("Enter a bit value for output (0 | 1) : ");
	      scanf("%hhd", &bit_value);
              usbDBitOut_USBSSR(hid, port, pin, bit_value);
            }
            if (status & 0x1<<6) {
              printf("    Port C Low polarity = normal\n");
            } else {
              printf("    Port C Low polarity = inverted\n");
            }
            if (status & 0x1<<10) {
              printf("    Port C Low  = pull up\n");
            } else {
              printf("    Port C Low  = pull down\n");
            }
            break;
          case 3:   /* Port C High */
            if (status & 0x1<<3) {
              printf("    Port C High direction = input\n");
              input = usbDBitIn_USBSSR(hid, port, pin);
              printf("    Port %d  Pin %d = %#x\n", port, pin, input);

            } else {
              printf("    Port B direction = output\n");
	      printf("Enter a bit value for output (0 | 1) : ");
	      scanf("%hhd", &bit_value);
              usbDBitOut_USBSSR(hid, port, pin, bit_value);
            }
            if (status & 0x1<<7) {
              printf("    Port C High polarity = normal\n");
            } else {
              printf("    Port C High polarity = inverted\n");
            }
            if (status & 0x1<<11) {
              printf("    Port C High  = pull up\n");
            } else {
              printf("    Port C High  = pull down\n");
            }
            break;
        }
      } while (toContinue());
      break;
    case 's':
      status = usbGetStatus_USBSSR(hid);
      printf("Status = %#x\n", status);
      if (device == USBSSR24_PID) {
	if (status & 0x1<<0) {
	  printf("    Port A direction = input\n");
	} else {
	  printf("    Port A direction = output\n");
	}
	if (status & 0x1<<4) {
	printf("    Port A polarity = normal\n");
	} else {
	  printf("    Port A polarity = inverted\n");
	}
	if (status & 0x1<<8) {
	  printf("    Port A  = pull up\n");
	} else {
	  printf("    Port A  = pull down\n");
	}
	/* Port B */
	if (status & 0x1<<1) {
	  printf("    Port B direction = input\n");
	} else {
	  printf("    Port B direction = output\n");
	}
	if (status & 0x1<<5) {
	  printf("    Port B polarity = normal\n");
	} else {
	  printf("    Port B polarity = inverted\n");
	}
	if (status & 0x1<<9) {
	  printf("    Port B  = pull up\n");
	} else {
	  printf("    Port B  = pull down\n");
	}
      }
      /* Port C Low */
      if (status & 0x1<<2) {
	printf("    Port C Low direction = input\n");
      } else {
	printf("    Port C Low direction = output\n");
	usbDOut_USBSSR(hid, port, (__u8)temp);
      }
      if (status & 0x1<<6) {
	printf("    Port C Low polarity = normal\n");
      } else {
	printf("    Port C Low polarity = inverted\n");
      }
      if (status & 0x1<<10) {
	printf("    Port C Low  = pull up\n");
      } else {
	printf("    Port C Low  = pull down\n");
      }
      /* Port C High */
      if (status & 0x1<<3) {
	printf("    Port C High direction = input\n");
      } else {
	printf("    Port C High direction = output\n");
      }
      if (status & 0x1<<7) {
	printf("    Port C High polarity = normal\n");
      } else {
	printf("    Port C High polarity = inverted\n");
      }
      if (status & 0x1<<11) {
	printf("    Port C High  = pull up\n");
      } else {
	printf("    Port C High  = pull down\n");
      }
      break;
    case 'r':
      usbReset_USBSSR(hid);
      return 0;
      break;
    case 'e':
      ret = hid_close(hid);
      if (ret != HID_RET_SUCCESS) {
	fprintf(stderr, "hid_close failed with return code %d\n", ret);
	return 1;
      }

      hid_delete_HIDInterface(&hid);

      ret = hid_cleanup();
      if (ret != HID_RET_SUCCESS) {
	fprintf(stderr, "hid_cleanup failed with return code %d\n", ret);
	return 1;
      }
      return 0;
    default:
      break;
    }
  }
}
Exemplo n.º 14
0
int main (int argc, char *argv[])
{
	char buffer[64];
	HIDInterface* hid;
	HIDInterfaceMatcher matcher = { CH_USB_VID, CH_USB_PID_FIRMWARE, NULL, NULL, 0 };
	hid_return ret;
	int i;
	int iface_num = 0;
	int length;
	unsigned int timeout = 1000; /* ms */

	hid_set_debug (HID_DEBUG_ALL);
	hid_set_debug_stream (stderr);
	hid_set_usb_debug (0);

	ret = hid_init ();
	if (ret != HID_RET_SUCCESS) {
		fprintf (stderr, "hid_init failed with return code %d\n", ret);
		return 1;
	}

	hid = hid_new_HIDInterface ();
	if (hid == 0) {
		fprintf (stderr, "hid_new_HIDInterface () failed\n");
		return 1;
	}

	ret = hid_force_open (hid, iface_num, &matcher, 3);
	if (ret != HID_RET_SUCCESS) {
		fprintf (stderr, "hid_force_open failed with return code %d\n", ret);
		return 1;
	}

	ret = hid_write_identification (stdout, hid);
	if (ret != HID_RET_SUCCESS) {
		fprintf (stderr, "hid_write_identification failed with return code %d\n", ret);
		return 1;
	}

	ret = hid_dump_tree (stdout, hid);
	if (ret != HID_RET_SUCCESS) {
		fprintf (stderr, "hid_dump_tree failed with return code %d\n", ret);
		return 1;
	}

	/* turn all the LEDs on */
	memset (buffer, 0x00, sizeof (buffer));
	buffer[0] = 0x0e;
	buffer[1] = 0x03;
	ret = hid_interrupt_write (hid, 0x01,  buffer, sizeof (buffer), timeout);
	if (ret != HID_RET_SUCCESS) {
		fprintf (stderr, "failed calling hid_interrupt_write\n");
		return 1;
	}

	/* read the return message */
	length = 2;
	ret = hid_interrupt_read (hid, 0x01, buffer, length, timeout);
	if (ret != HID_RET_SUCCESS) {
		printf ("hid_interrupt_read failed\n");
		return 1;
	}
	printf ("Data received from USB device: ");
	for (i=0; i < length; i++) {
		printf ("0x%02x, ", buffer[i]);
	}
	printf ("\n");

	ret = hid_close (hid);
	if (ret != HID_RET_SUCCESS) {
		fprintf (stderr, "hid_close failed with return code %d\n", ret);
		return 1;
	}

	hid_delete_HIDInterface (&hid);

	ret = hid_cleanup ();
	if (ret != HID_RET_SUCCESS) {
		fprintf (stderr, "hid_cleanup failed with return code %d\n", ret);
		return 1;
	}

	return 0;
}
Exemplo n.º 15
0
int main(int argc, char** argp)
{
  HIDInterface* hid;
  hid_return ret;
  char* const HEXPREFIX = "0x";
  unsigned char const HEXPREFIXLEN = 2;
  unsigned char const HEXSTRINGLEN = 6;
  unsigned char const HEXNUMLEN = 4;
  char vendor_id_s[HEXSTRINGLEN + 1], product_id_s[HEXSTRINGLEN + 1];
  unsigned short vendor_id = 0, product_id = 0;

  if (argc != 2 || strlen(argp[1]) != strlen("dead:beef")) {
    fprintf(stderr, "Usage: %s vendor_id:product_id\n", argp[0]);
    fprintf(stderr, "   Eg: %s 06c2:0038\n", argp[0]);
    return 255;
  }

  memcpy(vendor_id_s, HEXPREFIX, HEXPREFIXLEN);
  strncpy(vendor_id_s + HEXPREFIXLEN, argp[1], HEXNUMLEN);
  vendor_id = 0xffff & strtol(vendor_id_s, NULL, 16);

  memcpy(product_id_s, HEXPREFIX, HEXPREFIXLEN);
  strncpy(product_id_s + HEXPREFIXLEN, argp[1] + HEXNUMLEN + 1, HEXNUMLEN);
  product_id = 0xffff & strtol(product_id_s, NULL, 16);

  fprintf(stderr, "Trying to detach HID with IDs %04x:%04x... ",
      vendor_id, product_id);
    
  HIDInterfaceMatcher matcher = { vendor_id, product_id, NULL, NULL, 0 };

  ret = hid_init();
  if (ret != HID_RET_SUCCESS) {
    fprintf(stderr, "hid_init failed with return code %d.\n", ret);
    return 1;
  }

  hid = hid_new_HIDInterface();
  if (hid == 0) {
    fprintf(stderr, "hid_new_HIDInterface() failed, out of memory?\n");
    return 1;
  }

  ret = hid_force_open(hid, 0, &matcher, 3);
  if (ret != HID_RET_SUCCESS) {
    fprintf(stderr, "hid_force_open failed with return code %d.\n", ret);
    return 1;
  }

  ret = hid_close(hid);
  if (ret != HID_RET_SUCCESS) {
    fprintf(stderr, "hid_close failed with return code %d.\n", ret);
    return 1;
  }

  hid_delete_HIDInterface(&hid);

  ret = hid_cleanup();
  if (ret != HID_RET_SUCCESS) {
    fprintf(stderr, "hid_cleanup failed with return code %d.\n", ret);
    return 1;
  }

  fprintf(stderr, "done.\n");
  
  return 0;
}