PageRenderTime 27ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/stm32f429i-disco/src/stm32_usb.c

https://bitbucket.org/sanyaade/nuttx_boards
C | 313 lines | 123 code | 52 blank | 138 comment | 11 complexity | c56cf4f2e4a53c9eba33d66c17bc6569 MD5 | raw file
  1. /************************************************************************************
  2. * configs/stm32f429i-disco/src/stm32_usbdev.c
  3. *
  4. * Copyright (C) 2013, 2015 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/usb/usbdev.h>
  47. #include <nuttx/usb/usbhost.h>
  48. #include <nuttx/usb/usbdev_trace.h>
  49. #include "up_arch.h"
  50. #include "stm32.h"
  51. #include "stm32_otghs.h"
  52. #include "stm32f429i-disco.h"
  53. #ifdef CONFIG_STM32_OTGHS
  54. /************************************************************************************
  55. * Pre-processor Definitions
  56. ************************************************************************************/
  57. #if defined(CONFIG_USBDEV) || defined(CONFIG_USBHOST)
  58. # define HAVE_USB 1
  59. #else
  60. # warning "CONFIG_STM32_OTGHS is enabled but neither CONFIG_USBDEV nor CONFIG_USBHOST"
  61. # undef HAVE_USB
  62. #endif
  63. #ifndef CONFIG_STM32F429IDISCO_USBHOST_PRIO
  64. # define CONFIG_STM32F429IDISCO_USBHOST_PRIO 100
  65. #endif
  66. #ifndef CONFIG_STM32F429IDISCO_USBHOST_STACKSIZE
  67. # define CONFIG_STM32F429IDISCO_USBHOST_STACKSIZE 1024
  68. #endif
  69. /************************************************************************************
  70. * Private Data
  71. ************************************************************************************/
  72. #ifdef CONFIG_USBHOST
  73. static struct usbhost_connection_s *g_usbconn;
  74. #endif
  75. /************************************************************************************
  76. * Private Functions
  77. ************************************************************************************/
  78. /************************************************************************************
  79. * Name: usbhost_waiter
  80. *
  81. * Description:
  82. * Wait for USB devices to be connected.
  83. *
  84. ************************************************************************************/
  85. #ifdef CONFIG_USBHOST
  86. static int usbhost_waiter(int argc, char *argv[])
  87. {
  88. struct usbhost_hubport_s *hport;
  89. uvdbg("Running\n");
  90. for (;;)
  91. {
  92. /* Wait for the device to change state */
  93. DEBUGVERIFY(CONN_WAIT(g_usbconn, &hport));
  94. uvdbg("%s\n", hport->connected ? "connected" : "disconnected");
  95. /* Did we just become connected? */
  96. if (hport->connected)
  97. {
  98. /* Yes.. enumerate the newly connected device */
  99. (void)CONN_ENUMERATE(g_usbconn, hport);
  100. }
  101. }
  102. /* Keep the compiler from complaining */
  103. return 0;
  104. }
  105. #endif
  106. /************************************************************************************
  107. * Public Functions
  108. ************************************************************************************/
  109. /************************************************************************************
  110. * Name: stm32_usbinitialize
  111. *
  112. * Description:
  113. * Called from stm32_usbinitialize very early in inialization to setup USB-related
  114. * GPIO pins for the STM32F4Discovery board.
  115. *
  116. ************************************************************************************/
  117. void stm32_usbinitialize(void)
  118. {
  119. /* The OTG HS has an internal soft pull-up. No GPIO configuration is required */
  120. /* Configure the OTG HS VBUS sensing GPIO, Power On, and Overcurrent GPIOs */
  121. #ifdef CONFIG_STM32_OTGHS
  122. stm32_configgpio(GPIO_OTGHS_VBUS);
  123. stm32_configgpio(GPIO_OTGHS_PWRON);
  124. stm32_configgpio(GPIO_OTGHS_OVER);
  125. #endif
  126. }
  127. /***********************************************************************************
  128. * Name: stm32_usbhost_initialize
  129. *
  130. * Description:
  131. * Called at application startup time to initialize the USB host functionality.
  132. * This function will start a thread that will monitor for device
  133. * connection/disconnection events.
  134. *
  135. ***********************************************************************************/
  136. #ifdef CONFIG_USBHOST
  137. int stm32_usbhost_initialize(void)
  138. {
  139. int pid;
  140. int ret;
  141. /* First, register all of the class drivers needed to support the drivers
  142. * that we care about:
  143. */
  144. uvdbg("Register class drivers\n");
  145. #ifdef CONFIG_USBHOST_HUB
  146. /* Initialize USB hub class support */
  147. ret = usbhost_hub_initialize();
  148. if (ret < 0)
  149. {
  150. udbg("ERROR: usbhost_hub_initialize failed: %d\n", ret);
  151. }
  152. #endif
  153. #ifdef CONFIG_USBHOST_MSC
  154. /* Register the USB mass storage class class */
  155. ret = usbhost_msc_initialize();
  156. if (ret != OK)
  157. {
  158. udbg("ERROR: Failed to register the mass storage class: %d\n", ret);
  159. }
  160. #endif
  161. #ifdef CONFIG_USBHOST_CDCACM
  162. /* Register the CDC/ACM serial class */
  163. ret = usbhost_cdcacm_initialize();
  164. if (ret != OK)
  165. {
  166. udbg("ERROR: Failed to register the CDC/ACM serial class: %d\n", ret);
  167. }
  168. #endif
  169. /* Then get an instance of the USB host interface */
  170. uvdbg("Initialize USB host\n");
  171. g_usbconn = stm32_otghshost_initialize(0);
  172. if (g_usbconn)
  173. {
  174. /* Start a thread to handle device connection. */
  175. uvdbg("Start usbhost_waiter\n");
  176. pid = task_create("usbhost", CONFIG_STM32F429IDISCO_USBHOST_PRIO,
  177. CONFIG_STM32F429IDISCO_USBHOST_STACKSIZE,
  178. (main_t)usbhost_waiter, (FAR char * const *)NULL);
  179. return pid < 0 ? -ENOEXEC : OK;
  180. }
  181. return -ENODEV;
  182. }
  183. #endif
  184. /***********************************************************************************
  185. * Name: stm32_usbhost_vbusdrive
  186. *
  187. * Description:
  188. * Enable/disable driving of VBUS 5V output. This function must be provided be
  189. * each platform that implements the STM32 OTG HS host interface
  190. *
  191. * "On-chip 5 V VBUS generation is not supported. For this reason, a charge pump
  192. * or, if 5 V are available on the application board, a basic power switch, must
  193. * be added externally to drive the 5 V VBUS line. The external charge pump can
  194. * be driven by any GPIO output. When the application decides to power on VBUS
  195. * using the chosen GPIO, it must also set the port power bit in the host port
  196. * control and status register (PPWR bit in OTG_HS_HPRT).
  197. *
  198. * "The application uses this field to control power to this port, and the core
  199. * clears this bit on an overcurrent condition."
  200. *
  201. * Input Parameters:
  202. * iface - For future growth to handle multiple USB host interface. Should be zero.
  203. * enable - true: enable VBUS power; false: disable VBUS power
  204. *
  205. * Returned Value:
  206. * None
  207. *
  208. ***********************************************************************************/
  209. #ifdef CONFIG_USBHOST
  210. void stm32_usbhost_vbusdrive(int iface, bool enable)
  211. {
  212. DEBUGASSERT(iface == 0);
  213. if (enable)
  214. {
  215. /* Enable the Power Switch by driving the enable pin low */
  216. stm32_gpiowrite(GPIO_OTGHS_PWRON, false);
  217. }
  218. else
  219. {
  220. /* Disable the Power Switch by driving the enable pin high */
  221. stm32_gpiowrite(GPIO_OTGHS_PWRON, true);
  222. }
  223. }
  224. #endif
  225. /************************************************************************************
  226. * Name: stm32_setup_overcurrent
  227. *
  228. * Description:
  229. * Setup to receive an interrupt-level callback if an overcurrent condition is
  230. * detected.
  231. *
  232. * Input Parameters:
  233. * handler - New overcurrent interrupt handler
  234. *
  235. * Returned Value:
  236. * Old overcurrent interrupt handler
  237. *
  238. ************************************************************************************/
  239. #ifdef CONFIG_USBHOST
  240. xcpt_t stm32_setup_overcurrent(xcpt_t handler)
  241. {
  242. return stm32_gpiosetevent(GPIO_OTGHS_OVER, true, true, true, handler);
  243. }
  244. #endif
  245. /************************************************************************************
  246. * Name: stm32_usbsuspend
  247. *
  248. * Description:
  249. * Board logic must provide the stm32_usbsuspend logic if the USBDEV driver is
  250. * used. This function is called whenever the USB enters or leaves suspend mode.
  251. * This is an opportunity for the board logic to shutdown clocks, power, etc.
  252. * while the USB is suspended.
  253. *
  254. ************************************************************************************/
  255. #ifdef CONFIG_USBDEV
  256. void stm32_usbsuspend(FAR struct usbdev_s *dev, bool resume)
  257. {
  258. ulldbg("resume: %d\n", resume);
  259. }
  260. #endif
  261. #endif /* CONFIG_STM32_OTGHS */