Commit 58b09a0b9ee61df2b1ee3095e36887f4a8960e53

Authored by root
1 parent bfb1101b

Ca marche git status

PartiePC/PCReveive.c 0 → 100644
... ... @@ -0,0 +1,170 @@
  1 +#include <libusb-1.0/libusb.h>
  2 +#include <stdio.h>
  3 +#include <stdlib.h>
  4 +#include <signal.h>
  5 +#include <unistd.h>
  6 +
  7 +// arduino ID vendor and product
  8 +#define ID_VENDOR 0x2341
  9 +#define ID_PRODUCT 0x01
  10 +#define ENDPOINTS_NUMBER 4
  11 +#define TIMEOUT 100
  12 +#define MAX_DATA 50
  13 +
  14 +void init(libusb_context **context, libusb_device ***devices, ssize_t *devices_count){
  15 + int status = libusb_init(context);
  16 + if(status != 0){perror("libusb_init"); exit(-1);}
  17 +
  18 + *devices_count = libusb_get_device_list(*context, devices);
  19 + if(*devices_count < 0){perror("libusb_get_device_list"); exit(-1);}
  20 +}
  21 +
  22 +libusb_device* searchArduino(libusb_device **devices, ssize_t devices_count){
  23 + for(int i=0; i<devices_count; i++){
  24 + libusb_device *device = devices[i];
  25 +
  26 + // device description
  27 + struct libusb_device_descriptor desc;
  28 + int status = libusb_get_device_descriptor(device, &desc);
  29 + if(status != 0) continue;
  30 +
  31 + // search for device
  32 + if(desc.idVendor == ID_VENDOR
  33 + && desc.idProduct == ID_PRODUCT)
  34 + return device;
  35 + }
  36 + return NULL;
  37 +}
  38 +
  39 +void openConnection(libusb_device *arduino, libusb_device_handle **handle, struct libusb_config_descriptor **config_desc){
  40 + // open connection
  41 + int status = libusb_open(arduino, handle);
  42 + if(status != 0){ perror("libusb_open"); exit(-1); }
  43 +
  44 + // prepare config
  45 + status = libusb_get_config_descriptor(arduino, 0, config_desc);
  46 + if(status != 0){ perror("libusb_get_config_descriptor"); exit(-1); }
  47 + int configuration = (*config_desc)->bConfigurationValue;
  48 +
  49 + // detach kernel
  50 + for(int j=0; j<(*config_desc)->bNumInterfaces; j++){
  51 + int interface = (*config_desc)->interface[j].altsetting[0].bInterfaceNumber;
  52 + if(libusb_kernel_driver_active(*handle, interface)){
  53 + status = libusb_detach_kernel_driver(*handle, interface);
  54 + if(status != 0){ perror("libusb_detach_kernel_driver"); exit(-1); }
  55 + }
  56 + }
  57 +
  58 + // use config
  59 + status = libusb_set_configuration(*handle, configuration);
  60 + if(status != 0){ perror("libusb_set_configuration"); exit(-1); }
  61 +
  62 + // claim interfaces
  63 + for(int j=0; j<(*config_desc)->bNumInterfaces; j++){
  64 + struct libusb_interface_descriptor interface_desc = (*config_desc)->interface[j].altsetting[0];
  65 + int interface = interface_desc.bInterfaceNumber;
  66 +
  67 + status = libusb_claim_interface(*handle, interface);
  68 + if(status != 0){ perror("libusb_claim_interface"); exit(-1); }
  69 + }
  70 +}
  71 +
  72 +void getEndpoints(struct libusb_config_descriptor *config_desc, struct libusb_endpoint_descriptor *endpoint_desc_list){
  73 + int count = 0;
  74 + // in interfaces
  75 + for(int j=0; j<config_desc->bNumInterfaces; j++){
  76 + // find endpoints
  77 + for(int k=0; k<config_desc->interface[j].altsetting[0].bNumEndpoints; k++){
  78 + if(count > ENDPOINTS_NUMBER){ printf("getEndpoints: Array out of bound :%d:", count); exit(-1); }
  79 + *(endpoint_desc_list + count) = config_desc->interface[j].altsetting[0].endpoint[k];
  80 + count++;
  81 + }
  82 + }
  83 + //if(count != ENDPOINTS_NUMBER){ printf("Wrong number of endpoints.\nIs this the good device ?\n"); exit(-1); }
  84 +}
  85 +
  86 +void start(libusb_context **context, libusb_device ***devices, libusb_device_handle **handle, struct libusb_config_descriptor **config_desc, struct libusb_endpoint_descriptor *endpoint_desc_list){
  87 + // init
  88 + ssize_t devices_count;
  89 + init(context, devices, &devices_count);
  90 +
  91 + // get arduino device
  92 + libusb_device *arduino = searchArduino(*devices, devices_count);
  93 + if(arduino == NULL){ printf("Arduino device not found\n"); exit(-1); }
  94 +
  95 + // open connection, use config, detach kernel and claim interfaces
  96 + openConnection(arduino, handle, config_desc);
  97 +
  98 + // get enpoints
  99 + getEndpoints(*config_desc, endpoint_desc_list);
  100 +}
  101 +
  102 +void stop(struct libusb_config_descriptor *config_desc, libusb_device_handle *handle, libusb_device **devices, libusb_context *context){
  103 + // release interfaces
  104 + for(int j=0; j<config_desc->bNumInterfaces; j++){
  105 + struct libusb_interface_descriptor interface_desc = config_desc->interface[j].altsetting[0];
  106 + int interface = interface_desc.bInterfaceNumber;
  107 +
  108 + int status = libusb_release_interface(handle, interface);
  109 + if(status != 0){ perror("libusb_release_interface"); exit(-1); }
  110 + }
  111 +
  112 + // free config
  113 + libusb_free_config_descriptor(config_desc);
  114 +
  115 + // close connection
  116 + libusb_close(handle);
  117 +
  118 + // free device list
  119 + libusb_free_device_list(devices, 1);
  120 +
  121 + // libusb exit
  122 + libusb_exit(context);
  123 +}
  124 +
  125 +void sendData(int endpoint_id, uint8_t data, libusb_device_handle *handle, struct libusb_endpoint_descriptor *endpoint_desc_list){
  126 + if(endpoint_id < 0 || endpoint_id > 1){ printf("(sendData) Wrong endpoint !\nMust be 0 or 1\n"); return; }
  127 + int status = libusb_interrupt_transfer(handle, endpoint_desc_list[endpoint_id].bEndpointAddress, (unsigned char *) &data, 1, NULL, TIMEOUT);
  128 + if(status!=0){ perror("libusb_interrupt_transfer"); exit(-1); }
  129 +}
  130 +
  131 +void receiveData(int endpoint_id, uint8_t *data, libusb_device_handle *handle, struct libusb_endpoint_descriptor *endpoint_desc_list){
  132 + if(endpoint_id < 2 || endpoint_id > 3){ printf("(sendData) Wrong endpoint !\nMust be 2 or 3\n"); return; }
  133 + int status = libusb_interrupt_transfer(handle, endpoint_desc_list[endpoint_id].bEndpointAddress, (unsigned char *) data, 1, NULL, 0);
  134 + if(status!=0){ perror("libusb_interrupt_transfer"); exit(-1); }
  135 +}
  136 +
  137 +int go = 1;
  138 +void signalINT(int sig){
  139 + if(sig == SIGINT){
  140 + printf("\nSignal SIGINT reçu...\n");
  141 + go = 0;
  142 + }
  143 +}
  144 +int main(){
  145 + libusb_context *context;
  146 + libusb_device **devices;
  147 + libusb_device_handle *handle;
  148 + struct libusb_config_descriptor *config_desc;
  149 + struct libusb_endpoint_descriptor endpoint_desc_list[ENDPOINTS_NUMBER];
  150 +
  151 + // start
  152 + printf("Starting...\n");
  153 + start(&context, &devices, &handle, &config_desc, endpoint_desc_list);
  154 + printf("Start complete\n");
  155 +
  156 + struct sigaction action;
  157 + action.sa_handler = signalINT;
  158 + sigaction(SIGINT, &action, NULL);
  159 +
  160 + while(go){
  161 + int data;
  162 + receiveData(3, &data, handle, endpoint_desc_list);
  163 + printf("0x%x\n", data);
  164 + }
  165 +
  166 + // stop
  167 + printf("Stoping...\n");
  168 + stop(config_desc, handle, devices, context);
  169 + printf("Stop complete...\n");
  170 +}
... ...
PartiePC/PC.c renamed to PartiePC/PCSend.c
... ... @@ -122,15 +122,15 @@ void stop(struct libusb_config_descriptor *config_desc, libusb_device_handle *ha
122 122 libusb_exit(context);
123 123 }
124 124  
125   -void sendData(int endpoint_id, uint8_t data){
  125 +void sendData(int endpoint_id, uint8_t data, libusb_device_handle *handle, struct libusb_endpoint_descriptor *endpoint_desc_list){
126 126 if(endpoint_id < 0 || endpoint_id > 1){ printf("(sendData) Wrong endpoint !\nMust be 0 or 1\n"); return; }
127 127 int status = libusb_interrupt_transfer(handle, endpoint_desc_list[endpoint_id].bEndpointAddress, (unsigned char *) &data, 1, NULL, TIMEOUT);
128 128 if(status!=0){ perror("libusb_interrupt_transfer"); exit(-1); }
129 129 }
130 130  
131   -void receiveData(int endpoint_id, uint8_t *data){
  131 +void receiveData(int endpoint_id, uint8_t *data, libusb_device_handle *handle, struct libusb_endpoint_descriptor *endpoint_desc_list){
132 132 if(endpoint_id < 2 || endpoint_id > 3){ printf("(sendData) Wrong endpoint !\nMust be 2 or 3\n"); return; }
133   - status = libusb_interrupt_transfer(handle, endpoint_desc_list[endpoint_id].bEndpointAddress, (unsigned char *) data, 1, NULL, TIMEOUT);
  133 + int status = libusb_interrupt_transfer(handle, endpoint_desc_list[endpoint_id].bEndpointAddress, (unsigned char *) data, 1, NULL, TIMEOUT);
134 134 if(status!=0){ perror("libusb_interrupt_transfer"); exit(-1); }
135 135 }
136 136  
... ... @@ -158,12 +158,14 @@ int main(){
158 158 sigaction(SIGINT, &action, NULL);
159 159  
160 160 while(go){
161   - sendData(0, 0x16); //0b00010110
  161 + sendData(1, 0x01, handle, endpoint_desc_list);
162 162 sleep(1);
163   - sendData(1, 0x01); //0b00000001
  163 + sendData(0, 0x1F, handle, endpoint_desc_list);
164 164 sleep(1);
165   - sendData(1, 0x00);
166   - sendData(1, 0x00);
  165 + sendData(0, 0x16, handle, endpoint_desc_list);
  166 + sleep(1);
  167 + sendData(0, 0x00, handle, endpoint_desc_list);
  168 + sendData(1, 0x00, handle, endpoint_desc_list);
167 169 sleep(1);
168 170 }
169 171  
... ...
PartiePC/a.out
No preview for this file type
PartiePC/makefile
1 1 all:
2   - gcc *.c -l usb-1.0 -Wall -Wextra
  2 + gcc PCSend.c -l usb-1.0 -Wall -Wextra
3 3  
4 4 clean:
5 5 rm -f *.o a.out
... ...
atmega16u2/base/makefile 0 → 100644
... ... @@ -0,0 +1,4 @@
  1 +all:
  2 + dfu-programmer atmega16u2 erase
  3 + dfu-programmer atmega16u2 flash *.hex
  4 + dfu-programmer atmega16u2 reset
... ...
atmega16u2/custom/lufa-LUFA-170418/PolytechLille/PAD/PAD.c
... ... @@ -15,6 +15,7 @@ int main(void){
15 15 }
16 16 }
17 17  
  18 +uint8_t led = 0;
18 19 void PAD_Task(void){
19 20 if (USB_DeviceState != DEVICE_STATE_Configured)
20 21 return;
... ... @@ -33,37 +34,34 @@ void PAD_Task(void){
33 34 * Z : état d'un bouton
34 35  
35 36 * Cas 0b1YZZZZZZ : (joystick)
36   - * 12u2 -> PC (endpoint button 2) : 0bXXXXYYYY
37   - * X != 0000 -> valeur en X du joystick (codé sur 4 bits) Y est mis à 0000
38   - * Y != 0000 -> valeur en Y du joystick (codé sur 4 bits) X est mis à 0000
  37 + * 12u2 -> PC (endpoint button 2) : 0bX0YYYYYY
  38 + * X = 0 -> valeur en X du joystick
  39 + * X = 1 -> valeur en Y du joystick
39 40 */
40 41 if(Serial_IsCharReceived()){
41 42 uint8_t byte = Serial_ReceiveByte();
42 43 if((byte & 0x80) == 0){ // bouttons
43 44 Endpoint_SelectEndpoint(PAD_IN_EP_BUTTON1);
44 45 if (Endpoint_IsINReady() && Endpoint_IsReadWriteAllowed()){
45   - LEDs_SetAllLEDs(LEDS_LED1);
  46 + LEDs_SetAllLEDs(LEDS_LED1); //LED Tx
46 47 Endpoint_Write_8(byte);
47 48 Endpoint_ClearIN();
48   - LEDs_SetAllLEDs(0);
49 49 }
50 50 }else if((byte & 0x40) == 0){ // joystick X
51   - uint8_t toSend = (byte << 2) && 0xF0;
  51 + uint8_t toSend = (byte) & 0x3F;
52 52 Endpoint_SelectEndpoint(PAD_IN_EP_BUTTON2);
53 53 if (Endpoint_IsINReady() && Endpoint_IsReadWriteAllowed()){
54   - LEDs_SetAllLEDs(LEDS_LED1);
55   - Endpoint_Write_8(byte);
  54 + LEDs_SetAllLEDs(LEDS_LED1); //LED Tx
  55 + Endpoint_Write_8(toSend);
56 56 Endpoint_ClearIN();
57   - LEDs_SetAllLEDs(0);
58 57 }
59 58 }else{ // joystick Y
60   - uint8_t toSend = (byte >> 2) && 0x0F;
  59 + uint8_t toSend = ((byte) & 0x3F) | 0x80;
61 60 Endpoint_SelectEndpoint(PAD_IN_EP_BUTTON2);
62 61 if (Endpoint_IsINReady() && Endpoint_IsReadWriteAllowed()){
63   - LEDs_SetAllLEDs(LEDS_LED1);
64   - Endpoint_Write_8(byte);
  62 + LEDs_SetAllLEDs(LEDS_LED1); //LED Tx
  63 + Endpoint_Write_8(toSend);
65 64 Endpoint_ClearIN();
66   - LEDs_SetAllLEDs(0);
67 65 }
68 66 }
69 67 }
... ... @@ -78,22 +76,27 @@ void PAD_Task(void){
78 76 * X = état d'une LED (LED 13)
79 77 *
80 78 * 16u2 -> 328p : 0bXYYYYYYY
81   - * X = 0 -> pour les LEDs 8 9 10 11 12 (Y : les données)
82   - * X = 1 -> pour la LED 13 (Y : la donnée)
  79 + * X = 0 -> pour les LEDs 8 9 10 11 12 13 (Y : les données)
83 80 */
84 81 Endpoint_SelectEndpoint(PAD_OUT_EP_LED1);
85 82 if (Endpoint_IsOUTReceived() && Endpoint_IsReadWriteAllowed()){
  83 + LEDs_SetAllLEDs(LEDS_LED2); //LED Rx
86 84 uint8_t byte = Endpoint_Read_8();
87   - Serial_SendByte(byte);
  85 + led = (led & 0x20) | byte;
  86 + Serial_SendByte(led);
88 87 Endpoint_ClearOUT();
89 88 }
90   - Endpoint_SelectEndpoint(PAD_OUT_EP_LED1);
  89 + Endpoint_SelectEndpoint(PAD_OUT_EP_LED2);
91 90 if (Endpoint_IsOUTReceived() && Endpoint_IsReadWriteAllowed()){
  91 + LEDs_SetAllLEDs(LEDS_LED2); //LED Rx
92 92 uint8_t byte = Endpoint_Read_8();
93   - uint8_t toSend = byte | 0x80;
94   - Serial_SendByte(byte);
  93 + led = (led & 0x1F) | byte << 5;
  94 + Serial_SendByte(led);
95 95 Endpoint_ClearOUT();
96 96 }
  97 +
  98 + //LED Reset
  99 + LEDs_SetAllLEDs(0);
97 100 }
98 101  
99 102 /** Configures the board hardware and chip peripherals for the project's functionality. */
... ... @@ -117,7 +120,7 @@ void SetupHardware(void){
117 120 void EVENT_USB_Device_ConfigurationChanged(void){
118 121 /* Setup HID Report Endpoints */
119 122 Endpoint_ConfigureEndpoint(PAD_OUT_EP_LED1 , EP_TYPE_INTERRUPT, PAD_EPSIZE, 1);
120   - Endpoint_ConfigureEndpoint(PAD_OUT_EP_LED2 , EP_TYPE_INTERRif(Serial_IsCharReceived())UPT, PAD_EPSIZE, 1);
  123 + Endpoint_ConfigureEndpoint(PAD_OUT_EP_LED2 , EP_TYPE_INTERRUPT, PAD_EPSIZE, 1);
121 124 Endpoint_ConfigureEndpoint(PAD_IN_EP_BUTTON1, EP_TYPE_INTERRUPT, PAD_EPSIZE, 1);
122 125 Endpoint_ConfigureEndpoint(PAD_IN_EP_BUTTON2, EP_TYPE_INTERRUPT, PAD_EPSIZE, 1);
123 126 }
... ...
atmega16u2/custom/lufa-LUFA-170418/PolytechLille/PAD/PAD.h
... ... @@ -29,9 +29,7 @@
29 29  
30 30 /* Function Prototypes: */
31 31 void SetupHardware(void);
32   - void HID_Task(void);
33   - void Send_Task(void);
34   - void EVENT_USB_Device_configure(void);
  32 + void PAD_Task(void);
35 33 void EVENT_USB_Device_ControlRequest(void);
36 34 #endif
37 35  
... ...