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.

PipeStream_UC3.c 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 "PipeStream_UC3.h"
  32. uint8_t Pipe_Discard_Stream(uint16_t Length,
  33. uint16_t* const BytesProcessed)
  34. {
  35. uint8_t ErrorCode;
  36. uint16_t BytesInTransfer = 0;
  37. Pipe_SetPipeToken(PIPE_TOKEN_IN);
  38. if ((ErrorCode = Pipe_WaitUntilReady()))
  39. return ErrorCode;
  40. if (BytesProcessed != NULL)
  41. Length -= *BytesProcessed;
  42. while (Length)
  43. {
  44. if (!(Pipe_IsReadWriteAllowed()))
  45. {
  46. Pipe_ClearIN();
  47. if (BytesProcessed != NULL)
  48. {
  49. *BytesProcessed += BytesInTransfer;
  50. return PIPE_RWSTREAM_IncompleteTransfer;
  51. }
  52. if ((ErrorCode = Pipe_WaitUntilReady()))
  53. return ErrorCode;
  54. }
  55. else
  56. {
  57. Pipe_Discard_8();
  58. Length--;
  59. BytesInTransfer++;
  60. }
  61. }
  62. return PIPE_RWSTREAM_NoError;
  63. }
  64. uint8_t Pipe_Null_Stream(uint16_t Length,
  65. uint16_t* const BytesProcessed)
  66. {
  67. uint8_t ErrorCode;
  68. uint16_t BytesInTransfer = 0;
  69. Pipe_SetPipeToken(PIPE_TOKEN_OUT);
  70. if ((ErrorCode = Pipe_WaitUntilReady()))
  71. return ErrorCode;
  72. if (BytesProcessed != NULL)
  73. Length -= *BytesProcessed;
  74. while (Length)
  75. {
  76. if (!(Pipe_IsReadWriteAllowed()))
  77. {
  78. Pipe_ClearOUT();
  79. if (BytesProcessed != NULL)
  80. {
  81. *BytesProcessed += BytesInTransfer;
  82. return PIPE_RWSTREAM_IncompleteTransfer;
  83. }
  84. USB_USBTask();
  85. if ((ErrorCode = Pipe_WaitUntilReady()))
  86. return ErrorCode;
  87. }
  88. else
  89. {
  90. Pipe_Write_8(0);
  91. Length--;
  92. BytesInTransfer++;
  93. }
  94. }
  95. return PIPE_RWSTREAM_NoError;
  96. }
  97. /* The following abuses the C preprocessor in order to copy-paste common code with slight alterations,
  98. * so that the code needs to be written once. It is a crude form of templating to reduce code maintenance. */
  99. #define TEMPLATE_FUNC_NAME Pipe_Write_Stream_LE
  100. #define TEMPLATE_BUFFER_TYPE const void*
  101. #define TEMPLATE_TOKEN PIPE_TOKEN_OUT
  102. #define TEMPLATE_CLEAR_PIPE() Pipe_ClearOUT()
  103. #define TEMPLATE_BUFFER_OFFSET(Length) 0
  104. #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) DataStream += Amount
  105. #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Pipe_Write_8(*BufferPtr)
  106. #include "Template/Template_Pipe_RW.c"
  107. #define TEMPLATE_FUNC_NAME Pipe_Write_Stream_BE
  108. #define TEMPLATE_BUFFER_TYPE const void*
  109. #define TEMPLATE_TOKEN PIPE_TOKEN_OUT
  110. #define TEMPLATE_CLEAR_PIPE() Pipe_ClearOUT()
  111. #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
  112. #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) DataStream -= Amount
  113. #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Pipe_Write_8(*BufferPtr)
  114. #include "Template/Template_Pipe_RW.c"
  115. #define TEMPLATE_FUNC_NAME Pipe_Read_Stream_LE
  116. #define TEMPLATE_BUFFER_TYPE void*
  117. #define TEMPLATE_TOKEN PIPE_TOKEN_IN
  118. #define TEMPLATE_CLEAR_PIPE() Pipe_ClearIN()
  119. #define TEMPLATE_BUFFER_OFFSET(Length) 0
  120. #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) DataStream += Amount
  121. #define TEMPLATE_TRANSFER_BYTE(BufferPtr) *BufferPtr = Pipe_Read_8()
  122. #include "Template/Template_Pipe_RW.c"
  123. #define TEMPLATE_FUNC_NAME Pipe_Read_Stream_BE
  124. #define TEMPLATE_BUFFER_TYPE void*
  125. #define TEMPLATE_TOKEN PIPE_TOKEN_IN
  126. #define TEMPLATE_CLEAR_PIPE() Pipe_ClearIN()
  127. #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
  128. #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) DataStream -= Amount
  129. #define TEMPLATE_TRANSFER_BYTE(BufferPtr) *BufferPtr = Pipe_Read_8()
  130. #include "Template/Template_Pipe_RW.c"
  131. #endif
  132. #endif