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.

usb1_function_dma.c 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*******************************************************************************
  2. * DISCLAIMER
  3. * This software is supplied by Renesas Electronics Corporation and is only
  4. * intended for use with Renesas products. No other uses are authorized. This
  5. * software is owned by Renesas Electronics Corporation and is protected under
  6. * all applicable laws, including copyright laws.
  7. * THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING
  8. * THIS SOFTWARE, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT
  9. * LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
  10. * AND NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED.
  11. * TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY LAW, NEITHER RENESAS
  12. * ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES SHALL BE LIABLE
  13. * FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR
  14. * ANY REASON RELATED TO THIS SOFTWARE, EVEN IF RENESAS OR ITS AFFILIATES HAVE
  15. * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  16. * Renesas reserves the right, without notice, to make changes to this software
  17. * and to discontinue the availability of this software. By using this software,
  18. * you agree to the additional terms and conditions found by accessing the
  19. * following link:
  20. * http://www.renesas.com/disclaimer
  21. * Copyright (C) 2012 - 2014 Renesas Electronics Corporation. All rights reserved.
  22. *******************************************************************************/
  23. /*******************************************************************************
  24. * File Name : usb1_function_dma.c
  25. * $Rev: 1116 $
  26. * $Date:: 2014-07-09 16:29:19 +0900#$
  27. * Device(s) : RZ/A1H
  28. * Tool-Chain :
  29. * OS : None
  30. * H/W Platform :
  31. * Description : RZ/A1H R7S72100 USB Sample Program
  32. * Operation :
  33. * Limitations :
  34. *******************************************************************************/
  35. /*******************************************************************************
  36. Includes <System Includes> , "Project Includes"
  37. *******************************************************************************/
  38. #include "usb1_function.h"
  39. /*******************************************************************************
  40. Typedef definitions
  41. *******************************************************************************/
  42. /*******************************************************************************
  43. Macro definitions
  44. *******************************************************************************/
  45. /*******************************************************************************
  46. Imported global variables and functions (from other files)
  47. *******************************************************************************/
  48. /*******************************************************************************
  49. Exported global variables and functions (to be accessed by other files)
  50. *******************************************************************************/
  51. /*******************************************************************************
  52. Private global variables and functions
  53. *******************************************************************************/
  54. static void usb1_function_dmaint(uint16_t fifo);
  55. static void usb1_function_dmaint_buf2fifo(uint16_t pipe);
  56. static void usb1_function_dmaint_fifo2buf(uint16_t pipe);
  57. /*******************************************************************************
  58. * Function Name: usb1_function_dma_stop_d0
  59. * Description : D0FIFO DMA stop
  60. * Arguments : uint16_t pipe : pipe number
  61. * : uint32_t remain : transfer byte
  62. * Return Value : none
  63. *******************************************************************************/
  64. void usb1_function_dma_stop_d0 (uint16_t pipe, uint32_t remain)
  65. {
  66. uint16_t dtln;
  67. uint16_t dfacc;
  68. uint16_t buffer;
  69. uint16_t sds_b = 1;
  70. dfacc = RZA_IO_RegRead_16(&USB201.D0FBCFG, USB_DnFBCFG_DFACC_SHIFT, USB_DnFBCFG_DFACC);
  71. if (dfacc == 2)
  72. {
  73. sds_b = 32;
  74. }
  75. else if (dfacc == 1)
  76. {
  77. sds_b = 16;
  78. }
  79. else
  80. {
  81. if (g_usb1_function_DmaInfo[USB_FUNCTION_D0FIFO].size == 2)
  82. {
  83. sds_b = 4;
  84. }
  85. else if (g_usb1_function_DmaInfo[USB_FUNCTION_D0FIFO].size == 1)
  86. {
  87. sds_b = 2;
  88. }
  89. else
  90. {
  91. sds_b = 1;
  92. }
  93. }
  94. if (RZA_IO_RegRead_16(&g_usb1_function_pipecfg[pipe], USB_PIPECFG_BFRE_SHIFT, USB_PIPECFG_BFRE) == 1)
  95. {
  96. if (g_usb1_function_pipe_status[pipe] != DEVDRV_USBF_PIPE_DONE)
  97. {
  98. buffer = USB201.D0FIFOCTR;
  99. dtln = (buffer & USB_FUNCTION_BITDTLN);
  100. if ((dtln % sds_b) != 0)
  101. {
  102. remain += (sds_b - (dtln % sds_b));
  103. }
  104. g_usb1_function_PipeDataSize[pipe] = (g_usb1_function_data_count[pipe] - remain);
  105. g_usb1_function_data_count[pipe] = remain;
  106. }
  107. }
  108. RZA_IO_RegWrite_16(&USB201.D0FIFOSEL, 0, USB_DnFIFOSEL_DREQE_SHIFT, USB_DnFIFOSEL_DREQE);
  109. }
  110. /*******************************************************************************
  111. * Function Name: usb1_function_dma_stop_d1
  112. * Description : D1FIFO DMA stop
  113. * Arguments : uint16_t pipe : pipe number
  114. * : uint32_t remain : transfer byte
  115. * Return Value : none
  116. *******************************************************************************/
  117. void usb1_function_dma_stop_d1 (uint16_t pipe, uint32_t remain)
  118. {
  119. uint16_t dtln;
  120. uint16_t dfacc;
  121. uint16_t buffer;
  122. uint16_t sds_b = 1;
  123. dfacc = RZA_IO_RegRead_16(&USB201.D1FBCFG, USB_DnFBCFG_DFACC_SHIFT, USB_DnFBCFG_DFACC);
  124. if (dfacc == 2)
  125. {
  126. sds_b = 32;
  127. }
  128. else if (dfacc == 1)
  129. {
  130. sds_b = 16;
  131. }
  132. else
  133. {
  134. if (g_usb1_function_DmaInfo[USB_FUNCTION_D1FIFO].size == 2)
  135. {
  136. sds_b = 4;
  137. }
  138. else if (g_usb1_function_DmaInfo[USB_FUNCTION_D1FIFO].size == 1)
  139. {
  140. sds_b = 2;
  141. }
  142. else
  143. {
  144. sds_b = 1;
  145. }
  146. }
  147. if (RZA_IO_RegRead_16(&g_usb1_function_pipecfg[pipe], USB_PIPECFG_BFRE_SHIFT, USB_PIPECFG_BFRE) == 1)
  148. {
  149. if (g_usb1_function_pipe_status[pipe] != DEVDRV_USBF_PIPE_DONE)
  150. {
  151. buffer = USB201.D1FIFOCTR;
  152. dtln = (buffer & USB_FUNCTION_BITDTLN);
  153. if ((dtln % sds_b) != 0)
  154. {
  155. remain += (sds_b - (dtln % sds_b));
  156. }
  157. g_usb1_function_PipeDataSize[pipe] = (g_usb1_function_data_count[pipe] - remain);
  158. g_usb1_function_data_count[pipe] = remain;
  159. }
  160. }
  161. RZA_IO_RegWrite_16(&USB201.D1FIFOSEL, 0, USB_DnFIFOSEL_DREQE_SHIFT, USB_DnFIFOSEL_DREQE);
  162. }
  163. /*******************************************************************************
  164. * Function Name: usb1_function_dma_interrupt_d0fifo
  165. * Description : This function is DMA interrupt handler entry.
  166. * : Execute usb1_function_dmaint() after disabling DMA interrupt in this function.
  167. * : Disable DMA interrupt to DMAC executed when USB_FUNCTION_D0FIFO_DMA is
  168. * : specified by dma->fifo.
  169. * : Register this function as DMA complete interrupt.
  170. * Arguments : uint32_t int_sense ; Interrupts detection mode
  171. * : ; INTC_LEVEL_SENSITIVE : Level sense
  172. * : ; INTC_EDGE_TRIGGER : Edge trigger
  173. * Return Value : none
  174. *******************************************************************************/
  175. void usb1_function_dma_interrupt_d0fifo (uint32_t int_sense)
  176. {
  177. usb1_function_dmaint(USB_FUNCTION_D0FIFO);
  178. g_usb1_function_DmaStatus[USB_FUNCTION_D0FIFO] = USB_FUNCTION_DMA_READY;
  179. }
  180. /*******************************************************************************
  181. * Function Name: usb1_function_dma_interrupt_d1fifo
  182. * Description : This function is DMA interrupt handler entry.
  183. * : Execute usb1_function_dmaint() after disabling DMA interrupt in this function.
  184. * : Disable DMA interrupt to DMAC executed when USB_FUNCTION_D1FIFO_DMA is
  185. * : specified by dma->fifo.
  186. * : Register this function as DMA complete interrupt.
  187. * Arguments : uint32_t int_sense ; Interrupts detection mode
  188. * : ; INTC_LEVEL_SENSITIVE : Level sense
  189. * : ; INTC_EDGE_TRIGGER : Edge trigger
  190. * Return Value : none
  191. *******************************************************************************/
  192. void usb1_function_dma_interrupt_d1fifo (uint32_t int_sense)
  193. {
  194. usb1_function_dmaint(USB_FUNCTION_D1FIFO);
  195. g_usb1_function_DmaStatus[USB_FUNCTION_D1FIFO] = USB_FUNCTION_DMA_READY;
  196. }
  197. /*******************************************************************************
  198. * Function Name: usb1_function_dmaint
  199. * Description : This function is DMA transfer end interrupt
  200. * Arguments : uint16_t fifo ; fifo number
  201. * : ; USB_FUNCTION_D0FIFO
  202. * : ; USB_FUNCTION_D1FIFO
  203. * Return Value : none
  204. *******************************************************************************/
  205. static void usb1_function_dmaint (uint16_t fifo)
  206. {
  207. uint16_t pipe;
  208. pipe = g_usb1_function_DmaPipe[fifo];
  209. if (g_usb1_function_DmaInfo[fifo].dir == USB_FUNCTION_BUF2FIFO)
  210. {
  211. usb1_function_dmaint_buf2fifo(pipe);
  212. }
  213. else
  214. {
  215. usb1_function_dmaint_fifo2buf(pipe);
  216. }
  217. }
  218. /*******************************************************************************
  219. * Function Name: usb1_function_dmaint_fifo2buf
  220. * Description : Executes read completion from FIFO by DMAC.
  221. * Arguments : uint16_t pipe : pipe number
  222. * Return Value : none
  223. *******************************************************************************/
  224. static void usb1_function_dmaint_fifo2buf (uint16_t pipe)
  225. {
  226. uint32_t remain;
  227. uint16_t useport;
  228. if (g_usb1_function_pipe_status[pipe] != DEVDRV_USBF_PIPE_DONE)
  229. {
  230. useport = (uint16_t)(g_usb1_function_PipeTbl[pipe] & USB_FUNCTION_FIFO_USE);
  231. if (useport == USB_FUNCTION_D0FIFO_DMA)
  232. {
  233. remain = Userdef_USB_usb1_function_stop_dma0();
  234. usb1_function_dma_stop_d0(pipe, remain);
  235. if (RZA_IO_RegRead_16(&g_usb1_function_pipecfg[pipe], USB_PIPECFG_BFRE_SHIFT, USB_PIPECFG_BFRE) == 0)
  236. {
  237. if (g_usb1_function_DmaStatus[USB_FUNCTION_D0FIFO] == USB_FUNCTION_DMA_BUSYEND)
  238. {
  239. USB201.D0FIFOCTR = USB_FUNCTION_BITBCLR;
  240. g_usb1_function_pipe_status[pipe] = DEVDRV_USBF_PIPE_DONE;
  241. }
  242. else
  243. {
  244. usb1_function_enable_brdy_int(pipe);
  245. }
  246. }
  247. }
  248. else
  249. {
  250. remain = Userdef_USB_usb1_function_stop_dma1();
  251. usb1_function_dma_stop_d1(pipe, remain);
  252. if (RZA_IO_RegRead_16(&g_usb1_function_pipecfg[pipe], USB_PIPECFG_BFRE_SHIFT, USB_PIPECFG_BFRE) == 0)
  253. {
  254. if (g_usb1_function_DmaStatus[USB_FUNCTION_D1FIFO] == USB_FUNCTION_DMA_BUSYEND)
  255. {
  256. USB201.D1FIFOCTR = USB_FUNCTION_BITBCLR;
  257. g_usb1_function_pipe_status[pipe] = DEVDRV_USBF_PIPE_DONE;
  258. }
  259. else
  260. {
  261. usb1_function_enable_brdy_int(pipe);
  262. }
  263. }
  264. }
  265. }
  266. }
  267. /*******************************************************************************
  268. * Function Name: usb1_function_dmaint_buf2fifo
  269. * Description : Executes write completion in FIFO by DMAC.
  270. * Arguments : uint16_t pipe : pipe number
  271. * Return Value : none
  272. *******************************************************************************/
  273. static void usb1_function_dmaint_buf2fifo (uint16_t pipe)
  274. {
  275. uint32_t remain;
  276. uint16_t useport;
  277. useport = (uint16_t)(g_usb1_function_PipeTbl[pipe] & USB_FUNCTION_FIFO_USE);
  278. if (useport == USB_FUNCTION_D0FIFO_DMA)
  279. {
  280. remain = Userdef_USB_usb1_function_stop_dma0();
  281. usb1_function_dma_stop_d0(pipe, remain);
  282. if (g_usb1_function_DmaBval[USB_FUNCTION_D0FIFO] != 0)
  283. {
  284. RZA_IO_RegWrite_16(&USB201.D0FIFOCTR,
  285. 1,
  286. USB_DnFIFOCTR_BVAL_SHIFT,
  287. USB_DnFIFOCTR_BVAL);
  288. }
  289. }
  290. else
  291. {
  292. remain = Userdef_USB_usb1_function_stop_dma1();
  293. usb1_function_dma_stop_d1(pipe, remain);
  294. if (g_usb1_function_DmaBval[USB_FUNCTION_D1FIFO] != 0)
  295. {
  296. RZA_IO_RegWrite_16(&USB201.D1FIFOCTR,
  297. 1,
  298. USB_DnFIFOCTR_BVAL_SHIFT,
  299. USB_DnFIFOCTR_BVAL);
  300. }
  301. }
  302. usb1_function_enable_bemp_int(pipe);
  303. }
  304. /* End of File */