Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

547 lines
15KB

  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. const uint8_t *desc_addr;
  339. uint8_t desc_length;
  340. UENUM = 0;
  341. intbits = UEINTX;
  342. if (intbits & (1<<RXSTPI)) {
  343. bmRequestType = UEDATX;
  344. bRequest = UEDATX;
  345. //wValue = UEDATX;
  346. //wValue |= (UEDATX << 8);
  347. read_word_lsbfirst(wValue, UEDATX);
  348. //wIndex = UEDATX;
  349. //wIndex |= (UEDATX << 8);
  350. read_word_lsbfirst(wIndex, UEDATX);
  351. //wLength = UEDATX;
  352. //wLength |= (UEDATX << 8);
  353. read_word_lsbfirst(wLength, UEDATX);
  354. UEINTX = ~((1<<RXSTPI) | (1<<RXOUTI) | (1<<TXINI));
  355. if (bRequest == GET_DESCRIPTOR) {
  356. list = (const uint8_t *)descriptor_list;
  357. for (i=0; ; i++) {
  358. if (i >= NUM_DESC_LIST) {
  359. UECONX = (1<<STALLRQ)|(1<<EPEN); //stall
  360. return;
  361. }
  362. //desc_val = pgm_read_word(list);
  363. //list += 2;
  364. pgm_read_word_postinc(desc_val, list);
  365. if (desc_val != wValue) {
  366. list += sizeof(struct descriptor_list_struct)-2;
  367. continue;
  368. }
  369. //desc_val = pgm_read_word(list);
  370. //list += 2;
  371. pgm_read_word_postinc(desc_val, list);
  372. if (desc_val != wIndex) {
  373. list += sizeof(struct descriptor_list_struct)-4;
  374. continue;
  375. }
  376. //desc_addr = (const uint8_t *)pgm_read_word(list);
  377. //list += 2;
  378. pgm_read_word_postinc(desc_addr, list);
  379. desc_length = pgm_read_byte(list);
  380. break;
  381. }
  382. len = (wLength < 256) ? wLength : 255;
  383. if (len > desc_length) len = desc_length;
  384. list = desc_addr;
  385. do {
  386. // wait for host ready for IN packet
  387. do {
  388. i = UEINTX;
  389. } while (!(i & ((1<<TXINI)|(1<<RXOUTI))));
  390. if (i & (1<<RXOUTI)) return; // abort
  391. // send IN packet
  392. n = len < ENDPOINT0_SIZE ? len : ENDPOINT0_SIZE;
  393. for (i = n; i; i--) {
  394. //UEDATX = pgm_read_byte(desc_addr++);
  395. //pgm_read_byte_postinc(UEDATX, desc_addr);
  396. pgm_read_byte_postinc(UEDATX, list);
  397. }
  398. len -= n;
  399. usb_send_in();
  400. } while (len || n == ENDPOINT0_SIZE);
  401. return;
  402. }
  403. if (bRequest == SET_ADDRESS) {
  404. usb_send_in();
  405. usb_wait_in_ready();
  406. UDADDR = wValue | (1<<ADDEN);
  407. return;
  408. }
  409. if (bRequest == SET_CONFIGURATION && bmRequestType == 0) {
  410. usb_configuration = wValue;
  411. cdc_line_rtsdtr = 0;
  412. transmit_flush_timer = 0;
  413. usb_send_in();
  414. cfg = endpoint_config_table;
  415. for (i=1; i<5; i++) {
  416. UENUM = i;
  417. //en = pgm_read_byte(cfg++);
  418. pgm_read_byte_postinc(en, cfg);
  419. UECONX = en;
  420. if (en) {
  421. //UECFG0X = pgm_read_byte(cfg++);
  422. //UECFG1X = pgm_read_byte(cfg++);
  423. pgm_read_byte_postinc(UECFG0X, cfg);
  424. pgm_read_byte_postinc(UECFG1X, cfg);
  425. }
  426. }
  427. UERST = 0x1E;
  428. UERST = 0;
  429. return;
  430. }
  431. if (bRequest == GET_CONFIGURATION && bmRequestType == 0x80) {
  432. usb_wait_in_ready();
  433. UEDATX = usb_configuration;
  434. usb_send_in();
  435. return;
  436. }
  437. if (bRequest == CDC_GET_LINE_CODING /* 0x21 */ && bmRequestType == 0xA1) {
  438. usb_wait_in_ready();
  439. p = cdc_line_coding;
  440. for (i=0; i<7; i++) {
  441. UEDATX = *p++;
  442. }
  443. usb_send_in();
  444. return;
  445. }
  446. if (bRequest == CDC_SET_LINE_CODING /* 0x20 */ && bmRequestType == 0x21) {
  447. usb_wait_receive_out();
  448. p = cdc_line_coding;
  449. for (i=0; i<7; i++) {
  450. *p++ = UEDATX;
  451. }
  452. usb_ack_out();
  453. usb_send_in();
  454. if (*(long *)cdc_line_coding == 134L) reboot_timer = 15;
  455. if (*(long *)cdc_line_coding == 150L) {
  456. UENUM = CDC_TX_ENDPOINT;
  457. while (UESTA0X & 0x03) {
  458. UEINTX = 0xFF;
  459. while (UEINTX & 0x04) /* TODO: timeout? */ ;
  460. }
  461. _restart_Teensyduino_();
  462. }
  463. return;
  464. }
  465. if (bRequest == CDC_SET_CONTROL_LINE_STATE /* 0x22 */ && bmRequestType == 0x21) {
  466. cdc_line_rtsdtr = wValue;
  467. usb_wait_in_ready();
  468. usb_send_in();
  469. return;
  470. }
  471. if (bRequest == CDC_SEND_BREAK /* 0x23 */ && bmRequestType == 0x21) {
  472. usb_wait_in_ready();
  473. usb_send_in();
  474. return;
  475. }
  476. if (bRequest == GET_STATUS) {
  477. usb_wait_in_ready();
  478. i = 0;
  479. if (bmRequestType == 0x82) {
  480. UENUM = wIndex;
  481. if (UECONX & (1<<STALLRQ)) i = 1;
  482. UENUM = 0;
  483. }
  484. UEDATX = i;
  485. UEDATX = 0;
  486. usb_send_in();
  487. return;
  488. }
  489. if ((bRequest == CLEAR_FEATURE || bRequest == SET_FEATURE)
  490. && bmRequestType == 0x02 && wValue == 0) {
  491. i = wIndex & 0x7F;
  492. if (i >= 1 && i <= MAX_ENDPOINT) {
  493. usb_send_in();
  494. UENUM = i;
  495. if (bRequest == SET_FEATURE) {
  496. UECONX = (1<<STALLRQ)|(1<<EPEN);
  497. } else {
  498. UECONX = (1<<STALLRQC)|(1<<RSTDT)|(1<<EPEN);
  499. UERST = (1 << i);
  500. UERST = 0;
  501. }
  502. return;
  503. }
  504. }
  505. }
  506. UECONX = (1<<STALLRQ) | (1<<EPEN); // stall
  507. }