X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/6198289b388e4122afe4913f899b37a8e7cd8af4..e0985b995009d71b80d214a66944e76f4e41aadb:/Demos/Host/GenericHIDHost/GenericHIDHost.c?ds=inline diff --git a/Demos/Host/GenericHIDHost/GenericHIDHost.c b/Demos/Host/GenericHIDHost/GenericHIDHost.c index 625ca7023..9f419ad6d 100644 --- a/Demos/Host/GenericHIDHost/GenericHIDHost.c +++ b/Demos/Host/GenericHIDHost/GenericHIDHost.c @@ -36,17 +36,11 @@ #include "GenericHIDHost.h" -/* Project Tags, for reading out using the ButtLoad project */ -BUTTLOADTAG(ProjName, "LUFA GenHid Host App"); -BUTTLOADTAG(BuildTime, __TIME__); -BUTTLOADTAG(BuildDate, __DATE__); -BUTTLOADTAG(LUFAVersion, "LUFA V" LUFA_VERSION_STRING); - /* Scheduler Task List */ TASK_LIST { - { Task: USB_USBTask , TaskStatus: TASK_STOP }, - { Task: USB_HID_Host , TaskStatus: TASK_STOP }, + { .Task = USB_USBTask , .TaskStatus = TASK_STOP }, + { .Task = USB_HID_Host , .TaskStatus = TASK_STOP }, }; @@ -75,7 +69,7 @@ int main(void) /* Initialize USB Subsystem */ USB_Init(); - /* Startup message */ + /* Start-up message */ puts_P(PSTR(ESC_RESET ESC_BG_WHITE ESC_INVERSE_ON ESC_ERASE_DISPLAY "Generic HID Host Demo running.\r\n" ESC_INVERSE_OFF)); @@ -132,7 +126,7 @@ EVENT_HANDLER(USB_HostError) for(;;); } -/** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occured while +/** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while * enumerating an attached USB device. */ EVENT_HANDLER(USB_DeviceEnumerationFailed) @@ -185,30 +179,34 @@ void ReadNextReport(void) Pipe_SelectPipe(HID_DATA_IN_PIPE); Pipe_Unfreeze(); - /* Ensure pipe contains data and is ready to be read before continuing */ - if (!(Pipe_ReadWriteAllowed())) + /* Check to see if a packet has been received */ + if (!(Pipe_IsINReceived())) { #if !defined(INTERRUPT_DATA_PIPE) /* Refreeze HID data IN pipe */ Pipe_Freeze(); #endif - + return; } - uint8_t ReportINData[Pipe_BytesInPipe()]; + /* Ensure pipe contains data before trying to read from it */ + if (Pipe_IsReadWriteAllowed()) + { + uint8_t ReportINData[Pipe_BytesInPipe()]; - /* Read in HID report data */ - Pipe_Read_Stream_LE(&ReportINData, sizeof(ReportINData)); + /* Read in HID report data */ + Pipe_Read_Stream_LE(&ReportINData, sizeof(ReportINData)); + + /* Print report data through the serial port */ + for (uint16_t CurrByte = 0; CurrByte < sizeof(ReportINData); CurrByte++) + printf_P(PSTR("0x%02X "), ReportINData[CurrByte]); + + puts_P(PSTR("\r\n")); + } /* Clear the IN endpoint, ready for next data packet */ - Pipe_ClearCurrentBank(); - - /* Print report data through the serial port */ - for (uint16_t CurrByte = 0; CurrByte < sizeof(ReportINData); CurrByte++) - printf_P(PSTR("0x%02X "), ReportINData[CurrByte]); - - puts_P(PSTR("\r\n")); + Pipe_ClearIN(); #if !defined(INTERRUPT_DATA_PIPE) /* Refreeze HID data IN pipe */ @@ -219,12 +217,14 @@ void ReadNextReport(void) /** Writes a report to the attached device. * * \param ReportOUTData Buffer containing the report to send to the device + * \param ReportIndex Index of the report in the device (zero if the device does not use multiple reports) + * \param ReportType Type of report to send, either HID_REPORTTYPE_OUTPUT or HID_REPORTTYPE_FEATURE * \param ReportLength Length of the report to send */ -void WriteNextReport(uint8_t ReportOUTData, uint16_t ReportLength) +void WriteNextReport(uint8_t* ReportOUTData, uint8_t ReportIndex, uint8_t ReportType, uint16_t ReportLength) { /* Select and unfreeze HID data OUT pipe */ - Pipe_SelectPipe(HID_DATA_IN_PIPE); + Pipe_SelectPipe(HID_DATA_OUT_PIPE); /* Not all HID devices have an OUT endpoint (some require OUT reports to be sent over the * control endpoint instead) - check to see if the OUT endpoint has been initialized */ @@ -233,19 +233,23 @@ void WriteNextReport(uint8_t ReportOUTData, uint16_t ReportLength) Pipe_Unfreeze(); /* Ensure pipe is ready to be written to before continuing */ - if (!(Pipe_ReadWriteAllowed())) + if (!(Pipe_IsOUTReady())) { /* Refreeze the data OUT pipe */ Pipe_Freeze(); return; } + + /* If the report index is used, send it before the report data */ + if (ReportIndex) + Pipe_Write_Byte(ReportIndex); - /* Read in HID report data */ - Pipe_Write_Stream_LE(&ReportOUTData, ReportLength); + /* Write out HID report data */ + Pipe_Write_Stream_LE(ReportOUTData, ReportLength); /* Clear the OUT endpoint, send last data packet */ - Pipe_ClearCurrentBank(); + Pipe_ClearOUT(); /* Refreeze the data OUT pipe */ Pipe_Freeze(); @@ -253,15 +257,18 @@ void WriteNextReport(uint8_t ReportOUTData, uint16_t ReportLength) else { /* Class specific request to send a HID report to the device */ - USB_HostRequest = (USB_Host_Request_Header_t) + USB_ControlRequest = (USB_Request_Header_t) { - bmRequestType: (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE), - bRequest: REQ_SetReport, - wValue: 0, - wIndex: 0, - wLength: ReportLength, + .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE), + .bRequest = REQ_SetReport, + .wValue = ((ReportType << 8) | ReportIndex), + .wIndex = 0, + .wLength = ReportLength, }; + /* Select the control pipe for the request transfer */ + Pipe_SelectPipe(PIPE_CONTROLPIPE); + /* Send the request to the device */ USB_Host_SendControlRequest(ReportOUTData); } @@ -279,16 +286,19 @@ TASK(USB_HID_Host) { case HOST_STATE_Addressed: /* Standard request to set the device configuration to configuration 1 */ - USB_HostRequest = (USB_Host_Request_Header_t) + USB_ControlRequest = (USB_Request_Header_t) { - bmRequestType: (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_DEVICE), - bRequest: REQ_SetConfiguration, - wValue: 1, - wIndex: 0, - wLength: 0, + .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_DEVICE), + .bRequest = REQ_SetConfiguration, + .wValue = 1, + .wIndex = 0, + .wLength = 0, }; - /* Send the request, display error and wait for device detatch if request fails */ + /* Select the control pipe for the request transfer */ + Pipe_SelectPipe(PIPE_CONTROLPIPE); + + /* Send the request, display error and wait for device detach if request fails */ if ((ErrorCode = USB_Host_SendControlRequest(NULL)) != HOST_SENDCONTROL_Successful) { puts_P(PSTR("Control Error (Set Configuration).\r\n")); @@ -350,6 +360,9 @@ TASK(USB_HID_Host) */ ISR(ENDPOINT_PIPE_vect, ISR_BLOCK) { + /* Save previously selected pipe before selecting a new pipe */ + uint8_t PrevSelectedPipe = Pipe_GetCurrentPipe(); + /* Check to see if the HID data IN pipe has caused the interrupt */ if (Pipe_HasPipeInterrupted(HID_DATA_IN_PIPE)) { @@ -367,5 +380,8 @@ ISR(ENDPOINT_PIPE_vect, ISR_BLOCK) ReadNextReport(); } } + + /* Restore previously selected pipe */ + Pipe_SelectPipe(PrevSelectedPipe); } #endif