hal_serial_usb.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 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 hal_serial_usb.h
  15. * @brief Serial over USB Driver macros and structures.
  16. *
  17. * @addtogroup SERIAL_USB
  18. * @{
  19. */
  20. #ifndef HAL_SERIAL_USB_H
  21. #define HAL_SERIAL_USB_H
  22. #if (HAL_USE_SERIAL_USB == TRUE) || defined(__DOXYGEN__)
  23. #include "hal_usb_cdc.h"
  24. /*===========================================================================*/
  25. /* Driver constants. */
  26. /*===========================================================================*/
  27. /*===========================================================================*/
  28. /* Driver pre-compile time settings. */
  29. /*===========================================================================*/
  30. /**
  31. * @name SERIAL_USB configuration options
  32. * @{
  33. */
  34. /**
  35. * @brief Serial over USB buffers size.
  36. * @details Configuration parameter, the buffer size must be a multiple of
  37. * the USB data endpoint maximum packet size.
  38. * @note The default is 256 bytes for both the transmission and receive
  39. * buffers.
  40. */
  41. #if !defined(SERIAL_USB_BUFFERS_SIZE) || defined(__DOXYGEN__)
  42. #define SERIAL_USB_BUFFERS_SIZE 256
  43. #endif
  44. /**
  45. * @brief Serial over USB number of buffers.
  46. * @note The default is 2 buffers.
  47. */
  48. #if !defined(SERIAL_USB_BUFFERS_NUMBER) || defined(__DOXYGEN__)
  49. #define SERIAL_USB_BUFFERS_NUMBER 2
  50. #endif
  51. /** @} */
  52. /*===========================================================================*/
  53. /* Derived constants and error checks. */
  54. /*===========================================================================*/
  55. #if HAL_USE_USB == FALSE
  56. #error "Serial over USB Driver requires HAL_USE_USB"
  57. #endif
  58. /*===========================================================================*/
  59. /* Driver data structures and types. */
  60. /*===========================================================================*/
  61. /**
  62. * @brief Driver state machine possible states.
  63. */
  64. typedef enum {
  65. SDU_UNINIT = 0, /**< Not initialized. */
  66. SDU_STOP = 1, /**< Stopped. */
  67. SDU_READY = 2 /**< Ready. */
  68. } sdustate_t;
  69. /**
  70. * @brief Structure representing a serial over USB driver.
  71. */
  72. typedef struct SerialUSBDriver SerialUSBDriver;
  73. /**
  74. * @brief Serial over USB Driver configuration structure.
  75. * @details An instance of this structure must be passed to @p sduStart()
  76. * in order to configure and start the driver operations.
  77. */
  78. typedef struct {
  79. /**
  80. * @brief USB driver to use.
  81. */
  82. USBDriver *usbp;
  83. /**
  84. * @brief Bulk IN endpoint used for outgoing data transfer.
  85. */
  86. usbep_t bulk_in;
  87. /**
  88. * @brief Bulk OUT endpoint used for incoming data transfer.
  89. */
  90. usbep_t bulk_out;
  91. /**
  92. * @brief Interrupt IN endpoint used for notifications.
  93. * @note If set to zero then the INT endpoint is assumed to be not
  94. * present, USB descriptors must be changed accordingly.
  95. */
  96. usbep_t int_in;
  97. } SerialUSBConfig;
  98. /**
  99. * @brief @p SerialDriver specific data.
  100. */
  101. #define _serial_usb_driver_data \
  102. _base_asynchronous_channel_data \
  103. /* Driver state.*/ \
  104. sdustate_t state; \
  105. /* Input buffers queue.*/ \
  106. input_buffers_queue_t ibqueue; \
  107. /* Output queue.*/ \
  108. output_buffers_queue_t obqueue; \
  109. /* Input buffer.*/ \
  110. uint8_t ib[BQ_BUFFER_SIZE(SERIAL_USB_BUFFERS_NUMBER, \
  111. SERIAL_USB_BUFFERS_SIZE)]; \
  112. /* Output buffer.*/ \
  113. uint8_t ob[BQ_BUFFER_SIZE(SERIAL_USB_BUFFERS_NUMBER, \
  114. SERIAL_USB_BUFFERS_SIZE)]; \
  115. /* End of the mandatory fields.*/ \
  116. /* Current configuration data.*/ \
  117. const SerialUSBConfig *config;
  118. /**
  119. * @brief @p SerialUSBDriver specific methods.
  120. */
  121. #define _serial_usb_driver_methods \
  122. _base_asynchronous_channel_methods
  123. /**
  124. * @extends BaseAsynchronousChannelVMT
  125. *
  126. * @brief @p SerialDriver virtual methods table.
  127. */
  128. struct SerialUSBDriverVMT {
  129. _serial_usb_driver_methods
  130. };
  131. /**
  132. * @extends BaseAsynchronousChannel
  133. *
  134. * @brief Full duplex serial driver class.
  135. * @details This class extends @p BaseAsynchronousChannel by adding physical
  136. * I/O queues.
  137. */
  138. struct SerialUSBDriver {
  139. /** @brief Virtual Methods Table.*/
  140. const struct SerialUSBDriverVMT *vmt;
  141. _serial_usb_driver_data
  142. };
  143. /*===========================================================================*/
  144. /* Driver macros. */
  145. /*===========================================================================*/
  146. /*===========================================================================*/
  147. /* External declarations. */
  148. /*===========================================================================*/
  149. #ifdef __cplusplus
  150. extern "C" {
  151. #endif
  152. void sduInit(void);
  153. void sduObjectInit(SerialUSBDriver *sdup);
  154. void sduStart(SerialUSBDriver *sdup, const SerialUSBConfig *config);
  155. void sduStop(SerialUSBDriver *sdup);
  156. void sduSuspendHookI(SerialUSBDriver *sdup);
  157. void sduWakeupHookI(SerialUSBDriver *sdup);
  158. void sduConfigureHookI(SerialUSBDriver *sdup);
  159. bool sduRequestsHook(USBDriver *usbp);
  160. void sduSOFHookI(SerialUSBDriver *sdup);
  161. void sduDataTransmitted(USBDriver *usbp, usbep_t ep);
  162. void sduDataReceived(USBDriver *usbp, usbep_t ep);
  163. void sduInterruptTransmitted(USBDriver *usbp, usbep_t ep);
  164. msg_t sduControl(USBDriver *usbp, unsigned int operation, void *arg);
  165. #ifdef __cplusplus
  166. }
  167. #endif
  168. #endif /* HAL_USE_SERIAL_USB == TRUE */
  169. #endif /* HAL_SERIAL_USB_H */
  170. /** @} */