Keyboard firmwares for Atmel AVR and Cortex-M
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

usb0_function_dmacdrv.c 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  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_dmacdrv.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 <stdio.h>
  39. #include "r_typedefs.h"
  40. #include "iodefine.h"
  41. #include "rza_io_regrw.h"
  42. #include "usb0_function_dmacdrv.h"
  43. /*******************************************************************************
  44. Typedef definitions
  45. *******************************************************************************/
  46. /*******************************************************************************
  47. Macro definitions
  48. *******************************************************************************/
  49. #define DMAC_INDEFINE (255) /* Macro definition when REQD bit is not used */
  50. /* ==== Request setting information for on-chip peripheral module ==== */
  51. typedef enum dmac_peri_req_reg_type
  52. {
  53. DMAC_REQ_MID,
  54. DMAC_REQ_RID,
  55. DMAC_REQ_AM,
  56. DMAC_REQ_LVL,
  57. DMAC_REQ_REQD
  58. } dmac_peri_req_reg_type_t;
  59. /*******************************************************************************
  60. Imported global variables and functions (from other files)
  61. *******************************************************************************/
  62. /*******************************************************************************
  63. Exported global variables and functions (to be accessed by other files)
  64. *******************************************************************************/
  65. /*******************************************************************************
  66. Private global variables and functions
  67. *******************************************************************************/
  68. /* ==== Prototype declaration ==== */
  69. /* ==== Global variable ==== */
  70. /* On-chip peripheral module request setting table */
  71. static const uint8_t usb0_function_dmac_peri_req_init_table[8][5] =
  72. {
  73. /* MID,RID,AM,LVL,REQD */
  74. {32, 3, 2, 1, 1}, /* USB_0 channel 0 transmit FIFO empty */
  75. {32, 3, 2, 1, 0}, /* USB_0 channel 0 receive FIFO full */
  76. {33, 3, 2, 1, 1}, /* USB_0 channel 1 transmit FIFO empty */
  77. {33, 3, 2, 1, 0}, /* USB_0 channel 1 receive FIFO full */
  78. {34, 3, 2, 1, 1}, /* USB_1 channel 0 transmit FIFO empty */
  79. {34, 3, 2, 1, 0}, /* USB_1 channel 0 receive FIFO full */
  80. {35, 3, 2, 1, 1}, /* USB_1 channel 1 transmit FIFO empty */
  81. {35, 3, 2, 1, 0}, /* USB_1 channel 1 receive FIFO full */
  82. };
  83. /*******************************************************************************
  84. * Function Name: usb0_function_DMAC1_PeriReqInit
  85. * Description : Sets the register mode for DMA mode and the on-chip peripheral
  86. * : module request for transfer request for DMAC channel 1.
  87. * : Executes DMAC initial setting using the DMA information
  88. * : specified by the argument *trans_info and the enabled/disabled
  89. * : continuous transfer specified by the argument continuation.
  90. * : Registers DMAC channel 1 interrupt handler function and sets
  91. * : the interrupt priority level. Then enables transfer completion
  92. * : interrupt.
  93. * Arguments : dmac_transinfo_t *trans_info : Setting information to DMAC register
  94. * : uint32_t dmamode : DMA mode (only for DMAC_MODE_REGISTER)
  95. * : uint32_t continuation : Set continuous transfer to be valid
  96. * : after DMA transfer has been completed
  97. * : DMAC_SAMPLE_CONTINUATION : Execute continuous transfer
  98. * : DMAC_SAMPLE_SINGLE : Do not execute continuous transfer
  99. * : uint32_t request_factor : Factor for on-chip peripheral module request
  100. * : DMAC_REQ_OSTM0TINT : OSTM_0 compare match
  101. * : DMAC_REQ_OSTM1TINT : OSTM_1 compare match
  102. * : DMAC_REQ_TGI0A : MTU2_0 input capture/compare match
  103. * : :
  104. * : uint32_t req_direction: Setting value of CHCFG_n register REQD bit
  105. * Return Value : none
  106. *******************************************************************************/
  107. void usb0_function_DMAC1_PeriReqInit (const dmac_transinfo_t * trans_info,
  108. uint32_t dmamode, uint32_t continuation,
  109. uint32_t request_factor, uint32_t req_direction)
  110. {
  111. /* ==== Register mode ==== */
  112. if (DMAC_MODE_REGISTER == dmamode)
  113. {
  114. /* ==== Next0 register set ==== */
  115. DMAC1.N0SA_n = trans_info->src_addr; /* Start address of transfer source */
  116. DMAC1.N0DA_n = trans_info->dst_addr; /* Start address of transfer destination */
  117. DMAC1.N0TB_n = trans_info->count; /* Total transfer byte count */
  118. /* DAD : Transfer destination address counting direction */
  119. /* SAD : Transfer source address counting direction */
  120. /* DDS : Transfer destination transfer size */
  121. /* SDS : Transfer source transfer size */
  122. RZA_IO_RegWrite_32(&DMAC1.CHCFG_n,
  123. trans_info->daddr_dir,
  124. DMAC1_CHCFG_n_DAD_SHIFT,
  125. DMAC1_CHCFG_n_DAD);
  126. RZA_IO_RegWrite_32(&DMAC1.CHCFG_n,
  127. trans_info->saddr_dir,
  128. DMAC1_CHCFG_n_SAD_SHIFT,
  129. DMAC1_CHCFG_n_SAD);
  130. RZA_IO_RegWrite_32(&DMAC1.CHCFG_n,
  131. trans_info->dst_size,
  132. DMAC1_CHCFG_n_DDS_SHIFT,
  133. DMAC1_CHCFG_n_DDS);
  134. RZA_IO_RegWrite_32(&DMAC1.CHCFG_n,
  135. trans_info->src_size,
  136. DMAC1_CHCFG_n_SDS_SHIFT,
  137. DMAC1_CHCFG_n_SDS);
  138. /* DMS : Register mode */
  139. /* RSEL : Select Next0 register set */
  140. /* SBE : No discharge of buffer data when aborted */
  141. /* DEM : No DMA interrupt mask */
  142. RZA_IO_RegWrite_32(&DMAC1.CHCFG_n,
  143. 0,
  144. DMAC1_CHCFG_n_DMS_SHIFT,
  145. DMAC1_CHCFG_n_DMS);
  146. RZA_IO_RegWrite_32(&DMAC1.CHCFG_n,
  147. 0,
  148. DMAC1_CHCFG_n_RSEL_SHIFT,
  149. DMAC1_CHCFG_n_RSEL);
  150. RZA_IO_RegWrite_32(&DMAC1.CHCFG_n,
  151. 0,
  152. DMAC1_CHCFG_n_SBE_SHIFT,
  153. DMAC1_CHCFG_n_SBE);
  154. RZA_IO_RegWrite_32(&DMAC1.CHCFG_n,
  155. 0,
  156. DMAC1_CHCFG_n_DEM_SHIFT,
  157. DMAC1_CHCFG_n_DEM);
  158. /* ---- Continuous transfer ---- */
  159. if (DMAC_SAMPLE_CONTINUATION == continuation)
  160. {
  161. /* REN : Execute continuous transfer */
  162. /* RSW : Change register set when DMA transfer is completed. */
  163. RZA_IO_RegWrite_32(&DMAC1.CHCFG_n,
  164. 1,
  165. DMAC1_CHCFG_n_REN_SHIFT,
  166. DMAC1_CHCFG_n_REN);
  167. RZA_IO_RegWrite_32(&DMAC1.CHCFG_n,
  168. 1,
  169. DMAC1_CHCFG_n_RSW_SHIFT,
  170. DMAC1_CHCFG_n_RSW);
  171. }
  172. /* ---- Single transfer ---- */
  173. else
  174. {
  175. /* REN : Do not execute continuous transfer */
  176. /* RSW : Do not change register set when DMA transfer is completed. */
  177. RZA_IO_RegWrite_32(&DMAC1.CHCFG_n,
  178. 0,
  179. DMAC1_CHCFG_n_REN_SHIFT,
  180. DMAC1_CHCFG_n_REN);
  181. RZA_IO_RegWrite_32(&DMAC1.CHCFG_n,
  182. 0,
  183. DMAC1_CHCFG_n_RSW_SHIFT,
  184. DMAC1_CHCFG_n_RSW);
  185. }
  186. /* TM : Single transfer */
  187. /* SEL : Channel setting */
  188. /* HIEN, LOEN : On-chip peripheral module request */
  189. RZA_IO_RegWrite_32(&DMAC1.CHCFG_n,
  190. 0,
  191. DMAC1_CHCFG_n_TM_SHIFT,
  192. DMAC1_CHCFG_n_TM);
  193. RZA_IO_RegWrite_32(&DMAC1.CHCFG_n,
  194. 1,
  195. DMAC1_CHCFG_n_SEL_SHIFT,
  196. DMAC1_CHCFG_n_SEL);
  197. RZA_IO_RegWrite_32(&DMAC1.CHCFG_n,
  198. 1,
  199. DMAC1_CHCFG_n_HIEN_SHIFT,
  200. DMAC1_CHCFG_n_HIEN);
  201. RZA_IO_RegWrite_32(&DMAC1.CHCFG_n,
  202. 0,
  203. DMAC1_CHCFG_n_LOEN_SHIFT,
  204. DMAC1_CHCFG_n_LOEN);
  205. /* ---- Set factor by specified on-chip peripheral module request ---- */
  206. RZA_IO_RegWrite_32(&DMAC1.CHCFG_n,
  207. usb0_function_dmac_peri_req_init_table[request_factor][DMAC_REQ_AM],
  208. DMAC1_CHCFG_n_AM_SHIFT,
  209. DMAC1_CHCFG_n_AM);
  210. RZA_IO_RegWrite_32(&DMAC1.CHCFG_n,
  211. usb0_function_dmac_peri_req_init_table[request_factor][DMAC_REQ_LVL],
  212. DMAC1_CHCFG_n_LVL_SHIFT,
  213. DMAC1_CHCFG_n_LVL);
  214. if (usb0_function_dmac_peri_req_init_table[request_factor][DMAC_REQ_REQD] != DMAC_INDEFINE)
  215. {
  216. RZA_IO_RegWrite_32(&DMAC1.CHCFG_n,
  217. usb0_function_dmac_peri_req_init_table[request_factor][DMAC_REQ_REQD],
  218. DMAC1_CHCFG_n_REQD_SHIFT,
  219. DMAC1_CHCFG_n_REQD);
  220. }
  221. else
  222. {
  223. RZA_IO_RegWrite_32(&DMAC1.CHCFG_n,
  224. req_direction,
  225. DMAC1_CHCFG_n_REQD_SHIFT,
  226. DMAC1_CHCFG_n_REQD);
  227. }
  228. RZA_IO_RegWrite_32(&DMAC01.DMARS,
  229. usb0_function_dmac_peri_req_init_table[request_factor][DMAC_REQ_RID],
  230. DMAC01_DMARS_CH1_RID_SHIFT,
  231. DMAC01_DMARS_CH1_RID);
  232. RZA_IO_RegWrite_32(&DMAC01.DMARS,
  233. usb0_function_dmac_peri_req_init_table[request_factor][DMAC_REQ_MID],
  234. DMAC01_DMARS_CH1_MID_SHIFT,
  235. DMAC01_DMARS_CH1_MID);
  236. /* PR : Round robin mode */
  237. RZA_IO_RegWrite_32(&DMAC07.DCTRL_0_7,
  238. 1,
  239. DMAC07_DCTRL_0_7_PR_SHIFT,
  240. DMAC07_DCTRL_0_7_PR);
  241. }
  242. }
  243. /*******************************************************************************
  244. * Function Name: usb0_function_DMAC1_Open
  245. * Description : Enables DMAC channel 1 transfer.
  246. * Arguments : uint32_t req : DMAC request mode
  247. * Return Value : 0 : Succeeded in enabling DMA transfer
  248. * : -1 : Failed to enable DMA transfer (due to DMA operation)
  249. *******************************************************************************/
  250. int32_t usb0_function_DMAC1_Open (uint32_t req)
  251. {
  252. int32_t ret;
  253. volatile uint8_t dummy;
  254. /* Transferable? */
  255. if ((0 == RZA_IO_RegRead_32(&DMAC1.CHSTAT_n,
  256. DMAC1_CHSTAT_n_EN_SHIFT,
  257. DMAC1_CHSTAT_n_EN)) &&
  258. (0 == RZA_IO_RegRead_32(&DMAC1.CHSTAT_n,
  259. DMAC1_CHSTAT_n_TACT_SHIFT,
  260. DMAC1_CHSTAT_n_TACT)))
  261. {
  262. /* Clear Channel Status Register */
  263. RZA_IO_RegWrite_32(&DMAC1.CHCTRL_n,
  264. 1,
  265. DMAC1_CHCTRL_n_SWRST_SHIFT,
  266. DMAC1_CHCTRL_n_SWRST);
  267. dummy = RZA_IO_RegRead_32(&DMAC1.CHCTRL_n,
  268. DMAC1_CHCTRL_n_SWRST_SHIFT,
  269. DMAC1_CHCTRL_n_SWRST);
  270. /* Enable DMA transfer */
  271. RZA_IO_RegWrite_32(&DMAC1.CHCTRL_n,
  272. 1,
  273. DMAC1_CHCTRL_n_SETEN_SHIFT,
  274. DMAC1_CHCTRL_n_SETEN);
  275. /* ---- Request by software ---- */
  276. if (DMAC_REQ_MODE_SOFT == req)
  277. {
  278. /* DMA transfer Request by software */
  279. RZA_IO_RegWrite_32(&DMAC1.CHCTRL_n,
  280. 1,
  281. DMAC1_CHCTRL_n_STG_SHIFT,
  282. DMAC1_CHCTRL_n_STG);
  283. }
  284. ret = 0;
  285. }
  286. else
  287. {
  288. ret = -1;
  289. }
  290. return ret;
  291. }
  292. /*******************************************************************************
  293. * Function Name: usb0_function_DMAC1_Close
  294. * Description : Aborts DMAC channel 1 transfer. Returns the remaining transfer
  295. * : byte count at the time of DMA transfer abort to the argument
  296. * : *remain.
  297. * Arguments : uint32_t * remain : Remaining transfer byte count when
  298. * : : DMA transfer is aborted
  299. * Return Value : none
  300. *******************************************************************************/
  301. void usb0_function_DMAC1_Close (uint32_t * remain)
  302. {
  303. /* ==== Abort transfer ==== */
  304. RZA_IO_RegWrite_32(&DMAC1.CHCTRL_n,
  305. 1,
  306. DMAC1_CHCTRL_n_CLREN_SHIFT,
  307. DMAC1_CHCTRL_n_CLREN);
  308. while (1 == RZA_IO_RegRead_32(&DMAC1.CHSTAT_n,
  309. DMAC1_CHSTAT_n_TACT_SHIFT,
  310. DMAC1_CHSTAT_n_TACT))
  311. {
  312. /* Loop until transfer is aborted */
  313. }
  314. while (1 == RZA_IO_RegRead_32(&DMAC1.CHSTAT_n,
  315. DMAC1_CHSTAT_n_EN_SHIFT,
  316. DMAC1_CHSTAT_n_EN))
  317. {
  318. /* Loop until 0 is set in EN before checking the remaining transfer byte count */
  319. }
  320. /* ==== Obtain remaining transfer byte count ==== */
  321. *remain = DMAC1.CRTB_n;
  322. }
  323. /*******************************************************************************
  324. * Function Name: usb0_function_DMAC1_Load_Set
  325. * Description : Sets the transfer source address, transfer destination
  326. * : address, and total transfer byte count respectively
  327. * : specified by the argument src_addr, dst_addr, and count to
  328. * : DMAC channel 1 as DMA transfer information.
  329. * : Sets the register set selected by the CHCFG_n register
  330. * : RSEL bit from the Next0 or Next1 register set.
  331. * : This function should be called when DMA transfer of DMAC
  332. * : channel 1 is aboted.
  333. * Arguments : uint32_t src_addr : Transfer source address
  334. * : uint32_t dst_addr : Transfer destination address
  335. * : uint32_t count : Total transfer byte count
  336. * Return Value : none
  337. *******************************************************************************/
  338. void usb0_function_DMAC1_Load_Set (uint32_t src_addr, uint32_t dst_addr, uint32_t count)
  339. {
  340. uint8_t reg_set;
  341. /* Obtain register set in use */
  342. reg_set = RZA_IO_RegRead_32(&DMAC1.CHSTAT_n,
  343. DMAC1_CHSTAT_n_SR_SHIFT,
  344. DMAC1_CHSTAT_n_SR);
  345. /* ==== Load ==== */
  346. if (0 == reg_set)
  347. {
  348. /* ---- Next0 Register Set ---- */
  349. DMAC1.N0SA_n = src_addr; /* Start address of transfer source */
  350. DMAC1.N0DA_n = dst_addr; /* Start address of transfer destination */
  351. DMAC1.N0TB_n = count; /* Total transfer byte count */
  352. }
  353. else
  354. {
  355. /* ---- Next1 Register Set ---- */
  356. DMAC1.N1SA_n = src_addr; /* Start address of transfer source */
  357. DMAC1.N1DA_n = dst_addr; /* Start address of transfer destination */
  358. DMAC1.N1TB_n = count; /* Total transfer byte count */
  359. }
  360. }
  361. /*******************************************************************************
  362. * Function Name: usb0_function_DMAC2_PeriReqInit
  363. * Description : Sets the register mode for DMA mode and the on-chip peripheral
  364. * : module request for transfer request for DMAC channel 2.
  365. * : Executes DMAC initial setting using the DMA information
  366. * : specified by the argument *trans_info and the enabled/disabled
  367. * : continuous transfer specified by the argument continuation.
  368. * : Registers DMAC channel 2 interrupt handler function and sets
  369. * : the interrupt priority level. Then enables transfer completion
  370. * : interrupt.
  371. * Arguments : dmac_transinfo_t * trans_info : Setting information to DMAC
  372. * : : register
  373. * : uint32_t dmamode : DMA mode (only for DMAC_MODE_REGISTER)
  374. * : uint32_t continuation : Set continuous transfer to be valid
  375. * : : after DMA transfer has been completed
  376. * : DMAC_SAMPLE_CONTINUATION : Execute continuous transfer
  377. * : DMAC_SAMPLE_SINGLE : Do not execute continuous
  378. * : : transfer
  379. * : uint32_t request_factor : Factor for on-chip peripheral module
  380. * : : request
  381. * : DMAC_REQ_OSTM0TINT : OSTM_0 compare match
  382. * : DMAC_REQ_OSTM1TINT : OSTM_1 compare match
  383. * : DMAC_REQ_TGI0A : MTU2_0 input capture/compare match
  384. * : :
  385. * : uint32_t req_direction : Setting value of CHCFG_n register
  386. * : : REQD bit
  387. *******************************************************************************/
  388. void usb0_function_DMAC2_PeriReqInit (const dmac_transinfo_t * trans_info,
  389. uint32_t dmamode, uint32_t continuation,
  390. uint32_t request_factor, uint32_t req_direction)
  391. {
  392. /* ==== Register mode ==== */
  393. if (DMAC_MODE_REGISTER == dmamode)
  394. {
  395. /* ==== Next0 register set ==== */
  396. DMAC2.N0SA_n = trans_info->src_addr; /* Start address of transfer source */
  397. DMAC2.N0DA_n = trans_info->dst_addr; /* Start address of transfer destination */
  398. DMAC2.N0TB_n = trans_info->count; /* Total transfer byte count */
  399. /* DAD : Transfer destination address counting direction */
  400. /* SAD : Transfer source address counting direction */
  401. /* DDS : Transfer destination transfer size */
  402. /* SDS : Transfer source transfer size */
  403. RZA_IO_RegWrite_32(&DMAC2.CHCFG_n,
  404. trans_info->daddr_dir,
  405. DMAC2_CHCFG_n_DAD_SHIFT,
  406. DMAC2_CHCFG_n_DAD);
  407. RZA_IO_RegWrite_32(&DMAC2.CHCFG_n,
  408. trans_info->saddr_dir,
  409. DMAC2_CHCFG_n_SAD_SHIFT,
  410. DMAC2_CHCFG_n_SAD);
  411. RZA_IO_RegWrite_32(&DMAC2.CHCFG_n,
  412. trans_info->dst_size,
  413. DMAC2_CHCFG_n_DDS_SHIFT,
  414. DMAC2_CHCFG_n_DDS);
  415. RZA_IO_RegWrite_32(&DMAC2.CHCFG_n,
  416. trans_info->src_size,
  417. DMAC2_CHCFG_n_SDS_SHIFT,
  418. DMAC2_CHCFG_n_SDS);
  419. /* DMS : Register mode */
  420. /* RSEL : Select Next0 register set */
  421. /* SBE : No discharge of buffer data when aborted */
  422. /* DEM : No DMA interrupt mask */
  423. RZA_IO_RegWrite_32(&DMAC2.CHCFG_n,
  424. 0,
  425. DMAC2_CHCFG_n_DMS_SHIFT,
  426. DMAC2_CHCFG_n_DMS);
  427. RZA_IO_RegWrite_32(&DMAC2.CHCFG_n,
  428. 0,
  429. DMAC2_CHCFG_n_RSEL_SHIFT,
  430. DMAC2_CHCFG_n_RSEL);
  431. RZA_IO_RegWrite_32(&DMAC2.CHCFG_n,
  432. 0,
  433. DMAC2_CHCFG_n_SBE_SHIFT,
  434. DMAC2_CHCFG_n_SBE);
  435. RZA_IO_RegWrite_32(&DMAC2.CHCFG_n,
  436. 0,
  437. DMAC2_CHCFG_n_DEM_SHIFT,
  438. DMAC2_CHCFG_n_DEM);
  439. /* ---- Continuous transfer ---- */
  440. if (DMAC_SAMPLE_CONTINUATION == continuation)
  441. {
  442. /* REN : Execute continuous transfer */
  443. /* RSW : Change register set when DMA transfer is completed. */
  444. RZA_IO_RegWrite_32(&DMAC2.CHCFG_n,
  445. 1,
  446. DMAC2_CHCFG_n_REN_SHIFT,
  447. DMAC2_CHCFG_n_REN);
  448. RZA_IO_RegWrite_32(&DMAC2.CHCFG_n,
  449. 1,
  450. DMAC2_CHCFG_n_RSW_SHIFT,
  451. DMAC2_CHCFG_n_RSW);
  452. }
  453. /* ---- Single transfer ---- */
  454. else
  455. {
  456. /* REN : Do not execute continuous transfer */
  457. /* RSW : Do not change register set when DMA transfer is completed. */
  458. RZA_IO_RegWrite_32(&DMAC2.CHCFG_n,
  459. 0,
  460. DMAC2_CHCFG_n_REN_SHIFT,
  461. DMAC2_CHCFG_n_REN);
  462. RZA_IO_RegWrite_32(&DMAC2.CHCFG_n,
  463. 0,
  464. DMAC2_CHCFG_n_RSW_SHIFT,
  465. DMAC2_CHCFG_n_RSW);
  466. }
  467. /* TM : Single transfer */
  468. /* SEL : Channel setting */
  469. /* HIEN, LOEN : On-chip peripheral module request */
  470. RZA_IO_RegWrite_32(&DMAC2.CHCFG_n,
  471. 0,
  472. DMAC2_CHCFG_n_TM_SHIFT,
  473. DMAC2_CHCFG_n_TM);
  474. RZA_IO_RegWrite_32(&DMAC2.CHCFG_n,
  475. 2,
  476. DMAC2_CHCFG_n_SEL_SHIFT,
  477. DMAC2_CHCFG_n_SEL);
  478. RZA_IO_RegWrite_32(&DMAC2.CHCFG_n,
  479. 1,
  480. DMAC2_CHCFG_n_HIEN_SHIFT,
  481. DMAC2_CHCFG_n_HIEN);
  482. RZA_IO_RegWrite_32(&DMAC2.CHCFG_n,
  483. 0,
  484. DMAC2_CHCFG_n_LOEN_SHIFT,
  485. DMAC2_CHCFG_n_LOEN);
  486. /* ---- Set factor by specified on-chip peripheral module request ---- */
  487. RZA_IO_RegWrite_32(&DMAC2.CHCFG_n,
  488. usb0_function_dmac_peri_req_init_table[request_factor][DMAC_REQ_AM],
  489. DMAC2_CHCFG_n_AM_SHIFT,
  490. DMAC2_CHCFG_n_AM);
  491. RZA_IO_RegWrite_32(&DMAC2.CHCFG_n,
  492. usb0_function_dmac_peri_req_init_table[request_factor][DMAC_REQ_LVL],
  493. DMAC2_CHCFG_n_LVL_SHIFT,
  494. DMAC2_CHCFG_n_LVL);
  495. if (usb0_function_dmac_peri_req_init_table[request_factor][DMAC_REQ_REQD] != DMAC_INDEFINE)
  496. {
  497. RZA_IO_RegWrite_32(&DMAC2.CHCFG_n,
  498. usb0_function_dmac_peri_req_init_table[request_factor][DMAC_REQ_REQD],
  499. DMAC2_CHCFG_n_REQD_SHIFT,
  500. DMAC2_CHCFG_n_REQD);
  501. }
  502. else
  503. {
  504. RZA_IO_RegWrite_32(&DMAC2.CHCFG_n,
  505. req_direction,
  506. DMAC2_CHCFG_n_REQD_SHIFT,
  507. DMAC2_CHCFG_n_REQD);
  508. }
  509. RZA_IO_RegWrite_32(&DMAC23.DMARS,
  510. usb0_function_dmac_peri_req_init_table[request_factor][DMAC_REQ_RID],
  511. DMAC23_DMARS_CH2_RID_SHIFT,
  512. DMAC23_DMARS_CH2_RID);
  513. RZA_IO_RegWrite_32(&DMAC23.DMARS,
  514. usb0_function_dmac_peri_req_init_table[request_factor][DMAC_REQ_MID],
  515. DMAC23_DMARS_CH2_MID_SHIFT,
  516. DMAC23_DMARS_CH2_MID);
  517. /* PR : Round robin mode */
  518. RZA_IO_RegWrite_32(&DMAC07.DCTRL_0_7,
  519. 1,
  520. DMAC07_DCTRL_0_7_PR_SHIFT,
  521. DMAC07_DCTRL_0_7_PR);
  522. }
  523. }
  524. /*******************************************************************************
  525. * Function Name: usb0_function_DMAC2_Open
  526. * Description : Enables DMAC channel 2 transfer.
  527. * Arguments : uint32_t req : DMAC request mode
  528. * Return Value : 0 : Succeeded in enabling DMA transfer
  529. * : -1 : Failed to enable DMA transfer (due to DMA operation)
  530. *******************************************************************************/
  531. int32_t usb0_function_DMAC2_Open (uint32_t req)
  532. {
  533. int32_t ret;
  534. volatile uint8_t dummy;
  535. /* Transferable? */
  536. if ((0 == RZA_IO_RegRead_32(&DMAC.CHSTAT_2,
  537. DMAC2_CHSTAT_n_EN_SHIFT,
  538. DMAC2_CHSTAT_n_EN)) &&
  539. (0 == RZA_IO_RegRead_32(&DMAC.CHSTAT_2,
  540. DMAC2_CHSTAT_n_TACT_SHIFT,
  541. DMAC2_CHSTAT_n_TACT)))
  542. {
  543. /* Clear Channel Status Register */
  544. RZA_IO_RegWrite_32(&DMAC2.CHCTRL_n,
  545. 1,
  546. DMAC2_CHCTRL_n_SWRST_SHIFT,
  547. DMAC2_CHCTRL_n_SWRST);
  548. dummy = RZA_IO_RegRead_32(&DMAC2.CHCTRL_n,
  549. DMAC2_CHCTRL_n_SWRST_SHIFT,
  550. DMAC2_CHCTRL_n_SWRST);
  551. /* Enable DMA transfer */
  552. RZA_IO_RegWrite_32(&DMAC2.CHCTRL_n,
  553. 1,
  554. DMAC2_CHCTRL_n_SETEN_SHIFT,
  555. DMAC2_CHCTRL_n_SETEN);
  556. /* ---- Request by software ---- */
  557. if (DMAC_REQ_MODE_SOFT == req)
  558. {
  559. /* DMA transfer Request by software */
  560. RZA_IO_RegWrite_32(&DMAC2.CHCTRL_n,
  561. 1,
  562. DMAC2_CHCTRL_n_STG_SHIFT,
  563. DMAC2_CHCTRL_n_STG);
  564. }
  565. ret = 0;
  566. }
  567. else
  568. {
  569. ret = -1;
  570. }
  571. return ret;
  572. }
  573. /*******************************************************************************
  574. * Function Name: usb0_function_DMAC2_Close
  575. * Description : Aborts DMAC channel 2 transfer. Returns the remaining transfer
  576. * : byte count at the time of DMA transfer abort to the argument
  577. * : *remain.
  578. * Arguments : uint32_t * remain : Remaining transfer byte count when
  579. * : : DMA transfer is aborted
  580. * Return Value : none
  581. *******************************************************************************/
  582. void usb0_function_DMAC2_Close (uint32_t * remain)
  583. {
  584. /* ==== Abort transfer ==== */
  585. RZA_IO_RegWrite_32(&DMAC2.CHCTRL_n,
  586. 1,
  587. DMAC2_CHCTRL_n_CLREN_SHIFT,
  588. DMAC2_CHCTRL_n_CLREN);
  589. while (1 == RZA_IO_RegRead_32(&DMAC2.CHSTAT_n,
  590. DMAC2_CHSTAT_n_TACT_SHIFT,
  591. DMAC2_CHSTAT_n_TACT))
  592. {
  593. /* Loop until transfer is aborted */
  594. }
  595. while (1 == RZA_IO_RegRead_32(&DMAC2.CHSTAT_n,
  596. DMAC2_CHSTAT_n_EN_SHIFT,
  597. DMAC2_CHSTAT_n_EN))
  598. {
  599. /* Loop until 0 is set in EN before checking the remaining transfer byte count */
  600. }
  601. /* ==== Obtain remaining transfer byte count ==== */
  602. *remain = DMAC2.CRTB_n;
  603. }
  604. /*******************************************************************************
  605. * Function Name: usb0_function_DMAC2_Load_Set
  606. * Description : Sets the transfer source address, transfer destination
  607. * : address, and total transfer byte count respectively
  608. * : specified by the argument src_addr, dst_addr, and count to
  609. * : DMAC channel 2 as DMA transfer information.
  610. * : Sets the register set selected by the CHCFG_n register
  611. * : RSEL bit from the Next0 or Next1 register set.
  612. * : This function should be called when DMA transfer of DMAC
  613. * : channel 2 is aboted.
  614. * Arguments : uint32_t src_addr : Transfer source address
  615. * : uint32_t dst_addr : Transfer destination address
  616. * : uint32_t count : Total transfer byte count
  617. * Return Value : none
  618. *******************************************************************************/
  619. void usb0_function_DMAC2_Load_Set (uint32_t src_addr, uint32_t dst_addr, uint32_t count)
  620. {
  621. uint8_t reg_set;
  622. /* Obtain register set in use */
  623. reg_set = RZA_IO_RegRead_32(&DMAC2.CHSTAT_n,
  624. DMAC2_CHSTAT_n_SR_SHIFT,
  625. DMAC2_CHSTAT_n_SR);
  626. /* ==== Load ==== */
  627. if (0 == reg_set)
  628. {
  629. /* ---- Next0 Register Set ---- */
  630. DMAC2.N0SA_n = src_addr; /* Start address of transfer source */
  631. DMAC2.N0DA_n = dst_addr; /* Start address of transfer destination */
  632. DMAC2.N0TB_n = count; /* Total transfer byte count */
  633. }
  634. else
  635. {
  636. /* ---- Next1 Register Set ---- */
  637. DMAC2.N1SA_n = src_addr; /* Start address of transfer source */
  638. DMAC2.N1DA_n = dst_addr; /* Start address of transfer destination */
  639. DMAC2.N1TB_n = count; /* Total transfer byte count */
  640. }
  641. }
  642. /* End of File */