Kiibohd Controller
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

usb_dev.c 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. #include <Lib/USBLib.h>
  2. #include "usb_dev.h"
  3. #include "usb_mem.h"
  4. // buffer descriptor table
  5. typedef struct {
  6. uint32_t desc;
  7. void * addr;
  8. } bdt_t;
  9. __attribute__ ((section(".usbdescriptortable"), used))
  10. static bdt_t table[64];
  11. #define BDT_OWN 0x80
  12. #define BDT_DATA1 0x40
  13. #define BDT_DATA0 0x00
  14. #define BDT_DTS 0x08
  15. #define BDT_STALL 0x04
  16. #define BDT_PID(n) (((n) >> 2) & 15)
  17. #define BDT_DESC(count, data) (BDT_OWN | BDT_DTS \
  18. | ((data) ? BDT_DATA1 : BDT_DATA0) \
  19. | ((count) << 16))
  20. #define TX 1
  21. #define RX 0
  22. #define ODD 1
  23. #define EVEN 0
  24. #define DATA0 0
  25. #define DATA1 1
  26. #define index(endpoint, tx, odd) (((endpoint) << 2) | ((tx) << 1) | (odd))
  27. #define stat2bufferdescriptor(stat) (table + ((stat) >> 2))
  28. static union {
  29. struct {
  30. union {
  31. struct {
  32. uint8_t bmRequestType;
  33. uint8_t bRequest;
  34. };
  35. uint16_t wRequestAndType;
  36. };
  37. uint16_t wValue;
  38. uint16_t wIndex;
  39. uint16_t wLength;
  40. };
  41. struct {
  42. uint32_t word1;
  43. uint32_t word2;
  44. };
  45. } setup;
  46. #define GET_STATUS 0
  47. #define CLEAR_FEATURE 1
  48. #define SET_FEATURE 3
  49. #define SET_ADDRESS 5
  50. #define GET_DESCRIPTOR 6
  51. #define SET_DESCRIPTOR 7
  52. #define GET_CONFIGURATION 8
  53. #define SET_CONFIGURATION 9
  54. #define GET_INTERFACE 10
  55. #define SET_INTERFACE 11
  56. #define SYNCH_FRAME 12
  57. // SETUP always uses a DATA0 PID for the data field of the SETUP transaction.
  58. // transactions in the data phase start with DATA1 and toggle (figure 8-12, USB1.1)
  59. // Status stage uses a DATA1 PID.
  60. static uint8_t ep0_rx0_buf[EP0_SIZE] __attribute__ ((aligned (4)));
  61. static uint8_t ep0_rx1_buf[EP0_SIZE] __attribute__ ((aligned (4)));
  62. static const uint8_t *ep0_tx_ptr = NULL;
  63. static uint16_t ep0_tx_len;
  64. static uint8_t ep0_tx_bdt_bank = 0;
  65. static uint8_t ep0_tx_data_toggle = 0;
  66. uint8_t usb_rx_memory_needed = 0;
  67. volatile uint8_t usb_configuration = 0;
  68. volatile uint8_t usb_reboot_timer = 0;
  69. static void endpoint0_stall(void)
  70. {
  71. USB0_ENDPT0 = USB_ENDPT_EPSTALL | USB_ENDPT_EPRXEN | USB_ENDPT_EPTXEN | USB_ENDPT_EPHSHK;
  72. }
  73. static void endpoint0_transmit(const void *data, uint32_t len)
  74. {
  75. #if 0
  76. serial_print("tx0:");
  77. serial_phex32((uint32_t)data);
  78. serial_print(",");
  79. serial_phex16(len);
  80. serial_print(ep0_tx_bdt_bank ? ", odd" : ", even");
  81. serial_print(ep0_tx_data_toggle ? ", d1\n" : ", d0\n");
  82. #endif
  83. table[index(0, TX, ep0_tx_bdt_bank)].addr = (void *)data;
  84. table[index(0, TX, ep0_tx_bdt_bank)].desc = BDT_DESC(len, ep0_tx_data_toggle);
  85. ep0_tx_data_toggle ^= 1;
  86. ep0_tx_bdt_bank ^= 1;
  87. }
  88. static uint8_t reply_buffer[8];
  89. static void usbdev_setup(void)
  90. {
  91. const uint8_t *data = NULL;
  92. uint32_t datalen = 0;
  93. const usb_descriptor_list_t *list;
  94. uint32_t size;
  95. volatile uint8_t *reg;
  96. uint8_t epconf;
  97. const uint8_t *cfg;
  98. int i;
  99. switch (setup.wRequestAndType) {
  100. case 0x0500: // SET_ADDRESS
  101. break;
  102. case 0x0900: // SET_CONFIGURATION
  103. //serial_print("configure\n");
  104. usb_configuration = setup.wValue;
  105. reg = &USB0_ENDPT1;
  106. cfg = usb_endpoint_config_table;
  107. // clear all BDT entries, free any allocated memory...
  108. for (i=4; i <= NUM_ENDPOINTS*4; i++) {
  109. if (table[i].desc & BDT_OWN) {
  110. usb_free((usb_packet_t *)((uint8_t *)(table[i].addr) - 8));
  111. table[i].desc = 0;
  112. }
  113. }
  114. usb_rx_memory_needed = 0;
  115. for (i=1; i <= NUM_ENDPOINTS; i++) {
  116. epconf = *cfg++;
  117. *reg = epconf;
  118. reg += 4;
  119. if (epconf & USB_ENDPT_EPRXEN) {
  120. usb_packet_t *p;
  121. p = usb_malloc();
  122. if (p) {
  123. table[index(i, RX, EVEN)].addr = p->buf;
  124. table[index(i, RX, EVEN)].desc = BDT_DESC(64, 0);
  125. } else {
  126. table[index(i, RX, EVEN)].desc = 0;
  127. usb_rx_memory_needed++;
  128. }
  129. p = usb_malloc();
  130. if (p) {
  131. table[index(i, RX, ODD)].addr = p->buf;
  132. table[index(i, RX, ODD)].desc = BDT_DESC(64, 1);
  133. } else {
  134. table[index(i, RX, ODD)].desc = 0;
  135. usb_rx_memory_needed++;
  136. }
  137. }
  138. table[index(i, TX, EVEN)].desc = 0;
  139. table[index(i, TX, ODD)].desc = 0;
  140. }
  141. break;
  142. case 0x0880: // GET_CONFIGURATION
  143. reply_buffer[0] = usb_configuration;
  144. datalen = 1;
  145. data = reply_buffer;
  146. break;
  147. case 0x0080: // GET_STATUS (device)
  148. reply_buffer[0] = 0;
  149. reply_buffer[1] = 0;
  150. datalen = 2;
  151. data = reply_buffer;
  152. break;
  153. case 0x0082: // GET_STATUS (endpoint)
  154. if (setup.wIndex > NUM_ENDPOINTS) {
  155. // TODO: do we need to handle IN vs OUT here?
  156. endpoint0_stall();
  157. return;
  158. }
  159. reply_buffer[0] = 0;
  160. reply_buffer[1] = 0;
  161. if (*(uint8_t *)(&USB0_ENDPT0 + setup.wIndex * 4) & 0x02) reply_buffer[0] = 1;
  162. data = reply_buffer;
  163. datalen = 2;
  164. break;
  165. case 0x0102: // CLEAR_FEATURE (endpoint)
  166. i = setup.wIndex & 0x7F;
  167. if (i > NUM_ENDPOINTS || setup.wValue != 0) {
  168. // TODO: do we need to handle IN vs OUT here?
  169. endpoint0_stall();
  170. return;
  171. }
  172. (*(uint8_t *)(&USB0_ENDPT0 + setup.wIndex * 4)) &= ~0x02;
  173. // TODO: do we need to clear the data toggle here?
  174. break;
  175. case 0x0302: // SET_FEATURE (endpoint)
  176. i = setup.wIndex & 0x7F;
  177. if (i > NUM_ENDPOINTS || setup.wValue != 0) {
  178. // TODO: do we need to handle IN vs OUT here?
  179. endpoint0_stall();
  180. return;
  181. }
  182. (*(uint8_t *)(&USB0_ENDPT0 + setup.wIndex * 4)) |= 0x02;
  183. // TODO: do we need to clear the data toggle here?
  184. break;
  185. case 0x0680: // GET_DESCRIPTOR
  186. case 0x0681:
  187. //serial_print("desc:");
  188. //serial_phex16(setup.wValue);
  189. //serial_print("\n");
  190. for (list = usb_descriptor_list; 1; list++) {
  191. if (list->addr == NULL) break;
  192. //if (setup.wValue == list->wValue &&
  193. //(setup.wIndex == list->wIndex) || ((setup.wValue >> 8) == 3)) {
  194. if (setup.wValue == list->wValue && setup.wIndex == list->wIndex) {
  195. data = list->addr;
  196. datalen = list->length;
  197. #if 0
  198. serial_print("Desc found, ");
  199. serial_phex32((uint32_t)data);
  200. serial_print(",");
  201. serial_phex16(datalen);
  202. serial_print(",");
  203. serial_phex(data[0]);
  204. serial_phex(data[1]);
  205. serial_phex(data[2]);
  206. serial_phex(data[3]);
  207. serial_phex(data[4]);
  208. serial_phex(data[5]);
  209. serial_print("\n");
  210. #endif
  211. goto send;
  212. }
  213. }
  214. //serial_print("desc: not found\n");
  215. endpoint0_stall();
  216. return;
  217. #if defined(CDC_STATUS_INTERFACE)
  218. case 0x2221: // CDC_SET_CONTROL_LINE_STATE
  219. usb_cdc_line_rtsdtr = setup.wValue;
  220. //serial_print("set control line state\n");
  221. break;
  222. case 0x2021: // CDC_SET_LINE_CODING
  223. //serial_print("set coding, waiting...\n");
  224. return;
  225. #endif
  226. // TODO: this does not work... why?
  227. #if defined(KEYBOARD_INTERFACE)
  228. case 0x0921: // HID SET_REPORT
  229. //serial_print(":)\n");
  230. return;
  231. case 0x0A21: // HID SET_IDLE
  232. break;
  233. // case 0xC940:
  234. #endif
  235. default:
  236. endpoint0_stall();
  237. return;
  238. }
  239. send:
  240. //serial_print("setup send ");
  241. //serial_phex32(data);
  242. //serial_print(",");
  243. //serial_phex16(datalen);
  244. //serial_print("\n");
  245. if (datalen > setup.wLength) datalen = setup.wLength;
  246. size = datalen;
  247. if (size > EP0_SIZE) size = EP0_SIZE;
  248. endpoint0_transmit(data, size);
  249. data += size;
  250. datalen -= size;
  251. if (datalen == 0 && size < EP0_SIZE) return;
  252. size = datalen;
  253. if (size > EP0_SIZE) size = EP0_SIZE;
  254. endpoint0_transmit(data, size);
  255. data += size;
  256. datalen -= size;
  257. if (datalen == 0 && size < EP0_SIZE) return;
  258. ep0_tx_ptr = data;
  259. ep0_tx_len = datalen;
  260. }
  261. //A bulk endpoint's toggle sequence is initialized to DATA0 when the endpoint
  262. //experiences any configuration event (configuration events are explained in
  263. //Sections 9.1.1.5 and 9.4.5).
  264. //Configuring a device or changing an alternate setting causes all of the status
  265. //and configuration values associated with endpoints in the affected interfaces
  266. //to be set to their default values. This includes setting the data toggle of
  267. //any endpoint using data toggles to the value DATA0.
  268. //For endpoints using data toggle, regardless of whether an endpoint has the
  269. //Halt feature set, a ClearFeature(ENDPOINT_HALT) request always results in the
  270. //data toggle being reinitialized to DATA0.
  271. // #define stat2bufferdescriptor(stat) (table + ((stat) >> 2))
  272. static void usb_control(uint32_t stat)
  273. {
  274. bdt_t *b;
  275. uint32_t pid, size;
  276. uint8_t *buf;
  277. const uint8_t *data;
  278. b = stat2bufferdescriptor(stat);
  279. pid = BDT_PID(b->desc);
  280. //count = b->desc >> 16;
  281. buf = b->addr;
  282. //serial_print("pid:");
  283. //serial_phex(pid);
  284. //serial_print(", count:");
  285. //serial_phex(count);
  286. //serial_print("\n");
  287. switch (pid) {
  288. case 0x0D: // Setup received from host
  289. //serial_print("PID=Setup\n");
  290. //if (count != 8) ; // panic?
  291. // grab the 8 byte setup info
  292. setup.word1 = *(uint32_t *)(buf);
  293. setup.word2 = *(uint32_t *)(buf + 4);
  294. // give the buffer back
  295. b->desc = BDT_DESC(EP0_SIZE, DATA1);
  296. //table[index(0, RX, EVEN)].desc = BDT_DESC(EP0_SIZE, 1);
  297. //table[index(0, RX, ODD)].desc = BDT_DESC(EP0_SIZE, 1);
  298. // clear any leftover pending IN transactions
  299. ep0_tx_ptr = NULL;
  300. if (ep0_tx_data_toggle) {
  301. }
  302. //if (table[index(0, TX, EVEN)].desc & 0x80) {
  303. //serial_print("leftover tx even\n");
  304. //}
  305. //if (table[index(0, TX, ODD)].desc & 0x80) {
  306. //serial_print("leftover tx odd\n");
  307. //}
  308. table[index(0, TX, EVEN)].desc = 0;
  309. table[index(0, TX, ODD)].desc = 0;
  310. // first IN after Setup is always DATA1
  311. ep0_tx_data_toggle = 1;
  312. #if 0
  313. serial_print("bmRequestType:");
  314. serial_phex(setup.bmRequestType);
  315. serial_print(", bRequest:");
  316. serial_phex(setup.bRequest);
  317. serial_print(", wValue:");
  318. serial_phex16(setup.wValue);
  319. serial_print(", wIndex:");
  320. serial_phex16(setup.wIndex);
  321. serial_print(", len:");
  322. serial_phex16(setup.wLength);
  323. serial_print("\n");
  324. #endif
  325. // actually "do" the setup request
  326. usbdev_setup();
  327. // unfreeze the USB, now that we're ready
  328. USB0_CTL = USB_CTL_USBENSOFEN; // clear TXSUSPENDTOKENBUSY bit
  329. break;
  330. case 0x01: // OUT transaction received from host
  331. case 0x02:
  332. //serial_print("PID=OUT\n");
  333. #ifdef CDC_STATUS_INTERFACE
  334. if (setup.wRequestAndType == 0x2021 /*CDC_SET_LINE_CODING*/) {
  335. int i;
  336. uint8_t *dst = usb_cdc_line_coding;
  337. //serial_print("set line coding ");
  338. for (i=0; i<7; i++) {
  339. //serial_phex(*buf);
  340. *dst++ = *buf++;
  341. }
  342. //serial_phex32(*(uint32_t *)usb_cdc_line_coding);
  343. //serial_print("\n");
  344. // XXX - Not sure why this was casted to uint32_t... -HaaTa
  345. //if (*(uint32_t *)usb_cdc_line_coding == 134) usb_reboot_timer = 15;
  346. if (*usb_cdc_line_coding == 134) usb_reboot_timer = 15;
  347. endpoint0_transmit(NULL, 0);
  348. }
  349. #endif
  350. #ifdef KEYBOARD_INTERFACE
  351. if (setup.word1 == 0x02000921 && setup.word2 == ((1<<16)|KEYBOARD_INTERFACE)) {
  352. USBKeys_LEDs = buf[0];
  353. endpoint0_transmit(NULL, 0);
  354. }
  355. #endif
  356. // give the buffer back
  357. b->desc = BDT_DESC(EP0_SIZE, DATA1);
  358. break;
  359. case 0x09: // IN transaction completed to host
  360. //serial_print("PID=IN:");
  361. //serial_phex(stat);
  362. //serial_print("\n");
  363. // send remaining data, if any...
  364. data = ep0_tx_ptr;
  365. if (data) {
  366. size = ep0_tx_len;
  367. if (size > EP0_SIZE) size = EP0_SIZE;
  368. endpoint0_transmit(data, size);
  369. data += size;
  370. ep0_tx_len -= size;
  371. ep0_tx_ptr = (ep0_tx_len > 0 || size == EP0_SIZE) ? data : NULL;
  372. }
  373. if (setup.bRequest == 5 && setup.bmRequestType == 0) {
  374. setup.bRequest = 0;
  375. //serial_print("set address: ");
  376. //serial_phex16(setup.wValue);
  377. //serial_print("\n");
  378. USB0_ADDR = setup.wValue;
  379. }
  380. break;
  381. //default:
  382. //serial_print("PID=unknown:");
  383. //serial_phex(pid);
  384. //serial_print("\n");
  385. }
  386. USB0_CTL = USB_CTL_USBENSOFEN; // clear TXSUSPENDTOKENBUSY bit
  387. }
  388. static usb_packet_t *rx_first[NUM_ENDPOINTS];
  389. static usb_packet_t *rx_last[NUM_ENDPOINTS];
  390. static usb_packet_t *tx_first[NUM_ENDPOINTS];
  391. static usb_packet_t *tx_last[NUM_ENDPOINTS];
  392. static uint8_t tx_state[NUM_ENDPOINTS];
  393. #define TX_STATE_BOTH_FREE_EVEN_FIRST 0
  394. #define TX_STATE_BOTH_FREE_ODD_FIRST 1
  395. #define TX_STATE_EVEN_FREE 2
  396. #define TX_STATE_ODD_FREE 3
  397. #define TX_STATE_NONE_FREE 4
  398. usb_packet_t *usb_rx(uint32_t endpoint)
  399. {
  400. usb_packet_t *ret;
  401. endpoint--;
  402. if (endpoint >= NUM_ENDPOINTS) return NULL;
  403. __disable_irq();
  404. ret = rx_first[endpoint];
  405. if (ret) rx_first[endpoint] = ret->next;
  406. __enable_irq();
  407. //serial_print("rx, epidx=");
  408. //serial_phex(endpoint);
  409. //serial_print(", packet=");
  410. //serial_phex32(ret);
  411. //serial_print("\n");
  412. return ret;
  413. }
  414. static uint32_t usb_queue_byte_count(const usb_packet_t *p)
  415. {
  416. uint32_t count=0;
  417. __disable_irq();
  418. for ( ; p; p = p->next) {
  419. count += p->len;
  420. }
  421. __enable_irq();
  422. return count;
  423. }
  424. uint32_t usb_rx_byte_count(uint32_t endpoint)
  425. {
  426. endpoint--;
  427. if (endpoint >= NUM_ENDPOINTS) return 0;
  428. return usb_queue_byte_count(rx_first[endpoint]);
  429. }
  430. uint32_t usb_tx_byte_count(uint32_t endpoint)
  431. {
  432. endpoint--;
  433. if (endpoint >= NUM_ENDPOINTS) return 0;
  434. return usb_queue_byte_count(tx_first[endpoint]);
  435. }
  436. uint32_t usb_tx_packet_count(uint32_t endpoint)
  437. {
  438. const usb_packet_t *p;
  439. uint32_t count=0;
  440. endpoint--;
  441. if (endpoint >= NUM_ENDPOINTS) return 0;
  442. p = tx_first[endpoint];
  443. __disable_irq();
  444. for ( ; p; p = p->next) count++;
  445. __enable_irq();
  446. return count;
  447. }
  448. // Called from usb_free, but only when usb_rx_memory_needed > 0, indicating
  449. // receive endpoints are starving for memory. The intention is to give
  450. // endpoints needing receive memory priority over the user's code, which is
  451. // likely calling usb_malloc to obtain memory for transmitting. When the
  452. // user is creating data very quickly, their consumption could starve reception
  453. // without this prioritization. The packet buffer (input) is assigned to the
  454. // first endpoint needing memory.
  455. //
  456. void usb_rx_memory(usb_packet_t *packet)
  457. {
  458. unsigned int i;
  459. const uint8_t *cfg;
  460. cfg = usb_endpoint_config_table;
  461. //serial_print("rx_mem:");
  462. __disable_irq();
  463. for (i=1; i <= NUM_ENDPOINTS; i++) {
  464. if (*cfg++ & USB_ENDPT_EPRXEN) {
  465. if (table[index(i, RX, EVEN)].desc == 0) {
  466. table[index(i, RX, EVEN)].addr = packet->buf;
  467. table[index(i, RX, EVEN)].desc = BDT_DESC(64, 0);
  468. usb_rx_memory_needed--;
  469. __enable_irq();
  470. //serial_phex(i);
  471. //serial_print(",even\n");
  472. return;
  473. }
  474. if (table[index(i, RX, ODD)].desc == 0) {
  475. table[index(i, RX, ODD)].addr = packet->buf;
  476. table[index(i, RX, ODD)].desc = BDT_DESC(64, 1);
  477. usb_rx_memory_needed--;
  478. __enable_irq();
  479. //serial_phex(i);
  480. //serial_print(",odd\n");
  481. return;
  482. }
  483. }
  484. }
  485. __enable_irq();
  486. // we should never reach this point. If we get here, it means
  487. // usb_rx_memory_needed was set greater than zero, but no memory
  488. // was actually needed.
  489. usb_rx_memory_needed = 0;
  490. usb_free(packet);
  491. return;
  492. }
  493. //#define index(endpoint, tx, odd) (((endpoint) << 2) | ((tx) << 1) | (odd))
  494. //#define stat2bufferdescriptor(stat) (table + ((stat) >> 2))
  495. void usb_tx(uint32_t endpoint, usb_packet_t *packet)
  496. {
  497. bdt_t *b = &table[index(endpoint, TX, EVEN)];
  498. uint8_t next;
  499. endpoint--;
  500. if (endpoint >= NUM_ENDPOINTS) return;
  501. __disable_irq();
  502. //serial_print("txstate=");
  503. //serial_phex(tx_state[endpoint]);
  504. //serial_print("\n");
  505. switch (tx_state[endpoint]) {
  506. case TX_STATE_BOTH_FREE_EVEN_FIRST:
  507. next = TX_STATE_ODD_FREE;
  508. break;
  509. case TX_STATE_BOTH_FREE_ODD_FIRST:
  510. b++;
  511. next = TX_STATE_EVEN_FREE;
  512. break;
  513. case TX_STATE_EVEN_FREE:
  514. next = TX_STATE_NONE_FREE;
  515. break;
  516. case TX_STATE_ODD_FREE:
  517. b++;
  518. next = TX_STATE_NONE_FREE;
  519. break;
  520. default:
  521. if (tx_first[endpoint] == NULL) {
  522. tx_first[endpoint] = packet;
  523. } else {
  524. tx_last[endpoint]->next = packet;
  525. }
  526. tx_last[endpoint] = packet;
  527. __enable_irq();
  528. return;
  529. }
  530. tx_state[endpoint] = next;
  531. b->addr = packet->buf;
  532. b->desc = BDT_DESC(packet->len, ((uint32_t)b & 8) ? DATA1 : DATA0);
  533. __enable_irq();
  534. }
  535. void usb_device_reload()
  536. {
  537. asm volatile("bkpt");
  538. }
  539. void _reboot_Teensyduino_(void)
  540. {
  541. // TODO: initialize R0 with a code....
  542. asm volatile("bkpt");
  543. }
  544. void usb_isr(void)
  545. {
  546. uint8_t status, stat, t;
  547. //serial_print("isr");
  548. //status = USB0_ISTAT;
  549. //serial_phex(status);
  550. //serial_print("\n");
  551. restart:
  552. status = USB0_ISTAT;
  553. if ((status & USB_INTEN_SOFTOKEN /* 04 */ )) {
  554. if (usb_configuration) {
  555. t = usb_reboot_timer;
  556. if (t) {
  557. usb_reboot_timer = --t;
  558. if (!t) _reboot_Teensyduino_();
  559. }
  560. #ifdef CDC_DATA_INTERFACE
  561. t = usb_cdc_transmit_flush_timer;
  562. if (t) {
  563. usb_cdc_transmit_flush_timer = --t;
  564. if (t == 0) usb_serial_flush_callback();
  565. }
  566. #endif
  567. }
  568. USB0_ISTAT = USB_INTEN_SOFTOKEN;
  569. }
  570. if ((status & USB_ISTAT_TOKDNE /* 08 */ )) {
  571. uint8_t endpoint;
  572. stat = USB0_STAT;
  573. //serial_print("token: ep=");
  574. //serial_phex(stat >> 4);
  575. //serial_print(stat & 0x08 ? ",tx" : ",rx");
  576. //serial_print(stat & 0x04 ? ",odd\n" : ",even\n");
  577. endpoint = stat >> 4;
  578. if (endpoint == 0) {
  579. usb_control(stat);
  580. } else {
  581. bdt_t *b = stat2bufferdescriptor(stat);
  582. usb_packet_t *packet = (usb_packet_t *)((uint8_t *)(b->addr) - 8);
  583. #if 0
  584. serial_print("ep:");
  585. serial_phex(endpoint);
  586. serial_print(", pid:");
  587. serial_phex(BDT_PID(b->desc));
  588. serial_print(((uint32_t)b & 8) ? ", odd" : ", even");
  589. serial_print(", count:");
  590. serial_phex(b->desc >> 16);
  591. serial_print("\n");
  592. #endif
  593. endpoint--; // endpoint is index to zero-based arrays
  594. if (stat & 0x08) { // transmit
  595. usb_free(packet);
  596. packet = tx_first[endpoint];
  597. if (packet) {
  598. //serial_print("tx packet\n");
  599. tx_first[endpoint] = packet->next;
  600. b->addr = packet->buf;
  601. switch (tx_state[endpoint]) {
  602. case TX_STATE_BOTH_FREE_EVEN_FIRST:
  603. tx_state[endpoint] = TX_STATE_ODD_FREE;
  604. break;
  605. case TX_STATE_BOTH_FREE_ODD_FIRST:
  606. tx_state[endpoint] = TX_STATE_EVEN_FREE;
  607. break;
  608. case TX_STATE_EVEN_FREE:
  609. case TX_STATE_ODD_FREE:
  610. default:
  611. tx_state[endpoint] = TX_STATE_NONE_FREE;
  612. break;
  613. }
  614. b->desc = BDT_DESC(packet->len, ((uint32_t)b & 8) ? DATA1 : DATA0);
  615. } else {
  616. //serial_print("tx no packet\n");
  617. switch (tx_state[endpoint]) {
  618. case TX_STATE_BOTH_FREE_EVEN_FIRST:
  619. case TX_STATE_BOTH_FREE_ODD_FIRST:
  620. break;
  621. case TX_STATE_EVEN_FREE:
  622. tx_state[endpoint] = TX_STATE_BOTH_FREE_EVEN_FIRST;
  623. break;
  624. case TX_STATE_ODD_FREE:
  625. tx_state[endpoint] = TX_STATE_BOTH_FREE_ODD_FIRST;
  626. break;
  627. default:
  628. tx_state[endpoint] = ((uint32_t)b & 8) ?
  629. TX_STATE_ODD_FREE : TX_STATE_EVEN_FREE;
  630. break;
  631. }
  632. }
  633. } else { // receive
  634. packet->len = b->desc >> 16;
  635. packet->index = 0;
  636. packet->next = NULL;
  637. if (rx_first[endpoint] == NULL) {
  638. //serial_print("rx 1st, epidx=");
  639. //serial_phex(endpoint);
  640. //serial_print(", packet=");
  641. //serial_phex32((uint32_t)packet);
  642. //serial_print("\n");
  643. rx_first[endpoint] = packet;
  644. } else {
  645. //serial_print("rx Nth, epidx=");
  646. //serial_phex(endpoint);
  647. //serial_print(", packet=");
  648. //serial_phex32((uint32_t)packet);
  649. //serial_print("\n");
  650. rx_last[endpoint]->next = packet;
  651. }
  652. rx_last[endpoint] = packet;
  653. // TODO: implement a per-endpoint maximum # of allocated packets
  654. // so a flood of incoming data on 1 endpoint doesn't starve
  655. // the others if the user isn't reading it regularly
  656. packet = usb_malloc();
  657. if (packet) {
  658. b->addr = packet->buf;
  659. b->desc = BDT_DESC(64, ((uint32_t)b & 8) ? DATA1 : DATA0);
  660. } else {
  661. //serial_print("starving ");
  662. //serial_phex(endpoint + 1);
  663. //serial_print(((uint32_t)b & 8) ? ",odd\n" : ",even\n");
  664. b->desc = 0;
  665. usb_rx_memory_needed++;
  666. }
  667. }
  668. }
  669. USB0_ISTAT = USB_ISTAT_TOKDNE;
  670. goto restart;
  671. }
  672. if (status & USB_ISTAT_USBRST /* 01 */ ) {
  673. //serial_print("reset\n");
  674. // initialize BDT toggle bits
  675. USB0_CTL = USB_CTL_ODDRST;
  676. ep0_tx_bdt_bank = 0;
  677. // set up buffers to receive Setup and OUT packets
  678. table[index(0, RX, EVEN)].desc = BDT_DESC(EP0_SIZE, 0);
  679. table[index(0, RX, EVEN)].addr = ep0_rx0_buf;
  680. table[index(0, RX, ODD)].desc = BDT_DESC(EP0_SIZE, 0);
  681. table[index(0, RX, ODD)].addr = ep0_rx1_buf;
  682. table[index(0, TX, EVEN)].desc = 0;
  683. table[index(0, TX, ODD)].desc = 0;
  684. // activate endpoint 0
  685. USB0_ENDPT0 = USB_ENDPT_EPRXEN | USB_ENDPT_EPTXEN | USB_ENDPT_EPHSHK;
  686. // clear all ending interrupts
  687. USB0_ERRSTAT = 0xFF;
  688. USB0_ISTAT = 0xFF;
  689. // set the address to zero during enumeration
  690. USB0_ADDR = 0;
  691. // enable other interrupts
  692. USB0_ERREN = 0xFF;
  693. USB0_INTEN = USB_INTEN_TOKDNEEN |
  694. USB_INTEN_SOFTOKEN |
  695. USB_INTEN_STALLEN |
  696. USB_INTEN_ERROREN |
  697. USB_INTEN_USBRSTEN |
  698. USB_INTEN_SLEEPEN;
  699. // is this necessary?
  700. USB0_CTL = USB_CTL_USBENSOFEN;
  701. return;
  702. }
  703. if ((status & USB_ISTAT_STALL /* 80 */ )) {
  704. //serial_print("stall:\n");
  705. USB0_ENDPT0 = USB_ENDPT_EPRXEN | USB_ENDPT_EPTXEN | USB_ENDPT_EPHSHK;
  706. USB0_ISTAT = USB_ISTAT_STALL;
  707. }
  708. if ((status & USB_ISTAT_ERROR /* 02 */ )) {
  709. uint8_t err = USB0_ERRSTAT;
  710. USB0_ERRSTAT = err;
  711. //serial_print("err:");
  712. //serial_phex(err);
  713. //serial_print("\n");
  714. USB0_ISTAT = USB_ISTAT_ERROR;
  715. }
  716. if ((status & USB_ISTAT_SLEEP /* 10 */ )) {
  717. //serial_print("sleep\n");
  718. USB0_ISTAT = USB_ISTAT_SLEEP;
  719. }
  720. }
  721. void usb_init(void)
  722. {
  723. int i;
  724. //serial_begin(BAUD2DIV(115200));
  725. //serial_print("usb_init\n");
  726. for (i=0; i <= NUM_ENDPOINTS*4; i++) {
  727. table[i].desc = 0;
  728. table[i].addr = 0;
  729. }
  730. // this basically follows the flowchart in the Kinetis
  731. // Quick Reference User Guide, Rev. 1, 03/2012, page 141
  732. // assume 48 MHz clock already running
  733. // SIM - enable clock
  734. SIM_SCGC4 |= SIM_SCGC4_USBOTG;
  735. // reset USB module
  736. USB0_USBTRC0 = USB_USBTRC_USBRESET;
  737. while ((USB0_USBTRC0 & USB_USBTRC_USBRESET) != 0) ; // wait for reset to end
  738. // set desc table base addr
  739. USB0_BDTPAGE1 = ((uint32_t)table) >> 8;
  740. USB0_BDTPAGE2 = ((uint32_t)table) >> 16;
  741. USB0_BDTPAGE3 = ((uint32_t)table) >> 24;
  742. // clear all ISR flags
  743. USB0_ISTAT = 0xFF;
  744. USB0_ERRSTAT = 0xFF;
  745. USB0_OTGISTAT = 0xFF;
  746. USB0_USBTRC0 |= 0x40; // undocumented bit
  747. // enable USB
  748. USB0_CTL = USB_CTL_USBENSOFEN;
  749. USB0_USBCTRL = 0;
  750. // enable reset interrupt
  751. USB0_INTEN = USB_INTEN_USBRSTEN;
  752. // enable interrupt in NVIC...
  753. NVIC_ENABLE_IRQ(IRQ_USBOTG);
  754. // enable d+ pullup
  755. USB0_CONTROL = USB_CONTROL_DPPULLUPNONOTG;
  756. }
  757. // return 0 if the USB is not configured, or the configuration
  758. // number selected by the HOST
  759. uint8_t usb_configured(void)
  760. {
  761. return usb_configuration;
  762. }