Kiibohd Controller
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

usb_rawio.c 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* Copyright (C) 2016 by Jacob Alexander
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. * THE SOFTWARE.
  20. */
  21. #include <kll_defs.h>
  22. #if enableRawIO_define == 1
  23. // ----- Includes -----
  24. // Compiler Includes
  25. #include <string.h> // For memcpy
  26. // Project Includes
  27. #include <Lib/OutputLib.h>
  28. #include <print.h>
  29. // Local Includes
  30. #include "usb_dev.h"
  31. #include "usb_rawio.h"
  32. // ----- Defines -----
  33. // Maximum number of transmit packets to queue so we don't starve other endpoints for memory
  34. #define TX_PACKET_LIMIT 5
  35. // ----- Functions -----
  36. // Check for packets available from host
  37. uint32_t usb_rawio_available()
  38. {
  39. // Error if USB isn't configured
  40. if ( !usb_configuration )
  41. return 0;
  42. // Query number of bytes available from the endpoint
  43. return usb_rx_byte_count( RAWIO_RX_ENDPOINT );
  44. }
  45. // Retrieve packets from host
  46. // Always returns RAWIO_RX_SIZE
  47. int32_t usb_rawio_rx( void *buf, uint32_t timeout )
  48. {
  49. usb_packet_t *rx_packet;
  50. uint32_t begin = millis();
  51. // Read
  52. while ( 1 )
  53. {
  54. // Error if USB isn't configured
  55. if ( !usb_configuration )
  56. return -1;
  57. // Retrieve packet
  58. rx_packet = usb_rx( RAWIO_RX_ENDPOINT );
  59. if ( rx_packet )
  60. break;
  61. // Check for timeout
  62. if ( millis() - begin > timeout || !timeout )
  63. {
  64. warn_msg("RAWIO Rx - Timeout, dropping packet.");
  65. return 0;
  66. }
  67. yield();
  68. }
  69. // Transfer packet from USB buffer to given buffer
  70. memcpy( buf, rx_packet->buf, RAWIO_RX_SIZE );
  71. usb_free( rx_packet );
  72. // Data sent in full packet chunks
  73. return RAWIO_RX_SIZE;
  74. }
  75. // Send packet to host
  76. // XXX Only transfers RAWIO_TX_SIZE on each call (likely 64 bytes)
  77. // Always returns RAWIO_TX_SIZE
  78. int32_t usb_rawio_tx( const void *buf, uint32_t timeout )
  79. {
  80. usb_packet_t *tx_packet;
  81. uint32_t begin = millis();
  82. while ( 1 )
  83. {
  84. // Error if USB isn't configured
  85. if ( !usb_configuration )
  86. return -1;
  87. // Make sure we haven't exceeded the outgoing packet limit
  88. if ( usb_tx_packet_count( RAWIO_TX_ENDPOINT ) < TX_PACKET_LIMIT )
  89. {
  90. // Allocate a packet buffer
  91. tx_packet = usb_malloc();
  92. if ( tx_packet )
  93. break;
  94. }
  95. // Check for timeout
  96. if ( millis() - begin > timeout )
  97. {
  98. warn_msg("RAWIO Tx - Timeout, dropping packet.");
  99. return 0;
  100. }
  101. yield();
  102. }
  103. // Copy input buffer to usb packet buffer and assign length
  104. memcpy( tx_packet->buf, buf, RAWIO_TX_SIZE );
  105. tx_packet->len = RAWIO_TX_SIZE;
  106. // Send USB packet
  107. usb_tx( RAWIO_TX_ENDPOINT, tx_packet );
  108. return RAWIO_TX_SIZE;
  109. }
  110. #endif