From 97064000d7e1c35390002eb9512f14d14129e617 Mon Sep 17 00:00:00 2001 From: root Date: Thu, 17 Jan 2019 19:39:02 +0100 Subject: [PATCH] 16u2 desc terminé + début endpoint tests --- atmega16u2/custom/lufa-LUFA-170418/PolytechLille/PAD/Descriptors.c | 6 +++--- atmega16u2/custom/lufa-LUFA-170418/PolytechLille/PAD/PAD.c | 49 ++++++++++++++++++++++++++++++++++++++++--------- atmega16u2/custom/lufa-LUFA-170418/PolytechLille/PAD/PAD.h | 17 ++++------------- atmega16u2/custom/lufa-LUFA-170418/PolytechLille/PAD/makefile | 6 ++++++ tests/Serial/Makefile | 1 + tests/USBSendData/a.out | Bin 0 -> 13744 bytes tests/USBSendData/makefile | 6 ++++++ tests/USBSendData/test_send_data.c | 142 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 202 insertions(+), 25 deletions(-) create mode 100755 tests/USBSendData/a.out create mode 100644 tests/USBSendData/makefile create mode 100644 tests/USBSendData/test_send_data.c diff --git a/atmega16u2/custom/lufa-LUFA-170418/PolytechLille/PAD/Descriptors.c b/atmega16u2/custom/lufa-LUFA-170418/PolytechLille/PAD/Descriptors.c index 08349a4..ad15e7c 100644 --- a/atmega16u2/custom/lufa-LUFA-170418/PolytechLille/PAD/Descriptors.c +++ b/atmega16u2/custom/lufa-LUFA-170418/PolytechLille/PAD/Descriptors.c @@ -54,8 +54,8 @@ const USB_Descriptor_Device_t PROGMEM RelayBoard_DeviceDescriptor = .Endpoint0Size = FIXED_CONTROL_ENDPOINT_SIZE, - .VendorID = 0x04B4, - .ProductID = 0xFD11, + .VendorID = 0x2341, + .ProductID = 0x0001, .ReleaseNumber = VERSION_BCD(2,0,0), .ManufacturerStrIndex = STRING_ID_Manufacturer, @@ -77,7 +77,7 @@ const USB_Descriptor_Configuration_t PROGMEM RelayBoard_ConfigurationDescriptor .Header = {.Size = sizeof(USB_Descriptor_Configuration_Header_t), .Type = DTYPE_Configuration}, .TotalConfigurationSize = sizeof(USB_Descriptor_Configuration_t), - .TotalInterfaces = 1, + .TotalInterfaces = 2, .ConfigurationNumber = 1, .ConfigurationStrIndex = NO_DESCRIPTOR, diff --git a/atmega16u2/custom/lufa-LUFA-170418/PolytechLille/PAD/PAD.c b/atmega16u2/custom/lufa-LUFA-170418/PolytechLille/PAD/PAD.c index 002b0c7..d0de6aa 100644 --- a/atmega16u2/custom/lufa-LUFA-170418/PolytechLille/PAD/PAD.c +++ b/atmega16u2/custom/lufa-LUFA-170418/PolytechLille/PAD/PAD.c @@ -41,19 +41,43 @@ /** Main program entry point. This routine contains the overall program flow, including initial * setup of all components and the main program loop. */ -int main(void) -{ +int main(void){ SetupHardware(); GlobalInterruptEnable(); - for (;;) - USB_USBTask(); + for (;;){ + USB_USBTask(); + + // out endpoints + Endpoint_SelectEndpoint(PAD_OUT_EP_LED1); + Endpoint_SelectEndpoint(PAD_OUT_EP_LED2); + if (Endpoint_IsOUTReceived()){ + if (Endpoint_IsReadWriteAllowed()){ + // LEDs states + uint8_t LEDs = Endpoint_Read_8(); + Serial_SendByte(LEDs); + Endpoint_ClearOUT(); + } + } + + // in endpoints + Endpoint_SelectEndpoint(PAD_IN_EP_BUTTON1); + Endpoint_SelectEndpoint(PAD_IN_EP_BUTTON2); + if(Serial_IsCharReceived()){ + //On lit ce qui a été envoyé depuis le clavier + uint8_t Received = Serial_ReceiveByte(); + if (Endpoint_IsReadWriteAllowed()){ + //On envoi les données sur le endpoint + Endpoint_Write_8(Received); + Endpoint_ClearIN(); + } + } + } } /** Configures the board hardware and chip peripherals for the project's functionality. */ -void SetupHardware(void) -{ +void SetupHardware(void){ #if (ARCH == ARCH_AVR8) /* Disable watchdog if enabled by bootloader/fuses */ MCUSR &= ~(1 << WDRF); @@ -65,8 +89,15 @@ void SetupHardware(void) /* Hardware Initialization */ USB_Init(); + Serial_Init(9600, 0); +} - /* Initialize Relays */ - DDRC |= ALL_RELAYS; - PORTC &= ~ALL_RELAYS; +// endpoints creation +void EVENT_USB_Device_configure(void){ + /* Setup HID Report Endpoints */ + Endpoint_ConfigureEndpoint(PAD_OUT_EP_LED1 , EP_TYPE_INTERRUPT, PAD_EPSIZE, 1); + Endpoint_ConfigureEndpoint(PAD_OUT_EP_LED2 , EP_TYPE_INTERRUPT, PAD_EPSIZE, 1); + Endpoint_ConfigureEndpoint(PAD_IN_EP_BUTTON1, EP_TYPE_INTERRUPT, PAD_EPSIZE, 1); + Endpoint_ConfigureEndpoint(PAD_IN_EP_BUTTON2, EP_TYPE_INTERRUPT, PAD_EPSIZE, 1); } + diff --git a/atmega16u2/custom/lufa-LUFA-170418/PolytechLille/PAD/PAD.h b/atmega16u2/custom/lufa-LUFA-170418/PolytechLille/PAD/PAD.h index 31ea732..7ea6bab 100644 --- a/atmega16u2/custom/lufa-LUFA-170418/PolytechLille/PAD/PAD.h +++ b/atmega16u2/custom/lufa-LUFA-170418/PolytechLille/PAD/PAD.h @@ -31,11 +31,11 @@ /** \file * - * Header file for RelayBoard.c. + * Header file for PAD.c. */ -#ifndef _RELAYBOARD_H_ -#define _RELAYBOARD_H_ +#ifndef _PAD_H_ +#define _PAD_H_ /* Includes: */ #include @@ -48,18 +48,9 @@ #include #include #include - - /* Macros: */ - #define RELAY1 (1 << 7) - #define RELAY2 (1 << 6) - #define RELAY3 (1 << 5) - #define RELAY4 (1 << 4) - #define ALL_RELAYS (RELAY1 | RELAY2 | RELAY3 | RELAY4) + #include /* Function Prototypes: */ void SetupHardware(void); - - void EVENT_USB_Device_ControlRequest(void); - #endif diff --git a/atmega16u2/custom/lufa-LUFA-170418/PolytechLille/PAD/makefile b/atmega16u2/custom/lufa-LUFA-170418/PolytechLille/PAD/makefile index 86188fa..b22db8f 100644 --- a/atmega16u2/custom/lufa-LUFA-170418/PolytechLille/PAD/makefile +++ b/atmega16u2/custom/lufa-LUFA-170418/PolytechLille/PAD/makefile @@ -41,3 +41,9 @@ include $(DMBS_PATH)/gcc.mk include $(DMBS_PATH)/hid.mk include $(DMBS_PATH)/avrdude.mk include $(DMBS_PATH)/atprogram.mk + +upload: all + dfu-programmer atmega16u2 erase + dfu-programmer atmega16u2 flash $(TARGET).hex + dfu-programmer atmega16u2 reset + make clean diff --git a/tests/Serial/Makefile b/tests/Serial/Makefile index 1230694..6079247 100755 --- a/tests/Serial/Makefile +++ b/tests/Serial/Makefile @@ -34,4 +34,5 @@ $(TARGET).hex: $(TARGET).elf upload: $(TARGET).hex stty -F $(TERM) hupcl # reset $(DUDE) $(PGMERISP) -U flash:w:$(TARGET).hex + make clean diff --git a/tests/USBSendData/a.out b/tests/USBSendData/a.out new file mode 100755 index 0000000..29d9303 Binary files /dev/null and b/tests/USBSendData/a.out differ diff --git a/tests/USBSendData/makefile b/tests/USBSendData/makefile new file mode 100644 index 0000000..8ff52c7 --- /dev/null +++ b/tests/USBSendData/makefile @@ -0,0 +1,6 @@ +all: + gcc *.c -l usb-1.0 -Wall -Wextra + +clean: + rm -f *.o a.out + diff --git a/tests/USBSendData/test_send_data.c b/tests/USBSendData/test_send_data.c new file mode 100644 index 0000000..e03343a --- /dev/null +++ b/tests/USBSendData/test_send_data.c @@ -0,0 +1,142 @@ +#include +#include +#include + +// arduino ID vendor and product +#define ID_VENDOR 0x2341 +#define ID_PRODUCT 0x01 + +// gcc file.c -l usb-1.0 + +typedef struct { + int interface_id; + int endpoint_id; +} Endpoint; + +void init(libusb_context **context, libusb_device ***devices, ssize_t *devices_count){ + int status = libusb_init(context); + if(status != 0){perror("libusb_init"); exit(-1);} + + *devices_count = libusb_get_device_list(*context, devices); + if(*devices_count < 0){perror("libusb_get_device_list"); exit(-1);} +} + +libusb_device* searchArduino(libusb_device **devices, ssize_t devices_count){ + for(int i=0; ibConfigurationValue; + + // detach kernel + for(int j=0; j<(*config_desc)->bNumInterfaces; 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; j<(*config_desc)->bNumInterfaces; j++){ + struct 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); } + } +} + +void getEndpoints(struct libusb_config_descriptor *config_desc, Endpoint* endpoint_list, int* endpoints_size){ + // how many endpoints in total ? + *endpoints_size = 0; + for(int j=0; jbNumInterfaces; j++) + *endpoints_size += config_desc->interface[j].altsetting[0].bNumEndpoints; + + // list endpoints + endpoints[*endpoints_size]; + // in interfaces + for(int j=0; jbNumInterfaces; j++){ + // find endpoints + for(int k=0; kinterface[j].altsetting[0].bNumEndpoints; k++){ + struct libusb_endpoint_descriptor endpoint_desc = config_desc->interface[j].altsetting[0].endpoint[k]; + + //DEBUG 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);*/ + } + } + +} + +int main(){ + // init + libusb_context *context; + libusb_device **devices; + ssize_t devices_count; + init(&context, &devices, &devices_count); + + // get arduino device + libusb_device *arduino = searchArduino(devices, devices_count); + if(arduino == NULL){ printf("Arduino not found\n"); exit(-1); } + + // open connection, use config, detach kernel and claim interfaces + libusb_device_handle *handle; + struct libusb_config_descriptor *config_desc; + openConnection(arduino, &handle, &config_desc); + + // get enpoints + Endpoint* endpoint_list; + int endpoint_list_size; + getEndpoints(config_desc); +//TODO ^^^^^^^^^^^^ + + + + // release interfaces + for(int j=0; jbNumInterfaces; j++){ + struct libusb_interface_descriptor interface_desc = config_desc->interface[j].altsetting[0]; + int interface = interface_desc.bInterfaceNumber; + + int 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); + + // free device list + libusb_free_device_list(devices, 1); + + // libusb exit + libusb_exit(context); +} + -- libgit2 0.21.2