usbcfg.c 12 KB

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