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

usb0_function_dataio.c 108KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933
  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 : usb0_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 "usb0_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_usb0_function_mbw[(USB_FUNCTION_MAX_PIPE_NO + 1)];
  55. static void usb0_function_start_receive_trns_c(uint16_t pipe, uint32_t size, uint8_t *data);
  56. static void usb0_function_start_receive_trns_d0(uint16_t pipe, uint32_t size, uint8_t *data);
  57. static void usb0_function_start_receive_trns_d1(uint16_t pipe, uint32_t size, uint8_t *data);
  58. static void usb0_function_start_receive_dma_d0(uint16_t pipe, uint32_t size, uint8_t *data);
  59. static void usb0_function_start_receive_dma_d1(uint16_t pipe, uint32_t size, uint8_t *data);
  60. static uint16_t usb0_function_read_dma_d0(uint16_t pipe);
  61. static uint16_t usb0_function_read_dma_d1(uint16_t pipe);
  62. static uint16_t usb0_function_write_dma_d0(uint16_t pipe);
  63. static uint16_t usb0_function_write_dma_d1(uint16_t pipe);
  64. static void usb0_function_read_c_fifo(uint16_t pipe, uint16_t count);
  65. static void usb0_function_write_c_fifo(uint16_t Pipe, uint16_t count);
  66. static void usb0_function_read_d0_fifo(uint16_t pipe, uint16_t count);
  67. static void usb0_function_write_d0_fifo(uint16_t pipe, uint16_t count);
  68. static void usb0_function_read_d1_fifo(uint16_t pipe, uint16_t count);
  69. static void usb0_function_write_d1_fifo(uint16_t pipe, uint16_t count);
  70. static void usb0_function_clear_transaction_counter(uint16_t pipe);
  71. static void usb0_function_set_transaction_counter(uint16_t pipe, uint32_t count);
  72. static uint32_t usb0_function_com_get_dmasize(uint32_t trncount, uint32_t dtptr);
  73. static uint16_t usb0_function_set_dfacc_d0(uint16_t mbw, uint32_t count);
  74. static uint16_t usb0_function_set_dfacc_d1(uint16_t mbw, uint32_t count);
  75. /*******************************************************************************
  76. * Function Name: usb0_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 usb0_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_usb0_function_data_count[pipe] = size;
  93. g_usb0_function_data_pointer[pipe] = (uint8_t *)data;
  94. g_usb0_function_pipe_status[pipe] = DEVDRV_USBF_PIPE_WAIT;
  95. usb0_function_clear_bemp_sts(pipe);
  96. usb0_function_clear_brdy_sts(pipe);
  97. usb0_function_clear_nrdy_sts(pipe);
  98. mbw = usb0_function_get_mbw(size, (uint32_t)data);
  99. usefifo = (uint16_t)(g_usb0_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. usb0_function_set_curpipe(USB_FUNCTION_PIPE0, usefifo, DEVDRV_USBF_NO, mbw);
  115. usb0_function_clear_transaction_counter(pipe);
  116. usb0_function_aclrm(pipe);
  117. status = usb0_function_write_buffer(pipe);
  118. if (status != DEVDRV_USBF_FIFOERROR)
  119. {
  120. usb0_function_set_pid_buf(pipe);
  121. }
  122. return status;
  123. }
  124. /*******************************************************************************
  125. * Function Name: usb0_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 usb0_function_write_buffer (uint16_t pipe)
  136. {
  137. uint16_t status;
  138. uint16_t usefifo;
  139. g_usb0_function_PipeIgnore[pipe] = 0;
  140. usefifo = (uint16_t)(g_usb0_function_PipeTbl[pipe] & USB_FUNCTION_FIFO_USE);
  141. switch (usefifo)
  142. {
  143. case USB_FUNCTION_D0FIFO_USE:
  144. status = usb0_function_write_buffer_d0(pipe);
  145. break;
  146. case USB_FUNCTION_D1FIFO_USE:
  147. status = usb0_function_write_buffer_d1(pipe);
  148. break;
  149. case USB_FUNCTION_D0FIFO_DMA:
  150. status = usb0_function_write_dma_d0(pipe);
  151. break;
  152. case USB_FUNCTION_D1FIFO_DMA:
  153. status = usb0_function_write_dma_d1(pipe);
  154. break;
  155. default:
  156. status = usb0_function_write_buffer_c(pipe);
  157. break;
  158. };
  159. switch (status)
  160. {
  161. case DEVDRV_USBF_WRITING: /* Continue of data write */
  162. usb0_function_enable_nrdy_int(pipe); /* Error (NORES or STALL) */
  163. usb0_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. usb0_function_disable_brdy_int(pipe); /* Disable Ready Interrupt */
  168. usb0_function_clear_nrdy_sts(pipe);
  169. usb0_function_enable_nrdy_int(pipe); /* Error (NORES or STALL) */
  170. /* for last transfer */
  171. usb0_function_enable_bemp_int(pipe); /* Enable Empty Interrupt */
  172. break;
  173. case DEVDRV_USBF_WRITEDMA: /* DMA write */
  174. usb0_function_clear_nrdy_sts(pipe);
  175. usb0_function_enable_nrdy_int(pipe); /* Error (NORES or STALL) */
  176. break;
  177. case DEVDRV_USBF_FIFOERROR: /* FIFO access status */
  178. default:
  179. usb0_function_disable_brdy_int(pipe); /* Disable Ready Interrupt */
  180. usb0_function_disable_bemp_int(pipe); /* Disable Empty Interrupt */
  181. g_usb0_function_pipe_status[pipe] = DEVDRV_USBF_FIFOERROR;
  182. break;
  183. }
  184. return status; /* End or Err or Continue */
  185. }
  186. /*******************************************************************************
  187. * Function Name: usb0_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 usb0_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_usb0_function_CtrZeroLengthFlag == 1)
  206. {
  207. g_usb0_function_CtrZeroLengthFlag = 0; /* Zero Length Packet Flag CLR */
  208. return DEVDRV_USBF_WRITEEND;
  209. }
  210. mbw = usb0_function_get_mbw(g_usb0_function_data_count[pipe], (uint32_t)g_usb0_function_data_pointer[pipe]);
  211. if (pipe == USB_FUNCTION_PIPE0)
  212. {
  213. buffer = usb0_function_change_fifo_port(pipe, USB_FUNCTION_CUSE, USB_FUNCTION_CFIFO_WRITE, mbw);
  214. }
  215. else
  216. {
  217. buffer = usb0_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 = usb0_function_get_buf_size(pipe); /* Data buffer size */
  224. mxps = usb0_function_get_mxps(pipe); /* Max Packet Size */
  225. if (g_usb0_function_data_count[pipe] <= (uint32_t)size)
  226. {
  227. status = DEVDRV_USBF_WRITEEND; /* write continues */
  228. count = g_usb0_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. usb0_function_write_c_fifo(pipe, (uint16_t)count);
  244. if (g_usb0_function_data_count[pipe] < (uint32_t)size)
  245. {
  246. g_usb0_function_data_count[pipe] = 0;
  247. if (RZA_IO_RegRead_16(&USB200.CFIFOCTR, USB_CFIFOCTR_BVAL_SHIFT, USB_CFIFOCTR_BVAL) == 0)
  248. {
  249. USB200.CFIFOCTR = USB_FUNCTION_BITBVAL; /* Short Packet */
  250. g_usb0_function_CtrZeroLengthFlag = 1; /* Zero Length Packet Flag */
  251. }
  252. }
  253. else
  254. {
  255. g_usb0_function_data_count[pipe] -= count;
  256. }
  257. return status; /* End or Err or Continue */
  258. }
  259. /*******************************************************************************
  260. * Function Name: usb0_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 usb0_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 = usb0_function_get_mbw(g_usb0_function_data_count[pipe], (uint32_t)g_usb0_function_data_pointer[pipe]);
  279. buffer = usb0_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 = usb0_function_get_buf_size(pipe); /* Data buffer size */
  285. mxps = usb0_function_get_mxps(pipe); /* Max Packet Size */
  286. if (g_usb0_function_data_count[pipe] <= (uint32_t)size)
  287. {
  288. status = DEVDRV_USBF_WRITEEND; /* write continues */
  289. count = g_usb0_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. usb0_function_write_d0_fifo(pipe, (uint16_t)count);
  305. if (g_usb0_function_data_count[pipe] < (uint32_t)size)
  306. {
  307. g_usb0_function_data_count[pipe] = 0;
  308. if (RZA_IO_RegRead_16(&USB200.D0FIFOCTR, USB_DnFIFOCTR_BVAL_SHIFT, USB_DnFIFOCTR_BVAL) == 0)
  309. {
  310. USB200.D0FIFOCTR = USB_FUNCTION_BITBVAL; /* Short Packet */
  311. }
  312. }
  313. else
  314. {
  315. g_usb0_function_data_count[pipe] -= count;
  316. }
  317. return status; /* End or Err or Continue */
  318. }
  319. /*******************************************************************************
  320. * Function Name: usb0_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 usb0_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 = usb0_function_get_mbw(g_usb0_function_data_count[pipe], (uint32_t)g_usb0_function_data_pointer[pipe]);
  339. buffer = usb0_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 = usb0_function_get_buf_size(pipe); /* Data buffer size */
  345. mxps = usb0_function_get_mxps(pipe); /* Max Packet Size */
  346. if (g_usb0_function_data_count[pipe] <= (uint32_t)size)
  347. {
  348. status = DEVDRV_USBF_WRITEEND; /* write continues */
  349. count = g_usb0_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. usb0_function_write_d1_fifo(pipe, (uint16_t)count);
  365. if (g_usb0_function_data_count[pipe] < (uint32_t)size)
  366. {
  367. g_usb0_function_data_count[pipe] = 0;
  368. if (RZA_IO_RegRead_16(&USB200.D1FIFOCTR, USB_DnFIFOCTR_BVAL_SHIFT, USB_DnFIFOCTR_BVAL) == 0)
  369. {
  370. USB200.D1FIFOCTR = USB_FUNCTION_BITBVAL; /* Short Packet */
  371. }
  372. }
  373. else
  374. {
  375. g_usb0_function_data_count[pipe] -= count;
  376. }
  377. return status; /* End or Err or Continue */
  378. }
  379. /*******************************************************************************
  380. * Function Name: usb0_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_usb0_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 usb0_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 = usb0_function_get_mbw(g_usb0_function_data_count[pipe], (uint32_t)g_usb0_function_data_pointer[pipe]);
  400. buffer = usb0_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 = usb0_function_get_buf_size(pipe); /* Data buffer size */
  406. count = g_usb0_function_data_count[pipe];
  407. if (count != 0)
  408. {
  409. g_usb0_function_DmaPipe[USB_FUNCTION_D0FIFO] = pipe;
  410. if ((count % size) != 0)
  411. {
  412. g_usb0_function_DmaBval[USB_FUNCTION_D0FIFO] = 1;
  413. }
  414. else
  415. {
  416. g_usb0_function_DmaBval[USB_FUNCTION_D0FIFO] = 0;
  417. }
  418. dfacc = usb0_function_set_dfacc_d0(mbw, count);
  419. if (mbw == USB_FUNCTION_BITMBW_32)
  420. {
  421. g_usb0_function_DmaInfo[USB_FUNCTION_D0FIFO].size = 2; /* 32bit transfer */
  422. }
  423. else if (mbw == USB_FUNCTION_BITMBW_16)
  424. {
  425. g_usb0_function_DmaInfo[USB_FUNCTION_D0FIFO].size = 1; /* 16bit transfer */
  426. }
  427. else
  428. {
  429. g_usb0_function_DmaInfo[USB_FUNCTION_D0FIFO].size = 0; /* 8bit transfer */
  430. }
  431. g_usb0_function_DmaInfo[USB_FUNCTION_D0FIFO].fifo = USB_FUNCTION_D0FIFO_DMA;
  432. g_usb0_function_DmaInfo[USB_FUNCTION_D0FIFO].dir = USB_FUNCTION_BUF2FIFO;
  433. g_usb0_function_DmaInfo[USB_FUNCTION_D0FIFO].buffer = (uint32_t)g_usb0_function_data_pointer[pipe];
  434. g_usb0_function_DmaInfo[USB_FUNCTION_D0FIFO].bytes = count;
  435. Userdef_USB_usb0_function_start_dma(&g_usb0_function_DmaInfo[USB_FUNCTION_D0FIFO], dfacc);
  436. usb0_function_set_curpipe2(pipe, USB_FUNCTION_D0DMA, DEVDRV_USBF_NO, mbw, dfacc);
  437. RZA_IO_RegWrite_16(&USB200.D0FIFOSEL, 1, USB_DnFIFOSEL_DREQE_SHIFT, USB_DnFIFOSEL_DREQE);
  438. g_usb0_function_data_count[pipe] = 0;
  439. g_usb0_function_data_pointer[pipe] += count;
  440. status = DEVDRV_USBF_WRITEDMA; /* DMA write */
  441. }
  442. else
  443. {
  444. if (RZA_IO_RegRead_16(&USB200.D0FIFOCTR, USB_DnFIFOCTR_BVAL_SHIFT, USB_DnFIFOCTR_BVAL) == 0)
  445. {
  446. RZA_IO_RegWrite_16(&USB200.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: usb0_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_usb0_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 usb0_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 = usb0_function_get_mbw(g_usb0_function_data_count[pipe], (uint32_t)g_usb0_function_data_pointer[pipe]);
  473. buffer = usb0_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 = usb0_function_get_buf_size(pipe); /* Data buffer size */
  479. count = g_usb0_function_data_count[pipe];
  480. if (count != 0)
  481. {
  482. g_usb0_function_DmaPipe[USB_FUNCTION_D1FIFO] = pipe;
  483. if ((count % size) != 0)
  484. {
  485. g_usb0_function_DmaBval[USB_FUNCTION_D1FIFO] = 1;
  486. }
  487. else
  488. {
  489. g_usb0_function_DmaBval[USB_FUNCTION_D1FIFO] = 0;
  490. }
  491. dfacc = usb0_function_set_dfacc_d1(mbw, count);
  492. if (mbw == USB_FUNCTION_BITMBW_32)
  493. {
  494. g_usb0_function_DmaInfo[USB_FUNCTION_D1FIFO].size = 2; /* 32bit transfer */
  495. }
  496. else if (mbw == USB_FUNCTION_BITMBW_16)
  497. {
  498. g_usb0_function_DmaInfo[USB_FUNCTION_D1FIFO].size = 1; /* 16bit transfer */
  499. }
  500. else
  501. {
  502. g_usb0_function_DmaInfo[USB_FUNCTION_D1FIFO].size = 0; /* 8bit transfer */
  503. }
  504. g_usb0_function_DmaInfo[USB_FUNCTION_D1FIFO].fifo = USB_FUNCTION_D1FIFO_DMA;
  505. g_usb0_function_DmaInfo[USB_FUNCTION_D1FIFO].dir = USB_FUNCTION_BUF2FIFO;
  506. g_usb0_function_DmaInfo[USB_FUNCTION_D1FIFO].buffer = (uint32_t)g_usb0_function_data_pointer[pipe];
  507. g_usb0_function_DmaInfo[USB_FUNCTION_D1FIFO].bytes = count;
  508. Userdef_USB_usb0_function_start_dma(&g_usb0_function_DmaInfo[USB_FUNCTION_D1FIFO], dfacc);
  509. usb0_function_set_curpipe2(pipe, USB_FUNCTION_D1DMA, DEVDRV_USBF_NO, mbw, dfacc);
  510. RZA_IO_RegWrite_16(&USB200.D1FIFOSEL, 1, USB_DnFIFOSEL_DREQE_SHIFT, USB_DnFIFOSEL_DREQE);
  511. g_usb0_function_data_count[pipe] = 0;
  512. g_usb0_function_data_pointer[pipe] += count;
  513. status = DEVDRV_USBF_WRITEDMA; /* DMA write */
  514. }
  515. else
  516. {
  517. if (RZA_IO_RegRead_16(&USB200.D1FIFOCTR, USB_DnFIFOCTR_BVAL_SHIFT, USB_DnFIFOCTR_BVAL) == 0)
  518. {
  519. RZA_IO_RegWrite_16(&USB200.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: usb0_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 usb0_function_start_receive_transfer (uint16_t pipe, uint32_t size, uint8_t * data)
  535. {
  536. uint16_t usefifo;
  537. usb0_function_clear_bemp_sts(pipe);
  538. usb0_function_clear_brdy_sts(pipe);
  539. usb0_function_clear_nrdy_sts(pipe);
  540. usefifo = (uint16_t)(g_usb0_function_PipeTbl[pipe] & USB_FUNCTION_FIFO_USE);
  541. switch (usefifo)
  542. {
  543. case USB_FUNCTION_D0FIFO_USE:
  544. usb0_function_start_receive_trns_d0(pipe, size, data);
  545. break;
  546. case USB_FUNCTION_D1FIFO_USE:
  547. usb0_function_start_receive_trns_d1(pipe, size, data);
  548. break;
  549. case USB_FUNCTION_D0FIFO_DMA:
  550. usb0_function_start_receive_dma_d0(pipe, size, data);
  551. break;
  552. case USB_FUNCTION_D1FIFO_DMA:
  553. usb0_function_start_receive_dma_d1(pipe, size, data);
  554. break;
  555. default:
  556. usb0_function_start_receive_trns_c(pipe, size, data);
  557. break;
  558. }
  559. }
  560. /*******************************************************************************
  561. * Function Name: usb0_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 usb0_function_start_receive_trns_c (uint16_t pipe, uint32_t size, uint8_t * data)
  573. {
  574. uint16_t mbw;
  575. usb0_function_set_pid_nak(pipe);
  576. g_usb0_function_data_count[pipe] = size;
  577. g_usb0_function_data_pointer[pipe] = (uint8_t *)data;
  578. g_usb0_function_PipeIgnore[pipe] = 0;
  579. g_usb0_function_PipeDataSize[pipe] = size;
  580. g_usb0_function_pipe_status[pipe] = DEVDRV_USBF_PIPE_WAIT;
  581. mbw = usb0_function_get_mbw(size, (uint32_t)data);
  582. usb0_function_set_curpipe(USB_FUNCTION_PIPE0, USB_FUNCTION_CUSE, USB_FUNCTION_CFIFO_READ, mbw);
  583. USB200.CFIFOCTR = USB_FUNCTION_BITBCLR;
  584. usb0_function_set_transaction_counter(pipe, size);
  585. usb0_function_aclrm(pipe);
  586. usb0_function_enable_nrdy_int(pipe);
  587. usb0_function_enable_brdy_int(pipe);
  588. usb0_function_set_pid_buf(pipe);
  589. }
  590. /*******************************************************************************
  591. * Function Name: usb0_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 usb0_function_start_receive_trns_d0 (uint16_t pipe, uint32_t size, uint8_t * data)
  604. {
  605. uint16_t mbw;
  606. usb0_function_set_pid_nak(pipe);
  607. g_usb0_function_data_count[pipe] = size;
  608. g_usb0_function_data_pointer[pipe] = (uint8_t *)data;
  609. g_usb0_function_PipeIgnore[pipe] = 0;
  610. g_usb0_function_PipeDataSize[pipe] = size;
  611. g_usb0_function_pipe_status[pipe] = DEVDRV_USBF_PIPE_WAIT;
  612. mbw = usb0_function_get_mbw(size, (uint32_t)data);
  613. usb0_function_set_curpipe(USB_FUNCTION_PIPE0, USB_FUNCTION_D0USE, DEVDRV_USBF_NO, mbw);
  614. usb0_function_set_transaction_counter(pipe, size);
  615. usb0_function_aclrm(pipe);
  616. usb0_function_enable_nrdy_int(pipe);
  617. usb0_function_enable_brdy_int(pipe);
  618. usb0_function_set_pid_buf(pipe);
  619. }
  620. /*******************************************************************************
  621. * Function Name: usb0_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 usb0_function_start_receive_trns_d1 (uint16_t pipe, uint32_t size, uint8_t * data)
  633. {
  634. uint16_t mbw;
  635. usb0_function_set_pid_nak(pipe);
  636. g_usb0_function_data_count[pipe] = size;
  637. g_usb0_function_data_pointer[pipe] = (uint8_t *)data;
  638. g_usb0_function_PipeIgnore[pipe] = 0;
  639. g_usb0_function_PipeDataSize[pipe] = size;
  640. g_usb0_function_pipe_status[pipe] = DEVDRV_USBF_PIPE_WAIT;
  641. mbw = usb0_function_get_mbw(size, (uint32_t)data);
  642. usb0_function_set_curpipe(USB_FUNCTION_PIPE0, USB_FUNCTION_D1USE, DEVDRV_USBF_NO, mbw);
  643. usb0_function_set_transaction_counter(pipe, size);
  644. usb0_function_aclrm(pipe);
  645. usb0_function_enable_nrdy_int(pipe);
  646. usb0_function_enable_brdy_int(pipe);
  647. usb0_function_set_pid_buf(pipe);
  648. }
  649. /*******************************************************************************
  650. * Function Name: usb0_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 usb0_function_start_receive_dma_d0 (uint16_t pipe, uint32_t size, uint8_t * data)
  663. {
  664. uint16_t mbw;
  665. usb0_function_set_pid_nak(pipe);
  666. g_usb0_function_data_count[pipe] = size;
  667. g_usb0_function_data_pointer[pipe] = (uint8_t *)data;
  668. g_usb0_function_PipeIgnore[pipe] = 0;
  669. g_usb0_function_PipeDataSize[pipe] = 0;
  670. g_usb0_function_pipe_status[pipe] = DEVDRV_USBF_PIPE_WAIT;
  671. mbw = usb0_function_get_mbw(size, (uint32_t)data);
  672. usb0_function_set_curpipe(USB_FUNCTION_PIPE0, USB_FUNCTION_D0USE, DEVDRV_USBF_NO, mbw);
  673. usb0_function_set_transaction_counter(pipe, size);
  674. usb0_function_aclrm(pipe);
  675. if (RZA_IO_RegRead_16(&g_usb0_function_pipecfg[pipe], USB_PIPECFG_BFRE_SHIFT, USB_PIPECFG_BFRE) == 1)
  676. {
  677. usb0_function_read_dma(pipe);
  678. usb0_function_enable_nrdy_int(pipe);
  679. usb0_function_enable_brdy_int(pipe);
  680. }
  681. else
  682. {
  683. usb0_function_enable_nrdy_int(pipe);
  684. usb0_function_enable_brdy_int(pipe);
  685. }
  686. usb0_function_set_pid_buf(pipe);
  687. }
  688. /*******************************************************************************
  689. * Function Name: usb0_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 usb0_function_start_receive_dma_d1 (uint16_t pipe, uint32_t size, uint8_t * data)
  702. {
  703. uint16_t mbw;
  704. usb0_function_set_pid_nak(pipe);
  705. g_usb0_function_data_count[pipe] = size;
  706. g_usb0_function_data_pointer[pipe] = (uint8_t *)data;
  707. g_usb0_function_PipeIgnore[pipe] = 0;
  708. g_usb0_function_PipeDataSize[pipe] = 0;
  709. g_usb0_function_pipe_status[pipe] = DEVDRV_USBF_PIPE_WAIT;
  710. mbw = usb0_function_get_mbw(size, (uint32_t)data);
  711. usb0_function_set_curpipe(USB_FUNCTION_PIPE0, USB_FUNCTION_D1USE, DEVDRV_USBF_NO, mbw);
  712. usb0_function_set_transaction_counter(pipe, size);
  713. usb0_function_aclrm(pipe);
  714. if (RZA_IO_RegRead_16(&g_usb0_function_pipecfg[pipe], USB_PIPECFG_BFRE_SHIFT, USB_PIPECFG_BFRE) == 1)
  715. {
  716. usb0_function_read_dma(pipe);
  717. usb0_function_enable_nrdy_int(pipe);
  718. usb0_function_enable_brdy_int(pipe);
  719. }
  720. else
  721. {
  722. usb0_function_enable_nrdy_int(pipe);
  723. usb0_function_enable_brdy_int(pipe);
  724. }
  725. usb0_function_set_pid_buf(pipe);
  726. }
  727. /*******************************************************************************
  728. * Function Name: usb0_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 usb0_function_read_buffer (uint16_t pipe)
  740. {
  741. uint16_t status;
  742. g_usb0_function_PipeIgnore[pipe] = 0;
  743. if ((g_usb0_function_PipeTbl[pipe] & USB_FUNCTION_FIFO_USE) == USB_FUNCTION_D0FIFO_USE)
  744. {
  745. status = usb0_function_read_buffer_d0(pipe);
  746. }
  747. else if ((g_usb0_function_PipeTbl[pipe] & USB_FUNCTION_FIFO_USE) == USB_FUNCTION_D1FIFO_USE)
  748. {
  749. status = usb0_function_read_buffer_d1(pipe);
  750. }
  751. else
  752. {
  753. status = usb0_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. usb0_function_disable_brdy_int(pipe);
  762. g_usb0_function_PipeDataSize[pipe] -= g_usb0_function_data_count[pipe];
  763. g_usb0_function_pipe_status[pipe] = DEVDRV_USBF_PIPE_DONE;
  764. break;
  765. case USB_FUNCTION_READOVER: /* buffer over */
  766. if ((g_usb0_function_PipeTbl[pipe] & USB_FUNCTION_FIFO_USE) == USB_FUNCTION_D0FIFO_USE)
  767. {
  768. USB200.D0FIFOCTR = USB_FUNCTION_BITBCLR; /* Clear BCLR */
  769. }
  770. else if ((g_usb0_function_PipeTbl[pipe] & USB_FUNCTION_FIFO_USE) == USB_FUNCTION_D1FIFO_USE)
  771. {
  772. USB200.D1FIFOCTR = USB_FUNCTION_BITBCLR; /* Clear BCLR */
  773. }
  774. else
  775. {
  776. USB200.CFIFOCTR = USB_FUNCTION_BITBCLR; /* Clear BCLR */
  777. }
  778. usb0_function_disable_brdy_int(pipe); /* Disable Ready Interrupt */
  779. g_usb0_function_PipeDataSize[pipe] -= g_usb0_function_data_count[pipe];
  780. g_usb0_function_pipe_status[pipe] = DEVDRV_USBF_FIFOERROR;
  781. break;
  782. case DEVDRV_USBF_FIFOERROR: /* FIFO access status */
  783. default:
  784. usb0_function_disable_brdy_int(pipe); /* Disable Ready Interrupt */
  785. g_usb0_function_pipe_status[pipe] = DEVDRV_USBF_FIFOERROR;
  786. break;
  787. }
  788. return status; /* End or Err or Continue */
  789. }
  790. /*******************************************************************************
  791. * Function Name: usb0_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 usb0_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 = usb0_function_get_mbw(g_usb0_function_data_count[pipe], (uint32_t)g_usb0_function_data_pointer[pipe]);
  810. buffer = usb0_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 = usb0_function_get_mxps(pipe); /* Max Packet Size */
  817. if (g_usb0_function_data_count[pipe] < dtln) /* Buffer Over ? */
  818. {
  819. status = USB_FUNCTION_READOVER;
  820. usb0_function_set_pid_nak(pipe); /* Set NAK */
  821. count = g_usb0_function_data_count[pipe];
  822. }
  823. else if (g_usb0_function_data_count[pipe] == dtln) /* just Receive Size */
  824. {
  825. status = USB_FUNCTION_READEND;
  826. usb0_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. usb0_function_set_pid_nak(pipe); /* Set NAK */
  845. }
  846. if ((count % mxps) != 0)
  847. {
  848. status = USB_FUNCTION_READSHRT; /* Short Packet receive */
  849. usb0_function_set_pid_nak(pipe); /* Set NAK */
  850. }
  851. }
  852. if (count == 0) /* 0 length packet */
  853. {
  854. USB200.CFIFOCTR = USB_FUNCTION_BITBCLR; /* Clear BCLR */
  855. }
  856. else
  857. {
  858. usb0_function_read_c_fifo(pipe, (uint16_t)count);
  859. }
  860. g_usb0_function_data_count[pipe] -= count;
  861. return status; /* End or Err or Continue */
  862. }
  863. /*******************************************************************************
  864. * Function Name: usb0_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 usb0_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 = usb0_function_get_mbw(g_usb0_function_data_count[pipe], (uint32_t)g_usb0_function_data_pointer[pipe]);
  885. buffer = usb0_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 = usb0_function_get_mxps(pipe); /* Max Packet Size */
  892. if (g_usb0_function_data_count[pipe] < dtln) /* Buffer Over ? */
  893. {
  894. status = USB_FUNCTION_READOVER;
  895. usb0_function_set_pid_nak(pipe); /* Set NAK */
  896. count = g_usb0_function_data_count[pipe];
  897. }
  898. else if (g_usb0_function_data_count[pipe] == dtln) /* just Receive Size */
  899. {
  900. status = USB_FUNCTION_READEND;
  901. usb0_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. usb0_function_set_pid_nak(pipe); /* Set NAK */
  920. }
  921. if ((count % mxps) != 0)
  922. {
  923. status = USB_FUNCTION_READSHRT; /* Short Packet receive */
  924. usb0_function_set_pid_nak(pipe); /* Set NAK */
  925. }
  926. else
  927. {
  928. pipebuf_size = usb0_function_get_buf_size(pipe); /* Data buffer size */
  929. if (count != pipebuf_size)
  930. {
  931. status = USB_FUNCTION_READSHRT; /* Short Packet receive */
  932. usb0_function_set_pid_nak(pipe); /* Set NAK */
  933. }
  934. }
  935. }
  936. if (count == 0) /* 0 length packet */
  937. {
  938. USB200.D0FIFOCTR = USB_FUNCTION_BITBCLR; /* Clear BCLR */
  939. }
  940. else
  941. {
  942. usb0_function_read_d0_fifo(pipe, (uint16_t)count);
  943. }
  944. g_usb0_function_data_count[pipe] -= count;
  945. return status; /* End or Err or Continue */
  946. }
  947. /*******************************************************************************
  948. * Function Name: usb0_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 usb0_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 = usb0_function_get_mbw(g_usb0_function_data_count[pipe], (uint32_t)g_usb0_function_data_pointer[pipe]);
  969. buffer = usb0_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 = usb0_function_get_mxps(pipe); /* Max Packet Size */
  976. if (g_usb0_function_data_count[pipe] < dtln) /* Buffer Over ? */
  977. {
  978. status = USB_FUNCTION_READOVER;
  979. usb0_function_set_pid_nak(pipe); /* Set NAK */
  980. count = g_usb0_function_data_count[pipe];
  981. }
  982. else if (g_usb0_function_data_count[pipe] == dtln) /* just Receive Size */
  983. {
  984. status = USB_FUNCTION_READEND;
  985. usb0_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. usb0_function_set_pid_nak(pipe); /* Set NAK */
  1004. }
  1005. if ((count % mxps) != 0)
  1006. {
  1007. status = USB_FUNCTION_READSHRT; /* Short Packet receive */
  1008. usb0_function_set_pid_nak(pipe); /* Set NAK */
  1009. }
  1010. else
  1011. {
  1012. pipebuf_size = usb0_function_get_buf_size(pipe); /* Data buffer size */
  1013. if (count != pipebuf_size)
  1014. {
  1015. status = USB_FUNCTION_READSHRT; /* Short Packet receive */
  1016. usb0_function_set_pid_nak(pipe); /* Set NAK */
  1017. }
  1018. }
  1019. }
  1020. if (count == 0) /* 0 length packet */
  1021. {
  1022. USB200.D1FIFOCTR = USB_FUNCTION_BITBCLR; /* Clear BCLR */
  1023. }
  1024. else
  1025. {
  1026. usb0_function_read_d1_fifo(pipe, (uint16_t)count);
  1027. }
  1028. g_usb0_function_data_count[pipe] -= count;
  1029. return status; /* End or Err or Continue */
  1030. }
  1031. /*******************************************************************************
  1032. * Function Name: usb0_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 usb0_function_read_dma (uint16_t pipe)
  1044. {
  1045. uint16_t status;
  1046. g_usb0_function_PipeIgnore[pipe] = 0;
  1047. if ((g_usb0_function_PipeTbl[pipe] & USB_FUNCTION_FIFO_USE) == USB_FUNCTION_D0FIFO_DMA)
  1048. {
  1049. status = usb0_function_read_dma_d0(pipe);
  1050. }
  1051. else
  1052. {
  1053. status = usb0_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. usb0_function_disable_brdy_int(pipe);
  1061. g_usb0_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. usb0_function_disable_brdy_int(pipe);
  1066. if (RZA_IO_RegRead_16(&g_usb0_function_pipecfg[pipe], USB_PIPECFG_BFRE_SHIFT, USB_PIPECFG_BFRE) == 1)
  1067. {
  1068. g_usb0_function_PipeDataSize[pipe] -= g_usb0_function_data_count[pipe];
  1069. }
  1070. break;
  1071. case USB_FUNCTION_READOVER: /* buffer over */
  1072. usb0_function_disable_brdy_int(pipe); /* Disable Ready Interrupt */
  1073. if (RZA_IO_RegRead_16(&g_usb0_function_pipecfg[pipe], USB_PIPECFG_BFRE_SHIFT, USB_PIPECFG_BFRE) == 1)
  1074. {
  1075. g_usb0_function_PipeDataSize[pipe] -= g_usb0_function_data_count[pipe];
  1076. }
  1077. g_usb0_function_pipe_status[pipe] = DEVDRV_USBF_FIFOERROR;
  1078. break;
  1079. case DEVDRV_USBF_FIFOERROR: /* FIFO access status */
  1080. default:
  1081. usb0_function_disable_brdy_int(pipe); /* Disable Ready Interrupt */
  1082. g_usb0_function_pipe_status[pipe] = DEVDRV_USBF_FIFOERROR;
  1083. break;
  1084. }
  1085. return status; /* End or Err or Continue */
  1086. }
  1087. /*******************************************************************************
  1088. * Function Name: usb0_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 usb0_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_usb0_function_DmaStatus[USB_FUNCTION_D0FIFO] = USB_FUNCTION_DMA_READY;
  1111. mbw = usb0_function_get_mbw(g_usb0_function_data_count[pipe], (uint32_t)g_usb0_function_data_pointer[pipe]);
  1112. if (RZA_IO_RegRead_16(&g_usb0_function_pipecfg[pipe], USB_PIPECFG_BFRE_SHIFT, USB_PIPECFG_BFRE) == 1)
  1113. {
  1114. count = g_usb0_function_data_count[pipe];
  1115. status = USB_FUNCTION_READING;
  1116. }
  1117. else
  1118. {
  1119. buffer = usb0_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 = usb0_function_get_mxps(pipe); /* Max Packet Size */
  1126. if (g_usb0_function_data_count[pipe] < dtln) /* Buffer Over ? */
  1127. {
  1128. status = USB_FUNCTION_READOVER;
  1129. count = g_usb0_function_data_count[pipe];
  1130. }
  1131. else if (g_usb0_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 = usb0_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_usb0_function_pipecfg[pipe], USB_PIPECFG_BFRE_SHIFT, USB_PIPECFG_BFRE) == 0)
  1169. {
  1170. USB200.D0FIFOCTR = USB_FUNCTION_BITBCLR; /* Clear BCLR */
  1171. status = USB_FUNCTION_READZERO; /* Null Packet receive */
  1172. }
  1173. else
  1174. {
  1175. usb0_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 = usb0_function_set_dfacc_d0(mbw, count);
  1183. if (mbw == USB_FUNCTION_BITMBW_32)
  1184. {
  1185. g_usb0_function_DmaInfo[USB_FUNCTION_D0FIFO].size = 2; /* 32bit transfer */
  1186. }
  1187. else if (mbw == USB_FUNCTION_BITMBW_16)
  1188. {
  1189. g_usb0_function_DmaInfo[USB_FUNCTION_D0FIFO].size = 1; /* 16bit transfer */
  1190. }
  1191. else
  1192. {
  1193. g_usb0_function_DmaInfo[USB_FUNCTION_D0FIFO].size = 0; /* 8bit transfer */
  1194. }
  1195. g_usb0_function_DmaPipe[USB_FUNCTION_D0FIFO] = pipe; /* not use in read operation */
  1196. g_usb0_function_DmaBval[USB_FUNCTION_D0FIFO] = 0; /* not use in read operation */
  1197. g_usb0_function_DmaInfo[USB_FUNCTION_D0FIFO].fifo = USB_FUNCTION_D0FIFO_DMA;
  1198. g_usb0_function_DmaInfo[USB_FUNCTION_D0FIFO].dir = USB_FUNCTION_FIFO2BUF;
  1199. g_usb0_function_DmaInfo[USB_FUNCTION_D0FIFO].buffer = (uint32_t)g_usb0_function_data_pointer[pipe];
  1200. g_usb0_function_DmaInfo[USB_FUNCTION_D0FIFO].bytes = count;
  1201. if (status == USB_FUNCTION_READING)
  1202. {
  1203. g_usb0_function_DmaStatus[USB_FUNCTION_D0FIFO] = USB_FUNCTION_DMA_BUSY;
  1204. }
  1205. else
  1206. {
  1207. g_usb0_function_DmaStatus[USB_FUNCTION_D0FIFO] = USB_FUNCTION_DMA_BUSYEND;
  1208. }
  1209. Userdef_USB_usb0_function_start_dma(&g_usb0_function_DmaInfo[USB_FUNCTION_D0FIFO], dfacc);
  1210. usb0_function_set_curpipe2(pipe, USB_FUNCTION_D0DMA, DEVDRV_USBF_NO, mbw, dfacc);
  1211. RZA_IO_RegWrite_16(&USB200.D0FIFOSEL,
  1212. 1,
  1213. USB_DnFIFOSEL_DREQE_SHIFT,
  1214. USB_DnFIFOSEL_DREQE);
  1215. }
  1216. if (RZA_IO_RegRead_16(&g_usb0_function_pipecfg[pipe], USB_PIPECFG_BFRE_SHIFT, USB_PIPECFG_BFRE) == 0)
  1217. {
  1218. g_usb0_function_data_count[pipe] -= count;
  1219. g_usb0_function_data_pointer[pipe] += count;
  1220. g_usb0_function_PipeDataSize[pipe] += count;
  1221. }
  1222. return status; /* End or Err or Continue */
  1223. }
  1224. /*******************************************************************************
  1225. * Function Name: usb0_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 usb0_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_usb0_function_DmaStatus[USB_FUNCTION_D1FIFO] = USB_FUNCTION_DMA_READY;
  1248. mbw = usb0_function_get_mbw(g_usb0_function_data_count[pipe], (uint32_t)g_usb0_function_data_pointer[pipe]);
  1249. if (RZA_IO_RegRead_16(&g_usb0_function_pipecfg[pipe], USB_PIPECFG_BFRE_SHIFT, USB_PIPECFG_BFRE) == 1)
  1250. {
  1251. count = g_usb0_function_data_count[pipe];
  1252. status = USB_FUNCTION_READING;
  1253. }
  1254. else
  1255. {
  1256. buffer = usb0_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 = usb0_function_get_mxps(pipe); /* Max Packet Size */
  1263. if (g_usb0_function_data_count[pipe] < dtln) /* Buffer Over ? */
  1264. {
  1265. status = USB_FUNCTION_READOVER;
  1266. count = g_usb0_function_data_count[pipe];
  1267. }
  1268. else if (g_usb0_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 = usb0_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_usb0_function_pipecfg[pipe], USB_PIPECFG_BFRE_SHIFT, USB_PIPECFG_BFRE) == 0)
  1306. {
  1307. USB200.D1FIFOCTR = USB_FUNCTION_BITBCLR; /* Clear BCLR */
  1308. status = USB_FUNCTION_READZERO; /* Null Packet receive */
  1309. }
  1310. else
  1311. {
  1312. usb0_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 = usb0_function_set_dfacc_d1(mbw, count);
  1320. if (mbw == USB_FUNCTION_BITMBW_32)
  1321. {
  1322. g_usb0_function_DmaInfo[USB_FUNCTION_D1FIFO].size = 2; /* 32bit transfer */
  1323. }
  1324. else if (mbw == USB_FUNCTION_BITMBW_16)
  1325. {
  1326. g_usb0_function_DmaInfo[USB_FUNCTION_D1FIFO].size = 1; /* 16bit transfer */
  1327. }
  1328. else
  1329. {
  1330. g_usb0_function_DmaInfo[USB_FUNCTION_D1FIFO].size = 0; /* 8bit transfer */
  1331. }
  1332. g_usb0_function_DmaPipe[USB_FUNCTION_D1FIFO] = pipe; /* not use in read operation */
  1333. g_usb0_function_DmaBval[USB_FUNCTION_D1FIFO] = 0; /* not use in read operation */
  1334. g_usb0_function_DmaInfo[USB_FUNCTION_D1FIFO].fifo = USB_FUNCTION_D1FIFO_DMA;
  1335. g_usb0_function_DmaInfo[USB_FUNCTION_D1FIFO].dir = USB_FUNCTION_FIFO2BUF;
  1336. g_usb0_function_DmaInfo[USB_FUNCTION_D1FIFO].buffer = (uint32_t)g_usb0_function_data_pointer[pipe];
  1337. g_usb0_function_DmaInfo[USB_FUNCTION_D1FIFO].bytes = count;
  1338. if (status == USB_FUNCTION_READING)
  1339. {
  1340. g_usb0_function_DmaStatus[USB_FUNCTION_D1FIFO] = USB_FUNCTION_DMA_BUSY;
  1341. }
  1342. else
  1343. {
  1344. g_usb0_function_DmaStatus[USB_FUNCTION_D1FIFO] = USB_FUNCTION_DMA_BUSYEND;
  1345. }
  1346. Userdef_USB_usb0_function_start_dma(&g_usb0_function_DmaInfo[USB_FUNCTION_D1FIFO], dfacc);
  1347. usb0_function_set_curpipe2(pipe, USB_FUNCTION_D1DMA, DEVDRV_USBF_NO, mbw, dfacc);
  1348. RZA_IO_RegWrite_16(&USB200.D1FIFOSEL,
  1349. 1,
  1350. USB_DnFIFOSEL_DREQE_SHIFT,
  1351. USB_DnFIFOSEL_DREQE);
  1352. }
  1353. if (RZA_IO_RegRead_16(&g_usb0_function_pipecfg[pipe], USB_PIPECFG_BFRE_SHIFT, USB_PIPECFG_BFRE) == 0)
  1354. {
  1355. g_usb0_function_data_count[pipe] -= count;
  1356. g_usb0_function_data_pointer[pipe] += count;
  1357. g_usb0_function_PipeDataSize[pipe] += count;
  1358. }
  1359. return status; /* End or Err or Continue */
  1360. }
  1361. /*******************************************************************************
  1362. * Function Name: usb0_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 usb0_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. usb0_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 = USB200.CFIFOCTR;
  1385. break;
  1386. case USB_FUNCTION_D0USE:
  1387. case USB_FUNCTION_D0DMA:
  1388. buffer = USB200.D0FIFOCTR;
  1389. break;
  1390. case USB_FUNCTION_D1USE:
  1391. case USB_FUNCTION_D1DMA:
  1392. buffer = USB200.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: usb0_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 usb0_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_usb0_function_mbw[pipe] = mbw;
  1426. switch (fifosel)
  1427. {
  1428. case USB_FUNCTION_CUSE:
  1429. buffer = USB200.CFIFOSEL;
  1430. buffer &= (uint16_t)~(USB_FUNCTION_BITISEL | USB_FUNCTION_BITCURPIPE);
  1431. buffer |= (uint16_t)(~isel & USB_FUNCTION_BITISEL);
  1432. USB200.CFIFOSEL = buffer;
  1433. for (loop = 0; loop < 4; loop++)
  1434. {
  1435. if ((USB200.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. USB200.CFIFOSEL = buffer;
  1449. for (loop = 0; loop < 4; loop++)
  1450. {
  1451. if ((USB200.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 = USB200.D0FIFOSEL;
  1466. buffer &= (uint16_t)~(USB_FUNCTION_BITCURPIPE);
  1467. USB200.D0FIFOSEL = buffer;
  1468. for (loop = 0; loop < 4; loop++)
  1469. {
  1470. if ((USB200.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. USB200.D0FIFOSEL = buffer;
  1484. for (loop = 0; loop < 4; loop++)
  1485. {
  1486. if ((USB200.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 = USB200.D1FIFOSEL;
  1501. buffer &= (uint16_t)~(USB_FUNCTION_BITCURPIPE);
  1502. USB200.D1FIFOSEL = buffer;
  1503. for (loop = 0; loop < 4; loop++)
  1504. {
  1505. if ((USB200.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. USB200.D1FIFOSEL = buffer;
  1519. for (loop = 0; loop < 4; loop++)
  1520. {
  1521. if ((USB200.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: usb0_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 usb0_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_usb0_function_mbw[pipe] = mbw;
  1565. switch (fifosel)
  1566. {
  1567. case USB_FUNCTION_CUSE:
  1568. buffer = USB200.CFIFOSEL;
  1569. buffer &= (uint16_t)~(USB_FUNCTION_BITISEL | USB_FUNCTION_BITCURPIPE);
  1570. buffer |= (uint16_t)(~isel & USB_FUNCTION_BITISEL);
  1571. USB200.CFIFOSEL = buffer;
  1572. for (loop = 0; loop < 4; loop++)
  1573. {
  1574. if ((USB200.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. USB200.CFIFOSEL = buffer;
  1588. for (loop = 0; loop < 4; loop++)
  1589. {
  1590. if ((USB200.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 = USB200.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. USB200.D0FIFOSEL = buffer;
  1615. for (loop = 0; loop < 4; loop++)
  1616. {
  1617. if ((USB200.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 = USB200.D0FIFO.UINT32;
  1631. }
  1632. #endif
  1633. buffer &= (uint16_t)~(USB_FUNCTION_BITCURPIPE | USB_FUNCTION_BITMBW);
  1634. buffer |= (uint16_t)(pipe | mbw);
  1635. USB200.D0FIFOSEL = buffer;
  1636. for (loop = 0; loop < 4; loop++)
  1637. {
  1638. if ((USB200.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 = USB200.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. USB200.D1FIFOSEL = buffer;
  1662. for (loop = 0; loop < 4; loop++)
  1663. {
  1664. if ((USB200.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 = USB200.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. USB200.D1FIFOSEL = buffer;
  1684. for (loop = 0; loop < 4; loop++)
  1685. {
  1686. if ((USB200.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: usb0_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_usb0_function_mbw[].
  1718. * Arguments : uint16_t pipe ; Pipe Number
  1719. * : uint16_t count ; Data Size(Byte)
  1720. * Return Value : none
  1721. *******************************************************************************/
  1722. static void usb0_function_write_c_fifo (uint16_t pipe, uint16_t count)
  1723. {
  1724. uint16_t even;
  1725. if (g_usb0_function_mbw[pipe] == USB_FUNCTION_BITMBW_8)
  1726. {
  1727. for (even = count; even; --even)
  1728. {
  1729. USB200.CFIFO.UINT8[HH] = *g_usb0_function_data_pointer[pipe];
  1730. g_usb0_function_data_pointer[pipe] += 1;
  1731. }
  1732. }
  1733. else if (g_usb0_function_mbw[pipe] == USB_FUNCTION_BITMBW_16)
  1734. {
  1735. for (even = (uint16_t)(count / 2); even; --even)
  1736. {
  1737. USB200.CFIFO.UINT16[H] = *((uint16_t *)g_usb0_function_data_pointer[pipe]);
  1738. g_usb0_function_data_pointer[pipe] += 2;
  1739. }
  1740. }
  1741. else
  1742. {
  1743. for (even = (uint16_t)(count / 4); even; --even)
  1744. {
  1745. USB200.CFIFO.UINT32 = *((uint32_t *)g_usb0_function_data_pointer[pipe]);
  1746. g_usb0_function_data_pointer[pipe] += 4;
  1747. }
  1748. }
  1749. }
  1750. /*******************************************************************************
  1751. * Function Name: usb0_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_usb0_function_mbw[].
  1759. * Arguments : uint16_t pipe ; Pipe Number
  1760. * : uint16_t count ; Data Size(Byte)
  1761. * Return Value : none
  1762. *******************************************************************************/
  1763. static void usb0_function_read_c_fifo (uint16_t pipe, uint16_t count)
  1764. {
  1765. uint16_t even;
  1766. if (g_usb0_function_mbw[pipe] == USB_FUNCTION_BITMBW_8)
  1767. {
  1768. for (even = count; even; --even)
  1769. {
  1770. *g_usb0_function_data_pointer[pipe] = USB200.CFIFO.UINT8[HH];
  1771. g_usb0_function_data_pointer[pipe] += 1;
  1772. }
  1773. }
  1774. else if (g_usb0_function_mbw[pipe] == USB_FUNCTION_BITMBW_16)
  1775. {
  1776. for (even = (uint16_t)((count + 1) / 2); even; --even)
  1777. {
  1778. *((uint16_t *)g_usb0_function_data_pointer[pipe]) = USB200.CFIFO.UINT16[H];
  1779. g_usb0_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_usb0_function_data_pointer[pipe]) = USB200.CFIFO.UINT32;
  1787. g_usb0_function_data_pointer[pipe] += 4;
  1788. }
  1789. }
  1790. }
  1791. /*******************************************************************************
  1792. * Function Name: usb0_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_usb0_function_mbw[].
  1800. * Arguments : uint16_t pipe ; Pipe Number
  1801. * : uint16_t count ; Data Size(Byte)
  1802. * Return Value : none
  1803. *******************************************************************************/
  1804. static void usb0_function_write_d0_fifo (uint16_t pipe, uint16_t count)
  1805. {
  1806. uint16_t even;
  1807. if (g_usb0_function_mbw[pipe] == USB_FUNCTION_BITMBW_8)
  1808. {
  1809. for (even = count; even; --even)
  1810. {
  1811. USB200.D0FIFO.UINT8[HH] = *g_usb0_function_data_pointer[pipe];
  1812. g_usb0_function_data_pointer[pipe] += 1;
  1813. }
  1814. }
  1815. else if (g_usb0_function_mbw[pipe] == USB_FUNCTION_BITMBW_16)
  1816. {
  1817. for (even = (uint16_t)(count / 2); even; --even)
  1818. {
  1819. USB200.D0FIFO.UINT16[H] = *((uint16_t *)g_usb0_function_data_pointer[pipe]);
  1820. g_usb0_function_data_pointer[pipe] += 2;
  1821. }
  1822. }
  1823. else
  1824. {
  1825. for (even = (uint16_t)(count / 4); even; --even)
  1826. {
  1827. USB200.D0FIFO.UINT32 = *((uint32_t *)g_usb0_function_data_pointer[pipe]);
  1828. g_usb0_function_data_pointer[pipe] += 4;
  1829. }
  1830. }
  1831. }
  1832. /*******************************************************************************
  1833. * Function Name: usb0_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_usb0_function_mbw[].
  1841. * Arguments : uint16_t pipe ; Pipe Number
  1842. * : uint16_t count ; Data Size(Byte)
  1843. * Return Value : none
  1844. *******************************************************************************/
  1845. static void usb0_function_read_d0_fifo (uint16_t pipe, uint16_t count)
  1846. {
  1847. uint16_t even;
  1848. if (g_usb0_function_mbw[pipe] == USB_FUNCTION_BITMBW_8)
  1849. {
  1850. for (even = count; even; --even)
  1851. {
  1852. *g_usb0_function_data_pointer[pipe] = USB200.D0FIFO.UINT8[HH];
  1853. g_usb0_function_data_pointer[pipe] += 1;
  1854. }
  1855. }
  1856. else if (g_usb0_function_mbw[pipe] == USB_FUNCTION_BITMBW_16)
  1857. {
  1858. for (even = (uint16_t)((count + 1) / 2); even; --even)
  1859. {
  1860. *((uint16_t *)g_usb0_function_data_pointer[pipe]) = USB200.D0FIFO.UINT16[H];
  1861. g_usb0_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_usb0_function_data_pointer[pipe]) = USB200.D0FIFO.UINT32;
  1869. g_usb0_function_data_pointer[pipe] += 4;
  1870. }
  1871. }
  1872. }
  1873. /*******************************************************************************
  1874. * Function Name: usb0_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_usb0_function_mbw[].
  1882. * Arguments : uint16_t pipe ; Pipe Number
  1883. * : uint16_t count ; Data Size(Byte)
  1884. * Return Value : none
  1885. *******************************************************************************/
  1886. static void usb0_function_write_d1_fifo (uint16_t pipe, uint16_t count)
  1887. {
  1888. uint16_t even;
  1889. if (g_usb0_function_mbw[pipe] == USB_FUNCTION_BITMBW_8)
  1890. {
  1891. for (even = count; even; --even)
  1892. {
  1893. USB200.D1FIFO.UINT8[HH] = *g_usb0_function_data_pointer[pipe];
  1894. g_usb0_function_data_pointer[pipe] += 1;
  1895. }
  1896. }
  1897. else if (g_usb0_function_mbw[pipe] == USB_FUNCTION_BITMBW_16)
  1898. {
  1899. for (even = (uint16_t)(count / 2); even; --even)
  1900. {
  1901. USB200.D1FIFO.UINT16[H] = *((uint16_t *)g_usb0_function_data_pointer[pipe]);
  1902. g_usb0_function_data_pointer[pipe] += 2;
  1903. }
  1904. }
  1905. else
  1906. {
  1907. for (even = (uint16_t)(count / 4); even; --even)
  1908. {
  1909. USB200.D1FIFO.UINT32 = *((uint32_t *)g_usb0_function_data_pointer[pipe]);
  1910. g_usb0_function_data_pointer[pipe] += 4;
  1911. }
  1912. }
  1913. }
  1914. /*******************************************************************************
  1915. * Function Name: usb0_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_usb0_function_mbw[].
  1923. * Arguments : uint16_t pipe ; Pipe Number
  1924. * : uint16_t count ; Data Size(Byte)
  1925. * Return Value : none
  1926. *******************************************************************************/
  1927. static void usb0_function_read_d1_fifo (uint16_t pipe, uint16_t count)
  1928. {
  1929. uint16_t even;
  1930. if (g_usb0_function_mbw[pipe] == USB_FUNCTION_BITMBW_8)
  1931. {
  1932. for (even = count; even; --even)
  1933. {
  1934. *g_usb0_function_data_pointer[pipe] = USB200.D1FIFO.UINT8[HH];
  1935. g_usb0_function_data_pointer[pipe] += 1;
  1936. }
  1937. }
  1938. else if (g_usb0_function_mbw[pipe] == USB_FUNCTION_BITMBW_16)
  1939. {
  1940. for (even = (uint16_t)((count + 1) / 2); even; --even)
  1941. {
  1942. *((uint16_t *)g_usb0_function_data_pointer[pipe]) = USB200.D1FIFO.UINT16[H];
  1943. g_usb0_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_usb0_function_data_pointer[pipe]) = USB200.D1FIFO.UINT32;
  1951. g_usb0_function_data_pointer[pipe] += 4;
  1952. }
  1953. }
  1954. }
  1955. /*******************************************************************************
  1956. * Function Name: usb0_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 usb0_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: usb0_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 usb0_function_get_mbw (uint32_t trncount, uint32_t dtptr)
  1998. {
  1999. uint32_t size;
  2000. uint16_t mbw;
  2001. size = usb0_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: usb0_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 usb0_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 = usb0_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(&USB200.PIPE1TRE,
  2048. 1,
  2049. USB_PIPEnTRE_TRCLR_SHIFT,
  2050. USB_PIPEnTRE_TRCLR);
  2051. USB200.PIPE1TRN = cnt;
  2052. RZA_IO_RegWrite_16(&USB200.PIPE1TRE,
  2053. 1,
  2054. USB_PIPEnTRE_TRENB_SHIFT,
  2055. USB_PIPEnTRE_TRENB);
  2056. break;
  2057. case USB_FUNCTION_PIPE2:
  2058. RZA_IO_RegWrite_16(&USB200.PIPE2TRE,
  2059. 1,
  2060. USB_PIPEnTRE_TRCLR_SHIFT,
  2061. USB_PIPEnTRE_TRCLR);
  2062. USB200.PIPE2TRN = cnt;
  2063. RZA_IO_RegWrite_16(&USB200.PIPE2TRE,
  2064. 1,
  2065. USB_PIPEnTRE_TRENB_SHIFT,
  2066. USB_PIPEnTRE_TRENB);
  2067. break;
  2068. case USB_FUNCTION_PIPE3:
  2069. RZA_IO_RegWrite_16(&USB200.PIPE3TRE,
  2070. 1,
  2071. USB_PIPEnTRE_TRCLR_SHIFT,
  2072. USB_PIPEnTRE_TRCLR);
  2073. USB200.PIPE3TRN = cnt;
  2074. RZA_IO_RegWrite_16(&USB200.PIPE3TRE,
  2075. 1,
  2076. USB_PIPEnTRE_TRENB_SHIFT,
  2077. USB_PIPEnTRE_TRENB);
  2078. break;
  2079. case USB_FUNCTION_PIPE4:
  2080. RZA_IO_RegWrite_16(&USB200.PIPE4TRE,
  2081. 1,
  2082. USB_PIPEnTRE_TRCLR_SHIFT,
  2083. USB_PIPEnTRE_TRCLR);
  2084. USB200.PIPE4TRN = cnt;
  2085. RZA_IO_RegWrite_16(&USB200.PIPE4TRE,
  2086. 1,
  2087. USB_PIPEnTRE_TRENB_SHIFT,
  2088. USB_PIPEnTRE_TRENB);
  2089. break;
  2090. case USB_FUNCTION_PIPE5:
  2091. RZA_IO_RegWrite_16(&USB200.PIPE5TRE,
  2092. 1,
  2093. USB_PIPEnTRE_TRCLR_SHIFT,
  2094. USB_PIPEnTRE_TRCLR);
  2095. USB200.PIPE5TRN = cnt;
  2096. RZA_IO_RegWrite_16(&USB200.PIPE5TRE,
  2097. 1,
  2098. USB_PIPEnTRE_TRENB_SHIFT,
  2099. USB_PIPEnTRE_TRENB);
  2100. break;
  2101. case USB_FUNCTION_PIPE9:
  2102. RZA_IO_RegWrite_16(&USB200.PIPE9TRE,
  2103. 1,
  2104. USB_PIPEnTRE_TRCLR_SHIFT,
  2105. USB_PIPEnTRE_TRCLR);
  2106. USB200.PIPE9TRN = cnt;
  2107. RZA_IO_RegWrite_16(&USB200.PIPE9TRE,
  2108. 1,
  2109. USB_PIPEnTRE_TRENB_SHIFT,
  2110. USB_PIPEnTRE_TRENB);
  2111. break;
  2112. case USB_FUNCTION_PIPEA:
  2113. RZA_IO_RegWrite_16(&USB200.PIPEATRE,
  2114. 1,
  2115. USB_PIPEnTRE_TRCLR_SHIFT,
  2116. USB_PIPEnTRE_TRCLR);
  2117. USB200.PIPEATRN = cnt;
  2118. RZA_IO_RegWrite_16(&USB200.PIPEATRE,
  2119. 1,
  2120. USB_PIPEnTRE_TRENB_SHIFT,
  2121. USB_PIPEnTRE_TRENB);
  2122. break;
  2123. case USB_FUNCTION_PIPEB:
  2124. RZA_IO_RegWrite_16(&USB200.PIPEBTRE,
  2125. 1,
  2126. USB_PIPEnTRE_TRCLR_SHIFT,
  2127. USB_PIPEnTRE_TRCLR);
  2128. USB200.PIPEBTRN = cnt;
  2129. RZA_IO_RegWrite_16(&USB200.PIPEBTRE,
  2130. 1,
  2131. USB_PIPEnTRE_TRENB_SHIFT,
  2132. USB_PIPEnTRE_TRENB);
  2133. break;
  2134. case USB_FUNCTION_PIPEC:
  2135. RZA_IO_RegWrite_16(&USB200.PIPECTRE,
  2136. 1,
  2137. USB_PIPEnTRE_TRCLR_SHIFT,
  2138. USB_PIPEnTRE_TRCLR);
  2139. USB200.PIPECTRN = cnt;
  2140. RZA_IO_RegWrite_16(&USB200.PIPECTRE,
  2141. 1,
  2142. USB_PIPEnTRE_TRENB_SHIFT,
  2143. USB_PIPEnTRE_TRENB);
  2144. break;
  2145. case USB_FUNCTION_PIPED:
  2146. RZA_IO_RegWrite_16(&USB200.PIPEDTRE,
  2147. 1,
  2148. USB_PIPEnTRE_TRCLR_SHIFT,
  2149. USB_PIPEnTRE_TRCLR);
  2150. USB200.PIPEDTRN = cnt;
  2151. RZA_IO_RegWrite_16(&USB200.PIPEDTRE,
  2152. 1,
  2153. USB_PIPEnTRE_TRENB_SHIFT,
  2154. USB_PIPEnTRE_TRENB);
  2155. break;
  2156. case USB_FUNCTION_PIPEE:
  2157. RZA_IO_RegWrite_16(&USB200.PIPEETRE,
  2158. 1,
  2159. USB_PIPEnTRE_TRCLR_SHIFT,
  2160. USB_PIPEnTRE_TRCLR);
  2161. USB200.PIPEETRN = cnt;
  2162. RZA_IO_RegWrite_16(&USB200.PIPEETRE,
  2163. 1,
  2164. USB_PIPEnTRE_TRENB_SHIFT,
  2165. USB_PIPEnTRE_TRENB);
  2166. break;
  2167. case USB_FUNCTION_PIPEF:
  2168. RZA_IO_RegWrite_16(&USB200.PIPEFTRE,
  2169. 1,
  2170. USB_PIPEnTRE_TRCLR_SHIFT,
  2171. USB_PIPEnTRE_TRCLR);
  2172. USB200.PIPEFTRN = cnt;
  2173. RZA_IO_RegWrite_16(&USB200.PIPEFTRE,
  2174. 1,
  2175. USB_PIPEnTRE_TRENB_SHIFT,
  2176. USB_PIPEnTRE_TRENB);
  2177. break;
  2178. default:
  2179. break;
  2180. }
  2181. }
  2182. /*******************************************************************************
  2183. * Function Name: usb0_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 usb0_function_clear_transaction_counter (uint16_t pipe)
  2190. {
  2191. switch (pipe)
  2192. {
  2193. case USB_FUNCTION_PIPE1:
  2194. RZA_IO_RegWrite_16(&USB200.PIPE1TRE,
  2195. 0,
  2196. USB_PIPEnTRE_TRENB_SHIFT,
  2197. USB_PIPEnTRE_TRENB);
  2198. RZA_IO_RegWrite_16(&USB200.PIPE1TRE,
  2199. 1,
  2200. USB_PIPEnTRE_TRCLR_SHIFT,
  2201. USB_PIPEnTRE_TRCLR);
  2202. break;
  2203. case USB_FUNCTION_PIPE2:
  2204. RZA_IO_RegWrite_16(&USB200.PIPE2TRE,
  2205. 0,
  2206. USB_PIPEnTRE_TRENB_SHIFT,
  2207. USB_PIPEnTRE_TRENB);
  2208. RZA_IO_RegWrite_16(&USB200.PIPE2TRE,
  2209. 1,
  2210. USB_PIPEnTRE_TRCLR_SHIFT,
  2211. USB_PIPEnTRE_TRCLR);
  2212. break;
  2213. case USB_FUNCTION_PIPE3:
  2214. RZA_IO_RegWrite_16(&USB200.PIPE3TRE,
  2215. 0,
  2216. USB_PIPEnTRE_TRENB_SHIFT,
  2217. USB_PIPEnTRE_TRENB);
  2218. RZA_IO_RegWrite_16(&USB200.PIPE3TRE,
  2219. 1,
  2220. USB_PIPEnTRE_TRCLR_SHIFT,
  2221. USB_PIPEnTRE_TRCLR);
  2222. break;
  2223. case USB_FUNCTION_PIPE4:
  2224. RZA_IO_RegWrite_16(&USB200.PIPE4TRE,
  2225. 0,
  2226. USB_PIPEnTRE_TRENB_SHIFT,
  2227. USB_PIPEnTRE_TRENB);
  2228. RZA_IO_RegWrite_16(&USB200.PIPE4TRE,
  2229. 1,
  2230. USB_PIPEnTRE_TRCLR_SHIFT,
  2231. USB_PIPEnTRE_TRCLR);
  2232. break;
  2233. case USB_FUNCTION_PIPE5:
  2234. RZA_IO_RegWrite_16(&USB200.PIPE5TRE,
  2235. 0,
  2236. USB_PIPEnTRE_TRENB_SHIFT,
  2237. USB_PIPEnTRE_TRENB);
  2238. RZA_IO_RegWrite_16(&USB200.PIPE5TRE,
  2239. 1,
  2240. USB_PIPEnTRE_TRCLR_SHIFT,
  2241. USB_PIPEnTRE_TRCLR);
  2242. break;
  2243. case USB_FUNCTION_PIPE9:
  2244. RZA_IO_RegWrite_16(&USB200.PIPE9TRE,
  2245. 0,
  2246. USB_PIPEnTRE_TRENB_SHIFT,
  2247. USB_PIPEnTRE_TRENB);
  2248. RZA_IO_RegWrite_16(&USB200.PIPE9TRE,
  2249. 1,
  2250. USB_PIPEnTRE_TRCLR_SHIFT,
  2251. USB_PIPEnTRE_TRCLR);
  2252. break;
  2253. case USB_FUNCTION_PIPEA:
  2254. RZA_IO_RegWrite_16(&USB200.PIPEATRE,
  2255. 0,
  2256. USB_PIPEnTRE_TRENB_SHIFT,
  2257. USB_PIPEnTRE_TRENB);
  2258. RZA_IO_RegWrite_16(&USB200.PIPEATRE,
  2259. 1,
  2260. USB_PIPEnTRE_TRCLR_SHIFT,
  2261. USB_PIPEnTRE_TRCLR);
  2262. break;
  2263. case USB_FUNCTION_PIPEB:
  2264. RZA_IO_RegWrite_16(&USB200.PIPEBTRE,
  2265. 0,
  2266. USB_PIPEnTRE_TRENB_SHIFT,
  2267. USB_PIPEnTRE_TRENB);
  2268. RZA_IO_RegWrite_16(&USB200.PIPEBTRE,
  2269. 1,
  2270. USB_PIPEnTRE_TRCLR_SHIFT,
  2271. USB_PIPEnTRE_TRCLR);
  2272. break;
  2273. case USB_FUNCTION_PIPEC:
  2274. RZA_IO_RegWrite_16(&USB200.PIPECTRE,
  2275. 0,
  2276. USB_PIPEnTRE_TRENB_SHIFT,
  2277. USB_PIPEnTRE_TRENB);
  2278. RZA_IO_RegWrite_16(&USB200.PIPECTRE,
  2279. 1,
  2280. USB_PIPEnTRE_TRCLR_SHIFT,
  2281. USB_PIPEnTRE_TRCLR);
  2282. break;
  2283. case USB_FUNCTION_PIPED:
  2284. RZA_IO_RegWrite_16(&USB200.PIPEDTRE,
  2285. 0,
  2286. USB_PIPEnTRE_TRENB_SHIFT,
  2287. USB_PIPEnTRE_TRENB);
  2288. RZA_IO_RegWrite_16(&USB200.PIPEDTRE,
  2289. 1,
  2290. USB_PIPEnTRE_TRCLR_SHIFT,
  2291. USB_PIPEnTRE_TRCLR);
  2292. break;
  2293. case USB_FUNCTION_PIPEE:
  2294. RZA_IO_RegWrite_16(&USB200.PIPEETRE,
  2295. 0,
  2296. USB_PIPEnTRE_TRENB_SHIFT,
  2297. USB_PIPEnTRE_TRENB);
  2298. RZA_IO_RegWrite_16(&USB200.PIPEETRE,
  2299. 1,
  2300. USB_PIPEnTRE_TRCLR_SHIFT,
  2301. USB_PIPEnTRE_TRCLR);
  2302. break;
  2303. case USB_FUNCTION_PIPEF:
  2304. RZA_IO_RegWrite_16(&USB200.PIPEFTRE,
  2305. 0,
  2306. USB_PIPEnTRE_TRENB_SHIFT,
  2307. USB_PIPEnTRE_TRENB);
  2308. RZA_IO_RegWrite_16(&USB200.PIPEFTRE,
  2309. 1,
  2310. USB_PIPEnTRE_TRCLR_SHIFT,
  2311. USB_PIPEnTRE_TRCLR);
  2312. break;
  2313. default:
  2314. break;
  2315. }
  2316. }
  2317. /*******************************************************************************
  2318. * Function Name: usb0_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 usb0_function_stop_transfer (uint16_t pipe)
  2330. {
  2331. uint16_t usefifo;
  2332. uint32_t remain;
  2333. uint16_t fifo;
  2334. usb0_function_set_pid_nak(pipe);
  2335. usefifo = (uint16_t)(g_usb0_function_PipeTbl[pipe] & USB_FUNCTION_FIFO_USE);
  2336. switch (usefifo)
  2337. {
  2338. case USB_FUNCTION_D0FIFO_USE:
  2339. usb0_function_clear_transaction_counter(pipe);
  2340. USB200.D0FIFOCTR = USB_FUNCTION_BITBCLR; /* Buffer Clear */
  2341. fifo = USB_FUNCTION_D0USE;
  2342. break;
  2343. case USB_FUNCTION_D1FIFO_USE:
  2344. usb0_function_clear_transaction_counter(pipe);
  2345. USB200.D1FIFOCTR = USB_FUNCTION_BITBCLR; /* Buffer Clear */
  2346. fifo = USB_FUNCTION_D1USE;
  2347. break;
  2348. case USB_FUNCTION_D0FIFO_DMA:
  2349. remain = Userdef_USB_usb0_function_stop_dma0();
  2350. usb0_function_dma_stop_d0(pipe, remain);
  2351. usb0_function_clear_transaction_counter(pipe);
  2352. USB200.D0FIFOCTR = USB_FUNCTION_BITBCLR; /* Buffer Clear */
  2353. fifo = USB_FUNCTION_D0DMA;
  2354. break;
  2355. case USB_FUNCTION_D1FIFO_DMA:
  2356. remain = Userdef_USB_usb0_function_stop_dma1();
  2357. usb0_function_dma_stop_d1(pipe, remain);
  2358. usb0_function_clear_transaction_counter(pipe);
  2359. USB200.D1FIFOCTR = USB_FUNCTION_BITBCLR; /* Buffer Clear */
  2360. fifo = USB_FUNCTION_D1DMA;
  2361. break;
  2362. default:
  2363. usb0_function_clear_transaction_counter(pipe);
  2364. USB200.CFIFOCTR = USB_FUNCTION_BITBCLR; /* Buffer Clear */
  2365. fifo = USB_FUNCTION_CUSE;
  2366. break;
  2367. }
  2368. usb0_function_set_curpipe(USB_FUNCTION_PIPE0, fifo, DEVDRV_USBF_NO, USB_FUNCTION_BITMBW_16);
  2369. /* Interrupt of pipe set is disabled */
  2370. usb0_function_disable_brdy_int(pipe);
  2371. usb0_function_disable_nrdy_int(pipe);
  2372. usb0_function_disable_bemp_int(pipe);
  2373. usb0_function_aclrm(pipe);
  2374. usb0_function_set_csclr(pipe);
  2375. if ( g_usb0_function_pipe_status[pipe] == DEVDRV_USBF_PIPE_WAIT )
  2376. {
  2377. g_usb0_function_pipe_status[pipe] = DEVDRV_USBF_PIPE_NORES;
  2378. }
  2379. }
  2380. /*******************************************************************************
  2381. * Function Name: usb0_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 usb0_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(&USB200.D0FBCFG,
  2392. 0,
  2393. USB_DnFBCFG_DFACC_SHIFT,
  2394. USB_DnFBCFG_DFACC);
  2395. RZA_IO_RegWrite_16(&USB200.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(&USB200.D0FBCFG,
  2407. 2,
  2408. USB_DnFBCFG_DFACC_SHIFT,
  2409. USB_DnFBCFG_DFACC);
  2410. RZA_IO_RegWrite_16(&USB200.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(&USB200.D0FBCFG,
  2420. 1,
  2421. USB_DnFBCFG_DFACC_SHIFT,
  2422. USB_DnFBCFG_DFACC);
  2423. RZA_IO_RegWrite_16(&USB200.D0FBCFG,
  2424. 0,
  2425. USB_DnFBCFG_TENDE_SHIFT,
  2426. USB_DnFBCFG_TENDE);
  2427. dfacc = 1;
  2428. }
  2429. else
  2430. {
  2431. RZA_IO_RegWrite_16(&USB200.D0FBCFG,
  2432. 0,
  2433. USB_DnFBCFG_DFACC_SHIFT,
  2434. USB_DnFBCFG_DFACC);
  2435. RZA_IO_RegWrite_16(&USB200.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(&USB200.D0FBCFG,
  2445. 0,
  2446. USB_DnFBCFG_DFACC_SHIFT,
  2447. USB_DnFBCFG_DFACC);
  2448. RZA_IO_RegWrite_16(&USB200.D0FBCFG,
  2449. 0,
  2450. USB_DnFBCFG_TENDE_SHIFT,
  2451. USB_DnFBCFG_TENDE);
  2452. dfacc = 0;
  2453. }
  2454. else
  2455. {
  2456. RZA_IO_RegWrite_16(&USB200.D0FBCFG,
  2457. 0,
  2458. USB_DnFBCFG_DFACC_SHIFT,
  2459. USB_DnFBCFG_DFACC);
  2460. RZA_IO_RegWrite_16(&USB200.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: usb0_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 usb0_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(&USB200.D1FBCFG,
  2481. 0,
  2482. USB_DnFBCFG_DFACC_SHIFT,
  2483. USB_DnFBCFG_DFACC);
  2484. RZA_IO_RegWrite_16(&USB200.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(&USB200.D1FBCFG,
  2496. 2,
  2497. USB_DnFBCFG_DFACC_SHIFT,
  2498. USB_DnFBCFG_DFACC);
  2499. RZA_IO_RegWrite_16(&USB200.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(&USB200.D1FBCFG,
  2509. 1,
  2510. USB_DnFBCFG_DFACC_SHIFT,
  2511. USB_DnFBCFG_DFACC);
  2512. RZA_IO_RegWrite_16(&USB200.D1FBCFG,
  2513. 0,
  2514. USB_DnFBCFG_TENDE_SHIFT,
  2515. USB_DnFBCFG_TENDE);
  2516. dfacc = 1;
  2517. }
  2518. else
  2519. {
  2520. RZA_IO_RegWrite_16(&USB200.D1FBCFG,
  2521. 0,
  2522. USB_DnFBCFG_DFACC_SHIFT,
  2523. USB_DnFBCFG_DFACC);
  2524. RZA_IO_RegWrite_16(&USB200.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(&USB200.D1FBCFG,
  2534. 0,
  2535. USB_DnFBCFG_DFACC_SHIFT,
  2536. USB_DnFBCFG_DFACC);
  2537. RZA_IO_RegWrite_16(&USB200.D1FBCFG,
  2538. 0,
  2539. USB_DnFBCFG_TENDE_SHIFT,
  2540. USB_DnFBCFG_TENDE);
  2541. dfacc = 0;
  2542. }
  2543. else
  2544. {
  2545. RZA_IO_RegWrite_16(&USB200.D1FBCFG,
  2546. 0,
  2547. USB_DnFBCFG_DFACC_SHIFT,
  2548. USB_DnFBCFG_DFACC);
  2549. RZA_IO_RegWrite_16(&USB200.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 */