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.

EndpointStream_XMEGA.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 "EndpointStream_XMEGA.h"
  32. #if !defined(CONTROL_ONLY_DEVICE)
  33. uint8_t Endpoint_Discard_Stream(uint16_t Length,
  34. uint16_t* const BytesProcessed)
  35. {
  36. uint8_t ErrorCode;
  37. uint16_t BytesInTransfer = 0;
  38. if ((ErrorCode = Endpoint_WaitUntilReady()))
  39. return ErrorCode;
  40. if (BytesProcessed != NULL)
  41. Length -= *BytesProcessed;
  42. while (Length)
  43. {
  44. if (!(Endpoint_IsReadWriteAllowed()))
  45. {
  46. Endpoint_ClearOUT();
  47. if (BytesProcessed != NULL)
  48. {
  49. *BytesProcessed += BytesInTransfer;
  50. return ENDPOINT_RWSTREAM_IncompleteTransfer;
  51. }
  52. if ((ErrorCode = Endpoint_WaitUntilReady()))
  53. return ErrorCode;
  54. }
  55. else
  56. {
  57. Endpoint_Discard_8();
  58. Length--;
  59. BytesInTransfer++;
  60. }
  61. }
  62. return ENDPOINT_RWSTREAM_NoError;
  63. }
  64. uint8_t Endpoint_Null_Stream(uint16_t Length,
  65. uint16_t* const BytesProcessed)
  66. {
  67. uint8_t ErrorCode;
  68. uint16_t BytesInTransfer = 0;
  69. if ((ErrorCode = Endpoint_WaitUntilReady()))
  70. return ErrorCode;
  71. if (BytesProcessed != NULL)
  72. Length -= *BytesProcessed;
  73. while (Length)
  74. {
  75. if (!(Endpoint_IsReadWriteAllowed()))
  76. {
  77. Endpoint_ClearIN();
  78. if (BytesProcessed != NULL)
  79. {
  80. *BytesProcessed += BytesInTransfer;
  81. return ENDPOINT_RWSTREAM_IncompleteTransfer;
  82. }
  83. if ((ErrorCode = Endpoint_WaitUntilReady()))
  84. return ErrorCode;
  85. }
  86. else
  87. {
  88. Endpoint_Write_8(0);
  89. Length--;
  90. BytesInTransfer++;
  91. }
  92. }
  93. return ENDPOINT_RWSTREAM_NoError;
  94. }
  95. /* The following abuses the C preprocessor in order to copy-paste common code with slight alterations,
  96. * so that the code needs to be written once. It is a crude form of templating to reduce code maintenance. */
  97. #define TEMPLATE_FUNC_NAME Endpoint_Write_Stream_LE
  98. #define TEMPLATE_BUFFER_TYPE const void*
  99. #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearIN()
  100. #define TEMPLATE_BUFFER_OFFSET(Length) 0
  101. #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) BufferPtr += Amount
  102. #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_8(*BufferPtr)
  103. #include "Template/Template_Endpoint_RW.c"
  104. #define TEMPLATE_FUNC_NAME Endpoint_Write_Stream_BE
  105. #define TEMPLATE_BUFFER_TYPE const void*
  106. #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearIN()
  107. #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
  108. #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) BufferPtr -= Amount
  109. #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_8(*BufferPtr)
  110. #include "Template/Template_Endpoint_RW.c"
  111. #define TEMPLATE_FUNC_NAME Endpoint_Read_Stream_LE
  112. #define TEMPLATE_BUFFER_TYPE void*
  113. #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearOUT()
  114. #define TEMPLATE_BUFFER_OFFSET(Length) 0
  115. #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) BufferPtr += Amount
  116. #define TEMPLATE_TRANSFER_BYTE(BufferPtr) *BufferPtr = Endpoint_Read_8()
  117. #include "Template/Template_Endpoint_RW.c"
  118. #define TEMPLATE_FUNC_NAME Endpoint_Read_Stream_BE
  119. #define TEMPLATE_BUFFER_TYPE void*
  120. #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearOUT()
  121. #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
  122. #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) BufferPtr -= Amount
  123. #define TEMPLATE_TRANSFER_BYTE(BufferPtr) *BufferPtr = Endpoint_Read_8()
  124. #include "Template/Template_Endpoint_RW.c"
  125. #if defined(ARCH_HAS_FLASH_ADDRESS_SPACE)
  126. #define TEMPLATE_FUNC_NAME Endpoint_Write_PStream_LE
  127. #define TEMPLATE_BUFFER_TYPE const void*
  128. #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearIN()
  129. #define TEMPLATE_BUFFER_OFFSET(Length) 0
  130. #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) BufferPtr += Amount
  131. #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_8(pgm_read_byte(BufferPtr))
  132. #include "Template/Template_Endpoint_RW.c"
  133. #define TEMPLATE_FUNC_NAME Endpoint_Write_PStream_BE
  134. #define TEMPLATE_BUFFER_TYPE const void*
  135. #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearIN()
  136. #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
  137. #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) BufferPtr -= Amount
  138. #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_8(pgm_read_byte(BufferPtr))
  139. #include "Template/Template_Endpoint_RW.c"
  140. #endif
  141. #if defined(ARCH_HAS_EEPROM_ADDRESS_SPACE)
  142. #define TEMPLATE_FUNC_NAME Endpoint_Write_EStream_LE
  143. #define TEMPLATE_BUFFER_TYPE const void*
  144. #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearIN()
  145. #define TEMPLATE_BUFFER_OFFSET(Length) 0
  146. #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) BufferPtr += Amount
  147. #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_8(eeprom_read_byte(BufferPtr))
  148. #include "Template/Template_Endpoint_RW.c"
  149. #define TEMPLATE_FUNC_NAME Endpoint_Write_EStream_BE
  150. #define TEMPLATE_BUFFER_TYPE const void*
  151. #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearIN()
  152. #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
  153. #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) BufferPtr -= Amount
  154. #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_8(eeprom_read_byte(BufferPtr))
  155. #include "Template/Template_Endpoint_RW.c"
  156. #define TEMPLATE_FUNC_NAME Endpoint_Read_EStream_LE
  157. #define TEMPLATE_BUFFER_TYPE void*
  158. #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearOUT()
  159. #define TEMPLATE_BUFFER_OFFSET(Length) 0
  160. #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) BufferPtr += Amount
  161. #define TEMPLATE_TRANSFER_BYTE(BufferPtr) eeprom_update_byte(BufferPtr, Endpoint_Read_8())
  162. #include "Template/Template_Endpoint_RW.c"
  163. #define TEMPLATE_FUNC_NAME Endpoint_Read_EStream_BE
  164. #define TEMPLATE_BUFFER_TYPE void*
  165. #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearOUT()
  166. #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
  167. #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) BufferPtr -= Amount
  168. #define TEMPLATE_TRANSFER_BYTE(BufferPtr) eeprom_update_byte(BufferPtr, Endpoint_Read_8())
  169. #include "Template/Template_Endpoint_RW.c"
  170. #endif
  171. #endif
  172. #define TEMPLATE_FUNC_NAME Endpoint_Write_Control_Stream_LE
  173. #define TEMPLATE_BUFFER_OFFSET(Length) 0
  174. #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) BufferPtr += Amount
  175. #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_8(*BufferPtr)
  176. #include "Template/Template_Endpoint_Control_W.c"
  177. #define TEMPLATE_FUNC_NAME Endpoint_Write_Control_Stream_BE
  178. #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
  179. #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) BufferPtr -= Amount
  180. #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_8(*BufferPtr)
  181. #include "Template/Template_Endpoint_Control_W.c"
  182. #define TEMPLATE_FUNC_NAME Endpoint_Read_Control_Stream_LE
  183. #define TEMPLATE_BUFFER_OFFSET(Length) 0
  184. #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) BufferPtr += Amount
  185. #define TEMPLATE_TRANSFER_BYTE(BufferPtr) *BufferPtr = Endpoint_Read_8()
  186. #include "Template/Template_Endpoint_Control_R.c"
  187. #define TEMPLATE_FUNC_NAME Endpoint_Read_Control_Stream_BE
  188. #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
  189. #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) BufferPtr -= Amount
  190. #define TEMPLATE_TRANSFER_BYTE(BufferPtr) *BufferPtr = Endpoint_Read_8()
  191. #include "Template/Template_Endpoint_Control_R.c"
  192. #if defined(ARCH_HAS_FLASH_ADDRESS_SPACE)
  193. #define TEMPLATE_FUNC_NAME Endpoint_Write_Control_PStream_LE
  194. #define TEMPLATE_BUFFER_OFFSET(Length) 0
  195. #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) BufferPtr += Amount
  196. #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_8(pgm_read_byte(BufferPtr))
  197. #include "Template/Template_Endpoint_Control_W.c"
  198. #define TEMPLATE_FUNC_NAME Endpoint_Write_Control_PStream_BE
  199. #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
  200. #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) BufferPtr -= Amount
  201. #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_8(pgm_read_byte(BufferPtr))
  202. #include "Template/Template_Endpoint_Control_W.c"
  203. #endif
  204. #if defined(ARCH_HAS_EEPROM_ADDRESS_SPACE)
  205. #define TEMPLATE_FUNC_NAME Endpoint_Write_Control_EStream_LE
  206. #define TEMPLATE_BUFFER_OFFSET(Length) 0
  207. #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) BufferPtr += Amount
  208. #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_8(eeprom_read_byte(BufferPtr))
  209. #include "Template/Template_Endpoint_Control_W.c"
  210. #define TEMPLATE_FUNC_NAME Endpoint_Write_Control_EStream_BE
  211. #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
  212. #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) BufferPtr -= Amount
  213. #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_8(eeprom_read_byte(BufferPtr))
  214. #include "Template/Template_Endpoint_Control_W.c"
  215. #define TEMPLATE_FUNC_NAME Endpoint_Read_Control_EStream_LE
  216. #define TEMPLATE_BUFFER_OFFSET(Length) 0
  217. #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) BufferPtr += Amount
  218. #define TEMPLATE_TRANSFER_BYTE(BufferPtr) eeprom_update_byte(BufferPtr, Endpoint_Read_8())
  219. #include "Template/Template_Endpoint_Control_R.c"
  220. #define TEMPLATE_FUNC_NAME Endpoint_Read_Control_EStream_BE
  221. #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
  222. #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) BufferPtr -= Amount
  223. #define TEMPLATE_TRANSFER_BYTE(BufferPtr) eeprom_update_byte(BufferPtr, Endpoint_Read_8())
  224. #include "Template/Template_Endpoint_Control_R.c"
  225. #endif
  226. #endif
  227. #endif