usbcfg.c 14 KB

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