Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

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