usbcfg.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. #include "hal.h"
  14. #include "hal_usb_cdc.h"
  15. #include "usbcfg.h"
  16. /*
  17. * USB Device Descriptor.
  18. */
  19. static const uint8_t vcom_device_descriptor_data[18] = {
  20. USB_DESC_DEVICE (0x0200, /* bcdUSB (2.0). */
  21. 0x02, /* bDeviceClass (CDC). */
  22. 0x00, /* bDeviceSubClass. */
  23. 0x00, /* bDeviceProtocol. */
  24. 0x40, /* bMaxPacketSize. */
  25. 0x0483, /* idVendor (ST). */
  26. 0x5740, /* idProduct. */
  27. 0x0200, /* bcdDevice. */
  28. 1, /* iManufacturer. */
  29. 2, /* iProduct. */
  30. 3, /* iSerialNumber. */
  31. 1) /* bNumConfigurations. */
  32. };
  33. /*
  34. * Device Descriptor wrapper.
  35. */
  36. static const USBDescriptor vcom_device_descriptor = {
  37. sizeof vcom_device_descriptor_data,
  38. vcom_device_descriptor_data
  39. };
  40. /* Configuration Descriptor tree for a CDC.*/
  41. static const uint8_t vcom_configuration_descriptor_data[67] = {
  42. /* Configuration Descriptor.*/
  43. USB_DESC_CONFIGURATION(67, /* wTotalLength. */
  44. 0x02, /* bNumInterfaces. */
  45. 0x01, /* bConfigurationValue. */
  46. 0, /* iConfiguration. */
  47. 0xC0, /* bmAttributes (self powered). */
  48. 50), /* bMaxPower (100mA). */
  49. /* Interface Descriptor.*/
  50. USB_DESC_INTERFACE (0x00, /* bInterfaceNumber. */
  51. 0x00, /* bAlternateSetting. */
  52. 0x01, /* bNumEndpoints. */
  53. 0x02, /* bInterfaceClass (Communications
  54. Interface Class, CDC section
  55. 4.2). */
  56. 0x02, /* bInterfaceSubClass (Abstract
  57. Control Model, CDC section 4.3). */
  58. 0x01, /* bInterfaceProtocol (AT commands,
  59. CDC section 4.4). */
  60. 0), /* iInterface. */
  61. /* Header Functional Descriptor (CDC section 5.2.3).*/
  62. USB_DESC_BYTE (5), /* bLength. */
  63. USB_DESC_BYTE (0x24), /* bDescriptorType (CS_INTERFACE). */
  64. USB_DESC_BYTE (0x00), /* bDescriptorSubtype (Header
  65. Functional Descriptor. */
  66. USB_DESC_BCD (0x0110), /* bcdCDC. */
  67. /* Call Management Functional Descriptor. */
  68. USB_DESC_BYTE (5), /* bFunctionLength. */
  69. USB_DESC_BYTE (0x24), /* bDescriptorType (CS_INTERFACE). */
  70. USB_DESC_BYTE (0x01), /* bDescriptorSubtype (Call Management
  71. Functional Descriptor). */
  72. USB_DESC_BYTE (0x00), /* bmCapabilities (D0+D1). */
  73. USB_DESC_BYTE (0x01), /* bDataInterface. */
  74. /* ACM Functional Descriptor.*/
  75. USB_DESC_BYTE (4), /* bFunctionLength. */
  76. USB_DESC_BYTE (0x24), /* bDescriptorType (CS_INTERFACE). */
  77. USB_DESC_BYTE (0x02), /* bDescriptorSubtype (Abstract
  78. Control Management Descriptor). */
  79. USB_DESC_BYTE (0x02), /* bmCapabilities. */
  80. /* Union Functional Descriptor.*/
  81. USB_DESC_BYTE (5), /* bFunctionLength. */
  82. USB_DESC_BYTE (0x24), /* bDescriptorType (CS_INTERFACE). */
  83. USB_DESC_BYTE (0x06), /* bDescriptorSubtype (Union
  84. Functional Descriptor). */
  85. USB_DESC_BYTE (0x00), /* bMasterInterface (Communication
  86. Class Interface). */
  87. USB_DESC_BYTE (0x01), /* bSlaveInterface0 (Data Class
  88. Interface). */
  89. /* Endpoint 2 Descriptor.*/
  90. USB_DESC_ENDPOINT (USBD2_INTERRUPT_REQUEST_EP|0x80,
  91. 0x03, /* bmAttributes (Interrupt). */
  92. 0x0008, /* wMaxPacketSize. */
  93. 0xFF), /* bInterval. */
  94. /* Interface Descriptor.*/
  95. USB_DESC_INTERFACE (0x01, /* bInterfaceNumber. */
  96. 0x00, /* bAlternateSetting. */
  97. 0x02, /* bNumEndpoints. */
  98. 0x0A, /* bInterfaceClass (Data Class
  99. Interface, CDC section 4.5). */
  100. 0x00, /* bInterfaceSubClass (CDC section
  101. 4.6). */
  102. 0x00, /* bInterfaceProtocol (CDC section
  103. 4.7). */
  104. 0x00), /* iInterface. */
  105. /* Endpoint 3 Descriptor.*/
  106. USB_DESC_ENDPOINT (USBD2_DATA_AVAILABLE_EP, /* bEndpointAddress.*/
  107. 0x02, /* bmAttributes (Bulk). */
  108. 0x0040, /* wMaxPacketSize. */
  109. 0x00), /* bInterval. */
  110. /* Endpoint 1 Descriptor.*/
  111. USB_DESC_ENDPOINT (USBD2_DATA_REQUEST_EP|0x80, /* bEndpointAddress.*/
  112. 0x02, /* bmAttributes (Bulk). */
  113. 0x0040, /* wMaxPacketSize. */
  114. 0x00) /* bInterval. */
  115. };
  116. /*
  117. * Configuration Descriptor wrapper.
  118. */
  119. static const USBDescriptor vcom_configuration_descriptor = {
  120. sizeof vcom_configuration_descriptor_data,
  121. vcom_configuration_descriptor_data
  122. };
  123. /*
  124. * U.S. English language identifier.
  125. */
  126. static const uint8_t vcom_string0[] = {
  127. USB_DESC_BYTE(4), /* bLength. */
  128. USB_DESC_BYTE(USB_DESCRIPTOR_STRING), /* bDescriptorType. */
  129. USB_DESC_WORD(0x0409) /* wLANGID (U.S. English). */
  130. };
  131. /*
  132. * Vendor string.
  133. */
  134. static const uint8_t vcom_string1[] = {
  135. USB_DESC_BYTE(38), /* bLength. */
  136. USB_DESC_BYTE(USB_DESCRIPTOR_STRING), /* bDescriptorType. */
  137. 'S', 0, 'T', 0, 'M', 0, 'i', 0, 'c', 0, 'r', 0, 'o', 0, 'e', 0,
  138. 'l', 0, 'e', 0, 'c', 0, 't', 0, 'r', 0, 'o', 0, 'n', 0, 'i', 0,
  139. 'c', 0, 's', 0
  140. };
  141. /*
  142. * Device Description string.
  143. */
  144. static const uint8_t vcom_string2[] = {
  145. USB_DESC_BYTE(56), /* bLength. */
  146. USB_DESC_BYTE(USB_DESCRIPTOR_STRING), /* bDescriptorType. */
  147. 'C', 0, 'h', 0, 'i', 0, 'b', 0, 'i', 0, 'O', 0, 'S', 0, '/', 0,
  148. 'R', 0, 'T', 0, ' ', 0, 'V', 0, 'i', 0, 'r', 0, 't', 0, 'u', 0,
  149. 'a', 0, 'l', 0, ' ', 0, 'C', 0, 'O', 0, 'M', 0, ' ', 0, 'P', 0,
  150. 'o', 0, 'r', 0, 't', 0
  151. };
  152. /*
  153. * Serial Number string.
  154. */
  155. static const uint8_t vcom_string3[] = {
  156. USB_DESC_BYTE(8), /* bLength. */
  157. USB_DESC_BYTE(USB_DESCRIPTOR_STRING), /* bDescriptorType. */
  158. '0' + CH_KERNEL_MAJOR, 0,
  159. '0' + CH_KERNEL_MINOR, 0,
  160. '0' + CH_KERNEL_PATCH, 0
  161. };
  162. /*
  163. * Strings wrappers array.
  164. */
  165. static const USBDescriptor vcom_strings[] = {
  166. {sizeof vcom_string0, vcom_string0},
  167. {sizeof vcom_string1, vcom_string1},
  168. {sizeof vcom_string2, vcom_string2},
  169. {sizeof vcom_string3, vcom_string3}
  170. };
  171. /*
  172. * Handles the GET_DESCRIPTOR callback. All required descriptors must be
  173. * handled here.
  174. */
  175. static const USBDescriptor *get_descriptor(USBDriver *usbp,
  176. uint8_t dtype,
  177. uint8_t dindex,
  178. uint16_t lang) {
  179. (void)usbp;
  180. (void)lang;
  181. switch (dtype) {
  182. case USB_DESCRIPTOR_DEVICE:
  183. return &vcom_device_descriptor;
  184. case USB_DESCRIPTOR_CONFIGURATION:
  185. return &vcom_configuration_descriptor;
  186. case USB_DESCRIPTOR_STRING:
  187. if (dindex < 4)
  188. return &vcom_strings[dindex];
  189. }
  190. return NULL;
  191. }
  192. /**
  193. * @brief IN EP1 state.
  194. */
  195. static USBInEndpointState ep1instate;
  196. /**
  197. * @brief OUT EP1 state.
  198. */
  199. static USBOutEndpointState ep1outstate;
  200. /**
  201. * @brief EP1 initialization structure (both IN and OUT).
  202. */
  203. static const USBEndpointConfig ep1config = {
  204. USB_EP_MODE_TYPE_BULK,
  205. NULL,
  206. NULL,
  207. NULL,
  208. 0x0040,
  209. 0x0040,
  210. &ep1instate,
  211. &ep1outstate,
  212. 4,
  213. NULL
  214. };
  215. /**
  216. * @brief IN EP2 state.
  217. */
  218. static USBInEndpointState ep2instate;
  219. /**
  220. * @brief EP2 initialization structure (IN only).
  221. */
  222. static const USBEndpointConfig ep2config = {
  223. USB_EP_MODE_TYPE_INTR,
  224. NULL,
  225. NULL,
  226. NULL,
  227. 0x0010,
  228. 0x0000,
  229. &ep2instate,
  230. NULL,
  231. 1,
  232. NULL
  233. };
  234. /*
  235. * Handles the USB driver global events.
  236. */
  237. static void usb_event(USBDriver *usbp, usbevent_t event) {
  238. switch (event) {
  239. case USB_EVENT_RESET:
  240. return;
  241. case USB_EVENT_ADDRESS:
  242. return;
  243. case USB_EVENT_CONFIGURED:
  244. chSysLockFromISR();
  245. /* Enables the endpoints specified into the configuration.
  246. Note, this callback is invoked from an ISR so I-Class functions
  247. must be used.*/
  248. usbInitEndpointI(usbp, USBD2_DATA_REQUEST_EP, &ep1config);
  249. usbInitEndpointI(usbp, USBD2_INTERRUPT_REQUEST_EP, &ep2config);
  250. chSysUnlockFromISR();
  251. return;
  252. case USB_EVENT_UNCONFIGURED:
  253. return;
  254. case USB_EVENT_SUSPEND:
  255. return;
  256. case USB_EVENT_WAKEUP:
  257. return;
  258. case USB_EVENT_STALLED:
  259. return;
  260. }
  261. return;
  262. }
  263. static cdc_linecoding_t linecoding = {
  264. {0x00, 0x96, 0x00, 0x00}, /* 38400. */
  265. LC_STOP_1, LC_PARITY_NONE, 8
  266. };
  267. bool request_hook(USBDriver *usbp) {
  268. if ((usbp->setup[0] & USB_RTYPE_TYPE_MASK) == USB_RTYPE_TYPE_CLASS) {
  269. switch (usbp->setup[1]) {
  270. case CDC_GET_LINE_CODING:
  271. usbSetupTransfer(usbp, (uint8_t *)&linecoding, sizeof(linecoding), NULL);
  272. return true;
  273. case CDC_SET_LINE_CODING:
  274. usbSetupTransfer(usbp, (uint8_t *)&linecoding, sizeof(linecoding), NULL);
  275. return true;
  276. case CDC_SET_CONTROL_LINE_STATE:
  277. /* Nothing to do, there are no control lines.*/
  278. usbSetupTransfer(usbp, NULL, 0, NULL);
  279. return true;
  280. default:
  281. return false;
  282. }
  283. }
  284. return false;
  285. }
  286. /*
  287. * USB driver configuration.
  288. */
  289. const USBConfig usbcfg = {
  290. usb_event,
  291. get_descriptor,
  292. request_hook,
  293. NULL
  294. };