usb_comm.cpp
Go to the documentation of this file.
00001 #include "mrp2_hardware/usb_comm.h"
00002 
00003 #define MILVUS_EXCEPT(except, msg, ...) \
00004 { \
00005     char buf[1000]; \
00006     snprintf(buf, 1000, msg " (in milvus::UsbComm::%s)" , ##__VA_ARGS__, __FUNCTION__); \
00007     throw except(buf); \
00008 }
00009 
00010 milvus::UsbComm::UsbComm()
00011 {
00012   devh = NULL;
00013 }
00014 
00015 milvus::UsbComm::~UsbComm()
00016 {
00017 }
00018 
00019 int milvus::UsbComm::open_device(uint16_t vendor_id, uint16_t product_id, int ep_in_addr, int ep_out_addr)
00020 {
00021   ep_in_addr_ = ep_in_addr;
00022   ep_out_addr_ = ep_out_addr;
00023 
00024   int rc;
00025 
00026   rc = libusb_init(NULL);
00027   if (rc < 0) {
00028       fprintf(stderr, "Error initializing libusb: %s\n", libusb_error_name(rc));
00029       exit(1);
00030   }
00031 
00032   libusb_set_debug(NULL, 3);
00033 
00034   devh = libusb_open_device_with_vid_pid(NULL, vendor_id, product_id);
00035   if (!devh) {
00036       fprintf(stderr, "Error finding USB device\n");
00037       close_device();
00038   }
00039 
00040   for (int if_num = 0; if_num < 2; if_num++) {
00041       if (libusb_kernel_driver_active(devh, if_num)) {
00042           libusb_detach_kernel_driver(devh, if_num);
00043       }
00044       rc = libusb_claim_interface(devh, if_num);
00045       if (rc < 0) {
00046           fprintf(stderr, "Error claiming interface: %s\n",
00047                   libusb_error_name(rc));
00048           close_device();
00049       }
00050   }
00051 
00052   rc = libusb_control_transfer(devh, 0x21, 0x22, ACM_CTRL_DTR | ACM_CTRL_RTS,
00053                                 0, NULL, 0, 0);
00054   if (rc < 0) {
00055       fprintf(stderr, "Error during control transfer: %s\n",
00056               libusb_error_name(rc));
00057   }
00058 
00059   /* - set line encoding: here 9600 8N1
00060    * 9600 = 0x2580 ~> 0x80, 0x25 in little endian
00061    */
00062   unsigned char encoding[] = { 0x80, 0x25, 0x00, 0x00, 0x00, 0x00, 0x08 };
00063   rc = libusb_control_transfer(devh, 0x21, 0x20, 0, 0, encoding,
00064                               sizeof(encoding), 0);
00065   if (rc < 0) {
00066       fprintf(stderr, "Error during control transfer: %s\n",
00067               libusb_error_name(rc));
00068   }
00069 
00070   return rc;
00071 }
00072 
00073 int milvus::UsbComm::close_device() 
00074 {
00075   libusb_release_interface(devh, 0);
00076 
00077   if (devh)
00078     libusb_close(devh);
00079 
00080   libusb_exit(NULL);
00081   return 0;
00082 }
00083 
00084 int milvus::UsbComm::read_bytes(unsigned char *buf, int size)
00085 {
00086   struct timespec  tv1;
00087   int actual_length;
00088   int rc = libusb_bulk_transfer(devh, ep_in_addr_, buf, size, &actual_length,
00089                                 1);
00090   if (rc == LIBUSB_ERROR_TIMEOUT) {
00091       //gettimeofday(&tv1, NULL);
00092       clock_gettime(CLOCK_MONOTONIC, &tv1);
00093       printf("%ld.%06ld - timeout (%d)\n", tv1.tv_sec, tv1.tv_nsec, actual_length);
00094       //print_array(buf, actual_length);
00095   } else if (rc < 0) {
00096       fprintf(stderr, "Error while waiting for char\n");
00097       return -1;
00098   }
00099 
00100   return actual_length;
00101 }
00102 
00103 
00104 int milvus::UsbComm::write_bytes(unsigned char *buf, int size)
00105 {
00106   struct timespec  tv1;
00107 
00108   int actual_length;
00109   if (libusb_bulk_transfer(devh, ep_out_addr_, buf, size,
00110                            &actual_length, 0) < 0) {
00111       fprintf(stderr, "Error while sending char\n");
00112   }
00113   clock_gettime(CLOCK_MONOTONIC, &tv1);
00114   printf("%ld.%06ld - write sent \n", tv1.tv_sec, tv1.tv_nsec);
00115 }
00116 
00117 void milvus::UsbComm::print_array(uint8_t *buf, int length) 
00118 {
00119   printf("Array: ");
00120   int i = 0;
00121   for (i = 0; i < length; i++)
00122   {
00123       printf("%02x ",buf[i] );
00124   }
00125   printf("\n");
00126 };
00127 


mrp2_hardware
Author(s): Akif Hacinecipoglu
autogenerated on Thu Jun 6 2019 21:43:37