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_XMEGA.c 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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_XMEGA)
  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. Endpoint_FIFOPair_t USB_Endpoint_FIFOs[ENDPOINT_TOTAL_ENDPOINTS];
  36. volatile uint8_t USB_Endpoint_SelectedEndpoint;
  37. volatile USB_EP_t* USB_Endpoint_SelectedHandle;
  38. volatile Endpoint_FIFO_t* USB_Endpoint_SelectedFIFO;
  39. bool Endpoint_IsINReady(void)
  40. {
  41. Endpoint_SelectEndpoint(USB_Endpoint_SelectedEndpoint | ENDPOINT_DIR_IN);
  42. return ((USB_Endpoint_SelectedHandle->STATUS & USB_EP_BUSNACK0_bm) ? true : false);
  43. }
  44. bool Endpoint_IsOUTReceived(void)
  45. {
  46. Endpoint_SelectEndpoint(USB_Endpoint_SelectedEndpoint & ~ENDPOINT_DIR_IN);
  47. if (USB_Endpoint_SelectedHandle->STATUS & USB_EP_TRNCOMPL0_bm)
  48. {
  49. USB_Endpoint_SelectedFIFO->Length = USB_Endpoint_SelectedHandle->CNT;
  50. return true;
  51. }
  52. return false;
  53. }
  54. bool Endpoint_IsSETUPReceived(void)
  55. {
  56. Endpoint_SelectEndpoint(USB_Endpoint_SelectedEndpoint & ~ENDPOINT_DIR_IN);
  57. if (USB_Endpoint_SelectedHandle->STATUS & USB_EP_SETUP_bm)
  58. {
  59. USB_Endpoint_SelectedFIFO->Length = USB_Endpoint_SelectedHandle->CNT;
  60. return true;
  61. }
  62. return false;
  63. }
  64. void Endpoint_ClearSETUP(void)
  65. {
  66. Endpoint_SelectEndpoint(USB_Endpoint_SelectedEndpoint & ~ENDPOINT_DIR_IN);
  67. USB_Endpoint_SelectedHandle->STATUS &= ~(USB_EP_SETUP_bm | USB_EP_TRNCOMPL0_bm | USB_EP_BUSNACK0_bm | USB_EP_OVF_bm);
  68. USB_Endpoint_SelectedHandle->STATUS |= USB_EP_TOGGLE_bm;
  69. USB_Endpoint_SelectedFIFO->Position = 0;
  70. Endpoint_SelectEndpoint(USB_Endpoint_SelectedEndpoint | ENDPOINT_DIR_IN);
  71. USB_Endpoint_SelectedHandle->STATUS |= USB_EP_TOGGLE_bm;
  72. USB_Endpoint_SelectedFIFO->Position = 0;
  73. }
  74. void Endpoint_ClearIN(void)
  75. {
  76. USB_Endpoint_SelectedHandle->CNT = USB_Endpoint_SelectedFIFO->Position;
  77. USB_Endpoint_SelectedHandle->STATUS &= ~(USB_EP_TRNCOMPL0_bm | USB_EP_BUSNACK0_bm | USB_EP_OVF_bm);
  78. USB_Endpoint_SelectedFIFO->Position = 0;
  79. }
  80. void Endpoint_ClearOUT(void)
  81. {
  82. USB_Endpoint_SelectedHandle->STATUS &= ~(USB_EP_TRNCOMPL0_bm | USB_EP_BUSNACK0_bm | USB_EP_OVF_bm);
  83. USB_Endpoint_SelectedFIFO->Position = 0;
  84. }
  85. void Endpoint_StallTransaction(void)
  86. {
  87. USB_Endpoint_SelectedHandle->CTRL |= USB_EP_STALL_bm;
  88. if ((USB_Endpoint_SelectedHandle->CTRL & USB_EP_TYPE_gm) == USB_EP_TYPE_CONTROL_gc)
  89. {
  90. Endpoint_SelectEndpoint(USB_Endpoint_SelectedEndpoint ^ ENDPOINT_DIR_IN);
  91. USB_Endpoint_SelectedHandle->CTRL |= USB_EP_STALL_bm;
  92. }
  93. }
  94. uint8_t Endpoint_Read_8(void)
  95. {
  96. return USB_Endpoint_SelectedFIFO->Data[USB_Endpoint_SelectedFIFO->Position++];
  97. }
  98. void Endpoint_Write_8(const uint8_t Data)
  99. {
  100. USB_Endpoint_SelectedFIFO->Data[USB_Endpoint_SelectedFIFO->Position++] = Data;
  101. }
  102. void Endpoint_SelectEndpoint(const uint8_t Address)
  103. {
  104. uint8_t EndpointNumber = (Address & ENDPOINT_EPNUM_MASK);
  105. USB_Endpoint_SelectedEndpoint = Address;
  106. Endpoint_FIFOPair_t* EndpointFIFOPair = &USB_Endpoint_FIFOs[EndpointNumber];
  107. USB_EndpointTable_t* EndpointTable = (USB_EndpointTable_t*)USB.EPPTR;
  108. if (Address & ENDPOINT_DIR_IN)
  109. {
  110. USB_Endpoint_SelectedFIFO = &EndpointFIFOPair->IN;
  111. USB_Endpoint_SelectedHandle = &EndpointTable->Endpoints[EndpointNumber].IN;
  112. }
  113. else
  114. {
  115. USB_Endpoint_SelectedFIFO = &EndpointFIFOPair->OUT;
  116. USB_Endpoint_SelectedHandle = &EndpointTable->Endpoints[EndpointNumber].OUT;
  117. }
  118. }
  119. bool Endpoint_ConfigureEndpointTable(const USB_Endpoint_Table_t* const Table,
  120. const uint8_t Entries)
  121. {
  122. for (uint8_t i = 0; i < Entries; i++)
  123. {
  124. if (!(Table[i].Address))
  125. continue;
  126. if (!(Endpoint_ConfigureEndpoint(Table[i].Address, Table[i].Type, Table[i].Size, Table[i].Banks)))
  127. {
  128. return false;
  129. }
  130. }
  131. return true;
  132. }
  133. bool Endpoint_ConfigureEndpoint_PRV(const uint8_t Address,
  134. const uint8_t Config,
  135. const uint8_t Size)
  136. {
  137. Endpoint_SelectEndpoint(Address);
  138. USB_Endpoint_SelectedHandle->CTRL = 0;
  139. USB_Endpoint_SelectedHandle->STATUS = (Address & ENDPOINT_DIR_IN) ? USB_EP_BUSNACK0_bm : 0;
  140. USB_Endpoint_SelectedHandle->CTRL = Config;
  141. USB_Endpoint_SelectedHandle->CNT = 0;
  142. USB_Endpoint_SelectedHandle->DATAPTR = (intptr_t)USB_Endpoint_SelectedFIFO->Data;
  143. USB_Endpoint_SelectedFIFO->Length = (Address & ENDPOINT_DIR_IN) ? Size : 0;
  144. USB_Endpoint_SelectedFIFO->Position = 0;
  145. return true;
  146. }
  147. void Endpoint_ClearEndpoints(void)
  148. {
  149. for (uint8_t EPNum = 0; EPNum < ENDPOINT_TOTAL_ENDPOINTS; EPNum++)
  150. {
  151. ((USB_EndpointTable_t*)USB.EPPTR)->Endpoints[EPNum].IN.CTRL = 0;
  152. ((USB_EndpointTable_t*)USB.EPPTR)->Endpoints[EPNum].OUT.CTRL = 0;
  153. }
  154. }
  155. void Endpoint_ClearStatusStage(void)
  156. {
  157. if (USB_ControlRequest.bmRequestType & REQDIR_DEVICETOHOST)
  158. {
  159. while (!(Endpoint_IsOUTReceived()))
  160. {
  161. if (USB_DeviceState == DEVICE_STATE_Unattached)
  162. return;
  163. }
  164. Endpoint_ClearOUT();
  165. }
  166. else
  167. {
  168. while (!(Endpoint_IsINReady()))
  169. {
  170. if (USB_DeviceState == DEVICE_STATE_Unattached)
  171. return;
  172. }
  173. Endpoint_ClearIN();
  174. }
  175. }
  176. #if !defined(CONTROL_ONLY_DEVICE)
  177. uint8_t Endpoint_WaitUntilReady(void)
  178. {
  179. #if (USB_STREAM_TIMEOUT_MS < 0xFF)
  180. uint8_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
  181. #else
  182. uint16_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
  183. #endif
  184. uint16_t PreviousFrameNumber = USB_Device_GetFrameNumber();
  185. for (;;)
  186. {
  187. if (Endpoint_GetEndpointDirection() == ENDPOINT_DIR_IN)
  188. {
  189. if (Endpoint_IsINReady())
  190. return ENDPOINT_READYWAIT_NoError;
  191. }
  192. else
  193. {
  194. if (Endpoint_IsOUTReceived())
  195. return ENDPOINT_READYWAIT_NoError;
  196. }
  197. uint8_t USB_DeviceState_LCL = USB_DeviceState;
  198. if (USB_DeviceState_LCL == DEVICE_STATE_Unattached)
  199. return ENDPOINT_READYWAIT_DeviceDisconnected;
  200. else if (USB_DeviceState_LCL == DEVICE_STATE_Suspended)
  201. return ENDPOINT_READYWAIT_BusSuspended;
  202. else if (Endpoint_IsStalled())
  203. return ENDPOINT_READYWAIT_EndpointStalled;
  204. uint16_t CurrentFrameNumber = USB_Device_GetFrameNumber();
  205. if (CurrentFrameNumber != PreviousFrameNumber)
  206. {
  207. PreviousFrameNumber = CurrentFrameNumber;
  208. if (!(TimeoutMSRem--))
  209. return ENDPOINT_READYWAIT_Timeout;
  210. }
  211. }
  212. }
  213. #endif
  214. #endif
  215. #endif