Hi,
I am using i.Mx RT 1024 EVK kit.
I want to configure micro controller as Device and send some data to Host PC running Linux over USB protocol.
In SDK so many examples are available. Which example is best to use please suggest.
I am using following code to send data from Host PC:
#include <stdio.h>
#include <stdlib.h>
#include <libusb-1.0/libusb.h>
#define VENDOR_ID 0x1fc9 // Replace with your actual Vendor ID
#define PRODUCT_ID 0x0094 // Replace with your actual Product ID
#define ENDPOINT_IN 0x02 // Endpoint address for IN endpoint (endpoint 1 IN)
#define ENDPOINT_OUT 0x03 // Endpoint address for OUT endpoint (endpoint 1 OUT)
#define MAX_BUF_SIZE 1 // Maximum buffer size for data
#define INTERFACE_0 0x0
#define INTERFACE_1 0x1
int main() {
libusb_context *ctx = NULL;
libusb_device_handle *dev_handle = NULL;
int r;
// Initialize libusb
r = libusb_init(&ctx);
if (r < 0) {
fprintf(stderr, "Failed to initialize libusb\n");
return 1;
}
// Open device with specified VID and PID
dev_handle = libusb_open_device_with_vid_pid(ctx, VENDOR_ID, PRODUCT_ID);
if (dev_handle == NULL) {
fprintf(stderr, "Failed to open USB device\n");
libusb_exit(ctx);
return 1;
}
// Claim interface 0 of the USB device
r = libusb_claim_interface(dev_handle, INTERFACE_0);
if (r < 0) {
fprintf(stderr, "Failed to claim interface\n");
libusb_close(dev_handle);
libusb_exit(ctx);
return 1;
}
// Perform bulk transfer from endpoint IN (device to host)
unsigned char in_buf[MAX_BUF_SIZE];
int transferred;
while(1)
{
/*
r = libusb_bulk_transfer(dev_handle, ENDPOINT_IN, in_buf, sizeof(in_buf), &transferred, 0);
if (r == 0 && transferred > 0) {
printf("Received data (%d bytes): %s\n", transferred, in_buf);
} else {
fprintf(stderr, "Error receiving data from endpoint IN: %s\n", libusb_error_name(r));
}
*/
// Perform bulk transfer to endpoint OUT (host to device)
unsigned char out_buf[MAX_BUF_SIZE] = "H";
int sent;
r = libusb_bulk_transfer(dev_handle, ENDPOINT_OUT, out_buf, sizeof(out_buf), &sent, 0);
if (r == 0 && sent == sizeof(out_buf)) {
printf("Sent data to device (%d bytes)\n", sent);
} else {
fprintf(stderr, "Error sending data to endpoint OUT: %s and r=%d\n", libusb_error_name(r),r);
}
sleep(2);
}
// Release interface and close device
libusb_release_interface(dev_handle, 0);
libusb_close(dev_handle);
// Cleanup libusb context
libusb_exit(ctx);
return 0;
}
I am getting following error:
Errors from my application code : Error sending data to endpoint OUT: LIBUSB_ERROR_IO and r=-1
from dmesg:
[97608.385202] usb 3-4: usbfs: process 134644 (pp) did not claim interface 1 before use
Can anybody can guide me to send and receive data over USB protocol.
Thank you
Hi
My apologies for the delayed response.
Selecting the SDK example depends on the desired device class. For example the CDC. This application note could also come handy to create your USB application with the i.MX RT AN14169 How to Generate a User-Defined Class USB Device based on i.MX RT Chips
Are you still getting issues with this? and if you do, do not hesitate to let us know with more details regarding your application.
Thank you,
Diego