Skip to content

Instantly share code, notes, and snippets.

@zardam
Created April 7, 2019 23:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zardam/52e93baff669eaba0950533ef6923025 to your computer and use it in GitHub Desktop.
Save zardam/52e93baff669eaba0950533ef6923025 to your computer and use it in GitHub Desktop.
Small libusb test program to send querie to a TI-Python module
// gcc usb.c `pkg-config --libs --cflags libusb-1.0
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <libusb.h>
static struct libusb_device_handle *devh = NULL;
static int open_device(void) {
devh = libusb_open_device_with_vid_pid(NULL, 0x0451, 0xE020);
return devh ? 0 : -EIO;
}
void DumpHex(const void* data, size_t size) {
char ascii[17];
size_t i, j;
ascii[16] = '\0';
for (i = 0; i < size; ++i) {
printf("%02X ", ((unsigned char*)data)[i]);
if (((unsigned char*)data)[i] >= ' ' && ((unsigned char*)data)[i] <= '~') {
ascii[i % 16] = ((unsigned char*)data)[i];
} else {
ascii[i % 16] = '.';
}
if ((i+1) % 8 == 0 || i+1 == size) {
printf(" ");
if ((i+1) % 16 == 0) {
printf("| %s \n", ascii);
} else if (i+1 == size) {
ascii[(i+1) % 16] = '\0';
if ((i+1) % 16 <= 8) {
printf(" ");
}
for (j = (i+1) % 16; j < 16; ++j) {
printf(" ");
}
printf("| %s \n", ascii);
}
}
}
}
int main(int argc, char* argv[]) {
int r;
r = libusb_init(NULL);
if (r < 0) {
fprintf(stderr, "failed to initialise libusb\n");
exit(1);
}
r = open_device();
if (r < 0) {
fprintf(stderr, "Could not find/open device\n");
goto out;
}
if(libusb_kernel_driver_active(devh, 2)) {
r = libusb_detach_kernel_driver(devh, 2);
if (r < 0) {
fprintf(stderr, "libusb_detach_kernel_driver error %d\n", r);
goto out;
}
printf("interface detached\n");
}
r = libusb_claim_interface(devh, 2);
if (r < 0) {
fprintf(stderr, "usb_claim_interface error %d\n", r);
goto out;
}
printf("claimed interface\n");
// 80 03 06 12 lun 17 18 19 20
unsigned char req[31] = {0x55,0x53,0x42,0x43, 0x19,0x95,0x48,0x00 ,0x65,0x00,0x00,0x00, 0x80 ,0x03 ,0x06, 0x12,0x00,0x76,0x04,0x65,0x0C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
//unsigned char req[31] = {0x55,0x53,0x42,0x43,0xC1,0x4A,0x9F,0x00,0x65,0x00,0x00,0x00,0x80,0x03,0x06,0x12,0x00,0x2D,0x02,0x65,0x3C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
// req[17] = 0x09;
// req[18] = 0x04;
// req[19] = 0x65; // Unused ?
// req[20] = 0x0C;
//printf("%x\n", req[20] + (req[17] << 8) + (req[18] << 14) + 0x2000);
//unsigned char req[31] = {0x55 ,0x53 ,0x42 ,0x43 ,0xA1 ,0x43 ,0x1C ,0x00 ,0x24 ,0x00 ,0x00 ,0x00 ,0x80 ,0x02 ,0x06 ,0x12 ,0x00 ,0x00 ,0x00 ,0x24 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00};
r = libusb_bulk_transfer(devh, 0x04, req, 31, NULL, 1000);
if (r < 0) {
fprintf(stderr, "send error %d\n", r);
goto out;
}
printf("send ok\n");
int nb_read;
unsigned char read[1024];
r = libusb_bulk_transfer(devh, 0x83, read, 116, &nb_read, 1000);
if (r < 0) {
fprintf(stderr, "read error %d\n", r);
goto out;
}
printf("read ok: %d\n", nb_read);
DumpHex(read, nb_read);
r = libusb_bulk_transfer(devh, 0x83, read, 116, &nb_read, 1000);
if (r < 0) {
fprintf(stderr, "read error %d\n", r);
goto out;
}
printf("read ok: %d\n", nb_read);
DumpHex(read, nb_read);
out:
libusb_close(devh);
libusb_exit(NULL);
return r >= 0 ? r : -r;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment