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.

179 lines
5.6 KiB

  1. /*
  2. ChibiOS - Copyright (C) 2006..2016 Giovanni Di Sirio
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /**
  14. * @file usb_driver.h
  15. * @brief Usb driver suitable for both packet and serial formats
  16. *
  17. * @addtogroup SERIAL_USB
  18. * @{
  19. */
  20. #pragma once
  21. #include <hal_usb_cdc.h>
  22. /*===========================================================================*/
  23. /* Driver constants. */
  24. /*===========================================================================*/
  25. /*===========================================================================*/
  26. /* Derived constants and error checks. */
  27. /*===========================================================================*/
  28. #if HAL_USE_USB == FALSE
  29. # error "The USB Driver requires HAL_USE_USB"
  30. #endif
  31. /*===========================================================================*/
  32. /* Driver data structures and types. */
  33. /*===========================================================================*/
  34. /**
  35. * @brief Driver state machine possible states.
  36. */
  37. typedef enum {
  38. QMKUSB_UNINIT = 0, /**< Not initialized. */
  39. QMKUSB_STOP = 1, /**< Stopped. */
  40. QMKUSB_READY = 2 /**< Ready. */
  41. } qmkusbstate_t;
  42. /**
  43. * @brief Structure representing a serial over USB driver.
  44. */
  45. typedef struct QMKUSBDriver QMKUSBDriver;
  46. /**
  47. * @brief Serial over USB Driver configuration structure.
  48. * @details An instance of this structure must be passed to @p sduStart()
  49. * in order to configure and start the driver operations.
  50. */
  51. typedef struct {
  52. /**
  53. * @brief USB driver to use.
  54. */
  55. USBDriver *usbp;
  56. /**
  57. * @brief Bulk IN endpoint used for outgoing data transfer.
  58. */
  59. usbep_t bulk_in;
  60. /**
  61. * @brief Bulk OUT endpoint used for incoming data transfer.
  62. */
  63. usbep_t bulk_out;
  64. /**
  65. * @brief Interrupt IN endpoint used for notifications.
  66. * @note If set to zero then the INT endpoint is assumed to be not
  67. * present, USB descriptors must be changed accordingly.
  68. */
  69. usbep_t int_in;
  70. /**
  71. * @brief The number of buffers in the queues
  72. */
  73. size_t in_buffers;
  74. size_t out_buffers;
  75. /**
  76. * @brief The size of each buffer in the queue, typically the same as the endpoint size
  77. */
  78. size_t in_size;
  79. size_t out_size;
  80. /**
  81. * @brief Always send full buffers in_size (the rest is filled with zeroes)
  82. */
  83. bool fixed_size;
  84. /* Input buffer
  85. * @note needs to be initialized with a memory buffer of the right size
  86. */
  87. uint8_t *ib;
  88. /* Output buffer
  89. * @note needs to be initialized with a memory buffer of the right size
  90. */
  91. uint8_t *ob;
  92. } QMKUSBConfig;
  93. /**
  94. * @brief @p SerialDriver specific data.
  95. */
  96. #define _qmk_usb_driver_data \
  97. _base_asynchronous_channel_data /* Driver state.*/ \
  98. qmkusbstate_t state; \
  99. /* Input buffers queue.*/ \
  100. input_buffers_queue_t ibqueue; \
  101. /* Output queue.*/ \
  102. output_buffers_queue_t obqueue; \
  103. /* End of the mandatory fields.*/ \
  104. /* Current configuration data.*/ \
  105. const QMKUSBConfig *config;
  106. /**
  107. * @brief @p SerialUSBDriver specific methods.
  108. */
  109. #define _qmk_usb_driver_methods _base_asynchronous_channel_methods
  110. /**
  111. * @extends BaseAsynchronousChannelVMT
  112. *
  113. * @brief @p SerialDriver virtual methods table.
  114. */
  115. struct QMKUSBDriverVMT {
  116. _qmk_usb_driver_methods
  117. };
  118. /**
  119. * @extends BaseAsynchronousChannel
  120. *
  121. * @brief Full duplex serial driver class.
  122. * @details This class extends @p BaseAsynchronousChannel by adding physical
  123. * I/O queues.
  124. */
  125. struct QMKUSBDriver {
  126. /** @brief Virtual Methods Table.*/
  127. const struct QMKUSBDriverVMT *vmt;
  128. _qmk_usb_driver_data
  129. };
  130. /*===========================================================================*/
  131. /* Driver macros. */
  132. /*===========================================================================*/
  133. /*===========================================================================*/
  134. /* External declarations. */
  135. /*===========================================================================*/
  136. #ifdef __cplusplus
  137. extern "C" {
  138. #endif
  139. void qmkusbInit(void);
  140. void qmkusbObjectInit(QMKUSBDriver *qmkusbp, const QMKUSBConfig *config);
  141. void qmkusbStart(QMKUSBDriver *qmkusbp, const QMKUSBConfig *config);
  142. void qmkusbStop(QMKUSBDriver *qmkusbp);
  143. void qmkusbSuspendHookI(QMKUSBDriver *qmkusbp);
  144. void qmkusbWakeupHookI(QMKUSBDriver *qmkusbp);
  145. void qmkusbConfigureHookI(QMKUSBDriver *qmkusbp);
  146. bool qmkusbRequestsHook(USBDriver *usbp);
  147. void qmkusbSOFHookI(QMKUSBDriver *qmkusbp);
  148. void qmkusbDataTransmitted(USBDriver *usbp, usbep_t ep);
  149. void qmkusbDataReceived(USBDriver *usbp, usbep_t ep);
  150. void qmkusbInterruptTransmitted(USBDriver *usbp, usbep_t ep);
  151. #ifdef __cplusplus
  152. }
  153. #endif
  154. /** @} */