Commit ca6bb997c5dc4e04fa340f3be00b9a1d66c546bd

Authored by rguillom
1 parent fd611cac

Ajout Pgm/init.c car oublié au précédent commit (+ ajout fonctions émission et r…

…éception par les points d'accès
Légère retouches des descriptors et PAD.h
Pgm PC/init_USB.c 0 → 100644
... ... @@ -0,0 +1,252 @@
  1 +#include <libusb-1.0/libusb.h>
  2 +#include <stdio.h>
  3 +#include <stdlib.h>
  4 +
  5 +/* Caractéristiques du périphérique */
  6 +#define VENDOR_ID 0x2341
  7 +#define PRODUCT_ID 0x0001
  8 +#define TAB_PA_SIZE 10
  9 +
  10 +libusb_device_handle *handle=NULL;
  11 +libusb_device *device=NULL;
  12 +libusb_device *found=NULL; //notre périphérique
  13 +libusb_context *context;
  14 +
  15 +/*
  16 +struct TABLEAU{
  17 + struct EP *tableau[3]; //tableau des points d'accès en interruption
  18 + int dernier;
  19 +};
  20 +
  21 +struct EP {
  22 + uint8_t ep; //adresse d'accès de l'endpoint
  23 + unsigned char io; //décrit si c'est une entrée ou une sortie
  24 +};
  25 +
  26 +//init structure tableau
  27 +struct TABLEAU tab_pa;
  28 +
  29 +void init_tableau(){
  30 + for (int i=0;i<3;i++){
  31 + tab_pa.tableau[i]=NULL;
  32 + }
  33 + tab_pa.dernier=0;
  34 +}
  35 +*/
  36 +
  37 +/*énumération des périphériques USB*/
  38 +void enum_periph(){
  39 + libusb_device **list;
  40 +
  41 + /* On compte le nombre de périphérique connectés... */
  42 + ssize_t count=libusb_get_device_list(context,&list);
  43 + if(count<0) {perror("libusb_get_device_list"); exit(-1);}
  44 + ssize_t i=0;
  45 +
  46 + /* et on en établit la liste complète */
  47 + for(i=0;i<count;i++){
  48 + libusb_device *device=list[i]; //enregistre l'appareil i dans la liste list
  49 + struct libusb_device_descriptor desc;
  50 + int status=libusb_get_device_descriptor(device,&desc); //enregistre le descripteur d'appareil
  51 + if(status!=0) continue;
  52 +
  53 + /* On capture et affiche les numéros de bus et d'addresse */
  54 + uint8_t bus=libusb_get_bus_number(device);
  55 + uint8_t address=libusb_get_device_address(device);
  56 + printf("Device Found @ (Bus:Address) %d:%d\n",bus,address);
  57 + printf("Vendor ID 0x0%x\n",desc.idVendor);
  58 + printf("Product ID 0x0%x\n",desc.idProduct);
  59 +
  60 + /* Recherche de notre périphérique et sauvegarde dans 'found' */
  61 + if(desc.idVendor == VENDOR_ID && desc.idProduct == PRODUCT_ID){
  62 + found = device;
  63 + printf("\nPériphérique trouvé @ (Bus:Adresse) %d:%d\n", bus, address); //test si le "found" reste le meme
  64 + break;
  65 + }
  66 + }
  67 + if (found == NULL) printf("Périphérique non trouvé\n");
  68 + libusb_free_device_list(list,1);
  69 +}
  70 +
  71 +
  72 +void config_periph(char tab_PA[TAB_PA_SIZE]){
  73 +
  74 + /* Récupération de la 1ère configuration du périphérique (indice 0) */
  75 + struct libusb_config_descriptor *config_desc=NULL;
  76 + int status = libusb_get_config_descriptor(found,0,&config_desc); //config d'indice 0
  77 + printf("ID de la configuration d'indice 0 : %d\n",config_desc->bConfigurationValue);
  78 +
  79 + int interface;
  80 +
  81 + /* Si le noyau est passé avant nous,
  82 + On le détache pour chaque alternative d'interface numéro 0 */
  83 + for(int indice_intf=0;indice_intf < config_desc->bNumInterfaces ;indice_intf++){
  84 +
  85 + interface=config_desc->interface[indice_intf].altsetting[0].bInterfaceNumber;
  86 +
  87 + if(libusb_kernel_driver_active(handle,interface)){
  88 + int status=libusb_detach_kernel_driver(handle,interface);
  89 + if(status!=0){ perror("libusb_detach_kernel_driver"); exit(-1); } //erreur si status!=0
  90 + }
  91 + //printf("indice intf trouvée %d\n",interface);
  92 + }
  93 +
  94 +
  95 + /* Utilisation de la configuration */
  96 + int configuration=config_desc->bConfigurationValue;
  97 +
  98 + printf("valeur config %d\n", configuration); // Affichage valeur de la configuration
  99 + status=libusb_set_configuration(handle,configuration);
  100 + if(status!=0){ perror("libusb_set_configuration"); exit(-1); }
  101 +
  102 +
  103 + /* Appropriation de toutes les interfaces */
  104 + int num_intf;
  105 + for(int indice_intf=0;indice_intf < config_desc->bNumInterfaces ;indice_intf++){
  106 + num_intf = config_desc->interface[indice_intf].altsetting[0].bInterfaceNumber;
  107 +
  108 + status=libusb_claim_interface(handle,indice_intf);
  109 + if(status!=0){ perror("libusb_claim_interface"); exit(-1); }
  110 + printf("Interface d'indice %d et de numéro %d réclamée\n",indice_intf,num_intf);
  111 +
  112 +
  113 +
  114 + /* Parcours des points d'accès (Endpoint) pour chaque interface*/
  115 + for(int num_PA=0;num_PA<config_desc->interface[indice_intf].altsetting[0].bNumEndpoints ;num_PA++){
  116 +
  117 + int type_PA = config_desc->interface[indice_intf].altsetting[0].endpoint[num_PA].bmAttributes;
  118 + uint8_t adresse_PA = config_desc->interface[indice_intf].altsetting[0].endpoint[num_PA].bEndpointAddress;
  119 + printf("Point d'accès trouvé. Adresse :%d\n",adresse_PA);
  120 +
  121 + /* Regarde si le point d'accès est de type interruption.
  122 + Si oui, on le sauvegarde. */
  123 + int dernier_PA=-1;
  124 + if((type_PA & 0b11)==LIBUSB_TRANSFER_TYPE_INTERRUPT){
  125 + //struct EP ep1;
  126 + dernier_PA++;
  127 + tab_PA[dernier_PA] = adresse_PA;
  128 + printf("Point d'accès numéro %d sauvegardé. Adresse : %d\n", dernier_PA, adresse_PA);
  129 +
  130 + /*
  131 + ep1.ep=config_desc->interface[indice_intf].altsetting[0].endpoint->bEndpointAddress;
  132 + if (indice_intf==0){
  133 + tab_pa.tableau[indice_intf]->ep=ep1.ep;
  134 + tab_pa.tableau[indice_intf]->io=1;
  135 + tab_pa.dernier++;
  136 + }
  137 + else{
  138 + tab_pa.tableau[indice_intf]->ep=ep1.ep;
  139 + tab_pa.tableau[indice_intf]->io=0;
  140 + tab_pa.dernier++;
  141 + if (num_ep >2)break; //entrée => io=0
  142 +
  143 + // Le if et else ci-dessus doivent pouvoir être simplifiés.
  144 + */
  145 + }
  146 + }
  147 + }
  148 +}
  149 +
  150 +
  151 +/* Libération des interfaces de la configuration active */
  152 +void liberer_interfaces(){
  153 +
  154 + /* Récupération de la configuration active */
  155 +
  156 + struct libusb_config_descriptor* config_desc;
  157 + int status =libusb_get_active_config_descriptor(found,&config_desc);
  158 + if(status!=0){perror("libusb_get_active_config_descriptor");exit(-1);}
  159 +
  160 + /* On parcourt toutes les interfaces*/
  161 + int num_intf;
  162 + for(int indice_intf=0;indice_intf < config_desc->bNumInterfaces ;indice_intf++){
  163 + num_intf=config_desc->interface[indice_intf].altsetting[0].bInterfaceNumber;
  164 +
  165 + /* Libération de l'interface num_intf*/
  166 + status=libusb_release_interface(handle,num_intf);
  167 + if(status!=0){ perror("libusb_release_interface"); exit(-1); }
  168 +
  169 + printf("L'interface numéro %d , d'indice %d a été libérée.\n", num_intf,indice_intf);
  170 + }
  171 +}
  172 +
  173 +
  174 +/* Envoie un caractère (de commande de LED) sur le port USB */
  175 +void send_data(unsigned char tab_PA[TAB_PA_SIZE], unsigned char data){
  176 +
  177 + unsigned char PA = tab_PA[0]; //LEDs sur le premier point d'accès
  178 + int *transferred = 1; //nombre d'octets transférés
  179 + unsigned int timeout = 1000; //temps avant un timeout
  180 + int status = libusb_interrupt_transfer(struct libusb_device_handle *handle, PA, &data, sizeof(data), &transferred, timeout)
  181 + if(status!=0){perror("libusb_interrupt_transfer");exit(-1);}
  182 +}
  183 +
  184 +
  185 +/* Lis le contenu des points d'accès de l'interface IN (boutons et joystick) */
  186 +void receive_data(unsigned char tab_PA[TAB_PA_SIZE], unsigned char *boutons, unsigned char *joystick_x, unsigned char *joystick_y){
  187 +
  188 + /* Lecture du point d'accès des boutons */
  189 + unsigned char PA = tab_PA[1]; //LEDs sur le premier point d'accès
  190 + int *transferred = 1; //nombre d'octets transférés
  191 + unsigned int timeout = 1000; //temps avant un timeout
  192 +
  193 + int status = libusb_interrupt_transfer(struct libusb_device_handle *handle, PA, &boutons, sizeof(boutons), &transferred, timeout)
  194 + if(status!=0){perror("libusb_interrupt_transfer");exit(-1);}
  195 +
  196 + /* Lecture du point d'accès du joystick */
  197 + unsigned char *joystick_xy; //stocke la donnée du point d'accès (1 octet pour chaque axe)
  198 +
  199 + PA = tab_PA[2]; //LEDs sur le premier point d'accès
  200 + *transferred = 2; //nombre d'octets transférés
  201 + timeout = 1000; //temps avant un timeout
  202 +
  203 + int status = libusb_interrupt_transfer(struct libusb_device_handle *handle, PA, &joystick_xy, sizeof(joystick_xy), &transferred, timeout)
  204 + if(status!=0){perror("libusb_interrupt_transfer");exit(-1);}
  205 +
  206 + //TODO Pas sûr !!!
  207 + *joystick_x = joystick_xy[0]; //On sépare la data de chaque axe
  208 + *joystick_y = joystick_xy[1];
  209 +}
  210 +
  211 +
  212 +
  213 +int main(){
  214 + //init_tableau(); // initialisation tableau point d'accès endpoint
  215 +
  216 + /* Initialisation de la bibliothèque libusb-1.0 */
  217 +
  218 + int status=libusb_init(&context);
  219 + if(status!=0) {perror("libusb_init"); exit(-1);}
  220 + //fin inititialisation
  221 +
  222 + /* Enumération des périphériques USB */
  223 + enum_periph();
  224 +
  225 + /* Ouverture de notre périphérique 'found' */
  226 + int status_ouv=libusb_open(found,&handle);
  227 + if(status_ouv!=0){ perror("libusb_open"); exit(-1); }
  228 +
  229 + /* Configuration du périphérique et sauvegarde des points d'accès */
  230 + char tab_PA[TAB_PA_SIZE];
  231 + config_periph(tab_PA);
  232 +
  233 + unsigned char *boutons;
  234 + unsigned char *joystick_x;
  235 + unsigned char *joystick_x;
  236 +
  237 + //boucle while(pas d'arrêt), envoi et rcpt
  238 + // "pas d'arrêt" = appui sur 's' par exemple
  239 +
  240 +
  241 + /* Libération des interfaces*/
  242 + liberer_interfaces();
  243 +
  244 + /* Fermeture du périphérique */
  245 + libusb_close(handle);
  246 +
  247 +
  248 +
  249 +
  250 + libusb_exit(context); //fermeture de la bibliothèque
  251 + return 0;
  252 +}
... ...
lufa-master/PolytechLille/PAD/Descriptors.c
... ... @@ -123,7 +123,7 @@ const USB_Descriptor_Configuration_t PROGMEM RelayBoard_ConfigurationDescriptor
123 123  
124 124 .EndpointAddress = KEYBOARD_OUT_EPADDR,
125 125 .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
126   - .EndpointSize = KEYBOARD_EPSIZE,
  126 + .EndpointSize = LED_EPSIZE,
127 127 .PollingIntervalMS = 0x05
128 128 },
129 129  
... ...
lufa-master/PolytechLille/PAD/Descriptors.h
... ... @@ -53,11 +53,11 @@
53 53 // Relay Board Interface
54 54 USB_Descriptor_Interface_t InterfaceOUT;
55 55 //USB_HID_Descriptor_HID_t HID_KeyboardHID; //UTILE ?
56   - USB_Descriptor_Endpoint_t HID_ReportOUTEndpoint;
  56 + USB_Descriptor_Endpoint_t ReportOUTEndpoint;
57 57  
58 58 USB_Descriptor_Interface_t InterfaceIN;
59   - USB_Descriptor_Endpoint_t HID_ReportINEndpointBoutons;
60   - USB_Descriptor_Endpoint_t HID_ReportOUTEndpointJoystick;
  59 + USB_Descriptor_Endpoint_t ReportINEndpointBoutons;
  60 + USB_Descriptor_Endpoint_t ReportOUTEndpointJoystick;
61 61 } USB_Descriptor_Configuration_t;
62 62  
63 63 /** Enum for the device interface descriptor IDs within the device. Each interface descriptor
... ... @@ -93,9 +93,9 @@
93 93 #define KEYBOARD_OUT_EPADDR (ENDPOINT_DIR_OUT | 2)
94 94  
95 95 /** Size in bytes of the Keyboard HID reporting IN and OUT endpoints. */
96   - #define KEYBOARD_EPSIZE 8
97   - #define BOUTONS_EPSIZE 8
98   - #define JOYSTICK_EPSIZE 16
  96 + #define LED_EPSIZE 1
  97 + #define BOUTONS_EPSIZE 1
  98 + #define JOYSTICK_EPSIZE 2
99 99  
100 100  
101 101 /* Function Prototypes: */
... ...
lufa-master/PolytechLille/PAD/PAD.h
... ... @@ -58,7 +58,7 @@
58 58 void EVENT_USB_Device_ConfigurationChanged(void);
59 59 void SendNextReport(void);
60 60 void ReceiveNextReport(void);
61   - void EVENT_USB_Device_ControlRequest(void);
  61 + //void EVENT_USB_Device_ControlRequest(void);
62 62  
63 63 #endif
64 64  
... ...