usbcdc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. /*
  2. File: usbcdc.h
  3. Copyright (c) 2010,2013 Kustaa Nyholm / SpareTimeLabs
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. Version 1.1 Compatible with SDCC 3.x
  16. Respond to GET_LINE_CODING to work with overzealous
  17. Windows software (like Hyperterminal)
  18. */
  19. #include "pic18f2550.h"
  20. #include "usbcdc.h"
  21. #include "usbpic_defs.h"
  22. #include "usbcdc_defs.h"
  23. #include "usb_defs.h"
  24. typedef __code unsigned char* codePtr;
  25. typedef __data unsigned char* dataPtr;
  26. // Device and configuration descriptors
  27. typedef struct {
  28. USB_CFG_DSC cd01;
  29. USB_INTF_DSC i01a00;
  30. USB_CDC_HEADER_FN_DSC cdc_header_fn_i01a00;
  31. USB_CDC_CALL_MGT_FN_DSC cdc_call_mgt_fn_i01a00;
  32. USB_CDC_ACM_FN_DSC cdc_acm_fn_i01a00;
  33. USB_CDC_UNION_FN_DSC cdc_union_fn_i01a00;
  34. USB_EP_DSC ep02i_i01a00;
  35. USB_INTF_DSC i02a00;
  36. USB_EP_DSC ep03o_i02a00;
  37. USB_EP_DSC ep03i_i02a00;
  38. } config_struct;
  39. // Global variables
  40. unsigned char usbcdc_device_state;
  41. static unsigned char device_address;
  42. static unsigned char current_configuration; // 0 or 1
  43. static unsigned char idx; // loop counter for data transfers loops
  44. static unsigned char control_stage; // Holds the current stage in a control transfer
  45. static unsigned char request_handled; // Set to 1 if request was understood and processed.
  46. static dataPtr data_ptr; // Data to host from RAM
  47. static codePtr code_ptr; // Data to host from FLASH
  48. static dataPtr in_ptr; // Data from the host
  49. static unsigned char dlen; // Number of unsigned chars of data
  50. // See USB spec chapter 5
  51. #define SETUP_STAGE 0
  52. #define DATA_OUT_STAGE 1
  53. #define DATA_IN_STAGE 2
  54. #define STATUS_STAGE 3
  55. __code unsigned char device_descriptor[] = { //
  56. 0x12, 0x01, // bLength, bDescriptorType
  57. 0x00, 0x02, // bcdUSB lsb, bcdUSB msb
  58. 0x02, 0x00, // bDeviceClass, bDeviceSubClass
  59. 0x00, E0SZ, // bDeviceProtocl, bMaxPacketSize
  60. 0x08, 0x04, // idVendor lsb, idVendor msb
  61. 0x0A, 0x00, // idProduct lsb, idProduct msb
  62. 0x00, 0x01, // bcdDevice lsb, bcdDevice msb
  63. 0x01, 0x00, // iManufacturer, iProduct
  64. 0x00, 0x01 // iSerialNumber (none), bNumConfigurations*/
  65. };
  66. __code unsigned char device_qualifier_descriptor[] = { //
  67. 0x0A, 0x06, // bLength, bDescriptorType
  68. 0x00, 0x02, // bcdUSB lsb, bcdUSB msb
  69. 0x02, 0x00, // bDeviceClass, bDeviceSubClass
  70. 0x00, E0SZ, // bDeviceProtocl, bMaxPacketSize
  71. 0x01, 0x00 // iSerialNumber, bNumConfigurations*/
  72. };
  73. static __code char const_values_0x00_0x00[] = { 0, 0 };
  74. static __code char const_values_0x00_0x01[] = { 0, 1 };
  75. static __code char const_values_0x01_0x00[] = { 1, 0 };
  76. static __code char const_values_status[] = { (USBCDC_SELF_POWERED<<0), 0 }; // first byte tells self powered and remote wakeup status
  77. __code config_struct
  78. config_descriptor = {
  79. {
  80. // Configuration descriptor
  81. sizeof(USB_CFG_DSC), DSC_CFG, // bLength, bDescriptorType (Configuration)
  82. sizeof(config_struct), // wTotalLength
  83. 0x02, 0x01, // bNumInterfaces, bConfigurationValue
  84. 0x00, 0x80 | (USBCDC_SELF_POWERED<<6),//_DEFAULT, // iConfiguration, bmAttributes ()
  85. USBCDC_MAXPOWER / 2, // bMaxPower
  86. },
  87. /* Interface Descriptor */
  88. { sizeof(USB_INTF_DSC), // Size of this descriptor in unsigned chars
  89. DSC_INTF, // INTERFACE descriptor type
  90. 0, // Interface Number
  91. 0, // Alternate Setting Number
  92. 1, // Number of endpoints in this intf
  93. COMM_INTF, // Class __code
  94. ABSTRACT_CONTROL_MODEL, // Subclass __code
  95. V25TER, // Protocol __code
  96. 0 // Interface string index
  97. },
  98. /* CDC Class-Specific Descriptors */
  99. { sizeof(USB_CDC_HEADER_FN_DSC), CS_INTERFACE,DSC_FN_HEADER,
  100. 0x0110 },
  101. { sizeof(USB_CDC_CALL_MGT_FN_DSC), CS_INTERFACE,
  102. DSC_FN_CALL_MGT, 0x01, CDC_DATA_INTF_ID},
  103. { sizeof(USB_CDC_ACM_FN_DSC), CS_INTERFACE,
  104. DSC_FN_ACM, 0x02 },
  105. { sizeof(USB_CDC_UNION_FN_DSC), CS_INTERFACE,
  106. DSC_FN_UNION, CDC_COMM_INTF_ID, CDC_DATA_INTF_ID },
  107. /* Endpoint Descriptor */
  108. {//notification endpoint
  109. sizeof(USB_EP_DSC), DSC_EP,_EP01_IN, _INT,CDC_INT_EP_SIZE, 0x0A, },
  110. /* Interface Descriptor */
  111. { sizeof(USB_INTF_DSC), // Size of this descriptor in unsigned chars
  112. DSC_INTF, // INTERFACE descriptor type
  113. 1, // Interface Number
  114. 0, // Alternate Setting Number
  115. 2, // Number of endpoints in this intf
  116. DATA_INTF, // Class __code
  117. 0, // Subclass __code
  118. NO_PROTOCOL, // Protocol __code
  119. 2, // Interface string index
  120. },
  121. /* Endpoint Descriptors */
  122. { sizeof(USB_EP_DSC), DSC_EP,_EP02_OUT,
  123. _BULK,USBCDC_BUFFER_LEN, 0x00, }, //
  124. { sizeof(USB_EP_DSC), DSC_EP,_EP02_IN, _BULK,USBCDC_BUFFER_LEN,
  125. 0x00 }, //
  126. };
  127. __code unsigned char string_descriptor0[] = { // available languages descriptor
  128. 0x04, STRING_DESCRIPTOR, //
  129. 0x09, 0x04, //
  130. };
  131. __code unsigned char string_descriptor1[] = { //
  132. 0x0E, STRING_DESCRIPTOR, // bLength, bDscType
  133. 'T', 0x00, //
  134. 'e', 0x00, //
  135. 's', 0x00, //
  136. 't', 0x00, //
  137. 'i', 0x00, //
  138. '!', 0x00, //
  139. };
  140. __code unsigned char string_descriptor2[] = { //
  141. 0x20, STRING_DESCRIPTOR, //
  142. 'U', 0x00, //
  143. 'S', 0x00, //
  144. 'B', 0x00, //
  145. ' ', 0x00, //
  146. 'G', 0x00, //
  147. 'e', 0x00, //
  148. 'n', 0x00, //
  149. 'e', 0x00, //
  150. 'r', 0x00, //
  151. 'i', 0x00, //
  152. 'c', 0x00, //
  153. ' ', 0x00, //
  154. 'C', 0x00, //
  155. 'D', 0x00, //
  156. 'C', 0x00, //
  157. };
  158. // Put endpoint 0 buffers into dual port RAM
  159. // Put USB I/O buffers into dual port RAM.
  160. //#pragma udata usbram5 setup_packet control_transfer_buffer cdc_rx_buffer cdc_tx_buffer cdcint_buffer
  161. //#pragma udata usb_data cdc_tx_buffer cdcint_buffer
  162. //#pragma udata usb_data2 setup_packet control_transfer_buffer cdc_rx_buffer
  163. static volatile setup_packet_struct setup_packet;
  164. static volatile unsigned char control_transfer_buffer[E0SZ];
  165. // CDC buffers
  166. static unsigned char tx_len = 0;
  167. static unsigned char rx_idx = 0;
  168. //static volatile unsigned char cdcint_buffer[USBCDC_BUFFER_LEN];
  169. volatile unsigned char cdc_rx_buffer[USBCDC_BUFFER_LEN];
  170. volatile unsigned char cdc_tx_buffer[USBCDC_BUFFER_LEN];
  171. static volatile unsigned char cdcint_buffer[USBCDC_BUFFER_LEN];
  172. static __code char cdc_line_coding[7] = {9600&0xFF,9600>>8,0,0,0,0,8};
  173. //static __code char cdc_line_coding[7] = {9600&0xFF,9600>>8,0,0,1,2,8};
  174. void usbcdc_putchar(char c)__wparam
  175. {
  176. while (usbcdc_wr_busy());
  177. cdc_tx_buffer[tx_len++]=c;
  178. if (tx_len>=sizeof(cdc_tx_buffer)) {
  179. usbcdc_flush();
  180. }
  181. }
  182. char usbcdc_wr_busy() {
  183. return (ep2_i.STAT & UOWN)!=0;
  184. }
  185. unsigned char usbcdc_rd_ready() {
  186. if (ep2_o.STAT & UOWN)
  187. return 0;
  188. if (rx_idx >= ep2_o.CNT) {
  189. usbcdc_read();
  190. return 0;
  191. }
  192. return 1;
  193. }
  194. void usbcdc_write(unsigned char len)__wparam
  195. {
  196. if (len> 0) {
  197. ep2_i.CNT = len;
  198. if (ep2_i.STAT & DTS)
  199. ep2_i.STAT = UOWN | DTSEN;
  200. else
  201. ep2_i.STAT = UOWN | DTS | DTSEN;
  202. }
  203. }
  204. void usbcdc_flush() {
  205. usbcdc_write(tx_len);
  206. tx_len = 0;
  207. }
  208. void usbcdc_read() {
  209. rx_idx=0;
  210. ep2_o.CNT = sizeof(cdc_rx_buffer);
  211. if (ep2_o.STAT & DTS)
  212. ep2_o.STAT = UOWN | DTSEN;
  213. else
  214. ep2_o.STAT = UOWN | DTS | DTSEN;
  215. }
  216. char usbcdc_getchar() {
  217. char c;
  218. while (!usbcdc_rd_ready());
  219. c = cdc_rx_buffer[rx_idx++];
  220. if (rx_idx>=ep2_o.CNT) {
  221. usbcdc_read();
  222. }
  223. return c;
  224. }
  225. static void get_descriptor(void) {
  226. if (setup_packet.bmrequesttype == 0x80) {
  227. unsigned char descriptorType = setup_packet.wvalue1;
  228. unsigned char descriptorIndex = setup_packet.wvalue0;
  229. if (descriptorType == DEVICE_DESCRIPTOR) {
  230. request_handled = 1;
  231. code_ptr = (codePtr) &device_descriptor;
  232. dlen = *code_ptr;//DEVICE_DESCRIPTOR_SIZE;
  233. } else if (descriptorType == QUALIFIER_DESCRIPTOR) {
  234. request_handled = 1;
  235. code_ptr = (codePtr) &device_qualifier_descriptor;
  236. dlen = sizeof(device_qualifier_descriptor);
  237. } else if (descriptorType == CONFIGURATION_DESCRIPTOR) {
  238. request_handled = 1;
  239. code_ptr = (codePtr) &config_descriptor;
  240. dlen = *(code_ptr + 2);
  241. } else if (descriptorType == STRING_DESCRIPTOR) {
  242. request_handled = 1;
  243. if (descriptorIndex == 0) {
  244. code_ptr = (codePtr) &string_descriptor0;
  245. } else if (descriptorIndex == 1) {
  246. code_ptr = (codePtr) &string_descriptor1;
  247. } else {
  248. code_ptr = (codePtr) &string_descriptor2;
  249. }
  250. dlen = *code_ptr;
  251. }
  252. }
  253. }
  254. // Process GET_STATUS
  255. static void get_status(void) {
  256. // Mask off the Recipient bits
  257. unsigned char recipient = setup_packet.bmrequesttype & 0x1F;
  258. // See where the request goes
  259. if (recipient == 0x00) {
  260. // Device
  261. request_handled = 1;
  262. code_ptr = (codePtr) &const_values_status; // hard __code device status
  263. } else if (recipient == 0x01) {
  264. // Interface
  265. code_ptr = (codePtr) &const_values_0x00_0x00;
  266. request_handled = 1;
  267. } else if (recipient == 0x02) {
  268. // Endpoint
  269. unsigned char endpointNum = setup_packet.windex0 & 0x0F;
  270. unsigned char endpointDir = setup_packet.windex0 & 0x80;
  271. request_handled = 1;
  272. // Endpoint descriptors are 8 unsigned chars long, with each in and out taking 4 unsigned chars
  273. // within the endpoint. (See PIC datasheet.)
  274. in_ptr = (dataPtr) &ep0_o + (endpointNum * 8);
  275. if (endpointDir)
  276. in_ptr += 4;
  277. if (*in_ptr & BSTALL)
  278. code_ptr = (codePtr) &const_values_0x01_0x00;
  279. }
  280. if (request_handled) {
  281. dlen = 2;
  282. }
  283. }
  284. // Process SET_FEATURE and CLEAR_FEATURE
  285. static void set_feature(void) {
  286. unsigned char recipient = setup_packet.bmrequesttype & 0x1F;
  287. unsigned char feature = setup_packet.wvalue0;
  288. if (recipient == 0x02) {
  289. // Endpoint
  290. unsigned char endpointNum = setup_packet.windex0 & 0x0F;
  291. unsigned char endpointDir = setup_packet.windex0 & 0x80;
  292. if ((feature == ENDPOINT_HALT) && (endpointNum != 0)) {
  293. char temp; // FIXME can't we find a global variable
  294. // Halt endpoint (as long as it isn't endpoint 0)
  295. request_handled = 1;
  296. // Endpoint descriptors are 8 unsigned chars long, with each in and out taking 4 unsigned chars
  297. // within the endpoint. (See PIC datasheet.)
  298. in_ptr = (dataPtr) &ep0_o + (endpointNum * 8);
  299. if (endpointDir)
  300. in_ptr += 4;
  301. // FIXME figure out what this is
  302. if (setup_packet.brequest == SET_FEATURE)
  303. temp = 0x84;
  304. else {
  305. if (endpointDir == 1)
  306. temp = 0x00;
  307. else
  308. temp = 0x88;
  309. }
  310. *in_ptr = temp;
  311. }
  312. }
  313. }
  314. // Data stage for a Control Transfer that sends data to the host
  315. void in_data_stage(void) {
  316. unsigned char bufferSize;
  317. // Determine how many unsigned chars are going to the host
  318. if (dlen < E0SZ)
  319. bufferSize = dlen;
  320. else
  321. bufferSize = E0SZ;
  322. // Load the high two bits of the unsigned char dlen into BC8:BC9
  323. ep0_i.STAT &= ~(BC8| BC9); // Clear BC8 and BC9
  324. //ep0_i.STAT |= (unsigned char) ((bufferSize & 0x0300) >> 8);
  325. //ep0_i.CNT = (unsigned char) (bufferSize & 0xFF);
  326. ep0_i.CNT = bufferSize;
  327. ep0_i.ADDR = (int) &control_transfer_buffer;
  328. // Update the number of unsigned chars that still need to be sent. Getting
  329. // all the data back to the host can take multiple transactions, so
  330. // we need to track how far along we are.
  331. dlen = dlen - bufferSize;
  332. // Move data to the USB output buffer from wherever it sits now.
  333. in_ptr = (dataPtr) &control_transfer_buffer;
  334. // for (idx = 0; idx < bufferSize; idx++)
  335. for (idx = bufferSize; idx--;)
  336. *in_ptr++ = *code_ptr++;
  337. }
  338. void prepare_for_setup_stage(void) {
  339. control_stage = SETUP_STAGE;
  340. ep0_o.CNT = E0SZ;
  341. ep0_o.ADDR = (int) &setup_packet;
  342. ep0_o.STAT = UOWN | DTSEN;
  343. ep0_i.STAT = 0x00;
  344. UCONbits.PKTDIS = 0;
  345. }
  346. char debug=0;
  347. void process_control_transfer(void) {
  348. if (USTAT == USTAT_OUT) {
  349. unsigned char PID = (ep0_o.STAT & 0x3C) >> 2; // Pull PID from middle of BD0STAT
  350. if (PID == 0x0D) {
  351. // Setup stage
  352. // Note: Microchip says to turn off the UOWN bit on the IN direction as
  353. // soon as possible after detecting that a SETUP has been received.
  354. ep0_i.STAT &= ~UOWN;
  355. ep0_o.STAT &= ~UOWN;
  356. // Initialize the transfer process
  357. control_stage = SETUP_STAGE;
  358. request_handled = 0; // Default is that request hasn't been handled
  359. dlen = 0; // No unsigned chars transferred
  360. // See if this is a standard (as definded in USB chapter 9) request
  361. debug=setup_packet.bmrequesttype;
  362. if (1 /* (setup_packet.bmrequesttype & 0x60) == 0x00*/) {// ----------
  363. unsigned char request = setup_packet.brequest;
  364. debug = request;
  365. if (request == SET_ADDRESS) {
  366. // Set the address of the device. All future requests
  367. // will come to that address. Can't actually set UADDR
  368. // to the new address yet because the rest of the SET_ADDRESS
  369. // transaction uses address 0.
  370. request_handled = 1;
  371. usbcdc_device_state = ADDRESS;
  372. device_address = setup_packet.wvalue0;
  373. } else if (request == GET_DESCRIPTOR) {
  374. get_descriptor();
  375. } else if (request == SET_CONFIGURATION) {
  376. request_handled = 1;
  377. current_configuration = setup_packet.wvalue0;
  378. // TBD: ensure the new configuration value is one that
  379. // exists in the descriptor.
  380. if (current_configuration == 0) {
  381. // If configuration value is zero, device is put in
  382. // address state (USB 2.0 - 9.4.7)
  383. usbcdc_device_state = ADDRESS;
  384. } else {
  385. // Set the configuration.
  386. usbcdc_device_state = CONFIGURED;
  387. // Initialize the endpoints for all interfaces
  388. { // Turn on both in and out for this endpoint
  389. UEP1 = 0x1E;
  390. ep1_i.ADDR = (int) &cdcint_buffer;
  391. ep1_i.STAT = DTS;
  392. UEP2 = 0x1E;
  393. ep2_o.CNT = sizeof(cdc_rx_buffer);
  394. ep2_o.ADDR = (int) &cdc_rx_buffer;
  395. ep2_o.STAT = UOWN | DTSEN; //set up to receive stuff as soon as we get something
  396. ep2_i.ADDR = (int) &cdc_tx_buffer;
  397. ep2_i.STAT = DTS;
  398. }
  399. }
  400. } else if (request == GET_CONFIGURATION) { // Never seen in Windows
  401. request_handled = 1;
  402. code_ptr = (codePtr) &const_values_0x00_0x01[current_configuration];
  403. dlen = 1;
  404. } else if (request == GET_STATUS) { // Never seen in Windows
  405. get_status();
  406. } else if ((request == CLEAR_FEATURE) || (request
  407. == SET_FEATURE)) { // Never seen in Windows
  408. set_feature();
  409. } else if (request == GET_INTERFACE) { // Never seen in Windows
  410. // No support for alternate interfaces. Send
  411. // zero back to the host.
  412. request_handled = 1;
  413. code_ptr = (codePtr) (&const_values_0x00_0x00);
  414. dlen = 1;
  415. } else if ((request == SET_INTERFACE) || (request == SET_LINE_CODING) || (request == SET_CONTROL_LINE_STATE)) {
  416. // No support for alternate interfaces - just ignore.
  417. request_handled = 1;
  418. } else if (request == GET_LINE_CODING) {
  419. code_ptr = (codePtr) (&cdc_line_coding);
  420. dlen = sizeof(cdc_line_coding);
  421. request_handled = 1;
  422. }
  423. ////////////////////
  424. /*
  425. // If request recipient is not an interface then return
  426. if((SetupPacket.bmRequestType & 0x1f)!= 0x01) return;
  427. // If request type is not class-specific then return
  428. if((SetupPacket.bmRequestType & 0x60)!= 0x20) return;
  429. // Interface ID must match interface numbers associated with
  430. // CDC class, else return
  431. if((SetupPacket.wIndex0 != 0x00)&&
  432. (SetupPacket.wIndex0 != 0x01)) return;
  433. switch(SetupPacket.bRequest)
  434. {
  435. case SEND_ENCAPSULATED_COMMAND:
  436. requestHandled = 1;
  437. outPtr = dummy_encapsulated_cmd_response;
  438. wCount = DUMMY_LENGTH;
  439. break;
  440. case GET_ENCAPSULATED_RESPONSE:
  441. requestHandled = 1;
  442. // Populate dummy_encapsulated_cmd_response first.
  443. inPtr = dummy_encapsulated_cmd_response;
  444. break;
  445. case SET_LINE_CODING:
  446. requestHandled = 1;
  447. inPtr = (byte*)&line_coding; // Set destination
  448. break;
  449. case GET_LINE_CODING:
  450. requestHandled = 1;
  451. outPtr = (byte*) &line_coding; // Set source
  452. wCount = sizeof(LINE_CODING); // Set data count
  453. break;
  454. */
  455. ////////////////////
  456. } //----------
  457. if (!request_handled) {
  458. // If this service wasn't handled then stall endpoint 0
  459. ep0_o.CNT = E0SZ;
  460. ep0_o.ADDR = (int) &setup_packet;
  461. ep0_o.STAT = UOWN | BSTALL;
  462. ep0_i.STAT = UOWN | BSTALL;
  463. } else if (setup_packet.bmrequesttype & 0x80) {
  464. // Device-to-host
  465. if (setup_packet.wlength < dlen)//9.4.3, p.253
  466. dlen = setup_packet.wlength;
  467. in_data_stage();
  468. control_stage = DATA_IN_STAGE;
  469. // Reset the out buffer descriptor for endpoint 0
  470. ep0_o.CNT = E0SZ;
  471. ep0_o.ADDR = (int) &setup_packet;
  472. ep0_o.STAT = UOWN;
  473. // Set the in buffer descriptor on endpoint 0 to send data
  474. // NOT NEEDED ep0_i.ADDR = (int) &control_transfer_buffer;
  475. // Give to SIE, DATA1 packet, enable data toggle checks
  476. ep0_i.STAT = UOWN | DTS | DTSEN;
  477. } else {
  478. // Host-to-device
  479. control_stage = DATA_OUT_STAGE;
  480. // Clear the input buffer descriptor
  481. ep0_i.CNT = 0;
  482. ep0_i.STAT = UOWN | DTS | DTSEN;
  483. // Set the out buffer descriptor on endpoint 0 to receive data
  484. ep0_o.CNT = E0SZ;
  485. ep0_o.ADDR = (int) &control_transfer_buffer;
  486. // Give to SIE, DATA1 packet, enable data toggle checks
  487. ep0_o.STAT = UOWN | DTS | DTSEN;
  488. }
  489. // Enable SIE token and packet processing
  490. UCONbits.PKTDIS = 0;
  491. } else if (control_stage == DATA_OUT_STAGE) {
  492. // Complete the data stage so that all information has
  493. // passed from host to device before servicing it.
  494. {
  495. unsigned char bufferSize;
  496. //bufferSize = ((0x03 & ep0_o.STAT) << 8) | ep0_o.CNT;
  497. bufferSize = ep0_o.CNT;
  498. // Accumulate total number of unsigned chars read
  499. dlen = dlen + bufferSize;
  500. data_ptr = (dataPtr) &control_transfer_buffer;
  501. for (idx = bufferSize; idx--;)
  502. *in_ptr++ = *data_ptr++;
  503. }
  504. // Turn control over to the SIE and toggle the data bit
  505. if (ep0_o.STAT & DTS)
  506. ep0_o.STAT = UOWN | DTSEN;
  507. else
  508. ep0_o.STAT = UOWN | DTS | DTSEN;
  509. } else {
  510. // Prepare for the Setup stage of a control transfer
  511. prepare_for_setup_stage();
  512. }
  513. } else if (USTAT == USTAT_IN) {
  514. // Endpoint 0:in
  515. //set address
  516. if ((UADDR == 0) && (usbcdc_device_state == ADDRESS)) {
  517. // TBD: ensure that the new address matches the value of
  518. // "device_address" (which came in through a SET_ADDRESS).
  519. UADDR = setup_packet.wvalue0;
  520. if (UADDR == 0) {
  521. // If we get a reset after a SET_ADDRESS, then we need
  522. // to drop back to the Default state.
  523. usbcdc_device_state = DEFAULT;
  524. }
  525. }
  526. if (control_stage == DATA_IN_STAGE) {
  527. // Start (or continue) transmitting data
  528. in_data_stage();
  529. // Turn control over to the SIE and toggle the data bit
  530. if (ep0_i.STAT & DTS)
  531. ep0_i.STAT = UOWN | DTSEN;
  532. else
  533. ep0_i.STAT = UOWN | DTS | DTSEN;
  534. } else {
  535. // Prepare for the Setup stage of a control transfer
  536. prepare_for_setup_stage();
  537. }
  538. }
  539. }
  540. void usbcdc_init() {
  541. UCFG = 0x14; // Enable pullup resistors; full speed mode
  542. usbcdc_device_state = DETACHED;
  543. // remote_wakeup = 0x00;
  544. current_configuration = 0x00;
  545. // attach
  546. if (UCONbits.USBEN == 0) {//enable usb controller
  547. UCON = 0;
  548. UIE = 0;
  549. UCONbits.USBEN = 1;
  550. usbcdc_device_state = ATTACHED;
  551. }
  552. {//Wait for bus reset
  553. UIR = 0;
  554. UIE = 0;
  555. UIEbits.URSTIE = 1;
  556. usbcdc_device_state = POWERED;
  557. }
  558. PIE2bits.USBIE = 1;
  559. }
  560. // Main entry point for USB tasks. Checks interrupts, then checks for transactions.
  561. void usbcdc_handler(void) {
  562. if ((UCFGbits.UTEYE == 1) || //eye test
  563. (usbcdc_device_state == DETACHED) || //not connected
  564. (UCONbits.SUSPND == 1))//suspended
  565. return;
  566. // Process a bus reset
  567. if (UIRbits.URSTIF && UIEbits.URSTIE) {
  568. { // bus_reset
  569. UEIR = 0x00;
  570. UIR = 0x00;
  571. UEIE = 0x9f;
  572. UIE = 0x2b;
  573. UADDR = 0x00;
  574. // Set endpoint 0 as a control pipe
  575. UEP0 = 0x16;
  576. // Flush any pending transactions
  577. while (UIRbits.TRNIF == 1)
  578. UIRbits.TRNIF = 0;
  579. // Enable packet processing
  580. UCONbits.PKTDIS = 0;
  581. // Prepare for the Setup stage of a control transfer
  582. prepare_for_setup_stage();
  583. current_configuration = 0; // Clear active configuration
  584. usbcdc_device_state = DEFAULT;
  585. }
  586. UIRbits.URSTIF = 0;
  587. }
  588. //nothing is done to start of frame
  589. if (UIRbits.SOFIF && UIEbits.SOFIE) {
  590. UIRbits.SOFIF = 0;
  591. }
  592. // stall processing
  593. if (UIRbits.STALLIF && UIEbits.STALLIE) {
  594. if (UEP0bits.EPSTALL == 1) {
  595. // Prepare for the Setup stage of a control transfer
  596. prepare_for_setup_stage();
  597. UEP0bits.EPSTALL = 0;
  598. }
  599. UIRbits.STALLIF = 0;
  600. }
  601. if (UIRbits.UERRIF && UIEbits.UERRIE) {
  602. // Clear errors
  603. UIRbits.UERRIF = 0;
  604. }
  605. // A transaction has finished. Try default processing on endpoint 0.
  606. if (UIRbits.TRNIF && UIEbits.TRNIE) {
  607. process_control_transfer();
  608. // Turn off interrupt
  609. UIRbits.TRNIF = 0;
  610. }
  611. }