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.

Pipe_UC3.c 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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_HOST)
  31. #include "../Pipe.h"
  32. uint8_t USB_Host_ControlPipeSize = PIPE_CONTROLPIPE_DEFAULT_SIZE;
  33. volatile uint32_t USB_Pipe_SelectedPipe = PIPE_CONTROLPIPE;
  34. volatile uint8_t* USB_Pipe_FIFOPos[PIPE_TOTAL_PIPES];
  35. bool Pipe_ConfigurePipeTable(const USB_Pipe_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 (!(Pipe_ConfigurePipe(Table[i].Address, Table[i].Type, Table[i].EndpointAddress, Table[i].Size, Table[i].Banks)))
  43. {
  44. return false;
  45. }
  46. }
  47. return true;
  48. }
  49. bool Pipe_ConfigurePipe(const uint8_t Address,
  50. const uint8_t Type,
  51. const uint8_t EndpointAddress,
  52. const uint16_t Size,
  53. const uint8_t Banks)
  54. {
  55. uint8_t Number = (Address & PIPE_EPNUM_MASK);
  56. uint8_t Token = (Address & PIPE_DIR_IN) ? PIPE_TOKEN_IN : PIPE_TOKEN_OUT;
  57. if (Number >= PIPE_TOTAL_PIPES)
  58. return false;
  59. if (Type == EP_TYPE_CONTROL)
  60. Token = PIPE_TOKEN_SETUP;
  61. USB_Pipe_FIFOPos[Number] = &AVR32_USBB_SLAVE[Number * PIPE_HSB_ADDRESS_SPACE_SIZE];
  62. #if defined(ORDERED_EP_CONFIG)
  63. Pipe_SelectPipe(Number);
  64. Pipe_EnablePipe();
  65. (&AVR32_USBB.upcfg0)[Number] = 0;
  66. (&AVR32_USBB.upcfg0)[Number] = (AVR32_USBB_ALLOC_MASK |
  67. ((uint32_t)Type << AVR32_USBB_PTYPE_OFFSET) |
  68. ((uint32_t)Token << AVR32_USBB_PTOKEN_OFFSET) |
  69. ((Banks > 1) ? AVR32_USBB_PBK_MASK : 0) |
  70. Pipe_BytesToEPSizeMask(Size) |
  71. ((uint32_t)Number << AVR32_USBB_PEPNUM_OFFSET));
  72. Pipe_SetInfiniteINRequests();
  73. return Pipe_IsConfigured();
  74. #else
  75. for (uint8_t PNum = Number; PNum < PIPE_TOTAL_PIPES; PNum++)
  76. {
  77. uint32_t UPCFG0Temp;
  78. Pipe_SelectPipe(PNum);
  79. if (PNum == Number)
  80. {
  81. UPCFG0Temp = (AVR32_USBB_ALLOC_MASK |
  82. ((uint32_t)Type << AVR32_USBB_PTYPE_OFFSET) |
  83. ((uint32_t)Token << AVR32_USBB_PTOKEN_OFFSET) |
  84. ((Banks > 1) ? AVR32_USBB_PBK_MASK : 0) |
  85. Pipe_BytesToEPSizeMask(Size) |
  86. ((EndpointAddress & PIPE_EPNUM_MASK) << AVR32_USBB_PEPNUM_OFFSET));
  87. }
  88. else
  89. {
  90. UPCFG0Temp = (&AVR32_USBB.upcfg0)[PNum];
  91. }
  92. if (!(UPCFG0Temp & AVR32_USBB_ALLOC_MASK))
  93. continue;
  94. Pipe_DisablePipe();
  95. (&AVR32_USBB.upcfg0)[PNum] &= ~AVR32_USBB_ALLOC_MASK;
  96. Pipe_EnablePipe();
  97. (&AVR32_USBB.upcfg0)[PNum] = UPCFG0Temp;
  98. Pipe_SetInfiniteINRequests();
  99. if (!(Pipe_IsConfigured()))
  100. return false;
  101. }
  102. Pipe_SelectPipe(Number);
  103. return true;
  104. #endif
  105. }
  106. void Pipe_ClearPipes(void)
  107. {
  108. for (uint8_t PNum = 0; PNum < PIPE_TOTAL_PIPES; PNum++)
  109. {
  110. Pipe_SelectPipe(PNum);
  111. (&AVR32_USBB.upcfg0)[PNum] = 0;
  112. (&AVR32_USBB.upcon0clr)[PNum] = -1;
  113. USB_Pipe_FIFOPos[PNum] = &AVR32_USBB_SLAVE[PNum * 0x10000];
  114. Pipe_DisablePipe();
  115. }
  116. }
  117. bool Pipe_IsEndpointBound(const uint8_t EndpointAddress)
  118. {
  119. uint8_t PrevPipeNumber = Pipe_GetCurrentPipe();
  120. for (uint8_t PNum = 0; PNum < PIPE_TOTAL_PIPES; PNum++)
  121. {
  122. Pipe_SelectPipe(PNum);
  123. if (!(Pipe_IsConfigured()))
  124. continue;
  125. if (Pipe_GetBoundEndpointAddress() == EndpointAddress)
  126. return true;
  127. }
  128. Pipe_SelectPipe(PrevPipeNumber);
  129. return false;
  130. }
  131. uint8_t Pipe_WaitUntilReady(void)
  132. {
  133. #if (USB_STREAM_TIMEOUT_MS < 0xFF)
  134. uint8_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
  135. #else
  136. uint16_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
  137. #endif
  138. uint16_t PreviousFrameNumber = USB_Host_GetFrameNumber();
  139. for (;;)
  140. {
  141. if (Pipe_GetPipeToken() == PIPE_TOKEN_IN)
  142. {
  143. if (Pipe_IsINReceived())
  144. return PIPE_READYWAIT_NoError;
  145. }
  146. else
  147. {
  148. if (Pipe_IsOUTReady())
  149. return PIPE_READYWAIT_NoError;
  150. }
  151. if (Pipe_IsStalled())
  152. return PIPE_READYWAIT_PipeStalled;
  153. else if (USB_HostState == HOST_STATE_Unattached)
  154. return PIPE_READYWAIT_DeviceDisconnected;
  155. uint16_t CurrentFrameNumber = USB_Host_GetFrameNumber();
  156. if (CurrentFrameNumber != PreviousFrameNumber)
  157. {
  158. PreviousFrameNumber = CurrentFrameNumber;
  159. if (!(TimeoutMSRem--))
  160. return PIPE_READYWAIT_Timeout;
  161. }
  162. }
  163. }
  164. #endif
  165. #endif