You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

552 lines
16KB

  1. /* USB Serial for Teensy USB Development Board
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2008 PJRC.COM, LLC
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. #include "usb_common.h"
  24. #include "usb_private.h"
  25. /**************************************************************************
  26. *
  27. * Endpoint Buffer Configuration
  28. *
  29. **************************************************************************/
  30. static const uint8_t PROGMEM endpoint_config_table[] = {
  31. 0,
  32. 1, EP_TYPE_INTERRUPT_IN, EP_SIZE(CDC_ACM_SIZE) | CDC_ACM_BUFFER,
  33. 1, EP_TYPE_BULK_OUT, EP_SIZE(CDC_RX_SIZE) | CDC_RX_BUFFER,
  34. 1, EP_TYPE_BULK_IN, EP_SIZE(CDC_TX_SIZE) | CDC_TX_BUFFER
  35. };
  36. /**************************************************************************
  37. *
  38. * Descriptor Data
  39. *
  40. **************************************************************************/
  41. // Descriptors are the data that your computer reads when it auto-detects
  42. // this USB device (called "enumeration" in USB lingo). The most commonly
  43. // changed items are editable at the top of this file. Changing things
  44. // in here should only be done by those who've read chapter 9 of the USB
  45. // spec and relevant portions of any USB class specifications!
  46. static const uint8_t PROGMEM device_descriptor[] = {
  47. 18, // bLength
  48. 1, // bDescriptorType
  49. 0x00, 0x02, // bcdUSB
  50. 2, // bDeviceClass
  51. 0, // bDeviceSubClass
  52. 0, // bDeviceProtocol
  53. ENDPOINT0_SIZE, // bMaxPacketSize0
  54. LSB(VENDOR_ID), MSB(VENDOR_ID), // idVendor
  55. LSB(PRODUCT_ID), MSB(PRODUCT_ID), // idProduct
  56. 0x00, 0x01, // bcdDevice
  57. 1, // iManufacturer
  58. 2, // iProduct
  59. 3, // iSerialNumber
  60. 1 // bNumConfigurations
  61. };
  62. #define CONFIG1_DESC_SIZE (9+9+5+5+4+5+7+9+7+7)
  63. static const uint8_t PROGMEM config1_descriptor[CONFIG1_DESC_SIZE] = {
  64. // configuration descriptor, USB spec 9.6.3, page 264-266, Table 9-10
  65. 9, // bLength;
  66. 2, // bDescriptorType;
  67. LSB(CONFIG1_DESC_SIZE), // wTotalLength
  68. MSB(CONFIG1_DESC_SIZE),
  69. 2, // bNumInterfaces
  70. 1, // bConfigurationValue
  71. 0, // iConfiguration
  72. 0xC0, // bmAttributes
  73. 50, // bMaxPower
  74. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  75. 9, // bLength
  76. 4, // bDescriptorType
  77. 0, // bInterfaceNumber
  78. 0, // bAlternateSetting
  79. 1, // bNumEndpoints
  80. 0x02, // bInterfaceClass
  81. 0x02, // bInterfaceSubClass
  82. 0x01, // bInterfaceProtocol
  83. 0, // iInterface
  84. // CDC Header Functional Descriptor, CDC Spec 5.2.3.1, Table 26
  85. 5, // bFunctionLength
  86. 0x24, // bDescriptorType
  87. 0x00, // bDescriptorSubtype
  88. 0x10, 0x01, // bcdCDC
  89. // Call Management Functional Descriptor, CDC Spec 5.2.3.2, Table 27
  90. 5, // bFunctionLength
  91. 0x24, // bDescriptorType
  92. 0x01, // bDescriptorSubtype
  93. 0x00, // bmCapabilities
  94. 1, // bDataInterface
  95. // Abstract Control Management Functional Descriptor, CDC Spec 5.2.3.3, Table 28
  96. 4, // bFunctionLength
  97. 0x24, // bDescriptorType
  98. 0x02, // bDescriptorSubtype
  99. 0x06, // bmCapabilities
  100. // Union Functional Descriptor, CDC Spec 5.2.3.8, Table 33
  101. 5, // bFunctionLength
  102. 0x24, // bDescriptorType
  103. 0x06, // bDescriptorSubtype
  104. 0, // bMasterInterface
  105. 1, // bSlaveInterface0
  106. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  107. 7, // bLength
  108. 5, // bDescriptorType
  109. CDC_ACM_ENDPOINT | 0x80, // bEndpointAddress
  110. 0x03, // bmAttributes (0x03=intr)
  111. CDC_ACM_SIZE, 0, // wMaxPacketSize
  112. 64, // bInterval
  113. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  114. 9, // bLength
  115. 4, // bDescriptorType
  116. 1, // bInterfaceNumber
  117. 0, // bAlternateSetting
  118. 2, // bNumEndpoints
  119. 0x0A, // bInterfaceClass
  120. 0x00, // bInterfaceSubClass
  121. 0x00, // bInterfaceProtocol
  122. 0, // iInterface
  123. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  124. 7, // bLength
  125. 5, // bDescriptorType
  126. CDC_RX_ENDPOINT, // bEndpointAddress
  127. 0x02, // bmAttributes (0x02=bulk)
  128. CDC_RX_SIZE, 0, // wMaxPacketSize
  129. 0, // bInterval
  130. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  131. 7, // bLength
  132. 5, // bDescriptorType
  133. CDC_TX_ENDPOINT | 0x80, // bEndpointAddress
  134. 0x02, // bmAttributes (0x02=bulk)
  135. CDC_TX_SIZE, 0, // wMaxPacketSize
  136. 0 // bInterval
  137. };
  138. // If you're desperate for a little extra code memory, these strings
  139. // can be completely removed if iManufacturer, iProduct, iSerialNumber
  140. // in the device desciptor are changed to zeros.
  141. struct usb_string_descriptor_struct {
  142. uint8_t bLength;
  143. uint8_t bDescriptorType;
  144. int16_t wString[];
  145. };
  146. static const struct usb_string_descriptor_struct PROGMEM string0 = {
  147. 4,
  148. 3,
  149. {0x0409}
  150. };
  151. static const struct usb_string_descriptor_struct PROGMEM string1 = {
  152. sizeof(STR_MANUFACTURER),
  153. 3,
  154. STR_MANUFACTURER
  155. };
  156. static const struct usb_string_descriptor_struct PROGMEM string2 = {
  157. sizeof(STR_PRODUCT),
  158. 3,
  159. STR_PRODUCT
  160. };
  161. static const struct usb_string_descriptor_struct PROGMEM string3 = {
  162. sizeof(STR_SERIAL_NUMBER),
  163. 3,
  164. STR_SERIAL_NUMBER
  165. };
  166. // This table defines which descriptor data is sent for each specific
  167. // request from the host (in wValue and wIndex).
  168. static const struct descriptor_list_struct {
  169. uint16_t wValue;
  170. uint16_t wIndex;
  171. const uint8_t *addr;
  172. uint8_t length;
  173. } PROGMEM descriptor_list[] = {
  174. {0x0100, 0x0000, device_descriptor, sizeof(device_descriptor)},
  175. {0x0200, 0x0000, config1_descriptor, sizeof(config1_descriptor)},
  176. {0x0300, 0x0000, (const uint8_t *)&string0, 4},
  177. {0x0301, 0x0409, (const uint8_t *)&string1, sizeof(STR_MANUFACTURER)},
  178. {0x0302, 0x0409, (const uint8_t *)&string2, sizeof(STR_PRODUCT)},
  179. {0x0303, 0x0409, (const uint8_t *)&string3, sizeof(STR_SERIAL_NUMBER)}
  180. };
  181. #define NUM_DESC_LIST (sizeof(descriptor_list)/sizeof(struct descriptor_list_struct))
  182. /**************************************************************************
  183. *
  184. * Variables - these are the only non-stack RAM usage
  185. *
  186. **************************************************************************/
  187. // zero when we are not configured, non-zero when enumerated
  188. volatile uint8_t usb_configuration USBSTATE;
  189. volatile uint8_t usb_suspended USBSTATE;
  190. // the time remaining before we transmit any partially full
  191. // packet, or send a zero length packet.
  192. volatile uint8_t transmit_flush_timer=0;
  193. volatile uint8_t reboot_timer=0;
  194. uint8_t transmit_previous_timeout=0;
  195. // serial port settings (baud rate, control signals, etc) set
  196. // by the PC. These are ignored, but kept in RAM because the
  197. // CDC spec requires a read that returns the current settings.
  198. volatile uint8_t cdc_line_coding[7]={0x00, 0xE1, 0x00, 0x00, 0x00, 0x00, 0x08};
  199. volatile uint8_t cdc_line_rtsdtr USBSTATE;
  200. /**************************************************************************
  201. *
  202. * Public Functions - these are the API intended for the user
  203. *
  204. **************************************************************************/
  205. // initialize USB serial
  206. void usb_init(void)
  207. {
  208. uint8_t u;
  209. u = USBCON;
  210. if ((u & (1<<USBE)) && !(u & (1<<FRZCLK))) return;
  211. HW_CONFIG();
  212. USB_FREEZE(); // enable USB
  213. PLL_CONFIG(); // config PLL
  214. while (!(PLLCSR & (1<<PLOCK))) ; // wait for PLL lock
  215. USB_CONFIG(); // start USB clock
  216. UDCON = 0; // enable attach resistor
  217. usb_configuration = 0;
  218. usb_suspended = 0;
  219. cdc_line_rtsdtr = 0;
  220. UDINT = 0;
  221. UDIEN = (1<<EORSTE)|(1<<SOFE)|(1<<SUSPE);
  222. }
  223. void usb_shutdown(void)
  224. {
  225. UDIEN = 0; // disable interrupts
  226. UDCON = 1; // disconnect attach resistor
  227. USBCON = 0; // shut off USB periperal
  228. PLLCSR = 0; // shut off PLL
  229. usb_configuration = 0;
  230. usb_suspended = 1;
  231. }
  232. // Public API functions moved to usb_api.cpp
  233. /**************************************************************************
  234. *
  235. * Private Functions - not intended for general user consumption....
  236. *
  237. **************************************************************************/
  238. // USB Device Interrupt - handle all device-level events
  239. // the transmit buffer flushing is triggered by the start of frame
  240. //
  241. ISR(USB_GEN_vect)
  242. {
  243. uint8_t intbits, t;
  244. intbits = UDINT;
  245. UDINT = 0;
  246. if (intbits & (1<<EORSTI)) {
  247. // USB Reset
  248. UENUM = 0;
  249. UECONX = 1;
  250. UECFG0X = EP_TYPE_CONTROL;
  251. UECFG1X = EP_SIZE(ENDPOINT0_SIZE) | EP_SINGLE_BUFFER;
  252. UEIENX = (1<<RXSTPE);
  253. usb_configuration = 0;
  254. cdc_line_rtsdtr = 0;
  255. }
  256. if (intbits & (1<<SOFI)) {
  257. // Start Of Frame
  258. if (usb_configuration) {
  259. t = transmit_flush_timer;
  260. if (t) {
  261. transmit_flush_timer = --t;
  262. if (!t) {
  263. UENUM = CDC_TX_ENDPOINT;
  264. UEINTX = 0x3A;
  265. }
  266. }
  267. t = reboot_timer;
  268. if (t) {
  269. reboot_timer = --t;
  270. if (!t) _reboot_Teensyduino_();
  271. }
  272. }
  273. }
  274. // in active state
  275. if (intbits & (1<<SUSPI)) {
  276. // USB Suspend (inactivity for 3ms)
  277. UDIEN = (1<<WAKEUPE);
  278. usb_configuration = 0;
  279. usb_suspended = 1;
  280. #if (F_CPU >= 8000000L)
  281. // WAKEUPI does not work with USB clock freeze
  282. // when CPU is running less than 8 MHz.
  283. // Is this a hardware bug?
  284. USB_FREEZE(); // shut off USB
  285. PLLCSR = 0; // shut off PLL
  286. #endif
  287. // to properly meet the USB spec, current must
  288. // reduce to less than 2.5 mA, which means using
  289. // powerdown mode, but that breaks the Arduino
  290. // user's paradigm....
  291. }
  292. if (usb_suspended && (intbits & (1<<WAKEUPI))) {
  293. // USB Resume (pretty much any activity)
  294. #if (F_CPU >= 8000000L)
  295. PLL_CONFIG();
  296. while (!(PLLCSR & (1<<PLOCK))) ;
  297. USB_CONFIG();
  298. #endif
  299. UDIEN = (1<<EORSTE)|(1<<SOFE)|(1<<SUSPE);
  300. usb_suspended = 0;
  301. return;
  302. }
  303. }
  304. // Misc functions to wait for ready and send/receive packets
  305. static inline void usb_wait_in_ready(void)
  306. {
  307. while (!(UEINTX & (1<<TXINI))) ;
  308. }
  309. static inline void usb_send_in(void)
  310. {
  311. UEINTX = ~(1<<TXINI);
  312. }
  313. static inline void usb_wait_receive_out(void)
  314. {
  315. while (!(UEINTX & (1<<RXOUTI))) ;
  316. }
  317. static inline void usb_ack_out(void)
  318. {
  319. UEINTX = ~(1<<RXOUTI);
  320. }
  321. // USB Endpoint Interrupt - endpoint 0 is handled here. The
  322. // other endpoints are manipulated by the user-callable
  323. // functions, and the start-of-frame interrupt.
  324. //
  325. ISR(USB_COM_vect)
  326. {
  327. uint8_t intbits;
  328. const uint8_t *list;
  329. const uint8_t *cfg;
  330. uint8_t i, n, len, en;
  331. volatile uint8_t *p;
  332. uint8_t bmRequestType;
  333. uint8_t bRequest;
  334. uint16_t wValue;
  335. uint16_t wIndex;
  336. uint16_t wLength;
  337. uint16_t desc_val;
  338. uint32_t baud;
  339. const uint8_t *desc_addr;
  340. uint8_t desc_length;
  341. UENUM = 0;
  342. intbits = UEINTX;
  343. if (intbits & (1<<RXSTPI)) {
  344. bmRequestType = UEDATX;
  345. bRequest = UEDATX;
  346. //wValue = UEDATX;
  347. //wValue |= (UEDATX << 8);
  348. read_word_lsbfirst(wValue, UEDATX);
  349. //wIndex = UEDATX;
  350. //wIndex |= (UEDATX << 8);
  351. read_word_lsbfirst(wIndex, UEDATX);
  352. //wLength = UEDATX;
  353. //wLength |= (UEDATX << 8);
  354. read_word_lsbfirst(wLength, UEDATX);
  355. UEINTX = ~((1<<RXSTPI) | (1<<RXOUTI) | (1<<TXINI));
  356. if (bRequest == GET_DESCRIPTOR) {
  357. list = (const uint8_t *)descriptor_list;
  358. for (i=0; ; i++) {
  359. if (i >= NUM_DESC_LIST) {
  360. UECONX = (1<<STALLRQ)|(1<<EPEN); //stall
  361. return;
  362. }
  363. //desc_val = pgm_read_word(list);
  364. //list += 2;
  365. pgm_read_word_postinc(desc_val, list);
  366. if (desc_val != wValue) {
  367. list += sizeof(struct descriptor_list_struct)-2;
  368. continue;
  369. }
  370. //desc_val = pgm_read_word(list);
  371. //list += 2;
  372. pgm_read_word_postinc(desc_val, list);
  373. if (desc_val != wIndex) {
  374. list += sizeof(struct descriptor_list_struct)-4;
  375. continue;
  376. }
  377. //desc_addr = (const uint8_t *)pgm_read_word(list);
  378. //list += 2;
  379. pgm_read_word_postinc(desc_addr, list);
  380. desc_length = pgm_read_byte(list);
  381. break;
  382. }
  383. len = (wLength < 256) ? wLength : 255;
  384. if (len > desc_length) len = desc_length;
  385. list = desc_addr;
  386. do {
  387. // wait for host ready for IN packet
  388. do {
  389. i = UEINTX;
  390. } while (!(i & ((1<<TXINI)|(1<<RXOUTI))));
  391. if (i & (1<<RXOUTI)) return; // abort
  392. // send IN packet
  393. n = len < ENDPOINT0_SIZE ? len : ENDPOINT0_SIZE;
  394. for (i = n; i; i--) {
  395. //UEDATX = pgm_read_byte(desc_addr++);
  396. //pgm_read_byte_postinc(UEDATX, desc_addr);
  397. pgm_read_byte_postinc(UEDATX, list);
  398. }
  399. len -= n;
  400. usb_send_in();
  401. } while (len || n == ENDPOINT0_SIZE);
  402. return;
  403. }
  404. if (bRequest == SET_ADDRESS) {
  405. usb_send_in();
  406. usb_wait_in_ready();
  407. UDADDR = wValue | (1<<ADDEN);
  408. return;
  409. }
  410. if (bRequest == SET_CONFIGURATION && bmRequestType == 0) {
  411. usb_configuration = wValue;
  412. cdc_line_rtsdtr = 0;
  413. transmit_flush_timer = 0;
  414. usb_send_in();
  415. cfg = endpoint_config_table;
  416. for (i=1; i<5; i++) {
  417. UENUM = i;
  418. //en = pgm_read_byte(cfg++);
  419. pgm_read_byte_postinc(en, cfg);
  420. UECONX = en;
  421. if (en) {
  422. //UECFG0X = pgm_read_byte(cfg++);
  423. //UECFG1X = pgm_read_byte(cfg++);
  424. pgm_read_byte_postinc(UECFG0X, cfg);
  425. pgm_read_byte_postinc(UECFG1X, cfg);
  426. }
  427. }
  428. UERST = 0x1E;
  429. UERST = 0;
  430. return;
  431. }
  432. if (bRequest == GET_CONFIGURATION && bmRequestType == 0x80) {
  433. usb_wait_in_ready();
  434. UEDATX = usb_configuration;
  435. usb_send_in();
  436. return;
  437. }
  438. if (bRequest == CDC_GET_LINE_CODING /* 0x21 */ && bmRequestType == 0xA1) {
  439. usb_wait_in_ready();
  440. p = cdc_line_coding;
  441. for (i=0; i<7; i++) {
  442. UEDATX = *p++;
  443. }
  444. usb_send_in();
  445. return;
  446. }
  447. if (bRequest == CDC_SET_LINE_CODING /* 0x20 */ && bmRequestType == 0x21) {
  448. usb_wait_receive_out();
  449. p = cdc_line_coding;
  450. for (i=0; i<7; i++) {
  451. *p++ = UEDATX;
  452. }
  453. usb_ack_out();
  454. usb_send_in();
  455. baud = (uint32_t)cdc_line_coding[0]
  456. | ((uint32_t)cdc_line_coding[1] << 8)
  457. | ((uint32_t)cdc_line_coding[2] << 16)
  458. | ((uint32_t)cdc_line_coding[3] << 24);
  459. if (baud == 134UL) reboot_timer = 15;
  460. if (baud == 150UL) {
  461. UENUM = CDC_TX_ENDPOINT;
  462. while (UESTA0X & 0x03) {
  463. UEINTX = 0xFF;
  464. while (UEINTX & 0x04) /* TODO: timeout? */ ;
  465. }
  466. _restart_Teensyduino_();
  467. }
  468. return;
  469. }
  470. if (bRequest == CDC_SET_CONTROL_LINE_STATE /* 0x22 */ && bmRequestType == 0x21) {
  471. cdc_line_rtsdtr = wValue;
  472. usb_wait_in_ready();
  473. usb_send_in();
  474. return;
  475. }
  476. if (bRequest == CDC_SEND_BREAK /* 0x23 */ && bmRequestType == 0x21) {
  477. usb_wait_in_ready();
  478. usb_send_in();
  479. return;
  480. }
  481. if (bRequest == GET_STATUS) {
  482. usb_wait_in_ready();
  483. i = 0;
  484. if (bmRequestType == 0x82) {
  485. UENUM = wIndex;
  486. if (UECONX & (1<<STALLRQ)) i = 1;
  487. UENUM = 0;
  488. }
  489. UEDATX = i;
  490. UEDATX = 0;
  491. usb_send_in();
  492. return;
  493. }
  494. if ((bRequest == CLEAR_FEATURE || bRequest == SET_FEATURE)
  495. && bmRequestType == 0x02 && wValue == 0) {
  496. i = wIndex & 0x7F;
  497. if (i >= 1 && i <= MAX_ENDPOINT) {
  498. usb_send_in();
  499. UENUM = i;
  500. if (bRequest == SET_FEATURE) {
  501. UECONX = (1<<STALLRQ)|(1<<EPEN);
  502. } else {
  503. UECONX = (1<<STALLRQC)|(1<<RSTDT)|(1<<EPEN);
  504. UERST = (1 << i);
  505. UERST = 0;
  506. }
  507. return;
  508. }
  509. }
  510. }
  511. UECONX = (1<<STALLRQ) | (1<<EPEN); // stall
  512. }