1
0

usb_cdc.h 15 KB

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