2013-08-29

AVR USB HOWTO

How to compile a C program with libusbx-1.0 support

gcc cmdClient.c -ocmdClient -O -Wall -I/usr/local/include/libusb-1.0 -L/usr/local/lib -lusb-1.0 -ludev -lrt
[root@localhost cmdClient]# ./cmdClient  
./cmdClient: symbol lookup error: ./cmdClient: undefined symbol: libusb_get_port_numbers
########## The first thing you want to check is whether your program is actually using the libusbx library or libusb. If libusb is installed as a system library and hasn't been replaced, there's a good chance your application is using libusb rather than libusbx, and if it does, you will get errors for the function calls that are only present in libusbx. You may want to try to call libusb_get_version() and print the data (see the end of examples/xusb.c for how to do that), to confirm that you are actually using libusbx 1.0.15 and not libusb. ############

gcc cmdClient.c -ocmdClient -O -Wall -I./libusb -L/usr/local/lib -lusb-1.0 ./cmdClient
./cmdClient: error while loading shared libraries: libusb-1.0.so.0: cannot open shared object file: No such file or directory find /usr -name "*libusb-1.0.so.0*" /usr/local/lib/libusb-1.0.so.0 /usr/local/lib/libusb-1.0.so.0.1.0

Solution 1:
gcc cmdClient.c -ocmdClient -O -Wall -I/usr/local/include/libusb-1.0 -L/usr/local/lib -lusb-1.0 -Wl,-rpath -Wl,/usr/local/lib

[root@localhost cmdClient]# ./cmdClient
Using libusbx v1.0.16.10774

Solution 2:
strace ./cmdClient

open("/usr/lib/libusb-1.0.so.0", O_RDONLY) = -1 ENOENT (No such file or directory)

ln -s /usr/local/lib/libusb-1.0.so.0 /usr/lib/libusb-1.0.so.0

gcc cmdClient.c -ocmdClient -O -Wall -I/usr/local/include/libusb-1.0 -L/usr/local/lib -lusb-1.0
or
gcc cmdClient.c -ocmdClient -O -Wall -I./libusb -L./libusb -lusb-1.0

[root@localhost cmdClient]# ./cmdClient
Using libusbx v1.0.16.10774


###########################
linux tools:
file  
locate
ldd /usr/local/lib/libusb-1.0.so.0

http://sourceforge.net/projects/libusbx/
http://libusbx.sourceforge.net/api-1.0/modules.html
##############################
Final result:

#include stdio.h
#include stdint.h
#include stdlib.h
#include string.h
#include stdarg.h
#include unistd.h

#include "libusb.h"

uint16_t VID, PID;


static int test_device_on_off(int i_usb_led_on)
{   
    libusb_device_handle *handle;

    int i, r;

    printf("Opening device %04X:%04X...\n", VID, PID);
    handle = libusb_open_device_with_vid_pid(NULL, VID, PID);

    if (handle == NULL) {
        printf("  Failed.\n");
        return -1;
    }

    unsigned char data[256];
    r = libusb_control_transfer(handle,LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, i_usb_led_on, 0, 0, (unsigned char *)data,sizeof(data), 5000);
    if (r < 0) {
        printf("F0 error %d\n", r);

    }
    if ((unsigned int) r < sizeof(data)) {
        printf( "short read (%d)\n", r);

    }

    printf("F0 data:");
    for (i = 0; i < sizeof(data); i++)
        printf("%02x ", data[i]);
    printf("\n");

    printf("Closing device...\n");
    libusb_close(handle);
}

int main(void)
{
    const struct libusb_version* version;
    int r;

    // Default to generic, expecting VID:PID
    VID = 0x16c0;
    PID = 0x05dc;

    version = libusb_get_version();
    printf("Using libusbx v%d.%d.%d.%d\n\n", version->major, version->minor, version->micro, version->nano);
    r = libusb_init(NULL);
    if (r < 0)
        return r;

    libusb_set_debug(NULL, 0?LIBUSB_LOG_LEVEL_DEBUG:LIBUSB_LOG_LEVEL_INFO);

    usleep(500000);
    test_device_on_off(0);
    usleep(500000);
    test_device_on_off(1);

    libusb_exit(NULL);
    return 0;
}


http://we.easyelectronics.ru/electro-and-pc/usb-dlya-avr-chast-1-vvodnaya.html
http://codeandlife.com/2012/01/22/avr-attiny-usb-tutorial-part-1/

##########################################################

avrdude -P /dev/ttyACM0 -p m8 -c avr910 -U flash:r:flash.hex:i

http://www.rogerclark.net/updating-firmware-on-usbasp-bought-from-ebay/
http://habrahabr.ru/post/208470/ Нестандартное использование USBasp
http://rootadmin.livejournal.com/10824.html Программатор AVR910 под Linux

http://www.gaw.ru/html.cgi/txt/app/micros/avr/AVR309.htm

No comments: