Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

USBHAL_RZ_A1H.cpp 51KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497
  1. /* Copyright (c) 2010-2011 mbed.org, MIT License
  2. *
  3. * Permission is hereby granted, free of charge, to any person
  4. * obtaining a copy of this software and associated documentation
  5. * files (the "Software"), to deal in the Software without
  6. * restriction, including without limitation the rights to use,
  7. * copy, modify, merge, publish, distribute, sublicense, and/or
  8. * sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following
  10. * conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the
  14. * Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
  17. * KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  18. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  19. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  20. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  23. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. #if defined(TARGET_RZ_A1H)
  26. /*
  27. This class can use the pipe1, pipe3 and pipe6 only. You should
  28. re-program this class if you wanted to use other pipe.
  29. */
  30. /*************************************************************************/
  31. extern "C"
  32. {
  33. #include "r_typedefs.h"
  34. #include "iodefine.h"
  35. }
  36. #include "USBHAL.h"
  37. #include "devdrv_usb_function_api.h"
  38. #include "usb_iobitmask.h"
  39. #include "rza_io_regrw.h"
  40. #include "USBDevice_Types.h"
  41. #include "usb_function_setting.h"
  42. /*************************************************************************/
  43. /* constants */
  44. const struct PIPECFGREC {
  45. uint16_t endpoint;
  46. uint16_t pipesel;
  47. uint16_t pipecfg;
  48. uint16_t pipebuf;
  49. uint16_t pipemaxp;
  50. uint16_t pipeperi;
  51. } def_pipecfg[] = {
  52. /*EP0OUT and EP0IN are configured by USB IP*/
  53. {
  54. EP1OUT, /*EP1: Host -> Func, INT*/
  55. 6 | USB_FUNCTION_D0FIFO_USE,
  56. USB_FUNCTION_INTERRUPT |
  57. USB_FUNCTION_BFREOFF |
  58. USB_FUNCTION_DBLBOFF |
  59. USB_FUNCTION_CNTMDON |
  60. USB_FUNCTION_SHTNAKOFF |
  61. USB_FUNCTION_DIR_P_OUT |
  62. USB_FUNCTION_EP1,
  63. ( ( ( 64) / 64 - 1 ) << 10 ) | 0x04u,
  64. MAX_PACKET_SIZE_EP1,
  65. DEVDRV_USBF_OFF |
  66. ( 3 << USB_PIPEPERI_IITV_SHIFT ),
  67. },
  68. {
  69. EP1IN, /*EP1: Host <- Func, INT*/
  70. 7 | USB_FUNCTION_D1FIFO_USE,
  71. USB_FUNCTION_INTERRUPT |
  72. USB_FUNCTION_BFREOFF |
  73. USB_FUNCTION_DBLBOFF |
  74. USB_FUNCTION_CNTMDOFF |
  75. USB_FUNCTION_SHTNAKOFF |
  76. USB_FUNCTION_DIR_P_IN |
  77. USB_FUNCTION_EP1,
  78. ( ( ( 64) / 64 - 1 ) << 10 ) | 0x05u,
  79. MAX_PACKET_SIZE_EP1,
  80. DEVDRV_USBF_OFF |
  81. ( 3 << USB_PIPEPERI_IITV_SHIFT ),
  82. },
  83. {
  84. EP2OUT, /*EP2: Host -> Func, BULK*/
  85. 3 | USB_FUNCTION_D0FIFO_USE,
  86. USB_FUNCTION_BULK |
  87. USB_FUNCTION_BFREOFF |
  88. USB_FUNCTION_DBLBON |
  89. USB_FUNCTION_CNTMDON |
  90. USB_FUNCTION_SHTNAKON |
  91. USB_FUNCTION_DIR_P_OUT |
  92. USB_FUNCTION_EP2,
  93. ( ( (2048) / 64 - 1 ) << 10 ) | 0x30u,
  94. MAX_PACKET_SIZE_EP2,
  95. DEVDRV_USBF_OFF |
  96. ( 0 << USB_PIPEPERI_IITV_SHIFT ),
  97. },
  98. {
  99. EP2IN, /*EP2: Host <- Func, BULK*/
  100. 4 | USB_FUNCTION_D1FIFO_USE,
  101. USB_FUNCTION_BULK |
  102. USB_FUNCTION_BFREOFF |
  103. USB_FUNCTION_DBLBOFF |
  104. USB_FUNCTION_CNTMDON |
  105. USB_FUNCTION_SHTNAKOFF |
  106. USB_FUNCTION_DIR_P_IN |
  107. USB_FUNCTION_EP2,
  108. ( ( (2048) / 64 - 1 ) << 10 ) | 0x50u,
  109. MAX_PACKET_SIZE_EP2,
  110. DEVDRV_USBF_OFF |
  111. ( 0 << USB_PIPEPERI_IITV_SHIFT ),
  112. },
  113. {
  114. EP3OUT, /*EP3: Host -> Func, ISO*/
  115. 1 | USB_FUNCTION_D0FIFO_USE,
  116. USB_FUNCTION_ISO |
  117. USB_FUNCTION_BFREOFF |
  118. USB_FUNCTION_DBLBON |
  119. USB_FUNCTION_CNTMDOFF |
  120. USB_FUNCTION_SHTNAKON |
  121. USB_FUNCTION_DIR_P_OUT |
  122. USB_FUNCTION_EP3,
  123. ( ( ( 512) / 64 - 1 ) << 10 ) | 0x10u,
  124. MAX_PACKET_SIZE_EP3,
  125. DEVDRV_USBF_OFF |
  126. ( 0 << USB_PIPEPERI_IITV_SHIFT ),
  127. },
  128. {
  129. EP3IN, /*EP3: Host <- Func, ISO*/
  130. 2 | USB_FUNCTION_D1FIFO_USE,
  131. USB_FUNCTION_ISO |
  132. USB_FUNCTION_BFREOFF |
  133. USB_FUNCTION_DBLBON |
  134. USB_FUNCTION_CNTMDOFF |
  135. USB_FUNCTION_SHTNAKOFF |
  136. USB_FUNCTION_DIR_P_IN |
  137. USB_FUNCTION_EP3,
  138. ( ( ( 512) / 64 - 1 ) << 10 ) | 0x20u,
  139. MAX_PACKET_SIZE_EP3,
  140. DEVDRV_USBF_OFF |
  141. ( 0 << USB_PIPEPERI_IITV_SHIFT ),
  142. },
  143. { /*terminator*/
  144. 0, 0, 0, 0, 0,
  145. },
  146. };
  147. /*************************************************************************/
  148. /* workareas */
  149. USBHAL * USBHAL::instance;
  150. static IRQn_Type int_id; /* interrupt ID */
  151. static uint16_t int_level; /* initerrupt level */
  152. static uint16_t clock_mode; /* input clock selector */
  153. static uint16_t mode; /* USB speed (HIGH/FULL) */
  154. //static DigitalOut *usbx_en;
  155. static uint16_t EP0_read_status;
  156. static uint16_t EPx_read_status;
  157. static uint16_t setup_buffer[MAX_PACKET_SIZE_EP0 / 2];
  158. /* 0: not used / other: a pipe number to use recv_buffer*/
  159. static uint8_t recv_buffer[MAX_PACKET_SIZE_EPBULK];
  160. volatile static uint16_t recv_error;
  161. /*************************************************************************/
  162. /* prototypes for C */
  163. extern "C" {
  164. void usbx_function_BRDYInterruptPIPE0 (uint16_t status, uint16_t intenb,
  165. USBHAL *object, void (USBHAL::*EP0func)(void));
  166. void usbx_function_BRDYInterrupt (uint16_t status, uint16_t intenb,
  167. USBHAL *object, bool (USBHAL::*epCallback[])(void));
  168. void usbx_function_NRDYInterruptPIPE0(uint16_t status, uint16_t intenb,
  169. USBHAL *object, void (USBHAL::*EP0func)(void));
  170. void usbx_function_NRDYInterrupt (uint16_t status, uint16_t intenb,
  171. USBHAL *object, bool (USBHAL::*epCallback[])(void));
  172. void usbx_function_BEMPInterruptPIPE0(uint16_t status, uint16_t intenb,
  173. USBHAL *object, void (USBHAL::*EP0func)(void));
  174. void usbx_function_BEMPInterrupt (uint16_t status, uint16_t intenb,
  175. USBHAL *object, bool (USBHAL::*epCallback[])(void));
  176. }
  177. /*************************************************************************/
  178. /* macros */
  179. /******************************************************************************
  180. * Function Name: usbx_function_BRDYInterruptPIPE0
  181. * Description : Executes BRDY interrupt for pipe0.
  182. * Arguments : uint16_t status ; BRDYSTS Register Value
  183. * : uint16_t intenb ; BRDYENB Register Value
  184. * Return Value : none
  185. *****************************************************************************/
  186. extern "C" {
  187. void usbx_function_BRDYInterruptPIPE0 (
  188. uint16_t status,
  189. uint16_t intenb,
  190. USBHAL *object,
  191. void (USBHAL::*EP0func)(void)
  192. )
  193. {
  194. volatile uint16_t dumy_sts;
  195. uint16_t read_status;
  196. USB20X.BRDYSTS =
  197. (uint16_t)~g_usbx_function_bit_set[USB_FUNCTION_PIPE0];
  198. RZA_IO_RegWrite_16(
  199. &USB20X.CFIFOSEL, USB_FUNCTION_PIPE0,
  200. USB_CFIFOSEL_CURPIPE_SHIFT, USB_CFIFOSEL_CURPIPE);
  201. g_usbx_function_PipeDataSize[USB_FUNCTION_PIPE0] =
  202. g_usbx_function_data_count[USB_FUNCTION_PIPE0];
  203. read_status = usbx_function_read_buffer_c(USB_FUNCTION_PIPE0);
  204. g_usbx_function_PipeDataSize[USB_FUNCTION_PIPE0] -=
  205. g_usbx_function_data_count[USB_FUNCTION_PIPE0];
  206. switch (read_status) {
  207. case USB_FUNCTION_READING: /* Continue of data read */
  208. case USB_FUNCTION_READEND: /* End of data read */
  209. /* PID = BUF */
  210. usbx_function_set_pid_buf(USB_FUNCTION_PIPE0);
  211. /*callback*/
  212. (object->*EP0func)();
  213. break;
  214. case USB_FUNCTION_READSHRT: /* End of data read */
  215. usbx_function_disable_brdy_int(USB_FUNCTION_PIPE0);
  216. /* PID = BUF */
  217. usbx_function_set_pid_buf(USB_FUNCTION_PIPE0);
  218. /*callback*/
  219. (object->*EP0func)();
  220. break;
  221. case USB_FUNCTION_READOVER: /* FIFO access error */
  222. /* Buffer Clear */
  223. USB20X.CFIFOCTR = USB_FUNCTION_BITBCLR;
  224. usbx_function_disable_brdy_int(USB_FUNCTION_PIPE0);
  225. /* Req Error */
  226. usbx_function_set_pid_stall(USB_FUNCTION_PIPE0);
  227. /*callback*/
  228. (object->*EP0func)();
  229. break;
  230. case DEVDRV_USBF_FIFOERROR: /* FIFO access error */
  231. default:
  232. usbx_function_disable_brdy_int(USB_FUNCTION_PIPE0);
  233. /* Req Error */
  234. usbx_function_set_pid_stall(USB_FUNCTION_PIPE0);
  235. break;
  236. }
  237. /* Three dummy reads for clearing interrupt requests */
  238. dumy_sts = USB20X.BRDYSTS;
  239. }
  240. }
  241. /******************************************************************************
  242. * Function Name: usbx_function_BRDYInterrupt
  243. * Description : Executes BRDY interrupt uxclude pipe0.
  244. * Arguments : uint16_t status ; BRDYSTS Register Value
  245. * : uint16_t intenb ; BRDYENB Register Value
  246. * Return Value : none
  247. *****************************************************************************/
  248. extern "C" {
  249. void usbx_function_BRDYInterrupt(
  250. uint16_t status,
  251. uint16_t intenb,
  252. USBHAL *object,
  253. bool (USBHAL::*epCallback[])(void)
  254. )
  255. {
  256. volatile uint16_t dumy_sts;
  257. /**************************************************************
  258. * Function Name: usbx_function_brdy_int
  259. * Description : Executes BRDY interrupt(USB_FUNCTION_PIPE1-9).
  260. * : According to the pipe that interrupt is generated in,
  261. * : reads/writes buffer allocated in the pipe.
  262. * : This function is executed in the BRDY
  263. * : interrupt handler. This function
  264. * : clears BRDY interrupt status and BEMP
  265. * : interrupt status.
  266. * Arguments : uint16_t Status ; BRDYSTS Register Value
  267. * : uint16_t Int_enbl ; BRDYENB Register Value
  268. * Return Value : none
  269. *************************************************************/
  270. /* copied from usbx_function_intrn.c */
  271. uint32_t int_sense = 0;
  272. uint16_t pipe;
  273. uint16_t pipebit;
  274. uint16_t ep;
  275. for (pipe = USB_FUNCTION_PIPE1; pipe <= USB_FUNCTION_MAX_PIPE_NO; pipe++) {
  276. pipebit = g_usbx_function_bit_set[pipe];
  277. if ((status & pipebit) && (intenb & pipebit)) {
  278. USB20X.BRDYSTS = (uint16_t)~pipebit;
  279. USB20X.BEMPSTS = (uint16_t)~pipebit;
  280. switch (g_usbx_function_PipeTbl[pipe] & USB_FUNCTION_FIFO_USE) {
  281. case USB_FUNCTION_D0FIFO_DMA:
  282. if (g_usbx_function_DmaStatus[USB_FUNCTION_D0FIFO] != USB_FUNCTION_DMA_READY) {
  283. /*now, DMA is not supported*/
  284. usbx_function_dma_interrupt_d0fifo(int_sense);
  285. }
  286. if (RZA_IO_RegRead_16(
  287. &g_usbx_function_pipecfg[pipe], USB_PIPECFG_BFRE_SHIFT, USB_PIPECFG_BFRE) == 0) {
  288. /*now, DMA is not supported*/
  289. usbx_function_read_dma(pipe);
  290. usbx_function_disable_brdy_int(pipe);
  291. } else {
  292. USB20X.D0FIFOCTR = USB_FUNCTION_BITBCLR;
  293. g_usbx_function_pipe_status[pipe] = DEVDRV_USBF_PIPE_DONE;
  294. }
  295. break;
  296. case USB_FUNCTION_D1FIFO_DMA:
  297. if (g_usbx_function_DmaStatus[USB_FUNCTION_D1FIFO] != USB_FUNCTION_DMA_READY) {
  298. /*now, DMA is not supported*/
  299. usbx_function_dma_interrupt_d1fifo(int_sense);
  300. }
  301. if (RZA_IO_RegRead_16(
  302. &g_usbx_function_pipecfg[pipe], USB_PIPECFG_BFRE_SHIFT, USB_PIPECFG_BFRE) == 0) {
  303. /*now, DMA is not supported*/
  304. usbx_function_read_dma(pipe);
  305. usbx_function_disable_brdy_int(pipe);
  306. } else {
  307. USB20X.D1FIFOCTR = USB_FUNCTION_BITBCLR;
  308. g_usbx_function_pipe_status[pipe] = DEVDRV_USBF_PIPE_DONE;
  309. }
  310. break;
  311. default:
  312. ep = (g_usbx_function_pipecfg[pipe] & USB_PIPECFG_EPNUM) >> USB_PIPECFG_EPNUM_SHIFT;
  313. ep <<= 1;
  314. if (RZA_IO_RegRead_16(
  315. &g_usbx_function_pipecfg[pipe], USB_PIPECFG_DIR_SHIFT, USB_PIPECFG_DIR) == 0) {
  316. /* read */
  317. EPx_read_status = DEVDRV_USBF_PIPE_WAIT;
  318. (object->*(epCallback[ep - 2])) ();
  319. EPx_read_status = DEVDRV_USBF_PIPE_DONE;
  320. } else {
  321. /* write */
  322. EPx_read_status = DEVDRV_USBF_PIPE_WAIT;
  323. (object->*(epCallback[ep - 2 + 1])) ();
  324. EPx_read_status = DEVDRV_USBF_PIPE_DONE;
  325. usbx_function_write_buffer(pipe);
  326. }
  327. }
  328. }
  329. }
  330. /* Three dummy reads for clearing interrupt requests */
  331. dumy_sts = USB20X.BRDYSTS;
  332. }
  333. }
  334. /******************************************************************************
  335. * Function Name: usbx_function_NRDYInterruptPIPE0
  336. * Description : Executes NRDY interrupt for pipe0.
  337. * Arguments : uint16_t status ; NRDYSTS Register Value
  338. * : uint16_t intenb ; NRDYENB Register Value
  339. * Return Value : none
  340. *****************************************************************************/
  341. extern "C" {
  342. void usbx_function_NRDYInterruptPIPE0(
  343. uint16_t status,
  344. uint16_t intenb,
  345. USBHAL *object,
  346. void (USBHAL::*EP0func)(void)
  347. )
  348. {
  349. volatile uint16_t dumy_sts;
  350. USB20X.NRDYSTS =
  351. (uint16_t)~g_usbx_function_bit_set[USB_FUNCTION_PIPE0];
  352. /* Three dummy reads for clearing interrupt requests */
  353. dumy_sts = USB20X.NRDYSTS;
  354. }
  355. }
  356. /******************************************************************************
  357. * Function Name: usbx_function_NRDYInterrupt
  358. * Description : Executes NRDY interrupt exclude pipe0.
  359. * Arguments : uint16_t status ; NRDYSTS Register Value
  360. * : uint16_t intenb ; NRDYENB Register Value
  361. * Return Value : none
  362. *****************************************************************************/
  363. extern "C" {
  364. void usbx_function_NRDYInterrupt(
  365. uint16_t status,
  366. uint16_t intenb,
  367. USBHAL *object,
  368. bool (USBHAL::*epCallback[])(void)
  369. )
  370. {
  371. volatile uint16_t dumy_sts;
  372. /**************************************************************
  373. * Function Name: usbx_function_nrdy_int
  374. * Description : Executes NRDY interrupt(USB_FUNCTION_PIPE1-9).
  375. * : Checks NRDY interrupt cause by PID. When the cause if STALL,
  376. * : regards the pipe state as STALL and ends the processing.
  377. * : Then the cause is not STALL, increments the error count to
  378. * : communicate again. When the error count is 3, determines
  379. * : the pipe state as DEVDRV_USBF_PIPE_NORES and ends the processing.
  380. * : This function is executed in the NRDY interrupt handler.
  381. * : This function clears NRDY interrupt status.
  382. * Arguments : uint16_t status ; NRDYSTS Register Value
  383. * : uint16_t int_enb ; NRDYENB Register Value
  384. * Return Value : none
  385. *************************************************************/
  386. /* copied from usbx_function_intrn.c */
  387. #if 0
  388. uint16_t usefifo;
  389. #endif
  390. uint16_t pid;
  391. uint16_t pipe;
  392. uint16_t bitcheck;
  393. #if 0
  394. uint16_t mbw;
  395. uint32_t size;
  396. #endif
  397. uint16_t ep;
  398. bitcheck = (uint16_t)(status & intenb);
  399. USB20X.NRDYSTS = (uint16_t)~status;
  400. if (RZA_IO_RegRead_16(&USB20X.SYSCFG0, USB_SYSCFG_DCFM_SHIFT, USB_SYSCFG_DCFM) == 1) {
  401. /* USB HOST */
  402. /* not support */
  403. } else {
  404. /* USB Function */
  405. for (pipe = USB_FUNCTION_PIPE1; pipe <= USB_FUNCTION_MAX_PIPE_NO; pipe++) {
  406. if ((bitcheck&g_usbx_function_bit_set[pipe]) != g_usbx_function_bit_set[pipe]) {
  407. continue;
  408. }
  409. if (g_usbx_function_pipe_status[pipe] != DEVDRV_USBF_PIPE_WAIT) {
  410. continue;
  411. }
  412. #if 0
  413. usbx_function_set_pid_nak(pipe);
  414. size = (uint32_t)g_usbx_function_data_count[pipe];
  415. mbw = usbx_function_get_mbw(
  416. size, (uint32_t)g_usbx_function_data_pointer[pipe]);
  417. usefifo = (uint16_t)(g_usbx_function_PipeTbl[pipe] & USB_FUNCTION_FIFO_USE);
  418. switch (usefifo) {
  419. case USB_FUNCTION_D0FIFO_USE:
  420. usbx_function_set_curpipe(
  421. pipe, USB_FUNCTION_D0USE, DEVDRV_USBF_NO, mbw);
  422. USB20X.D0FIFOCTR = USB_FUNCTION_BITBCLR;
  423. break;
  424. case USB_FUNCTION_D1FIFO_USE:
  425. usbx_function_set_curpipe(
  426. pipe, USB_FUNCTION_D1USE, DEVDRV_USBF_NO, mbw);
  427. USB20X.D1FIFOCTR = USB_FUNCTION_BITBCLR;
  428. break;
  429. default:
  430. usbx_function_set_curpipe(
  431. pipe, USB_FUNCTION_CUSE, USB_FUNCTION_CFIFO_READ, mbw);
  432. USB20X.CFIFOCTR = USB_FUNCTION_BITBCLR;
  433. break;
  434. }
  435. usbx_function_aclrm(pipe);
  436. usbx_function_enable_nrdy_int(pipe);
  437. usbx_function_enable_brdy_int(pipe);
  438. usbx_function_set_pid_buf(pipe);
  439. #endif
  440. pid = usbx_function_get_pid(pipe);
  441. if ((pid == DEVDRV_USBF_PID_STALL) || (pid == DEVDRV_USBF_PID_STALL2)) {
  442. g_usbx_function_pipe_status[pipe] = DEVDRV_USBF_PIPE_STALL;
  443. } else {
  444. usbx_function_set_pid_buf(pipe);
  445. }
  446. ep = (g_usbx_function_pipecfg[pipe] & USB_PIPECFG_EPNUM) >> USB_PIPECFG_EPNUM_SHIFT;
  447. ep <<= 1;
  448. if (RZA_IO_RegRead_16(
  449. &g_usbx_function_pipecfg[pipe], USB_PIPECFG_DIR_SHIFT, USB_PIPECFG_DIR) == 0) {
  450. /* read */
  451. __NOP();
  452. } else {
  453. /* write */
  454. __NOP();
  455. }
  456. }
  457. }
  458. /* Three dummy reads for clearing interrupt requests */
  459. dumy_sts = USB20X.NRDYSTS;
  460. }
  461. }
  462. /******************************************************************************
  463. * Function Name: usbx_function_BEMPInterruptPIPE0
  464. * Description : Executes BEMP interrupt for pipe0.
  465. * Arguments : uint16_t status ; BEMPSTS Register Value
  466. * : uint16_t intenb ; BEMPENB Register Value
  467. * Return Value : none
  468. *****************************************************************************/
  469. extern "C" {
  470. void usbx_function_BEMPInterruptPIPE0(
  471. uint16_t status,
  472. uint16_t intenb,
  473. USBHAL *object,
  474. void (USBHAL::*EP0func)(void)
  475. )
  476. {
  477. volatile uint16_t dumy_sts;
  478. USB20X.BEMPSTS =
  479. (uint16_t)~g_usbx_function_bit_set[USB_FUNCTION_PIPE0];
  480. RZA_IO_RegWrite_16(
  481. &USB20X.CFIFOSEL, USB_FUNCTION_PIPE0,
  482. USB_CFIFOSEL_CURPIPE_SHIFT, USB_CFIFOSEL_CURPIPE);
  483. /*usbx_function_write_buffer_c(USB_FUNCTION_PIPE0);*/
  484. (object->*EP0func)();
  485. /* Three dummy reads for clearing interrupt requests */
  486. dumy_sts = USB20X.BEMPSTS;
  487. }
  488. }
  489. /******************************************************************************
  490. * Function Name: usbx_function_BEMPInterrupt
  491. * Description : Executes BEMP interrupt exclude pipe0.
  492. * Arguments : uint16_t status ; BEMPSTS Register Value
  493. * : uint16_t intenb ; BEMPENB Register Value
  494. * Return Value : none
  495. *****************************************************************************/
  496. extern "C" {
  497. void usbx_function_BEMPInterrupt(
  498. uint16_t status,
  499. uint16_t intenb,
  500. USBHAL *object,
  501. bool (USBHAL::*epCallback[])(void)
  502. )
  503. {
  504. volatile uint16_t dumy_sts;
  505. /**************************************************************
  506. * Function Name: usbx_function_bemp_int
  507. * Description : Executes BEMP interrupt(USB_FUNCTION_PIPE1-9).
  508. * Arguments : uint16_t status ; BEMPSTS Register Value
  509. * : uint16_t intenb ; BEMPENB Register Value
  510. * Return Value : none
  511. *************************************************************/
  512. /* copied from usbx_function_intrn.c */
  513. uint16_t pid;
  514. uint16_t pipe;
  515. uint16_t bitcheck;
  516. uint16_t inbuf;
  517. uint16_t ep;
  518. bitcheck = (uint16_t)(status & intenb);
  519. USB20X.BEMPSTS = (uint16_t)~status;
  520. for (pipe = USB_FUNCTION_PIPE1; pipe <= USB_FUNCTION_MAX_PIPE_NO; pipe++) {
  521. if ((bitcheck&g_usbx_function_bit_set[pipe]) != g_usbx_function_bit_set[pipe]) {
  522. continue;
  523. }
  524. pid = usbx_function_get_pid(pipe);
  525. if ((pid == DEVDRV_USBF_PID_STALL) ||
  526. (pid == DEVDRV_USBF_PID_STALL2)) {
  527. g_usbx_function_pipe_status[pipe] = DEVDRV_USBF_PIPE_STALL;
  528. } else {
  529. inbuf = usbx_function_get_inbuf(pipe);
  530. if (inbuf == 0) {
  531. usbx_function_disable_bemp_int(pipe);
  532. usbx_function_set_pid_nak(pipe);
  533. g_usbx_function_pipe_status[pipe] = DEVDRV_USBF_PIPE_DONE;
  534. switch (g_usbx_function_PipeTbl[pipe] & USB_FUNCTION_FIFO_USE) {
  535. case USB_FUNCTION_D0FIFO_DMA:
  536. /*now, DMA is not supported*/
  537. break;
  538. case USB_FUNCTION_D1FIFO_DMA:
  539. /*now, DMA is not supported*/
  540. break;
  541. default:
  542. ep = (g_usbx_function_pipecfg[pipe] & USB_PIPECFG_EPNUM) >> USB_PIPECFG_EPNUM_SHIFT;
  543. ep <<= 1;
  544. if (RZA_IO_RegRead_16(
  545. &g_usbx_function_pipecfg[pipe], USB_PIPECFG_DIR_SHIFT, USB_PIPECFG_DIR) == 0) {
  546. /* read */
  547. __NOP();
  548. } else {
  549. /* write */
  550. EPx_read_status = DEVDRV_USBF_PIPE_WAIT;
  551. (object->*(epCallback[ep - 2 + 1])) ();
  552. EPx_read_status = DEVDRV_USBF_PIPE_DONE;
  553. }
  554. }
  555. }
  556. }
  557. }
  558. /* Three dummy reads for clearing interrupt requests */
  559. dumy_sts = USB20X.BEMPSTS;
  560. }
  561. }
  562. /******************************************************************************
  563. * Function Name: EP2PIPE
  564. * Description : Converts from endpoint to pipe
  565. * Arguments : number of endpoint
  566. * Return Value : number of pipe
  567. *****************************************************************************/
  568. /*EP2PIPE converter is for pipe1, pipe3 and pipe6 only.*/
  569. #define EP2PIPE(endpoint) ((uint32_t)usbx_function_EpToPipe(endpoint))
  570. /******************************************************************************
  571. * Function Name: usbx_function_save_request
  572. * Description : Retains the USB request information in variables.
  573. * Arguments : none
  574. * Return Value : none
  575. *****************************************************************************/
  576. #define usbx_function_save_request() \
  577. { \
  578. uint16_t *bufO = &setup_buffer[0]; \
  579. \
  580. USB20X.INTSTS0 = (uint16_t)~USB_FUNCTION_BITVALID; \
  581. /*data[0] <= bmRequest, data[1] <= bmRequestType */ \
  582. *bufO++ = USB20X.USBREQ; \
  583. /*data[2] data[3] <= wValue*/ \
  584. *bufO++ = USB20X.USBVAL; \
  585. /*data[4] data[5] <= wIndex*/ \
  586. *bufO++ = USB20X.USBINDX; \
  587. /*data[6] data[6] <= wIndex*/ \
  588. *bufO++ = USB20X.USBLENG; \
  589. }
  590. /*************************************************************************/
  591. /*************************************************************************/
  592. /*************************************************************************/
  593. /*************************************************************************/
  594. /* constructor */
  595. USBHAL::USBHAL(void)
  596. {
  597. /* ---- P4_1 : P4_1 (USB0_EN for GR-PEACH) ---- */
  598. //usbx_en = new DigitalOut(P4_1, 1);
  599. /* some constants */
  600. int_id = USBIX_IRQn;
  601. int_level = ( 2 << 3 );
  602. clock_mode = USBFCLOCK_X1_48MHZ;
  603. #if (USB_FUNCTION_HISPEED == 0)
  604. mode = USB_FUNCTION_FULL_SPEED;
  605. #else
  606. mode = USB_FUNCTION_HIGH_SPEED;
  607. #endif
  608. EP0_read_status = DEVDRV_USBF_WRITEEND;
  609. EPx_read_status = DEVDRV_USBF_PIPE_DONE;
  610. /* Disables interrupt for usb */
  611. GIC_DisableIRQ(int_id);
  612. /* Setup the end point */
  613. epCallback[ 0] = &USBHAL::EP1_OUT_callback;
  614. epCallback[ 1] = &USBHAL::EP1_IN_callback;
  615. epCallback[ 2] = &USBHAL::EP2_OUT_callback;
  616. epCallback[ 3] = &USBHAL::EP2_IN_callback;
  617. epCallback[ 4] = &USBHAL::EP3_OUT_callback;
  618. epCallback[ 5] = &USBHAL::EP3_IN_callback;
  619. epCallback[ 6] = &USBHAL::EP4_OUT_callback;
  620. epCallback[ 7] = &USBHAL::EP4_IN_callback;
  621. epCallback[ 8] = &USBHAL::EP5_OUT_callback;
  622. epCallback[ 9] = &USBHAL::EP5_IN_callback;
  623. epCallback[10] = &USBHAL::EP6_OUT_callback;
  624. epCallback[11] = &USBHAL::EP6_IN_callback;
  625. epCallback[12] = &USBHAL::EP7_OUT_callback;
  626. epCallback[13] = &USBHAL::EP7_IN_callback;
  627. epCallback[14] = &USBHAL::EP8_OUT_callback;
  628. epCallback[15] = &USBHAL::EP8_IN_callback;
  629. epCallback[16] = &USBHAL::EP9_OUT_callback;
  630. epCallback[17] = &USBHAL::EP9_IN_callback;
  631. epCallback[18] = &USBHAL::EP10_OUT_callback;
  632. epCallback[19] = &USBHAL::EP10_IN_callback;
  633. epCallback[20] = &USBHAL::EP11_OUT_callback;
  634. epCallback[21] = &USBHAL::EP11_IN_callback;
  635. epCallback[22] = &USBHAL::EP12_OUT_callback;
  636. epCallback[23] = &USBHAL::EP12_IN_callback;
  637. epCallback[24] = &USBHAL::EP13_OUT_callback;
  638. epCallback[25] = &USBHAL::EP13_IN_callback;
  639. epCallback[26] = &USBHAL::EP14_OUT_callback;
  640. epCallback[27] = &USBHAL::EP14_IN_callback;
  641. epCallback[28] = &USBHAL::EP15_OUT_callback;
  642. epCallback[29] = &USBHAL::EP15_IN_callback;
  643. /* registers me */
  644. instance = this;
  645. /* Clear pipe table */
  646. usbx_function_clear_pipe_tbl();
  647. /******************************************************************************
  648. * Function Name: usbx_api_function_init
  649. * Description : Initializes the USB module in the USB function mode.
  650. *****************************************************************************/
  651. /* The clock of USB0 modules is permitted */
  652. #if (USB_FUNCTION_CH == 0)
  653. CPG.STBCR7 &= ~(CPG_STBCR7_MSTP71);
  654. #else
  655. CPG.STBCR7 &= ~(CPG_STBCR7_MSTP71 | CPG_STBCR7_MSTP70);
  656. #endif
  657. volatile uint8_t dummy8;
  658. dummy8 = CPG.STBCR7;
  659. {
  660. /******************************************************************************
  661. * Function Name: usbx_function_setting_interrupt
  662. * Description : Sets the USB module interrupt level.
  663. *****************************************************************************/
  664. #if 0 /*DMA is not supported*/
  665. IRQn_Type d0fifo_dmaintid;
  666. IRQn_Type d1fifo_dmaintid;
  667. #endif
  668. InterruptHandlerRegister(int_id, &_usbisr);
  669. GIC_SetPriority(int_id, int_level);
  670. GIC_EnableIRQ(int_id);
  671. #if 0 /*DMA is not supported*/
  672. d0fifo_dmaintid = Userdef_USB_usbx_function_d0fifo_dmaintid();
  673. if (d0fifo_dmaintid != 0xFFFF) {
  674. InterruptHandlerRegister(d0fifo_dmaintid, usbx_function_dma_interrupt_d0fifo);
  675. GIC_SetPriority(d0fifo_dmaintid, int_level);
  676. GIC_EnableIRQ(d0fifo_dmaintid);
  677. }
  678. #endif
  679. #if 0 /*DMA is not supported*/
  680. d1fifo_dmaintid = Userdef_USB_usbx_function_d1fifo_dmaintid();
  681. if (d1fifo_dmaintid != 0xFFFF) {
  682. InterruptHandlerRegister(d1fifo_dmaintid, usbx_function_dma_interrupt_d1fifo);
  683. GIC_SetPriority(d1fifo_dmaintid, int_level);
  684. GIC_EnableIRQ(d1fifo_dmaintid);
  685. }
  686. #endif
  687. /*****************************************************************************/
  688. }
  689. /* reset USB module with setting tranciever and HSE=1 */
  690. usbx_function_reset_module(clock_mode);
  691. /* clear variables */
  692. usbx_function_init_status();
  693. /* select USB Function and Interrupt Enable */
  694. /* Detect USB Device to attach or detach */
  695. usbx_function_InitModule(mode);
  696. {
  697. uint16_t buf;
  698. buf = USB20X.INTENB0;
  699. buf |= USB_INTENB0_SOFE;
  700. USB20X.INTENB0 = buf;
  701. }
  702. }
  703. /*************************************************************************/
  704. USBHAL::~USBHAL(void)
  705. {
  706. /* Disables interrupt for usb */
  707. GIC_DisableIRQ( int_id );
  708. /* Unregisters interrupt function and priority */
  709. InterruptHandlerRegister( int_id, (uint32_t)NULL );
  710. //usbx_en = NULL;
  711. instance = NULL;
  712. }
  713. /*************************************************************************/
  714. void USBHAL::connect(void)
  715. {
  716. /* Activates USB0_EN */
  717. //(*usbx_en) = 0;
  718. }
  719. /*************************************************************************/
  720. void USBHAL::disconnect(void)
  721. {
  722. /* Deactivates USB0_EN */
  723. //(*usbx_en) = 1;
  724. }
  725. /*************************************************************************/
  726. void USBHAL::configureDevice(void)
  727. {
  728. /*The pipes set up in USBHAL::realiseEndpoint*/
  729. /*usbx_function_clear_alt();*/ /* Alternate setting clear */
  730. /*usbx_function_set_pid_buf(USB_FUNCTION_PIPE0);*/
  731. }
  732. /*************************************************************************/
  733. void USBHAL::unconfigureDevice(void)
  734. {
  735. /* The Interface would be managed by USBDevice */
  736. /*usbx_function_clear_alt();*/ /* Alternate setting clear */
  737. /*usbx_function_set_pid_buf(USB_FUNCTION_PIPE0);*/
  738. }
  739. /*************************************************************************/
  740. void USBHAL::setAddress(uint8_t address)
  741. {
  742. if (address <= 127) {
  743. usbx_function_set_pid_buf(USB_FUNCTION_PIPE0); /* OK */
  744. } else {
  745. usbx_function_set_pid_stall(USB_FUNCTION_PIPE0); /* Not Spec */
  746. }
  747. }
  748. /*************************************************************************/
  749. bool USBHAL::realiseEndpoint(uint8_t endpoint, uint32_t maxPacket, uint32_t flags)
  750. {
  751. const struct PIPECFGREC *cfg;
  752. uint16_t pipe;
  753. uint16_t buf;
  754. if ( (EP0OUT == endpoint) || (EP0IN == endpoint) ) {
  755. return true;
  756. }
  757. for (cfg = &def_pipecfg[0]; cfg->pipesel != 0; cfg++) {
  758. if (cfg->endpoint == endpoint) {
  759. break;
  760. }
  761. }
  762. if (cfg->pipesel == 0) {
  763. return false;
  764. }
  765. pipe = ((cfg->pipesel & USB_PIPESEL_PIPESEL) >> USB_PIPESEL_PIPESEL_SHIFT);
  766. g_usbx_function_PipeTbl[ pipe ] = (uint16_t)(endpoint | ((cfg->pipesel & USB_FUNCTION_FIFO_USE) << 0));
  767. /* There are maintenance routine of SHTNAK and BFRE bits
  768. * in original sample program. This sample is not
  769. * programmed. Do maintenance the "def_pipecfg" array if
  770. * you want it. */
  771. /* Interrupt Disable */
  772. buf = USB20X.BRDYENB;
  773. buf &= (uint16_t)~g_usbx_function_bit_set[pipe];
  774. USB20X.BRDYENB = buf;
  775. buf = USB20X.NRDYENB;
  776. buf &= (uint16_t)~g_usbx_function_bit_set[pipe];
  777. USB20X.NRDYENB = buf;
  778. buf = USB20X.BEMPENB;
  779. buf &= (uint16_t)~g_usbx_function_bit_set[pipe];
  780. USB20X.BEMPENB = buf;
  781. usbx_function_set_pid_nak(pipe);
  782. /* CurrentPIPE Clear */
  783. if (RZA_IO_RegRead_16(&USB20X.CFIFOSEL, USB_CFIFOSEL_CURPIPE_SHIFT, USB_CFIFOSEL_CURPIPE) == pipe) {
  784. RZA_IO_RegWrite_16(&USB20X.CFIFOSEL, 0, USB_CFIFOSEL_CURPIPE_SHIFT, USB_CFIFOSEL_CURPIPE);
  785. }
  786. if (RZA_IO_RegRead_16(&USB20X.D0FIFOSEL, USB_DnFIFOSEL_CURPIPE_SHIFT, USB_DnFIFOSEL_CURPIPE) == pipe) {
  787. RZA_IO_RegWrite_16(&USB20X.D0FIFOSEL, 0, USB_DnFIFOSEL_CURPIPE_SHIFT, USB_DnFIFOSEL_CURPIPE);
  788. }
  789. if (RZA_IO_RegRead_16(&USB20X.D1FIFOSEL, USB_DnFIFOSEL_CURPIPE_SHIFT, USB_DnFIFOSEL_CURPIPE) == pipe) {
  790. RZA_IO_RegWrite_16(&USB20X.D1FIFOSEL, 0, USB_DnFIFOSEL_CURPIPE_SHIFT, USB_DnFIFOSEL_CURPIPE);
  791. }
  792. /* PIPE Configuration */
  793. USB20X.PIPESEL = pipe;
  794. USB20X.PIPECFG = cfg->pipecfg;
  795. USB20X.PIPEBUF = cfg->pipebuf;
  796. USB20X.PIPEMAXP = cfg->pipemaxp;
  797. USB20X.PIPEPERI = cfg->pipeperi;
  798. g_usbx_function_pipecfg[pipe] = cfg->pipecfg;
  799. g_usbx_function_pipebuf[pipe] = cfg->pipebuf;
  800. g_usbx_function_pipemaxp[pipe] = cfg->pipemaxp;
  801. g_usbx_function_pipeperi[pipe] = cfg->pipeperi;
  802. /* Buffer Clear */
  803. usbx_function_set_sqclr(pipe);
  804. usbx_function_aclrm(pipe);
  805. /* init Global */
  806. g_usbx_function_pipe_status[pipe] = DEVDRV_USBF_PIPE_IDLE;
  807. g_usbx_function_PipeDataSize[pipe] = 0;
  808. return true;
  809. }
  810. /*************************************************************************/
  811. // read setup packet
  812. void USBHAL::EP0setup(uint8_t *buffer)
  813. {
  814. memcpy(buffer, setup_buffer, MAX_PACKET_SIZE_EP0);
  815. }
  816. /*************************************************************************/
  817. void USBHAL::EP0readStage(void)
  818. {
  819. // No implements
  820. }
  821. /*************************************************************************/
  822. void USBHAL::EP0read(void)
  823. {
  824. uint8_t *buffer;
  825. uint32_t size;
  826. /* remain of last writing */
  827. while (EP0_read_status != DEVDRV_USBF_WRITEEND) {
  828. static uint8_t bbb[2] = { 255, 255 };
  829. EP0write(&bbb[0], 0);
  830. }
  831. buffer = (uint8_t*)(&setup_buffer[4]);
  832. size = (MAX_PACKET_SIZE_EP0 / 2) - 8;
  833. usbx_api_function_CtrlWriteStart(size, buffer);
  834. }
  835. /*************************************************************************/
  836. uint32_t USBHAL::EP0getReadResult(uint8_t *buffer)
  837. {
  838. memcpy(buffer, (uint8_t*)(&setup_buffer[4]), g_usbx_function_PipeDataSize[USB_FUNCTION_PIPE0]);
  839. return g_usbx_function_PipeDataSize[USB_FUNCTION_PIPE0];
  840. }
  841. /*************************************************************************/
  842. void USBHAL::EP0write(uint8_t *buffer, uint32_t size)
  843. {
  844. /* zero byte writing */
  845. if ( (size == 0) && (EP0_read_status == DEVDRV_USBF_WRITEEND) ) {
  846. return;
  847. }
  848. if (EP0_read_status == DEVDRV_USBF_WRITEEND) {
  849. /*1st block*/
  850. EP0_read_status = usbx_api_function_CtrlReadStart(size, buffer);
  851. } else {
  852. /* waits the last transmission */
  853. /*other blocks*/
  854. g_usbx_function_data_count[ USB_FUNCTION_PIPE0 ] = size;
  855. g_usbx_function_data_pointer [ USB_FUNCTION_PIPE0 ] = buffer;
  856. EP0_read_status = usbx_function_write_buffer_c(USB_FUNCTION_PIPE0);
  857. }
  858. /*max size may be deblocking outside*/
  859. if (size == MAX_PACKET_SIZE_EP0) {
  860. EP0_read_status = DEVDRV_USBF_WRITING;
  861. }
  862. }
  863. /*************************************************************************/
  864. #if 0 // No implements
  865. void USBHAL::EP0getWriteResult(void)
  866. {
  867. }
  868. #endif
  869. /*************************************************************************/
  870. void USBHAL::EP0stall(void)
  871. {
  872. stallEndpoint( 0 );
  873. }
  874. /*************************************************************************/
  875. EP_STATUS USBHAL::endpointRead(uint8_t endpoint, uint32_t max_size)
  876. {
  877. uint32_t pipe = EP2PIPE(endpoint);
  878. uint32_t pipe_size;
  879. uint16_t pipe_status;
  880. EP_STATUS status = EP_COMPLETED;
  881. pipe_status = usbx_api_function_check_pipe_status(pipe, &pipe_size);
  882. switch (pipe_status) {
  883. case DEVDRV_USBF_PIPE_IDLE:
  884. case DEVDRV_USBF_PIPE_WAIT:
  885. usbx_api_function_set_pid_nak(pipe);
  886. usbx_api_function_clear_pipe_status(pipe);
  887. usbx_api_function_start_receive_transfer(pipe, max_size, recv_buffer);
  888. break;
  889. default:
  890. status = EP_PENDING;
  891. break;
  892. }
  893. return status;
  894. }
  895. /*************************************************************************/
  896. EP_STATUS USBHAL::endpointReadResult(uint8_t endpoint, uint8_t *buffer, uint32_t *bytes_read )
  897. {
  898. uint32_t pipe = EP2PIPE(endpoint);
  899. uint16_t pipe_status;
  900. uint16_t err;
  901. EP_STATUS status = EP_PENDING;
  902. if (EPx_read_status != DEVDRV_USBF_PIPE_WAIT) {
  903. return status;
  904. }
  905. pipe_status = usbx_api_function_check_pipe_status(pipe, bytes_read);
  906. switch (pipe_status) {
  907. case DEVDRV_USBF_PIPE_IDLE:
  908. return EP_COMPLETED;
  909. case DEVDRV_USBF_PIPE_DONE:
  910. return EP_COMPLETED;
  911. case DEVDRV_USBF_PIPE_WAIT:
  912. break;
  913. default:
  914. return status;
  915. }
  916. /* sets the output buffer and size */
  917. g_usbx_function_data_pointer[pipe] = buffer;
  918. /* receives data from pipe */
  919. err = usbx_function_read_buffer(pipe);
  920. recv_error = err;
  921. switch (err) {
  922. case USB_FUNCTION_READEND:
  923. case USB_FUNCTION_READSHRT:
  924. case USB_FUNCTION_READOVER:
  925. *bytes_read = g_usbx_function_PipeDataSize[pipe];
  926. break;
  927. case USB_FUNCTION_READING:
  928. case DEVDRV_USBF_FIFOERROR:
  929. break;
  930. }
  931. pipe_status = usbx_api_function_check_pipe_status(pipe, bytes_read);
  932. switch (pipe_status) {
  933. case DEVDRV_USBF_PIPE_DONE:
  934. status = EP_COMPLETED;
  935. break;
  936. case DEVDRV_USBF_PIPE_IDLE:
  937. case DEVDRV_USBF_PIPE_NORES:
  938. case DEVDRV_USBF_PIPE_STALL:
  939. case DEVDRV_USBF_FIFOERROR:
  940. default:
  941. break;
  942. }
  943. return status;
  944. }
  945. /*************************************************************************/
  946. EP_STATUS USBHAL::endpointWrite(uint8_t endpoint, uint8_t *data, uint32_t size)
  947. {
  948. uint32_t pipe = EP2PIPE(endpoint);
  949. uint32_t pipe_size;
  950. uint16_t pipe_status;
  951. uint16_t err;
  952. uint16_t count;
  953. EP_STATUS status = EP_PENDING;
  954. pipe_status = usbx_api_function_check_pipe_status(pipe, &pipe_size);
  955. /* waits the last transmission */
  956. count = 30000;
  957. while ((pipe_status == DEVDRV_USBF_PIPE_WAIT) || (pipe_status == DEVDRV_USBF_PIPE_DONE)) {
  958. pipe_status = usbx_api_function_check_pipe_status(pipe, &pipe_size);
  959. if( --count == 0 ) {
  960. pipe_status = DEVDRV_USBF_PIPE_STALL;
  961. break;
  962. }
  963. }
  964. switch (pipe_status) {
  965. case DEVDRV_USBF_PIPE_IDLE:
  966. err = usbx_api_function_start_send_transfer(pipe, size, data);
  967. switch (err) {
  968. /* finish to write */
  969. case DEVDRV_USBF_WRITEEND:
  970. /* finish to write, but data is short */
  971. case DEVDRV_USBF_WRITESHRT:
  972. /* continue to write */
  973. case DEVDRV_USBF_WRITING:
  974. /* use DMA */
  975. case DEVDRV_USBF_WRITEDMA:
  976. /* error */
  977. case DEVDRV_USBF_FIFOERROR:
  978. status = EP_PENDING;
  979. break;
  980. }
  981. break;
  982. case DEVDRV_USBF_PIPE_WAIT:
  983. case DEVDRV_USBF_PIPE_DONE:
  984. status = EP_PENDING;
  985. break;
  986. case DEVDRV_USBF_PIPE_NORES:
  987. case DEVDRV_USBF_PIPE_STALL:
  988. default:
  989. status = EP_STALLED;
  990. break;
  991. }
  992. return status;
  993. }
  994. /*************************************************************************/
  995. EP_STATUS USBHAL::endpointWriteResult(uint8_t endpoint)
  996. {
  997. uint32_t pipe = EP2PIPE(endpoint);
  998. uint32_t pipe_size;
  999. uint16_t pipe_status;
  1000. EP_STATUS status = EP_PENDING;
  1001. pipe_status = usbx_api_function_check_pipe_status(pipe, &pipe_size);
  1002. switch (pipe_status) {
  1003. case DEVDRV_USBF_PIPE_IDLE:
  1004. status = EP_COMPLETED;
  1005. break;
  1006. case DEVDRV_USBF_PIPE_WAIT:
  1007. status = EP_PENDING;
  1008. break;
  1009. case DEVDRV_USBF_PIPE_DONE:
  1010. usbx_function_stop_transfer(pipe);
  1011. status = EP_COMPLETED;
  1012. break;
  1013. case DEVDRV_USBF_PIPE_NORES:
  1014. status = EP_STALLED;
  1015. break;
  1016. case DEVDRV_USBF_PIPE_STALL:
  1017. status = EP_STALLED;
  1018. break;
  1019. default:
  1020. status = EP_PENDING;
  1021. }
  1022. return status;
  1023. }
  1024. /*************************************************************************/
  1025. void USBHAL::stallEndpoint(uint8_t endpoint)
  1026. {
  1027. uint32_t pipe = EP2PIPE(endpoint);
  1028. usbx_function_clear_pid_stall(pipe);
  1029. }
  1030. /*************************************************************************/
  1031. void USBHAL::unstallEndpoint(uint8_t endpoint)
  1032. {
  1033. uint32_t pipe = EP2PIPE(endpoint);
  1034. usbx_function_set_pid_stall( pipe );
  1035. }
  1036. /*************************************************************************/
  1037. bool USBHAL::getEndpointStallState(uint8_t endpoint)
  1038. {
  1039. // No implemens
  1040. return false;
  1041. }
  1042. /*************************************************************************/
  1043. #if 0 // No implements
  1044. void USBHAL::remoteWakeup(void)
  1045. {
  1046. }
  1047. #endif
  1048. /*************************************************************************/
  1049. void USBHAL::_usbisr(void)
  1050. {
  1051. instance->usbisr();
  1052. }
  1053. /*************************************************************************/
  1054. void USBHAL::usbisr(void)
  1055. {
  1056. uint16_t int_sts0;
  1057. uint16_t int_sts1;
  1058. uint16_t int_sts2;
  1059. uint16_t int_sts3;
  1060. uint16_t int_enb0;
  1061. uint16_t int_enb2;
  1062. uint16_t int_enb3;
  1063. uint16_t int_enb4;
  1064. volatile uint16_t dumy_sts;
  1065. int_sts0 = USB20X.INTSTS0;
  1066. if (!(int_sts0 & (
  1067. USB_FUNCTION_BITVBINT |
  1068. USB_FUNCTION_BITRESM |
  1069. USB_FUNCTION_BITSOFR |
  1070. USB_FUNCTION_BITDVST |
  1071. USB_FUNCTION_BITCTRT |
  1072. USB_FUNCTION_BITBEMP |
  1073. USB_FUNCTION_BITNRDY |
  1074. USB_FUNCTION_BITBRDY ))) {
  1075. return;
  1076. }
  1077. int_sts1 = USB20X.BRDYSTS;
  1078. int_sts2 = USB20X.NRDYSTS;
  1079. int_sts3 = USB20X.BEMPSTS;
  1080. int_enb0 = USB20X.INTENB0;
  1081. int_enb2 = USB20X.BRDYENB;
  1082. int_enb3 = USB20X.NRDYENB;
  1083. int_enb4 = USB20X.BEMPENB;
  1084. if ((int_sts0 & USB_FUNCTION_BITRESM) &&
  1085. (int_enb0 & USB_FUNCTION_BITRSME)) {
  1086. USB20X.INTSTS0 = (uint16_t)~USB_FUNCTION_BITRESM;
  1087. RZA_IO_RegWrite_16(&USB20X.INTENB0, 0, USB_INTENB0_RSME_SHIFT, USB_INTENB0_RSME);
  1088. /*usbx_function_USB_FUNCTION_Resume();*/
  1089. suspendStateChanged(1);
  1090. } else if (
  1091. (int_sts0 & USB_FUNCTION_BITVBINT) &&
  1092. (int_enb0 & USB_FUNCTION_BITVBSE)) {
  1093. USB20X.INTSTS0 = (uint16_t)~USB_FUNCTION_BITVBINT;
  1094. if (usbx_function_CheckVBUStaus() == DEVDRV_USBF_ON) {
  1095. usbx_function_USB_FUNCTION_Attach();
  1096. } else {
  1097. usbx_function_USB_FUNCTION_Detach();
  1098. }
  1099. } else if (
  1100. (int_sts0 & USB_FUNCTION_BITSOFR) &&
  1101. (int_enb0 & USB_FUNCTION_BITSOFE)) {
  1102. USB20X.INTSTS0 = (uint16_t)~USB_FUNCTION_BITSOFR;
  1103. SOF((USB20X.FRMNUM & USB_FRMNUM_FRNM) >> USB_FRMNUM_FRNM_SHIFT);
  1104. } else if (
  1105. (int_sts0 & USB_FUNCTION_BITDVST) &&
  1106. (int_enb0 & USB_FUNCTION_BITDVSE)) {
  1107. USB20X.INTSTS0 = (uint16_t)~USB_FUNCTION_BITDVST;
  1108. switch (int_sts0 & USB_FUNCTION_BITDVSQ) {
  1109. case USB_FUNCTION_DS_POWR:
  1110. break;
  1111. case USB_FUNCTION_DS_DFLT:
  1112. /*****************************************************************************
  1113. * Function Name: usbx_function_USB_FUNCTION_BusReset
  1114. * Description : This function is executed when the USB device is transitioned
  1115. * : to POWERD_STATE. Sets the device descriptor according to the
  1116. * : connection speed determined by the USB reset hand shake.
  1117. * Arguments : none
  1118. * Return Value : none
  1119. *****************************************************************************/
  1120. usbx_function_init_status(); /* memory clear */
  1121. #if 0
  1122. /* You would program those steps in USBCallback_busReset
  1123. * if the system need the comment out steps.
  1124. */
  1125. if (usbx_function_is_hispeed() == USB_FUNCTION_HIGH_SPEED) {
  1126. /* Device Descriptor reset */
  1127. usbx_function_ResetDescriptor(USB_FUNCTION_HIGH_SPEED);
  1128. } else {
  1129. /* Device Descriptor reset */
  1130. usbx_function_ResetDescriptor(USB_FUNCTION_FULL_SPEED);
  1131. }
  1132. #endif
  1133. /* Default Control PIPE reset */
  1134. /*****************************************************************************
  1135. * Function Name: usbx_function_ResetDCP
  1136. * Description : Initializes the default control pipe(DCP).
  1137. * Outline : Reset default control pipe
  1138. * Arguments : none
  1139. * Return Value : none
  1140. *****************************************************************************/
  1141. USB20X.DCPCFG = 0;
  1142. USB20X.DCPMAXP = 64; /*TODO: This value is copied from sample*/
  1143. USB20X.CFIFOSEL = (uint16_t)(USB_FUNCTION_BITMBW_8 | USB_FUNCTION_BITBYTE_LITTLE);
  1144. USB20X.D0FIFOSEL = (uint16_t)(USB_FUNCTION_BITMBW_8 | USB_FUNCTION_BITBYTE_LITTLE);
  1145. USB20X.D1FIFOSEL = (uint16_t)(USB_FUNCTION_BITMBW_8 | USB_FUNCTION_BITBYTE_LITTLE);
  1146. busReset();
  1147. break;
  1148. case USB_FUNCTION_DS_ADDS:
  1149. break;
  1150. case USB_FUNCTION_DS_CNFG:
  1151. break;
  1152. case USB_FUNCTION_DS_SPD_POWR:
  1153. case USB_FUNCTION_DS_SPD_DFLT:
  1154. case USB_FUNCTION_DS_SPD_ADDR:
  1155. case USB_FUNCTION_DS_SPD_CNFG:
  1156. suspendStateChanged(0);
  1157. /*usbx_function_USB_FUNCTION_Suspend();*/
  1158. break;
  1159. default:
  1160. break;
  1161. }
  1162. } else if (
  1163. (int_sts0 & USB_FUNCTION_BITBEMP) &&
  1164. (int_enb0 & USB_FUNCTION_BITBEMP) &&
  1165. ((int_sts3 & int_enb4) & g_usbx_function_bit_set[USB_FUNCTION_PIPE0])) {
  1166. /* ==== BEMP PIPE0 ==== */
  1167. usbx_function_BEMPInterruptPIPE0(int_sts3, int_enb4, this, &USBHAL::EP0in);
  1168. } else if (
  1169. (int_sts0 & USB_FUNCTION_BITBRDY) &&
  1170. (int_enb0 & USB_FUNCTION_BITBRDY) &&
  1171. ((int_sts1 & int_enb2) & g_usbx_function_bit_set[USB_FUNCTION_PIPE0])) {
  1172. /* ==== BRDY PIPE0 ==== */
  1173. usbx_function_BRDYInterruptPIPE0(int_sts1, int_enb2, this, &USBHAL::EP0out);
  1174. } else if (
  1175. (int_sts0 & USB_FUNCTION_BITNRDY) &&
  1176. (int_enb0 & USB_FUNCTION_BITNRDY) &&
  1177. ((int_sts2 & int_enb3) & g_usbx_function_bit_set[USB_FUNCTION_PIPE0])) {
  1178. /* ==== NRDY PIPE0 ==== */
  1179. usbx_function_NRDYInterruptPIPE0(int_sts2, int_enb3, this, NULL);
  1180. } else if (
  1181. (int_sts0 & USB_FUNCTION_BITCTRT) && (int_enb0 & USB_FUNCTION_BITCTRE)) {
  1182. int_sts0 = USB20X.INTSTS0;
  1183. USB20X.INTSTS0 = (uint16_t)~USB_FUNCTION_BITCTRT;
  1184. if (((int_sts0 & USB_FUNCTION_BITCTSQ) == USB_FUNCTION_CS_RDDS) ||
  1185. ((int_sts0 & USB_FUNCTION_BITCTSQ) == USB_FUNCTION_CS_WRDS) ||
  1186. ((int_sts0 & USB_FUNCTION_BITCTSQ) == USB_FUNCTION_CS_WRND)) {
  1187. /* remake EP0 into buffer */
  1188. usbx_function_save_request();
  1189. if ((USB20X.INTSTS0 & USB_FUNCTION_BITVALID) && (
  1190. ((int_sts0 & USB_FUNCTION_BITCTSQ) == USB_FUNCTION_CS_RDDS) ||
  1191. ((int_sts0 & USB_FUNCTION_BITCTSQ) == USB_FUNCTION_CS_WRDS) ||
  1192. ((int_sts0 & USB_FUNCTION_BITCTSQ) == USB_FUNCTION_CS_WRND))) {
  1193. /* New SETUP token received */
  1194. /* Three dummy reads for cleearing interrupt requests */
  1195. dumy_sts = USB20X.INTSTS0;
  1196. dumy_sts = USB20X.INTSTS0;
  1197. dumy_sts = USB20X.INTSTS0;
  1198. return;
  1199. }
  1200. }
  1201. switch (int_sts0 & USB_FUNCTION_BITCTSQ) {
  1202. case USB_FUNCTION_CS_IDST:
  1203. if (g_usbx_function_TestModeFlag == DEVDRV_USBF_YES) {
  1204. /* ==== Test Mode ==== */
  1205. usbx_function_USB_FUNCTION_TestMode();
  1206. }
  1207. /* Needs not procedure in this state */
  1208. break;
  1209. case USB_FUNCTION_CS_RDDS:
  1210. /* Reads a setup packet */
  1211. EP0setupCallback();
  1212. break;
  1213. case USB_FUNCTION_CS_WRDS:
  1214. /* Original code was the SetDescriptor was called */
  1215. EP0setupCallback();
  1216. break;
  1217. case USB_FUNCTION_CS_WRND:
  1218. EP0setupCallback();
  1219. /*The EP0setupCallback should finish in successful */
  1220. usbx_function_set_pid_buf(USB_FUNCTION_PIPE0);
  1221. RZA_IO_RegWrite_16(&USB20X.DCPCTR, 1, USB_DCPCTR_CCPL_SHIFT, USB_DCPCTR_CCPL);
  1222. break;
  1223. case USB_FUNCTION_CS_RDSS:
  1224. RZA_IO_RegWrite_16(&USB20X.DCPCTR, 1, USB_DCPCTR_CCPL_SHIFT, USB_DCPCTR_CCPL);
  1225. break;
  1226. case USB_FUNCTION_CS_WRSS:
  1227. RZA_IO_RegWrite_16(&USB20X.DCPCTR, 1, USB_DCPCTR_CCPL_SHIFT, USB_DCPCTR_CCPL);
  1228. break;
  1229. case USB_FUNCTION_CS_SQER:
  1230. usbx_function_set_pid_stall(USB_FUNCTION_PIPE0);
  1231. break;
  1232. default:
  1233. usbx_function_set_pid_stall(USB_FUNCTION_PIPE0);
  1234. break;
  1235. }
  1236. } else if (
  1237. (int_sts0 & USB_FUNCTION_BITBEMP) &&
  1238. (int_enb0 & USB_FUNCTION_BITBEMP) &&
  1239. (int_sts3 & int_enb4) ) {
  1240. /* ==== BEMP PIPEx ==== */
  1241. usbx_function_BEMPInterrupt(int_sts3, int_enb4, this, epCallback);
  1242. } else if (
  1243. (int_sts0 & USB_FUNCTION_BITBRDY) &&
  1244. (int_enb0 & USB_FUNCTION_BITBRDY) &&
  1245. (int_sts1 & int_enb2) ) {
  1246. /* ==== BRDY PIPEx ==== */
  1247. usbx_function_BRDYInterrupt(int_sts1, int_enb2, this, epCallback);
  1248. } else if (
  1249. (int_sts0 & USB_FUNCTION_BITNRDY) &&
  1250. (int_enb0 & USB_FUNCTION_BITNRDY) &&
  1251. (int_sts2 & int_enb3)) {
  1252. /* ==== NRDY PIPEx ==== */
  1253. usbx_function_NRDYInterrupt(int_sts2, int_enb3, this, epCallback);
  1254. } else {
  1255. /* Do Nothing */
  1256. }
  1257. /* Three dummy reads for cleearing interrupt requests */
  1258. dumy_sts = USB20X.INTSTS0;
  1259. dumy_sts = USB20X.INTSTS1;
  1260. }
  1261. /*************************************************************************/
  1262. #endif
  1263. /*************************************************************************/
  1264. /*EOF*/