Commit 97064000d7e1c35390002eb9512f14d14129e617
1 parent
8a7dc1f5
16u2 desc terminé + début endpoint tests
Showing
8 changed files
with
202 additions
and
25 deletions
Show diff stats
atmega16u2/custom/lufa-LUFA-170418/PolytechLille/PAD/Descriptors.c
@@ -54,8 +54,8 @@ const USB_Descriptor_Device_t PROGMEM RelayBoard_DeviceDescriptor = | @@ -54,8 +54,8 @@ const USB_Descriptor_Device_t PROGMEM RelayBoard_DeviceDescriptor = | ||
54 | 54 | ||
55 | .Endpoint0Size = FIXED_CONTROL_ENDPOINT_SIZE, | 55 | .Endpoint0Size = FIXED_CONTROL_ENDPOINT_SIZE, |
56 | 56 | ||
57 | - .VendorID = 0x04B4, | ||
58 | - .ProductID = 0xFD11, | 57 | + .VendorID = 0x2341, |
58 | + .ProductID = 0x0001, | ||
59 | .ReleaseNumber = VERSION_BCD(2,0,0), | 59 | .ReleaseNumber = VERSION_BCD(2,0,0), |
60 | 60 | ||
61 | .ManufacturerStrIndex = STRING_ID_Manufacturer, | 61 | .ManufacturerStrIndex = STRING_ID_Manufacturer, |
@@ -77,7 +77,7 @@ const USB_Descriptor_Configuration_t PROGMEM RelayBoard_ConfigurationDescriptor | @@ -77,7 +77,7 @@ const USB_Descriptor_Configuration_t PROGMEM RelayBoard_ConfigurationDescriptor | ||
77 | .Header = {.Size = sizeof(USB_Descriptor_Configuration_Header_t), .Type = DTYPE_Configuration}, | 77 | .Header = {.Size = sizeof(USB_Descriptor_Configuration_Header_t), .Type = DTYPE_Configuration}, |
78 | 78 | ||
79 | .TotalConfigurationSize = sizeof(USB_Descriptor_Configuration_t), | 79 | .TotalConfigurationSize = sizeof(USB_Descriptor_Configuration_t), |
80 | - .TotalInterfaces = 1, | 80 | + .TotalInterfaces = 2, |
81 | 81 | ||
82 | .ConfigurationNumber = 1, | 82 | .ConfigurationNumber = 1, |
83 | .ConfigurationStrIndex = NO_DESCRIPTOR, | 83 | .ConfigurationStrIndex = NO_DESCRIPTOR, |
atmega16u2/custom/lufa-LUFA-170418/PolytechLille/PAD/PAD.c
@@ -41,19 +41,43 @@ | @@ -41,19 +41,43 @@ | ||
41 | /** Main program entry point. This routine contains the overall program flow, including initial | 41 | /** Main program entry point. This routine contains the overall program flow, including initial |
42 | * setup of all components and the main program loop. | 42 | * setup of all components and the main program loop. |
43 | */ | 43 | */ |
44 | -int main(void) | ||
45 | -{ | 44 | +int main(void){ |
46 | SetupHardware(); | 45 | SetupHardware(); |
47 | 46 | ||
48 | GlobalInterruptEnable(); | 47 | GlobalInterruptEnable(); |
49 | 48 | ||
50 | - for (;;) | ||
51 | - USB_USBTask(); | 49 | + for (;;){ |
50 | + USB_USBTask(); | ||
51 | + | ||
52 | + // out endpoints | ||
53 | + Endpoint_SelectEndpoint(PAD_OUT_EP_LED1); | ||
54 | + Endpoint_SelectEndpoint(PAD_OUT_EP_LED2); | ||
55 | + if (Endpoint_IsOUTReceived()){ | ||
56 | + if (Endpoint_IsReadWriteAllowed()){ | ||
57 | + // LEDs states | ||
58 | + uint8_t LEDs = Endpoint_Read_8(); | ||
59 | + Serial_SendByte(LEDs); | ||
60 | + Endpoint_ClearOUT(); | ||
61 | + } | ||
62 | + } | ||
63 | + | ||
64 | + // in endpoints | ||
65 | + Endpoint_SelectEndpoint(PAD_IN_EP_BUTTON1); | ||
66 | + Endpoint_SelectEndpoint(PAD_IN_EP_BUTTON2); | ||
67 | + if(Serial_IsCharReceived()){ | ||
68 | + //On lit ce qui a été envoyé depuis le clavier | ||
69 | + uint8_t Received = Serial_ReceiveByte(); | ||
70 | + if (Endpoint_IsReadWriteAllowed()){ | ||
71 | + //On envoi les données sur le endpoint | ||
72 | + Endpoint_Write_8(Received); | ||
73 | + Endpoint_ClearIN(); | ||
74 | + } | ||
75 | + } | ||
76 | + } | ||
52 | } | 77 | } |
53 | 78 | ||
54 | /** Configures the board hardware and chip peripherals for the project's functionality. */ | 79 | /** Configures the board hardware and chip peripherals for the project's functionality. */ |
55 | -void SetupHardware(void) | ||
56 | -{ | 80 | +void SetupHardware(void){ |
57 | #if (ARCH == ARCH_AVR8) | 81 | #if (ARCH == ARCH_AVR8) |
58 | /* Disable watchdog if enabled by bootloader/fuses */ | 82 | /* Disable watchdog if enabled by bootloader/fuses */ |
59 | MCUSR &= ~(1 << WDRF); | 83 | MCUSR &= ~(1 << WDRF); |
@@ -65,8 +89,15 @@ void SetupHardware(void) | @@ -65,8 +89,15 @@ void SetupHardware(void) | ||
65 | 89 | ||
66 | /* Hardware Initialization */ | 90 | /* Hardware Initialization */ |
67 | USB_Init(); | 91 | USB_Init(); |
92 | + Serial_Init(9600, 0); | ||
93 | +} | ||
68 | 94 | ||
69 | - /* Initialize Relays */ | ||
70 | - DDRC |= ALL_RELAYS; | ||
71 | - PORTC &= ~ALL_RELAYS; | 95 | +// endpoints creation |
96 | +void EVENT_USB_Device_configure(void){ | ||
97 | + /* Setup HID Report Endpoints */ | ||
98 | + Endpoint_ConfigureEndpoint(PAD_OUT_EP_LED1 , EP_TYPE_INTERRUPT, PAD_EPSIZE, 1); | ||
99 | + Endpoint_ConfigureEndpoint(PAD_OUT_EP_LED2 , EP_TYPE_INTERRUPT, PAD_EPSIZE, 1); | ||
100 | + Endpoint_ConfigureEndpoint(PAD_IN_EP_BUTTON1, EP_TYPE_INTERRUPT, PAD_EPSIZE, 1); | ||
101 | + Endpoint_ConfigureEndpoint(PAD_IN_EP_BUTTON2, EP_TYPE_INTERRUPT, PAD_EPSIZE, 1); | ||
72 | } | 102 | } |
103 | + |
atmega16u2/custom/lufa-LUFA-170418/PolytechLille/PAD/PAD.h
@@ -31,11 +31,11 @@ | @@ -31,11 +31,11 @@ | ||
31 | 31 | ||
32 | /** \file | 32 | /** \file |
33 | * | 33 | * |
34 | - * Header file for RelayBoard.c. | 34 | + * Header file for PAD.c. |
35 | */ | 35 | */ |
36 | 36 | ||
37 | -#ifndef _RELAYBOARD_H_ | ||
38 | -#define _RELAYBOARD_H_ | 37 | +#ifndef _PAD_H_ |
38 | +#define _PAD_H_ | ||
39 | 39 | ||
40 | /* Includes: */ | 40 | /* Includes: */ |
41 | #include <avr/io.h> | 41 | #include <avr/io.h> |
@@ -48,18 +48,9 @@ | @@ -48,18 +48,9 @@ | ||
48 | #include <LUFA/Drivers/Board/LEDs.h> | 48 | #include <LUFA/Drivers/Board/LEDs.h> |
49 | #include <LUFA/Drivers/USB/USB.h> | 49 | #include <LUFA/Drivers/USB/USB.h> |
50 | #include <LUFA/Platform/Platform.h> | 50 | #include <LUFA/Platform/Platform.h> |
51 | - | ||
52 | - /* Macros: */ | ||
53 | - #define RELAY1 (1 << 7) | ||
54 | - #define RELAY2 (1 << 6) | ||
55 | - #define RELAY3 (1 << 5) | ||
56 | - #define RELAY4 (1 << 4) | ||
57 | - #define ALL_RELAYS (RELAY1 | RELAY2 | RELAY3 | RELAY4) | 51 | + #include <LUFA/Drivers/Peripheral/Serial.h> |
58 | 52 | ||
59 | /* Function Prototypes: */ | 53 | /* Function Prototypes: */ |
60 | void SetupHardware(void); | 54 | void SetupHardware(void); |
61 | - | ||
62 | - void EVENT_USB_Device_ControlRequest(void); | ||
63 | - | ||
64 | #endif | 55 | #endif |
65 | 56 |
atmega16u2/custom/lufa-LUFA-170418/PolytechLille/PAD/makefile
@@ -41,3 +41,9 @@ include $(DMBS_PATH)/gcc.mk | @@ -41,3 +41,9 @@ include $(DMBS_PATH)/gcc.mk | ||
41 | include $(DMBS_PATH)/hid.mk | 41 | include $(DMBS_PATH)/hid.mk |
42 | include $(DMBS_PATH)/avrdude.mk | 42 | include $(DMBS_PATH)/avrdude.mk |
43 | include $(DMBS_PATH)/atprogram.mk | 43 | include $(DMBS_PATH)/atprogram.mk |
44 | + | ||
45 | +upload: all | ||
46 | + dfu-programmer atmega16u2 erase | ||
47 | + dfu-programmer atmega16u2 flash $(TARGET).hex | ||
48 | + dfu-programmer atmega16u2 reset | ||
49 | + make clean |
tests/Serial/Makefile
@@ -34,4 +34,5 @@ $(TARGET).hex: $(TARGET).elf | @@ -34,4 +34,5 @@ $(TARGET).hex: $(TARGET).elf | ||
34 | upload: $(TARGET).hex | 34 | upload: $(TARGET).hex |
35 | stty -F $(TERM) hupcl # reset | 35 | stty -F $(TERM) hupcl # reset |
36 | $(DUDE) $(PGMERISP) -U flash:w:$(TARGET).hex | 36 | $(DUDE) $(PGMERISP) -U flash:w:$(TARGET).hex |
37 | + make clean | ||
37 | 38 |
No preview for this file type
@@ -0,0 +1,142 @@ | @@ -0,0 +1,142 @@ | ||
1 | +#include <libusb-1.0/libusb.h> | ||
2 | +#include <stdio.h> | ||
3 | +#include <stdlib.h> | ||
4 | + | ||
5 | +// arduino ID vendor and product | ||
6 | +#define ID_VENDOR 0x2341 | ||
7 | +#define ID_PRODUCT 0x01 | ||
8 | + | ||
9 | +// gcc file.c -l usb-1.0 | ||
10 | + | ||
11 | +typedef struct { | ||
12 | + int interface_id; | ||
13 | + int endpoint_id; | ||
14 | +} Endpoint; | ||
15 | + | ||
16 | +void init(libusb_context **context, libusb_device ***devices, ssize_t *devices_count){ | ||
17 | + int status = libusb_init(context); | ||
18 | + if(status != 0){perror("libusb_init"); exit(-1);} | ||
19 | + | ||
20 | + *devices_count = libusb_get_device_list(*context, devices); | ||
21 | + if(*devices_count < 0){perror("libusb_get_device_list"); exit(-1);} | ||
22 | +} | ||
23 | + | ||
24 | +libusb_device* searchArduino(libusb_device **devices, ssize_t devices_count){ | ||
25 | + for(int i=0; i<devices_count; i++){ | ||
26 | + libusb_device *device = devices[i]; | ||
27 | + | ||
28 | + // device description | ||
29 | + struct libusb_device_descriptor desc; | ||
30 | + int status = libusb_get_device_descriptor(device, &desc); | ||
31 | + if(status != 0) continue; | ||
32 | + | ||
33 | + // search for device | ||
34 | + if(desc.idVendor == ID_VENDOR | ||
35 | + && desc.idProduct == ID_PRODUCT) | ||
36 | + return device; | ||
37 | + } | ||
38 | + return NULL; | ||
39 | +} | ||
40 | + | ||
41 | +void openConnection(libusb_device *arduino, libusb_device_handle **handle, struct libusb_config_descriptor **config_desc){ | ||
42 | + // open connection | ||
43 | + int status = libusb_open(arduino, handle); | ||
44 | + if(status != 0){ perror("libusb_open"); exit(-1); } | ||
45 | + | ||
46 | + // prepare config | ||
47 | + status = libusb_get_config_descriptor(arduino, 0, config_desc); | ||
48 | + if(status != 0){ perror("libusb_get_config_descriptor"); exit(-1); } | ||
49 | + int configuration = (*config_desc)->bConfigurationValue; | ||
50 | + | ||
51 | + // detach kernel | ||
52 | + for(int j=0; j<(*config_desc)->bNumInterfaces; j++){ | ||
53 | + int interface = (*config_desc)->interface[j].altsetting[0].bInterfaceNumber; | ||
54 | + if(libusb_kernel_driver_active(*handle, interface)){ | ||
55 | + status = libusb_detach_kernel_driver(*handle, interface); | ||
56 | + if(status != 0){ perror("libusb_detach_kernel_driver"); exit(-1); } | ||
57 | + } | ||
58 | + } | ||
59 | + | ||
60 | + // use config | ||
61 | + status = libusb_set_configuration(*handle, configuration); | ||
62 | + if(status != 0){ perror("libusb_set_configuration"); exit(-1); } | ||
63 | + | ||
64 | + // claim interfaces | ||
65 | + for(int j=0; j<(*config_desc)->bNumInterfaces; j++){ | ||
66 | + struct libusb_interface_descriptor interface_desc = (*config_desc)->interface[j].altsetting[0]; | ||
67 | + int interface = interface_desc.bInterfaceNumber; | ||
68 | + | ||
69 | + status = libusb_claim_interface(*handle, interface); | ||
70 | + if(status != 0){ perror("libusb_claim_interface"); exit(-1); } | ||
71 | + } | ||
72 | +} | ||
73 | + | ||
74 | +void getEndpoints(struct libusb_config_descriptor *config_desc, Endpoint* endpoint_list, int* endpoints_size){ | ||
75 | + // how many endpoints in total ? | ||
76 | + *endpoints_size = 0; | ||
77 | + for(int j=0; j<config_desc->bNumInterfaces; j++) | ||
78 | + *endpoints_size += config_desc->interface[j].altsetting[0].bNumEndpoints; | ||
79 | + | ||
80 | + // list endpoints | ||
81 | + endpoints[*endpoints_size]; | ||
82 | + // in interfaces | ||
83 | + for(int j=0; j<config_desc->bNumInterfaces; j++){ | ||
84 | + // find endpoints | ||
85 | + for(int k=0; k<config_desc->interface[j].altsetting[0].bNumEndpoints; k++){ | ||
86 | + struct libusb_endpoint_descriptor endpoint_desc = config_desc->interface[j].altsetting[0].endpoint[k]; | ||
87 | + | ||
88 | + //DEBUG list endpoints bEndpointAddress and bmAttributes | ||
89 | + /*printf("Endpoint inter. %d, num. %d\n", j, k); | ||
90 | + printf("bEndpointAddress = 0x%x\n", endpoint_desc.bEndpointAddress); | ||
91 | + printf("bmAttributes = 0x%x\n", endpoint_desc.bmAttributes);*/ | ||
92 | + } | ||
93 | + } | ||
94 | + | ||
95 | +} | ||
96 | + | ||
97 | +int main(){ | ||
98 | + // init | ||
99 | + libusb_context *context; | ||
100 | + libusb_device **devices; | ||
101 | + ssize_t devices_count; | ||
102 | + init(&context, &devices, &devices_count); | ||
103 | + | ||
104 | + // get arduino device | ||
105 | + libusb_device *arduino = searchArduino(devices, devices_count); | ||
106 | + if(arduino == NULL){ printf("Arduino not found\n"); exit(-1); } | ||
107 | + | ||
108 | + // open connection, use config, detach kernel and claim interfaces | ||
109 | + libusb_device_handle *handle; | ||
110 | + struct libusb_config_descriptor *config_desc; | ||
111 | + openConnection(arduino, &handle, &config_desc); | ||
112 | + | ||
113 | + // get enpoints | ||
114 | + Endpoint* endpoint_list; | ||
115 | + int endpoint_list_size; | ||
116 | + getEndpoints(config_desc); | ||
117 | +//TODO ^^^^^^^^^^^^ | ||
118 | + | ||
119 | + | ||
120 | + | ||
121 | + // release interfaces | ||
122 | + for(int j=0; j<config_desc->bNumInterfaces; j++){ | ||
123 | + struct libusb_interface_descriptor interface_desc = config_desc->interface[j].altsetting[0]; | ||
124 | + int interface = interface_desc.bInterfaceNumber; | ||
125 | + | ||
126 | + int status = libusb_release_interface(handle, interface); | ||
127 | + if(status != 0){ perror("libusb_release_interface"); exit(-1); } | ||
128 | + } | ||
129 | + | ||
130 | + // free config | ||
131 | + libusb_free_config_descriptor(config_desc); | ||
132 | + | ||
133 | + // close connection | ||
134 | + libusb_close(handle); | ||
135 | + | ||
136 | + // free device list | ||
137 | + libusb_free_device_list(devices, 1); | ||
138 | + | ||
139 | + // libusb exit | ||
140 | + libusb_exit(context); | ||
141 | +} | ||
142 | + |