From 752b62a91c77eefced7c5d89ab5bbfc45d17c0f0 Mon Sep 17 00:00:00 2001 From: rguillom Date: Wed, 15 Jan 2020 15:01:36 +0100 Subject: [PATCH] Descriptors + PAD en cours --- lufa-master/PolytechLille/PAD/Descriptors.c | 8 +++++--- lufa-master/PolytechLille/PAD/Descriptors.h | 21 ++++++++++++++++++--- lufa-master/PolytechLille/PAD/PAD.c | 176 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------------------------------------------------- lufa-master/PolytechLille/PAD/PAD.h | 13 ++++++------- 4 files changed, 134 insertions(+), 84 deletions(-) diff --git a/lufa-master/PolytechLille/PAD/Descriptors.c b/lufa-master/PolytechLille/PAD/Descriptors.c index bd72ec7..9ff8aef 100644 --- a/lufa-master/PolytechLille/PAD/Descriptors.c +++ b/lufa-master/PolytechLille/PAD/Descriptors.c @@ -104,6 +104,7 @@ 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}, @@ -114,6 +115,7 @@ const USB_Descriptor_Configuration_t PROGMEM RelayBoard_ConfigurationDescriptor .HIDReportType = HID_DTYPE_Report, .HIDReportLength = sizeof(KeyboardReport) }, + */ .ReportOUTEndpoint = { @@ -147,17 +149,17 @@ const USB_Descriptor_Configuration_t PROGMEM RelayBoard_ConfigurationDescriptor { .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint}, - .EndpointAddress = KEYBOARD_IN_EPADDR, + .EndpointAddress = KEYBOARD_IN_Bout_EPADDR, .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), .EndpointSize = KEYBOARD_EPSIZE, .PollingIntervalMS = 0x05 }, - .ReportINEndpointJoysticks = + .ReportINEndpointJoystick = { .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint}, - .EndpointAddress = KEYBOARD_IN_EPADDR, + .EndpointAddress = KEYBOARD_IN_Joy_EPADDR, .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), .EndpointSize = KEYBOARD_EPSIZE, .PollingIntervalMS = 0x05 diff --git a/lufa-master/PolytechLille/PAD/Descriptors.h b/lufa-master/PolytechLille/PAD/Descriptors.h index 46cfbad..23f6c0f 100644 --- a/lufa-master/PolytechLille/PAD/Descriptors.h +++ b/lufa-master/PolytechLille/PAD/Descriptors.h @@ -52,12 +52,12 @@ // Relay Board Interface USB_Descriptor_Interface_t InterfaceOUT; - USB_HID_Descriptor_HID_t HID_KeyboardHID; //UTILE ? + //USB_HID_Descriptor_HID_t HID_KeyboardHID; //UTILE ? USB_Descriptor_Endpoint_t HID_ReportOUTEndpoint; USB_Descriptor_Interface_t InterfaceIN; USB_Descriptor_Endpoint_t HID_ReportINEndpointBoutons; - USB_Descriptor_Endpoint_t HID_ReportOUTEndpointJoysticks; + USB_Descriptor_Endpoint_t HID_ReportOUTEndpointJoystick; } USB_Descriptor_Configuration_t; /** Enum for the device interface descriptor IDs within the device. Each interface descriptor @@ -66,7 +66,8 @@ */ enum InterfaceDescriptors_t { - INTERFACE_ID_RelayBoard = 0, /**< Relay board interface descriptor ID */ + INTERFACE_ID_InterfaceOUT = 0, /**< Relay board interface descriptor ID */ + INTERFACE_ID_InterfaceIN = 1, }; /** Enum for the device string descriptor IDs within the device. Each string descriptor should @@ -81,6 +82,20 @@ STRING_ID_Serial = 3, /**< Serial number string ID */ }; + /* Macros: */ + + + /** 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 + + /** 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. */ + #define KEYBOARD_EPSIZE 8 + + /* Function Prototypes: */ uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue, const uint16_t wIndex, diff --git a/lufa-master/PolytechLille/PAD/PAD.c b/lufa-master/PolytechLille/PAD/PAD.c index 87f4608..5f82928 100644 --- a/lufa-master/PolytechLille/PAD/PAD.c +++ b/lufa-master/PolytechLille/PAD/PAD.c @@ -53,7 +53,7 @@ int main(void) USB_USBTask(); //HID_Task(); } - } +} /** Configures the board hardware and chip peripherals for the project's functionality. */ void SetupHardware(void) @@ -65,87 +65,121 @@ void SetupHardware(void) /* Disable clock division */ clock_prescale_set(clock_div_1); -#endif -//TODO INITIALISATIONS +/**INITIALISATIONS*/ /* Hardware Initialization */ USB_Init(); + Serial_init(9600,0); //9600 Bauds, configuration /* Initialize Relays */ - DDRC |= ALL_RELAYS; - PORTC &= ~ALL_RELAYS; + DDRD |= ALL_RELAYS; // Port pour la liaison série + PORTD &= ~ALL_RELAYS; } -/** Event handler for the library USB Control Request reception event. */ -void EVENT_USB_Device_ControlRequest(void) + +/** Event handler for the USB_ConfigurationChanged event. This is fired when the host sets the current configuration + * of the USB device after enumeration, and configures the keyboard device endpoints. + */ +void EVENT_USB_Device_ConfigurationChanged(void) { - const uint8_t SerialNumber[5] = { 0, 0, 0, 0, 1 }; - uint8_t ControlData[2] = { 0, 0 }; + bool ConfigSuccess = true; + + /* Setup HID Report Endpoints */ //MODIFS ICI + ConfigSuccess &= Endpoint_ConfigureEndpoint(KEYBOARD_IN_Bout_EPADDR, EP_TYPE_INTERRUPT, KEYBOARD_EPSIZE, 1); + ConfigSuccess &= Endpoint_ConfigureEndpoint(KEYBOARD_IN_Joy_EPADDR, EP_TYPE_INTERRUPT, KEYBOARD_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(); + + /* Indicate endpoint configuration success or failure */ + //LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR); +} + + +/** Sends the next HID report to the host, via the keyboard data endpoint. */ +void SendNextReport(void) //Envoi liaison Série +{ + 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); + + /* Check if the idle period is set and has elapsed */ + if (IdleCount && (!(IdleMSRemaining))) + { + /* Reset the idle time remaining counter */ + IdleMSRemaining = IdleCount; + + /* Idle period is set and has elapsed, must send a report to the host */ + 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); + } + + + /** Envoi sur INTERFACE BOUTONS**/ + /* Select the Keyboard 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) + { + /* Save the current report data for later comparison to check for changes */ + PrevKeyboardReportData = KeyboardReportData; - switch (USB_ControlRequest.bRequest) + /* Write Keyboard Report Data */ + Endpoint_Write_Stream_LE(&KeyboardReportData, sizeof(KeyboardReportData), NULL); + + /* Finalize the stream transfer to send the last packet */ + Endpoint_ClearIN(); + } + + /** 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) { - case 0x09: - if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE)) - { - LEDs_ToggleLEDs(LEDS_LED1); - - Endpoint_ClearSETUP(); - - Endpoint_Read_Control_Stream_LE(ControlData, sizeof(ControlData)); - Endpoint_ClearIN(); - - switch (USB_ControlRequest.wValue) - { - case 0x303: - if (ControlData[1]) PORTC &= ~RELAY1; else PORTC |= RELAY1; - break; - case 0x306: - if (ControlData[1]) PORTC &= ~RELAY2; else PORTC |= RELAY2; - break; - case 0x309: - if (ControlData[1]) PORTC &= ~RELAY3; else PORTC |= RELAY3; - break; - case 0x30c: - if (ControlData[1]) PORTC &= ~RELAY4; else PORTC |= RELAY4; - break; - } - } - - break; - case 0x01: - if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE)) - { - LEDs_ToggleLEDs(LEDS_LED1); - - Endpoint_ClearSETUP(); - - switch (USB_ControlRequest.wValue) - { - case 0x301: - Endpoint_Write_Control_Stream_LE(SerialNumber, sizeof(SerialNumber)); - break; - case 0x303: - ControlData[1] = (PORTC & RELAY1) ? 2 : 3; - break; - case 0x306: - ControlData[1] = (PORTC & RELAY2) ? 2 : 3; - break; - case 0x309: - ControlData[1] = (PORTC & RELAY3) ? 2 : 3; - break; - case 0x30c: - ControlData[1] = (PORTC & RELAY4) ? 2 : 3; - break; - } - - if (ControlData[1]) - Endpoint_Write_Control_Stream_LE(ControlData, sizeof(ControlData)); - - Endpoint_ClearOUT(); - } - - break; + /* Save the current report data for later comparison to check for changes */ + PrevKeyboardReportData = KeyboardReportData; + + /* Write Keyboard Report Data */ + Endpoint_Write_Stream_LE(&KeyboardReportData, sizeof(KeyboardReportData), NULL); + + /* Finalize the stream transfer to send the last packet */ + Endpoint_ClearIN(); } } + +/** 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 +{ + /* Select the Keyboard LED Report Endpoint */ + Endpoint_SelectEndpoint(KEYBOARD_OUT_EPADDR); + + /* Check if Keyboard LED Endpoint contains a packet */ + if (Endpoint_IsOUTReceived()) + { + /* Check to see if the packet contains data */ + if (Endpoint_IsReadWriteAllowed()) + { + /* Read in the LED report from the host */ + uint8_t LEDReport = Endpoint_Read_8(); + + /* Process the read LED report from the host */ + ProcessLEDReport(LEDReport); + } + + /* Handshake the OUT Endpoint - clear endpoint and ready for next report */ + Endpoint_ClearOUT(); + } +} diff --git a/lufa-master/PolytechLille/PAD/PAD.h b/lufa-master/PolytechLille/PAD/PAD.h index 9599934..052fc62 100644 --- a/lufa-master/PolytechLille/PAD/PAD.h +++ b/lufa-master/PolytechLille/PAD/PAD.h @@ -45,20 +45,19 @@ #include "Descriptors.h" - #include + //#include + #include "LUFA/Drivers/PeripheralSerial.h" #include #include /* Macros: */ - #define RELAY1 (1 << 7) - #define RELAY2 (1 << 6) - #define RELAY3 (1 << 5) - #define RELAY4 (1 << 4) - #define ALL_RELAYS (RELAY1 | RELAY2 | RELAY3 | RELAY4) + /* Function Prototypes: */ void SetupHardware(void); - + void EVENT_USB_Device_ConfigurationChanged(void); + void SendNextReport(void); + void ReceiveNextReport(void); void EVENT_USB_Device_ControlRequest(void); #endif -- libgit2 0.21.2