usb_cdc.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /* This file is the part of the Lightweight USB device Stack for STM32 microcontrollers
  2. *
  3. * Copyright ©2016 Dmitry Filimonchuk <dmitrystu[at]gmail[dot]com>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. #ifndef _USB_CDC_H_
  16. #define _USB_CDC_H_
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. /**\addtogroup USB_CDC USB CDC class
  21. * \brief Generic USB CDC class definitions
  22. * \details This module based on
  23. * + Universal Serial Bus Class Definitions for Communications Devices Revision 1.2 (Errata 1)
  24. * @{ */
  25. /**\name USB CDC Class codes
  26. * @{ */
  27. #define USB_CLASS_CDC 0x02 /**<\brief Communicational Device class */
  28. #define USB_CLASS_CDC_DATA 0x0A /**<\brief Data Interface class */
  29. /** @} */
  30. /**\name USB CDC subclass codes
  31. * @{ */
  32. #define USB_CDC_SUBCLASS_ACM 0x02 /**<\brief Abstract Control Model */
  33. /** @} */
  34. /**\name Communications Class Protocol Codes
  35. * @{ */
  36. #define USB_CDC_PROTO_NONE 0x00 /**<\brief No class specific protocol required */
  37. #define USB_CDC_PROTO_V25TER 0x01 /**<\brief AT Commands: V.250 etc.*/
  38. /** @} */
  39. /** \name Data Interface Class Protocol Codes
  40. * @{ */
  41. #define USB_CDC_PROTO_NTB 0x01 /**<\brief Network Transfer Block.*/
  42. #define USB_CDC_PROTO_HOST 0xFD /**<\brief Host based driver.
  43. * \details This protocol code should only be used
  44. * in messages between host and device to identify
  45. * the host driver portion of a protocol stack.*/
  46. #define USB_CDC_PROTO_CDCSPEC 0xFE /**<\brief CDC specified.
  47. * \details The protocol(s) are described using a
  48. * Protocol Unit Functional Descriptors on
  49. *Communication Class Interface.*/
  50. /** @} */
  51. /**\name USB CDC class-specified functional descriptors
  52. * @{ */
  53. #define USB_DTYPE_CDC_HEADER 0x00 /**<\brief Header Functional Descriptor.*/
  54. #define USB_DTYPE_CDC_CALL_MANAGEMENT 0x01 /**<\brief Call Management Functional Descriptor.*/
  55. #define USB_DTYPE_CDC_ACM 0x02 /**<\brief Abstract Control Management Functional
  56. * Descriptor.*/
  57. #define USB_DTYPE_CDC_UNION 0x06 /**<\brief Union Functional Descriptor.*/
  58. #define USB_DTYPE_CDC_COUNTRY 0x07 /**<\brief Country Selection Functional Descriptor.*/
  59. /** @} */
  60. /** \name USB CDC class-specific requests
  61. * @{ */
  62. #define USB_CDC_SEND_ENCAPSULATED_CMD 0x00 /**<\brief Used to issue a command in the format of
  63. * the supported control protocol of the Communication
  64. * Class interface.*/
  65. #define USB_CDC_GET_ENCAPSULATED_RESP 0x01 /**<\brief Used to request a response in the format
  66. * of the supported control protocol of the
  67. * Communication Class interface.*/
  68. #define USB_CDC_SET_COMM_FEATURE 0x02 /**<\brief Controls the settings for a particular
  69. * communication feature of a particular target.*/
  70. #define USB_CDC_GET_COMM_FEATURE 0x03 /**<\brief Returns the current settings for the
  71. * communication feature as selected.*/
  72. #define USB_CDC_CLEAR_COMM_FEATURE 0x04 /**<\brief Controls the settings for a particular
  73. * communication feature of a particular target,
  74. * setting the selected feature to its default state.*/
  75. #define USB_CDC_SET_LINE_CODING 0x20 /**<\brief Allows the host to specify typical
  76. * asynchronous line-character formatting properties.*/
  77. #define USB_CDC_GET_LINE_CODING 0x21 /**<\brief Allows the host to find out the currently
  78. * configured line coding.*/
  79. #define USB_CDC_SET_CONTROL_LINE_STATE 0x22 /**<\brief Generates RS-232/V.24 style control signals.*/
  80. #define USB_CDC_SEND_BREAK 0x23 /**<\brief Sends special carrier modulation that
  81. * generates an RS-232 style break.*/
  82. /** @} */
  83. /**\name Generic CDC specific notifications
  84. * @{ */
  85. #define USB_CDC_NTF_NETWORK_CONNECTION 0x00 /**<\brief Allows the device to notify the host about
  86. * network connection status.*/
  87. #define USB_CDC_NTF_RESPONSE_AVAILABLE 0x01 /**<\brief Allows the device to notify the host that
  88. * a response is available.*/
  89. #define USB_CDC_NTF_SERIAL_STATE 0x20 /**<\brief Sends asynchronous notification of UART status.*/
  90. #define USB_CDC_NTF_SPEED_CHANGE 0x2A /**<\brief Allows the device to inform the host-networking
  91. * driver that a change in either the uplink or the
  92. * downlink bit rate of the connection has occurred.*/
  93. /** @} */
  94. /**\anchor USB_CDC_ACMGMNTCAP
  95. * \name USB CDC Abstract Control Management capabilities
  96. * @{ */
  97. #define USB_CDC_COMM_FEATURE 0x01 /**<\brief Supports the request combination of
  98. * Set_Comm_Feature, Clear_Comm_Feature, Get_Comm_Feature.*/
  99. #define USB_CDC_CAP_LINE 0x02 /**<\brief Supports the request combination of
  100. * Set_Line_Coding, Set_Control_Line_State,
  101. * Get_Line_Coding, and the notification Serial_State.*/
  102. #define USB_CDC_CAP_BRK 0x04 /**<\brief Supports the request Send_Break.*/
  103. #define USB_CDC_CAP_NOTIFY 0x08 /**<\brief Supports notification Network_Connection.*/
  104. /** @} */
  105. /**\anchor USB_CDC_CALLMGMTCAP
  106. * \name USB CDC Call Management capabilities
  107. * @{ */
  108. #define USB_CDC_CALL_MGMT_CAP_CALL_MGMT 0x01 /**<\brief Device handles call management itself.*/
  109. #define USB_CDC_CALL_MGMT_CAP_DATA_INTF 0x02 /**<\brief Device can send/receive call management
  110. * information over a Data Class interface.*/
  111. /** @} */
  112. /**\anchor USB_CDC_LINECODE
  113. * \name Line coding structire bit fields
  114. * @{ */
  115. #define USB_CDC_1_STOP_BITS 0x00 /**<\brief 1 stop bit.*/
  116. #define USB_CDC_1_5_STOP_BITS 0x01 /**<\brief 1.5 stop bits.*/
  117. #define USB_CDC_2_STOP_BITS 0x02 /**<\brief 2 stop bits.*/
  118. #define USB_CDC_NO_PARITY 0x00 /**<\brief NO parity bit.*/
  119. #define USB_CDC_ODD_PARITY 0x01 /**<\brief ODD parity bit.*/
  120. #define USB_CDC_EVEN_PARITY 0x02 /**<\brief EVEN parity bit.*/
  121. #define USB_CDC_MARK_PARITY 0x03 /**<\brief patity is MARK.*/
  122. #define USB_CDC_SPACE_PARITY 0x04 /**<\brief patity is SPACE.*/
  123. /** @} */
  124. /**\name SERIAL_STATE notification data values
  125. * @{ */
  126. #define USB_CDC_STATE_RX_CARRIER 0x0001 /**<\brief State of receiver carrier detection mechanism.
  127. * \details This signal corresponds to V.24 signal 109
  128. * and RS-232 DCD.*/
  129. #define USB_CDC_STATE_TX_CARRIER 0x0002 /**<\brief State of transmission carrier.
  130. * \details This signal corresponds to V.24 signal 106
  131. * and RS-232 DSR.*/
  132. #define USB_CDC_STATE_BREAK 0x0004 /**<\brief State of break detection mechanism of the device.*/
  133. #define USB_CDC_STATE_RING 0x0008 /**<\brief State of ring signal detection of the device.*/
  134. #define USB_CDC_STATE_FRAMING 0x0010 /**<\brief A framing error has occurred.*/
  135. #define USB_CDC_STATE_PARITY 0x0020 /**<\brief A parity error has occurred.*/
  136. #define USB_CDC_STATE_OVERRUN 0x0040 /**<\brief Received data has been discarded due to
  137. * overrun in the device.*/
  138. /** @} */
  139. /**\brief Header Functional Descriptor
  140. * \details Header Functional Descriptor marks the beginning of the concatenated set of functional
  141. * descriptors for the interface. */
  142. struct usb_cdc_header_desc {
  143. uint8_t bFunctionLength; /**<\brief Size of this descriptor in bytes.*/
  144. uint8_t bDescriptorType; /**<\brief CS_INTERFACE descriptor type.*/
  145. uint8_t bDescriptorSubType; /**<\brief Header functional descriptor subtype.*/
  146. uint16_t bcdCDC; /**<\brief USB CDC Specification release number in BCD.*/
  147. } __attribute__ ((packed));
  148. /**\brief Union Functional Descriptor
  149. * \details The Union functional descriptor describes the relationship between a group of interfaces
  150. * that can be considered to form a functional unit. It can only occur within the class-specific
  151. * portion of an Interface descriptor. One of the interfaces in the group is designated as a master
  152. * or controlling interface for the group, and certain class-specific messages can be sent to this
  153. * interface to act upon the group as a whole.*/
  154. struct usb_cdc_union_desc {
  155. uint8_t bFunctionLength; /**<\brief Size of this functional descriptor, in bytes.*/
  156. uint8_t bDescriptorType; /**<\brief CS_INTERFACE descriptor type.*/
  157. uint8_t bDescriptorSubType; /**<\brief Union Functional Descriptor.*/
  158. uint8_t bMasterInterface0; /**<\brief The interface number of the CDC interface designated
  159. * as the master or controlling interface for the union.*/
  160. uint8_t bSlaveInterface0; /**<\brief Interface number of first slave or associated interface
  161. * in the union.*/
  162. /* ... and there could be other slave interfaces */
  163. } __attribute__ ((packed));
  164. /**\brief Country Selection Functional Descriptor
  165. * \details The Country Selection functional descriptor identifies the countries in which the
  166. * communication device is qualified to operate. The parameters of the network connection often vary
  167. * from one country to another, especially in Europe. Also legal requirements impose certain
  168. * restrictions on devices because of different regulations by the governing body of the network to
  169. * which the device must adhere. This descriptor can only occur within the class-specific portion of
  170. * an Interface descriptor and should only be provided to a master Communication Class interface of
  171. * a union. The country codes used in the Country Selection Functional Descriptor are not the same
  172. * as the country codes used in dialing international telephone calls. Implementers should refer to
  173. * the ISO 3166 specification for more information.*/
  174. struct usb_cdc_country_desc {
  175. uint8_t bFunctionLength; /**<\brief Size of this functional descriptor, in bytes.*/
  176. uint8_t bDescriptorType; /**<\brief CS_INTERFACE descriptor type.*/
  177. uint8_t bDescriptorSubType; /**<\brief Country Selection Functional Descriptor.*/
  178. uint8_t iCountryCodeRelDate; /**<\brief Index of a string giving the release date for the
  179. * implemented ISO 3166 Country Codes. */
  180. uint8_t wCountyCode0; /**<\brief Country code in hexadecimal format.
  181. * \details As defined in ISO 3166, release date as specified
  182. * in iCountryCodeRelDate for the first supported country. */
  183. /* ... and there can be a lot of country codes */
  184. } __attribute__ ((packed));
  185. /**\brief Call Management Functional Descriptor.
  186. * \details The Call Management functional descriptor describes the processing of calls for the
  187. * Communication Class interface. It can only occur within the class-specific portion of an Interface
  188. * descriptor.*/
  189. struct usb_cdc_call_mgmt_desc {
  190. uint8_t bFunctionLength; /**<\brief Size of this functional descriptor, in bytes.*/
  191. uint8_t bDescriptorType; /**<\brief CS_INTERFACE descriptor type.*/
  192. uint8_t bDescriptorSubType; /**<\brief Call Management functional descriptor subtype.*/
  193. uint8_t bmCapabilities; /**<\brief The call management capabilities that this
  194. * configuration supports.*/
  195. uint8_t bDataInterface; /**<\brief Interface number of Data Class interface optionally
  196. * used for call management.*/
  197. } __attribute__ ((packed));
  198. /**\brief Abstract Control Management Functional Descriptor
  199. * \details The Abstract Control Management functional descriptor describes the commands supported
  200. * by the Communication Class interface, as defined in Section 3.6.2, with the SubClass code of
  201. * Abstract Control Model. It can only occur within the class-specific portion of an Interface
  202. * descriptor.*/
  203. struct usb_cdc_acm_desc {
  204. uint8_t bFunctionLength; /**<\brief Size of this functional descriptor, in bytes.*/
  205. uint8_t bDescriptorType; /**<\brief CS_INTERFACE descriptor type.*/
  206. uint8_t bDescriptorSubType; /**<\brief Abstract Control Management functional descriptor subtype.*/
  207. uint8_t bmCapabilities; /**<\brief The capabilities that this configuration supports.*/
  208. } __attribute__ ((packed));
  209. /**\brief Notification structure from CDC */
  210. struct usb_cdc_notification {
  211. uint8_t bmRequestType; /**<\brief This bitmapped field identifies the characteristics
  212. * of the specific request.*/
  213. uint8_t bNotificationType; /**<\brief Notification type.*/
  214. uint16_t wValue; /**<\brief Notification value.*/
  215. uint16_t wIndex; /**<\brief Interface.*/
  216. uint16_t wLength; /**<\brief Data payload length in bytes.*/
  217. uint8_t Data[]; /**<\brief Data payload.*/
  218. } __attribute__ ((packed));
  219. /**\brief Line Coding Structure */
  220. struct usb_cdc_line_coding {
  221. uint32_t dwDTERate; /**<\brief Data terminal rate, in bits per second.*/
  222. uint8_t bCharFormat; /**<\brief Stop bits.*/
  223. uint8_t bParityType; /**<\brief Parity.*/
  224. uint8_t bDataBits; /**<\brief Data bits (5,6,7,8 or 16).*/
  225. } __attribute__ ((packed));
  226. /** @} */
  227. #ifdef __cplusplus
  228. }
  229. #endif
  230. #endif /* _USB_CDC_H_ */