usbcfg.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. ChibiOS - Copyright (C) 2006..2015 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. * This file is free software: you can redistribute it and/or modify it
  15. * under the terms of the GNU General Public License as published by the
  16. * Free Software Foundation, either version 3 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This file is distributed in the hope that it will be useful, but
  20. * WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  22. * See the GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License along
  25. * with this program. If not, see <http://www.gnu.org/licenses/>.
  26. *
  27. * Modified for use in AP_HAL by Andrew Tridgell and Siddharth Bharat Purohit
  28. */
  29. #include "hal.h"
  30. #include "hwdef.h"
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include "usbcfg.h"
  34. // #pragma GCC optimize("O0")
  35. #if defined(HAL_USB_PRODUCT_ID) && !HAL_HAVE_DUAL_USB_CDC
  36. /* Virtual serial port over USB.*/
  37. SerialUSBDriver SDU1;
  38. /*
  39. * Endpoints to be used for USBD1.
  40. */
  41. #define USBD1_DATA_REQUEST_EP 1
  42. #define USBD1_DATA_AVAILABLE_EP 1
  43. #define USBD1_INTERRUPT_REQUEST_EP 2
  44. /*
  45. * USB Device Descriptor.
  46. */
  47. static const uint8_t vcom_device_descriptor_data[18] = {
  48. USB_DESC_DEVICE(
  49. 0x0110, /* bcdUSB (1.1). */
  50. 0x02, /* bDeviceClass (CDC). */
  51. 0x00, /* bDeviceSubClass. */
  52. 0x00, /* bDeviceProtocol. */
  53. 0x40, /* bMaxPacketSize. */
  54. HAL_USB_VENDOR_ID, /* idVendor (ST). */
  55. HAL_USB_PRODUCT_ID, /* idProduct. */
  56. 0x0200, /* bcdDevice. */
  57. 1, /* iManufacturer. */
  58. 2, /* iProduct. */
  59. 3, /* iSerialNumber. */
  60. 1) /* bNumConfigurations. */
  61. };
  62. /*
  63. * Device Descriptor wrapper.
  64. */
  65. static const USBDescriptor vcom_device_descriptor = {
  66. sizeof vcom_device_descriptor_data,
  67. vcom_device_descriptor_data
  68. };
  69. /* Configuration Descriptor tree for a CDC.*/
  70. static const uint8_t vcom_configuration_descriptor_data[67] = {
  71. /* Configuration Descriptor.*/
  72. USB_DESC_CONFIGURATION(67, /* wTotalLength. */
  73. 0x02, /* bNumInterfaces. */
  74. 0x01, /* bConfigurationValue. */
  75. 0, /* iConfiguration. */
  76. 0xC0, /* bmAttributes (self powered). */
  77. 50), /* bMaxPower (100mA). */
  78. /* Interface Descriptor.*/
  79. USB_DESC_INTERFACE (0x00, /* bInterfaceNumber. */
  80. 0x00, /* bAlternateSetting. */
  81. 0x01, /* bNumEndpoints. */
  82. 0x02, /* bInterfaceClass (Communications
  83. Interface Class, CDC section
  84. 4.2). */
  85. 0x02, /* bInterfaceSubClass (Abstract
  86. Control Model, CDC section 4.3). */
  87. 0x01, /* bInterfaceProtocol (AT commands,
  88. CDC section 4.4). */
  89. 0), /* iInterface. */
  90. /* Header Functional Descriptor (CDC section 5.2.3).*/
  91. USB_DESC_BYTE (5), /* bLength. */
  92. USB_DESC_BYTE (0x24), /* bDescriptorType (CS_INTERFACE). */
  93. USB_DESC_BYTE (0x00), /* bDescriptorSubtype (Header
  94. Functional Descriptor. */
  95. USB_DESC_BCD (0x0110), /* bcdCDC. */
  96. /* Call Management Functional Descriptor. */
  97. USB_DESC_BYTE (5), /* bFunctionLength. */
  98. USB_DESC_BYTE (0x24), /* bDescriptorType (CS_INTERFACE). */
  99. USB_DESC_BYTE (0x01), /* bDescriptorSubtype (Call Management
  100. Functional Descriptor). */
  101. USB_DESC_BYTE (0x00), /* bmCapabilities (D0+D1). */
  102. USB_DESC_BYTE (0x01), /* bDataInterface. */
  103. /* ACM Functional Descriptor.*/
  104. USB_DESC_BYTE (4), /* bFunctionLength. */
  105. USB_DESC_BYTE (0x24), /* bDescriptorType (CS_INTERFACE). */
  106. USB_DESC_BYTE (0x02), /* bDescriptorSubtype (Abstract
  107. Control Management Descriptor). */
  108. USB_DESC_BYTE (0x02), /* bmCapabilities. */
  109. /* Union Functional Descriptor.*/
  110. USB_DESC_BYTE (5), /* bFunctionLength. */
  111. USB_DESC_BYTE (0x24), /* bDescriptorType (CS_INTERFACE). */
  112. USB_DESC_BYTE (0x06), /* bDescriptorSubtype (Union
  113. Functional Descriptor). */
  114. USB_DESC_BYTE (0x00), /* bMasterInterface (Communication
  115. Class Interface). */
  116. USB_DESC_BYTE (0x01), /* bSlaveInterface0 (Data Class
  117. Interface). */
  118. /* Endpoint 2 Descriptor.*/
  119. USB_DESC_ENDPOINT (USBD1_INTERRUPT_REQUEST_EP|0x80,
  120. 0x03, /* bmAttributes (Interrupt). */
  121. 0x0008, /* wMaxPacketSize. */
  122. 0xFF), /* bInterval. */
  123. /* Interface Descriptor.*/
  124. USB_DESC_INTERFACE (0x01, /* bInterfaceNumber. */
  125. 0x00, /* bAlternateSetting. */
  126. 0x02, /* bNumEndpoints. */
  127. 0x0A, /* bInterfaceClass (Data Class
  128. Interface, CDC section 4.5). */
  129. 0x00, /* bInterfaceSubClass (CDC section
  130. 4.6). */
  131. 0x00, /* bInterfaceProtocol (CDC section
  132. 4.7). */
  133. 0x00), /* iInterface. */
  134. /* Endpoint 3 Descriptor.*/
  135. USB_DESC_ENDPOINT (USBD1_DATA_AVAILABLE_EP, /* bEndpointAddress.*/
  136. 0x02, /* bmAttributes (Bulk). */
  137. 0x0040, /* wMaxPacketSize. */
  138. 0x00), /* bInterval. */
  139. /* Endpoint 1 Descriptor.*/
  140. USB_DESC_ENDPOINT (USBD1_DATA_REQUEST_EP|0x80, /* bEndpointAddress.*/
  141. 0x02, /* bmAttributes (Bulk). */
  142. 0x0040, /* wMaxPacketSize. */
  143. 0x00) /* bInterval. */
  144. };
  145. /*
  146. * Configuration Descriptor wrapper.
  147. */
  148. static const USBDescriptor vcom_configuration_descriptor = {
  149. sizeof vcom_configuration_descriptor_data,
  150. vcom_configuration_descriptor_data
  151. };
  152. /*
  153. * U.S. English language identifier.
  154. */
  155. static const uint8_t vcom_string0[] = {
  156. USB_DESC_BYTE(4), /* bLength. */
  157. USB_DESC_BYTE(USB_DESCRIPTOR_STRING), /* bDescriptorType. */
  158. USB_DESC_WORD(0x0409) /* wLANGID (U.S. English). */
  159. };
  160. /*
  161. * Strings wrappers array. The strings are created dynamically to
  162. * allow them to be setup with apj_tool
  163. */
  164. static USBDescriptor vcom_strings[] = {
  165. {sizeof vcom_string0, vcom_string0},
  166. {0, NULL}, // manufacturer
  167. {0, NULL}, // product
  168. {0, NULL}, // version
  169. };
  170. static uint8_t vcom_buffers[3][2+2*USB_DESC_MAX_STRLEN];
  171. /*
  172. dynamically allocate a USB descriptor string
  173. */
  174. static void setup_usb_string(USBDescriptor *desc, const char *str, uint8_t *b)
  175. {
  176. char str2[USB_DESC_MAX_STRLEN];
  177. string_substitute(str, str2);
  178. uint8_t len = strlen(str2);
  179. desc->ud_size = 2+2*len;
  180. desc->ud_string = (const uint8_t *)b;
  181. b[0] = USB_DESC_BYTE(desc->ud_size);
  182. b[1] = USB_DESC_BYTE(USB_DESCRIPTOR_STRING);
  183. uint8_t i;
  184. for (i=0; i<len; i++) {
  185. b[2+i*2] = str2[i];
  186. b[2+i*2+1] = 0;
  187. }
  188. }
  189. /*
  190. dynamically allocate a USB descriptor strings
  191. */
  192. void setup_usb_strings(void)
  193. {
  194. setup_usb_string(&vcom_strings[1], HAL_USB_STRING_MANUFACTURER, vcom_buffers[0]);
  195. setup_usb_string(&vcom_strings[2], HAL_USB_STRING_PRODUCT, vcom_buffers[1]);
  196. setup_usb_string(&vcom_strings[3], HAL_USB_STRING_SERIAL, vcom_buffers[2]);
  197. }
  198. /*
  199. * Handles the GET_DESCRIPTOR callback. All required descriptors must be
  200. * handled here.
  201. */
  202. static const USBDescriptor *get_descriptor(USBDriver *usbp,
  203. uint8_t dtype,
  204. uint8_t dindex,
  205. uint16_t lang) {
  206. (void)usbp;
  207. (void)lang;
  208. switch (dtype) {
  209. case USB_DESCRIPTOR_DEVICE:
  210. return &vcom_device_descriptor;
  211. case USB_DESCRIPTOR_CONFIGURATION:
  212. return &vcom_configuration_descriptor;
  213. case USB_DESCRIPTOR_STRING:
  214. if (dindex < 4) {
  215. return &vcom_strings[dindex];
  216. }
  217. }
  218. return NULL;
  219. }
  220. /**
  221. * @brief IN EP1 state.
  222. */
  223. static USBInEndpointState ep1instate;
  224. /**
  225. * @brief OUT EP1 state.
  226. */
  227. static USBOutEndpointState ep1outstate;
  228. /**
  229. * @brief EP1 initialization structure (both IN and OUT).
  230. */
  231. static const USBEndpointConfig ep1config = {
  232. USB_EP_MODE_TYPE_BULK,
  233. NULL,
  234. sduDataTransmitted,
  235. sduDataReceived,
  236. 0x0040,
  237. 0x0040,
  238. &ep1instate,
  239. &ep1outstate,
  240. 2,
  241. NULL
  242. };
  243. /**
  244. * @brief IN EP2 state.
  245. */
  246. static USBInEndpointState ep2instate;
  247. /**
  248. * @brief EP2 initialization structure (IN only).
  249. */
  250. static const USBEndpointConfig ep2config = {
  251. USB_EP_MODE_TYPE_INTR,
  252. NULL,
  253. sduInterruptTransmitted,
  254. NULL,
  255. 0x0010,
  256. 0x0000,
  257. &ep2instate,
  258. NULL,
  259. 1,
  260. NULL
  261. };
  262. /*
  263. * Handles the USB driver global events.
  264. */
  265. static void usb_event(USBDriver *usbp, usbevent_t event) {
  266. extern SerialUSBDriver SDU1;
  267. switch (event) {
  268. case USB_EVENT_ADDRESS:
  269. return;
  270. case USB_EVENT_CONFIGURED:
  271. chSysLockFromISR();
  272. /* Enables the endpoints specified into the configuration.
  273. Note, this callback is invoked from an ISR so I-Class functions
  274. must be used.*/
  275. usbInitEndpointI(usbp, USBD1_DATA_REQUEST_EP, &ep1config);
  276. usbInitEndpointI(usbp, USBD1_INTERRUPT_REQUEST_EP, &ep2config);
  277. /* Resetting the state of the CDC subsystem.*/
  278. sduConfigureHookI(&SDU1);
  279. chSysUnlockFromISR();
  280. return;
  281. case USB_EVENT_RESET:
  282. /* Falls into.*/
  283. case USB_EVENT_UNCONFIGURED:
  284. /* Falls into.*/
  285. case USB_EVENT_SUSPEND:
  286. chSysLockFromISR();
  287. /* Disconnection event on suspend.*/
  288. sduSuspendHookI(&SDU1);
  289. chSysUnlockFromISR();
  290. return;
  291. case USB_EVENT_WAKEUP:
  292. chSysLockFromISR();
  293. /* Disconnection event on suspend.*/
  294. sduWakeupHookI(&SDU1);
  295. chSysUnlockFromISR();
  296. return;
  297. case USB_EVENT_STALLED:
  298. return;
  299. }
  300. return;
  301. }
  302. /*
  303. * Handles the USB driver global events.
  304. */
  305. static void sof_handler(USBDriver *usbp) {
  306. (void)usbp;
  307. osalSysLockFromISR();
  308. sduSOFHookI(&SDU1);
  309. osalSysUnlockFromISR();
  310. }
  311. /*
  312. * USB driver configuration.
  313. */
  314. const USBConfig usbcfg = {
  315. usb_event,
  316. get_descriptor,
  317. sduRequestsHook,
  318. sof_handler
  319. };
  320. /*
  321. * Serial over USB driver configuration.
  322. */
  323. const SerialUSBConfig serusbcfg1 = {
  324. &USBD1,
  325. USBD1_DATA_REQUEST_EP,
  326. USBD1_DATA_AVAILABLE_EP,
  327. USBD1_INTERRUPT_REQUEST_EP
  328. };
  329. #endif // HAL_USB_PRODUCT_ID