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_api.c 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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_api.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. #include "dev_drv.h"
  40. /*******************************************************************************
  41. Typedef definitions
  42. *******************************************************************************/
  43. /*******************************************************************************
  44. Macro definitions
  45. *******************************************************************************/
  46. /*******************************************************************************
  47. Imported global variables and functions (from other files)
  48. *******************************************************************************/
  49. /*******************************************************************************
  50. Exported global variables and functions (to be accessed by other files)
  51. *******************************************************************************/
  52. /*******************************************************************************
  53. Private global variables and functions
  54. *******************************************************************************/
  55. /*******************************************************************************
  56. * Function Name: usb1_api_function_init
  57. * Description : Initializes the USB module in the USB function mode.
  58. * Arguments : uint8_t int_level ; interruput level
  59. * : uint16_t mode : Speed modes
  60. * : : USB_FUCNTION_HIGH_SPEED: High-speed device
  61. * : : USB_FUCNTION_FULL_SPEED: Full-speed device
  62. * : uint16_t clockmode ; 48MHz ; USBFCLOCK_X1_48MHZ
  63. * : ; 12MHz ; USBFCLOCK_EXTAL_12MHZ
  64. * Return Value : none
  65. *******************************************************************************/
  66. #if 0
  67. void usb1_api_function_init (uint8_t int_level, uint16_t mode, uint16_t clockmode)
  68. {
  69. volatile uint8_t dummy_buf;
  70. CPG.STBCR7 &= 0xfc; /*The clock of USB0/1 modules is permitted */
  71. dummy_buf = CPG.STBCR7; /* (Dummy read) */
  72. usb1_function_setting_interrupt(int_level);
  73. usb1_function_reset_module(clockmode); /* reset USB module with setting tranciever */
  74. /* and HSE=1 */
  75. usb1_function_init_status(); /* clear variables */
  76. usb1_function_InitModule(mode); /* select USB Function and Interrupt Enable */
  77. /* Detect USB Device to attach or detach */
  78. }
  79. #endif
  80. /*******************************************************************************
  81. * Function Name: usb1_api_function_IsConfigured
  82. * Description : Checks if the USB device is configured to return the result as
  83. * : the return value.
  84. * Arguments : none
  85. * Return Value : DEVDRV_USBF_YES : Configured & Configured Suspend
  86. * : DEVDRV_USBF_NO : not Configured
  87. *******************************************************************************/
  88. uint16_t usb1_api_function_IsConfigured (void)
  89. {
  90. uint16_t dvst;
  91. dvst = usb1_function_GetDeviceState();
  92. if ((dvst == USB_FUNCTION_DVST_CONFIGURED) ||
  93. (dvst == USB_FUNCTION_DVST_CONFIGURED_SUSPEND))
  94. {
  95. return DEVDRV_USBF_YES;
  96. }
  97. return DEVDRV_USBF_NO;
  98. }
  99. /*******************************************************************************
  100. * Function Name: usb1_function_GetDeviceState
  101. * Description : Returns the state of USB device.
  102. * Arguments : none
  103. * Return Value : Device States
  104. *******************************************************************************/
  105. uint16_t usb1_function_GetDeviceState (void)
  106. {
  107. uint16_t dvsq;
  108. uint16_t dvst;
  109. dvsq = USB201.INTSTS0;
  110. switch (dvsq & USB_FUNCTION_BITDVSQ)
  111. {
  112. case USB_FUNCTION_DS_POWR: /* Power state *//* power-on */
  113. dvst = USB_FUNCTION_DVST_POWERED;
  114. break;
  115. case USB_FUNCTION_DS_DFLT: /* Default state *//* bus-reset */
  116. dvst = USB_FUNCTION_DVST_DEFAULT;
  117. break;
  118. case USB_FUNCTION_DS_ADDS: /* Address state */
  119. dvst = USB_FUNCTION_DVST_ADDRESS;
  120. break;
  121. case USB_FUNCTION_DS_CNFG: /* Configured state */
  122. dvst = USB_FUNCTION_DVST_CONFIGURED;
  123. break;
  124. case USB_FUNCTION_DS_SPD_CNFG: /* Configured Suspend state */
  125. dvst = USB_FUNCTION_DVST_CONFIGURED_SUSPEND;
  126. break;
  127. case USB_FUNCTION_DS_SPD_POWR: /* Power Suspend state */
  128. case USB_FUNCTION_DS_SPD_DFLT: /* Default Suspend state */
  129. case USB_FUNCTION_DS_SPD_ADDR: /* Address Suspend state */
  130. dvst = USB_FUNCTION_DVST_SUSPEND;
  131. break;
  132. default: /* error */
  133. dvst = USB_FUNCTION_DVST_SUSPEND;
  134. break;
  135. }
  136. return dvst;
  137. }
  138. /*******************************************************************************
  139. * Function Name: usb1_api_function_start_receive_transfer
  140. * Description : Starts USB data reception using the pipe specified in the argument.
  141. * : The FIFO for using is set in the pipe definition table.
  142. * Arguments : uint16_t pipe ; Pipe Number
  143. * : uint32_t size ; Data Size
  144. * : uint8_t *data ; Data data Address
  145. * Return Value : none
  146. *******************************************************************************/
  147. void usb1_api_function_start_receive_transfer (uint16_t pipe, uint32_t size, uint8_t * data)
  148. {
  149. usb1_function_start_receive_transfer(pipe, size, data);
  150. }
  151. /*******************************************************************************
  152. * Function Name: usb1_api_function_start_send_transfer
  153. * Description : Starts the USB data communication using pipe specified by the argument.
  154. * Arguments : uint16_t pipe ; Pipe Number
  155. * : uint32_t size ; Data Size
  156. * : uint8_t *data ; Data data Address
  157. * Return Value : DEVDRV_USBF_WRITEEND ; Write end
  158. * : DEVDRV_USBF_WRITESHRT ; short data
  159. * : DEVDRV_USBF_WRITING ; Continue of data write
  160. * : DEVDRV_USBF_WRITEDMA ; Write DMA
  161. * : DEVDRV_USBF_FIFOERROR ; FIFO status
  162. *******************************************************************************/
  163. uint16_t usb1_api_function_start_send_transfer (uint16_t pipe, uint32_t size, uint8_t * data)
  164. {
  165. uint16_t status;
  166. status = usb1_function_start_send_transfer(pipe, size, data);
  167. return status;
  168. }
  169. /*******************************************************************************
  170. * Function Name: usb1_api_function_check_pipe_status
  171. * Description : Starts USB data reception using the pipe specified in the argument.
  172. * : The FIFO for using is set in the pipe definition table.
  173. * Arguments : uint16_t pipe ; Pipe Number
  174. * : uint32_t *size ; Data Size
  175. * Return Value : Pipe Status
  176. *******************************************************************************/
  177. uint16_t usb1_api_function_check_pipe_status (uint16_t pipe, uint32_t * size)
  178. {
  179. if (g_usb1_function_pipe_status[pipe] == DEVDRV_USBF_PIPE_DONE)
  180. {
  181. *size = g_usb1_function_PipeDataSize[pipe];
  182. g_usb1_function_pipe_status[pipe] = DEVDRV_USBF_PIPE_IDLE;
  183. return DEVDRV_USBF_PIPE_DONE;
  184. }
  185. else if (g_usb1_function_pipe_status[pipe] == DEVDRV_USBF_PIPE_NORES)
  186. {
  187. *size = 0;
  188. g_usb1_function_pipe_status[pipe] = DEVDRV_USBF_PIPE_IDLE;
  189. return DEVDRV_USBF_PIPE_NORES;
  190. }
  191. else if (g_usb1_function_pipe_status[pipe] == DEVDRV_USBF_PIPE_STALL)
  192. {
  193. *size = 0;
  194. g_usb1_function_pipe_status[pipe] = DEVDRV_USBF_PIPE_IDLE;
  195. return DEVDRV_USBF_PIPE_STALL;
  196. }
  197. else if (g_usb1_function_pipe_status[pipe] == DEVDRV_USBF_FIFOERROR)
  198. {
  199. *size = 0;
  200. g_usb1_function_pipe_status[pipe] = DEVDRV_USBF_PIPE_IDLE;
  201. return DEVDRV_USBF_FIFOERROR;
  202. }
  203. else
  204. {
  205. /* Do Nothing */
  206. }
  207. return g_usb1_function_pipe_status[pipe];
  208. }
  209. /*******************************************************************************
  210. * Function Name: usb1_api_function_clear_pipe_status
  211. * Description : Starts USB data reception using the pipe specified in the argument.
  212. * : The FIFO for using is set in the pipe definition table.
  213. * Arguments : uint16_t pipe ; Pipe Number
  214. * Return Value : Pipe Status
  215. *******************************************************************************/
  216. void usb1_api_function_clear_pipe_status (uint16_t pipe)
  217. {
  218. g_usb1_function_pipe_status[pipe] = DEVDRV_USBF_PIPE_IDLE;
  219. g_usb1_function_PipeDataSize[pipe] = 0;
  220. }
  221. /*******************************************************************************
  222. * Function Name: usb1_api_function_set_pid_buf
  223. * Description : Enables communicaqtion in the pipe specified by the argument
  224. * : (BUF).
  225. * Arguments : uint16_t pipe ; pipe Number
  226. * Return Value : none
  227. *******************************************************************************/
  228. void usb1_api_function_set_pid_buf (uint16_t pipe)
  229. {
  230. usb1_function_set_pid_buf(pipe);
  231. }
  232. /*******************************************************************************
  233. * Function Name: usb1_api_function_set_pid_nak
  234. * Description : Disables communication (NAK) in the pipe specified by the argument.
  235. * : When the pipe status was enabling communication (BUF) before
  236. * : executing before executing this function, waits in the software
  237. * : until the pipe becomes ready after setting disabled.
  238. * Arguments : uint16_t pipe ; pipe Number
  239. * Return Value : none
  240. *******************************************************************************/
  241. void usb1_api_function_set_pid_nak (uint16_t pipe)
  242. {
  243. usb1_function_set_pid_nak(pipe);
  244. }
  245. /*******************************************************************************
  246. * Function Name: usb1_api_function_set_pid_stall
  247. * Description : Disables communication (STALL) in the pipe specified by the
  248. * : argument.
  249. * Arguments : uint16_t pipe ; pipe Number
  250. * Return Value : none
  251. *******************************************************************************/
  252. void usb1_api_function_set_pid_stall (uint16_t pipe)
  253. {
  254. usb1_function_set_pid_stall(pipe);
  255. }
  256. /*******************************************************************************
  257. * Function Name: usb1_api_function_clear_pid_stall
  258. * Description : Disables communication (NAK) in the pipe specified by the argument.
  259. * Arguments : uint16_t pipe ; pipe Number
  260. * Return Value : none
  261. *******************************************************************************/
  262. void usb1_api_function_clear_pid_stall (uint16_t pipe)
  263. {
  264. usb1_function_clear_pid_stall(pipe);
  265. }
  266. /*******************************************************************************
  267. * Function Name: usb1_api_function_get_pid
  268. * Description : Returns the pipe state specified by the argument.
  269. * Arguments : uint16_t pipe ; Pipe Number
  270. * Return Value : PID
  271. *******************************************************************************/
  272. uint16_t usb1_api_function_get_pid (uint16_t pipe)
  273. {
  274. uint16_t pid;
  275. pid = usb1_function_get_pid(pipe);
  276. return pid;
  277. }
  278. /*******************************************************************************
  279. * Function Name: usb1_api_function_check_stall
  280. * Description :
  281. * Arguments : uint16_t pipe ; Pipe Number
  282. * Return Value : PID
  283. *******************************************************************************/
  284. int32_t usb1_api_function_check_stall (uint16_t pipe)
  285. {
  286. uint16_t pid;
  287. pid = usb1_function_get_pid(pipe);
  288. if ((pid & DEVDRV_USBF_PID_STALL) == DEVDRV_USBF_PID_STALL)
  289. {
  290. return DEVDRV_USBF_STALL;
  291. }
  292. return DEVDRV_SUCCESS;
  293. }
  294. /*******************************************************************************
  295. * Function Name: usb1_api_function_set_sqclr
  296. * Description : Sets the sequence bit of the pipe specified by the argument to
  297. * : DATA0.
  298. * Arguments : uint16_t pipe ; Pipe Number
  299. * Return Value : none
  300. *******************************************************************************/
  301. void usb1_api_function_set_sqclr (uint16_t pipe)
  302. {
  303. usb1_function_set_sqclr(pipe);
  304. }
  305. /*******************************************************************************
  306. * Function Name: usb1_api_function_set_sqset
  307. * Description : Sets the sequence bit of the pipe specified by the argument to
  308. * : DATA1.
  309. * Arguments : uint16_t pipe ; Pipe number
  310. * Return Value : none
  311. *******************************************************************************/
  312. void usb1_api_function_set_sqset (uint16_t pipe)
  313. {
  314. usb1_function_set_sqset(pipe);
  315. }
  316. /*******************************************************************************
  317. * Function Name: usb1_api_function_set_csclr
  318. * Description : CSPLIT status clear setting of sprit transaction in specified
  319. * : pipe is performed.
  320. * : When SQSET bit or SQCLR bit, and SQSET bit or SQCLR bit
  321. * : in DCPCTR register are continuously changed (when the sequence
  322. * : toggle bit of data PID is continuously changed over two or more pipes),
  323. * : the access cycle with 120 ns and more than 5 cycle bus clock is necessary.
  324. * : Do not set both SQCLR bit and SQSET bit to 1 at the same time.
  325. * : In addition, both bits should be operated after PID is set to NAK.
  326. * : However, when it is set to the isochronous transfer as the transfer type
  327. * : (TYPE=11), writing in SQSET bit is disabled.
  328. * Arguments : uint16_t pipe ; Pipe number
  329. * Return Value : none
  330. *******************************************************************************/
  331. void usb1_api_function_set_csclr (uint16_t pipe)
  332. {
  333. usb1_function_set_csclr(pipe);
  334. }
  335. /*******************************************************************************
  336. * Function Name: usb1_api_function_set_curpipe
  337. * Description : Allocates FIF0 specifed by the argument in the pipe assigned
  338. * : by the argument.
  339. * Arguments : uint16_t pipe ; Pipe Number
  340. * : uint16_t fifosel ; Select FIFO
  341. * : uint16_t isel ; FIFO Access Direction
  342. * : uint16_t mbw ; FIFO Port Access Bit Width
  343. * Return Value : none
  344. *******************************************************************************/
  345. void usb1_api_function_set_curpipe (uint16_t pipe, uint16_t fifosel, uint16_t isel, uint16_t mbw)
  346. {
  347. usb1_function_set_curpipe(pipe, fifosel, isel, mbw);
  348. }
  349. /*******************************************************************************
  350. * Function Name: usb1_api_function_clear_brdy_sts
  351. * Description : Clear BRDY interrupt status in the pipe spceified by the argument.
  352. * Arguments : uint16_t pipe ; pipe Number
  353. * Return Value : none
  354. *******************************************************************************/
  355. void usb1_api_function_clear_brdy_sts (uint16_t pipe)
  356. {
  357. usb1_function_clear_brdy_sts(pipe);
  358. }
  359. /*******************************************************************************
  360. * Function Name: usb1_api_function_clear_bemp_sts
  361. * Description : Clear BEMP interrupt status in the pipe spceified by the argument.
  362. * Arguments : uint16_t pipe ; pipe Number
  363. * Return Value : none
  364. *******************************************************************************/
  365. void usb1_api_function_clear_bemp_sts (uint16_t pipe)
  366. {
  367. usb1_function_clear_bemp_sts(pipe);
  368. }
  369. /*******************************************************************************
  370. * Function Name: usb1_api_function_clear_nrdy_sts
  371. * Description : Clear NRDY interrupt status in the pipe spceified by the argument.
  372. * Arguments : uint16_t pipe ; pipe Number
  373. * Return Value : none
  374. *******************************************************************************/
  375. void usb1_api_function_clear_nrdy_sts (uint16_t pipe)
  376. {
  377. usb1_function_clear_nrdy_sts(pipe);
  378. }
  379. /* End of File */