yc11xx_usb.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include "yc11xx_usb.h"
  4. //#include "Drv_debug.h"
  5. #include "yc11xx_gpio.h"
  6. const device_descriptor devicedesc =
  7. {
  8. 0x12,
  9. 0x01,
  10. 0x0002,
  11. 0x00,
  12. 0x00,
  13. 0x00,
  14. EP0_PACKET_SIZE,
  15. 0x1224,
  16. 0x1218,
  17. 0x0200,
  18. 0x01,
  19. 0x02,
  20. 0x00,
  21. 0x01,
  22. }; //end of DEVICEDESC
  23. const uint8_t hidreportdesc_kb[HID_KB_RP_DESCRIPTOR_LEN] =
  24. {
  25. //kb hid
  26. 0x05,0x01, // Usage Page(Generic Desktop)
  27. 0x09,0x06, // Usage(Keyboard)
  28. 0xa1,0x01, // Collection(Application)
  29. 0x05,0x07, // Usage Page(Keyboard/Keypad)
  30. 0x19,0xe0, // Usage Minimum(224)
  31. 0x29,0xe7, // Usage Maximum(231)
  32. 0x15,0x00, // Logical Minimum(0)
  33. 0x25,0x01, // Logical Maximum(1)
  34. 0x75,0x01, // Report Size(1)
  35. 0x95,0x08, // Report Count(8)
  36. 0x81,0x02, // Input(Data Variable Absolute) ; Modifier byte
  37. 0x95,0x01, // Report Count(1)
  38. 0x75,0x08, // Report Size(8)
  39. 0x81,0x01, // Input(Padding) ; Reserved byte
  40. 0x95,0x05, // Report Count(5)
  41. 0x75,0x01, // Report Size(1)
  42. 0x05,0x08, // Usage Page(LEDs)
  43. 0x19,0x01, // Usage Minimum(1)
  44. 0x29,0x05, // Usage Maximum(5)
  45. 0x91,0x02, // Output(Data Variable Absolute) ; LED report
  46. 0x95,0x01, // Report Count(1)
  47. 0x75,0x03, // Report Size(3)
  48. 0x91,0x01, // Output(Padding) ; LED report padding
  49. 0x95,0x06, // Report Count(6)
  50. 0x75,0x08, // Report Size(8)
  51. 0x15,0x00, // Logical Minimum(0)
  52. 0x25,0x68, // Usage Maximum(104)
  53. 0x05,0x07, // Usage Page(Keyboard/Keypad)
  54. 0x19,0x00, // Usage Minimum(0)
  55. 0x29,0x68, // Usage Maximum(104)
  56. 0x81,0x00, // Input(Data Array) ; Key arrarys 6 bytes
  57. 0xc0
  58. };
  59. const uint8_t hidreportdesc_m[HID_MO_RP_DESCRIPTOR_LEN]={
  60. //====================鼠标====75====================================
  61. 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
  62. 0x09, 0x02, // USAGE (Mouse)
  63. 0xa1, 0x01, // COLLECTION (Application)
  64. 0x85, 0x01, // Report ID (1)
  65. 0x09, 0x01, // USAGE (Pointer)
  66. 0xa1, 0x00, // COLLECTION (Physical)
  67. 0x05, 0x09, // USAGE_PAGE (Button)
  68. 0x19, 0x01, // USAGE_MINIMUM (Button 1)
  69. 0x29, 0x05, // USAGE_MAXIMUM (Button 3)
  70. 0x15, 0x00, // LOGICAL_MINIMUM (0)
  71. 0x25, 0x01, // LOGICAL_MAXIMUM (1)
  72. 0x95, 0x05, // REPORT_COUNT (5)
  73. 0x75, 0x01, // REPORT_SIZE (1)
  74. 0x81, 0x02, // INPUT (Data,Var,Abs)
  75. 0x95, 0x01, // REPORT_COUNT (1)
  76. 0x75, 0x03, // REPORT_SIZE (3)
  77. 0x81, 0x01, // INPUT (Cnst,Var,Abs)
  78. 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
  79. 0x09, 0x30, // USAGE (X)
  80. 0x09, 0x31, // USAGE (Y)
  81. 0x16, 0x01, 0xF8, // Logical Minimum (-2047)
  82. 0x26, 0xFF, 0x07, // Logical Maximum (2047)
  83. 0x75, 0x10, // REPORT_SIZE (16)
  84. 0x95, 0x02, // REPORT_COUNT (2)
  85. 0x81, 0x06, // INPUT (Data,Var,Rel)
  86. 0x09, 0x38, // USAGE (Wheel)
  87. 0x15, 0x81, // LOGICAL_MINIMUM (-127)
  88. 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
  89. 0x75, 0x08, // REPORT_SIZE (8)
  90. 0x95, 0x01, // REPORT_COUNT (1)
  91. 0x81, 0x06, // INPUT (Data,Var,Rel)
  92. 0x05, 0x0c, // USAGE_PAGE(Consumer Devices)
  93. 0x0a, 0x38,0x02,
  94. 0x95, 0x01, // REPORT_COUNT (1)
  95. 0x81, 0x06, // INPUT (Data,Var,Rel)
  96. 0xc0, // END_COLLECTION
  97. 0xc0, // END_COLLECTION
  98. //====================电源控制======31==============================
  99. 0x05, 0x01, // Usage Page (Generic Desktop),
  100. 0x09, 0x80, // USAGE (3-D Digitizer)
  101. 0xa1, 0x01, // Collection (Application),
  102. 0x85, 0x02, // Report ID (2)
  103. 0x05, 0x01, // Usage Page (Generic Desktop),
  104. 0x19, 0x81, // Usage Minimum (),
  105. 0x29, 0x83, // Usage Maximum (),
  106. 0x15, 0x00, // Logical Minimum (0),
  107. 0x25, 0x01, // Logical Maximum (1),
  108. 0x95, 0x03, // Report Count (3),
  109. 0x75, 0x01, // Report Size (1),
  110. 0x81, 0x06, // Input (Data,Var,Rel)
  111. 0x95, 0x01, // Report Count (1),
  112. 0x75, 0x05, // Report Size (5),
  113. 0x81, 0x01, // Input (Const,Ary,Abs)
  114. 0xc0, // END_COLLECTION
  115. //====================多媒体按键控制=======25========================
  116. 0x05, 0x0c, // USAGE_PAGE (Consumer Devices)
  117. 0x09, 0x01, // USAGE (Consumer Control)
  118. 0xa1, 0x01, // COLLECTION (Application)
  119. 0x85, 0x03, // Report ID (3)
  120. 0x15, 0x00, // LOGICAL_MINIMUM (0)
  121. 0x26, 0x80,
  122. 0x03, 0x19,
  123. 0x00, 0x2a,
  124. 0x80, 0x03,
  125. 0x75, 0x10, // REPORT_SIZE (0x10)
  126. 0x95, 0x01, // REPORT_COUNT (1)
  127. 0x81, 0x00, // Input (Data,Ary,Abs)
  128. 0xc0, // END_COLLECTION
  129. //====================MCE键盘控制=======26===========================
  130. 0x06, 0xbc, 0xff, // USAGE_PAGE (Vendor Defined Page *)
  131. 0x09, 0x88, // USAGE(?)
  132. 0xa1, 0x01, // Collection (Application),
  133. 0x85, 0x04, // Report ID (4)
  134. 0x19, 0x00, // USAGE_MINIMUM (undefined)
  135. 0x2a, 0xff, 0x00,
  136. 0x15, 0x00, // Logical Minimum (0),
  137. 0x26, 0xff, 0x00,
  138. 0x75, 0x08, // REPORT_SIZE (8)
  139. 0x95, 0x01, // REPORT_COUNT (1)
  140. 0x81, 0x00, // Input (Data,Ary,Abs)
  141. 0xc0, // END_COLLECTION
  142. //====================beken============38======================
  143. 0x06, 0x00, 0xff, // USAGE_PAGE (Vendor Defined Page 1)
  144. 0x09, 0x0e, // USAGE (Vendor Usage 1)
  145. 0xa1, 0x01, // COLLECTION (Application)
  146. 0x85, 0xBA, // Report ID
  147. 0x95, 0x1f, // REPORT_COUNT ( )
  148. 0x75, 0x08, // REPORT_SIZE (8)
  149. 0x26, 0xff, 0x00, // LOGICAL_MAXIMUM (255)
  150. 0x15, 0x00, // LOGICAL_MINIMUM (0)
  151. 0x09, 0x01, // USAGE (Vendor Usage 1)
  152. 0x91, 0x02, // OUTPUT (Data,Var,Abs)
  153. 0x85, 0xBA, // Report ID
  154. 0x95, 0x1f, // REPORT_COUNT ( )
  155. 0x75, 0x08, // REPORT_SIZE (8)
  156. 0x26, 0xff, 0x00, // LOGICAL_MAXIMUM (255)
  157. 0x15, 0x00, // LOGICAL_MINIMUM (0)
  158. 0x09, 0x01, // USAGE (Vendor Usage 1)
  159. 0x81, 0x02, // INPUT (Data,Var,Abs)
  160. 0xC0
  161. };
  162. const uint8_t confdesc[HID_CONFIG_DESCRIPTOR_LEN] = {
  163. // From "USB Device Class Definition for Human Interface Devices (HID)".
  164. // Section 7.1:
  165. // "When a Get_Descriptor(Configuration) request is issued,
  166. // it returns the Configuration confdesc, all Interface descriptors,
  167. // all Endpoint descriptors, and the HID confdesc for each interface."
  168. // configuration_descriptor hid_configuration_descriptor
  169. //config
  170. 0x09,//0xLength
  171. 0x02,//0xType
  172. HID_CONFIG_DESCRIPTOR_LEN, 0x00,//0xTotallength
  173. 0x02,//0xNumInterfaces
  174. 0x01,//0xbConfigurationValue
  175. 0x00,//0xstringindexConfiguration
  176. 0xa0,//0xbmAttributes
  177. 0x32,//0xMaxPower0x(in0x2mA0xunits)
  178. //0xinterface_descriptor0xhid_interface_descriptor--mouse
  179. 0x09,//0xbLength
  180. 0x04,//0xbDescriptorType
  181. 0x00,//0xbInterfaceNumber
  182. 0x00,//0xbAlternateSetting
  183. 0x01,//0xbNumEndpoints
  184. 0x03,//0xbInterfaceClass0x(30x=0xHID)
  185. 0x01,//0xbInterfaceSubClass
  186. 0x01,//0xbInterfaceProcotol0x0x0x;0x0x2=mouse
  187. 0x00,//0xiInterface
  188. //0xclass_descriptor0xhid_descriptor--mouse 0x0x0x0x0x
  189. 0x09,//0xbLength
  190. 0x21,//0xbDescriptorType
  191. 0x00,0x02,//0xbcdHID
  192. 0x00,//0xbCountryCode
  193. 0x01,//0xbNumDescriptors
  194. 0x22,//0xbDescriptorType
  195. HID_KB_RP_DESCRIPTOR_LEN,0x00,//0xwItemLength0x(tot.0xlen.0xof0xreport0xconfdesc)
  196. //0xendpoint_descriptor0xhid_endpoint_in_descriptor--mouse
  197. 0x07,//0xbLength
  198. 0x05,//0xbDescriptorType
  199. 0x81,//0xbEndpointAddress
  200. 0x03,//0xbmAttributes
  201. EP1_PACKET_SIZE,0x00,//0xMaxPacketSize0x
  202. 0x02,//0xbInterval
  203. 0x09,
  204. 0x04, //bDescriptorType
  205. 0x01, //bInterfaceNumber
  206. 0x00, //bAlternateSetting
  207. 0x01, //bNumEndpoints
  208. 0x03, //bInterfaceClass (3 = HID)
  209. 0x01, //bInterfaceSubClass
  210. 0x02, //bInterfaceProcotol ; 2=mouse
  211. 0x00, //iInterface
  212. //class_descriptor hid_descriptor--mouse
  213. 0x09, //bLength
  214. 0x21, //bDescriptorType
  215. 0x00,0x02, //bcdHID
  216. 0x00, //bCountryCode
  217. 0x01, //bNumDescriptors
  218. 0x22, //bDescriptorType
  219. HID_MO_RP_DESCRIPTOR_LEN,0x00, //wItemLength (tot. len. of report confdesc)
  220. //endpoint_descriptor hid_endpoint_in_descriptor--mouse
  221. 0x07, //bLength
  222. 0x05, //bDescriptorType
  223. 0x82, //bEndpointAddress
  224. 0x03, // bmAttributes
  225. EP2_PACKET_SIZE, 0x00, //MaxPacketSize (LITTLE ENDIAN)
  226. 0x02, //bInterval
  227. };
  228. #define STR0LEN 4
  229. const uint8_t string0[STR0LEN] =
  230. {
  231. STR0LEN, 0x03, 0x09, 0x04
  232. }; //end of string0_desc
  233. #define STR1LEN sizeof ("YICHIP") * 2
  234. const uint8_t string1[] =
  235. {
  236. STR1LEN, 0x03,
  237. 'Y', 0,
  238. 'i', 0,
  239. 'C', 0,
  240. 'h', 0,
  241. 'i', 0,
  242. 'p', 0
  243. }; //end of string1_desc
  244. #define STR2LEN sizeof ("YICHIP 2.4G Device") * 2
  245. const uint8_t string2[] =
  246. {
  247. STR2LEN, 0x03,
  248. 'Y', 0,
  249. 'i', 0,
  250. 'C', 0,
  251. 'h', 0,
  252. 'i', 0,
  253. 'p', 0,
  254. ' ', 0,
  255. '2', 0,
  256. '.', 0,
  257. '4', 0,
  258. 'G', 0,
  259. ' ', 0,
  260. 'D', 0,
  261. 'e', 0,
  262. 'v', 0,
  263. 'i', 0,
  264. 'c', 0,
  265. 'e', 0
  266. }; //end of string2_desc
  267. #define STR3LEN sizeof ("b120300001") * 2
  268. const uint8_t string3[]=
  269. {
  270. STR3LEN,0x03,
  271. 'b','0',
  272. '1','0',
  273. '2','0',
  274. '0','0',
  275. '3','0',
  276. '0','0',
  277. '0','0',
  278. '0','0',
  279. '0','0',
  280. '1','0',
  281. };//"b120300001";
  282. const uint8_t* const string_table [] =
  283. {
  284. string0,
  285. string1,
  286. string2,
  287. string3
  288. };
  289. extern uint8_t ms_data[8];
  290. extern uint8_t kb_data[8];
  291. extern uint8_t kb_multikey[8];
  292. extern uint8_t usb_ep1_data,usb_ep2_data;
  293. uint8_t ONES_PACKET[2] ={0x01,0x00};
  294. uint8_t ZERO_PACKET[2] ={0x00,0x00};
  295. uint8_t TWO_PACKET[2] ={0x02,0x00};
  296. usb_setup usb0_setup ;
  297. const uint8_t *bufptr;
  298. uint8_t remain,isstr;
  299. uint8_t usb_state,usb0_state,usb_tx_enable,usb_wakestate_onetime_flag;
  300. uint8_t usb_tx_wait,usb_clear_halt,usb_tx_win_enable,usb_mac_wake_trig;
  301. uint8_t usb_set_high_addr_flag,usb_get_protocol_flag,
  302. usb_device_enumeration_endflag;
  303. uint8_t usb_idle_flag,usb_set_protocol_status,
  304. usb_set_protocol_value;
  305. uint8_t usb_remote_wakeup,usb_idle_rate,usb_clear_remote_wakeup;
  306. uint8_t usb_ep0_stall_status,usb_ep1_stall_status,usb_ep2_stall_status,usb_ep3_stall_status;
  307. uint32_t wakeup_delay =0;
  308. uint32_t nak_clk_now =0;
  309. void usb_init()
  310. {
  311. //uint8_t temp;
  312. HWRITE(CORE_USB_CONFIG,0x00);
  313. delay_us(850);
  314. HWRITE(CORE_USB_TRIG,0xc0);
  315. //usb clock
  316. HWCOR(CORE_CLKOFF + 1, 0x04);
  317. // temp = HREAD(USB_CLOCK);
  318. // temp = temp & 0xFB;
  319. // HWRITE(0x8051,temp);
  320. HWRITE(CORE_USB_CONFIG,0x3c);
  321. //HWRITE(USB_INT_MASK,0x00);
  322. //HWRITE(USB_INT_MASK+1,0x00);
  323. //HWRITE(USB_INT_MASK+2,0xFF);
  324. HWRITE(CORE_USB_STATUS,0xFF);
  325. HWRITE(CORE_USB_FIFO_EMPTY,0xFF);
  326. HWRITE(CORE_USB_ADDR,0x00);
  327. usb_state = USB_STAY_BY;
  328. usb0_state = DEV_DEFAULT;
  329. usb_tx_wait = 0;
  330. usb_set_protocol_status = 0;
  331. usb_get_protocol_flag = 0;
  332. usb_idle_flag = 0;
  333. #ifdef SDK_DEBUG
  334. printf("usb init\r\n");
  335. #endif
  336. }
  337. static void usb_wakeup()
  338. {
  339. if((HREAD(CORE_USB_CONFIG)>>7))
  340. {
  341. HWRITE(CORE_USB_CONFIG,0xfc);
  342. delay_ms(2);
  343. delay_us(500);
  344. HWRITE(CORE_USB_CONFIG,0x3c);
  345. }
  346. else
  347. {
  348. return;
  349. //break;
  350. }
  351. }
  352. static void usb0_reply_zerolen(void)
  353. {
  354. HWRITE(CORE_USB_CLEAR,0X40);
  355. }
  356. static void usb0_forcce_stall(void)
  357. {
  358. HWRITE(CORE_USB_STALL,HREAD(CORE_USB_STALL) |0x01);
  359. usb0_reply_zerolen();
  360. }
  361. static void usb_trig(int ep)
  362. {
  363. HWRITE(CORE_USB_TRIG,1 <<ep) ;
  364. usb_tx_wait |= ( 1 << ep);
  365. }
  366. static void usb0_tx()
  367. {
  368. uint8_t len,i;
  369. if(remain)
  370. {
  371. len= remain > EP0_PACKET_SIZE ? EP0_PACKET_SIZE : remain;
  372. for(i = 0;i<len;i++)
  373. {
  374. HWRITE(CORE_USB_DFIFO(0) , bufptr[i]) ;
  375. delay_us(1);
  376. // #ifdef SDK_DEBUG
  377. // printf(" %x,%d\r\n",bufptr[i],usb0_setup.bValue.c[MSB]);
  378. // #endif
  379. // if(isstr) CORE_USB_EP(0) = 0;
  380. }
  381. bufptr+=len;
  382. usb_trig(0);
  383. }
  384. else
  385. {
  386. return;
  387. }
  388. // bufptr+=len;
  389. remain-=len;
  390. delay_us(50);
  391. // #ifdef SDK_DEBUG
  392. // printf("指针地址= %x,剩余buf大小= %d\r\n",bufptr,remain);
  393. // #endif
  394. if(remain)
  395. {
  396. return;
  397. }
  398. if(usb_state == USB_GOT_REPORT_REQ)
  399. usb_state = USB_CONNECTED;
  400. printf("usb状态机= %d\r\n",usb_state);
  401. }
  402. static void usb0_respend( const uint8_t *buff,uint8_t size)
  403. {
  404. // if(isstr)
  405. // {
  406. // CORE_USB_EP(0) = size-- *2;
  407. // CORE_USB_EP(0) = 3;
  408. // }
  409. if(size > usb0_setup.bLength.i)
  410. {
  411. size = usb0_setup.bLength.i;
  412. }
  413. bufptr = buff;
  414. remain = size;
  415. usb0_tx();
  416. }
  417. void Msdata_normal_mode()
  418. {
  419. uint8_t i;
  420. for(i=0;i<8;i++)
  421. {
  422. HWRITE(CORE_USB_DFIFO(2),ms_data[i]);
  423. }
  424. usb_trig(2);
  425. }
  426. void Msdata_mac_boot_mode()
  427. {
  428. uint8_t i;
  429. if((ms_data[1] == R_KEY) ||(ms_data[1] == LR_KEY))
  430. {
  431. Msdata_normal_mode();
  432. }
  433. else
  434. {
  435. ms_data[0] = ms_data[1];
  436. ms_data[1] = ms_data[2];
  437. ms_data[2] = ms_data[4];
  438. ms_data[3] = ms_data[6];
  439. for(i=0;i<4;i++)
  440. {
  441. HWRITE(CORE_USB_DFIFO(2),ms_data[i]);
  442. }
  443. usb_trig(2);
  444. }
  445. }
  446. void usb_tx_ms_data()
  447. {
  448. if((usb_idle_flag &usb_get_protocol_flag) == 0)
  449. {
  450. usb_ep2_data =0;
  451. Msdata_normal_mode();
  452. return;
  453. }
  454. else if((usb_idle_flag & usb_set_protocol_status) == MAC_BOOT_MODE)
  455. {
  456. usb_ep2_data =0;
  457. Msdata_mac_boot_mode();
  458. return;
  459. }
  460. else
  461. {
  462. usb_ep2_data =0;
  463. Msdata_normal_mode();
  464. }
  465. }
  466. void usb_tx_kb_normal_data()
  467. {
  468. uint8_t i;
  469. usb_ep1_data =0;
  470. for(i=0;i<8;i++)
  471. {
  472. HWRITE(CORE_USB_DFIFO(1),kb_data[i]);
  473. }
  474. usb_trig(1);
  475. }
  476. void usb_tx_kb_multikey_data()
  477. {
  478. uint8_t i;
  479. usb_ep1_data =0;
  480. for(i=0;i<3;i++)
  481. {
  482. HWRITE(CORE_USB_DFIFO(1),kb_multikey[i]);
  483. //kb_multikey[0] = KB_MULTIKEY_REPORT_ID;
  484. }
  485. usb_trig(1);
  486. }
  487. void usb_tx_ep2()
  488. {
  489. if(usb_tx_enable &0x04)
  490. {
  491. if(usb_tx_wait &0x04)
  492. {
  493. return;
  494. }
  495. else
  496. {
  497. usb_tx_enable &= 0xfb;
  498. //ep2_data
  499. if(usb_ep2_data == USB_EP2_MS)
  500. {
  501. usb_tx_ms_data();
  502. }
  503. else if(usb_ep2_data == USB_EP2_MULTIKEY)
  504. {
  505. usb_tx_kb_multikey_data();
  506. }
  507. else
  508. {
  509. while(1);
  510. }
  511. }
  512. }
  513. else
  514. {
  515. return;
  516. }
  517. }
  518. void usb_tx_ep1()
  519. {
  520. if(usb_tx_enable &0x02)
  521. {
  522. if(usb_tx_wait &0x02)
  523. {
  524. return;
  525. }
  526. else
  527. {
  528. usb_tx_enable &= 0xfd;
  529. //ep1_data
  530. if(usb_ep1_data == USB_EP1_KB )
  531. {
  532. usb_tx_kb_normal_data();
  533. }
  534. }
  535. }
  536. else
  537. {
  538. return;
  539. }
  540. }
  541. void usb_tx()
  542. {
  543. if(usb_tx_enable == 0)
  544. {
  545. return;
  546. //break;
  547. }
  548. else if(usb_tx_wait)
  549. {
  550. switch (usb_state)
  551. {
  552. case USB_SLEEP:
  553. usb_tx_wait = 0;
  554. break;
  555. case USB_CONNECTED:
  556. HWRITE(CORE_USB_CONFIG,HREAD(CORE_USB_CONFIG) |0x80);
  557. usb_mac_wake_trig = 1;
  558. break;
  559. default:
  560. break;
  561. }
  562. }
  563. else
  564. {
  565. if(usb_device_enumeration_endflag)
  566. {
  567. if(usb_state == USB_SLEEP)
  568. {
  569. //timer
  570. if(wakeup_delay)
  571. {
  572. if(systick_get_ms() -wakeup_delay > 3000)
  573. {
  574. wakeup_delay =0;
  575. usb_wakeup();
  576. }
  577. }
  578. return;
  579. }
  580. else if(usb_state != USB_CONNECTED )
  581. {
  582. //buf_release
  583. return;
  584. }
  585. else if(usb_clear_remote_wakeup)
  586. {
  587. //buf_release
  588. return;
  589. }
  590. usb_tx_ep1();
  591. usb_tx_ep2();
  592. // #ifdef SDK_DEBUG
  593. // printf(" %d\r\n",usb_state);
  594. // #endif
  595. }
  596. else
  597. {
  598. //buf_release
  599. return;
  600. }
  601. }
  602. }
  603. void Set_Protocol()
  604. {
  605. if(usb0_setup.bLength.c[LSB])
  606. {
  607. usb0_forcce_stall();
  608. }
  609. else
  610. {
  611. switch (usb0_setup.bValue.c[LSB])
  612. {
  613. case BOOT_PROTOCOL:
  614. usb_set_protocol_status = 0;
  615. usb_set_protocol_value = 1;
  616. usb0_reply_zerolen();
  617. break;
  618. case REPORT_PROTOCOL:
  619. usb_set_protocol_status = 1;
  620. usb_set_protocol_value = 0;
  621. usb0_reply_zerolen();
  622. break;
  623. default:
  624. usb0_forcce_stall();
  625. break;
  626. }
  627. }
  628. }
  629. void Get_Protocol()
  630. {
  631. if(usb0_setup.bLength.c[LSB] != 1)
  632. {
  633. usb0_forcce_stall();
  634. return;
  635. }
  636. usb_get_protocol_flag =1;
  637. if(usb_set_protocol_value)
  638. usb0_respend(ZERO_PACKET,1);
  639. else
  640. {
  641. usb0_respend(ONES_PACKET,1);
  642. }
  643. }
  644. void Set_Report()
  645. {
  646. //match code
  647. #ifdef SDK_DEBUG
  648. printf("set_report usb_state = %d\r\n",usb_state);
  649. #endif
  650. usb0_reply_zerolen();
  651. #ifdef SDK_DEBUG
  652. printf("set_report usb_state = %d\r\n",usb_state);
  653. #endif
  654. }
  655. void Get_Report()
  656. {
  657. if(usb0_setup.bValue.i != PC_GET_REPORT)
  658. usb0_forcce_stall();
  659. else
  660. {
  661. while(1);
  662. //match code
  663. }
  664. }
  665. void Clear_Feature()
  666. {
  667. if((usb0_state != DEV_CONFIGURED) ||
  668. usb0_setup.bLength.c[MSB] ||usb0_setup.bLength.c[LSB] ||
  669. usb0_setup.bValue.c[MSB] ||usb0_setup.wIndex.c[MSB])
  670. {
  671. usb0_forcce_stall();
  672. }
  673. else
  674. {
  675. switch (usb0_setup.bmRequestType)
  676. {
  677. case OUT_DEVICE:
  678. if(usb0_setup.bValue.c[LSB] != DEVICE_REMOTE_WAKEUP)
  679. {
  680. usb0_forcce_stall();
  681. }
  682. else
  683. {
  684. HWRITE(CORE_USB_CONFIG,HREAD(CORE_USB_CONFIG) &0x7f);
  685. usb_remote_wakeup = 0;
  686. usb_clear_remote_wakeup= 1;
  687. usb0_reply_zerolen();
  688. }
  689. break;
  690. case OUT_INTERFACE:
  691. usb0_forcce_stall();
  692. break;
  693. case OUT_ENDPOINT:
  694. if(usb0_setup.bValue.c[LSB] != ENDPOINT_HALT )
  695. usb0_forcce_stall();
  696. else
  697. {
  698. if(usb0_setup.wIndex.c[LSB] == IN_EP1 )
  699. {
  700. usb_ep1_stall_status = 0;
  701. usb_clear_halt = 1;
  702. usb0_reply_zerolen();
  703. }
  704. else if(usb0_setup.wIndex.c[LSB] == IN_EP2)
  705. {
  706. usb_ep2_stall_status =0;
  707. usb_clear_halt = 1;
  708. usb0_reply_zerolen();
  709. }
  710. else if(usb0_setup.wIndex.c[LSB] == IN_EP3)
  711. {
  712. usb_ep3_stall_status =0;
  713. usb_clear_halt = 1;
  714. usb0_reply_zerolen();
  715. }
  716. else if((usb0_setup.wIndex.c[LSB] & 0x7f)== 0x00)
  717. {
  718. usb_ep0_stall_status =0;
  719. usb0_reply_zerolen();
  720. }
  721. else
  722. {
  723. usb0_forcce_stall();
  724. }
  725. }
  726. break;
  727. default:
  728. usb0_forcce_stall();
  729. break;
  730. }
  731. }
  732. }
  733. void Get_Idle()
  734. {
  735. usb_idle_flag = 1;
  736. usb0_respend(( uint8_t * )&usb_idle_rate,1);
  737. }
  738. void Set_Idle()
  739. {
  740. usb_idle_flag =1;
  741. usb_idle_rate = usb0_setup.bValue.c[MSB];
  742. if(usb0_setup.wIndex.c[LSB] == 1)
  743. {
  744. usb_device_enumeration_endflag =1;
  745. usb_clear_remote_wakeup =0;
  746. usb_state = USB_RESUME;
  747. }
  748. if(usb_state != USB_RESUME )
  749. {
  750. usb0_reply_zerolen();
  751. return;
  752. }
  753. usb_state = USB_CONNECTED;
  754. usb0_reply_zerolen();
  755. delay_ms(5);
  756. }
  757. void Set_Feature()
  758. {
  759. if(usb0_setup.bLength.c[MSB] || usb0_setup.bLength.c[LSB])
  760. {
  761. usb0_forcce_stall();
  762. }
  763. else
  764. {
  765. switch(usb0_setup.bmRequestType)
  766. {
  767. case OUT_DEVICE:
  768. if(usb0_setup.bValue.c[LSB] != DEVICE_REMOTE_WAKEUP)
  769. {
  770. usb0_forcce_stall();
  771. }
  772. else
  773. {
  774. HWRITE(CORE_USB_CONFIG,HREAD(CORE_USB_CONFIG) |0x80);
  775. usb_remote_wakeup = 1;
  776. usb_state =USB_SLEEP;
  777. // usb_tx_win_enable = 0;
  778. usb0_reply_zerolen();
  779. //wake up timer
  780. wakeup_delay =systick_get_ms();
  781. }
  782. break;
  783. case OUT_INTERFACE:
  784. usb0_forcce_stall();
  785. break;
  786. case OUT_ENDPOINT:
  787. if((usb0_setup.bValue.c[LSB] != ENDPOINT_HALT ) ||usb0_setup.bLength.c[MSB])
  788. usb0_forcce_stall();
  789. else
  790. {
  791. if(usb0_setup.wIndex.c[LSB] == IN_EP1 )
  792. {
  793. usb_ep1_stall_status = 1;
  794. usb0_reply_zerolen();
  795. }
  796. else if(usb0_setup.wIndex.c[LSB] == IN_EP2)
  797. {
  798. usb_ep2_stall_status =1;
  799. usb0_reply_zerolen();
  800. }
  801. else if(usb0_setup.wIndex.c[LSB] == IN_EP3)
  802. {
  803. usb_ep3_stall_status =1;
  804. usb0_reply_zerolen();
  805. }
  806. else if((usb0_setup.wIndex.c[LSB] & 0x7f)== 0x00)
  807. {
  808. usb_ep0_stall_status =1;
  809. usb0_reply_zerolen();
  810. }
  811. else
  812. {
  813. usb0_forcce_stall();
  814. }
  815. }
  816. break;
  817. default:
  818. usb0_forcce_stall();
  819. break;
  820. }
  821. }
  822. }
  823. void set_wait_address()
  824. {
  825. HWRITE(CORE_USB_ADDR,usb0_setup.bValue.c[LSB]);
  826. usb0_reply_zerolen();
  827. usb_set_high_addr_flag = 1;
  828. }
  829. void Set_Address()
  830. {
  831. if((usb0_setup.bmRequestType != OUT_DEVICE) ||
  832. usb0_setup.bValue.c[MSB] ||usb0_setup.wIndex.c[MSB] ||
  833. usb0_setup.wIndex.c[LSB] ||usb0_setup.bLength.c[MSB] ||
  834. usb0_setup.bLength.c[LSB] ||(usb0_setup.bValue.c[LSB] & 0x80))
  835. {
  836. usb0_forcce_stall();
  837. }
  838. else
  839. {
  840. if(usb0_setup.bValue.c[LSB])
  841. {
  842. usb0_state = DEV_ADDRESS;
  843. set_wait_address();
  844. }
  845. else
  846. {
  847. usb0_state = DEV_DEFAULT;
  848. set_wait_address();
  849. }
  850. }
  851. }
  852. void Set_Interface()
  853. {
  854. if((usb0_state != DEV_CONFIGURED) ||
  855. (usb0_setup.bmRequestType != OUT_INTERFACE) ||
  856. usb0_setup.bValue.c[MSB] ||usb0_setup.wIndex.c[MSB] ||
  857. usb0_setup.wIndex.c[LSB] ||usb0_setup.bLength.c[MSB] ||
  858. usb0_setup.bLength.c[LSB] ||usb0_setup.bValue.c[LSB])
  859. {
  860. usb0_forcce_stall();
  861. }
  862. else
  863. {
  864. usb0_reply_zerolen();
  865. }
  866. }
  867. void Set_Descriptor()
  868. {
  869. usb0_forcce_stall();
  870. }
  871. void set_Wait_Status()
  872. {
  873. if(usb0_setup.bValue.c[LSB])
  874. {
  875. usb0_state = DEV_CONFIGURED;
  876. usb0_reply_zerolen();
  877. }
  878. else
  879. {
  880. usb0_state = DEV_ADDRESS;
  881. usb0_reply_zerolen();
  882. }
  883. }
  884. void Set_Configuration()
  885. {
  886. if((usb0_setup.bmRequestType != OUT_DEVICE) ||(usb0_state == DEV_DEFAULT)||
  887. usb0_setup.bLength.c[MSB] ||usb0_setup.bLength.c[LSB] ||
  888. usb0_setup.bValue.c[MSB] ||usb0_setup.wIndex.c[MSB] ||
  889. usb0_setup.wIndex.c[LSB] ||(usb0_setup.bValue.c[LSB] > USB_MAX_NUM_CFG) )
  890. {
  891. usb0_forcce_stall();
  892. }
  893. else
  894. {
  895. switch (usb0_state)
  896. {
  897. case DEV_ADDRESS:
  898. set_Wait_Status ( );
  899. break;
  900. case DEV_CONFIGURED:
  901. set_Wait_Status ( );
  902. break;
  903. default:
  904. usb0_forcce_stall();
  905. break;
  906. }
  907. }
  908. }
  909. void Get_Configuration(void)
  910. {
  911. if((usb0_setup.bmRequestType != IN_DEVICE) ||
  912. usb0_setup.bValue.c[MSB] ||usb0_setup.bValue.c[LSB] ||
  913. usb0_setup.wIndex.c[MSB] ||usb0_setup.wIndex.c[LSB] ||
  914. usb0_setup.bLength.c[MSB] ||(usb0_setup.bLength.c[LSB] != 1))
  915. {
  916. usb0_forcce_stall();
  917. }
  918. else
  919. {
  920. switch (usb0_state)
  921. {
  922. case DEV_CONFIGURED:
  923. usb0_respend(ONES_PACKET,1);
  924. break;
  925. case DEV_ADDRESS:
  926. usb0_respend(ZERO_PACKET,1);
  927. break;
  928. default:
  929. usb0_forcce_stall();
  930. break;
  931. }
  932. }
  933. }
  934. void Get_Descriptor()
  935. {
  936. uint8_t index;
  937. // isstr = 0;
  938. switch(usb0_setup.bValue.c[MSB])
  939. {
  940. case DSC_DEVICE:
  941. if(usb_set_high_addr_flag)
  942. {
  943. HWRITE(CORE_USB_ADDR,HREAD(CORE_USB_ADDR) |(1<<7));
  944. usb_set_high_addr_flag = 0;
  945. }
  946. usb0_respend(( uint8_t *)&devicedesc,sizeof(devicedesc));
  947. break;
  948. case DSC_CONFIG:
  949. usb0_respend(confdesc,sizeof(confdesc));
  950. break;
  951. case DSC_STRING:
  952. if(usb0_setup.bValue.c[LSB] >3)
  953. {
  954. usb0_forcce_stall();
  955. break;
  956. }
  957. index = usb0_setup.bValue.c[LSB] & 3;
  958. // isstr = index > 0;
  959. usb0_respend(string_table[index],string_table[index][0]);
  960. break;
  961. case DSC_HID:
  962. usb_get_protocol_flag = 0;
  963. if(usb0_setup.wIndex.c[LSB])
  964. usb0_respend(confdesc + 18,confdesc[18]);
  965. else if(usb0_setup.wIndex.c[LSB] == 1)
  966. usb0_respend(confdesc + 43,confdesc[43]);
  967. else
  968. usb0_forcce_stall();
  969. break;
  970. case DSC_HID_REPORT:
  971. if(usb0_setup.wIndex.i == 1)
  972. {
  973. usb_device_enumeration_endflag = 1;
  974. usb_state = USB_GOT_REPORT_REQ;
  975. usb0_respend(hidreportdesc_m,sizeof(hidreportdesc_m));
  976. #ifdef SDK_DEBUG
  977. printf("usb 枚举结束\r\n");
  978. #endif
  979. }
  980. else
  981. {
  982. usb0_respend(hidreportdesc_kb,sizeof(hidreportdesc_kb));
  983. }
  984. break;
  985. default:
  986. usb0_forcce_stall();
  987. break;
  988. }
  989. }
  990. void Get_Interface()
  991. {
  992. if((usb0_state != DEV_CONFIGURED) ||
  993. (usb0_setup.bmRequestType != IN_INTERFACE) ||
  994. usb0_setup.bLength.c[MSB] ||(usb0_setup.bLength.c[LSB] != 1)||
  995. usb0_setup.bValue.c[MSB] ||usb0_setup.bValue.c[LSB] ||
  996. usb0_setup.wIndex.c[MSB] ||usb0_setup.wIndex.c[LSB])
  997. {
  998. usb0_forcce_stall();
  999. }
  1000. else
  1001. {
  1002. usb0_respend(ZERO_PACKET,1);
  1003. }
  1004. }
  1005. void Get_Status()
  1006. {
  1007. if((usb_state == DEV_DEFAULT) ||usb0_setup.bValue.c[MSB] ||
  1008. usb0_setup.bValue.c[LSB] ||usb0_setup.bLength.c[MSB] ||
  1009. (usb0_setup.bLength.c[LSB] != 2) ||usb0_setup.wIndex.c[MSB])
  1010. {
  1011. usb0_forcce_stall();
  1012. }
  1013. else
  1014. {
  1015. switch (usb0_setup.bmRequestType)
  1016. {
  1017. case IN_DEVICE:
  1018. if(usb0_setup.wIndex.c[LSB])
  1019. {
  1020. usb0_forcce_stall();
  1021. return;
  1022. }
  1023. else if(usb_remote_wakeup == 1)
  1024. usb0_respend(TWO_PACKET,2);
  1025. else
  1026. {
  1027. usb0_respend(ZERO_PACKET,2);
  1028. }
  1029. break;
  1030. case IN_INTERFACE:
  1031. if(usb0_state != DEV_CONFIGURED )
  1032. usb0_forcce_stall();
  1033. else
  1034. {
  1035. usb0_respend(ZERO_PACKET,2);
  1036. }
  1037. break;
  1038. case IN_ENDPOINT:
  1039. if(usb0_state != DEV_CONFIGURED )
  1040. usb0_forcce_stall();
  1041. else
  1042. {
  1043. if(usb0_setup.wIndex.c[LSB] == IN_EP1 )
  1044. {
  1045. if(usb_ep1_stall_status)
  1046. usb0_respend(ONES_PACKET,2);
  1047. else
  1048. {
  1049. usb0_respend(ZERO_PACKET,2);
  1050. }
  1051. }
  1052. else if(usb0_setup.wIndex.c[LSB] == IN_EP2)
  1053. {
  1054. if(usb_ep2_stall_status)
  1055. usb0_respend(ONES_PACKET,2);
  1056. else
  1057. {
  1058. usb0_respend(ZERO_PACKET,2);
  1059. }
  1060. }
  1061. else if(usb0_setup.wIndex.c[LSB] == IN_EP3)
  1062. {
  1063. if(usb_ep3_stall_status)
  1064. usb0_respend(ONES_PACKET,2);
  1065. else
  1066. {
  1067. usb0_respend(ZERO_PACKET,2);
  1068. }
  1069. }
  1070. else if((usb0_setup.wIndex.c[LSB] & 0x7f)== 0x00)
  1071. {
  1072. if(usb_ep0_stall_status)
  1073. usb0_respend(ONES_PACKET,2);
  1074. else
  1075. {
  1076. usb0_respend(ZERO_PACKET,2);
  1077. }
  1078. }
  1079. else
  1080. {
  1081. usb0_forcce_stall();
  1082. }
  1083. }
  1084. break;
  1085. default:
  1086. usb0_forcce_stall();
  1087. break;
  1088. }
  1089. }
  1090. }
  1091. void usb_in_class_req()
  1092. {
  1093. switch (usb0_setup.bRequest)
  1094. {
  1095. case GET_IDLE:
  1096. Get_Idle();
  1097. break;
  1098. case GET_REPORT:
  1099. Get_Report();
  1100. break;
  1101. case GET_PROTOCOL:
  1102. Get_Protocol();
  1103. break;
  1104. default:
  1105. usb0_forcce_stall();
  1106. break;
  1107. }
  1108. }
  1109. void usb_in_standard_req()
  1110. {
  1111. switch(usb0_setup.bRequest)
  1112. {
  1113. case GET_CONFIGURATION:
  1114. Get_Configuration();
  1115. break;
  1116. case GET_DESCRIPTOR:
  1117. Get_Descriptor();
  1118. break;
  1119. case GET_INTERFACE:
  1120. Get_Interface();
  1121. break;
  1122. case GET_STATUS:
  1123. Get_Status();
  1124. break;
  1125. default:
  1126. usb0_forcce_stall();
  1127. break;
  1128. }
  1129. }
  1130. void usb_out_standard_req()
  1131. {
  1132. switch (usb0_setup.bRequest)
  1133. {
  1134. case CLEAR_FEATURE:
  1135. Clear_Feature();
  1136. break;
  1137. case SET_FEATURE:
  1138. Set_Feature();
  1139. break;
  1140. case SET_ADDRESS:
  1141. Set_Address();
  1142. break;
  1143. case SET_CONFIGURATION:
  1144. Set_Configuration();
  1145. break;
  1146. case SET_INTERFACE:
  1147. Set_Interface();
  1148. break;
  1149. case SET_DESCRIPTOR:
  1150. Set_Descriptor();
  1151. break;
  1152. default:
  1153. usb0_forcce_stall();
  1154. break;
  1155. }
  1156. }
  1157. void usb_out_class_req()
  1158. {
  1159. switch(usb0_setup.bRequest)
  1160. {
  1161. case SET_IDLE:
  1162. Set_Idle();
  1163. break;
  1164. case SET_REPORT:
  1165. Set_Report();
  1166. break;
  1167. case SET_PROTOCOL:
  1168. Set_Protocol();
  1169. break;
  1170. default:
  1171. usb0_forcce_stall();
  1172. break;
  1173. }
  1174. }
  1175. void usb_out_device()
  1176. {
  1177. if(((usb0_setup.bmRequestType >> 5) & 0x03 )== STANDARD_REQ)
  1178. {
  1179. usb_out_standard_req();
  1180. }
  1181. else if (((usb0_setup.bmRequestType >> 5) & 0x03 )== CLASS_REQ)
  1182. {
  1183. usb_out_class_req();
  1184. }
  1185. else
  1186. {
  1187. while(1);
  1188. }
  1189. }
  1190. void usb_in_device()
  1191. {
  1192. if(((usb0_setup.bmRequestType >> 5) & 0x03) == STANDARD_REQ)
  1193. {
  1194. usb_in_standard_req();
  1195. }
  1196. else if (((usb0_setup.bmRequestType >> 5) & 0x03) == CLASS_REQ)
  1197. {
  1198. usb_in_class_req();
  1199. }
  1200. else
  1201. {
  1202. while(1);
  1203. }
  1204. }
  1205. void usb_class_type()
  1206. {
  1207. if((usb0_setup.bmRequestType & 0x80) == IN_DEVICE)
  1208. {
  1209. usb_in_device();
  1210. }
  1211. else if ((usb0_setup.bmRequestType & 0x80) == OUT_DEVICE)
  1212. {
  1213. usb_out_device();
  1214. }
  1215. else
  1216. {
  1217. while(1);
  1218. }
  1219. }
  1220. void usb_nak_state_judge()
  1221. {
  1222. if(usb_state != USB_CONNECTED)
  1223. {
  1224. if(usb_clear_halt)
  1225. {
  1226. usb_clear_halt = 0;
  1227. usb_state = USB_CONNECTED;
  1228. usb_device_enumeration_endflag = 1;
  1229. }
  1230. }
  1231. else
  1232. {
  1233. usb_wakestate_onetime_flag = 0;
  1234. usb_mac_wake_trig = 0;
  1235. }
  1236. }
  1237. void USB_IRQHandler()
  1238. {
  1239. uint8_t i,len,empty,status;
  1240. uint8_t * buf = (uint8_t *) &usb0_setup;
  1241. if(HREAD(CORE_USB_STATUS) &(1<<7))
  1242. {
  1243. usb_init();
  1244. }
  1245. //nak_state_judge
  1246. status = HREAD(CORE_USB_STATUS);
  1247. empty = HREAD(CORE_USB_FIFO_EMPTY);
  1248. if(status &0x40)
  1249. {
  1250. usb_nak_state_judge();
  1251. nak_clk_now = systick_get_ms();
  1252. }
  1253. usb_tx_wait &= ~ HREAD(CORE_USB_FIFO_EMPTY);
  1254. // #ifdef SDK_DEBUG
  1255. // printf("%x \r\n",usb_tx_wait);
  1256. // #endif
  1257. HWRITE(CORE_USB_STATUS,0xe0);
  1258. // HWRITE(0x8203,1);
  1259. if(empty & 0x01)
  1260. {
  1261. HWRITE(CORE_USB_FIFO_EMPTY,0x1);
  1262. usb0_tx();
  1263. }
  1264. if(status & 1)
  1265. {
  1266. HWRITE(CORE_USB_STATUS,0x11);
  1267. len = HREAD(CORE_USB_EP_LEN);
  1268. if(len <= 7)
  1269. {
  1270. return;
  1271. }
  1272. for (i=0;i<len;i++)
  1273. {
  1274. buf[i] = HREAD(CORE_USB_EP) ;
  1275. // #ifdef SDK_DEBUG
  1276. // printf(" setup 地址=%x \r\n",&(buf[i]));
  1277. // #endif
  1278. }
  1279. // if(len <= 7)
  1280. // {
  1281. // return;
  1282. // }
  1283. if(status & USB_STATUS_SETUP )
  1284. {
  1285. // if((usb0_setup.bmRequestType & ~ 0x80) ==HCI_CLASS_REQUEST_TYPE)
  1286. // {
  1287. // usb0_reply_zerolen();
  1288. // }
  1289. // else
  1290. // {
  1291. usb_class_type();
  1292. // }
  1293. }
  1294. }
  1295. usb_tx();
  1296. if (systick_get_ms() -nak_clk_now > 400)
  1297. {
  1298. if(usb_wakestate_onetime_flag )
  1299. {
  1300. return;
  1301. }
  1302. else if (usb_device_enumeration_endflag)
  1303. {
  1304. switch (usb_state)
  1305. {
  1306. case USB_SLEEP:
  1307. break;
  1308. case USB_CONNECTED:
  1309. if(usb_mac_wake_trig)
  1310. {
  1311. usb_wakestate_onetime_flag =1;
  1312. usb_wakeup();
  1313. }
  1314. break;
  1315. default:
  1316. break;
  1317. }
  1318. }
  1319. }
  1320. }