usb_comm.cpp
Go to the documentation of this file.
2 
3 #define MILVUS_EXCEPT(except, msg, ...) \
4 { \
5  char buf[1000]; \
6  snprintf(buf, 1000, msg " (in milvus::UsbComm::%s)" , ##__VA_ARGS__, __FUNCTION__); \
7  throw except(buf); \
8 }
9 
11 {
12  devh = NULL;
13 }
14 
16 {
17 }
18 
19 int milvus::UsbComm::open_device(uint16_t vendor_id, uint16_t product_id, int ep_in_addr, int ep_out_addr)
20 {
21  ep_in_addr_ = ep_in_addr;
22  ep_out_addr_ = ep_out_addr;
23 
24  int rc;
25 
26  rc = libusb_init(NULL);
27  if (rc < 0) {
28  fprintf(stderr, "Error initializing libusb: %s\n", libusb_error_name(rc));
29  exit(1);
30  }
31 
32  libusb_set_debug(NULL, 3);
33 
34  devh = libusb_open_device_with_vid_pid(NULL, vendor_id, product_id);
35  if (!devh) {
36  fprintf(stderr, "Error finding USB device\n");
37  close_device();
38  }
39 
40  for (int if_num = 0; if_num < 2; if_num++) {
41  if (libusb_kernel_driver_active(devh, if_num)) {
42  libusb_detach_kernel_driver(devh, if_num);
43  }
44  rc = libusb_claim_interface(devh, if_num);
45  if (rc < 0) {
46  fprintf(stderr, "Error claiming interface: %s\n",
47  libusb_error_name(rc));
48  close_device();
49  }
50  }
51 
52  rc = libusb_control_transfer(devh, 0x21, 0x22, ACM_CTRL_DTR | ACM_CTRL_RTS,
53  0, NULL, 0, 0);
54  if (rc < 0) {
55  fprintf(stderr, "Error during control transfer: %s\n",
56  libusb_error_name(rc));
57  }
58 
59  /* - set line encoding: here 9600 8N1
60  * 9600 = 0x2580 ~> 0x80, 0x25 in little endian
61  */
62  unsigned char encoding[] = { 0x80, 0x25, 0x00, 0x00, 0x00, 0x00, 0x08 };
63  rc = libusb_control_transfer(devh, 0x21, 0x20, 0, 0, encoding,
64  sizeof(encoding), 0);
65  if (rc < 0) {
66  fprintf(stderr, "Error during control transfer: %s\n",
67  libusb_error_name(rc));
68  }
69 
70  return rc;
71 }
72 
74 {
75  libusb_release_interface(devh, 0);
76 
77  if (devh)
78  libusb_close(devh);
79 
80  libusb_exit(NULL);
81  return 0;
82 }
83 
84 int milvus::UsbComm::read_bytes(unsigned char *buf, int size)
85 {
86  struct timespec tv1;
87  int actual_length;
88  int rc = libusb_bulk_transfer(devh, ep_in_addr_, buf, size, &actual_length,
89  1);
90  if (rc == LIBUSB_ERROR_TIMEOUT) {
91  //gettimeofday(&tv1, NULL);
92  clock_gettime(CLOCK_MONOTONIC, &tv1);
93  printf("%ld.%06ld - timeout (%d)\n", tv1.tv_sec, tv1.tv_nsec, actual_length);
94  //print_array(buf, actual_length);
95  } else if (rc < 0) {
96  fprintf(stderr, "Error while waiting for char\n");
97  return -1;
98  }
99 
100  return actual_length;
101 }
102 
103 
104 int milvus::UsbComm::write_bytes(unsigned char *buf, int size)
105 {
106  struct timespec tv1;
107 
108  int actual_length;
109  if (libusb_bulk_transfer(devh, ep_out_addr_, buf, size,
110  &actual_length, 0) < 0) {
111  fprintf(stderr, "Error while sending char\n");
112  }
113  clock_gettime(CLOCK_MONOTONIC, &tv1);
114  printf("%ld.%06ld - write sent \n", tv1.tv_sec, tv1.tv_nsec);
115 }
116 
117 void milvus::UsbComm::print_array(uint8_t *buf, int length)
118 {
119  printf("Array: ");
120  int i = 0;
121  for (i = 0; i < length; i++)
122  {
123  printf("%02x ",buf[i] );
124  }
125  printf("\n");
126 };
127 
int write_bytes(unsigned char *, int)
Definition: usb_comm.cpp:104
int open_device(uint16_t vendor_id, uint16_t product_id, int ep_in_addr, int ep_out_addr)
Definition: usb_comm.cpp:19
void print_array(uint8_t *buf, int length)
Device handle.
Definition: usb_comm.cpp:117
~UsbComm()
Destructor.
Definition: usb_comm.cpp:15
int close_device()
Definition: usb_comm.cpp:73
struct libusb_device_handle * devh
Definition: usb_comm.h:30
#define ACM_CTRL_RTS
Definition: usb_comm.h:9
#define ACM_CTRL_DTR
Definition: usb_comm.h:8
int ep_out_addr_
Definition: usb_comm.h:32
UsbComm()
Constructor.
Definition: usb_comm.cpp:10
int read_bytes(unsigned char *, int)
Definition: usb_comm.cpp:84


mrp2_hardware
Author(s): Akif Hacinecipoglu
autogenerated on Mon Feb 28 2022 22:53:03