Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Endpoint_AVR8.c 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2014.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2014 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  9. Permission to use, copy, modify, distribute, and sell this
  10. software and its documentation for any purpose is hereby granted
  11. without fee, provided that the above copyright notice appear in
  12. all copies and that both that the copyright notice and this
  13. permission notice and warranty disclaimer appear in supporting
  14. documentation, and that the name of the author not be used in
  15. advertising or publicity pertaining to distribution of the
  16. software without specific, written prior permission.
  17. The author disclaims all warranties with regard to this
  18. software, including all implied warranties of merchantability
  19. and fitness. In no event shall the author be liable for any
  20. special, indirect or consequential damages or any damages
  21. whatsoever resulting from loss of use, data or profits, whether
  22. in an action of contract, negligence or other tortious action,
  23. arising out of or in connection with the use or performance of
  24. this software.
  25. */
  26. #include "../../../../Common/Common.h"
  27. #if (ARCH == ARCH_AVR8)
  28. #define __INCLUDE_FROM_USB_DRIVER
  29. #include "../USBMode.h"
  30. #if defined(USB_CAN_BE_DEVICE)
  31. #include "../Endpoint.h"
  32. #if !defined(FIXED_CONTROL_ENDPOINT_SIZE)
  33. uint8_t USB_Device_ControlEndpointSize = ENDPOINT_CONTROLEP_DEFAULT_SIZE;
  34. #endif
  35. bool Endpoint_ConfigureEndpointTable(const USB_Endpoint_Table_t* const Table,
  36. const uint8_t Entries)
  37. {
  38. for (uint8_t i = 0; i < Entries; i++)
  39. {
  40. if (!(Table[i].Address))
  41. continue;
  42. if (!(Endpoint_ConfigureEndpoint(Table[i].Address, Table[i].Type, Table[i].Size, Table[i].Banks)))
  43. return false;
  44. }
  45. return true;
  46. }
  47. bool Endpoint_ConfigureEndpoint_Prv(const uint8_t Number,
  48. const uint8_t UECFG0XData,
  49. const uint8_t UECFG1XData)
  50. {
  51. #if defined(CONTROL_ONLY_DEVICE) || defined(ORDERED_EP_CONFIG)
  52. Endpoint_SelectEndpoint(Number);
  53. Endpoint_EnableEndpoint();
  54. UECFG1X = 0;
  55. UECFG0X = UECFG0XData;
  56. UECFG1X = UECFG1XData;
  57. return Endpoint_IsConfigured();
  58. #else
  59. for (uint8_t EPNum = Number; EPNum < ENDPOINT_TOTAL_ENDPOINTS; EPNum++)
  60. {
  61. uint8_t UECFG0XTemp;
  62. uint8_t UECFG1XTemp;
  63. uint8_t UEIENXTemp;
  64. Endpoint_SelectEndpoint(EPNum);
  65. if (EPNum == Number)
  66. {
  67. UECFG0XTemp = UECFG0XData;
  68. UECFG1XTemp = UECFG1XData;
  69. UEIENXTemp = 0;
  70. }
  71. else
  72. {
  73. UECFG0XTemp = UECFG0X;
  74. UECFG1XTemp = UECFG1X;
  75. UEIENXTemp = UEIENX;
  76. }
  77. if (!(UECFG1XTemp & (1 << ALLOC)))
  78. continue;
  79. Endpoint_DisableEndpoint();
  80. UECFG1X &= ~(1 << ALLOC);
  81. Endpoint_EnableEndpoint();
  82. UECFG0X = UECFG0XTemp;
  83. UECFG1X = UECFG1XTemp;
  84. UEIENX = UEIENXTemp;
  85. if (!(Endpoint_IsConfigured()))
  86. return false;
  87. }
  88. Endpoint_SelectEndpoint(Number);
  89. return true;
  90. #endif
  91. }
  92. void Endpoint_ClearEndpoints(void)
  93. {
  94. UEINT = 0;
  95. for (uint8_t EPNum = 0; EPNum < ENDPOINT_TOTAL_ENDPOINTS; EPNum++)
  96. {
  97. Endpoint_SelectEndpoint(EPNum);
  98. UEIENX = 0;
  99. UEINTX = 0;
  100. UECFG1X = 0;
  101. Endpoint_DisableEndpoint();
  102. }
  103. }
  104. void Endpoint_ClearStatusStage(void)
  105. {
  106. if (USB_ControlRequest.bmRequestType & REQDIR_DEVICETOHOST)
  107. {
  108. while (!(Endpoint_IsOUTReceived()))
  109. {
  110. if (USB_DeviceState == DEVICE_STATE_Unattached)
  111. return;
  112. }
  113. Endpoint_ClearOUT();
  114. }
  115. else
  116. {
  117. while (!(Endpoint_IsINReady()))
  118. {
  119. if (USB_DeviceState == DEVICE_STATE_Unattached)
  120. return;
  121. }
  122. Endpoint_ClearIN();
  123. }
  124. }
  125. #if !defined(CONTROL_ONLY_DEVICE)
  126. uint8_t Endpoint_WaitUntilReady(void)
  127. {
  128. #if (USB_STREAM_TIMEOUT_MS < 0xFF)
  129. uint8_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
  130. #else
  131. uint16_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
  132. #endif
  133. uint16_t PreviousFrameNumber = USB_Device_GetFrameNumber();
  134. for (;;)
  135. {
  136. if (Endpoint_GetEndpointDirection() == ENDPOINT_DIR_IN)
  137. {
  138. if (Endpoint_IsINReady())
  139. return ENDPOINT_READYWAIT_NoError;
  140. }
  141. else
  142. {
  143. if (Endpoint_IsOUTReceived())
  144. return ENDPOINT_READYWAIT_NoError;
  145. }
  146. uint8_t USB_DeviceState_LCL = USB_DeviceState;
  147. if (USB_DeviceState_LCL == DEVICE_STATE_Unattached)
  148. return ENDPOINT_READYWAIT_DeviceDisconnected;
  149. else if (USB_DeviceState_LCL == DEVICE_STATE_Suspended)
  150. return ENDPOINT_READYWAIT_BusSuspended;
  151. else if (Endpoint_IsStalled())
  152. return ENDPOINT_READYWAIT_EndpointStalled;
  153. uint16_t CurrentFrameNumber = USB_Device_GetFrameNumber();
  154. if (CurrentFrameNumber != PreviousFrameNumber)
  155. {
  156. PreviousFrameNumber = CurrentFrameNumber;
  157. if (!(TimeoutMSRem--))
  158. return ENDPOINT_READYWAIT_Timeout;
  159. }
  160. }
  161. }
  162. #endif
  163. #endif
  164. #endif