Keyboard firmwares for Atmel AVR and Cortex-M
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

usb1_function_dataio.c 108KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932
  1. /*******************************************************************************
  2. * DISCLAIMER
  3. * This software is supplied by Renesas Electronics Corporation and is only
  4. * intended for use with Renesas products. No other uses are authorized. This
  5. * software is owned by Renesas Electronics Corporation and is protected under
  6. * all applicable laws, including copyright laws.
  7. * THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING
  8. * THIS SOFTWARE, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT
  9. * LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
  10. * AND NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED.
  11. * TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY LAW, NEITHER RENESAS
  12. * ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES SHALL BE LIABLE
  13. * FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR
  14. * ANY REASON RELATED TO THIS SOFTWARE, EVEN IF RENESAS OR ITS AFFILIATES HAVE
  15. * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  16. * Renesas reserves the right, without notice, to make changes to this software
  17. * and to discontinue the availability of this software. By using this software,
  18. * you agree to the additional terms and conditions found by accessing the
  19. * following link:
  20. * http://www.renesas.com/disclaimer
  21. * Copyright (C) 2012 - 2014 Renesas Electronics Corporation. All rights reserved.
  22. *******************************************************************************/
  23. /*******************************************************************************
  24. * File Name : usb1_function_dataio.c
  25. * $Rev: 1116 $
  26. * $Date:: 2014-07-09 16:29:19 +0900#$
  27. * Device(s) : RZ/A1H
  28. * Tool-Chain :
  29. * OS : None
  30. * H/W Platform :
  31. * Description : RZ/A1H R7S72100 USB Sample Program
  32. * Operation :
  33. * Limitations :
  34. *******************************************************************************/
  35. /*******************************************************************************
  36. Includes <System Includes> , "Project Includes"
  37. *******************************************************************************/
  38. #include "usb1_function.h"
  39. /*******************************************************************************
  40. Typedef definitions
  41. *******************************************************************************/
  42. /*******************************************************************************
  43. Macro definitions
  44. *******************************************************************************/
  45. /*******************************************************************************
  46. Imported global variables and functions (from other files)
  47. *******************************************************************************/
  48. /*******************************************************************************
  49. Exported global variables and functions (to be accessed by other files)
  50. *******************************************************************************/
  51. /*******************************************************************************
  52. Private global variables and functions
  53. *******************************************************************************/
  54. static uint16_t g_usb1_function_mbw[(USB_FUNCTION_MAX_PIPE_NO + 1)];
  55. static void usb1_function_start_receive_trns_c(uint16_t pipe, uint32_t size, uint8_t *data);
  56. static void usb1_function_start_receive_trns_d0(uint16_t pipe, uint32_t size, uint8_t *data);
  57. static void usb1_function_start_receive_trns_d1(uint16_t pipe, uint32_t size, uint8_t *data);
  58. static void usb1_function_start_receive_dma_d0(uint16_t pipe, uint32_t size, uint8_t *data);
  59. static void usb1_function_start_receive_dma_d1(uint16_t pipe, uint32_t size, uint8_t *data);
  60. static uint16_t usb1_function_read_dma_d0(uint16_t pipe);
  61. static uint16_t usb1_function_read_dma_d1(uint16_t pipe);
  62. static uint16_t usb1_function_write_dma_d0(uint16_t pipe);
  63. static uint16_t usb1_function_write_dma_d1(uint16_t pipe);
  64. static void usb1_function_read_c_fifo(uint16_t pipe, uint16_t count);
  65. static void usb1_function_write_c_fifo(uint16_t Pipe, uint16_t count);
  66. static void usb1_function_read_d0_fifo(uint16_t pipe, uint16_t count);
  67. static void usb1_function_write_d0_fifo(uint16_t pipe, uint16_t count);
  68. static void usb1_function_read_d1_fifo(uint16_t pipe, uint16_t count);
  69. static void usb1_function_write_d1_fifo(uint16_t pipe, uint16_t count);
  70. static void usb1_function_clear_transaction_counter(uint16_t pipe);
  71. static void usb1_function_set_transaction_counter(uint16_t pipe, uint32_t count);
  72. static uint32_t usb1_function_com_get_dmasize(uint32_t trncount, uint32_t dtptr);
  73. static uint16_t usb1_function_set_dfacc_d0(uint16_t mbw, uint32_t count);
  74. static uint16_t usb1_function_set_dfacc_d1(uint16_t mbw, uint32_t count);
  75. /*******************************************************************************
  76. * Function Name: usb1_function_start_send_transfer
  77. * Description : Starts the USB data communication using pipe specified by the argument.
  78. * Arguments : uint16_t pipe ; Pipe Number
  79. * : uint32_t size ; Data Size
  80. * : uint8_t *data ; Data Address
  81. * Return Value : DEVDRV_USBF_WRITEEND ; Write end
  82. * : DEVDRV_USBF_WRITESHRT ; short data
  83. * : DEVDRV_USBF_WRITING ; Continue of data write
  84. * : DEVDRV_USBF_WRITEDMA ; Write DMA
  85. * : DEVDRV_USBF_FIFOERROR ; FIFO status
  86. *******************************************************************************/
  87. uint16_t usb1_function_start_send_transfer (uint16_t pipe, uint32_t size, uint8_t * data)
  88. {
  89. uint16_t status;
  90. uint16_t usefifo;
  91. uint16_t mbw;
  92. g_usb1_function_data_count[pipe] = size;
  93. g_usb1_function_data_pointer[pipe] = (uint8_t *)data;
  94. g_usb1_function_pipe_status[pipe] = DEVDRV_USBF_PIPE_WAIT;
  95. usb1_function_clear_bemp_sts(pipe);
  96. usb1_function_clear_brdy_sts(pipe);
  97. usb1_function_clear_nrdy_sts(pipe);
  98. mbw = usb1_function_get_mbw(size, (uint32_t)data);
  99. usefifo = (uint16_t)(g_usb1_function_PipeTbl[pipe] & USB_FUNCTION_FIFO_USE);
  100. switch (usefifo)
  101. {
  102. case USB_FUNCTION_D0FIFO_USE:
  103. case USB_FUNCTION_D0FIFO_DMA:
  104. usefifo = USB_FUNCTION_D0USE;
  105. break;
  106. case USB_FUNCTION_D1FIFO_USE:
  107. case USB_FUNCTION_D1FIFO_DMA:
  108. usefifo = USB_FUNCTION_D1USE;
  109. break;
  110. default:
  111. usefifo = USB_FUNCTION_CUSE;
  112. break;
  113. };
  114. usb1_function_set_curpipe(USB_FUNCTION_PIPE0, usefifo, DEVDRV_USBF_NO, mbw);
  115. usb1_function_clear_transaction_counter(pipe);
  116. usb1_function_aclrm(pipe);
  117. status = usb1_function_write_buffer(pipe);
  118. if (status != DEVDRV_USBF_FIFOERROR)
  119. {
  120. usb1_function_set_pid_buf(pipe);
  121. }
  122. return status;
  123. }
  124. /*******************************************************************************
  125. * Function Name: usb1_function_write_buffer
  126. * Description : Writes data in the buffer allocated in the pipe specified by
  127. * : the argument. The FIFO for using is set in the pipe definition table.
  128. * Arguments : uint16_t pipe ; Pipe Number
  129. * Return Value : DEVDRV_USBF_WRITEEND ; Write end
  130. * : DEVDRV_USBF_WRITESHRT ; short data
  131. * : DEVDRV_USBF_WRITING ; Continue of data write
  132. * : DEVDRV_USBF_WRITEDMA ; Write DMA
  133. * : DEVDRV_USBF_FIFOERROR ; FIFO status
  134. *******************************************************************************/
  135. uint16_t usb1_function_write_buffer (uint16_t pipe)
  136. {
  137. uint16_t status;
  138. uint16_t usefifo;
  139. g_usb1_function_PipeIgnore[pipe] = 0;
  140. usefifo = (uint16_t)(g_usb1_function_PipeTbl[pipe] & USB_FUNCTION_FIFO_USE);
  141. switch (usefifo)
  142. {
  143. case USB_FUNCTION_D0FIFO_USE:
  144. status = usb1_function_write_buffer_d0(pipe);
  145. break;
  146. case USB_FUNCTION_D1FIFO_USE:
  147. status = usb1_function_write_buffer_d1(pipe);
  148. break;
  149. case USB_FUNCTION_D0FIFO_DMA:
  150. status = usb1_function_write_dma_d0(pipe);
  151. break;
  152. case USB_FUNCTION_D1FIFO_DMA:
  153. status = usb1_function_write_dma_d1(pipe);
  154. break;
  155. default:
  156. status = usb1_function_write_buffer_c(pipe);
  157. break;
  158. };
  159. switch (status)
  160. {
  161. case DEVDRV_USBF_WRITING: /* Continue of data write */
  162. usb1_function_enable_nrdy_int(pipe); /* Error (NORES or STALL) */
  163. usb1_function_enable_brdy_int(pipe); /* Enable Ready Interrupt */
  164. break;
  165. case DEVDRV_USBF_WRITEEND: /* End of data write */
  166. case DEVDRV_USBF_WRITESHRT: /* End of data write */
  167. usb1_function_disable_brdy_int(pipe); /* Disable Ready Interrupt */
  168. usb1_function_clear_nrdy_sts(pipe);
  169. usb1_function_enable_nrdy_int(pipe); /* Error (NORES or STALL) */
  170. /* for last transfer */
  171. usb1_function_enable_bemp_int(pipe); /* Enable Empty Interrupt */
  172. break;
  173. case DEVDRV_USBF_WRITEDMA: /* DMA write */
  174. usb1_function_clear_nrdy_sts(pipe);
  175. usb1_function_enable_nrdy_int(pipe); /* Error (NORES or STALL) */
  176. break;
  177. case DEVDRV_USBF_FIFOERROR: /* FIFO access status */
  178. default:
  179. usb1_function_disable_brdy_int(pipe); /* Disable Ready Interrupt */
  180. usb1_function_disable_bemp_int(pipe); /* Disable Empty Interrupt */
  181. g_usb1_function_pipe_status[pipe] = DEVDRV_USBF_FIFOERROR;
  182. break;
  183. }
  184. return status; /* End or Err or Continue */
  185. }
  186. /*******************************************************************************
  187. * Function Name: usb1_function_write_buffer_c
  188. * Description : Writes data in the buffer allocated in the pipe specified in
  189. * : the argument. Writes data by CPU transfer using CFIFO.
  190. * Arguments : uint16_t pipe ; Pipe Number
  191. * Return Value : DEVDRV_USBF_WRITEEND ; Write end
  192. * : DEVDRV_USBF_WRITESHRT ; short data
  193. * : DEVDRV_USBF_WRITING ; Continue of data write
  194. * : DEVDRV_USBF_WRITEDMA ; Write DMA
  195. * : DEVDRV_USBF_FIFOERROR ; FIFO status
  196. *******************************************************************************/
  197. uint16_t usb1_function_write_buffer_c (uint16_t pipe)
  198. {
  199. uint32_t count;
  200. uint16_t size;
  201. uint16_t buffer;
  202. uint16_t mxps;
  203. uint16_t status;
  204. uint16_t mbw;
  205. if (g_usb1_function_CtrZeroLengthFlag == 1)
  206. {
  207. g_usb1_function_CtrZeroLengthFlag = 0; /* Zero Length Packet Flag CLR */
  208. return DEVDRV_USBF_WRITEEND;
  209. }
  210. mbw = usb1_function_get_mbw(g_usb1_function_data_count[pipe], (uint32_t)g_usb1_function_data_pointer[pipe]);
  211. if (pipe == USB_FUNCTION_PIPE0)
  212. {
  213. buffer = usb1_function_change_fifo_port(pipe, USB_FUNCTION_CUSE, USB_FUNCTION_CFIFO_WRITE, mbw);
  214. }
  215. else
  216. {
  217. buffer = usb1_function_change_fifo_port(pipe, USB_FUNCTION_CUSE, DEVDRV_USBF_NO, mbw);
  218. }
  219. if (buffer == DEVDRV_USBF_FIFOERROR) /* FIFO access status */
  220. {
  221. return DEVDRV_USBF_FIFOERROR;
  222. }
  223. size = usb1_function_get_buf_size(pipe); /* Data buffer size */
  224. mxps = usb1_function_get_mxps(pipe); /* Max Packet Size */
  225. if (g_usb1_function_data_count[pipe] <= (uint32_t)size)
  226. {
  227. status = DEVDRV_USBF_WRITEEND; /* write continues */
  228. count = g_usb1_function_data_count[pipe];
  229. if (count == 0)
  230. {
  231. status = DEVDRV_USBF_WRITESHRT; /* Null Packet is end of write */
  232. }
  233. if ((count % mxps) != 0)
  234. {
  235. status = DEVDRV_USBF_WRITESHRT; /* Short Packet is end of write */
  236. }
  237. }
  238. else
  239. {
  240. status = DEVDRV_USBF_WRITING; /* write continues */
  241. count = (uint32_t)size;
  242. }
  243. usb1_function_write_c_fifo(pipe, (uint16_t)count);
  244. if (g_usb1_function_data_count[pipe] < (uint32_t)size)
  245. {
  246. g_usb1_function_data_count[pipe] = 0;
  247. if (RZA_IO_RegRead_16(&USB201.CFIFOCTR, USB_CFIFOCTR_BVAL_SHIFT, USB_CFIFOCTR_BVAL) == 0)
  248. {
  249. USB201.CFIFOCTR = USB_FUNCTION_BITBVAL; /* Short Packet */
  250. g_usb1_function_CtrZeroLengthFlag = 1; /* Zero Length Packet Flag */
  251. }
  252. }
  253. else
  254. {
  255. g_usb1_function_data_count[pipe] -= count;
  256. }
  257. return status; /* End or Err or Continue */
  258. }
  259. /*******************************************************************************
  260. * Function Name: usb1_function_write_buffer_d0
  261. * Description : Writes data in the buffer allocated in the pipe specified in the argument.
  262. * : Writes data by CPU transfer using D0FIFO.
  263. * Arguments : uint16_t pipe ; Pipe Number
  264. * Return Value : DEVDRV_USBF_WRITEEND ; Write end
  265. * : DEVDRV_USBF_WRITESHRT ; short data
  266. * : DEVDRV_USBF_WRITING ; Continue of data write
  267. * : DEVDRV_USBF_WRITEDMA ; Write DMA
  268. * : DEVDRV_USBF_FIFOERROR ; FIFO status
  269. *******************************************************************************/
  270. uint16_t usb1_function_write_buffer_d0 (uint16_t pipe)
  271. {
  272. uint32_t count;
  273. uint16_t size;
  274. uint16_t buffer;
  275. uint16_t mxps;
  276. uint16_t status;
  277. uint16_t mbw;
  278. mbw = usb1_function_get_mbw(g_usb1_function_data_count[pipe], (uint32_t)g_usb1_function_data_pointer[pipe]);
  279. buffer = usb1_function_change_fifo_port(pipe, USB_FUNCTION_D0USE, DEVDRV_USBF_NO, mbw);
  280. if (buffer == DEVDRV_USBF_FIFOERROR) /* FIFO access status */
  281. {
  282. return DEVDRV_USBF_FIFOERROR;
  283. }
  284. size = usb1_function_get_buf_size(pipe); /* Data buffer size */
  285. mxps = usb1_function_get_mxps(pipe); /* Max Packet Size */
  286. if (g_usb1_function_data_count[pipe] <= (uint32_t)size)
  287. {
  288. status = DEVDRV_USBF_WRITEEND; /* write continues */
  289. count = g_usb1_function_data_count[pipe];
  290. if (count == 0)
  291. {
  292. status = DEVDRV_USBF_WRITESHRT; /* Null Packet is end of write */
  293. }
  294. if ((count % mxps) != 0)
  295. {
  296. status = DEVDRV_USBF_WRITESHRT; /* Short Packet is end of write */
  297. }
  298. }
  299. else
  300. {
  301. status = DEVDRV_USBF_WRITING; /* write continues */
  302. count = (uint32_t)size;
  303. }
  304. usb1_function_write_d0_fifo(pipe, (uint16_t)count);
  305. if (g_usb1_function_data_count[pipe] < (uint32_t)size)
  306. {
  307. g_usb1_function_data_count[pipe] = 0;
  308. if (RZA_IO_RegRead_16(&USB201.D0FIFOCTR, USB_DnFIFOCTR_BVAL_SHIFT, USB_DnFIFOCTR_BVAL) == 0)
  309. {
  310. USB201.D0FIFOCTR = USB_FUNCTION_BITBVAL; /* Short Packet */
  311. }
  312. }
  313. else
  314. {
  315. g_usb1_function_data_count[pipe] -= count;
  316. }
  317. return status; /* End or Err or Continue */
  318. }
  319. /*******************************************************************************
  320. * Function Name: usb1_function_write_buffer_d1
  321. * Description : Writes data in the buffer allocated in the pipe specified in the argument.
  322. * : Writes data by CPU transfer using D1FIFO.
  323. * Arguments : uint16_t pipe ; Pipe Number
  324. * Return Value : DEVDRV_USBF_WRITEEND ; Write end
  325. * : DEVDRV_USBF_WRITESHRT ; short data
  326. * : DEVDRV_USBF_WRITING ; Continue of data write
  327. * : DEVDRV_USBF_WRITEDMA ; Write DMA
  328. * : DEVDRV_USBF_FIFOERROR ; FIFO status
  329. *******************************************************************************/
  330. uint16_t usb1_function_write_buffer_d1 (uint16_t pipe)
  331. {
  332. uint32_t count;
  333. uint16_t size;
  334. uint16_t buffer;
  335. uint16_t mxps;
  336. uint16_t status;
  337. uint16_t mbw;
  338. mbw = usb1_function_get_mbw(g_usb1_function_data_count[pipe], (uint32_t)g_usb1_function_data_pointer[pipe]);
  339. buffer = usb1_function_change_fifo_port(pipe, USB_FUNCTION_D1USE, DEVDRV_USBF_NO, mbw);
  340. if (buffer == DEVDRV_USBF_FIFOERROR) /* FIFO access status */
  341. {
  342. return DEVDRV_USBF_FIFOERROR;
  343. }
  344. size = usb1_function_get_buf_size(pipe); /* Data buffer size */
  345. mxps = usb1_function_get_mxps(pipe); /* Max Packet Size */
  346. if (g_usb1_function_data_count[pipe] <= (uint32_t)size)
  347. {
  348. status = DEVDRV_USBF_WRITEEND; /* write continues */
  349. count = g_usb1_function_data_count[pipe];
  350. if (count == 0)
  351. {
  352. status = DEVDRV_USBF_WRITESHRT; /* Null Packet is end of write */
  353. }
  354. if ((count % mxps) != 0)
  355. {
  356. status = DEVDRV_USBF_WRITESHRT; /* Short Packet is end of write */
  357. }
  358. }
  359. else
  360. {
  361. status = DEVDRV_USBF_WRITING; /* write continues */
  362. count = (uint32_t)size;
  363. }
  364. usb1_function_write_d1_fifo(pipe, (uint16_t)count);
  365. if (g_usb1_function_data_count[pipe] < (uint32_t)size)
  366. {
  367. g_usb1_function_data_count[pipe] = 0;
  368. if (RZA_IO_RegRead_16(&USB201.D1FIFOCTR, USB_DnFIFOCTR_BVAL_SHIFT, USB_DnFIFOCTR_BVAL) == 0)
  369. {
  370. USB201.D1FIFOCTR = USB_FUNCTION_BITBVAL; /* Short Packet */
  371. }
  372. }
  373. else
  374. {
  375. g_usb1_function_data_count[pipe] -= count;
  376. }
  377. return status; /* End or Err or Continue */
  378. }
  379. /*******************************************************************************
  380. * Function Name: usb1_function_write_dma_d0
  381. * Description : Writes data in the buffer allocated in the pipe specified in the argument.
  382. * : Writes data by DMA transfer using D0FIFO.
  383. * : The DMA-ch for using is specified by Userdef_USB_usb1_function_start_dma().
  384. * Arguments : uint16_t pipe ; Pipe Number
  385. * Return Value : DEVDRV_USBF_WRITEEND : Write end
  386. * : DEVDRV_USBF_WRITESHRT : short data
  387. * : DEVDRV_USBF_WRITING : Continue of data write
  388. * : DEVDRV_USBF_WRITEDMA : Write DMA
  389. * : DEVDRV_USBF_FIFOERROR : FIFO status
  390. *******************************************************************************/
  391. static uint16_t usb1_function_write_dma_d0 (uint16_t pipe)
  392. {
  393. uint32_t count;
  394. uint16_t size;
  395. uint16_t buffer;
  396. uint16_t status;
  397. uint16_t mbw;
  398. uint16_t dfacc = 0;
  399. mbw = usb1_function_get_mbw(g_usb1_function_data_count[pipe], (uint32_t)g_usb1_function_data_pointer[pipe]);
  400. buffer = usb1_function_change_fifo_port(pipe, USB_FUNCTION_D0DMA, DEVDRV_USBF_NO, mbw);
  401. if (buffer == DEVDRV_USBF_FIFOERROR) /* FIFO access status */
  402. {
  403. return DEVDRV_USBF_FIFOERROR;
  404. }
  405. size = usb1_function_get_buf_size(pipe); /* Data buffer size */
  406. count = g_usb1_function_data_count[pipe];
  407. if (count != 0)
  408. {
  409. g_usb1_function_DmaPipe[USB_FUNCTION_D0FIFO] = pipe;
  410. if ((count % size) != 0)
  411. {
  412. g_usb1_function_DmaBval[USB_FUNCTION_D0FIFO] = 1;
  413. }
  414. else
  415. {
  416. g_usb1_function_DmaBval[USB_FUNCTION_D0FIFO] = 0;
  417. }
  418. dfacc = usb1_function_set_dfacc_d0(mbw, count);
  419. if (mbw == USB_FUNCTION_BITMBW_32)
  420. {
  421. g_usb1_function_DmaInfo[USB_FUNCTION_D0FIFO].size = 2; /* 32bit transfer */
  422. }
  423. else if (mbw == USB_FUNCTION_BITMBW_16)
  424. {
  425. g_usb1_function_DmaInfo[USB_FUNCTION_D0FIFO].size = 1; /* 16bit transfer */
  426. }
  427. else
  428. {
  429. g_usb1_function_DmaInfo[USB_FUNCTION_D0FIFO].size = 0; /* 8bit transfer */
  430. }
  431. g_usb1_function_DmaInfo[USB_FUNCTION_D0FIFO].fifo = USB_FUNCTION_D0FIFO_DMA;
  432. g_usb1_function_DmaInfo[USB_FUNCTION_D0FIFO].dir = USB_FUNCTION_BUF2FIFO;
  433. g_usb1_function_DmaInfo[USB_FUNCTION_D0FIFO].buffer = (uint32_t)g_usb1_function_data_pointer[pipe];
  434. g_usb1_function_DmaInfo[USB_FUNCTION_D0FIFO].bytes = count;
  435. Userdef_USB_usb1_function_start_dma(&g_usb1_function_DmaInfo[USB_FUNCTION_D0FIFO], dfacc);
  436. usb1_function_set_curpipe2(pipe, USB_FUNCTION_D0DMA, DEVDRV_USBF_NO, mbw, dfacc);
  437. RZA_IO_RegWrite_16(&USB201.D0FIFOSEL, 1, USB_DnFIFOSEL_DREQE_SHIFT, USB_DnFIFOSEL_DREQE);
  438. g_usb1_function_data_count[pipe] = 0;
  439. g_usb1_function_data_pointer[pipe] += count;
  440. status = DEVDRV_USBF_WRITEDMA; /* DMA write */
  441. }
  442. else
  443. {
  444. if (RZA_IO_RegRead_16(&USB201.D0FIFOCTR, USB_DnFIFOCTR_BVAL_SHIFT, USB_DnFIFOCTR_BVAL) == 0)
  445. {
  446. RZA_IO_RegWrite_16(&USB201.D0FIFOCTR, 1, USB_DnFIFOCTR_BVAL_SHIFT, USB_DnFIFOCTR_BVAL); /* Short Packet */
  447. }
  448. status = DEVDRV_USBF_WRITESHRT; /* Short Packet is end of write */
  449. }
  450. return status; /* End or Err or Continue */
  451. }
  452. /*******************************************************************************
  453. * Function Name: usb1_function_write_dma_d1
  454. * Description : Writes data in the buffer allocated in the pipe specified in the argument.
  455. * : Writes data by DMA transfer using D1FIFO.
  456. * : The DMA-ch for using is specified by Userdef_USB_usb1_function_start_dma().
  457. * Arguments : uint16_t pipe ; Pipe Number
  458. * Return Value : DEVDRV_USBF_WRITEEND : Write end
  459. * : DEVDRV_USBF_WRITESHRT : short data
  460. * : DEVDRV_USBF_WRITING : Continue of data write
  461. * : DEVDRV_USBF_WRITEDMA : Write DMA
  462. * : DEVDRV_USBF_FIFOERROR : FIFO status
  463. *******************************************************************************/
  464. static uint16_t usb1_function_write_dma_d1 (uint16_t pipe)
  465. {
  466. uint32_t count;
  467. uint16_t size;
  468. uint16_t buffer;
  469. uint16_t status;
  470. uint16_t mbw;
  471. uint16_t dfacc=0;
  472. mbw = usb1_function_get_mbw(g_usb1_function_data_count[pipe], (uint32_t)g_usb1_function_data_pointer[pipe]);
  473. buffer = usb1_function_change_fifo_port(pipe, USB_FUNCTION_D1DMA, DEVDRV_USBF_NO, mbw);
  474. if (buffer == DEVDRV_USBF_FIFOERROR) /* FIFO access status */
  475. {
  476. return DEVDRV_USBF_FIFOERROR;
  477. }
  478. size = usb1_function_get_buf_size(pipe); /* Data buffer size */
  479. count = g_usb1_function_data_count[pipe];
  480. if (count != 0)
  481. {
  482. g_usb1_function_DmaPipe[USB_FUNCTION_D1FIFO] = pipe;
  483. if ((count % size) != 0)
  484. {
  485. g_usb1_function_DmaBval[USB_FUNCTION_D1FIFO] = 1;
  486. }
  487. else
  488. {
  489. g_usb1_function_DmaBval[USB_FUNCTION_D1FIFO] = 0;
  490. }
  491. dfacc = usb1_function_set_dfacc_d1(mbw, count);
  492. if (mbw == USB_FUNCTION_BITMBW_32)
  493. {
  494. g_usb1_function_DmaInfo[USB_FUNCTION_D1FIFO].size = 2; /* 32bit transfer */
  495. }
  496. else if (mbw == USB_FUNCTION_BITMBW_16)
  497. {
  498. g_usb1_function_DmaInfo[USB_FUNCTION_D1FIFO].size = 1; /* 16bit transfer */
  499. }
  500. else
  501. {
  502. g_usb1_function_DmaInfo[USB_FUNCTION_D1FIFO].size = 0; /* 8bit transfer */
  503. }
  504. g_usb1_function_DmaInfo[USB_FUNCTION_D1FIFO].fifo = USB_FUNCTION_D1FIFO_DMA;
  505. g_usb1_function_DmaInfo[USB_FUNCTION_D1FIFO].dir = USB_FUNCTION_BUF2FIFO;
  506. g_usb1_function_DmaInfo[USB_FUNCTION_D1FIFO].buffer = (uint32_t)g_usb1_function_data_pointer[pipe];
  507. g_usb1_function_DmaInfo[USB_FUNCTION_D1FIFO].bytes = count;
  508. Userdef_USB_usb1_function_start_dma(&g_usb1_function_DmaInfo[USB_FUNCTION_D1FIFO], dfacc);
  509. usb1_function_set_curpipe2(pipe, USB_FUNCTION_D1DMA, DEVDRV_USBF_NO, mbw, dfacc);
  510. RZA_IO_RegWrite_16(&USB201.D1FIFOSEL, 1, USB_DnFIFOSEL_DREQE_SHIFT, USB_DnFIFOSEL_DREQE);
  511. g_usb1_function_data_count[pipe] = 0;
  512. g_usb1_function_data_pointer[pipe] += count;
  513. status = DEVDRV_USBF_WRITEDMA; /* DMA write */
  514. }
  515. else
  516. {
  517. if (RZA_IO_RegRead_16(&USB201.D1FIFOCTR, USB_DnFIFOCTR_BVAL_SHIFT, USB_DnFIFOCTR_BVAL) == 0)
  518. {
  519. RZA_IO_RegWrite_16(&USB201.D1FIFOCTR, 1, USB_DnFIFOCTR_BVAL_SHIFT, USB_DnFIFOCTR_BVAL); /* Short Packet */
  520. }
  521. status = DEVDRV_USBF_WRITESHRT; /* Short Packet is end of write */
  522. }
  523. return status; /* End or Err or Continue */
  524. }
  525. /*******************************************************************************
  526. * Function Name: usb1_function_start_receive_transfer
  527. * Description : Starts USB data reception using the pipe specified in the argument.
  528. * : The FIFO for using is set in the pipe definition table.
  529. * Arguments : uint16_t pipe ; Pipe Number
  530. * : uint32_t size ; Data Size
  531. * : uint8_t *data ; Data Address
  532. * Return Value : none
  533. *******************************************************************************/
  534. void usb1_function_start_receive_transfer (uint16_t pipe, uint32_t size, uint8_t * data)
  535. {
  536. uint16_t usefifo;
  537. usb1_function_clear_bemp_sts(pipe);
  538. usb1_function_clear_brdy_sts(pipe);
  539. usb1_function_clear_nrdy_sts(pipe);
  540. usefifo = (uint16_t)(g_usb1_function_PipeTbl[pipe] & USB_FUNCTION_FIFO_USE);
  541. switch (usefifo)
  542. {
  543. case USB_FUNCTION_D0FIFO_USE:
  544. usb1_function_start_receive_trns_d0(pipe, size, data);
  545. break;
  546. case USB_FUNCTION_D1FIFO_USE:
  547. usb1_function_start_receive_trns_d1(pipe, size, data);
  548. break;
  549. case USB_FUNCTION_D0FIFO_DMA:
  550. usb1_function_start_receive_dma_d0(pipe, size, data);
  551. break;
  552. case USB_FUNCTION_D1FIFO_DMA:
  553. usb1_function_start_receive_dma_d1(pipe, size, data);
  554. break;
  555. default:
  556. usb1_function_start_receive_trns_c(pipe, size, data);
  557. break;
  558. }
  559. }
  560. /*******************************************************************************
  561. * Function Name: usb1_function_start_receive_trns_c
  562. * Description : Reads data from the buffer allocated in the pipe specified in the argument.
  563. * : Reads data by CPU transfer using CFIFO.
  564. * : When storing data in the buffer allocated in the pipe specified in the
  565. * : argument, BRDY interrupt is generated to read data
  566. * : in the interrupt.
  567. * Arguments : uint16_t pipe ; Pipe Number
  568. * : uint32_t size ; Data Size
  569. * : uint8_t *data ; Data Address
  570. * Return Value : none
  571. *******************************************************************************/
  572. static void usb1_function_start_receive_trns_c (uint16_t pipe, uint32_t size, uint8_t * data)
  573. {
  574. uint16_t mbw;
  575. usb1_function_set_pid_nak(pipe);
  576. g_usb1_function_data_count[pipe] = size;
  577. g_usb1_function_data_pointer[pipe] = (uint8_t *)data;
  578. g_usb1_function_PipeIgnore[pipe] = 0;
  579. g_usb1_function_PipeDataSize[pipe] = size;
  580. g_usb1_function_pipe_status[pipe] = DEVDRV_USBF_PIPE_WAIT;
  581. mbw = usb1_function_get_mbw(size, (uint32_t)data);
  582. usb1_function_set_curpipe(USB_FUNCTION_PIPE0, USB_FUNCTION_CUSE, USB_FUNCTION_CFIFO_READ, mbw);
  583. USB201.CFIFOCTR = USB_FUNCTION_BITBCLR;
  584. usb1_function_set_transaction_counter(pipe, size);
  585. usb1_function_aclrm(pipe);
  586. usb1_function_enable_nrdy_int(pipe);
  587. usb1_function_enable_brdy_int(pipe);
  588. usb1_function_set_pid_buf(pipe);
  589. }
  590. /*******************************************************************************
  591. * Function Name: usb1_function_start_receive_trns_d0
  592. * Description : Reads data from the buffer allocated in the pipe specified in the argument.
  593. * : Reads data by CPU transfer using D0FIFO.
  594. * : This function does not read data from the buffer.
  595. * : When storing data in the buffer allocated in the pipe specified
  596. * : in the argument, BRDY interrupt is generated to read data in the
  597. * : interrupt.
  598. * Arguments : uint16_t pipe ; Pipe Number
  599. * : uint32_t size ; Data Size
  600. * : uint8_t *data ; Data Address
  601. * Return Value : none
  602. *******************************************************************************/
  603. static void usb1_function_start_receive_trns_d0 (uint16_t pipe, uint32_t size, uint8_t * data)
  604. {
  605. uint16_t mbw;
  606. usb1_function_set_pid_nak(pipe);
  607. g_usb1_function_data_count[pipe] = size;
  608. g_usb1_function_data_pointer[pipe] = (uint8_t *)data;
  609. g_usb1_function_PipeIgnore[pipe] = 0;
  610. g_usb1_function_PipeDataSize[pipe] = size;
  611. g_usb1_function_pipe_status[pipe] = DEVDRV_USBF_PIPE_WAIT;
  612. mbw = usb1_function_get_mbw(size, (uint32_t)data);
  613. usb1_function_set_curpipe(USB_FUNCTION_PIPE0, USB_FUNCTION_D0USE, DEVDRV_USBF_NO, mbw);
  614. usb1_function_set_transaction_counter(pipe, size);
  615. usb1_function_aclrm(pipe);
  616. usb1_function_enable_nrdy_int(pipe);
  617. usb1_function_enable_brdy_int(pipe);
  618. usb1_function_set_pid_buf(pipe);
  619. }
  620. /*******************************************************************************
  621. * Function Name: usb1_function_start_receive_trns_d1
  622. * Description : Reads data from the buffer allocated in the pipe specified in the argument.
  623. * : Reads data by CPU transfer using D1FIFO.
  624. * : This function does not read data from the buffer.
  625. * : When storing data in the buffer allocated in the pipe specified
  626. * : in the argument, BRDY interrupt is generated to read data.
  627. * Arguments : uint16_t pipe ; Pipe Number
  628. * : uint32_t size ; Data Size
  629. * : uint8_t *data ; Data Address
  630. * Return Value : none
  631. *******************************************************************************/
  632. static void usb1_function_start_receive_trns_d1 (uint16_t pipe, uint32_t size, uint8_t * data)
  633. {
  634. uint16_t mbw;
  635. usb1_function_set_pid_nak(pipe);
  636. g_usb1_function_data_count[pipe] = size;
  637. g_usb1_function_data_pointer[pipe] = (uint8_t *)data;
  638. g_usb1_function_PipeIgnore[pipe] = 0;
  639. g_usb1_function_PipeDataSize[pipe] = size;
  640. g_usb1_function_pipe_status[pipe] = DEVDRV_USBF_PIPE_WAIT;
  641. mbw = usb1_function_get_mbw(size, (uint32_t)data);
  642. usb1_function_set_curpipe(USB_FUNCTION_PIPE0, USB_FUNCTION_D1USE, DEVDRV_USBF_NO, mbw);
  643. usb1_function_set_transaction_counter(pipe, size);
  644. usb1_function_aclrm(pipe);
  645. usb1_function_enable_nrdy_int(pipe);
  646. usb1_function_enable_brdy_int(pipe);
  647. usb1_function_set_pid_buf(pipe);
  648. }
  649. /*******************************************************************************
  650. * Function Name: usb1_function_start_receive_dma_d0
  651. * Description : Reads data from the buffer allocated in the pipe specified in the argument.
  652. * : Reads data by DMA transfer using D0FIFO.
  653. * : This function does not read data from the buffer.
  654. * : When storing data in the buffer allocated in the pipe specified
  655. * : in the argument, delivered read request to DMAC to read data from
  656. * : the buffer by DMAC.
  657. * Arguments : uint16_t pipe ; Pipe Number
  658. * : uint32_t size ; Data Size
  659. * : uint8_t *data ; Data Address
  660. * Return Value : none
  661. *******************************************************************************/
  662. static void usb1_function_start_receive_dma_d0 (uint16_t pipe, uint32_t size, uint8_t * data)
  663. {
  664. uint16_t mbw;
  665. usb1_function_set_pid_nak(pipe);
  666. g_usb1_function_data_count[pipe] = size;
  667. g_usb1_function_data_pointer[pipe] = (uint8_t *)data;
  668. g_usb1_function_PipeIgnore[pipe] = 0;
  669. g_usb1_function_PipeDataSize[pipe] = 0;
  670. g_usb1_function_pipe_status[pipe] = DEVDRV_USBF_PIPE_WAIT;
  671. mbw = usb1_function_get_mbw(size, (uint32_t)data);
  672. usb1_function_set_curpipe(USB_FUNCTION_PIPE0, USB_FUNCTION_D0USE, DEVDRV_USBF_NO, mbw);
  673. usb1_function_set_transaction_counter(pipe, size);
  674. usb1_function_aclrm(pipe);
  675. if (RZA_IO_RegRead_16(&g_usb1_function_pipecfg[pipe], USB_PIPECFG_BFRE_SHIFT, USB_PIPECFG_BFRE) == 1)
  676. {
  677. usb1_function_read_dma(pipe);
  678. usb1_function_enable_nrdy_int(pipe);
  679. usb1_function_enable_brdy_int(pipe);
  680. }
  681. else
  682. {
  683. usb1_function_enable_nrdy_int(pipe);
  684. usb1_function_enable_brdy_int(pipe);
  685. }
  686. usb1_function_set_pid_buf(pipe);
  687. }
  688. /*******************************************************************************
  689. * Function Name: usb1_function_start_receive_dma_d1
  690. * Description : Read data from the buffer allocated in the pipe specified in the argument.
  691. * : Reads data by DMA transfer using D0FIFO.
  692. * : This function does not read data from the buffer.
  693. * : When storing data in the buffer allocated in the pipe specified
  694. * : in the argument, delivered read request to DMAC to read data from
  695. * : the buffer by DMAC.
  696. * Arguments : uint16_t pipe ; Pipe Number
  697. * : uint32_t size ; Data Size
  698. * : uint8_t *data ; Data Address
  699. * Return Value : none
  700. *******************************************************************************/
  701. static void usb1_function_start_receive_dma_d1 (uint16_t pipe, uint32_t size, uint8_t * data)
  702. {
  703. uint16_t mbw;
  704. usb1_function_set_pid_nak(pipe);
  705. g_usb1_function_data_count[pipe] = size;
  706. g_usb1_function_data_pointer[pipe] = (uint8_t *)data;
  707. g_usb1_function_PipeIgnore[pipe] = 0;
  708. g_usb1_function_PipeDataSize[pipe] = 0;
  709. g_usb1_function_pipe_status[pipe] = DEVDRV_USBF_PIPE_WAIT;
  710. mbw = usb1_function_get_mbw(size, (uint32_t)data);
  711. usb1_function_set_curpipe(USB_FUNCTION_PIPE0, USB_FUNCTION_D1USE, DEVDRV_USBF_NO, mbw);
  712. usb1_function_set_transaction_counter(pipe, size);
  713. usb1_function_aclrm(pipe);
  714. if (RZA_IO_RegRead_16(&g_usb1_function_pipecfg[pipe], USB_PIPECFG_BFRE_SHIFT, USB_PIPECFG_BFRE) == 1)
  715. {
  716. usb1_function_read_dma(pipe);
  717. usb1_function_enable_nrdy_int(pipe);
  718. usb1_function_enable_brdy_int(pipe);
  719. }
  720. else
  721. {
  722. usb1_function_enable_nrdy_int(pipe);
  723. usb1_function_enable_brdy_int(pipe);
  724. }
  725. usb1_function_set_pid_buf(pipe);
  726. }
  727. /*******************************************************************************
  728. * Function Name: usb1_function_read_buffer
  729. * Description : Reads data from the buffer allocated in the pipe specified
  730. * : in the argument.
  731. * : Uses FIF0 set in the pipe definition table.
  732. * Arguments : uint16_t pipe ; Pipe Number
  733. * Return Value : USB_FUNCTION_READEND ; Read end
  734. * : USB_FUNCTION_READSHRT ; short data
  735. * : USB_FUNCTION_READING ; Continue of data read
  736. * : USB_FUNCTION_READOVER ; buffer over
  737. * : DEVDRV_USBF_FIFOERROR ; FIFO status
  738. *******************************************************************************/
  739. uint16_t usb1_function_read_buffer (uint16_t pipe)
  740. {
  741. uint16_t status;
  742. g_usb1_function_PipeIgnore[pipe] = 0;
  743. if ((g_usb1_function_PipeTbl[pipe] & USB_FUNCTION_FIFO_USE) == USB_FUNCTION_D0FIFO_USE)
  744. {
  745. status = usb1_function_read_buffer_d0(pipe);
  746. }
  747. else if ((g_usb1_function_PipeTbl[pipe] & USB_FUNCTION_FIFO_USE) == USB_FUNCTION_D1FIFO_USE)
  748. {
  749. status = usb1_function_read_buffer_d1(pipe);
  750. }
  751. else
  752. {
  753. status = usb1_function_read_buffer_c(pipe);
  754. }
  755. switch (status)
  756. {
  757. case USB_FUNCTION_READING: /* Continue of data read */
  758. break;
  759. case USB_FUNCTION_READEND: /* End of data read */
  760. case USB_FUNCTION_READSHRT: /* End of data read */
  761. usb1_function_disable_brdy_int(pipe);
  762. g_usb1_function_PipeDataSize[pipe] -= g_usb1_function_data_count[pipe];
  763. g_usb1_function_pipe_status[pipe] = DEVDRV_USBF_PIPE_DONE;
  764. break;
  765. case USB_FUNCTION_READOVER: /* buffer over */
  766. if ((g_usb1_function_PipeTbl[pipe] & USB_FUNCTION_FIFO_USE) == USB_FUNCTION_D0FIFO_USE)
  767. {
  768. USB201.D0FIFOCTR = USB_FUNCTION_BITBCLR; /* Clear BCLR */
  769. }
  770. else if ((g_usb1_function_PipeTbl[pipe] & USB_FUNCTION_FIFO_USE) == USB_FUNCTION_D1FIFO_USE)
  771. {
  772. USB201.D1FIFOCTR = USB_FUNCTION_BITBCLR; /* Clear BCLR */
  773. }
  774. else
  775. {
  776. USB201.CFIFOCTR = USB_FUNCTION_BITBCLR; /* Clear BCLR */
  777. }
  778. usb1_function_disable_brdy_int(pipe); /* Disable Ready Interrupt */
  779. g_usb1_function_PipeDataSize[pipe] -= g_usb1_function_data_count[pipe];
  780. g_usb1_function_pipe_status[pipe] = DEVDRV_USBF_FIFOERROR;
  781. break;
  782. case DEVDRV_USBF_FIFOERROR: /* FIFO access status */
  783. default:
  784. usb1_function_disable_brdy_int(pipe); /* Disable Ready Interrupt */
  785. g_usb1_function_pipe_status[pipe] = DEVDRV_USBF_FIFOERROR;
  786. break;
  787. }
  788. return status; /* End or Err or Continue */
  789. }
  790. /*******************************************************************************
  791. * Function Name: usb1_function_read_buffer_c
  792. * Description : Reads data from the buffer allocated in the pipe specified in the argument.
  793. * : Reads data by CPU transfer using CFIFO.
  794. * Arguments : uint16_t pipe ; Pipe Number
  795. * Return Value : USB_FUNCTION_READEND ; Read end
  796. * : USB_FUNCTION_READSHRT ; short data
  797. * : USB_FUNCTION_READING ; Continue of data read
  798. * : USB_FUNCTION_READOVER ; buffer over
  799. * : DEVDRV_USBF_FIFOERROR ; FIFO status
  800. *******************************************************************************/
  801. uint16_t usb1_function_read_buffer_c (uint16_t pipe)
  802. {
  803. uint32_t count;
  804. uint32_t dtln;
  805. uint16_t buffer;
  806. uint16_t mxps;
  807. uint16_t status;
  808. uint16_t mbw;
  809. mbw = usb1_function_get_mbw(g_usb1_function_data_count[pipe], (uint32_t)g_usb1_function_data_pointer[pipe]);
  810. buffer = usb1_function_change_fifo_port(pipe, USB_FUNCTION_CUSE, DEVDRV_USBF_NO, mbw);
  811. if (buffer == DEVDRV_USBF_FIFOERROR) /* FIFO access status */
  812. {
  813. return DEVDRV_USBF_FIFOERROR;
  814. }
  815. dtln = (uint32_t)(buffer & USB_FUNCTION_BITDTLN);
  816. mxps = usb1_function_get_mxps(pipe); /* Max Packet Size */
  817. if (g_usb1_function_data_count[pipe] < dtln) /* Buffer Over ? */
  818. {
  819. status = USB_FUNCTION_READOVER;
  820. usb1_function_set_pid_nak(pipe); /* Set NAK */
  821. count = g_usb1_function_data_count[pipe];
  822. }
  823. else if (g_usb1_function_data_count[pipe] == dtln) /* just Receive Size */
  824. {
  825. status = USB_FUNCTION_READEND;
  826. usb1_function_set_pid_nak(pipe); /* Set NAK */
  827. count = dtln;
  828. if (count == 0)
  829. {
  830. status = USB_FUNCTION_READSHRT; /* Null Packet receive */
  831. }
  832. if ((count % mxps) != 0)
  833. {
  834. status = USB_FUNCTION_READSHRT; /* Short Packet receive */
  835. }
  836. }
  837. else /* continue Receive data */
  838. {
  839. status = USB_FUNCTION_READING;
  840. count = dtln;
  841. if (count == 0)
  842. {
  843. status = USB_FUNCTION_READSHRT; /* Null Packet receive */
  844. usb1_function_set_pid_nak(pipe); /* Set NAK */
  845. }
  846. if ((count % mxps) != 0)
  847. {
  848. status = USB_FUNCTION_READSHRT; /* Short Packet receive */
  849. usb1_function_set_pid_nak(pipe); /* Set NAK */
  850. }
  851. }
  852. if (count == 0) /* 0 length packet */
  853. {
  854. USB201.CFIFOCTR = USB_FUNCTION_BITBCLR; /* Clear BCLR */
  855. }
  856. else
  857. {
  858. usb1_function_read_c_fifo(pipe, (uint16_t)count);
  859. }
  860. g_usb1_function_data_count[pipe] -= count;
  861. return status; /* End or Err or Continue */
  862. }
  863. /*******************************************************************************
  864. * Function Name: usb1_function_read_buffer_d0
  865. * Description : Reads data from the buffer allocated in the pipe specified in
  866. * : the argument.
  867. * : Reads data by CPU transfer using D0FIFO.
  868. * Arguments : uint16_t pipe ; Pipe Number
  869. * Return Value : USB_FUNCTION_READEND ; Read end
  870. * : USB_FUNCTION_READSHRT ; short data
  871. * : USB_FUNCTION_READING ; Continue of data read
  872. * : USB_FUNCTION_READOVER ; buffer over
  873. * : DEVDRV_USBF_FIFOERROR ; FIFO status
  874. *******************************************************************************/
  875. uint16_t usb1_function_read_buffer_d0 (uint16_t pipe)
  876. {
  877. uint32_t count;
  878. uint32_t dtln;
  879. uint16_t buffer;
  880. uint16_t mxps;
  881. uint16_t status;
  882. uint16_t mbw;
  883. uint16_t pipebuf_size;
  884. mbw = usb1_function_get_mbw(g_usb1_function_data_count[pipe], (uint32_t)g_usb1_function_data_pointer[pipe]);
  885. buffer = usb1_function_change_fifo_port(pipe, USB_FUNCTION_D0USE, DEVDRV_USBF_NO, mbw);
  886. if (buffer == DEVDRV_USBF_FIFOERROR) /* FIFO access status */
  887. {
  888. return DEVDRV_USBF_FIFOERROR;
  889. }
  890. dtln = (uint32_t)(buffer & USB_FUNCTION_BITDTLN);
  891. mxps = usb1_function_get_mxps(pipe); /* Max Packet Size */
  892. if (g_usb1_function_data_count[pipe] < dtln) /* Buffer Over ? */
  893. {
  894. status = USB_FUNCTION_READOVER;
  895. usb1_function_set_pid_nak(pipe); /* Set NAK */
  896. count = g_usb1_function_data_count[pipe];
  897. }
  898. else if (g_usb1_function_data_count[pipe] == dtln) /* just Receive Size */
  899. {
  900. status = USB_FUNCTION_READEND;
  901. usb1_function_set_pid_nak(pipe); /* Set NAK */
  902. count = dtln;
  903. if (count == 0)
  904. {
  905. status = USB_FUNCTION_READSHRT; /* Null Packet receive */
  906. }
  907. if ((count % mxps) != 0)
  908. {
  909. status = USB_FUNCTION_READSHRT; /* Short Packet receive */
  910. }
  911. }
  912. else /* continue Receive data */
  913. {
  914. status = USB_FUNCTION_READING;
  915. count = dtln;
  916. if (count == 0)
  917. {
  918. status = USB_FUNCTION_READSHRT; /* Null Packet receive */
  919. usb1_function_set_pid_nak(pipe); /* Set NAK */
  920. }
  921. if ((count % mxps) != 0)
  922. {
  923. status = USB_FUNCTION_READSHRT; /* Short Packet receive */
  924. usb1_function_set_pid_nak(pipe); /* Set NAK */
  925. }
  926. else
  927. {
  928. pipebuf_size = usb1_function_get_buf_size(pipe); /* Data buffer size */
  929. if (count != pipebuf_size)
  930. {
  931. status = USB_FUNCTION_READSHRT; /* Short Packet receive */
  932. usb1_function_set_pid_nak(pipe); /* Set NAK */
  933. }
  934. }
  935. }
  936. if (count == 0) /* 0 length packet */
  937. {
  938. USB201.D0FIFOCTR = USB_FUNCTION_BITBCLR; /* Clear BCLR */
  939. }
  940. else
  941. {
  942. usb1_function_read_d0_fifo(pipe, (uint16_t)count);
  943. }
  944. g_usb1_function_data_count[pipe] -= count;
  945. return status; /* End or Err or Continue */
  946. }
  947. /*******************************************************************************
  948. * Function Name: usb1_function_read_buffer_d1
  949. * Description : Reads data from the buffer allocated in the pipe specified
  950. * : in the argument.
  951. * : Reads data by CPU transfer using D1FIFO.
  952. * Arguments : uint16_t pipe ; Pipe Number
  953. * Return Value : USB_FUNCTION_READEND ; Read end
  954. * : USB_FUNCTION_READSHRT ; short data
  955. * : USB_FUNCTION_READING ; Continue of data read
  956. * : USB_FUNCTION_READOVER ; buffer over
  957. * : DEVDRV_USBF_FIFOERROR ; FIFO status
  958. *******************************************************************************/
  959. uint16_t usb1_function_read_buffer_d1 (uint16_t pipe)
  960. {
  961. uint32_t count;
  962. uint32_t dtln;
  963. uint16_t buffer;
  964. uint16_t mxps;
  965. uint16_t status;
  966. uint16_t mbw;
  967. uint16_t pipebuf_size;
  968. mbw = usb1_function_get_mbw(g_usb1_function_data_count[pipe], (uint32_t)g_usb1_function_data_pointer[pipe]);
  969. buffer = usb1_function_change_fifo_port(pipe, USB_FUNCTION_D1USE, DEVDRV_USBF_NO, mbw);
  970. if (buffer == DEVDRV_USBF_FIFOERROR) /* FIFO access status */
  971. {
  972. return DEVDRV_USBF_FIFOERROR;
  973. }
  974. dtln = (uint32_t)(buffer & USB_FUNCTION_BITDTLN);
  975. mxps = usb1_function_get_mxps(pipe); /* Max Packet Size */
  976. if (g_usb1_function_data_count[pipe] < dtln) /* Buffer Over ? */
  977. {
  978. status = USB_FUNCTION_READOVER;
  979. usb1_function_set_pid_nak(pipe); /* Set NAK */
  980. count = g_usb1_function_data_count[pipe];
  981. }
  982. else if (g_usb1_function_data_count[pipe] == dtln) /* just Receive Size */
  983. {
  984. status = USB_FUNCTION_READEND;
  985. usb1_function_set_pid_nak(pipe); /* Set NAK */
  986. count = dtln;
  987. if (count == 0)
  988. {
  989. status = USB_FUNCTION_READSHRT; /* Null Packet receive */
  990. }
  991. if ((count % mxps) != 0)
  992. {
  993. status = USB_FUNCTION_READSHRT; /* Short Packet receive */
  994. }
  995. }
  996. else /* continue Receive data */
  997. {
  998. status = USB_FUNCTION_READING;
  999. count = dtln;
  1000. if (count == 0)
  1001. {
  1002. status = USB_FUNCTION_READSHRT; /* Null Packet receive */
  1003. usb1_function_set_pid_nak(pipe); /* Set NAK */
  1004. }
  1005. if ((count % mxps) != 0)
  1006. {
  1007. status = USB_FUNCTION_READSHRT; /* Short Packet receive */
  1008. usb1_function_set_pid_nak(pipe); /* Set NAK */
  1009. }
  1010. else
  1011. {
  1012. pipebuf_size = usb1_function_get_buf_size(pipe); /* Data buffer size */
  1013. if (count != pipebuf_size)
  1014. {
  1015. status = USB_FUNCTION_READSHRT; /* Short Packet receive */
  1016. usb1_function_set_pid_nak(pipe); /* Set NAK */
  1017. }
  1018. }
  1019. }
  1020. if (count == 0) /* 0 length packet */
  1021. {
  1022. USB201.D1FIFOCTR = USB_FUNCTION_BITBCLR; /* Clear BCLR */
  1023. }
  1024. else
  1025. {
  1026. usb1_function_read_d1_fifo(pipe, (uint16_t)count);
  1027. }
  1028. g_usb1_function_data_count[pipe] -= count;
  1029. return status; /* End or Err or Continue */
  1030. }
  1031. /*******************************************************************************
  1032. * Function Name: usb1_function_read_dma
  1033. * Description : Reads data from the buffer allocated in the pipe specified
  1034. * : in the argument.
  1035. * : Reads data by DMA transfer using D0FIFO or D1FIFO.
  1036. * Arguments : uint16_t pipe ; Pipe Number
  1037. * Return Value : USB_FUNCTION_READEND ; Read end
  1038. * : USB_FUNCTION_READSHRT ; short data
  1039. * : USB_FUNCTION_READING ; Continue of data read
  1040. * : USB_FUNCTION_READOVER ; buffer over
  1041. * : DEVDRV_USBF_FIFOERROR ; FIFO status
  1042. *******************************************************************************/
  1043. uint16_t usb1_function_read_dma (uint16_t pipe)
  1044. {
  1045. uint16_t status;
  1046. g_usb1_function_PipeIgnore[pipe] = 0;
  1047. if ((g_usb1_function_PipeTbl[pipe] & USB_FUNCTION_FIFO_USE) == USB_FUNCTION_D0FIFO_DMA)
  1048. {
  1049. status = usb1_function_read_dma_d0(pipe);
  1050. }
  1051. else
  1052. {
  1053. status = usb1_function_read_dma_d1(pipe);
  1054. }
  1055. switch (status)
  1056. {
  1057. case USB_FUNCTION_READING: /* Continue of data read */
  1058. break;
  1059. case USB_FUNCTION_READZERO: /* End of data read */
  1060. usb1_function_disable_brdy_int(pipe);
  1061. g_usb1_function_pipe_status[pipe] = DEVDRV_USBF_PIPE_DONE;
  1062. break;
  1063. case USB_FUNCTION_READEND: /* End of data read */
  1064. case USB_FUNCTION_READSHRT: /* End of data read */
  1065. usb1_function_disable_brdy_int(pipe);
  1066. if (RZA_IO_RegRead_16(&g_usb1_function_pipecfg[pipe], USB_PIPECFG_BFRE_SHIFT, USB_PIPECFG_BFRE) == 1)
  1067. {
  1068. g_usb1_function_PipeDataSize[pipe] -= g_usb1_function_data_count[pipe];
  1069. }
  1070. break;
  1071. case USB_FUNCTION_READOVER: /* buffer over */
  1072. usb1_function_disable_brdy_int(pipe); /* Disable Ready Interrupt */
  1073. if (RZA_IO_RegRead_16(&g_usb1_function_pipecfg[pipe], USB_PIPECFG_BFRE_SHIFT, USB_PIPECFG_BFRE) == 1)
  1074. {
  1075. g_usb1_function_PipeDataSize[pipe] -= g_usb1_function_data_count[pipe];
  1076. }
  1077. g_usb1_function_pipe_status[pipe] = DEVDRV_USBF_FIFOERROR;
  1078. break;
  1079. case DEVDRV_USBF_FIFOERROR: /* FIFO access status */
  1080. default:
  1081. usb1_function_disable_brdy_int(pipe); /* Disable Ready Interrupt */
  1082. g_usb1_function_pipe_status[pipe] = DEVDRV_USBF_FIFOERROR;
  1083. break;
  1084. }
  1085. return status; /* End or Err or Continue */
  1086. }
  1087. /*******************************************************************************
  1088. * Function Name: usb1_function_read_dma_d0
  1089. * Description : Writes data in the buffer allocated in the pipe specified
  1090. * : in the argument.
  1091. * : Reads data by DMA transfer using D0FIFO.
  1092. * Arguments : uint16_t pipe ; Pipe Number
  1093. * Return Value : USB_FUNCTION_READEND ; Read end
  1094. * : USB_FUNCTION_READSHRT ; short data
  1095. * : USB_FUNCTION_READZERO ; zero data
  1096. * : USB_FUNCTION_READING ; Continue of data read
  1097. * : USB_FUNCTION_READOVER ; buffer over
  1098. * : DEVDRV_USBF_FIFOERROR ; FIFO status
  1099. *******************************************************************************/
  1100. static uint16_t usb1_function_read_dma_d0 (uint16_t pipe)
  1101. {
  1102. uint32_t count;
  1103. uint32_t dtln;
  1104. uint16_t buffer;
  1105. uint16_t mxps;
  1106. uint16_t status;
  1107. uint16_t mbw;
  1108. uint16_t dfacc = 0;
  1109. uint16_t pipebuf_size;
  1110. g_usb1_function_DmaStatus[USB_FUNCTION_D0FIFO] = USB_FUNCTION_DMA_READY;
  1111. mbw = usb1_function_get_mbw(g_usb1_function_data_count[pipe], (uint32_t)g_usb1_function_data_pointer[pipe]);
  1112. if (RZA_IO_RegRead_16(&g_usb1_function_pipecfg[pipe], USB_PIPECFG_BFRE_SHIFT, USB_PIPECFG_BFRE) == 1)
  1113. {
  1114. count = g_usb1_function_data_count[pipe];
  1115. status = USB_FUNCTION_READING;
  1116. }
  1117. else
  1118. {
  1119. buffer = usb1_function_change_fifo_port(pipe, USB_FUNCTION_D0DMA, DEVDRV_USBF_NO, mbw);
  1120. if (buffer == DEVDRV_USBF_FIFOERROR) /* FIFO access status */
  1121. {
  1122. return DEVDRV_USBF_FIFOERROR;
  1123. }
  1124. dtln = (uint32_t)(buffer & USB_FUNCTION_BITDTLN);
  1125. mxps = usb1_function_get_mxps(pipe); /* Max Packet Size */
  1126. if (g_usb1_function_data_count[pipe] < dtln) /* Buffer Over ? */
  1127. {
  1128. status = USB_FUNCTION_READOVER;
  1129. count = g_usb1_function_data_count[pipe];
  1130. }
  1131. else if (g_usb1_function_data_count[pipe] == dtln) /* just Receive Size */
  1132. {
  1133. status = USB_FUNCTION_READEND;
  1134. count = dtln;
  1135. if (count == 0)
  1136. {
  1137. status = USB_FUNCTION_READSHRT; /* Null Packet receive */
  1138. }
  1139. if ((count % mxps) != 0)
  1140. {
  1141. status = USB_FUNCTION_READSHRT; /* Short Packet receive */
  1142. }
  1143. }
  1144. else /* continue Receive data */
  1145. {
  1146. status = USB_FUNCTION_READING;
  1147. count = dtln;
  1148. if (count == 0)
  1149. {
  1150. status = USB_FUNCTION_READSHRT; /* Null Packet receive */
  1151. }
  1152. if ((count % mxps) != 0)
  1153. {
  1154. status = USB_FUNCTION_READSHRT; /* Short Packet receive */
  1155. }
  1156. else
  1157. {
  1158. pipebuf_size = usb1_function_get_buf_size(pipe); /* Data buffer size */
  1159. if (count != pipebuf_size)
  1160. {
  1161. status = USB_FUNCTION_READSHRT; /* Short Packet receive */
  1162. }
  1163. }
  1164. }
  1165. }
  1166. if (count == 0) /* 0 length packet */
  1167. {
  1168. if (RZA_IO_RegRead_16(&g_usb1_function_pipecfg[pipe], USB_PIPECFG_BFRE_SHIFT, USB_PIPECFG_BFRE) == 0)
  1169. {
  1170. USB201.D0FIFOCTR = USB_FUNCTION_BITBCLR; /* Clear BCLR */
  1171. status = USB_FUNCTION_READZERO; /* Null Packet receive */
  1172. }
  1173. else
  1174. {
  1175. usb1_function_set_curpipe(pipe, USB_FUNCTION_D0DMA, DEVDRV_USBF_NO, mbw);
  1176. /* transaction counter No set */
  1177. /* FRDY = 1, DTLN = 0 -> BRDY */
  1178. }
  1179. }
  1180. else
  1181. {
  1182. dfacc = usb1_function_set_dfacc_d0(mbw, count);
  1183. if (mbw == USB_FUNCTION_BITMBW_32)
  1184. {
  1185. g_usb1_function_DmaInfo[USB_FUNCTION_D0FIFO].size = 2; /* 32bit transfer */
  1186. }
  1187. else if (mbw == USB_FUNCTION_BITMBW_16)
  1188. {
  1189. g_usb1_function_DmaInfo[USB_FUNCTION_D0FIFO].size = 1; /* 16bit transfer */
  1190. }
  1191. else
  1192. {
  1193. g_usb1_function_DmaInfo[USB_FUNCTION_D0FIFO].size = 0; /* 8bit transfer */
  1194. }
  1195. g_usb1_function_DmaPipe[USB_FUNCTION_D0FIFO] = pipe; /* not use in read operation */
  1196. g_usb1_function_DmaBval[USB_FUNCTION_D0FIFO] = 0; /* not use in read operation */
  1197. g_usb1_function_DmaInfo[USB_FUNCTION_D0FIFO].fifo = USB_FUNCTION_D0FIFO_DMA;
  1198. g_usb1_function_DmaInfo[USB_FUNCTION_D0FIFO].dir = USB_FUNCTION_FIFO2BUF;
  1199. g_usb1_function_DmaInfo[USB_FUNCTION_D0FIFO].buffer = (uint32_t)g_usb1_function_data_pointer[pipe];
  1200. g_usb1_function_DmaInfo[USB_FUNCTION_D0FIFO].bytes = count;
  1201. if (status == USB_FUNCTION_READING)
  1202. {
  1203. g_usb1_function_DmaStatus[USB_FUNCTION_D0FIFO] = USB_FUNCTION_DMA_BUSY;
  1204. }
  1205. else
  1206. {
  1207. g_usb1_function_DmaStatus[USB_FUNCTION_D0FIFO] = USB_FUNCTION_DMA_BUSYEND;
  1208. }
  1209. Userdef_USB_usb1_function_start_dma(&g_usb1_function_DmaInfo[USB_FUNCTION_D0FIFO], dfacc);
  1210. usb1_function_set_curpipe2(pipe, USB_FUNCTION_D0DMA, DEVDRV_USBF_NO, mbw, dfacc);
  1211. RZA_IO_RegWrite_16(&USB201.D0FIFOSEL,
  1212. 1,
  1213. USB_DnFIFOSEL_DREQE_SHIFT,
  1214. USB_DnFIFOSEL_DREQE);
  1215. }
  1216. if (RZA_IO_RegRead_16(&g_usb1_function_pipecfg[pipe], USB_PIPECFG_BFRE_SHIFT, USB_PIPECFG_BFRE) == 0)
  1217. {
  1218. g_usb1_function_data_count[pipe] -= count;
  1219. g_usb1_function_data_pointer[pipe] += count;
  1220. g_usb1_function_PipeDataSize[pipe] += count;
  1221. }
  1222. return status; /* End or Err or Continue */
  1223. }
  1224. /*******************************************************************************
  1225. * Function Name: usb1_function_read_dma_d1
  1226. * Description : Reads data from the buffer allocated in the pipe specified in
  1227. * : the argument.
  1228. * : Reads data by DMA transfer using D1FIFO.
  1229. * Arguments : uint16_t pipe ; Pipe Number
  1230. * Return Value : USB_FUNCTION_READEND ; Read end
  1231. * : USB_FUNCTION_READSHRT ; short data
  1232. * : USB_FUNCTION_READZERO ; zero data
  1233. * : USB_FUNCTION_READING ; Continue of data read
  1234. * : USB_FUNCTION_READOVER ; buffer over
  1235. * : DEVDRV_USBF_FIFOERROR ; FIFO status
  1236. *******************************************************************************/
  1237. static uint16_t usb1_function_read_dma_d1 (uint16_t pipe)
  1238. {
  1239. uint32_t count;
  1240. uint32_t dtln;
  1241. uint16_t buffer;
  1242. uint16_t mxps;
  1243. uint16_t status;
  1244. uint16_t mbw;
  1245. uint16_t dfacc = 0;
  1246. uint16_t pipebuf_size;
  1247. g_usb1_function_DmaStatus[USB_FUNCTION_D1FIFO] = USB_FUNCTION_DMA_READY;
  1248. mbw = usb1_function_get_mbw(g_usb1_function_data_count[pipe], (uint32_t)g_usb1_function_data_pointer[pipe]);
  1249. if (RZA_IO_RegRead_16(&g_usb1_function_pipecfg[pipe], USB_PIPECFG_BFRE_SHIFT, USB_PIPECFG_BFRE) == 1)
  1250. {
  1251. count = g_usb1_function_data_count[pipe];
  1252. status = USB_FUNCTION_READING;
  1253. }
  1254. else
  1255. {
  1256. buffer = usb1_function_change_fifo_port(pipe, USB_FUNCTION_D1DMA, DEVDRV_USBF_NO, mbw);
  1257. if (buffer == DEVDRV_USBF_FIFOERROR) /* FIFO access status */
  1258. {
  1259. return DEVDRV_USBF_FIFOERROR;
  1260. }
  1261. dtln = (uint32_t)(buffer & USB_FUNCTION_BITDTLN);
  1262. mxps = usb1_function_get_mxps(pipe); /* Max Packet Size */
  1263. if (g_usb1_function_data_count[pipe] < dtln) /* Buffer Over ? */
  1264. {
  1265. status = USB_FUNCTION_READOVER;
  1266. count = g_usb1_function_data_count[pipe];
  1267. }
  1268. else if (g_usb1_function_data_count[pipe] == dtln) /* just Receive Size */
  1269. {
  1270. status = USB_FUNCTION_READEND;
  1271. count = dtln;
  1272. if (count == 0)
  1273. {
  1274. status = USB_FUNCTION_READSHRT; /* Null Packet receive */
  1275. }
  1276. if ((count % mxps) != 0)
  1277. {
  1278. status = USB_FUNCTION_READSHRT; /* Short Packet receive */
  1279. }
  1280. }
  1281. else /* continue Receive data */
  1282. {
  1283. status = USB_FUNCTION_READING;
  1284. count = dtln;
  1285. if (count == 0)
  1286. {
  1287. status = USB_FUNCTION_READSHRT; /* Null Packet receive */
  1288. }
  1289. if ((count % mxps) != 0)
  1290. {
  1291. status = USB_FUNCTION_READSHRT; /* Short Packet receive */
  1292. }
  1293. else
  1294. {
  1295. pipebuf_size = usb1_function_get_buf_size(pipe); /* Data buffer size */
  1296. if (count != pipebuf_size)
  1297. {
  1298. status = USB_FUNCTION_READSHRT; /* Short Packet receive */
  1299. }
  1300. }
  1301. }
  1302. }
  1303. if (count == 0) /* 0 length packet */
  1304. {
  1305. if (RZA_IO_RegRead_16(&g_usb1_function_pipecfg[pipe], USB_PIPECFG_BFRE_SHIFT, USB_PIPECFG_BFRE) == 0)
  1306. {
  1307. USB201.D1FIFOCTR = USB_FUNCTION_BITBCLR; /* Clear BCLR */
  1308. status = USB_FUNCTION_READZERO; /* Null Packet receive */
  1309. }
  1310. else
  1311. {
  1312. usb1_function_set_curpipe(pipe, USB_FUNCTION_D1DMA, DEVDRV_USBF_NO, mbw);
  1313. /* transaction counter No set */
  1314. /* FRDY = 1, DTLN = 0 -> BRDY */
  1315. }
  1316. }
  1317. else
  1318. {
  1319. dfacc = usb1_function_set_dfacc_d1(mbw, count);
  1320. if (mbw == USB_FUNCTION_BITMBW_32)
  1321. {
  1322. g_usb1_function_DmaInfo[USB_FUNCTION_D1FIFO].size = 2; /* 32bit transfer */
  1323. }
  1324. else if (mbw == USB_FUNCTION_BITMBW_16)
  1325. {
  1326. g_usb1_function_DmaInfo[USB_FUNCTION_D1FIFO].size = 1; /* 16bit transfer */
  1327. }
  1328. else
  1329. {
  1330. g_usb1_function_DmaInfo[USB_FUNCTION_D1FIFO].size = 0; /* 8bit transfer */
  1331. }
  1332. g_usb1_function_DmaPipe[USB_FUNCTION_D1FIFO] = pipe; /* not use in read operation */
  1333. g_usb1_function_DmaBval[USB_FUNCTION_D1FIFO] = 0; /* not use in read operation */
  1334. g_usb1_function_DmaInfo[USB_FUNCTION_D1FIFO].fifo = USB_FUNCTION_D1FIFO_DMA;
  1335. g_usb1_function_DmaInfo[USB_FUNCTION_D1FIFO].dir = USB_FUNCTION_FIFO2BUF;
  1336. g_usb1_function_DmaInfo[USB_FUNCTION_D1FIFO].buffer = (uint32_t)g_usb1_function_data_pointer[pipe];
  1337. g_usb1_function_DmaInfo[USB_FUNCTION_D1FIFO].bytes = count;
  1338. if (status == USB_FUNCTION_READING)
  1339. {
  1340. g_usb1_function_DmaStatus[USB_FUNCTION_D1FIFO] = USB_FUNCTION_DMA_BUSY;
  1341. }
  1342. else
  1343. {
  1344. g_usb1_function_DmaStatus[USB_FUNCTION_D1FIFO] = USB_FUNCTION_DMA_BUSYEND;
  1345. }
  1346. Userdef_USB_usb1_function_start_dma(&g_usb1_function_DmaInfo[USB_FUNCTION_D1FIFO], dfacc);
  1347. usb1_function_set_curpipe2(pipe, USB_FUNCTION_D1DMA, DEVDRV_USBF_NO, mbw, dfacc);
  1348. RZA_IO_RegWrite_16(&USB201.D1FIFOSEL,
  1349. 1,
  1350. USB_DnFIFOSEL_DREQE_SHIFT,
  1351. USB_DnFIFOSEL_DREQE);
  1352. }
  1353. if (RZA_IO_RegRead_16(&g_usb1_function_pipecfg[pipe], USB_PIPECFG_BFRE_SHIFT, USB_PIPECFG_BFRE) == 0)
  1354. {
  1355. g_usb1_function_data_count[pipe] -= count;
  1356. g_usb1_function_data_pointer[pipe] += count;
  1357. g_usb1_function_PipeDataSize[pipe] += count;
  1358. }
  1359. return status; /* End or Err or Continue */
  1360. }
  1361. /*******************************************************************************
  1362. * Function Name: usb1_function_change_fifo_port
  1363. * Description : Allocates FIF0 specified by the argument in the pipe assigned
  1364. * : by the argument. After allocating FIF0, waits in the software
  1365. * : till the corresponding pipe becomes ready.
  1366. * Arguments : uint16_t pipe ; Pipe Number
  1367. * : uint16_t fifosel ; Select FIFO
  1368. * : uint16_t isel ; FIFO Access Direction
  1369. * : uint16_t mbw ; FIFO Port Access Bit Width
  1370. * Return Value : DEVDRV_USBF_FIFOERROR ; Error
  1371. * : Others ; CFIFOCTR/D0FIFOCTR/D1FIFOCTR Register Value
  1372. *******************************************************************************/
  1373. uint16_t usb1_function_change_fifo_port (uint16_t pipe, uint16_t fifosel, uint16_t isel, uint16_t mbw)
  1374. {
  1375. uint16_t buffer;
  1376. uint32_t loop;
  1377. volatile uint32_t loop2;
  1378. usb1_function_set_curpipe(pipe, fifosel, isel, mbw);
  1379. for (loop = 0; loop < 4; loop++)
  1380. {
  1381. switch (fifosel)
  1382. {
  1383. case USB_FUNCTION_CUSE:
  1384. buffer = USB201.CFIFOCTR;
  1385. break;
  1386. case USB_FUNCTION_D0USE:
  1387. case USB_FUNCTION_D0DMA:
  1388. buffer = USB201.D0FIFOCTR;
  1389. break;
  1390. case USB_FUNCTION_D1USE:
  1391. case USB_FUNCTION_D1DMA:
  1392. buffer = USB201.D1FIFOCTR;
  1393. break;
  1394. default:
  1395. buffer = 0;
  1396. break;
  1397. }
  1398. if ((buffer & USB_FUNCTION_BITFRDY) == USB_FUNCTION_BITFRDY)
  1399. {
  1400. return buffer;
  1401. }
  1402. loop2 = 25;
  1403. while (loop2-- > 0)
  1404. {
  1405. /* wait */
  1406. }
  1407. }
  1408. return DEVDRV_USBF_FIFOERROR;
  1409. }
  1410. /*******************************************************************************
  1411. * Function Name: usb1_function_set_curpipe
  1412. * Description : Allocates FIF0 specified by the argument in the pipe assigned
  1413. * : by the argument.
  1414. * Arguments : uint16_t pipe ; Pipe Number
  1415. * : uint16_t fifosel ; Select FIFO
  1416. * : uint16_t isel ; FIFO Access Direction
  1417. * : uint16_t mbw ; FIFO Port Access Bit Width
  1418. * Return Value : none
  1419. *******************************************************************************/
  1420. void usb1_function_set_curpipe (uint16_t pipe, uint16_t fifosel, uint16_t isel, uint16_t mbw)
  1421. {
  1422. uint16_t buffer;
  1423. uint32_t loop;
  1424. volatile uint32_t loop2;
  1425. g_usb1_function_mbw[pipe] = mbw;
  1426. switch (fifosel)
  1427. {
  1428. case USB_FUNCTION_CUSE:
  1429. buffer = USB201.CFIFOSEL;
  1430. buffer &= (uint16_t)~(USB_FUNCTION_BITISEL | USB_FUNCTION_BITCURPIPE);
  1431. buffer |= (uint16_t)(~isel & USB_FUNCTION_BITISEL);
  1432. USB201.CFIFOSEL = buffer;
  1433. for (loop = 0; loop < 4; loop++)
  1434. {
  1435. if ((USB201.CFIFOSEL & (USB_FUNCTION_BITISEL | USB_FUNCTION_BITCURPIPE)) ==
  1436. (buffer & (USB_FUNCTION_BITISEL | USB_FUNCTION_BITCURPIPE)))
  1437. {
  1438. break;
  1439. }
  1440. loop2 = 100;
  1441. while (loop2-- > 0)
  1442. {
  1443. /* wait */
  1444. }
  1445. }
  1446. buffer &= (uint16_t)~(USB_FUNCTION_BITISEL | USB_FUNCTION_BITCURPIPE | USB_FUNCTION_BITMBW);
  1447. buffer |= (uint16_t)(isel | pipe | mbw);
  1448. USB201.CFIFOSEL = buffer;
  1449. for (loop = 0; loop < 4; loop++)
  1450. {
  1451. if ((USB201.CFIFOSEL & (USB_FUNCTION_BITISEL | USB_FUNCTION_BITCURPIPE)) ==
  1452. (buffer & (USB_FUNCTION_BITISEL | USB_FUNCTION_BITCURPIPE)))
  1453. {
  1454. break;
  1455. }
  1456. loop2 = 100;
  1457. while (loop2-- > 0)
  1458. {
  1459. /* wait */
  1460. }
  1461. }
  1462. break;
  1463. case USB_FUNCTION_D0DMA:
  1464. case USB_FUNCTION_D0USE:
  1465. buffer = USB201.D0FIFOSEL;
  1466. buffer &= (uint16_t)~(USB_FUNCTION_BITCURPIPE);
  1467. USB201.D0FIFOSEL = buffer;
  1468. for (loop = 0; loop < 4; loop++)
  1469. {
  1470. if ((USB201.D0FIFOSEL & USB_FUNCTION_BITCURPIPE) ==
  1471. (buffer & USB_FUNCTION_BITCURPIPE))
  1472. {
  1473. break;
  1474. }
  1475. loop2 = 100;
  1476. while (loop2-- > 0)
  1477. {
  1478. /* wait */
  1479. }
  1480. }
  1481. buffer &= (uint16_t)~(USB_FUNCTION_BITCURPIPE | USB_FUNCTION_BITMBW);
  1482. buffer |= (uint16_t)(pipe | mbw);
  1483. USB201.D0FIFOSEL = buffer;
  1484. for (loop = 0; loop < 4; loop++)
  1485. {
  1486. if ((USB201.D0FIFOSEL & USB_FUNCTION_BITCURPIPE) ==
  1487. (buffer & USB_FUNCTION_BITCURPIPE))
  1488. {
  1489. break;
  1490. }
  1491. loop2 = 100;
  1492. while (loop2-- > 0)
  1493. {
  1494. /* wait */
  1495. }
  1496. }
  1497. break;
  1498. case USB_FUNCTION_D1DMA:
  1499. case USB_FUNCTION_D1USE:
  1500. buffer = USB201.D1FIFOSEL;
  1501. buffer &= (uint16_t)~(USB_FUNCTION_BITCURPIPE);
  1502. USB201.D1FIFOSEL = buffer;
  1503. for (loop = 0; loop < 4; loop++)
  1504. {
  1505. if ((USB201.D1FIFOSEL & USB_FUNCTION_BITCURPIPE) ==
  1506. (buffer & USB_FUNCTION_BITCURPIPE))
  1507. {
  1508. break;
  1509. }
  1510. loop2 = 100;
  1511. while (loop2-- > 0)
  1512. {
  1513. /* wait */
  1514. }
  1515. }
  1516. buffer &= (uint16_t)~(USB_FUNCTION_BITCURPIPE | USB_FUNCTION_BITMBW);
  1517. buffer |= (uint16_t)(pipe | mbw);
  1518. USB201.D1FIFOSEL = buffer;
  1519. for (loop = 0; loop < 4; loop++)
  1520. {
  1521. if ((USB201.D1FIFOSEL & USB_FUNCTION_BITCURPIPE) ==
  1522. (buffer & USB_FUNCTION_BITCURPIPE))
  1523. {
  1524. break;
  1525. }
  1526. loop2 = 100;
  1527. while (loop2-- > 0)
  1528. {
  1529. /* wait */
  1530. }
  1531. }
  1532. break;
  1533. default:
  1534. break;
  1535. }
  1536. /* Cautions !!!
  1537. * Depending on the external bus speed of CPU, you may need to wait for 450ns here.
  1538. * For details, please look at the data sheet. */
  1539. loop2 = 100;
  1540. while (loop2-- > 0)
  1541. {
  1542. /* wait */
  1543. }
  1544. }
  1545. /*******************************************************************************
  1546. * Function Name: usb1_function_set_curpipe2
  1547. * Description : Allocates FIF0 specified by the argument in the pipe assigned
  1548. * : by the argument.
  1549. * Arguments : uint16_t pipe ; Pipe Number
  1550. * : uint16_t fifosel ; Select FIFO
  1551. * : uint16_t isel ; FIFO Access Direction
  1552. * : uint16_t mbw ; FIFO Port Access Bit Width
  1553. * : uint16_t dfacc ; DFACC Access mode
  1554. * Return Value : none
  1555. *******************************************************************************/
  1556. void usb1_function_set_curpipe2 (uint16_t pipe, uint16_t fifosel, uint16_t isel, uint16_t mbw, uint16_t dfacc)
  1557. {
  1558. uint16_t buffer;
  1559. uint32_t loop;
  1560. #ifdef __USB_FUNCTION_DF_ACC_ENABLE__
  1561. uint32_t dummy;
  1562. #endif
  1563. volatile uint32_t loop2;
  1564. g_usb1_function_mbw[pipe] = mbw;
  1565. switch (fifosel)
  1566. {
  1567. case USB_FUNCTION_CUSE:
  1568. buffer = USB201.CFIFOSEL;
  1569. buffer &= (uint16_t)~(USB_FUNCTION_BITISEL | USB_FUNCTION_BITCURPIPE);
  1570. buffer |= (uint16_t)(~isel & USB_FUNCTION_BITISEL);
  1571. USB201.CFIFOSEL = buffer;
  1572. for (loop = 0; loop < 4; loop++)
  1573. {
  1574. if ((USB201.CFIFOSEL & (USB_FUNCTION_BITISEL | USB_FUNCTION_BITCURPIPE)) ==
  1575. (buffer & (USB_FUNCTION_BITISEL | USB_FUNCTION_BITCURPIPE)))
  1576. {
  1577. break;
  1578. }
  1579. loop2 = 100;
  1580. while (loop2-- > 0)
  1581. {
  1582. /* wait */
  1583. }
  1584. }
  1585. buffer &= (uint16_t)~(USB_FUNCTION_BITISEL | USB_FUNCTION_BITCURPIPE | USB_FUNCTION_BITMBW);
  1586. buffer |= (uint16_t)(isel | pipe | mbw);
  1587. USB201.CFIFOSEL = buffer;
  1588. for (loop = 0; loop < 4; loop++)
  1589. {
  1590. if ((USB201.CFIFOSEL & (USB_FUNCTION_BITISEL | USB_FUNCTION_BITCURPIPE)) ==
  1591. (buffer & (USB_FUNCTION_BITISEL | USB_FUNCTION_BITCURPIPE)))
  1592. {
  1593. break;
  1594. }
  1595. loop2 = 100;
  1596. while (loop2-- > 0)
  1597. {
  1598. /* wait */
  1599. }
  1600. }
  1601. break;
  1602. case USB_FUNCTION_D0DMA:
  1603. case USB_FUNCTION_D0USE:
  1604. buffer = USB201.D0FIFOSEL;
  1605. #ifdef __USB_FUNCTION_DF_ACC_ENABLE__
  1606. buffer &= (uint16_t)~(USB_FUNCTION_BITCURPIPE | USB_FUNCTION_BITMBW);
  1607. if (dfacc != 0)
  1608. {
  1609. buffer |= (uint16_t)(USB_FUNCTION_BITMBW_32);
  1610. }
  1611. #else
  1612. buffer &= (uint16_t)~(USB_FUNCTION_BITCURPIPE);
  1613. #endif
  1614. USB201.D0FIFOSEL = buffer;
  1615. for (loop = 0; loop < 4; loop++)
  1616. {
  1617. if ((USB201.D0FIFOSEL & USB_FUNCTION_BITCURPIPE) == (buffer & USB_FUNCTION_BITCURPIPE))
  1618. {
  1619. break;
  1620. }
  1621. loop2 = 100;
  1622. while (loop2-- > 0)
  1623. {
  1624. /* wait */
  1625. }
  1626. }
  1627. #ifdef __USB_FUNCTION_DF_ACC_ENABLE__
  1628. if (dfacc != 0)
  1629. {
  1630. dummy = USB201.D0FIFO.UINT32;
  1631. }
  1632. #endif
  1633. buffer &= (uint16_t)~(USB_FUNCTION_BITCURPIPE | USB_FUNCTION_BITMBW);
  1634. buffer |= (uint16_t)(pipe | mbw);
  1635. USB201.D0FIFOSEL = buffer;
  1636. for (loop = 0; loop < 4; loop++)
  1637. {
  1638. if ((USB201.D0FIFOSEL & USB_FUNCTION_BITCURPIPE) == (buffer & USB_FUNCTION_BITCURPIPE))
  1639. {
  1640. break;
  1641. }
  1642. loop2 = 100;
  1643. while (loop2-- > 0)
  1644. {
  1645. /* wait */
  1646. }
  1647. }
  1648. break;
  1649. case USB_FUNCTION_D1DMA:
  1650. case USB_FUNCTION_D1USE:
  1651. buffer = USB201.D1FIFOSEL;
  1652. #ifdef __USB_FUNCTION_DF_ACC_ENABLE__
  1653. buffer &= (uint16_t)~(USB_FUNCTION_BITCURPIPE | USB_FUNCTION_BITMBW);
  1654. if (dfacc != 0)
  1655. {
  1656. buffer |= (uint16_t)(USB_FUNCTION_BITMBW_32);
  1657. }
  1658. #else
  1659. buffer &= (uint16_t)~(USB_FUNCTION_BITCURPIPE);
  1660. #endif
  1661. USB201.D1FIFOSEL = buffer;
  1662. for (loop = 0; loop < 4; loop++)
  1663. {
  1664. if ((USB201.D1FIFOSEL & USB_FUNCTION_BITCURPIPE) == (buffer & USB_FUNCTION_BITCURPIPE))
  1665. {
  1666. break;
  1667. }
  1668. loop2 = 100;
  1669. while (loop2-- > 0)
  1670. {
  1671. /* wait */
  1672. }
  1673. }
  1674. #ifdef __USB_FUNCTION_DF_ACC_ENABLE__
  1675. if (dfacc != 0)
  1676. {
  1677. dummy = USB201.D1FIFO.UINT32;
  1678. loop = dummy; // avoid warning.
  1679. }
  1680. #endif
  1681. buffer &= (uint16_t)~(USB_FUNCTION_BITCURPIPE | USB_FUNCTION_BITMBW);
  1682. buffer |= (uint16_t)(pipe | mbw);
  1683. USB201.D1FIFOSEL = buffer;
  1684. for (loop = 0; loop < 4; loop++)
  1685. {
  1686. if ((USB201.D1FIFOSEL & USB_FUNCTION_BITCURPIPE) == (buffer & USB_FUNCTION_BITCURPIPE))
  1687. {
  1688. break;
  1689. }
  1690. loop2 = 100;
  1691. while (loop2-- > 0)
  1692. {
  1693. /* wait */
  1694. }
  1695. }
  1696. break;
  1697. default:
  1698. break;
  1699. }
  1700. /* Cautions !!!
  1701. * Depending on the external bus speed of CPU, you may need to wait for 450ns here.
  1702. * For details, please look at the data sheet. */
  1703. loop2 = 100;
  1704. while (loop2-- > 0)
  1705. {
  1706. /* wait */
  1707. }
  1708. }
  1709. /*******************************************************************************
  1710. * Function Name: usb1_function_write_c_fifo
  1711. * Description : Writes data in CFIFO.
  1712. * : Writes data by BYTE/WORD/LONG according to access size
  1713. * : to the pipe specified by the arguments.
  1714. * : Before executing this function, allocating CFIF0 in the specified pipe
  1715. * : should be completed.
  1716. * : Before executing this function, access size to the specified pipe
  1717. * : should be fixed and set in g_usb1_function_mbw[].
  1718. * Arguments : uint16_t pipe ; Pipe Number
  1719. * : uint16_t count ; Data Size(Byte)
  1720. * Return Value : none
  1721. *******************************************************************************/
  1722. static void usb1_function_write_c_fifo (uint16_t pipe, uint16_t count)
  1723. {
  1724. uint16_t even;
  1725. if (g_usb1_function_mbw[pipe] == USB_FUNCTION_BITMBW_8)
  1726. {
  1727. for (even = count; even; --even)
  1728. {
  1729. USB201.CFIFO.UINT8[HH] = *g_usb1_function_data_pointer[pipe];
  1730. g_usb1_function_data_pointer[pipe] += 1;
  1731. }
  1732. }
  1733. else if (g_usb1_function_mbw[pipe] == USB_FUNCTION_BITMBW_16)
  1734. {
  1735. for (even = (uint16_t)(count / 2); even; --even)
  1736. {
  1737. USB201.CFIFO.UINT16[H] = *((uint16_t *)g_usb1_function_data_pointer[pipe]);
  1738. g_usb1_function_data_pointer[pipe] += 2;
  1739. }
  1740. }
  1741. else
  1742. {
  1743. for (even = (uint16_t)(count / 4); even; --even)
  1744. {
  1745. USB201.CFIFO.UINT32 = *((uint32_t *)g_usb1_function_data_pointer[pipe]);
  1746. g_usb1_function_data_pointer[pipe] += 4;
  1747. }
  1748. }
  1749. }
  1750. /*******************************************************************************
  1751. * Function Name: usb1_function_read_c_fifo
  1752. * Description : Reads data from CFIFO.
  1753. * : Reads data by BYTE/WORD/LONG according to access size
  1754. * : to the pipe specified by the arguments.
  1755. * : Before executing this function, allocating CFIF0 in the specified pipe
  1756. * : should be completed.
  1757. * : Before executing this function, access size to the specified pipe
  1758. * : should be fixed and set in g_usb1_function_mbw[].
  1759. * Arguments : uint16_t pipe ; Pipe Number
  1760. * : uint16_t count ; Data Size(Byte)
  1761. * Return Value : none
  1762. *******************************************************************************/
  1763. static void usb1_function_read_c_fifo (uint16_t pipe, uint16_t count)
  1764. {
  1765. uint16_t even;
  1766. if (g_usb1_function_mbw[pipe] == USB_FUNCTION_BITMBW_8)
  1767. {
  1768. for (even = count; even; --even)
  1769. {
  1770. *g_usb1_function_data_pointer[pipe] = USB201.CFIFO.UINT8[HH];
  1771. g_usb1_function_data_pointer[pipe] += 1;
  1772. }
  1773. }
  1774. else if (g_usb1_function_mbw[pipe] == USB_FUNCTION_BITMBW_16)
  1775. {
  1776. for (even = (uint16_t)((count + 1) / 2); even; --even)
  1777. {
  1778. *((uint16_t *)g_usb1_function_data_pointer[pipe]) = USB201.CFIFO.UINT16[H];
  1779. g_usb1_function_data_pointer[pipe] += 2;
  1780. }
  1781. }
  1782. else
  1783. {
  1784. for (even = (uint16_t)((count + 3) / 4); even; --even)
  1785. {
  1786. *((uint32_t *)g_usb1_function_data_pointer[pipe]) = USB201.CFIFO.UINT32;
  1787. g_usb1_function_data_pointer[pipe] += 4;
  1788. }
  1789. }
  1790. }
  1791. /*******************************************************************************
  1792. * Function Name: usb1_function_write_d0_fifo
  1793. * Description : Writes data in D0FIFO.
  1794. * : Writes data by BYTE/WORD/LONG according to access size
  1795. * : to the pipe specified by the arguments.
  1796. * : Before executing this function, allocating CFIF0 in the specified pipe
  1797. * : should be completed.
  1798. * : Before executing this function, access size to the specified pipe
  1799. * : should be fixed and set in g_usb1_function_mbw[].
  1800. * Arguments : uint16_t pipe ; Pipe Number
  1801. * : uint16_t count ; Data Size(Byte)
  1802. * Return Value : none
  1803. *******************************************************************************/
  1804. static void usb1_function_write_d0_fifo (uint16_t pipe, uint16_t count)
  1805. {
  1806. uint16_t even;
  1807. if (g_usb1_function_mbw[pipe] == USB_FUNCTION_BITMBW_8)
  1808. {
  1809. for (even = count; even; --even)
  1810. {
  1811. USB201.D0FIFO.UINT8[HH] = *g_usb1_function_data_pointer[pipe];
  1812. g_usb1_function_data_pointer[pipe] += 1;
  1813. }
  1814. }
  1815. else if (g_usb1_function_mbw[pipe] == USB_FUNCTION_BITMBW_16)
  1816. {
  1817. for (even = (uint16_t)(count / 2); even; --even)
  1818. {
  1819. USB201.D0FIFO.UINT16[H] = *((uint16_t *)g_usb1_function_data_pointer[pipe]);
  1820. g_usb1_function_data_pointer[pipe] += 2;
  1821. }
  1822. }
  1823. else
  1824. {
  1825. for (even = (uint16_t)(count / 4); even; --even)
  1826. {
  1827. USB201.D0FIFO.UINT32 = *((uint32_t *)g_usb1_function_data_pointer[pipe]);
  1828. g_usb1_function_data_pointer[pipe] += 4;
  1829. }
  1830. }
  1831. }
  1832. /*******************************************************************************
  1833. * Function Name: usb1_function_read_d0_fifo
  1834. * Description : Reads data from D0FIFO.
  1835. * : Reads data by BYTE/WORD/LONG according to access size
  1836. * : to the pipe specified by the arguments.
  1837. * : Before executing this function, allocating DOFIF0 in the specified pipe
  1838. * : should be completed.
  1839. * : Before executing this function, access size to the specified pipe
  1840. * : should be fixed and set in g_usb1_function_mbw[].
  1841. * Arguments : uint16_t pipe ; Pipe Number
  1842. * : uint16_t count ; Data Size(Byte)
  1843. * Return Value : none
  1844. *******************************************************************************/
  1845. static void usb1_function_read_d0_fifo (uint16_t pipe, uint16_t count)
  1846. {
  1847. uint16_t even;
  1848. if (g_usb1_function_mbw[pipe] == USB_FUNCTION_BITMBW_8)
  1849. {
  1850. for (even = count; even; --even)
  1851. {
  1852. *g_usb1_function_data_pointer[pipe] = USB201.D0FIFO.UINT8[HH];
  1853. g_usb1_function_data_pointer[pipe] += 1;
  1854. }
  1855. }
  1856. else if (g_usb1_function_mbw[pipe] == USB_FUNCTION_BITMBW_16)
  1857. {
  1858. for (even = (uint16_t)((count + 1) / 2); even; --even)
  1859. {
  1860. *((uint16_t *)g_usb1_function_data_pointer[pipe]) = USB201.D0FIFO.UINT16[H];
  1861. g_usb1_function_data_pointer[pipe] += 2;
  1862. }
  1863. }
  1864. else
  1865. {
  1866. for (even = (uint16_t)((count + 3) / 4); even; --even)
  1867. {
  1868. *((uint32_t *)g_usb1_function_data_pointer[pipe]) = USB201.D0FIFO.UINT32;
  1869. g_usb1_function_data_pointer[pipe] += 4;
  1870. }
  1871. }
  1872. }
  1873. /*******************************************************************************
  1874. * Function Name: usb1_function_write_d1_fifo
  1875. * Description : Writes data in D1FIFO.
  1876. * : Writes data by BYTE/WORD/LONG according to access size
  1877. * : to the pipe specified by the arguments.
  1878. * : Before executing this function, allocating D1FIF0 in the specified pipe
  1879. * : should be completed.
  1880. * : Before executing this function, access size to the specified pipe
  1881. * : should be fixed and set in g_usb1_function_mbw[].
  1882. * Arguments : uint16_t pipe ; Pipe Number
  1883. * : uint16_t count ; Data Size(Byte)
  1884. * Return Value : none
  1885. *******************************************************************************/
  1886. static void usb1_function_write_d1_fifo (uint16_t pipe, uint16_t count)
  1887. {
  1888. uint16_t even;
  1889. if (g_usb1_function_mbw[pipe] == USB_FUNCTION_BITMBW_8)
  1890. {
  1891. for (even = count; even; --even)
  1892. {
  1893. USB201.D1FIFO.UINT8[HH] = *g_usb1_function_data_pointer[pipe];
  1894. g_usb1_function_data_pointer[pipe] += 1;
  1895. }
  1896. }
  1897. else if (g_usb1_function_mbw[pipe] == USB_FUNCTION_BITMBW_16)
  1898. {
  1899. for (even = (uint16_t)(count / 2); even; --even)
  1900. {
  1901. USB201.D1FIFO.UINT16[H] = *((uint16_t *)g_usb1_function_data_pointer[pipe]);
  1902. g_usb1_function_data_pointer[pipe] += 2;
  1903. }
  1904. }
  1905. else
  1906. {
  1907. for (even = (uint16_t)(count / 4); even; --even)
  1908. {
  1909. USB201.D1FIFO.UINT32 = *((uint32_t *)g_usb1_function_data_pointer[pipe]);
  1910. g_usb1_function_data_pointer[pipe] += 4;
  1911. }
  1912. }
  1913. }
  1914. /*******************************************************************************
  1915. * Function Name: usb1_function_read_d1_fifo
  1916. * Description : Reads data from D1FIFO.
  1917. * : Reads data by BYTE/WORD/LONG according to access size
  1918. * : to the pipe specified by the arguments.
  1919. * : Before executing this function, allocating D1FIF0 in the specified pipe
  1920. * : should be completed.
  1921. * : Before executing this function, access size to the specified pipe
  1922. * : should be fixed and set in g_usb1_function_mbw[].
  1923. * Arguments : uint16_t pipe ; Pipe Number
  1924. * : uint16_t count ; Data Size(Byte)
  1925. * Return Value : none
  1926. *******************************************************************************/
  1927. static void usb1_function_read_d1_fifo (uint16_t pipe, uint16_t count)
  1928. {
  1929. uint16_t even;
  1930. if (g_usb1_function_mbw[pipe] == USB_FUNCTION_BITMBW_8)
  1931. {
  1932. for (even = count; even; --even)
  1933. {
  1934. *g_usb1_function_data_pointer[pipe] = USB201.D1FIFO.UINT8[HH];
  1935. g_usb1_function_data_pointer[pipe] += 1;
  1936. }
  1937. }
  1938. else if (g_usb1_function_mbw[pipe] == USB_FUNCTION_BITMBW_16)
  1939. {
  1940. for (even = (uint16_t)((count + 1) / 2); even; --even)
  1941. {
  1942. *((uint16_t *)g_usb1_function_data_pointer[pipe]) = USB201.D1FIFO.UINT16[H];
  1943. g_usb1_function_data_pointer[pipe] += 2;
  1944. }
  1945. }
  1946. else
  1947. {
  1948. for (even = (uint16_t)((count + 3) / 4); even; --even)
  1949. {
  1950. *((uint32_t *)g_usb1_function_data_pointer[pipe]) = USB201.D1FIFO.UINT32;
  1951. g_usb1_function_data_pointer[pipe] += 4;
  1952. }
  1953. }
  1954. }
  1955. /*******************************************************************************
  1956. * Function Name: usb1_function_com_get_dmasize
  1957. * Description : Calculates access width of DMA transfer by the argument to
  1958. * : return as the Return Value.
  1959. * Arguments : uint32_t trncount : transfer byte
  1960. * : uint32_t dtptr : transfer data pointer
  1961. * Return Value : DMA transfer size : 0 8bit
  1962. * : : 1 16bit
  1963. * : : 2 32bit
  1964. *******************************************************************************/
  1965. static uint32_t usb1_function_com_get_dmasize (uint32_t trncount, uint32_t dtptr)
  1966. {
  1967. uint32_t size;
  1968. if (((trncount & 0x0001) != 0) || ((dtptr & 0x00000001) != 0))
  1969. {
  1970. /* When transfer byte count is odd */
  1971. /* or transfer data area is 8-bit alignment */
  1972. size = 0; /* 8bit */
  1973. }
  1974. else if (((trncount & 0x0003) != 0) || ((dtptr & 0x00000003) != 0))
  1975. {
  1976. /* When the transfer byte count is multiples of 2 */
  1977. /* or the transfer data area is 16-bit alignment */
  1978. size = 1; /* 16bit */
  1979. }
  1980. else
  1981. {
  1982. /* When the transfer byte count is multiples of 4 */
  1983. /* or the transfer data area is 32-bit alignment */
  1984. size = 2; /* 32bit */
  1985. }
  1986. return size;
  1987. }
  1988. /*******************************************************************************
  1989. * Function Name: usb1_function_get_mbw
  1990. * Description : Calculates access width of DMA to return the value set in MBW.
  1991. * Arguments : uint32_t trncount : transfer byte
  1992. * : uint32_t dtptr : transfer data pointer
  1993. * Return Value : FIFO transfer size : USB_FUNCTION_BITMBW_8 8bit
  1994. * : : USB_FUNCTION_BITMBW_16 16bit
  1995. * : : USB_FUNCTION_BITMBW_32 32bit
  1996. *******************************************************************************/
  1997. uint16_t usb1_function_get_mbw (uint32_t trncount, uint32_t dtptr)
  1998. {
  1999. uint32_t size;
  2000. uint16_t mbw;
  2001. size = usb1_function_com_get_dmasize(trncount, dtptr);
  2002. if (size == 0)
  2003. {
  2004. /* 8bit */
  2005. mbw = USB_FUNCTION_BITMBW_8;
  2006. }
  2007. else if (size == 1)
  2008. {
  2009. /* 16bit */
  2010. mbw = USB_FUNCTION_BITMBW_16;
  2011. }
  2012. else
  2013. {
  2014. /* 32bit */
  2015. mbw = USB_FUNCTION_BITMBW_32;
  2016. }
  2017. return mbw;
  2018. }
  2019. /*******************************************************************************
  2020. * Function Name: usb1_function_set_transaction_counter
  2021. * Description : Sets transaction counter by the argument(PIPEnTRN).
  2022. * : Clears transaction before setting to enable transaction counter setting.
  2023. * Arguments : uint16_t pipe ; Pipe number
  2024. * : uint32_t bsize : Data transfer size
  2025. * Return Value : none
  2026. *******************************************************************************/
  2027. static void usb1_function_set_transaction_counter (uint16_t pipe, uint32_t bsize)
  2028. {
  2029. uint16_t mxps;
  2030. uint16_t cnt;
  2031. if (bsize == 0)
  2032. {
  2033. return;
  2034. }
  2035. mxps = usb1_function_get_mxps(pipe); /* Max Packet Size */
  2036. if ((bsize % mxps) == 0)
  2037. {
  2038. cnt = (uint16_t)(bsize / mxps);
  2039. }
  2040. else
  2041. {
  2042. cnt = (uint16_t)((bsize / mxps) + 1);
  2043. }
  2044. switch (pipe)
  2045. {
  2046. case USB_FUNCTION_PIPE1:
  2047. RZA_IO_RegWrite_16(&USB201.PIPE1TRE,
  2048. 1,
  2049. USB_PIPEnTRE_TRCLR_SHIFT,
  2050. USB_PIPEnTRE_TRCLR);
  2051. USB201.PIPE1TRN = cnt;
  2052. RZA_IO_RegWrite_16(&USB201.PIPE1TRE,
  2053. 1,
  2054. USB_PIPEnTRE_TRENB_SHIFT,
  2055. USB_PIPEnTRE_TRENB);
  2056. break;
  2057. case USB_FUNCTION_PIPE2:
  2058. RZA_IO_RegWrite_16(&USB201.PIPE2TRE,
  2059. 1,
  2060. USB_PIPEnTRE_TRCLR_SHIFT,
  2061. USB_PIPEnTRE_TRCLR);
  2062. USB201.PIPE2TRN = cnt;
  2063. RZA_IO_RegWrite_16(&USB201.PIPE2TRE,
  2064. 1,
  2065. USB_PIPEnTRE_TRENB_SHIFT,
  2066. USB_PIPEnTRE_TRENB);
  2067. break;
  2068. case USB_FUNCTION_PIPE3:
  2069. RZA_IO_RegWrite_16(&USB201.PIPE3TRE,
  2070. 1,
  2071. USB_PIPEnTRE_TRCLR_SHIFT,
  2072. USB_PIPEnTRE_TRCLR);
  2073. USB201.PIPE3TRN = cnt;
  2074. RZA_IO_RegWrite_16(&USB201.PIPE3TRE,
  2075. 1,
  2076. USB_PIPEnTRE_TRENB_SHIFT,
  2077. USB_PIPEnTRE_TRENB);
  2078. break;
  2079. case USB_FUNCTION_PIPE4:
  2080. RZA_IO_RegWrite_16(&USB201.PIPE4TRE,
  2081. 1,
  2082. USB_PIPEnTRE_TRCLR_SHIFT,
  2083. USB_PIPEnTRE_TRCLR);
  2084. USB201.PIPE4TRN = cnt;
  2085. RZA_IO_RegWrite_16(&USB201.PIPE4TRE,
  2086. 1,
  2087. USB_PIPEnTRE_TRENB_SHIFT,
  2088. USB_PIPEnTRE_TRENB);
  2089. break;
  2090. case USB_FUNCTION_PIPE5:
  2091. RZA_IO_RegWrite_16(&USB201.PIPE5TRE,
  2092. 1,
  2093. USB_PIPEnTRE_TRCLR_SHIFT,
  2094. USB_PIPEnTRE_TRCLR);
  2095. USB201.PIPE5TRN = cnt;
  2096. RZA_IO_RegWrite_16(&USB201.PIPE5TRE,
  2097. 1,
  2098. USB_PIPEnTRE_TRENB_SHIFT,
  2099. USB_PIPEnTRE_TRENB);
  2100. break;
  2101. case USB_FUNCTION_PIPE9:
  2102. RZA_IO_RegWrite_16(&USB201.PIPE9TRE,
  2103. 1,
  2104. USB_PIPEnTRE_TRCLR_SHIFT,
  2105. USB_PIPEnTRE_TRCLR);
  2106. USB201.PIPE9TRN = cnt;
  2107. RZA_IO_RegWrite_16(&USB201.PIPE9TRE,
  2108. 1,
  2109. USB_PIPEnTRE_TRENB_SHIFT,
  2110. USB_PIPEnTRE_TRENB);
  2111. break;
  2112. case USB_FUNCTION_PIPEA:
  2113. RZA_IO_RegWrite_16(&USB201.PIPEATRE,
  2114. 1,
  2115. USB_PIPEnTRE_TRCLR_SHIFT,
  2116. USB_PIPEnTRE_TRCLR);
  2117. USB201.PIPEATRN = cnt;
  2118. RZA_IO_RegWrite_16(&USB201.PIPEATRE,
  2119. 1,
  2120. USB_PIPEnTRE_TRENB_SHIFT,
  2121. USB_PIPEnTRE_TRENB);
  2122. break;
  2123. case USB_FUNCTION_PIPEB:
  2124. RZA_IO_RegWrite_16(&USB201.PIPEBTRE,
  2125. 1,
  2126. USB_PIPEnTRE_TRCLR_SHIFT,
  2127. USB_PIPEnTRE_TRCLR);
  2128. USB201.PIPEBTRN = cnt;
  2129. RZA_IO_RegWrite_16(&USB201.PIPEBTRE,
  2130. 1,
  2131. USB_PIPEnTRE_TRENB_SHIFT,
  2132. USB_PIPEnTRE_TRENB);
  2133. break;
  2134. case USB_FUNCTION_PIPEC:
  2135. RZA_IO_RegWrite_16(&USB201.PIPECTRE,
  2136. 1,
  2137. USB_PIPEnTRE_TRCLR_SHIFT,
  2138. USB_PIPEnTRE_TRCLR);
  2139. USB201.PIPECTRN = cnt;
  2140. RZA_IO_RegWrite_16(&USB201.PIPECTRE,
  2141. 1,
  2142. USB_PIPEnTRE_TRENB_SHIFT,
  2143. USB_PIPEnTRE_TRENB);
  2144. break;
  2145. case USB_FUNCTION_PIPED:
  2146. RZA_IO_RegWrite_16(&USB201.PIPEDTRE,
  2147. 1,
  2148. USB_PIPEnTRE_TRCLR_SHIFT,
  2149. USB_PIPEnTRE_TRCLR);
  2150. USB201.PIPEDTRN = cnt;
  2151. RZA_IO_RegWrite_16(&USB201.PIPEDTRE,
  2152. 1,
  2153. USB_PIPEnTRE_TRENB_SHIFT,
  2154. USB_PIPEnTRE_TRENB);
  2155. break;
  2156. case USB_FUNCTION_PIPEE:
  2157. RZA_IO_RegWrite_16(&USB201.PIPEETRE,
  2158. 1,
  2159. USB_PIPEnTRE_TRCLR_SHIFT,
  2160. USB_PIPEnTRE_TRCLR);
  2161. USB201.PIPEETRN = cnt;
  2162. RZA_IO_RegWrite_16(&USB201.PIPEETRE,
  2163. 1,
  2164. USB_PIPEnTRE_TRENB_SHIFT,
  2165. USB_PIPEnTRE_TRENB);
  2166. break;
  2167. case USB_FUNCTION_PIPEF:
  2168. RZA_IO_RegWrite_16(&USB201.PIPEFTRE,
  2169. 1,
  2170. USB_PIPEnTRE_TRCLR_SHIFT,
  2171. USB_PIPEnTRE_TRCLR);
  2172. USB201.PIPEFTRN = cnt;
  2173. RZA_IO_RegWrite_16(&USB201.PIPEFTRE,
  2174. 1,
  2175. USB_PIPEnTRE_TRENB_SHIFT,
  2176. USB_PIPEnTRE_TRENB);
  2177. break;
  2178. default:
  2179. break;
  2180. }
  2181. }
  2182. /*******************************************************************************
  2183. * Function Name: usb1_function_clear_transaction_counter
  2184. * Description : Clears the transaction counter by the argument.
  2185. * : After executing this function, the transaction counter is invalid.
  2186. * Arguments : uint16_t pipe ; Pipe number
  2187. * Return Value : none
  2188. *******************************************************************************/
  2189. void usb1_function_clear_transaction_counter (uint16_t pipe)
  2190. {
  2191. switch (pipe)
  2192. {
  2193. case USB_FUNCTION_PIPE1:
  2194. RZA_IO_RegWrite_16(&USB201.PIPE1TRE,
  2195. 0,
  2196. USB_PIPEnTRE_TRENB_SHIFT,
  2197. USB_PIPEnTRE_TRENB);
  2198. RZA_IO_RegWrite_16(&USB201.PIPE1TRE,
  2199. 1,
  2200. USB_PIPEnTRE_TRCLR_SHIFT,
  2201. USB_PIPEnTRE_TRCLR);
  2202. break;
  2203. case USB_FUNCTION_PIPE2:
  2204. RZA_IO_RegWrite_16(&USB201.PIPE2TRE,
  2205. 0,
  2206. USB_PIPEnTRE_TRENB_SHIFT,
  2207. USB_PIPEnTRE_TRENB);
  2208. RZA_IO_RegWrite_16(&USB201.PIPE2TRE,
  2209. 1,
  2210. USB_PIPEnTRE_TRCLR_SHIFT,
  2211. USB_PIPEnTRE_TRCLR);
  2212. break;
  2213. case USB_FUNCTION_PIPE3:
  2214. RZA_IO_RegWrite_16(&USB201.PIPE3TRE,
  2215. 0,
  2216. USB_PIPEnTRE_TRENB_SHIFT,
  2217. USB_PIPEnTRE_TRENB);
  2218. RZA_IO_RegWrite_16(&USB201.PIPE3TRE,
  2219. 1,
  2220. USB_PIPEnTRE_TRCLR_SHIFT,
  2221. USB_PIPEnTRE_TRCLR);
  2222. break;
  2223. case USB_FUNCTION_PIPE4:
  2224. RZA_IO_RegWrite_16(&USB201.PIPE4TRE,
  2225. 0,
  2226. USB_PIPEnTRE_TRENB_SHIFT,
  2227. USB_PIPEnTRE_TRENB);
  2228. RZA_IO_RegWrite_16(&USB201.PIPE4TRE,
  2229. 1,
  2230. USB_PIPEnTRE_TRCLR_SHIFT,
  2231. USB_PIPEnTRE_TRCLR);
  2232. break;
  2233. case USB_FUNCTION_PIPE5:
  2234. RZA_IO_RegWrite_16(&USB201.PIPE5TRE,
  2235. 0,
  2236. USB_PIPEnTRE_TRENB_SHIFT,
  2237. USB_PIPEnTRE_TRENB);
  2238. RZA_IO_RegWrite_16(&USB201.PIPE5TRE,
  2239. 1,
  2240. USB_PIPEnTRE_TRCLR_SHIFT,
  2241. USB_PIPEnTRE_TRCLR);
  2242. break;
  2243. case USB_FUNCTION_PIPE9:
  2244. RZA_IO_RegWrite_16(&USB201.PIPE9TRE,
  2245. 0,
  2246. USB_PIPEnTRE_TRENB_SHIFT,
  2247. USB_PIPEnTRE_TRENB);
  2248. RZA_IO_RegWrite_16(&USB201.PIPE9TRE,
  2249. 1,
  2250. USB_PIPEnTRE_TRCLR_SHIFT,
  2251. USB_PIPEnTRE_TRCLR);
  2252. break;
  2253. case USB_FUNCTION_PIPEA:
  2254. RZA_IO_RegWrite_16(&USB201.PIPEATRE,
  2255. 0,
  2256. USB_PIPEnTRE_TRENB_SHIFT,
  2257. USB_PIPEnTRE_TRENB);
  2258. RZA_IO_RegWrite_16(&USB201.PIPEATRE,
  2259. 1,
  2260. USB_PIPEnTRE_TRCLR_SHIFT,
  2261. USB_PIPEnTRE_TRCLR);
  2262. break;
  2263. case USB_FUNCTION_PIPEB:
  2264. RZA_IO_RegWrite_16(&USB201.PIPEBTRE,
  2265. 0,
  2266. USB_PIPEnTRE_TRENB_SHIFT,
  2267. USB_PIPEnTRE_TRENB);
  2268. RZA_IO_RegWrite_16(&USB201.PIPEBTRE,
  2269. 1,
  2270. USB_PIPEnTRE_TRCLR_SHIFT,
  2271. USB_PIPEnTRE_TRCLR);
  2272. break;
  2273. case USB_FUNCTION_PIPEC:
  2274. RZA_IO_RegWrite_16(&USB201.PIPECTRE,
  2275. 0,
  2276. USB_PIPEnTRE_TRENB_SHIFT,
  2277. USB_PIPEnTRE_TRENB);
  2278. RZA_IO_RegWrite_16(&USB201.PIPECTRE,
  2279. 1,
  2280. USB_PIPEnTRE_TRCLR_SHIFT,
  2281. USB_PIPEnTRE_TRCLR);
  2282. break;
  2283. case USB_FUNCTION_PIPED:
  2284. RZA_IO_RegWrite_16(&USB201.PIPEDTRE,
  2285. 0,
  2286. USB_PIPEnTRE_TRENB_SHIFT,
  2287. USB_PIPEnTRE_TRENB);
  2288. RZA_IO_RegWrite_16(&USB201.PIPEDTRE,
  2289. 1,
  2290. USB_PIPEnTRE_TRCLR_SHIFT,
  2291. USB_PIPEnTRE_TRCLR);
  2292. break;
  2293. case USB_FUNCTION_PIPEE:
  2294. RZA_IO_RegWrite_16(&USB201.PIPEETRE,
  2295. 0,
  2296. USB_PIPEnTRE_TRENB_SHIFT,
  2297. USB_PIPEnTRE_TRENB);
  2298. RZA_IO_RegWrite_16(&USB201.PIPEETRE,
  2299. 1,
  2300. USB_PIPEnTRE_TRCLR_SHIFT,
  2301. USB_PIPEnTRE_TRCLR);
  2302. break;
  2303. case USB_FUNCTION_PIPEF:
  2304. RZA_IO_RegWrite_16(&USB201.PIPEFTRE,
  2305. 0,
  2306. USB_PIPEnTRE_TRENB_SHIFT,
  2307. USB_PIPEnTRE_TRENB);
  2308. RZA_IO_RegWrite_16(&USB201.PIPEFTRE,
  2309. 1,
  2310. USB_PIPEnTRE_TRCLR_SHIFT,
  2311. USB_PIPEnTRE_TRCLR);
  2312. break;
  2313. default:
  2314. break;
  2315. }
  2316. }
  2317. /*******************************************************************************
  2318. * Function Name: usb1_function_stop_transfer
  2319. * Description : Stops the USB transfer in the pipe specified by the argument.
  2320. * : After stopping the USB transfer, clears the buffer allocated in
  2321. * : the pipe.
  2322. * : After executing this function, allocation in FIF0 becomes USB_FUNCTION_PIPE0;
  2323. * : invalid. After executing this function, BRDY/NRDY/BEMP interrupt
  2324. * : in the corresponding pipe becomes invalid. Sequence bit is also
  2325. * : cleared.
  2326. * Arguments : uint16_t pipe ; Pipe Number
  2327. * Return Value : none
  2328. *******************************************************************************/
  2329. void usb1_function_stop_transfer (uint16_t pipe)
  2330. {
  2331. uint16_t usefifo;
  2332. uint32_t remain;
  2333. uint16_t fifo;
  2334. usb1_function_set_pid_nak(pipe);
  2335. usefifo = (uint16_t)(g_usb1_function_PipeTbl[pipe] & USB_FUNCTION_FIFO_USE);
  2336. switch (usefifo)
  2337. {
  2338. case USB_FUNCTION_D0FIFO_USE:
  2339. usb1_function_clear_transaction_counter(pipe);
  2340. USB201.D0FIFOCTR = USB_FUNCTION_BITBCLR; /* Buffer Clear */
  2341. fifo = USB_FUNCTION_D0USE;
  2342. break;
  2343. case USB_FUNCTION_D1FIFO_USE:
  2344. usb1_function_clear_transaction_counter(pipe);
  2345. USB201.D1FIFOCTR = USB_FUNCTION_BITBCLR; /* Buffer Clear */
  2346. fifo = USB_FUNCTION_D1USE;
  2347. break;
  2348. case USB_FUNCTION_D0FIFO_DMA:
  2349. remain = Userdef_USB_usb1_function_stop_dma0();
  2350. usb1_function_dma_stop_d0(pipe, remain);
  2351. usb1_function_clear_transaction_counter(pipe);
  2352. USB201.D0FIFOCTR = USB_FUNCTION_BITBCLR; /* Buffer Clear */
  2353. fifo = USB_FUNCTION_D0DMA;
  2354. break;
  2355. case USB_FUNCTION_D1FIFO_DMA:
  2356. remain = Userdef_USB_usb1_function_stop_dma1();
  2357. usb1_function_dma_stop_d1(pipe, remain);
  2358. usb1_function_clear_transaction_counter(pipe);
  2359. USB201.D1FIFOCTR = USB_FUNCTION_BITBCLR; /* Buffer Clear */
  2360. fifo = USB_FUNCTION_D1DMA;
  2361. break;
  2362. default:
  2363. usb1_function_clear_transaction_counter(pipe);
  2364. USB201.CFIFOCTR = USB_FUNCTION_BITBCLR; /* Buffer Clear */
  2365. fifo = USB_FUNCTION_CUSE;
  2366. break;
  2367. }
  2368. usb1_function_set_curpipe(USB_FUNCTION_PIPE0, fifo, DEVDRV_USBF_NO, USB_FUNCTION_BITMBW_16);
  2369. /* Interrupt of pipe set is disabled */
  2370. usb1_function_disable_brdy_int(pipe);
  2371. usb1_function_disable_nrdy_int(pipe);
  2372. usb1_function_disable_bemp_int(pipe);
  2373. usb1_function_aclrm(pipe);
  2374. usb1_function_set_csclr(pipe);
  2375. if ( g_usb1_function_pipe_status[pipe] == DEVDRV_USBF_PIPE_WAIT )
  2376. {
  2377. g_usb1_function_pipe_status[pipe] = DEVDRV_USBF_PIPE_NORES;
  2378. }
  2379. }
  2380. /*******************************************************************************
  2381. * Function Name: usb1_function_set_dfacc_d0
  2382. * Description : Sets the DFACC setting value in D0FIFO using the transfer size.
  2383. * Arguments : uint16_t mbw ; MBW
  2384. * : uint16_t count ; data count
  2385. * Return Value : DFACC Access mode
  2386. *******************************************************************************/
  2387. static uint16_t usb1_function_set_dfacc_d0 (uint16_t mbw, uint32_t count)
  2388. {
  2389. uint16_t dfacc = 0;
  2390. #ifndef __USB_FUNCTION_DF_ACC_ENABLE__
  2391. RZA_IO_RegWrite_16(&USB201.D0FBCFG,
  2392. 0,
  2393. USB_DnFBCFG_DFACC_SHIFT,
  2394. USB_DnFBCFG_DFACC);
  2395. RZA_IO_RegWrite_16(&USB201.D0FBCFG,
  2396. 0,
  2397. USB_DnFBCFG_TENDE_SHIFT,
  2398. USB_DnFBCFG_TENDE);
  2399. dfacc = 0;
  2400. #else
  2401. if (mbw == USB_FUNCTION_BITMBW_32)
  2402. {
  2403. if ((count % 32) == 0)
  2404. {
  2405. /* 32byte transfer */
  2406. RZA_IO_RegWrite_16(&USB201.D0FBCFG,
  2407. 2,
  2408. USB_DnFBCFG_DFACC_SHIFT,
  2409. USB_DnFBCFG_DFACC);
  2410. RZA_IO_RegWrite_16(&USB201.D0FBCFG,
  2411. 0,
  2412. USB_DnFBCFG_TENDE_SHIFT,
  2413. USB_DnFBCFG_TENDE);
  2414. dfacc = 2;
  2415. }
  2416. else if ((count % 16) == 0)
  2417. {
  2418. /* 16byte transfer */
  2419. RZA_IO_RegWrite_16(&USB201.D0FBCFG,
  2420. 1,
  2421. USB_DnFBCFG_DFACC_SHIFT,
  2422. USB_DnFBCFG_DFACC);
  2423. RZA_IO_RegWrite_16(&USB201.D0FBCFG,
  2424. 0,
  2425. USB_DnFBCFG_TENDE_SHIFT,
  2426. USB_DnFBCFG_TENDE);
  2427. dfacc = 1;
  2428. }
  2429. else
  2430. {
  2431. RZA_IO_RegWrite_16(&USB201.D0FBCFG,
  2432. 0,
  2433. USB_DnFBCFG_DFACC_SHIFT,
  2434. USB_DnFBCFG_DFACC);
  2435. RZA_IO_RegWrite_16(&USB201.D0FBCFG,
  2436. 0,
  2437. USB_DnFBCFG_TENDE_SHIFT,
  2438. USB_DnFBCFG_TENDE);
  2439. dfacc = 0;
  2440. }
  2441. }
  2442. else if (mbw == USB_FUNCTION_BITMBW_16)
  2443. {
  2444. RZA_IO_RegWrite_16(&USB201.D0FBCFG,
  2445. 0,
  2446. USB_DnFBCFG_DFACC_SHIFT,
  2447. USB_DnFBCFG_DFACC);
  2448. RZA_IO_RegWrite_16(&USB201.D0FBCFG,
  2449. 0,
  2450. USB_DnFBCFG_TENDE_SHIFT,
  2451. USB_DnFBCFG_TENDE);
  2452. dfacc = 0;
  2453. }
  2454. else
  2455. {
  2456. RZA_IO_RegWrite_16(&USB201.D0FBCFG,
  2457. 0,
  2458. USB_DnFBCFG_DFACC_SHIFT,
  2459. USB_DnFBCFG_DFACC);
  2460. RZA_IO_RegWrite_16(&USB201.D0FBCFG,
  2461. 0,
  2462. USB_DnFBCFG_TENDE_SHIFT,
  2463. USB_DnFBCFG_TENDE);
  2464. dfacc = 0;
  2465. }
  2466. #endif
  2467. return dfacc;
  2468. }
  2469. /*******************************************************************************
  2470. * Function Name: usb1_function_set_dfacc_d1
  2471. * Description : Set the DFACC setting value in D1FIFO using the transfer size.
  2472. * Arguments : uint16_t mbw ; MBW
  2473. * : uint16_t count ; data count
  2474. * Return Value : DFACC Access mode
  2475. *******************************************************************************/
  2476. static uint16_t usb1_function_set_dfacc_d1 (uint16_t mbw, uint32_t count)
  2477. {
  2478. uint16_t dfacc = 0;
  2479. #ifndef __USB_FUNCTION_DF_ACC_ENABLE__
  2480. RZA_IO_RegWrite_16(&USB201.D1FBCFG,
  2481. 0,
  2482. USB_DnFBCFG_DFACC_SHIFT,
  2483. USB_DnFBCFG_DFACC);
  2484. RZA_IO_RegWrite_16(&USB201.D1FBCFG,
  2485. 0,
  2486. USB_DnFBCFG_TENDE_SHIFT,
  2487. USB_DnFBCFG_TENDE);
  2488. dfacc = 0;
  2489. #else
  2490. if (mbw == USB_FUNCTION_BITMBW_32)
  2491. {
  2492. if ((count % 32) == 0)
  2493. {
  2494. /* 32byte transfer */
  2495. RZA_IO_RegWrite_16(&USB201.D1FBCFG,
  2496. 2,
  2497. USB_DnFBCFG_DFACC_SHIFT,
  2498. USB_DnFBCFG_DFACC);
  2499. RZA_IO_RegWrite_16(&USB201.D1FBCFG,
  2500. 0,
  2501. USB_DnFBCFG_TENDE_SHIFT,
  2502. USB_DnFBCFG_TENDE);
  2503. dfacc = 2;
  2504. }
  2505. else if ((count % 16) == 0)
  2506. {
  2507. /* 16byte transfer */
  2508. RZA_IO_RegWrite_16(&USB201.D1FBCFG,
  2509. 1,
  2510. USB_DnFBCFG_DFACC_SHIFT,
  2511. USB_DnFBCFG_DFACC);
  2512. RZA_IO_RegWrite_16(&USB201.D1FBCFG,
  2513. 0,
  2514. USB_DnFBCFG_TENDE_SHIFT,
  2515. USB_DnFBCFG_TENDE);
  2516. dfacc = 1;
  2517. }
  2518. else
  2519. {
  2520. RZA_IO_RegWrite_16(&USB201.D1FBCFG,
  2521. 0,
  2522. USB_DnFBCFG_DFACC_SHIFT,
  2523. USB_DnFBCFG_DFACC);
  2524. RZA_IO_RegWrite_16(&USB201.D1FBCFG,
  2525. 0,
  2526. USB_DnFBCFG_TENDE_SHIFT,
  2527. USB_DnFBCFG_TENDE);
  2528. dfacc = 0;
  2529. }
  2530. }
  2531. else if (mbw == USB_FUNCTION_BITMBW_16)
  2532. {
  2533. RZA_IO_RegWrite_16(&USB201.D1FBCFG,
  2534. 0,
  2535. USB_DnFBCFG_DFACC_SHIFT,
  2536. USB_DnFBCFG_DFACC);
  2537. RZA_IO_RegWrite_16(&USB201.D1FBCFG,
  2538. 0,
  2539. USB_DnFBCFG_TENDE_SHIFT,
  2540. USB_DnFBCFG_TENDE);
  2541. dfacc = 0;
  2542. }
  2543. else
  2544. {
  2545. RZA_IO_RegWrite_16(&USB201.D1FBCFG,
  2546. 0,
  2547. USB_DnFBCFG_DFACC_SHIFT,
  2548. USB_DnFBCFG_DFACC);
  2549. RZA_IO_RegWrite_16(&USB201.D1FBCFG,
  2550. 0,
  2551. USB_DnFBCFG_TENDE_SHIFT,
  2552. USB_DnFBCFG_TENDE);
  2553. dfacc = 0;
  2554. }
  2555. #endif
  2556. return dfacc;
  2557. }
  2558. /* End of File */