Kiibohd Controller
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  1. /* USB Keyboard and CDC Serial Device for Teensy USB Development Board
  2. * Copyright (c) 2009 PJRC.COM, LLC
  3. * Modifications by Jacob Alexander (2011-2014)
  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. // Local Includes
  24. #include "usb_keyboard_serial.h"
  25. // ----- Functions -----
  26. // Set the avr into firmware reload mode
  27. void usb_debug_reload()
  28. {
  29. cli();
  30. // Disable watchdog, if enabled
  31. // Disable all peripherals
  32. UDCON = 1;
  33. USBCON = (1 << FRZCLK); // Disable USB
  34. UCSR1B = 0;
  35. _delay_ms( 5 );
  36. #if defined(__AVR_AT90USB162__) // Teensy 1.0
  37. EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0;
  38. TIMSK0 = 0; TIMSK1 = 0; UCSR1B = 0;
  39. DDRB = 0; DDRC = 0; DDRD = 0;
  40. PORTB = 0; PORTC = 0; PORTD = 0;
  41. asm volatile("jmp 0x3E00");
  42. #elif defined(__AVR_ATmega32U4__) // Teensy 2.0
  43. EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
  44. TIMSK0 = 0; TIMSK1 = 0; TIMSK3 = 0; TIMSK4 = 0; UCSR1B = 0; TWCR = 0;
  45. DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0; TWCR = 0;
  46. PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
  47. asm volatile("jmp 0x7E00");
  48. #elif defined(__AVR_AT90USB646__) // Teensy++ 1.0
  49. EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
  50. TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
  51. DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
  52. PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
  53. asm volatile("jmp 0xFC00");
  54. #elif defined(__AVR_AT90USB1286__) // Teensy++ 2.0
  55. EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
  56. TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
  57. DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
  58. PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
  59. asm volatile("jmp 0x1FC00");
  60. #endif
  61. }
  62. // WDT Setup for software reset the chip
  63. void wdt_init(void)
  64. {
  65. MCUSR = 0;
  66. wdt_disable();
  67. }
  68. /**************************************************************************
  69. *
  70. * Configurable Options
  71. *
  72. **************************************************************************/
  73. // When you write data, it goes into a USB endpoint buffer, which
  74. // is transmitted to the PC when it becomes full, or after a timeout
  75. // with no more writes. Even if you write in exactly packet-size
  76. // increments, this timeout is used to send a "zero length packet"
  77. // that tells the PC no more data is expected and it should pass
  78. // any buffered data to the application that may be waiting. If
  79. // you want data sent immediately, call usb_serial_flush_output().
  80. #define TRANSMIT_FLUSH_TIMEOUT 5 /* in milliseconds */
  81. // If the PC is connected but not "listening", this is the length
  82. // of time before usb_serial_getchar() returns with an error. This
  83. // is roughly equivilant to a real UART simply transmitting the
  84. // bits on a wire where nobody is listening, except you get an error
  85. // code which you can ignore for serial-like discard of data, or
  86. // use to know your data wasn't sent.
  87. #define TRANSMIT_TIMEOUT 25 /* in milliseconds */
  88. // USB devices are supposed to implment a halt feature, which is
  89. // rarely (if ever) used. If you comment this line out, the halt
  90. // code will be removed, saving 116 bytes of space (gcc 4.3.0).
  91. // This is not strictly USB compliant, but works with all major
  92. // operating systems.
  93. #define SUPPORT_ENDPOINT_HALT
  94. /**************************************************************************
  95. *
  96. * Descriptor Data
  97. *
  98. **************************************************************************/
  99. // Descriptors are the data that your computer reads when it auto-detects
  100. // this USB device (called "enumeration" in USB lingo). The most commonly
  101. // changed items are editable at the top of this file. Changing things
  102. // in here should only be done by those who've read chapter 9 of the USB
  103. // spec and relevant portions of any USB class specifications!
  104. static const uint8_t PROGMEM device_descriptor[] = {
  105. 18, // bLength
  106. 1, // bDescriptorType
  107. 0x00, 0x02, // bcdUSB
  108. 0, // bDeviceClass
  109. 0, // bDeviceSubClass
  110. 0, // bDeviceProtocol
  111. ENDPOINT0_SIZE, // bMaxPacketSize0
  112. LSB(VENDOR_ID), MSB(VENDOR_ID), // idVendor
  113. LSB(PRODUCT_ID), MSB(PRODUCT_ID), // idProduct
  114. 0x00, 0x01, // bcdDevice
  115. 1, // iManufacturer
  116. 2, // iProduct
  117. 3, // iSerialNumber
  118. 1 // bNumConfigurations
  119. };
  120. // Keyboard Protocol 1, HID 1.11 spec, Appendix B, page 59-60
  121. static const uint8_t PROGMEM keyboard_hid_report_desc[] = {
  122. 0x05, 0x01, // Usage Page (Generic Desktop),
  123. 0x09, 0x06, // Usage (Keyboard),
  124. 0xA1, 0x01, // Collection (Application),
  125. 0x75, 0x01, // Report Size (1),
  126. 0x95, 0x08, // Report Count (8),
  127. 0x05, 0x07, // Usage Page (Key Codes),
  128. 0x19, 0xE0, // Usage Minimum (224),
  129. 0x29, 0xE7, // Usage Maximum (231),
  130. 0x15, 0x00, // Logical Minimum (0),
  131. 0x25, 0x01, // Logical Maximum (1),
  132. 0x81, 0x02, // Input (Data, Variable, Absolute), ;Modifier byte
  133. 0x95, 0x08, // Report Count (8),
  134. 0x75, 0x01, // Report Size (1),
  135. 0x15, 0x00, // Logical Minimum (0),
  136. 0x25, 0x01, // Logical Maximum (1),
  137. 0x05, 0x0C, // Usage Page (Consumer),
  138. 0x09, 0xE9, // Usage (Volume Increment),
  139. 0x09, 0xEA, // Usage (Volume Decrement),
  140. 0x09, 0xE2, // Usage (Mute),
  141. 0x09, 0xCD, // Usage (Play/Pause),
  142. 0x09, 0xB5, // Usage (Scan Next Track),
  143. 0x09, 0xB6, // Usage (Scan Previous Track),
  144. 0x09, 0xB7, // Usage (Stop),
  145. 0x09, 0xB8, // Usage (Eject),
  146. 0x81, 0x02, // Input (Data, Variable, Absolute), ;Media keys
  147. 0x95, 0x05, // Report Count (5),
  148. 0x75, 0x01, // Report Size (1),
  149. 0x05, 0x08, // Usage Page (LEDs),
  150. 0x19, 0x01, // Usage Minimum (1),
  151. 0x29, 0x05, // Usage Maximum (5),
  152. 0x91, 0x02, // Output (Data, Variable, Absolute), ;LED report
  153. 0x95, 0x01, // Report Count (1),
  154. 0x75, 0x03, // Report Size (3),
  155. 0x91, 0x03, // Output (Constant), ;LED report padding
  156. 0x95, 0x06, // Report Count (6),
  157. 0x75, 0x08, // Report Size (8),
  158. 0x15, 0x00, // Logical Minimum (0),
  159. 0x25, 0x7F, // Logical Maximum(104),
  160. 0x05, 0x07, // Usage Page (Key Codes),
  161. 0x19, 0x00, // Usage Minimum (0),
  162. 0x29, 0x7F, // Usage Maximum (104),
  163. 0x81, 0x00, // Input (Data, Array), ;Normal keys
  164. 0xc0 // End Collection
  165. };
  166. // <Configuration> + <Keyboard HID> + <Serial CDC>
  167. #define CONFIG1_DESC_SIZE (9 + 9+9+7 + 8+9+5+5+4+5+7+9+7+7)
  168. #define KEYBOARD_HID_DESC_OFFSET (9 + 9)
  169. #define SERIAL_CDC_DESC_OFFSET (9 + 9+9+7)
  170. static const uint8_t PROGMEM config1_descriptor[CONFIG1_DESC_SIZE] = {
  171. // --- Configuration ---
  172. // - 9 bytes -
  173. // configuration descriptor, USB spec 9.6.3, page 264-266, Table 9-10
  174. 9, // bLength;
  175. 2, // bDescriptorType;
  176. LSB(CONFIG1_DESC_SIZE), // wTotalLength
  177. MSB(CONFIG1_DESC_SIZE),
  178. 3, // bNumInterfaces
  179. 1, // bConfigurationValue
  180. 0, // iConfiguration
  181. 0xC0, // bmAttributes
  182. 50, // bMaxPower
  183. // --- Keyboard HID ---
  184. // - 9 bytes -
  185. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  186. 9, // bLength
  187. 4, // bDescriptorType
  188. KEYBOARD_INTERFACE, // bInterfaceNumber
  189. 0, // bAlternateSetting
  190. 1, // bNumEndpoints
  191. 0x03, // bInterfaceClass (0x03 = HID)
  192. 0x01, // bInterfaceSubClass (0x01 = Boot)
  193. 0x01, // bInterfaceProtocol (0x01 = Keyboard)
  194. 0, // iInterface
  195. // - 9 bytes -
  196. // HID interface descriptor, HID 1.11 spec, section 6.2.1
  197. 9, // bLength
  198. 0x21, // bDescriptorType
  199. 0x11, 0x01, // bcdHID
  200. 0, // bCountryCode
  201. 1, // bNumDescriptors
  202. 0x22, // bDescriptorType
  203. LSB(sizeof(keyboard_hid_report_desc)), // wDescriptorLength
  204. MSB(sizeof(keyboard_hid_report_desc)),
  205. // - 7 bytes -
  206. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  207. 7, // bLength
  208. 5, // bDescriptorType
  209. KEYBOARD_ENDPOINT | 0x80, // bEndpointAddress
  210. 0x03, // bmAttributes (0x03=intr)
  211. KEYBOARD_SIZE, 0, // wMaxPacketSize
  212. KEYBOARD_INTERVAL, // bInterval
  213. // --- Serial CDC ---
  214. // - 8 bytes -
  215. // interface association descriptor, USB ECN, Table 9-Z
  216. 8, // bLength
  217. 11, // bDescriptorType
  218. CDC_STATUS_INTERFACE, // bFirstInterface
  219. 2, // bInterfaceCount
  220. 0x02, // bFunctionClass
  221. 0x02, // bFunctionSubClass
  222. 0x01, // bFunctionProtocol
  223. 4, // iFunction
  224. // - 9 bytes -
  225. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  226. 9, // bLength
  227. 4, // bDescriptorType
  228. CDC_STATUS_INTERFACE, // bInterfaceNumber
  229. 0, // bAlternateSetting
  230. 1, // bNumEndpoints
  231. 0x02, // bInterfaceClass
  232. 0x02, // bInterfaceSubClass
  233. 0x01, // bInterfaceProtocol
  234. 0, // iInterface
  235. // - 5 bytes -
  236. // CDC Header Functional Descriptor, CDC Spec 5.2.3.1, Table 26
  237. 5, // bFunctionLength
  238. 0x24, // bDescriptorType
  239. 0x00, // bDescriptorSubtype
  240. 0x10, 0x01, // bcdCDC
  241. // - 5 bytes -
  242. // Call Management Functional Descriptor, CDC Spec 5.2.3.2, Table 27
  243. 5, // bFunctionLength
  244. 0x24, // bDescriptorType
  245. 0x01, // bDescriptorSubtype
  246. 0x01, // bmCapabilities
  247. 1, // bDataInterface
  248. // - 4 bytes -
  249. // Abstract Control Management Functional Descriptor, CDC Spec 5.2.3.3, Table 28
  250. 4, // bFunctionLength
  251. 0x24, // bDescriptorType
  252. 0x02, // bDescriptorSubtype
  253. 0x06, // bmCapabilities
  254. // - 5 bytes -
  255. // Union Functional Descriptor, CDC Spec 5.2.3.8, Table 33
  256. 5, // bFunctionLength
  257. 0x24, // bDescriptorType
  258. 0x06, // bDescriptorSubtype
  259. CDC_STATUS_INTERFACE, // bMasterInterface
  260. CDC_DATA_INTERFACE, // bSlaveInterface0
  261. // - 7 bytes -
  262. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  263. 7, // bLength
  264. 5, // bDescriptorType
  265. CDC_ACM_ENDPOINT | 0x80, // bEndpointAddress
  266. 0x03, // bmAttributes (0x03=intr)
  267. CDC_ACM_SIZE, 0, // wMaxPacketSize
  268. 64, // bInterval
  269. // - 9 bytes -
  270. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  271. 9, // bLength
  272. 4, // bDescriptorType
  273. CDC_DATA_INTERFACE, // bInterfaceNumber
  274. 0, // bAlternateSetting
  275. 2, // bNumEndpoints
  276. 0x0A, // bInterfaceClass
  277. 0x00, // bInterfaceSubClass
  278. 0x00, // bInterfaceProtocol
  279. 0, // iInterface
  280. // - 7 bytes -
  281. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  282. 7, // bLength
  283. 5, // bDescriptorType
  284. CDC_RX_ENDPOINT, // bEndpointAddress
  285. 0x02, // bmAttributes (0x02=bulk)
  286. CDC_RX_SIZE, 0, // wMaxPacketSize
  287. 0, // bInterval
  288. // - 7 bytes -
  289. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  290. 7, // bLength
  291. 5, // bDescriptorType
  292. CDC_TX_ENDPOINT | 0x80, // bEndpointAddress
  293. 0x02, // bmAttributes (0x02=bulk)
  294. CDC_TX_SIZE, 0, // wMaxPacketSize
  295. 0, // bInterval
  296. };
  297. // If you're desperate for a little extra code memory, these strings
  298. // can be completely removed if iManufacturer, iProduct, iSerialNumber
  299. // in the device desciptor are changed to zeros.
  300. struct usb_string_descriptor_struct {
  301. uint8_t bLength;
  302. uint8_t bDescriptorType;
  303. int16_t wString[];
  304. };
  305. static const struct usb_string_descriptor_struct PROGMEM string0 = {
  306. 4,
  307. 3,
  308. {0x0409}
  309. };
  310. static const struct usb_string_descriptor_struct PROGMEM string1 = {
  311. sizeof(STR_MANUFACTURER),
  312. 3,
  313. STR_MANUFACTURER
  314. };
  315. static const struct usb_string_descriptor_struct PROGMEM string2 = {
  316. sizeof(STR_PRODUCT),
  317. 3,
  318. STR_PRODUCT
  319. };
  320. static const struct usb_string_descriptor_struct PROGMEM string3 = {
  321. sizeof(STR_SERIAL),
  322. 3,
  323. STR_SERIAL
  324. };
  325. // This table defines which descriptor data is sent for each specific
  326. // request from the host (in wValue and wIndex).
  327. static const struct descriptor_list_struct {
  328. uint16_t wValue;
  329. uint16_t wIndex;
  330. const uint8_t *addr;
  331. uint8_t length;
  332. } PROGMEM descriptor_list[] = {
  333. {0x0100, 0x0000, device_descriptor, sizeof(device_descriptor)},
  334. {0x0200, 0x0000, config1_descriptor, sizeof(config1_descriptor)},
  335. {0x2200, KEYBOARD_INTERFACE, keyboard_hid_report_desc, sizeof(keyboard_hid_report_desc)},
  336. {0x2100, KEYBOARD_INTERFACE, config1_descriptor+KEYBOARD_HID_DESC_OFFSET, 9},
  337. {0x0300, 0x0000, (const uint8_t *)&string0, 4},
  338. {0x0301, 0x0409, (const uint8_t *)&string1, sizeof(STR_MANUFACTURER)},
  339. {0x0302, 0x0409, (const uint8_t *)&string2, sizeof(STR_PRODUCT)},
  340. {0x0303, 0x0409, (const uint8_t *)&string3, sizeof(STR_SERIAL)}
  341. };
  342. #define NUM_DESC_LIST (sizeof(descriptor_list)/sizeof(struct descriptor_list_struct))
  343. /**************************************************************************
  344. *
  345. * Variables - these are the only non-stack RAM usage
  346. *
  347. **************************************************************************/
  348. // zero when we are not configured, non-zero when enumerated
  349. static volatile uint8_t usb_configuration=0;
  350. // the time remaining before we transmit any partially full
  351. // packet, or send a zero length packet.
  352. static volatile uint8_t transmit_flush_timer=0;
  353. static uint8_t transmit_previous_timeout=0;
  354. // serial port settings (baud rate, control signals, etc) set
  355. // by the PC. These are ignored, but kept in RAM.
  356. static uint8_t cdc_line_coding[7]={0x00, 0xE1, 0x00, 0x00, 0x00, 0x00, 0x08};
  357. static uint8_t cdc_line_rtsdtr=0;
  358. /**************************************************************************
  359. *
  360. * Public Functions - these are the API intended for the user
  361. *
  362. **************************************************************************/
  363. // initialize USB
  364. void usb_init(void)
  365. {
  366. HW_CONFIG();
  367. USB_FREEZE(); // enable USB
  368. PLL_CONFIG(); // config PLL
  369. while (!(PLLCSR & (1<<PLOCK))) ; // wait for PLL lock
  370. USB_CONFIG(); // start USB clock
  371. UDCON = 0; // enable attach resistor
  372. usb_configuration = 0;
  373. UDIEN = (1<<EORSTE)|(1<<SOFE);
  374. sei();
  375. // Disable watchdog timer after possible software reset
  376. //wdt_init(); // XXX Not working...seems to be ok without this, not sure though
  377. }
  378. // return 0 if the USB is not configured, or the configuration
  379. // number selected by the HOST
  380. uint8_t usb_configured(void)
  381. {
  382. return usb_configuration;
  383. }
  384. // send the contents of USBKeys_Array and USBKeys_Modifiers
  385. int8_t usb_keyboard_send(void)
  386. {
  387. uint8_t i, intr_state, timeout;
  388. if (!usb_configuration) return -1;
  389. intr_state = SREG;
  390. cli();
  391. UENUM = KEYBOARD_ENDPOINT;
  392. timeout = UDFNUML + 50;
  393. while (1) {
  394. // are we ready to transmit?
  395. if (UEINTX & (1<<RWAL)) break;
  396. SREG = intr_state;
  397. // has the USB gone offline?
  398. if (!usb_configuration) return -1;
  399. // have we waited too long?
  400. if (UDFNUML == timeout) return -1;
  401. // get ready to try checking again
  402. intr_state = SREG;
  403. cli();
  404. UENUM = KEYBOARD_ENDPOINT;
  405. }
  406. UEDATX = USBKeys_Modifiers;
  407. UEDATX = 0;
  408. for (i=0; i<6; i++) {
  409. UEDATX = USBKeys_Array[i];
  410. }
  411. UEINTX = 0x3A;
  412. USBKeys_Idle_Count = 0;
  413. SREG = intr_state;
  414. return 0;
  415. }
  416. // get the next character, or -1 if nothing received
  417. int16_t usb_serial_getchar(void)
  418. {
  419. uint8_t c, intr_state;
  420. // interrupts are disabled so these functions can be
  421. // used from the main program or interrupt context,
  422. // even both in the same program!
  423. intr_state = SREG;
  424. cli();
  425. if (!usb_configuration) {
  426. SREG = intr_state;
  427. return -1;
  428. }
  429. UENUM = CDC_RX_ENDPOINT;
  430. retry:
  431. c = UEINTX;
  432. if (!(c & (1<<RWAL))) {
  433. // no data in buffer
  434. if (c & (1<<RXOUTI)) {
  435. UEINTX = 0x6B;
  436. goto retry;
  437. }
  438. SREG = intr_state;
  439. return -2;
  440. }
  441. // take one byte out of the buffer
  442. c = UEDATX;
  443. // if buffer completely used, release it
  444. if (!(UEINTX & (1<<RWAL))) UEINTX = 0x6B;
  445. SREG = intr_state;
  446. return c;
  447. }
  448. // number of bytes available in the receive buffer
  449. uint8_t usb_serial_available(void)
  450. {
  451. uint8_t n=0, i, intr_state;
  452. intr_state = SREG;
  453. cli();
  454. if (usb_configuration) {
  455. UENUM = CDC_RX_ENDPOINT;
  456. n = UEBCLX;
  457. if (!n) {
  458. i = UEINTX;
  459. if (i & (1<<RXOUTI) && !(i & (1<<RWAL))) UEINTX = 0x6B;
  460. }
  461. }
  462. SREG = intr_state;
  463. return n;
  464. }
  465. // discard any buffered input
  466. void usb_serial_flush_input(void)
  467. {
  468. uint8_t intr_state;
  469. if (usb_configuration) {
  470. intr_state = SREG;
  471. cli();
  472. UENUM = CDC_RX_ENDPOINT;
  473. while ((UEINTX & (1<<RWAL))) {
  474. UEINTX = 0x6B;
  475. }
  476. SREG = intr_state;
  477. }
  478. }
  479. // transmit a character. 0 returned on success, -1 on error
  480. int8_t usb_serial_putchar(uint8_t c)
  481. {
  482. uint8_t timeout, intr_state;
  483. // if we're not online (enumerated and configured), error
  484. if (!usb_configuration) return -1;
  485. // interrupts are disabled so these functions can be
  486. // used from the main program or interrupt context,
  487. // even both in the same program!
  488. intr_state = SREG;
  489. cli();
  490. UENUM = CDC_TX_ENDPOINT;
  491. // if we gave up due to timeout before, don't wait again
  492. if (transmit_previous_timeout) {
  493. if (!(UEINTX & (1<<RWAL))) {
  494. SREG = intr_state;
  495. return -1;
  496. }
  497. transmit_previous_timeout = 0;
  498. }
  499. // wait for the FIFO to be ready to accept data
  500. timeout = UDFNUML + TRANSMIT_TIMEOUT;
  501. while (1) {
  502. // are we ready to transmit?
  503. if (UEINTX & (1<<RWAL)) break;
  504. SREG = intr_state;
  505. // have we waited too long? This happens if the user
  506. // is not running an application that is listening
  507. if (UDFNUML == timeout) {
  508. transmit_previous_timeout = 1;
  509. return -1;
  510. }
  511. // has the USB gone offline?
  512. if (!usb_configuration) return -1;
  513. // get ready to try checking again
  514. intr_state = SREG;
  515. cli();
  516. UENUM = CDC_TX_ENDPOINT;
  517. }
  518. // actually write the byte into the FIFO
  519. UEDATX = c;
  520. // if this completed a packet, transmit it now!
  521. if (!(UEINTX & (1<<RWAL))) UEINTX = 0x3A;
  522. transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
  523. SREG = intr_state;
  524. return 0;
  525. }
  526. // transmit a character, but do not wait if the buffer is full,
  527. // 0 returned on success, -1 on buffer full or error
  528. int8_t usb_serial_putchar_nowait(uint8_t c)
  529. {
  530. uint8_t intr_state;
  531. if (!usb_configuration) return -1;
  532. intr_state = SREG;
  533. cli();
  534. UENUM = CDC_TX_ENDPOINT;
  535. if (!(UEINTX & (1<<RWAL))) {
  536. // buffer is full
  537. SREG = intr_state;
  538. return -2;
  539. }
  540. // actually write the byte into the FIFO
  541. UEDATX = c;
  542. // if this completed a packet, transmit it now!
  543. if (!(UEINTX & (1<<RWAL))) UEINTX = 0x3A;
  544. transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
  545. SREG = intr_state;
  546. return 0;
  547. }
  548. // transmit a buffer.
  549. // 0 returned on success, -1 on error
  550. // This function is optimized for speed! Each call takes approx 6.1 us overhead
  551. // plus 0.25 us per byte. 12 Mbit/sec USB has 8.67 us per-packet overhead and
  552. // takes 0.67 us per byte. If called with 64 byte packet-size blocks, this function
  553. // can transmit at full USB speed using 43% CPU time. The maximum theoretical speed
  554. // is 19 packets per USB frame, or 1216 kbytes/sec. However, bulk endpoints have the
  555. // lowest priority, so any other USB devices will likely reduce the speed. Speed
  556. // can also be limited by how quickly the PC-based software reads data, as the host
  557. // controller in the PC will not allocate bandwitdh without a pending read request.
  558. // (thanks to Victor Suarez for testing and feedback and initial code)
  559. int8_t usb_serial_write(const char *buffer, uint16_t size)
  560. {
  561. uint8_t timeout, intr_state, write_size;
  562. // if we're not online (enumerated and configured), error
  563. if (!usb_configuration) return -1;
  564. // interrupts are disabled so these functions can be
  565. // used from the main program or interrupt context,
  566. // even both in the same program!
  567. intr_state = SREG;
  568. cli();
  569. UENUM = CDC_TX_ENDPOINT;
  570. // if we gave up due to timeout before, don't wait again
  571. /*
  572. if (transmit_previous_timeout) {
  573. if (!(UEINTX & (1<<RWAL))) {
  574. SREG = intr_state;
  575. return -2;
  576. }
  577. transmit_previous_timeout = 0;
  578. }
  579. */
  580. // each iteration of this loop transmits a packet
  581. while (size) {
  582. // wait for the FIFO to be ready to accept data
  583. timeout = UDFNUML + TRANSMIT_TIMEOUT;
  584. while (1) {
  585. // are we ready to transmit?
  586. if (UEINTX & (1<<RWAL)) break;
  587. SREG = intr_state;
  588. // have we waited too long? This happens if the user
  589. // is not running an application that is listening
  590. if (UDFNUML == timeout) {
  591. transmit_previous_timeout = 1;
  592. return -3;
  593. }
  594. // has the USB gone offline?
  595. if (!usb_configuration) return -4;
  596. // get ready to try checking again
  597. intr_state = SREG;
  598. cli();
  599. UENUM = CDC_TX_ENDPOINT;
  600. }
  601. // compute how many bytes will fit into the next packet
  602. write_size = CDC_TX_SIZE - UEBCLX;
  603. if (write_size > size) write_size = size;
  604. size -= write_size;
  605. // write the packet
  606. switch (write_size) {
  607. #if (CDC_TX_SIZE == 64)
  608. case 64: UEDATX = *buffer++;
  609. case 63: UEDATX = *buffer++;
  610. case 62: UEDATX = *buffer++;
  611. case 61: UEDATX = *buffer++;
  612. case 60: UEDATX = *buffer++;
  613. case 59: UEDATX = *buffer++;
  614. case 58: UEDATX = *buffer++;
  615. case 57: UEDATX = *buffer++;
  616. case 56: UEDATX = *buffer++;
  617. case 55: UEDATX = *buffer++;
  618. case 54: UEDATX = *buffer++;
  619. case 53: UEDATX = *buffer++;
  620. case 52: UEDATX = *buffer++;
  621. case 51: UEDATX = *buffer++;
  622. case 50: UEDATX = *buffer++;
  623. case 49: UEDATX = *buffer++;
  624. case 48: UEDATX = *buffer++;
  625. case 47: UEDATX = *buffer++;
  626. case 46: UEDATX = *buffer++;
  627. case 45: UEDATX = *buffer++;
  628. case 44: UEDATX = *buffer++;
  629. case 43: UEDATX = *buffer++;
  630. case 42: UEDATX = *buffer++;
  631. case 41: UEDATX = *buffer++;
  632. case 40: UEDATX = *buffer++;
  633. case 39: UEDATX = *buffer++;
  634. case 38: UEDATX = *buffer++;
  635. case 37: UEDATX = *buffer++;
  636. case 36: UEDATX = *buffer++;
  637. case 35: UEDATX = *buffer++;
  638. case 34: UEDATX = *buffer++;
  639. case 33: UEDATX = *buffer++;
  640. #endif
  641. #if (CDC_TX_SIZE >= 32)
  642. case 32: UEDATX = *buffer++;
  643. case 31: UEDATX = *buffer++;
  644. case 30: UEDATX = *buffer++;
  645. case 29: UEDATX = *buffer++;
  646. case 28: UEDATX = *buffer++;
  647. case 27: UEDATX = *buffer++;
  648. case 26: UEDATX = *buffer++;
  649. case 25: UEDATX = *buffer++;
  650. case 24: UEDATX = *buffer++;
  651. case 23: UEDATX = *buffer++;
  652. case 22: UEDATX = *buffer++;
  653. case 21: UEDATX = *buffer++;
  654. case 20: UEDATX = *buffer++;
  655. case 19: UEDATX = *buffer++;
  656. case 18: UEDATX = *buffer++;
  657. case 17: UEDATX = *buffer++;
  658. #endif
  659. #if (CDC_TX_SIZE >= 16)
  660. case 16: UEDATX = *buffer++;
  661. case 15: UEDATX = *buffer++;
  662. case 14: UEDATX = *buffer++;
  663. case 13: UEDATX = *buffer++;
  664. case 12: UEDATX = *buffer++;
  665. case 11: UEDATX = *buffer++;
  666. case 10: UEDATX = *buffer++;
  667. case 9: UEDATX = *buffer++;
  668. #endif
  669. case 8: UEDATX = *buffer++;
  670. case 7: UEDATX = *buffer++;
  671. case 6: UEDATX = *buffer++;
  672. case 5: UEDATX = *buffer++;
  673. case 4: UEDATX = *buffer++;
  674. case 3: UEDATX = *buffer++;
  675. case 2: UEDATX = *buffer++;
  676. default:
  677. case 1: UEDATX = *buffer++;
  678. case 0: break;
  679. }
  680. // if this completed a packet, transmit it now!
  681. if (!(UEINTX & (1<<RWAL))) UEINTX = 0x3A;
  682. transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
  683. SREG = intr_state;
  684. }
  685. return 0;
  686. }
  687. // immediately transmit any buffered output.
  688. // This doesn't actually transmit the data - that is impossible!
  689. // USB devices only transmit when the host allows, so the best
  690. // we can do is release the FIFO buffer for when the host wants it
  691. void usb_serial_flush_output(void)
  692. {
  693. uint8_t intr_state;
  694. intr_state = SREG;
  695. cli();
  696. if (transmit_flush_timer) {
  697. UENUM = CDC_TX_ENDPOINT;
  698. UEINTX = 0x3A;
  699. transmit_flush_timer = 0;
  700. }
  701. SREG = intr_state;
  702. }
  703. // functions to read the various async serial settings. These
  704. // aren't actually used by USB at all (communication is always
  705. // at full USB speed), but they are set by the host so we can
  706. // set them properly if we're converting the USB to a real serial
  707. // communication
  708. uint32_t usb_serial_get_baud(void)
  709. {
  710. uint32_t *baud = (uint32_t*)cdc_line_coding;
  711. return *baud;
  712. }
  713. uint8_t usb_serial_get_stopbits(void)
  714. {
  715. return cdc_line_coding[4];
  716. }
  717. uint8_t usb_serial_get_paritytype(void)
  718. {
  719. return cdc_line_coding[5];
  720. }
  721. uint8_t usb_serial_get_numbits(void)
  722. {
  723. return cdc_line_coding[6];
  724. }
  725. uint8_t usb_serial_get_control(void)
  726. {
  727. return cdc_line_rtsdtr;
  728. }
  729. // write the control signals, DCD, DSR, RI, etc
  730. // There is no CTS signal. If software on the host has transmitted
  731. // data to you but you haven't been calling the getchar function,
  732. // it remains buffered (either here or on the host) and can not be
  733. // lost because you weren't listening at the right time, like it
  734. // would in real serial communication.
  735. int8_t usb_serial_set_control(uint8_t signals)
  736. {
  737. uint8_t intr_state;
  738. intr_state = SREG;
  739. cli();
  740. if (!usb_configuration) {
  741. // we're not enumerated/configured
  742. SREG = intr_state;
  743. return -1;
  744. }
  745. UENUM = CDC_ACM_ENDPOINT;
  746. if (!(UEINTX & (1<<RWAL))) {
  747. // unable to write
  748. // TODO; should this try to abort the previously
  749. // buffered message??
  750. SREG = intr_state;
  751. return -1;
  752. }
  753. UEDATX = 0xA1;
  754. UEDATX = 0x20;
  755. UEDATX = 0;
  756. UEDATX = 0;
  757. UEDATX = 0; // 0 seems to work nicely. what if this is 1??
  758. UEDATX = 0;
  759. UEDATX = 1;
  760. UEDATX = 0;
  761. UEDATX = signals;
  762. UEINTX = 0x3A;
  763. SREG = intr_state;
  764. return 0;
  765. }
  766. /**************************************************************************
  767. *
  768. * Private Functions - not intended for general user consumption....
  769. *
  770. **************************************************************************/
  771. // USB Device Interrupt - handle all device-level events
  772. // the transmit buffer flushing is triggered by the start of frame
  773. //
  774. ISR(USB_GEN_vect)
  775. {
  776. uint8_t intbits, t_cdc, i;
  777. static uint8_t div4=0;
  778. intbits = UDINT;
  779. UDINT = 0;
  780. if (intbits & (1<<EORSTI)) {
  781. UENUM = 0;
  782. UECONX = 1;
  783. UECFG0X = EP_TYPE_CONTROL;
  784. UECFG1X = EP_SIZE(ENDPOINT0_SIZE) | EP_SINGLE_BUFFER;
  785. UEIENX = (1<<RXSTPE);
  786. usb_configuration = 0;
  787. cdc_line_rtsdtr = 0;
  788. }
  789. if ((intbits & (1<<SOFI)) && usb_configuration) {
  790. t_cdc = transmit_flush_timer;
  791. if (t_cdc) {
  792. transmit_flush_timer = --t_cdc;
  793. if (!t_cdc) {
  794. UENUM = CDC_TX_ENDPOINT;
  795. UEINTX = 0x3A;
  796. }
  797. }
  798. if (USBKeys_Idle_Config && (++div4 & 3) == 0) {
  799. UENUM = KEYBOARD_ENDPOINT;
  800. if (UEINTX & (1<<RWAL)) {
  801. USBKeys_Idle_Count++;
  802. if (USBKeys_Idle_Count == USBKeys_Idle_Config) {
  803. USBKeys_Idle_Count = 0;
  804. UEDATX = USBKeys_Modifiers;
  805. UEDATX = 0;
  806. for (i=0; i<6; i++) {
  807. UEDATX = USBKeys_Array[i];
  808. }
  809. UEINTX = 0x3A;
  810. }
  811. }
  812. }
  813. }
  814. }
  815. // Misc functions to wait for ready and send/receive packets
  816. static inline void usb_wait_in_ready(void)
  817. {
  818. while (!(UEINTX & (1<<TXINI))) ;
  819. }
  820. static inline void usb_send_in(void)
  821. {
  822. UEINTX = ~(1<<TXINI);
  823. }
  824. static inline void usb_wait_receive_out(void)
  825. {
  826. while (!(UEINTX & (1<<RXOUTI))) ;
  827. }
  828. static inline void usb_ack_out(void)
  829. {
  830. UEINTX = ~(1<<RXOUTI);
  831. }
  832. // USB Endpoint Interrupt - endpoint 0 is handled here. The
  833. // other endpoints are manipulated by the user-callable
  834. // functions, and the start-of-frame interrupt.
  835. //
  836. ISR(USB_COM_vect)
  837. {
  838. uint8_t intbits;
  839. const uint8_t *list;
  840. const uint8_t *cfg;
  841. uint8_t i, n, len, en;
  842. uint8_t *p;
  843. uint8_t bmRequestType;
  844. uint8_t bRequest;
  845. uint16_t wValue;
  846. uint16_t wIndex;
  847. uint16_t wLength;
  848. uint16_t desc_val;
  849. const uint8_t *desc_addr;
  850. uint8_t desc_length;
  851. UENUM = 0;
  852. intbits = UEINTX;
  853. if (intbits & (1<<RXSTPI)) {
  854. bmRequestType = UEDATX;
  855. bRequest = UEDATX;
  856. wValue = UEDATX;
  857. wValue |= (UEDATX << 8);
  858. wIndex = UEDATX;
  859. wIndex |= (UEDATX << 8);
  860. wLength = UEDATX;
  861. wLength |= (UEDATX << 8);
  862. UEINTX = ~((1<<RXSTPI) | (1<<RXOUTI) | (1<<TXINI));
  863. if (bRequest == GET_DESCRIPTOR) {
  864. list = (const uint8_t *)descriptor_list;
  865. for (i=0; ; i++) {
  866. if (i >= NUM_DESC_LIST) {
  867. UECONX = (1<<STALLRQ)|(1<<EPEN); //stall
  868. return;
  869. }
  870. desc_val = pgm_read_word(list);
  871. if (desc_val != wValue) {
  872. list += sizeof(struct descriptor_list_struct);
  873. continue;
  874. }
  875. list += 2;
  876. desc_val = pgm_read_word(list);
  877. if (desc_val != wIndex) {
  878. list += sizeof(struct descriptor_list_struct)-2;
  879. continue;
  880. }
  881. list += 2;
  882. desc_addr = (const uint8_t *)pgm_read_word(list);
  883. list += 2;
  884. desc_length = pgm_read_byte(list);
  885. break;
  886. }
  887. len = (wLength < 256) ? wLength : 255;
  888. if (len > desc_length) len = desc_length;
  889. do {
  890. // wait for host ready for IN packet
  891. do {
  892. i = UEINTX;
  893. } while (!(i & ((1<<TXINI)|(1<<RXOUTI))));
  894. if (i & (1<<RXOUTI)) return; // abort
  895. // send IN packet
  896. n = len < ENDPOINT0_SIZE ? len : ENDPOINT0_SIZE;
  897. for (i = n; i; i--) {
  898. UEDATX = pgm_read_byte(desc_addr++);
  899. }
  900. len -= n;
  901. usb_send_in();
  902. } while (len || n == ENDPOINT0_SIZE);
  903. return;
  904. }
  905. if (bRequest == SET_ADDRESS) {
  906. usb_send_in();
  907. usb_wait_in_ready();
  908. UDADDR = wValue | (1<<ADDEN);
  909. return;
  910. }
  911. if (bRequest == SET_CONFIGURATION && bmRequestType == 0) {
  912. usb_configuration = wValue;
  913. cdc_line_rtsdtr = 0;
  914. transmit_flush_timer = 0;
  915. usb_send_in();
  916. cfg = endpoint_config_table;
  917. for (i=1; i<6; i++) { // 4+1 of 7 endpoints are used // XXX Important to change if more endpoints are used
  918. UENUM = i;
  919. en = pgm_read_byte(cfg++);
  920. UECONX = en;
  921. if (en) {
  922. UECFG0X = pgm_read_byte(cfg++);
  923. UECFG1X = pgm_read_byte(cfg++);
  924. }
  925. }
  926. UERST = 0x1E;
  927. UERST = 0;
  928. return;
  929. }
  930. if (bRequest == GET_CONFIGURATION && bmRequestType == 0x80) {
  931. usb_wait_in_ready();
  932. UEDATX = usb_configuration;
  933. usb_send_in();
  934. return;
  935. }
  936. if (bRequest == CDC_GET_LINE_CODING && bmRequestType == 0xA1) {
  937. usb_wait_in_ready();
  938. p = cdc_line_coding;
  939. for (i=0; i<7; i++) {
  940. UEDATX = *p++;
  941. }
  942. usb_send_in();
  943. return;
  944. }
  945. if (bRequest == CDC_SET_LINE_CODING && bmRequestType == 0x21) {
  946. usb_wait_receive_out();
  947. p = cdc_line_coding;
  948. for (i=0; i<7; i++) {
  949. *p++ = UEDATX;
  950. }
  951. usb_ack_out();
  952. usb_send_in();
  953. return;
  954. }
  955. if (bRequest == CDC_SET_CONTROL_LINE_STATE && bmRequestType == 0x21) {
  956. cdc_line_rtsdtr = wValue;
  957. usb_wait_in_ready();
  958. usb_send_in();
  959. return;
  960. }
  961. if (bRequest == GET_STATUS) {
  962. usb_wait_in_ready();
  963. i = 0;
  964. #ifdef SUPPORT_ENDPOINT_HALT
  965. if (bmRequestType == 0x82) {
  966. UENUM = wIndex;
  967. if (UECONX & (1<<STALLRQ)) i = 1;
  968. UENUM = 0;
  969. }
  970. #endif
  971. UEDATX = i;
  972. UEDATX = 0;
  973. usb_send_in();
  974. return;
  975. }
  976. #ifdef SUPPORT_ENDPOINT_HALT
  977. if ((bRequest == CLEAR_FEATURE || bRequest == SET_FEATURE)
  978. && bmRequestType == 0x02 && wValue == 0) {
  979. i = wIndex & 0x7F;
  980. if (i >= 1 && i <= MAX_ENDPOINT) {
  981. usb_send_in();
  982. UENUM = i;
  983. if (bRequest == SET_FEATURE) {
  984. UECONX = (1<<STALLRQ)|(1<<EPEN);
  985. } else {
  986. UECONX = (1<<STALLRQC)|(1<<RSTDT)|(1<<EPEN);
  987. UERST = (1 << i);
  988. UERST = 0;
  989. }
  990. return;
  991. }
  992. }
  993. #endif
  994. if (wIndex == KEYBOARD_INTERFACE) {
  995. if (bmRequestType == 0xA1) {
  996. if (bRequest == HID_GET_REPORT) {
  997. usb_wait_in_ready();
  998. UEDATX = USBKeys_Modifiers;
  999. UEDATX = 0;
  1000. for (i=0; i<6; i++) {
  1001. UEDATX = USBKeys_Array[i];
  1002. }
  1003. usb_send_in();
  1004. return;
  1005. }
  1006. if (bRequest == HID_GET_IDLE) {
  1007. usb_wait_in_ready();
  1008. UEDATX = USBKeys_Idle_Config;
  1009. usb_send_in();
  1010. return;
  1011. }
  1012. if (bRequest == HID_GET_PROTOCOL) {
  1013. usb_wait_in_ready();
  1014. UEDATX = USBKeys_Protocol;
  1015. usb_send_in();
  1016. return;
  1017. }
  1018. }
  1019. if (bmRequestType == 0x21) {
  1020. if (bRequest == HID_SET_REPORT) {
  1021. usb_wait_receive_out();
  1022. USBKeys_LEDs = UEDATX;
  1023. usb_ack_out();
  1024. usb_send_in();
  1025. return;
  1026. }
  1027. if (bRequest == HID_SET_IDLE) {
  1028. USBKeys_Idle_Config = (wValue >> 8);
  1029. USBKeys_Idle_Count = 0;
  1030. //usb_wait_in_ready();
  1031. usb_send_in();
  1032. return;
  1033. }
  1034. if (bRequest == HID_SET_PROTOCOL) {
  1035. USBKeys_Protocol = wValue;
  1036. //usb_wait_in_ready();
  1037. usb_send_in();
  1038. return;
  1039. }
  1040. }
  1041. }
  1042. }
  1043. UECONX = (1<<STALLRQ) | (1<<EPEN); // stall
  1044. }