usbcfg.c 13 KB

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