PageRenderTime 57ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/configs/stm3220g-eval/src/stm32_usb.c

https://bitbucket.org/hg42/nuttx
C | 311 lines | 121 code | 51 blank | 139 comment | 10 complexity | 0fca7875c2810d903f78be222fca3a91 MD5 | raw file
Possible License(s): 0BSD
  1. /************************************************************************************
  2. * configs/stm3220g-eval/src/stm32_usb.c
  3. *
  4. * Copyright (C) 2012-2013, 2015, 2017 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ************************************************************************************/
  35. /************************************************************************************
  36. * Included Files
  37. ************************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <sys/types.h>
  40. #include <stdint.h>
  41. #include <stdbool.h>
  42. #include <sched.h>
  43. #include <errno.h>
  44. #include <assert.h>
  45. #include <debug.h>
  46. #include <nuttx/kthread.h>
  47. #include <nuttx/usb/usbdev.h>
  48. #include <nuttx/usb/usbhost.h>
  49. #include <nuttx/usb/usbdev_trace.h>
  50. #include "up_arch.h"
  51. #include "stm32.h"
  52. #include "stm32_otgfs.h"
  53. #include "stm3220g-eval.h"
  54. #ifdef CONFIG_STM32_OTGFS
  55. /************************************************************************************
  56. * Pre-processor Definitions
  57. ************************************************************************************/
  58. #if defined(CONFIG_USBDEV) || defined(CONFIG_USBHOST)
  59. # define HAVE_USB 1
  60. #else
  61. # warning "CONFIG_STM32_OTGFS is enabled but neither CONFIG_USBDEV nor CONFIG_USBHOST"
  62. # undef HAVE_USB
  63. #endif
  64. #ifndef CONFIG_USBHOST_DEFPRIO
  65. # define CONFIG_USBHOST_DEFPRIO 50
  66. #endif
  67. #ifndef CONFIG_USBHOST_STACKSIZE
  68. # ifdef CONFIG_USBHOST_HUB
  69. # define CONFIG_USBHOST_STACKSIZE 1536
  70. # else
  71. # define CONFIG_USBHOST_STACKSIZE 1024
  72. # endif
  73. #endif
  74. /************************************************************************************
  75. * Private Data
  76. ************************************************************************************/
  77. #ifdef CONFIG_USBHOST
  78. static struct usbhost_connection_s *g_usbconn;
  79. #endif
  80. /************************************************************************************
  81. * Private Functions
  82. ************************************************************************************/
  83. /************************************************************************************
  84. * Name: usbhost_waiter
  85. *
  86. * Description:
  87. * Wait for USB devices to be connected.
  88. *
  89. ************************************************************************************/
  90. #ifdef CONFIG_USBHOST
  91. static int usbhost_waiter(int argc, char *argv[])
  92. {
  93. struct usbhost_hubport_s *hport;
  94. uinfo("Running\n");
  95. for (;;)
  96. {
  97. /* Wait for the device to change state */
  98. DEBUGVERIFY(CONN_WAIT(g_usbconn, &hport));
  99. uinfo("%s\n", hport->connected ? "connected" : "disconnected");
  100. /* Did we just become connected? */
  101. if (hport->connected)
  102. {
  103. /* Yes.. enumerate the newly connected device */
  104. (void)CONN_ENUMERATE(g_usbconn, hport);
  105. }
  106. }
  107. /* Keep the compiler from complaining */
  108. return 0;
  109. }
  110. #endif
  111. /************************************************************************************
  112. * Public Functions
  113. ************************************************************************************/
  114. /************************************************************************************
  115. * Name: stm32_usbinitialize
  116. *
  117. * Description:
  118. * Called from stm32_usbinitialize very early in inialization to setup USB-related
  119. * GPIO pins for the STM3220G-EVAL board.
  120. *
  121. ************************************************************************************/
  122. void stm32_usbinitialize(void)
  123. {
  124. #ifdef HAVE_USB
  125. /* The OTG FS has an internal soft pull-up. No GPIO configuration is required */
  126. /* Configure the OTG FS VBUS sensing GPIO, Power On, and Overcurrent GPIOs */
  127. stm32_configgpio(GPIO_OTGFS_VBUS);
  128. stm32_configgpio(GPIO_OTGFS_PWRON);
  129. stm32_configgpio(GPIO_OTGFS_OVER);
  130. #endif
  131. }
  132. /***********************************************************************************
  133. * Name: stm32_usbhost_initialize
  134. *
  135. * Description:
  136. * Called at application startup time to initialize the USB host functionality.
  137. * This function will start a thread that will monitor for device
  138. * connection/disconnection events.
  139. *
  140. ***********************************************************************************/
  141. #ifdef CONFIG_USBHOST
  142. int stm32_usbhost_initialize(void)
  143. {
  144. int pid;
  145. int ret;
  146. /* First, register all of the class drivers needed to support the drivers
  147. * that we care about:
  148. */
  149. uinfo("Register class drivers\n");
  150. #ifdef CONFIG_USBHOST_MSC
  151. /* Register the USB mass storage class class */
  152. ret = usbhost_msc_initialize();
  153. if (ret != OK)
  154. {
  155. uerr("ERROR: Failed to register the mass storage class: %d\n", ret);
  156. }
  157. #endif
  158. #ifdef CONFIG_USBHOST_CDCACM
  159. /* Register the CDC/ACM serial class */
  160. ret = usbhost_cdcacm_initialize();
  161. if (ret != OK)
  162. {
  163. uerr("ERROR: Failed to register the CDC/ACM serial class: %d\n", ret);
  164. }
  165. #endif
  166. /* Then get an instance of the USB host interface */
  167. uinfo("Initialize USB host\n");
  168. g_usbconn = stm32_otgfshost_initialize(0);
  169. if (g_usbconn)
  170. {
  171. /* Start a thread to handle device connection. */
  172. uinfo("Start usbhost_waiter\n");
  173. pid = kthread_create("usbhost", CONFIG_USBHOST_DEFPRIO,
  174. CONFIG_USBHOST_STACKSIZE,
  175. (main_t)usbhost_waiter, (FAR char * const *)NULL);
  176. return pid < 0 ? -ENOEXEC : OK;
  177. }
  178. return -ENODEV;
  179. }
  180. #endif
  181. /***********************************************************************************
  182. * Name: stm32_usbhost_vbusdrive
  183. *
  184. * Description:
  185. * Enable/disable driving of VBUS 5V output. This function must be provided be
  186. * each platform that implements the STM32 OTG FS host interface
  187. *
  188. * "On-chip 5 V VBUS generation is not supported. For this reason, a charge pump
  189. * or, if 5 V are available on the application board, a basic power switch, must
  190. * be added externally to drive the 5 V VBUS line. The external charge pump can
  191. * be driven by any GPIO output. When the application decides to power on VBUS
  192. * using the chosen GPIO, it must also set the port power bit in the host port
  193. * control and status register (PPWR bit in OTG_FS_HPRT).
  194. *
  195. * "The application uses this field to control power to this port, and the core
  196. * clears this bit on an overcurrent condition."
  197. *
  198. * Input Parameters:
  199. * iface - For future growth to handle multiple USB host interface. Should be zero.
  200. * enable - true: enable VBUS power; false: disable VBUS power
  201. *
  202. * Returned Value:
  203. * None
  204. *
  205. ***********************************************************************************/
  206. #ifdef CONFIG_USBHOST
  207. void stm32_usbhost_vbusdrive(int iface, bool enable)
  208. {
  209. DEBUGASSERT(iface == 0);
  210. if (enable)
  211. {
  212. /* Enable the Power Switch by driving the enable pin low */
  213. stm32_gpiowrite(GPIO_OTGFS_PWRON, false);
  214. }
  215. else
  216. {
  217. /* Disable the Power Switch by driving the enable pin high */
  218. stm32_gpiowrite(GPIO_OTGFS_PWRON, true);
  219. }
  220. }
  221. #endif
  222. /************************************************************************************
  223. * Name: stm32_setup_overcurrent
  224. *
  225. * Description:
  226. * Setup to receive an interrupt-level callback if an overcurrent condition is
  227. * detected.
  228. *
  229. * Input Parameters:
  230. * handler - New overcurrent interrupt handler
  231. * arg - The argument provided for the interrupt handler
  232. *
  233. * Returned Value:
  234. * Zero (OK) is returned on success. Otherwise, a negated errno value is returned
  235. * to indicate the nature of the failure.
  236. *
  237. ************************************************************************************/
  238. #ifdef CONFIG_USBHOST
  239. int stm32_setup_overcurrent(xcpt_t handler, void *arg)
  240. {
  241. return stm32_gpiosetevent(GPIO_OTGFS_OVER, true, true, true, handler, arg);
  242. }
  243. #endif
  244. /************************************************************************************
  245. * Name: stm32_usbsuspend
  246. *
  247. * Description:
  248. * Board logic must provide the stm32_usbsuspend logic if the USBDEV driver is
  249. * used. This function is called whenever the USB enters or leaves suspend mode.
  250. * This is an opportunity for the board logic to shutdown clocks, power, etc.
  251. * while the USB is suspended.
  252. *
  253. ************************************************************************************/
  254. #ifdef CONFIG_USBDEV
  255. void stm32_usbsuspend(FAR struct usbdev_s *dev, bool resume)
  256. {
  257. uinfo("resume: %d\n", resume);
  258. }
  259. #endif
  260. #endif /* CONFIG_STM32_OTGFS */