2019-08-19 17:16:08 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* boards/arm/stm32/stm3220g-eval/src/stm32_usb.c
|
2012-05-28 21:48:26 +02:00
|
|
|
*
|
2021-03-19 12:39:00 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2012-05-28 21:48:26 +02:00
|
|
|
*
|
2021-03-19 12:39:00 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-05-28 21:48:26 +02:00
|
|
|
*
|
2021-03-19 12:39:00 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2012-05-28 21:48:26 +02:00
|
|
|
*
|
2019-08-19 17:16:08 +02:00
|
|
|
****************************************************************************/
|
2012-05-28 21:48:26 +02:00
|
|
|
|
2019-08-19 17:16:08 +02:00
|
|
|
/****************************************************************************
|
2012-05-28 21:48:26 +02:00
|
|
|
* Included Files
|
2019-08-19 17:16:08 +02:00
|
|
|
****************************************************************************/
|
2012-05-28 21:48:26 +02:00
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
2012-08-20 18:06:39 +02:00
|
|
|
#include <sched.h>
|
|
|
|
#include <errno.h>
|
2015-04-28 20:21:23 +02:00
|
|
|
#include <assert.h>
|
2012-05-28 21:48:26 +02:00
|
|
|
#include <debug.h>
|
|
|
|
|
2018-01-31 23:11:54 +01:00
|
|
|
#include <nuttx/kthread.h>
|
2012-05-28 21:48:26 +02:00
|
|
|
#include <nuttx/usb/usbdev.h>
|
2012-08-16 00:13:50 +02:00
|
|
|
#include <nuttx/usb/usbhost.h>
|
2012-05-28 21:48:26 +02:00
|
|
|
#include <nuttx/usb/usbdev_trace.h>
|
|
|
|
|
2022-03-11 17:41:15 +01:00
|
|
|
#include "arm_internal.h"
|
2013-02-09 16:03:49 +01:00
|
|
|
#include "stm32.h"
|
2013-08-12 01:11:32 +02:00
|
|
|
#include "stm32_otgfs.h"
|
2015-12-29 22:44:56 +01:00
|
|
|
#include "stm3220g-eval.h"
|
2012-05-28 21:48:26 +02:00
|
|
|
|
2012-08-15 19:58:54 +02:00
|
|
|
#ifdef CONFIG_STM32_OTGFS
|
|
|
|
|
2019-08-19 17:16:08 +02:00
|
|
|
/****************************************************************************
|
2015-03-21 15:22:00 +01:00
|
|
|
* Pre-processor Definitions
|
2019-08-19 17:16:08 +02:00
|
|
|
****************************************************************************/
|
2012-05-28 21:48:26 +02:00
|
|
|
|
2012-08-20 18:06:39 +02:00
|
|
|
#if defined(CONFIG_USBDEV) || defined(CONFIG_USBHOST)
|
2012-08-15 19:58:54 +02:00
|
|
|
# define HAVE_USB 1
|
|
|
|
#else
|
2012-08-20 18:06:39 +02:00
|
|
|
# warning "CONFIG_STM32_OTGFS is enabled but neither CONFIG_USBDEV nor CONFIG_USBHOST"
|
2012-08-15 19:58:54 +02:00
|
|
|
# undef HAVE_USB
|
|
|
|
#endif
|
|
|
|
|
2012-08-16 00:13:50 +02:00
|
|
|
#ifndef CONFIG_USBHOST_DEFPRIO
|
|
|
|
# define CONFIG_USBHOST_DEFPRIO 50
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_USBHOST_STACKSIZE
|
2015-05-04 00:45:44 +02:00
|
|
|
# ifdef CONFIG_USBHOST_HUB
|
|
|
|
# define CONFIG_USBHOST_STACKSIZE 1536
|
|
|
|
# else
|
|
|
|
# define CONFIG_USBHOST_STACKSIZE 1024
|
|
|
|
# endif
|
2012-08-16 00:13:50 +02:00
|
|
|
#endif
|
|
|
|
|
2019-08-19 17:16:08 +02:00
|
|
|
/****************************************************************************
|
2012-08-16 00:13:50 +02:00
|
|
|
* Private Data
|
2019-08-19 17:16:08 +02:00
|
|
|
****************************************************************************/
|
2012-08-16 00:13:50 +02:00
|
|
|
|
2012-08-20 18:06:39 +02:00
|
|
|
#ifdef CONFIG_USBHOST
|
2013-08-13 23:03:46 +02:00
|
|
|
static struct usbhost_connection_s *g_usbconn;
|
2012-08-16 00:13:50 +02:00
|
|
|
#endif
|
|
|
|
|
2019-08-19 17:16:08 +02:00
|
|
|
/****************************************************************************
|
2012-05-28 21:48:26 +02:00
|
|
|
* Private Functions
|
2019-08-19 17:16:08 +02:00
|
|
|
****************************************************************************/
|
2012-05-28 21:48:26 +02:00
|
|
|
|
2019-08-19 17:16:08 +02:00
|
|
|
/****************************************************************************
|
2012-08-16 00:13:50 +02:00
|
|
|
* Name: usbhost_waiter
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Wait for USB devices to be connected.
|
|
|
|
*
|
2019-08-19 17:16:08 +02:00
|
|
|
****************************************************************************/
|
2012-08-16 00:13:50 +02:00
|
|
|
|
2012-08-20 18:06:39 +02:00
|
|
|
#ifdef CONFIG_USBHOST
|
2012-08-16 00:13:50 +02:00
|
|
|
static int usbhost_waiter(int argc, char *argv[])
|
|
|
|
{
|
2015-04-28 20:21:23 +02:00
|
|
|
struct usbhost_hubport_s *hport;
|
2012-08-16 00:13:50 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Running\n");
|
2020-07-02 06:26:09 +02:00
|
|
|
for (; ; )
|
2012-08-16 00:13:50 +02:00
|
|
|
{
|
|
|
|
/* Wait for the device to change state */
|
|
|
|
|
2015-04-28 20:21:23 +02:00
|
|
|
DEBUGVERIFY(CONN_WAIT(g_usbconn, &hport));
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("%s\n", hport->connected ? "connected" : "disconnected");
|
2012-08-16 00:13:50 +02:00
|
|
|
|
|
|
|
/* Did we just become connected? */
|
|
|
|
|
2015-04-28 20:21:23 +02:00
|
|
|
if (hport->connected)
|
2012-08-16 00:13:50 +02:00
|
|
|
{
|
|
|
|
/* Yes.. enumerate the newly connected device */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
CONN_ENUMERATE(g_usbconn, hport);
|
2012-08-16 00:13:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Keep the compiler from complaining */
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-08-19 17:16:08 +02:00
|
|
|
/****************************************************************************
|
2012-05-28 21:48:26 +02:00
|
|
|
* Public Functions
|
2019-08-19 17:16:08 +02:00
|
|
|
****************************************************************************/
|
2012-05-28 21:48:26 +02:00
|
|
|
|
2019-08-19 17:16:08 +02:00
|
|
|
/****************************************************************************
|
2012-05-28 21:48:26 +02:00
|
|
|
* Name: stm32_usbinitialize
|
|
|
|
*
|
|
|
|
* Description:
|
2020-07-02 06:26:09 +02:00
|
|
|
* Called from stm32_usbinitialize very early in inialization to setup
|
|
|
|
* USB-related GPIO pins for the STM3220G-EVAL board.
|
2012-05-28 21:48:26 +02:00
|
|
|
*
|
2019-08-19 17:16:08 +02:00
|
|
|
****************************************************************************/
|
2012-05-28 21:48:26 +02:00
|
|
|
|
|
|
|
void stm32_usbinitialize(void)
|
|
|
|
{
|
2012-08-15 19:58:54 +02:00
|
|
|
#ifdef HAVE_USB
|
2020-07-02 06:26:09 +02:00
|
|
|
/* The OTG FS has an internal soft pull-up.
|
|
|
|
* No GPIO configuration is required
|
|
|
|
*/
|
2012-05-28 21:48:26 +02:00
|
|
|
|
2020-07-02 06:26:09 +02:00
|
|
|
/* Configure the OTG FS VBUS sensing GPIO,
|
|
|
|
* Power On, and Overcurrent GPIOs
|
|
|
|
*/
|
2012-05-28 21:48:26 +02:00
|
|
|
|
|
|
|
stm32_configgpio(GPIO_OTGFS_VBUS);
|
|
|
|
stm32_configgpio(GPIO_OTGFS_PWRON);
|
|
|
|
stm32_configgpio(GPIO_OTGFS_OVER);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-08-19 17:16:08 +02:00
|
|
|
/****************************************************************************
|
2012-08-15 19:58:54 +02:00
|
|
|
* Name: stm32_usbhost_initialize
|
|
|
|
*
|
|
|
|
* Description:
|
2020-07-02 06:26:09 +02:00
|
|
|
* Called at application startup time to initialize the USB host
|
|
|
|
* functionality.
|
2012-08-15 19:58:54 +02:00
|
|
|
* This function will start a thread that will monitor for device
|
|
|
|
* connection/disconnection events.
|
|
|
|
*
|
2019-08-19 17:16:08 +02:00
|
|
|
****************************************************************************/
|
2012-08-15 19:58:54 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_USBHOST
|
2012-08-16 00:13:50 +02:00
|
|
|
int stm32_usbhost_initialize(void)
|
2012-08-15 19:58:54 +02:00
|
|
|
{
|
2012-08-16 00:13:50 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* First, register all of the class drivers needed to support the drivers
|
|
|
|
* that we care about:
|
|
|
|
*/
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Register class drivers\n");
|
2015-05-06 22:11:29 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_USBHOST_MSC
|
|
|
|
/* Register the USB mass storage class class */
|
|
|
|
|
|
|
|
ret = usbhost_msc_initialize();
|
2012-08-16 00:13:50 +02:00
|
|
|
if (ret != OK)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
uerr("ERROR: Failed to register the mass storage class: %d\n", ret);
|
2012-08-16 00:13:50 +02:00
|
|
|
}
|
2015-05-06 22:11:29 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_USBHOST_CDCACM
|
|
|
|
/* Register the CDC/ACM serial class */
|
|
|
|
|
|
|
|
ret = usbhost_cdcacm_initialize();
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
uerr("ERROR: Failed to register the CDC/ACM serial class: %d\n", ret);
|
2015-05-06 22:11:29 +02:00
|
|
|
}
|
|
|
|
#endif
|
2012-08-16 00:13:50 +02:00
|
|
|
|
|
|
|
/* Then get an instance of the USB host interface */
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Initialize USB host\n");
|
2013-08-13 23:03:46 +02:00
|
|
|
g_usbconn = stm32_otgfshost_initialize(0);
|
|
|
|
if (g_usbconn)
|
2012-08-16 00:13:50 +02:00
|
|
|
{
|
|
|
|
/* Start a thread to handle device connection. */
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Start usbhost_waiter\n");
|
2012-08-16 00:13:50 +02:00
|
|
|
|
2022-03-21 23:47:09 +01:00
|
|
|
ret = kthread_create("usbhost", CONFIG_USBHOST_DEFPRIO,
|
2018-01-31 23:11:54 +01:00
|
|
|
CONFIG_USBHOST_STACKSIZE,
|
|
|
|
(main_t)usbhost_waiter, (FAR char * const *)NULL);
|
2022-03-21 23:47:09 +01:00
|
|
|
return ret < 0 ? -ENOEXEC : OK;
|
2012-08-16 00:13:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return -ENODEV;
|
2012-08-15 19:58:54 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-08-19 17:16:08 +02:00
|
|
|
/****************************************************************************
|
2012-08-15 19:58:54 +02:00
|
|
|
* Name: stm32_usbhost_vbusdrive
|
|
|
|
*
|
|
|
|
* Description:
|
2020-07-02 06:26:09 +02:00
|
|
|
* Enable/disable driving of VBUS 5V output. This function must be
|
|
|
|
* provided be each platform that implements the STM32 OTG FS host
|
|
|
|
* interface
|
2012-08-15 19:58:54 +02:00
|
|
|
*
|
2020-07-02 06:26:09 +02:00
|
|
|
* "On-chip 5 V VBUS generation is not supported. For this reason, a
|
|
|
|
* charge pump or, if 5 V are available on the application board, a
|
|
|
|
* basic power switch, must be added externally to drive the 5 V VBUS
|
|
|
|
* line. The external charge pump can be driven by any GPIO output.
|
|
|
|
* When the application decides to power on VBUS using the chosen GPIO,
|
|
|
|
* it must also set the port power bit in the host port control and
|
|
|
|
* status register (PPWR bit in OTG_FS_HPRT).
|
2012-08-15 19:58:54 +02:00
|
|
|
*
|
2020-07-02 06:26:09 +02:00
|
|
|
* "The application uses this field to control power to this port,
|
|
|
|
* and the core clears this bit on an overcurrent condition."
|
2012-08-15 19:58:54 +02:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
2020-07-02 06:26:09 +02:00
|
|
|
* iface - For future growth to handle multiple USB host interface.
|
|
|
|
* Should be zero.
|
2012-08-15 19:58:54 +02:00
|
|
|
* enable - true: enable VBUS power; false: disable VBUS power
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
2019-08-19 17:16:08 +02:00
|
|
|
****************************************************************************/
|
2012-08-15 19:58:54 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_USBHOST
|
|
|
|
void stm32_usbhost_vbusdrive(int iface, bool enable)
|
|
|
|
{
|
|
|
|
DEBUGASSERT(iface == 0);
|
2014-04-13 21:18:06 +02:00
|
|
|
|
2012-08-15 19:58:54 +02:00
|
|
|
if (enable)
|
|
|
|
{
|
|
|
|
/* Enable the Power Switch by driving the enable pin low */
|
|
|
|
|
|
|
|
stm32_gpiowrite(GPIO_OTGFS_PWRON, false);
|
|
|
|
}
|
|
|
|
else
|
2014-04-13 21:18:06 +02:00
|
|
|
{
|
2012-08-15 19:58:54 +02:00
|
|
|
/* Disable the Power Switch by driving the enable pin high */
|
2014-04-13 21:18:06 +02:00
|
|
|
|
2012-08-15 19:58:54 +02:00
|
|
|
stm32_gpiowrite(GPIO_OTGFS_PWRON, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-08-19 17:16:08 +02:00
|
|
|
/****************************************************************************
|
2012-08-15 19:58:54 +02:00
|
|
|
* Name: stm32_setup_overcurrent
|
|
|
|
*
|
|
|
|
* Description:
|
2020-07-02 06:26:09 +02:00
|
|
|
* Setup to receive an interrupt-level callback if an overcurrent
|
|
|
|
* condition is detected.
|
2012-08-15 19:58:54 +02:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Input Parameters:
|
2012-08-15 19:58:54 +02:00
|
|
|
* handler - New overcurrent interrupt handler
|
2017-03-03 01:20:38 +01:00
|
|
|
* arg - The argument provided for the interrupt handler
|
2012-08-15 19:58:54 +02:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2020-07-02 06:26:09 +02:00
|
|
|
* Zero (OK) is returned on success. Otherwise, a negated errno value
|
|
|
|
* is returned to indicate the nature of the failure.
|
2012-08-15 19:58:54 +02:00
|
|
|
*
|
2019-08-19 17:16:08 +02:00
|
|
|
****************************************************************************/
|
2012-08-15 19:58:54 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_USBHOST
|
2017-03-03 01:20:38 +01:00
|
|
|
int stm32_setup_overcurrent(xcpt_t handler, void *arg)
|
2012-08-15 19:58:54 +02:00
|
|
|
{
|
2017-03-03 01:20:38 +01:00
|
|
|
return stm32_gpiosetevent(GPIO_OTGFS_OVER, true, true, true, handler, arg);
|
2012-08-15 19:58:54 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-08-19 17:16:08 +02:00
|
|
|
/****************************************************************************
|
2012-05-28 21:48:26 +02:00
|
|
|
* Name: stm32_usbsuspend
|
|
|
|
*
|
|
|
|
* Description:
|
2020-07-02 06:26:09 +02:00
|
|
|
* Board logic must provide the stm32_usbsuspend logic if the USBDEV
|
|
|
|
* driver is used. This function is called whenever the USB enters or
|
|
|
|
* leaves suspend mode. This is an opportunity for the board logic to
|
|
|
|
* shutdown clocks, power, etc. while the USB is suspended.
|
2012-05-28 21:48:26 +02:00
|
|
|
*
|
2019-08-19 17:16:08 +02:00
|
|
|
****************************************************************************/
|
2012-05-28 21:48:26 +02:00
|
|
|
|
2012-08-15 19:58:54 +02:00
|
|
|
#ifdef CONFIG_USBDEV
|
2012-05-28 21:48:26 +02:00
|
|
|
void stm32_usbsuspend(FAR struct usbdev_s *dev, bool resume)
|
|
|
|
{
|
2016-06-20 19:59:15 +02:00
|
|
|
uinfo("resume: %d\n", resume);
|
2012-05-28 21:48:26 +02:00
|
|
|
}
|
2012-08-15 19:58:54 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* CONFIG_STM32_OTGFS */
|