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_sub.c 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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_sub.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. #if 0
  49. extern const uint16_t *g_usb1_function_EndPntPtr[];
  50. extern uint8_t g_usb1_function_DeviceDescriptor[];
  51. extern uint8_t *g_usb1_function_ConfigurationPtr[];
  52. #endif
  53. /*******************************************************************************
  54. Exported global variables and functions (to be accessed by other files)
  55. *******************************************************************************/
  56. /*******************************************************************************
  57. Private global variables and functions
  58. *******************************************************************************/
  59. /*******************************************************************************
  60. * Function Name: usb1_function_ResetDCP
  61. * Description : Initializes the default control pipe(DCP).
  62. * Outline : Reset default control pipe
  63. * Arguments : none
  64. * Return Value : none
  65. *******************************************************************************/
  66. void usb1_function_ResetDCP (void)
  67. {
  68. USB201.DCPCFG = 0;
  69. #if 0
  70. USB201.DCPMAXP = g_usb1_function_DeviceDescriptor[7];
  71. #else
  72. USB201.DCPMAXP = 64;
  73. #endif
  74. USB201.CFIFOSEL = (uint16_t)(USB_FUNCTION_BITMBW_8 | USB_FUNCTION_BITBYTE_LITTLE);
  75. USB201.D0FIFOSEL = (uint16_t)(USB_FUNCTION_BITMBW_8 | USB_FUNCTION_BITBYTE_LITTLE);
  76. USB201.D1FIFOSEL = (uint16_t)(USB_FUNCTION_BITMBW_8 | USB_FUNCTION_BITBYTE_LITTLE);
  77. }
  78. /*******************************************************************************
  79. * Function Name: usb1_function_ResetEP
  80. * Description : Initializes the end point.
  81. * Arguments : uint16_t num ; Configuration Number
  82. * Return Value : none
  83. *******************************************************************************/
  84. #if 0
  85. void usb1_function_ResetEP (uint16_t num)
  86. {
  87. uint16_t pipe;
  88. uint16_t ep;
  89. uint16_t index;
  90. uint16_t buf;
  91. uint16_t * tbl;
  92. tbl = (uint16_t *)(g_usb1_function_EndPntPtr[num - 1]);
  93. for (ep = 1; ep <= USB_FUNCTION_MAX_EP_NO; ++ep)
  94. {
  95. if (g_usb1_function_EPTableIndex[ep] != USB_FUNCTION_EP_ERROR)
  96. {
  97. index = (uint16_t)(USB_FUNCTION_EPTABLE_LENGTH * g_usb1_function_EPTableIndex[ep]);
  98. pipe = (uint16_t)(tbl[index + 0] & USB_FUNCTION_BITCURPIPE);
  99. g_usb1_function_PipeTbl[pipe] = (uint16_t)(((tbl[index + 1] & USB_FUNCTION_DIRFIELD) << 3) |
  100. ep |
  101. (tbl[index + 0] & USB_FUNCTION_FIFO_USE));
  102. if ((tbl[index + 1] & USB_FUNCTION_DIRFIELD) == USB_FUNCTION_DIR_P_OUT)
  103. {
  104. tbl[index + 1] |= USB_FUNCTION_SHTNAKON;
  105. #ifdef __USB_DMA_BFRE_ENABLE__
  106. /* this routine cannnot be perfomred if read operation is executed in buffer size */
  107. if (((tbl[index + 0] & USB_FUNCTION_FIFO_USE) == USB_FUNCTION_D0FIFO_DMA) ||
  108. ((tbl[index + 0] & USB_FUNCTION_FIFO_USE) == USB_FUNCTION_D1FIFO_DMA))
  109. {
  110. tbl[index + 1] |= USB_FUNCTION_BFREON;
  111. }
  112. #endif
  113. }
  114. /* Interrupt Disable */
  115. buf = USB201.BRDYENB;
  116. buf &= (uint16_t)~g_usb1_function_bit_set[pipe];
  117. USB201.BRDYENB = buf;
  118. buf = USB201.NRDYENB;
  119. buf &= (uint16_t)~g_usb1_function_bit_set[pipe];
  120. USB201.NRDYENB = buf;
  121. buf = USB201.BEMPENB;
  122. buf &= (uint16_t)~g_usb1_function_bit_set[pipe];
  123. USB201.BEMPENB = buf;
  124. usb1_function_set_pid_nak(pipe);
  125. /* CurrentPIPE Clear */
  126. if (RZA_IO_RegRead_16(&USB201.CFIFOSEL,
  127. USB_CFIFOSEL_CURPIPE_SHIFT,
  128. USB_CFIFOSEL_CURPIPE) == pipe)
  129. {
  130. RZA_IO_RegWrite_16(&USB201.CFIFOSEL,
  131. 0,
  132. USB_CFIFOSEL_CURPIPE_SHIFT,
  133. USB_CFIFOSEL_CURPIPE);
  134. }
  135. if (RZA_IO_RegRead_16(&USB201.D0FIFOSEL,
  136. USB_DnFIFOSEL_CURPIPE_SHIFT,
  137. USB_DnFIFOSEL_CURPIPE) == pipe)
  138. {
  139. RZA_IO_RegWrite_16(&USB201.D0FIFOSEL,
  140. 0,
  141. USB_DnFIFOSEL_CURPIPE_SHIFT,
  142. USB_DnFIFOSEL_CURPIPE);
  143. }
  144. if (RZA_IO_RegRead_16(&USB201.D1FIFOSEL,
  145. USB_DnFIFOSEL_CURPIPE_SHIFT,
  146. USB_DnFIFOSEL_CURPIPE) == pipe)
  147. {
  148. RZA_IO_RegWrite_16(&USB201.D1FIFOSEL,
  149. 0,
  150. USB_DnFIFOSEL_CURPIPE_SHIFT,
  151. USB_DnFIFOSEL_CURPIPE);
  152. }
  153. /* PIPE Configuration */
  154. USB201.PIPESEL = pipe;
  155. USB201.PIPECFG = tbl[index + 1];
  156. USB201.PIPEBUF = tbl[index + 2];
  157. USB201.PIPEMAXP = tbl[index + 3];
  158. USB201.PIPEPERI = tbl[index + 4];
  159. g_usb1_function_pipecfg[pipe] = tbl[index + 1];
  160. g_usb1_function_pipebuf[pipe] = tbl[index + 2];
  161. g_usb1_function_pipemaxp[pipe] = tbl[index + 3];
  162. g_usb1_function_pipeperi[pipe] = tbl[index + 4];
  163. /* Buffer Clear */
  164. usb1_function_set_sqclr(pipe);
  165. usb1_function_aclrm(pipe);
  166. /* init Global */
  167. g_usb1_function_pipe_status[pipe] = DEVDRV_USBF_PIPE_IDLE;
  168. g_usb1_function_PipeDataSize[pipe] = 0;
  169. }
  170. }
  171. }
  172. #endif
  173. /*******************************************************************************
  174. * Function Name: usb1_function_EpToPipe
  175. * Description : Returns the pipe which end point specified by the argument is
  176. * : allocated to.
  177. * Arguments : uint16_t ep ; Direction + Endpoint Number
  178. * Return Value : USB_FUNCTION_EP_ERROR : Error
  179. * : Others : Pipe Number
  180. *******************************************************************************/
  181. uint16_t usb1_function_EpToPipe (uint16_t ep)
  182. {
  183. uint16_t pipe;
  184. for (pipe = 1; pipe <= USB_FUNCTION_MAX_PIPE_NO; pipe++)
  185. {
  186. if ((g_usb1_function_PipeTbl[pipe] & 0x00ff) == ep)
  187. {
  188. return pipe;
  189. }
  190. }
  191. return USB_FUNCTION_EP_ERROR;
  192. }
  193. /*******************************************************************************
  194. * Function Name: usb1_function_InitEPTable
  195. * Description : Sets the end point by the Alternate setting value of the
  196. * : configuration number and the interface number specified by the
  197. * : argument.
  198. * Arguments : uint16_t Con_Num ; Configuration Number
  199. * : uint16_t Int_Num ; Interface Number
  200. * : uint16_t Alt_Num ; Alternate Setting
  201. * Return Value : none
  202. *******************************************************************************/
  203. #if 0
  204. void usb1_function_InitEPTable (uint16_t Con_Num, uint16_t Int_Num, uint16_t Alt_Num)
  205. {
  206. uint8_t * ptr;
  207. uint16_t point_interface;
  208. uint16_t point_endpoint;
  209. uint16_t length;
  210. uint16_t start;
  211. uint16_t numbers;
  212. uint16_t endpoint;
  213. ptr = (uint8_t *)g_usb1_function_ConfigurationPtr[Con_Num - 1];
  214. point_interface = *ptr;
  215. length = (uint16_t)((uint16_t)*(ptr + 3) << 8 | (uint16_t)*(ptr + 2));
  216. ptr += *ptr;
  217. start = 0;
  218. numbers = 0;
  219. point_endpoint = 0;
  220. for (; point_interface < length;)
  221. {
  222. switch (*(ptr + 1)) /* Descriptor Type ? */
  223. {
  224. case USB_FUNCTION_DT_INTERFACE: /* Interface */
  225. if ((*(ptr + 2) == Int_Num) && (*(ptr + 3) == Alt_Num))
  226. {
  227. numbers = *(ptr + 4);
  228. }
  229. else
  230. {
  231. start += *(ptr + 4);
  232. }
  233. point_interface += *ptr;
  234. ptr += *ptr;
  235. break;
  236. case USB_FUNCTION_DT_ENDPOINT: /* Endpoint */
  237. if (point_endpoint < numbers)
  238. {
  239. endpoint = (uint16_t)(*(ptr + 2) & 0x0f);
  240. g_usb1_function_EPTableIndex[endpoint] = (uint16_t)(start + point_endpoint);
  241. ++point_endpoint;
  242. }
  243. point_interface += *ptr;
  244. ptr += *ptr;
  245. break;
  246. case USB_FUNCTION_DT_DEVICE: /* Device */
  247. case USB_FUNCTION_DT_CONFIGURATION: /* Configuration */
  248. case USB_FUNCTION_DT_STRING: /* String */
  249. default: /* Class, Vendor, else */
  250. point_interface += *ptr;
  251. ptr += *ptr;
  252. break;
  253. }
  254. }
  255. }
  256. #endif
  257. /*******************************************************************************
  258. * Function Name: usb1_function_GetConfigNum
  259. * Description : Returns the number of configuration referring to the number of
  260. * : configuration described in the device descriptor.
  261. * Arguments : none
  262. * Return Value : Number of possible configurations (bNumConfigurations).
  263. *******************************************************************************/
  264. #if 0
  265. uint16_t usb1_function_GetConfigNum (void)
  266. {
  267. return (uint16_t)g_usb1_function_DeviceDescriptor[17];
  268. }
  269. #endif
  270. /*******************************************************************************
  271. * Function Name: usb1_function_GetInterfaceNum
  272. * Description : Returns the number of interface referring to the number of
  273. * : interface described in the configuration descriptor.
  274. * Arguments : uint16_t num ; Configuration Number
  275. * Return Value : Number of this interface (bNumInterfaces).
  276. *******************************************************************************/
  277. #if 0
  278. uint16_t usb1_function_GetInterfaceNum (uint16_t num)
  279. {
  280. return (uint16_t)(*(g_usb1_function_ConfigurationPtr[num - 1] + 4));
  281. }
  282. #endif
  283. /*******************************************************************************
  284. * Function Name: usb1_function_GetAltNum
  285. * Description : Returns the Alternate setting value of the configuration number
  286. * : and the interface number specified by the argument.
  287. * Arguments : uint16_t Con_Num ; Configuration Number
  288. * : uint16_t Int_Num ; Interface Number
  289. * Return Value : Value used to select this alternate setting(bAlternateSetting).
  290. *******************************************************************************/
  291. #if 0
  292. uint16_t usb1_function_GetAltNum (uint16_t Con_Num, uint16_t Int_Num)
  293. {
  294. uint8_t * ptr;
  295. uint16_t point;
  296. uint16_t alt_num = 0;
  297. uint16_t length;
  298. ptr = (uint8_t *)(g_usb1_function_ConfigurationPtr[Con_Num - 1]);
  299. point = ptr[0];
  300. ptr += ptr[0]; /* InterfaceDescriptor[0] */
  301. length = (uint16_t)(*(g_usb1_function_ConfigurationPtr[Con_Num - 1] + 2));
  302. length |= (uint16_t)((uint16_t)(*(g_usb1_function_ConfigurationPtr[Con_Num - 1] + 3)) << 8);
  303. for (; point < length;) /* Search Descriptor Table size */
  304. {
  305. switch (ptr[1]) /* Descriptor Type ? */
  306. {
  307. case USB_FUNCTION_DT_INTERFACE: /* Interface */
  308. if (Int_Num == ptr[2])
  309. {
  310. alt_num = (uint16_t)ptr[3]; /* Alternate Number count */
  311. }
  312. point += ptr[0];
  313. ptr += ptr[0];
  314. break;
  315. case USB_FUNCTION_DT_DEVICE: /* Device */
  316. case USB_FUNCTION_DT_CONFIGURATION: /* Configuration */
  317. case USB_FUNCTION_DT_STRING: /* String */
  318. case USB_FUNCTION_DT_ENDPOINT: /* Endpoint */
  319. default: /* Class, Vendor, else */
  320. point += ptr[0];
  321. ptr += ptr[0];
  322. break;
  323. }
  324. }
  325. return alt_num;
  326. }
  327. #endif
  328. /*******************************************************************************
  329. * Function Name: usb1_function_CheckRemoteWakeup
  330. * Description : Returns the result of the remote wake up function is supported
  331. * : or not referring to the configuration descriptor.
  332. * Arguments : none
  333. * Return Value : DEVDRV_USBF_ON : Support Remote Wakeup
  334. * : DEVDRV_USBF_OFF : not Support Remote Wakeup
  335. *******************************************************************************/
  336. #if 0
  337. uint16_t usb1_function_CheckRemoteWakeup (void)
  338. {
  339. uint8_t atr;
  340. if (g_usb1_function_ConfigNum == 0)
  341. {
  342. return DEVDRV_USBF_OFF;
  343. }
  344. atr = *(g_usb1_function_ConfigurationPtr[g_usb1_function_ConfigNum - 1] + 7);
  345. if (atr & USB_FUNCTION_CF_RWUP)
  346. {
  347. return DEVDRV_USBF_ON;
  348. }
  349. return DEVDRV_USBF_OFF;
  350. }
  351. #endif
  352. /*******************************************************************************
  353. * Function Name: usb1_function_clear_alt
  354. * Description : Initializes the Alternate setting area.
  355. * Arguments : none
  356. * Return Value : none
  357. *******************************************************************************/
  358. #if 0
  359. void usb1_function_clear_alt (void)
  360. {
  361. int i;
  362. for (i = 0; i < USB_FUNCTION_ALT_NO; ++i)
  363. {
  364. g_usb1_function_Alternate[i] = 0; /* Alternate */
  365. }
  366. }
  367. #endif
  368. /*******************************************************************************
  369. * Function Name: usb1_function_clear_pipe_tbl
  370. * Description : Initializes pipe definition table.
  371. * Arguments : none
  372. * Return Value : none
  373. *******************************************************************************/
  374. void usb1_function_clear_pipe_tbl (void)
  375. {
  376. int pipe;
  377. for (pipe = 0; pipe < (USB_FUNCTION_MAX_PIPE_NO + 1); ++pipe)
  378. {
  379. g_usb1_function_PipeTbl[pipe] = 0;
  380. }
  381. }
  382. /*******************************************************************************
  383. * Function Name: usb1_function_clear_ep_table_index
  384. * Description : Initializes the end point table index.
  385. * Arguments : none
  386. * Return Value : none
  387. *******************************************************************************/
  388. #if 0
  389. void usb1_function_clear_ep_table_index (void)
  390. {
  391. int ep;
  392. for (ep = 0; ep <= USB_FUNCTION_MAX_EP_NO; ++ep)
  393. {
  394. g_usb1_function_EPTableIndex[ep] = USB_FUNCTION_EP_ERROR;
  395. }
  396. }
  397. #endif
  398. /* End of File */