usbcfg.c 12 KB

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