From 022e60309b22978f432140efddd51886c0def791 Mon Sep 17 00:00:00 2001 From: pifou Date: Tue, 11 Dec 2018 14:19:58 +0000 Subject: [PATCH] Test de la com serie arduino --- Makefile | 37 +++++++++++++++++++++++++++++++++++++ makefiles/avr | 37 +++++++++++++++++++++++++++++++++++++ test_arduino_serial.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ test_list_devices.c | 34 ---------------------------------- test_list_endpoints.c | 89 ----------------------------------------------------------------------------------------- tests/test_arduino_serial_out.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/test_list_devices.c | 34 ++++++++++++++++++++++++++++++++++ tests/test_list_endpoints.c | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 323 insertions(+), 123 deletions(-) create mode 100755 Makefile create mode 100755 makefiles/avr create mode 100755 test_arduino_serial.c delete mode 100644 test_list_devices.c delete mode 100644 test_list_endpoints.c create mode 100755 tests/test_arduino_serial_out.c create mode 100644 tests/test_list_devices.c create mode 100644 tests/test_list_endpoints.c diff --git a/Makefile b/Makefile new file mode 100755 index 0000000..1230694 --- /dev/null +++ b/Makefile @@ -0,0 +1,37 @@ +export CC = avr-gcc + +export MCU = atmega328p +export TARGET_ARCH = -mmcu=$(MCU) + +export CFLAGS = -Wall -I. -DF_CPU=16000000 -Os #-g +export LDFLAGS = -g $(TARGET_ARCH) -lm -Wl,--gc-sections # -Os + +TARGET = usb +TERM = /dev/ttyACM0 +CPPFLAGS = -mmcu=$(MCU) +PGMERISP = -c stk500v1 -b 115200 -P $(TERM) +ARVDUDECONF= -C /usr/local/arduino/arduino-0022/hardware/tools/avrdude.conf +export DUDE = /usr/bin/avrdude -F -v -p $(MCU) $(AVRDUDECONF) + +C_SRC = $(wildcard *.c) +OBJS = $(C_SRC:.c=.o) + +all: $(TARGET).hex + +clean: + rm -f *.o *.hex *.elf + +%.o:%.c + $(CC) -c $(CPPFLAGS) $(CFLAGS) $< -o $@ + +$(TARGET).elf: $(OBJS) + $(CC) $(LDFLAGS) -o $@ $(OBJS) + +$(TARGET).hex: $(TARGET).elf + avr-objcopy -j .text -j .data -O ihex $(TARGET).elf $(TARGET).hex + avr-objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 -O ihex $(TARGET).elf eeprom.hex + +upload: $(TARGET).hex + stty -F $(TERM) hupcl # reset + $(DUDE) $(PGMERISP) -U flash:w:$(TARGET).hex + diff --git a/makefiles/avr b/makefiles/avr new file mode 100755 index 0000000..1230694 --- /dev/null +++ b/makefiles/avr @@ -0,0 +1,37 @@ +export CC = avr-gcc + +export MCU = atmega328p +export TARGET_ARCH = -mmcu=$(MCU) + +export CFLAGS = -Wall -I. -DF_CPU=16000000 -Os #-g +export LDFLAGS = -g $(TARGET_ARCH) -lm -Wl,--gc-sections # -Os + +TARGET = usb +TERM = /dev/ttyACM0 +CPPFLAGS = -mmcu=$(MCU) +PGMERISP = -c stk500v1 -b 115200 -P $(TERM) +ARVDUDECONF= -C /usr/local/arduino/arduino-0022/hardware/tools/avrdude.conf +export DUDE = /usr/bin/avrdude -F -v -p $(MCU) $(AVRDUDECONF) + +C_SRC = $(wildcard *.c) +OBJS = $(C_SRC:.c=.o) + +all: $(TARGET).hex + +clean: + rm -f *.o *.hex *.elf + +%.o:%.c + $(CC) -c $(CPPFLAGS) $(CFLAGS) $< -o $@ + +$(TARGET).elf: $(OBJS) + $(CC) $(LDFLAGS) -o $@ $(OBJS) + +$(TARGET).hex: $(TARGET).elf + avr-objcopy -j .text -j .data -O ihex $(TARGET).elf $(TARGET).hex + avr-objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 -O ihex $(TARGET).elf eeprom.hex + +upload: $(TARGET).hex + stty -F $(TERM) hupcl # reset + $(DUDE) $(PGMERISP) -U flash:w:$(TARGET).hex + diff --git a/test_arduino_serial.c b/test_arduino_serial.c new file mode 100755 index 0000000..d65c190 --- /dev/null +++ b/test_arduino_serial.c @@ -0,0 +1,63 @@ +#include // for the input/output registers +#include // for _delay_ms + +// For the serial port +#define CPU_FREQ 16000000L // Assume a CPU frequency of 16Mhz + +//compile -> makefile + +void init_serial(int speed){ + /* Set baud rate */ + UBRR0 = CPU_FREQ/(((unsigned long int)speed)<<4)-1; + + /* Enable transmitter & receiver */ + UCSR0B = (1< -#include -#include - -// gcc file.c -l usb-1.0 - -int main(){ - // init - libusb_context *context; - int status = libusb_init(&context); - if(status != 0){perror("libusb_init"); exit(-1);} - - // devices list - libusb_device **devices; - ssize_t devices_count = libusb_get_device_list(context, &devices); - if(devices_count < 0){perror("libusb_get_device_list");exit(-1);} - for(int i=0; i -#include -#include - -#define ID_VENDOR 0x0951 -#define ID_PRODUCT 0x01666 - -// gcc file.c -l usb-1.0 - -int main(){ - // init - libusb_context *context; - int status = libusb_init(&context); - if(status != 0){perror("libusb_init"); exit(-1);} - - // devices list - libusb_device **devices; - ssize_t devices_count = libusb_get_device_list(context, &devices); - if(devices_count < 0){perror("libusb_get_device_list"); exit(-1);} - for(int i=0; ibConfigurationValue; - - // detach kernel - for(int j=0; jbNumInterfaces; j++){ - int interface = config_desc->interface[j].altsetting[0].bInterfaceNumber; - if(libusb_kernel_driver_active(handle, interface)){ - status = libusb_detach_kernel_driver(handle, interface); - if(status != 0){ perror("libusb_detach_kernel_driver"); exit(-1); } - } - } - - // use config - status = libusb_set_configuration(handle, configuration); - if(status != 0){ perror("libusb_set_configuration"); exit(-1); } - - // claim interfaces - for(int j=0; jbNumInterfaces; j++){ - libusb_interface_descriptor *interface_desc = config_desc->interface[j].altsetting[0]; - - int interface = interface_desc->bInterfaceNumber; - status = libusb_claim_interface(handle, interface); - if(status != 0){ perror("libusb_claim_interface"); exit(-1); } - - // find endpoints - for(int k=0; kbNumEndpoints; k++){ - libusb_endpoint_descriptor *endpoint_desc = interface_desc->endpoints[k]; - - // list endpoints bEndpointAddress and bmAttributes - printf("Endpoint inter. %d, num. %d\n", j, k); - printf("bEndpointAddress = 0x%x\n", endpoint_desc->bEndpointAddress); - printf("bmAttributes = 0x%x\n", endpoint_desc->bmAttributes); - } - - // release interfaces - status = libusb_release_interface(handle, interface); - if(status != 0){ perror("libusb_release_interface"); exit(-1); } - } - - // free config - libusb_free_config_descriptor(config_desc); - - // close connection - libusb_close(handle); - } - libusb_free_device_list(devices, 1); - - libusb_exit(context); -} diff --git a/tests/test_arduino_serial_out.c b/tests/test_arduino_serial_out.c new file mode 100755 index 0000000..d65c190 --- /dev/null +++ b/tests/test_arduino_serial_out.c @@ -0,0 +1,63 @@ +#include // for the input/output registers +#include // for _delay_ms + +// For the serial port +#define CPU_FREQ 16000000L // Assume a CPU frequency of 16Mhz + +//compile -> makefile + +void init_serial(int speed){ + /* Set baud rate */ + UBRR0 = CPU_FREQ/(((unsigned long int)speed)<<4)-1; + + /* Enable transmitter & receiver */ + UCSR0B = (1< +#include +#include + +// gcc file.c -l usb-1.0 + +int main(){ + // init + libusb_context *context; + int status = libusb_init(&context); + if(status != 0){perror("libusb_init"); exit(-1);} + + // devices list + libusb_device **devices; + ssize_t devices_count = libusb_get_device_list(context, &devices); + if(devices_count < 0){perror("libusb_get_device_list");exit(-1);} + for(int i=0; i +#include +#include + +#define ID_VENDOR 0x0951 +#define ID_PRODUCT 0x01666 + +// gcc file.c -l usb-1.0 + +int main(){ + // init + libusb_context *context; + int status = libusb_init(&context); + if(status != 0){perror("libusb_init"); exit(-1);} + + // devices list + libusb_device **devices; + ssize_t devices_count = libusb_get_device_list(context, &devices); + if(devices_count < 0){perror("libusb_get_device_list"); exit(-1);} + for(int i=0; ibConfigurationValue; + + // detach kernel + for(int j=0; jbNumInterfaces; j++){ + int interface = config_desc->interface[j].altsetting[0].bInterfaceNumber; + if(libusb_kernel_driver_active(handle, interface)){ + status = libusb_detach_kernel_driver(handle, interface); + if(status != 0){ perror("libusb_detach_kernel_driver"); exit(-1); } + } + } + + // use config + status = libusb_set_configuration(handle, configuration); + if(status != 0){ perror("libusb_set_configuration"); exit(-1); } + + // claim interfaces + for(int j=0; jbNumInterfaces; j++){ + libusb_interface_descriptor *interface_desc = config_desc->interface[j].altsetting[0]; + + int interface = interface_desc->bInterfaceNumber; + status = libusb_claim_interface(handle, interface); + if(status != 0){ perror("libusb_claim_interface"); exit(-1); } + + // find endpoints + for(int k=0; kbNumEndpoints; k++){ + libusb_endpoint_descriptor *endpoint_desc = interface_desc->endpoints[k]; + + // list endpoints bEndpointAddress and bmAttributes + printf("Endpoint inter. %d, num. %d\n", j, k); + printf("bEndpointAddress = 0x%x\n", endpoint_desc->bEndpointAddress); + printf("bmAttributes = 0x%x\n", endpoint_desc->bmAttributes); + } + + // release interfaces + status = libusb_release_interface(handle, interface); + if(status != 0){ perror("libusb_release_interface"); exit(-1); } + } + + // free config + libusb_free_config_descriptor(config_desc); + + // close connection + libusb_close(handle); + } + libusb_free_device_list(devices, 1); + + libusb_exit(context); +} -- libgit2 0.21.2