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_UC3.c 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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_UC3)
  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. volatile uint32_t USB_Endpoint_SelectedEndpoint = ENDPOINT_CONTROLEP;
  36. volatile uint8_t* USB_Endpoint_FIFOPos[ENDPOINT_TOTAL_ENDPOINTS];
  37. bool Endpoint_ConfigureEndpointTable(const USB_Endpoint_Table_t* const Table,
  38. const uint8_t Entries)
  39. {
  40. for (uint8_t i = 0; i < Entries; i++)
  41. {
  42. if (!(Table[i].Address))
  43. continue;
  44. if (!(Endpoint_ConfigureEndpoint(Table[i].Address, Table[i].Type, Table[i].Size, Table[i].Banks)))
  45. {
  46. return false;
  47. }
  48. }
  49. return true;
  50. }
  51. bool Endpoint_ConfigureEndpoint_Prv(const uint8_t Number,
  52. const uint32_t UECFG0Data)
  53. {
  54. USB_Endpoint_FIFOPos[Number] = &AVR32_USBB_SLAVE[Number * ENDPOINT_HSB_ADDRESS_SPACE_SIZE];
  55. #if defined(CONTROL_ONLY_DEVICE) || defined(ORDERED_EP_CONFIG)
  56. Endpoint_SelectEndpoint(Number);
  57. Endpoint_EnableEndpoint();
  58. (&AVR32_USBB.uecfg0)[Number] = 0;
  59. (&AVR32_USBB.uecfg0)[Number] = UECFG0Data;
  60. return Endpoint_IsConfigured();
  61. #else
  62. for (uint8_t EPNum = Number; EPNum < ENDPOINT_TOTAL_ENDPOINTS; EPNum++)
  63. {
  64. uint32_t UECFG0Temp;
  65. Endpoint_SelectEndpoint(EPNum);
  66. if (EPNum == Number)
  67. {
  68. UECFG0Temp = UECFG0Data;
  69. }
  70. else
  71. {
  72. UECFG0Temp = (&AVR32_USBB.uecfg0)[EPNum];
  73. }
  74. if (!(UECFG0Temp & AVR32_USBB_ALLOC_MASK))
  75. continue;
  76. Endpoint_DisableEndpoint();
  77. (&AVR32_USBB.uecfg0)[EPNum] &= ~AVR32_USBB_ALLOC_MASK;
  78. Endpoint_EnableEndpoint();
  79. (&AVR32_USBB.uecfg0)[EPNum] = UECFG0Temp;
  80. if (!(Endpoint_IsConfigured()))
  81. return false;
  82. }
  83. Endpoint_SelectEndpoint(Number);
  84. return true;
  85. #endif
  86. }
  87. void Endpoint_ClearEndpoints(void)
  88. {
  89. for (uint8_t EPNum = 0; EPNum < ENDPOINT_TOTAL_ENDPOINTS; EPNum++)
  90. {
  91. Endpoint_SelectEndpoint(EPNum);
  92. (&AVR32_USBB.uecfg0)[EPNum] = 0;
  93. (&AVR32_USBB.uecon0clr)[EPNum] = -1;
  94. USB_Endpoint_FIFOPos[EPNum] = &AVR32_USBB_SLAVE[EPNum * 0x10000];
  95. Endpoint_DisableEndpoint();
  96. }
  97. }
  98. void Endpoint_ClearStatusStage(void)
  99. {
  100. if (USB_ControlRequest.bmRequestType & REQDIR_DEVICETOHOST)
  101. {
  102. while (!(Endpoint_IsOUTReceived()))
  103. {
  104. if (USB_DeviceState == DEVICE_STATE_Unattached)
  105. return;
  106. }
  107. Endpoint_ClearOUT();
  108. }
  109. else
  110. {
  111. while (!(Endpoint_IsINReady()))
  112. {
  113. if (USB_DeviceState == DEVICE_STATE_Unattached)
  114. return;
  115. }
  116. Endpoint_ClearIN();
  117. }
  118. }
  119. #if !defined(CONTROL_ONLY_DEVICE)
  120. uint8_t Endpoint_WaitUntilReady(void)
  121. {
  122. #if (USB_STREAM_TIMEOUT_MS < 0xFF)
  123. uint8_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
  124. #else
  125. uint16_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
  126. #endif
  127. uint16_t PreviousFrameNumber = USB_Device_GetFrameNumber();
  128. for (;;)
  129. {
  130. if (Endpoint_GetEndpointDirection() == ENDPOINT_DIR_IN)
  131. {
  132. if (Endpoint_IsINReady())
  133. return ENDPOINT_READYWAIT_NoError;
  134. }
  135. else
  136. {
  137. if (Endpoint_IsOUTReceived())
  138. return ENDPOINT_READYWAIT_NoError;
  139. }
  140. uint8_t USB_DeviceState_LCL = USB_DeviceState;
  141. if (USB_DeviceState_LCL == DEVICE_STATE_Unattached)
  142. return ENDPOINT_READYWAIT_DeviceDisconnected;
  143. else if (USB_DeviceState_LCL == DEVICE_STATE_Suspended)
  144. return ENDPOINT_READYWAIT_BusSuspended;
  145. else if (Endpoint_IsStalled())
  146. return ENDPOINT_READYWAIT_EndpointStalled;
  147. uint16_t CurrentFrameNumber = USB_Device_GetFrameNumber();
  148. if (CurrentFrameNumber != PreviousFrameNumber)
  149. {
  150. PreviousFrameNumber = CurrentFrameNumber;
  151. if (!(TimeoutMSRem--))
  152. return ENDPOINT_READYWAIT_Timeout;
  153. }
  154. }
  155. }
  156. #endif
  157. #endif
  158. #endif