Commit bcd81fea9a08aa46f80fc700d4b148cad9f495fc

Authored by dmohamed
1 parent 4474f7fc

UPDATE-AJout des programmes gérant le driver usb

USB/Makefile 0 → 100644
... ... @@ -0,0 +1,25 @@
  1 +
  2 +export CC = gcc
  3 +
  4 +export LDFLAGS = -l usb-1.0
  5 +
  6 +
  7 +TARGET = usb_driver
  8 +
  9 +
  10 +
  11 +C_SRC = $(wildcard *.c)
  12 +OBJS = $(C_SRC:.c=.o)
  13 +
  14 +all: $(TARGET)
  15 +
  16 +clean:
  17 + rm -f *.o
  18 +
  19 +
  20 +%.o:%.c
  21 + $(CC) -c $< -o $@
  22 +
  23 +$(TARGET): $(OBJS)
  24 + $(CC) -o $@ $(OBJS) $(LDFLAGS)
  25 +
... ...
USB/Makefile~ 0 → 100644
... ... @@ -0,0 +1,5 @@
  1 +tutorat : tutorat.o
  2 + gcc -o tutorat tutorat.o -l usb-1.0
  3 +
  4 +tutorat.o : tutorat.c
  5 + gcc -c tutorat.c
... ...
USB/usb_driver 0 → 100755
No preview for this file type
USB/usb_driver.c 0 → 100644
... ... @@ -0,0 +1,403 @@
  1 +#include <libusb-1.0/libusb.h> //bibliothèque USB
  2 +#include <stdio.h>
  3 +#include <stdlib.h>
  4 +#include <termios.h>
  5 +#include <fcntl.h>
  6 +#include <strings.h>
  7 +#include <unistd.h>
  8 +
  9 +
  10 +libusb_device **list;
  11 +libusb_device_handle *handle = NULL;
  12 +libusb_device *device;
  13 +struct termios termios_p;
  14 +
  15 +
  16 +int init_serial(char *device,int speed) //Initialisation du port série
  17 +{
  18 +int fd=open(device,O_RDWR);
  19 +if(fd<0){perror(device); exit(-1);}
  20 +//Lecture des parametres courants
  21 +tcgetattr(fd,&termios_p);
  22 +//On ignore les BREAK et les caracteres avec erreurs de parite
  23 +termios_p.c_iflag = IGNBRK | IGNPAR;
  24 +//Pas de mode de sortie particulier
  25 +termios_p.c_oflag = 0;
  26 +//Liaison a 9600 bps avec 7 bits de donnees et une parite paire
  27 +termios_p.c_cflag = B9600 | CS7 | PARENB;
  28 +//Mode non-canonique avec echo
  29 +termios_p.c_lflag = ECHO;
  30 +//Caracteres immediatement disponibles
  31 +termios_p.c_cc[VMIN] = 1;
  32 +termios_p.c_cc[VTIME] = 0;
  33 +//Sauvegarde des nouveaux parametres
  34 +tcsetattr(fd,TCSANOW,&termios_p);
  35 +return fd;
  36 +}
  37 +
  38 +
  39 +void close_serial(int fd) //Fermeture du port série
  40 +{
  41 +tcsetattr(fd,TCSANOW,&termios_p);
  42 +close(fd);
  43 +}
  44 +
  45 +
  46 +void callback(struct libusb_transfer *transf) //Fonction pour le transfert des instructions à la tourelle
  47 +{
  48 + libusb_fill_control_setup(transf->buffer, 0xa1, 0x01, 0x300, 0x00, 0);
  49 + libusb_fill_control_transfer(transf, transf->dev_handle, transf->buffer, NULL, NULL, 1000);
  50 + fflush(stdout);
  51 + libusb_free_transfer(transf); //on libere la structure de transfert
  52 +}
  53 +
  54 +void configuration_periph(libusb_device *device) //Configuration de la tourelle
  55 +{
  56 + //Ouverture du pepherique
  57 + int status=libusb_open(device,&handle);
  58 + if(status!=0)
  59 + {
  60 + perror("libusb_open"); exit(-1);
  61 + }
  62 +
  63 + //Recuperation de la configuration d'indice 0 du périphérique
  64 + struct libusb_config_descriptor *config;
  65 + status=libusb_get_active_config_descriptor(device,&config);
  66 + if(status!=0)
  67 + {
  68 + perror("libusb_get_active_config_descriptor"); exit(-1);
  69 + }
  70 +
  71 + //Interfaces
  72 + int i;
  73 + int interface;
  74 +
  75 + //Probleme noyau
  76 + for(i=0;i<(config->bNumInterfaces);i++)
  77 + {
  78 + if(libusb_kernel_driver_active(handle,interface))
  79 + {
  80 + status=libusb_detach_kernel_driver(handle,interface);
  81 + if(status!=0)
  82 + {
  83 + perror("libusb_detach_kernel_driver"); exit(-1);
  84 + }
  85 + }
  86 + }
  87 +
  88 + //Utilisation d'une configuration du périphérique
  89 + int configuration=config->bConfigurationValue;
  90 + printf("%d\n", configuration);
  91 +/*
  92 + status=libusb_set_configuration(handle,configuration);
  93 +
  94 +
  95 + if(status!=0)
  96 + {
  97 + perror("libusb_set_configuration"); exit(-1);
  98 + }
  99 +
  100 + //Claim interfaces
  101 + for(i=0;i<(config->bNumInterfaces);i++)
  102 + {
  103 + interface=config->interface[i].altsetting[0].bInterfaceNumber;
  104 + printf("Numero d'interface : %d\n", interface);
  105 + status=libusb_claim_interface(handle,interface);
  106 + if(status!=0)
  107 + {
  108 + perror("libusb_claim_interface"); exit(-1);
  109 + }
  110 + }*/
  111 +}
  112 +
  113 +
  114 +void fermeture(libusb_device_handle *handle) //Fermeture du périphérique
  115 +{
  116 + int i, interface, status;
  117 + libusb_device *device = libusb_get_device(handle);
  118 + struct libusb_config_descriptor *config;
  119 + status = libusb_get_active_config_descriptor(device, &config);
  120 + if(status != 0)
  121 + {
  122 + perror("libusb_get_active_config_descriptor");exit(-1);
  123 + }
  124 +
  125 + for(i=0;i<(config->bNumInterfaces);i++)
  126 + {
  127 + interface=config->interface[i].altsetting[0].bInterfaceNumber;
  128 + status=libusb_release_interface(handle,interface); //On libère l'interface
  129 + if(status!=0)
  130 + {
  131 + perror("libusb_release_interface");
  132 + exit(-1);
  133 + }
  134 + printf("Interface liberee : %d\n",interface);
  135 + }
  136 +
  137 + libusb_close(handle);
  138 + }
  139 +
  140 +
  141 +void enumeration(libusb_context *context) //Enumération des périphériques USB
  142 +{
  143 + int t=0;
  144 + libusb_device **list;
  145 + ssize_t count=libusb_get_device_list(context,&list);
  146 + if(count<0)
  147 + {
  148 + perror("libusb_get_device_list"); exit(-1);
  149 + }
  150 + ssize_t i=0;
  151 + for(i=0;i<count;i++)
  152 + {
  153 + libusb_device *device=list[i];
  154 + struct libusb_device_descriptor desc;
  155 + int status=libusb_get_device_descriptor(device,&desc);
  156 + if(status!=0) continue;
  157 + uint8_t bus=libusb_get_bus_number(device);
  158 + uint8_t address=libusb_get_device_address(device);
  159 + printf("Device Found @ (Bus:Address) %d:%d\n",bus,address);
  160 + printf("Vendor ID 0x0%x\n",desc.idVendor);
  161 + printf("Product ID 0x0%x\n",desc.idProduct);
  162 +
  163 + if ((desc.idVendor==0x0010) && (desc.idProduct==0x1010)) //si la tourelle est trouvee parmis les peripheriques USB, on lance sa configuration
  164 + {
  165 + printf("Tourelle trouvée !\n");
  166 + t=1;
  167 + configuration_periph(device);
  168 + }
  169 + }
  170 + if(t==0) printf("Tourelle non connectée !\n");
  171 +}
  172 +
  173 +
  174 +void tir(libusb_device_handle *handle) //On fait appel à cette fonction quand on désire tirer dans la fonction commande
  175 +{
  176 + unsigned char tab[13];
  177 + struct libusb_transfer *transf;
  178 +
  179 + //1ère requête
  180 + transf = libusb_alloc_transfer(0);
  181 + libusb_fill_control_setup(tab, 0x21, 0x09, 0x300, 0x00, 13);
  182 + tab[12] = 0x5e;
  183 + tab[11] = 0x00;
  184 + tab[10] = 0x00;
  185 + tab[8] = 0x00;
  186 + tab[9] = 0x00;
  187 +
  188 + //Structure de transfert
  189 + libusb_fill_control_transfer(transf, handle, tab, callback, NULL, 0);
  190 + //Envoi sur le port serie
  191 + libusb_submit_transfer(transf);
  192 +
  193 + //2ème requête
  194 + transf = libusb_alloc_transfer(0);
  195 + libusb_fill_control_setup(tab, 0x21, 0x09, 0x300, 0x00, 13);
  196 + tab[12] = 0x5c;
  197 + tab[11] = 0x00;
  198 + tab[10] = 0x00;
  199 + tab[8] = 0x00;
  200 + tab[9] = 0x00;
  201 +
  202 + //Structure de transfert
  203 + libusb_fill_control_transfer(transf, handle, tab, callback, NULL, 0);
  204 + //Envoi sur le port serie
  205 + libusb_submit_transfer(transf);
  206 +
  207 + //Tir
  208 + transf = libusb_alloc_transfer(0);
  209 + libusb_fill_control_setup(tab, 0x21, 0x09, 0x300, 0x00, 13);
  210 + tab[12] = 0xfe;
  211 + tab[11] = 0xff;
  212 + tab[10] = 0xe0;
  213 + tab[8] = 0x5f;
  214 + tab[9] = 0x10;
  215 +
  216 + //Structure de transfert
  217 + libusb_fill_control_transfer(transf, handle, tab, callback, NULL, 0);
  218 + //Envoi sur le port serie
  219 + libusb_submit_transfer(transf);
  220 +}
  221 +
  222 +
  223 +void commande(char action, libusb_device_handle *handle) //Cette fonction permet de commander les déplacements et tirs de la tourelle
  224 +{
  225 + char cmd;
  226 +
  227 + //Declaration de la structure d'envoi pour les données de la requette
  228 + struct libusb_transfer *transf;
  229 + unsigned char tab[13];
  230 + transf = libusb_alloc_transfer(0);
  231 +
  232 + //Paramètres des requêtes
  233 + libusb_fill_control_setup(tab, 0x21, 0x09, 0x300, 0x00, 13);
  234 +
  235 + switch(action)
  236 + {
  237 + case 'g': //déplacement à gauche
  238 + cmd=0x04;
  239 + break;
  240 + case 'd': //déplacement à droite
  241 + cmd=0x08;
  242 + break;
  243 + case 'h': //déplacement en haut
  244 + cmd=0x02;
  245 + break;
  246 + case 'b': //déplacement en bas
  247 + cmd=0x01;
  248 + break;
  249 + case 'a': //arrêt
  250 + cmd=0x00;
  251 + break;
  252 + case 't': //tir
  253 + cmd=0x10;
  254 + break;
  255 + default :
  256 + printf("Commande invalide\n");
  257 + break;
  258 + }
  259 +
  260 + if(cmd==0x10)//Si l'on veut tirer
  261 + {
  262 + tir(handle); //Les requêtes sont différentes pour la commande de tir (voir la fonction dédiée)
  263 + }
  264 + else
  265 + {
  266 + //Données de la requette à envoyer à la tourelle pour les mouvements. tab[9] correspond à l'action à effectuer (gauche, droite, haut, bas ou arrêt)
  267 + tab[12] = 0xfe;
  268 + tab[11] = 0xff;
  269 + tab[10] = 0xe0;
  270 + tab[8] = 0x5f;
  271 + tab[9] = cmd;
  272 +
  273 + //Structure de transfert
  274 + libusb_fill_control_transfer(transf, handle, tab, callback, NULL, 0);
  275 + //Envoi sur le port serie
  276 + libusb_submit_transfer(transf);
  277 + }
  278 +}
  279 +
  280 +
  281 +int main()
  282 +{
  283 + //Initialisation de la bibliotheque libusb
  284 + libusb_context *context;
  285 +
  286 + int status=libusb_init(&context);
  287 + if(status!=0)
  288 + {
  289 + perror("libusb_init"); exit(-1);
  290 + }
  291 +
  292 + enumeration(context); //Enumération des périphériques USB. Si la tourelle est trouvée, cette fonction fait appel à la fonction configuration_periph qui va configurer la tourelle.
  293 +
  294 + //Initialisation du port serie
  295 + char *device="/dev/ttyACM0";
  296 + int fd=init_serial(device,B9600); //Vitesse 9600 bauds
  297 + printf("port init\n");
  298 +
  299 +
  300 +//close_serial(fd);
  301 + //Menu de commande
  302 + char action, choix;
  303 + printf("\nPilotage de la tourelle\n");
  304 + printf("------------------------\n");
  305 + printf("Taper 0 pour piloter la tourelle au clavier ou 1 pour la piloter avec l'arduino : ");
  306 + scanf("%c",&choix);
  307 +
  308 + if(choix=='0')
  309 + {
  310 + printf("\nPilotage au clavier\n");
  311 + printf("Taper d pour droite, g pour gauche, h pour haut, b pour bas, t pour tirer, s pour sortir du programme :\n");
  312 + }
  313 + if(choix=='1')
  314 + {
  315 + printf("\nPilotage avec l'arduino\n");
  316 + printf("Pour diriger la tourelle, utiliser le joystick, pour tirer appuyer dessus et pour sortir du programme appuyer sur D3.\n");
  317 + }
  318 +
  319 + for(;;) //Boucle d'éxécution
  320 + {
  321 + if(choix=='0') //Pilotage au clavier
  322 + {
  323 + while(getchar() != '\n');
  324 + scanf("%c",&action);
  325 + switch (action)
  326 + {
  327 + case 's':
  328 + fermeture(handle);
  329 + close_serial(fd);
  330 + libusb_free_device_list(list,1);
  331 + libusb_exit(context);
  332 + return 0;
  333 + break;
  334 + case 'd':
  335 + commande(action, handle);
  336 + sleep(1);
  337 + commande('a',handle);
  338 + break;
  339 + case 'g':
  340 + commande(action, handle);
  341 + sleep(1);
  342 + commande('a',handle);
  343 + break;
  344 + case 'h':
  345 + commande(action, handle);
  346 + sleep(1);
  347 + commande('a',handle);
  348 + break;
  349 + case 'b':
  350 + commande(action, handle);
  351 + sleep(1);
  352 + commande('a',handle);
  353 + break;
  354 + case 't' :
  355 + commande(action, handle);
  356 + sleep(1);
  357 + commande('a',handle);
  358 + commande(action, handle);
  359 + sleep(1);
  360 + commande('a',handle);
  361 + commande(action, handle);
  362 + sleep(1);
  363 + commande('a',handle);
  364 + break;
  365 + default :
  366 + printf("Commande invalide ! Retaper :\n");
  367 + break;
  368 + }
  369 + }
  370 +
  371 + if(choix=='1') //Pilotage avec l'arduino
  372 + {
  373 + read(fd,&action,1); //On lit sur le port série
  374 + printf("%c\n",action);
  375 + write(fd,"o",1); //On envoie le caractère 'o' sur le port série en réponse
  376 + commande(action,handle); //Permet de déplacer la tourelle ou d'arreter ses mouvements selon le caractère reçu (d,g,b,h ou a)
  377 + if(action=='t') //Si l'on reçoit l'ordre de tirer
  378 + {
  379 + commande(action, handle);
  380 + sleep(1);
  381 + commande('a',handle);
  382 + commande(action, handle);
  383 + sleep(1);
  384 + commande('a',handle);
  385 + commande(action, handle);
  386 + sleep(1);
  387 + commande('a',handle);
  388 + }
  389 + if(action=='s') //Si l'on reçoit le caractère 's' on ferme le programme
  390 + {
  391 + fermeture(handle);
  392 + close_serial(fd);
  393 + libusb_free_device_list(list,1);
  394 + libusb_exit(context);
  395 + return 0;
  396 + }
  397 +
  398 + }
  399 +
  400 + }
  401 +
  402 + return 0;
  403 +}
... ...
gadget/Descriptors.c
... ... @@ -191,7 +191,7 @@ const USB_Descriptor_String_t PROGMEM ManufacturerString = USB_STRING_DESCRIPTOR
191 191 * and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
192 192 * Descriptor.
193 193 */
194   -const USB_Descriptor_String_t PROGMEM ProductString = USB_STRING_DESCRIPTOR(L"LUFA USB-RS232 Adapter");
  194 +const USB_Descriptor_String_t PROGMEM ProductString = USB_STRING_DESCRIPTOR(L"USB Gadget");
195 195  
196 196 /** This function is called by the library when in device mode, and must be overridden (see library "USB Descriptors"
197 197 * documentation) by the application code so that the address and size of a requested descriptor can be given
... ...