From 01b3f0af826369ec8732e353e824a0da70dc76c8 Mon Sep 17 00:00:00 2001 From: rguillom Date: Sun, 19 Jan 2020 23:22:54 +0100 Subject: [PATCH] PAD.c modif fonctions Send_EP_IN_Report + Receive_EP_OUT_Report + ajout Reception_Serie et PAD_Task Débugage (non terminé) Quelques fix supplémentaires --- Pgm PC/init_USB.c | 57 ++++++++++++++++++++++++++++++++++++++------------------- lufa-master/PolytechLille/PAD/Descriptors.c | 28 +++++++++------------------- lufa-master/PolytechLille/PAD/Descriptors.h | 15 ++++++++------- lufa-master/PolytechLille/PAD/PAD.c | 119 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------- lufa-master/PolytechLille/PAD/PAD.h | 9 +++++---- main.c | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------------- 6 files changed, 197 insertions(+), 132 deletions(-) diff --git a/Pgm PC/init_USB.c b/Pgm PC/init_USB.c index d148334..f9c71cc 100644 --- a/Pgm PC/init_USB.c +++ b/Pgm PC/init_USB.c @@ -55,7 +55,7 @@ void enum_periph(){ uint8_t address=libusb_get_device_address(device); printf("Device Found @ (Bus:Address) %d:%d\n",bus,address); printf("Vendor ID 0x0%x\n",desc.idVendor); - printf("Product ID 0x0%x\n",desc.idProduct); + printf("Product ID 0x0%x\n\n",desc.idProduct); /* Recherche de notre périphérique et sauvegarde dans 'found' */ if(desc.idVendor == VENDOR_ID && desc.idProduct == PRODUCT_ID){ @@ -74,7 +74,7 @@ void config_periph(char tab_PA[TAB_PA_SIZE]){ /* Récupération de la 1ère configuration du périphérique (indice 0) */ struct libusb_config_descriptor *config_desc=NULL; int status = libusb_get_config_descriptor(found,0,&config_desc); //config d'indice 0 - printf("ID de la configuration d'indice 0 : %d\n",config_desc->bConfigurationValue); + printf("La valeur de la configuration d'indice 0 est %d\n",config_desc->bConfigurationValue); int interface; @@ -95,7 +95,7 @@ void config_periph(char tab_PA[TAB_PA_SIZE]){ /* Utilisation de la configuration */ int configuration=config_desc->bConfigurationValue; - printf("valeur config %d\n", configuration); // Affichage valeur de la configuration + //printf("valeur config %d\n", configuration); // Affichage valeur de la configuration status=libusb_set_configuration(handle,configuration); if(status!=0){ perror("libusb_set_configuration"); exit(-1); } @@ -107,7 +107,7 @@ void config_periph(char tab_PA[TAB_PA_SIZE]){ status=libusb_claim_interface(handle,indice_intf); if(status!=0){ perror("libusb_claim_interface"); exit(-1); } - printf("Interface d'indice %d et de numéro %d réclamée\n",indice_intf,num_intf); + printf("\tInterface d'indice %d et de numéro %d réclamée\n",indice_intf,num_intf); @@ -116,7 +116,7 @@ void config_periph(char tab_PA[TAB_PA_SIZE]){ int type_PA = config_desc->interface[indice_intf].altsetting[0].endpoint[num_PA].bmAttributes; uint8_t adresse_PA = config_desc->interface[indice_intf].altsetting[0].endpoint[num_PA].bEndpointAddress; - printf("Point d'accès trouvé. Adresse :%d\n",adresse_PA); + printf("\t\tPoint d'accès trouvé. Adresse : %d\n",adresse_PA); /* Regarde si le point d'accès est de type interruption. Si oui, on le sauvegarde. */ @@ -125,7 +125,7 @@ void config_periph(char tab_PA[TAB_PA_SIZE]){ //struct EP ep1; dernier_PA++; tab_PA[dernier_PA] = adresse_PA; - printf("Point d'accès numéro %d sauvegardé. Adresse : %d\n", dernier_PA, adresse_PA); + printf("\t\tPoint d'accès numéro %d sauvegardé. Adresse : %d\n", dernier_PA, adresse_PA); /* ep1.ep=config_desc->interface[indice_intf].altsetting[0].endpoint->bEndpointAddress; @@ -166,7 +166,7 @@ void liberer_interfaces(){ status=libusb_release_interface(handle,num_intf); if(status!=0){ perror("libusb_release_interface"); exit(-1); } - printf("L'interface numéro %d , d'indice %d a été libérée.\n", num_intf,indice_intf); + printf("\tL'interface numéro %d, d'indice %d a été libérée.\n", num_intf,indice_intf); } } @@ -175,9 +175,9 @@ void liberer_interfaces(){ void send_data(unsigned char tab_PA[TAB_PA_SIZE], unsigned char data){ unsigned char PA = tab_PA[0]; //LEDs sur le premier point d'accès - int *transferred = 1; //nombre d'octets transférés + int transferred = 1; //nombre d'octets transférés unsigned int timeout = 1000; //temps avant un timeout - int status = libusb_interrupt_transfer(struct libusb_device_handle *handle, PA, &data, sizeof(data), &transferred, timeout) + int status = libusb_interrupt_transfer(handle, PA, &data, sizeof(data), &transferred, timeout); if(status!=0){perror("libusb_interrupt_transfer");exit(-1);} } @@ -187,25 +187,27 @@ void receive_data(unsigned char tab_PA[TAB_PA_SIZE], unsigned char *boutons, uns /* Lecture du point d'accès des boutons */ unsigned char PA = tab_PA[1]; //LEDs sur le premier point d'accès - int *transferred = 1; //nombre d'octets transférés + int transferred = 1; //nombre d'octets transférés unsigned int timeout = 1000; //temps avant un timeout - int status = libusb_interrupt_transfer(struct libusb_device_handle *handle, PA, &boutons, sizeof(boutons), &transferred, timeout) + int status = libusb_interrupt_transfer(handle, PA, boutons, sizeof(boutons), &transferred, timeout); if(status!=0){perror("libusb_interrupt_transfer");exit(-1);} /* Lecture du point d'accès du joystick */ - unsigned char *joystick_xy; //stocke la donnée du point d'accès (1 octet pour chaque axe) + unsigned char *joystick_xy=NULL; //stocke la donnée du point d'accès (1 octet pour chaque axe) PA = tab_PA[2]; //LEDs sur le premier point d'accès - *transferred = 2; //nombre d'octets transférés + transferred = 2; //nombre d'octets transférés timeout = 1000; //temps avant un timeout - int status = libusb_interrupt_transfer(struct libusb_device_handle *handle, PA, &joystick_xy, sizeof(joystick_xy), &transferred, timeout) + status = libusb_interrupt_transfer(handle, PA, joystick_xy, sizeof(joystick_xy), &transferred, timeout); if(status!=0){perror("libusb_interrupt_transfer");exit(-1);} //TODO Pas sûr !!! - *joystick_x = joystick_xy[0]; //On sépare la data de chaque axe - *joystick_y = joystick_xy[1]; + if (joystick_xy !=NULL){ + *joystick_x = joystick_xy[0]; //On sépare la data de chaque axe + *joystick_y = joystick_xy[1]; + } } @@ -230,13 +232,30 @@ int main(){ char tab_PA[TAB_PA_SIZE]; config_periph(tab_PA); - unsigned char *boutons; - unsigned char *joystick_x; - unsigned char *joystick_x; + unsigned char *boutons, *boutons_anc = NULL; + unsigned char *joystick_x, *joystick_x_anc = NULL; + unsigned char *joystick_y, *joystick_y_anc = NULL; + unsigned char caractere; + /* //boucle while(pas d'arrêt), envoi et rcpt // "pas d'arrêt" = appui sur 's' par exemple + + if (appui sur une touche){ + //récupération caractère + send_data(tab_PA, caractere); //Envoi de la commande des leds + } + else { + receive_data(tab_PA, boutons, joystick_x, joystick_y); //Réception des boutons et joystick + if ((*boutons != *boutons_anc) || (joystick_x != joystick_x_anc) || (joystick_y != joystick_y_anc)) printf("Boutons : %c, Joystick_x : %c, Joystick_y :%c\n", *boutons, *joystick_x, *joystick_y); //Affichage si changement + + *boutons_anc = *boutons; + *joystick_x_anc = *joystick_x; + *joystick_y_anc = *joystick_y; + } + + */ /* Libération des interfaces*/ liberer_interfaces(); diff --git a/lufa-master/PolytechLille/PAD/Descriptors.c b/lufa-master/PolytechLille/PAD/Descriptors.c index b2fed06..f07693e 100644 --- a/lufa-master/PolytechLille/PAD/Descriptors.c +++ b/lufa-master/PolytechLille/PAD/Descriptors.c @@ -38,6 +38,9 @@ #include "Descriptors.h" +#define VENDOR_ID 0x2341 +#define PRODUCT_ID 0x0001 + /** Device descriptor structure. This descriptor, located in FLASH memory, describes the overall * device characteristics, including the supported USB version, control endpoint size and the * number of device configurations. The descriptor is read out by the USB host when the enumeration @@ -54,8 +57,8 @@ const USB_Descriptor_Device_t PROGMEM RelayBoard_DeviceDescriptor = .Endpoint0Size = FIXED_CONTROL_ENDPOINT_SIZE, - .VendorID = 0x04B4, //A MODIFIER avec VendorID Carte - .ProductID = 0xFD11, //A MODIFIER + .VendorID = VENDOR_ID, + .ProductID = PRODUCT_ID, .ReleaseNumber = VERSION_BCD(2,0,0), .ManufacturerStrIndex = STRING_ID_Manufacturer, @@ -88,12 +91,12 @@ const USB_Descriptor_Configuration_t PROGMEM RelayBoard_ConfigurationDescriptor }, /* Description Interface SORTIE (LED)*/ - .InferfaceOUT = + .InterfaceOUT = { .Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface}, .InterfaceNumber = INTERFACE_ID_InterfaceOUT, - .AlternateSetting = 0, + .AlternateSetting = 0x00, .TotalEndpoints = 1, @@ -104,19 +107,6 @@ const USB_Descriptor_Configuration_t PROGMEM RelayBoard_ConfigurationDescriptor .InterfaceStrIndex = NO_DESCRIPTOR }, - /* - .HID_KeyboardHID = - { - .Header = {.Size = sizeof(USB_HID_Descriptor_HID_t), .Type = HID_DTYPE_HID}, - - .HIDSpec = VERSION_BCD(1,1,1), - .CountryCode = 0x00, - .TotalReportDescriptors = 1, - .HIDReportType = HID_DTYPE_Report, - .HIDReportLength = sizeof(KeyboardReport) - }, - */ - .ReportOUTEndpoint = { .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint}, @@ -133,7 +123,7 @@ const USB_Descriptor_Configuration_t PROGMEM RelayBoard_ConfigurationDescriptor { .Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface}, - .InterfaceNumber = INTERFACE_ID_InterfaceIN + .InterfaceNumber = INTERFACE_ID_InterfaceIN, .AlternateSetting = 0x00, .TotalEndpoints = 2, @@ -163,7 +153,7 @@ const USB_Descriptor_Configuration_t PROGMEM RelayBoard_ConfigurationDescriptor .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), .EndpointSize = JOYSTICK_EPSIZE, .PollingIntervalMS = 0x05 - } + }, }; /** Language descriptor structure. This descriptor, located in FLASH memory, is returned when the host requests diff --git a/lufa-master/PolytechLille/PAD/Descriptors.h b/lufa-master/PolytechLille/PAD/Descriptors.h index 56a83e2..d86a40e 100644 --- a/lufa-master/PolytechLille/PAD/Descriptors.h +++ b/lufa-master/PolytechLille/PAD/Descriptors.h @@ -52,7 +52,6 @@ // Relay Board Interface USB_Descriptor_Interface_t InterfaceOUT; - //USB_HID_Descriptor_HID_t HID_KeyboardHID; //UTILE ? USB_Descriptor_Endpoint_t ReportOUTEndpoint; USB_Descriptor_Interface_t InterfaceIN; @@ -84,15 +83,17 @@ /* Macros: */ - + /** Endpoint address of the Keyboard HID reporting OUT endpoint. */ + #define KEYBOARD_OUT_EPADDR (ENDPOINT_DIR_OUT | 1) + + /** Endpoint address of the Keyboard HID reporting IN endpoint. */ - #define KEYBOARD_IN_Bout_EPADDR (ENDPOINT_DIR_IN | 1) //Adr. EP Boutons - #define KEYBOARD_IN_Joy_EPADDR (ENDPOINT_DIR_IN | 1) //Adr. EP Joystick + #define KEYBOARD_IN_Bout_EPADDR (ENDPOINT_DIR_IN | 2) //Adr. EP Boutons (direction | numéro du point d'accès) + #define KEYBOARD_IN_Joy_EPADDR (ENDPOINT_DIR_IN | 3) //Adr. EP Joystick - /** Endpoint address of the Keyboard HID reporting OUT endpoint. */ - #define KEYBOARD_OUT_EPADDR (ENDPOINT_DIR_OUT | 2) - /** Size in bytes of the Keyboard HID reporting IN and OUT endpoints. */ + + /** Size in bytes of the PAD HID reporting IN and OUT endpoints. */ #define LED_EPSIZE 1 #define BOUTONS_EPSIZE 1 #define JOYSTICK_EPSIZE 2 diff --git a/lufa-master/PolytechLille/PAD/PAD.c b/lufa-master/PolytechLille/PAD/PAD.c index 562f122..274eb73 100644 --- a/lufa-master/PolytechLille/PAD/PAD.c +++ b/lufa-master/PolytechLille/PAD/PAD.c @@ -38,6 +38,13 @@ #include "PAD.h" +#define debut_rcpt 0xff //valeur arbitraire pour détecter un début de trame + +uint8_t boutons; +uint8_t joystick_x; +uint8_t joystick_y; + + /** Main program entry point. This routine contains the overall program flow, including initial * setup of all components and the main program loop. */ @@ -58,23 +65,23 @@ int main(void) /** Configures the board hardware and chip peripherals for the project's functionality. */ void SetupHardware(void) { -#if (ARCH == ARCH_AVR8) + #if (ARCH == ARCH_AVR8) /* Disable watchdog if enabled by bootloader/fuses */ MCUSR &= ~(1 << WDRF); wdt_disable(); /* Disable clock division */ clock_prescale_set(clock_div_1); - + #endif -/**INITIALISATIONS*/ + /**INITIALISATIONS*/ /* Hardware Initialization */ USB_Init(); - Serial_init(9600,0); //9600 Bauds, configuration + Serial_Init(9600,0); //9600 Bauds, configuration /* Initialize Relays */ - DDRD |= ALL_RELAYS; // Port pour la liaison série - PORTD &= ~ALL_RELAYS; + //DDRD |= ALL_RELAYS; // Port pour la liaison série + //PORTD &= ~ALL_RELAYS; } @@ -85,10 +92,11 @@ void EVENT_USB_Device_ConfigurationChanged(void) { bool ConfigSuccess = true; - /* Setup HID Report Endpoints */ //MODIFS ICI - ConfigSuccess &= Endpoint_ConfigureEndpoint(KEYBOARD_IN_Bout_EPADDR, EP_TYPE_INTERRUPT, BOUTONS_EPSIZE, 1); + /* Setup HID Report Endpoints */ + ConfigSuccess &= Endpoint_ConfigureEndpoint(KEYBOARD_OUT_EPADDR, EP_TYPE_INTERRUPT, LED_EPSIZE, 1); + ConfigSuccess &= Endpoint_ConfigureEndpoint(KEYBOARD_IN_Bout_EPADDR, EP_TYPE_INTERRUPT, BOUTONS_EPSIZE, 1); ConfigSuccess &= Endpoint_ConfigureEndpoint(KEYBOARD_IN_Joy_EPADDR, EP_TYPE_INTERRUPT, JOYSTICK_EPSIZE, 1); - ConfigSuccess &= Endpoint_ConfigureEndpoint(KEYBOARD_OUT_EPADDR, EP_TYPE_INTERRUPT, KEYBOARD_EPSIZE, 1); + /* Turn on Start-of-Frame events for tracking HID report period expiry */ USB_Device_EnableSOFEvents(); @@ -98,70 +106,77 @@ void EVENT_USB_Device_ConfigurationChanged(void) } -/** Sends the next HID report to the host, via the keyboard data endpoint. */ -void SendNextReport(void) //Envoi liaison Série + +/** Sends the next PAD report to the host, via the buttons and joystick endpoint. */ +void Send_EP_IN_Report(void) { - static USB_KeyboardReport_Data_t PrevKeyboardReportData; - USB_KeyboardReport_Data_t KeyboardReportData; - bool SendReport = false; + //static USB_KeyboardReport_Data_t PrevKeyboardReportData; + //USB_KeyboardReport_Data_t KeyboardReportData; + //bool SendReport = false; /* Create the next keyboard report for transmission to the host */ - CreateKeyboardReport(&KeyboardReportData); + //CreateKeyboardReport(&KeyboardReportData); /* Check if the idle period is set and has elapsed */ - if (IdleCount && (!(IdleMSRemaining))) - { + //if (IdleCount && (!(IdleMSRemaining))) + //{ /* Reset the idle time remaining counter */ - IdleMSRemaining = IdleCount; + // IdleMSRemaining = IdleCount; /* Idle period is set and has elapsed, must send a report to the host */ - SendReport = true; - } - else - { + //SendReport = true; + //} + //else + //{ /* Check to see if the report data has changed - if so a report MUST be sent */ - SendReport = (memcmp(&PrevKeyboardReportData, &KeyboardReportData, sizeof(USB_KeyboardReport_Data_t)) != 0); - } + //SendReport = (memcmp(&PrevKeyboardReportData, &KeyboardReportData, sizeof(USB_KeyboardReport_Data_t)) != 0); + //} /** Envoi sur INTERFACE BOUTONS**/ - /* Select the Keyboard Report Endpoint */ + /* Select the Buttons Report Endpoint */ Endpoint_SelectEndpoint(KEYBOARD_IN_Bout_EPADDR); /* Check if Keyboard Endpoint Ready for Read/Write and if we should send a new report */ - if (Endpoint_IsReadWriteAllowed() && SendReport) + if (Endpoint_IsReadWriteAllowed() && Endpoint_IsINReady) { /* Save the current report data for later comparison to check for changes */ - PrevKeyboardReportData = KeyboardReportData; + //PrevKeyboardReportData = KeyboardReportData; /* Write Keyboard Report Data */ - Endpoint_Write_Stream_LE(&KeyboardReportData, sizeof(KeyboardReportData), NULL); + //Endpoint_Write_Stream_LE(&KeyboardReportData, sizeof(KeyboardReportData), NULL); + Endpoint_Write_8(boutons); /* Finalize the stream transfer to send the last packet */ Endpoint_ClearIN(); } + else printf("Buttons endpoint not ready to send\n"); /** Envoi sur INTERFACE Joystick**/ /* Select the Keyboard Report Endpoint */ Endpoint_SelectEndpoint(KEYBOARD_IN_Joy_EPADDR); /* Check if Keyboard Endpoint Ready for Read/Write and if we should send a new report */ - if (Endpoint_IsReadWriteAllowed() && SendReport) + if (Endpoint_IsReadWriteAllowed() && Endpoint_IsINReady) { /* Save the current report data for later comparison to check for changes */ - PrevKeyboardReportData = KeyboardReportData; + //PrevKeyboardReportData = KeyboardReportData; /* Write Keyboard Report Data */ - Endpoint_Write_Stream_LE(&KeyboardReportData, sizeof(KeyboardReportData), NULL); + //Endpoint_Write_Stream_LE(&KeyboardReportData, sizeof(KeyboardReportData), NULL); + Endpoint_Write_8(joystick_x); + Endpoint_Write_8(joystick_y); /* Finalize the stream transfer to send the last packet */ Endpoint_ClearIN(); } + else printf("Joystick endpoint not ready to send\n"); } + /** Reads the next LED status report from the host from the LED data endpoint, if one has been sent. */ -void ReceiveNextReport(void) //Réception Liaison Série +void Receive_EP_OUT_Report(void) //Réception Liaison Série { /* Select the Keyboard LED Report Endpoint */ Endpoint_SelectEndpoint(KEYBOARD_OUT_EPADDR); @@ -175,11 +190,47 @@ void ReceiveNextReport(void) //Réception Liaison Série /* Read in the LED report from the host */ uint8_t LEDReport = Endpoint_Read_8(); - /* Process the read LED report from the host */ - ProcessLEDReport(LEDReport); + /* On envoie l'octet de commande de led sur la liaison série */ + Serial_SendByte(LEDReport); } + else printf("LED endpoint not ready to receive\n"); /* Handshake the OUT Endpoint - clear endpoint and ready for next report */ Endpoint_ClearOUT(); } } + + + +void Reception_Serie(void){ + if(Serial_IsCharReceived()){ // A la réception d'un caractère... + /* on vérifie qu'on a bien un début de trame*/ + unsigned char caractere = Serial_ReceiveByte(); + + /* si oui, on récupère les trois octets suivants*/ + if (caractere==debut_rcpt){ + boutons = Serial_ReceiveByte(); + joystick_x = Serial_ReceiveByte(); + joystick_y = Serial_ReceiveByte(); + } + } +} + + +/** Function to manage PAD report generation and transmission to the host, when in report mode. */ +void PAD_Task(void) +{ + /* Device must be connected and configured for the task to run */ + if (USB_DeviceState != DEVICE_STATE_Configured) + return; + + Reception_Serie(); //Réception des données de la liaison série + + /* Send the next button/joystick report to the host */ + Send_EP_IN_Report(); + + /* Process the LED report sent from the host */ + Receive_EP_OUT_Report(); +} + + diff --git a/lufa-master/PolytechLille/PAD/PAD.h b/lufa-master/PolytechLille/PAD/PAD.h index 35535e0..87ab5c5 100644 --- a/lufa-master/PolytechLille/PAD/PAD.h +++ b/lufa-master/PolytechLille/PAD/PAD.h @@ -46,9 +46,9 @@ #include "Descriptors.h" //#include - //#include //inventé? #include #include + #include "LUFA/Drivers/Peripheral/Serial.h" /* Macros: */ @@ -56,9 +56,10 @@ /* Function Prototypes: */ void SetupHardware(void); void EVENT_USB_Device_ConfigurationChanged(void); - void SendNextReport(void); - void ReceiveNextReport(void); - //void EVENT_USB_Device_ControlRequest(void); + void Send_EP_IN_Report(void); + void Receive_EP_OUT_Report(void); + void Reception_Serie(void); + void PAD_Task(void); #endif diff --git a/main.c b/main.c index 0893850..c8fa00d 100644 --- a/main.c +++ b/main.c @@ -3,9 +3,10 @@ // For the serial port -#define CPU_FREQ 16000000L // Assume a CPU frequency of 16Mhz -#define tempo 25 -#define debit 9600 //débit liaison série en bauds +#define CPU_FREQ 16000000L // Assume a CPU frequency of 16Mhz +#define tempo 25 +#define debit 9600 //débit liaison série en bauds +#define debut_serial_tx 0xff //pour détecter la transmission d'une trame pour le 16u2 void init_serial(int speed) { @@ -24,40 +25,40 @@ UCSR0A &= ~(1 << U2X0); void send_serial(unsigned char c) { -loop_until_bit_is_set(UCSR0A, UDRE0); -UDR0 = c; + loop_until_bit_is_set(UCSR0A, UDRE0); + UDR0 = c; } unsigned char get_serial(void) { -loop_until_bit_is_set(UCSR0A, RXC0); -return UDR0; + loop_until_bit_is_set(UCSR0A, RXC0); + return UDR0; } // For the AD converter void ad_init(unsigned char channel) { -ADCSRA|=(1<