diff --git a/configs/stm32butterfly2/include/board.h b/configs/stm32butterfly2/include/board.h index bb12930e197..761c0cf6a9d 100644 --- a/configs/stm32butterfly2/include/board.h +++ b/configs/stm32butterfly2/include/board.h @@ -81,6 +81,10 @@ #define STM32_HCLK_FREQUENCY STM32_PLL_FREQUENCY #define STM32_BOARD_HCLK STM32_HCLK_FREQUENCY +/* USB clock output is 47.9232MHz */ + +#define STM32_CFGR_OTGFSPRE RCC_CFGR_OTGFSPREd3 + /* APB2 clock (PCLK2) is HCLK */ #define STM32_RCC_CFGR_PPRE2 RCC_CFGR_PPRE2_HCLK diff --git a/configs/stm32butterfly2/nshnet/defconfig b/configs/stm32butterfly2/nshnet/defconfig index 826534dfc7d..c1ef03b861d 100644 --- a/configs/stm32butterfly2/nshnet/defconfig +++ b/configs/stm32butterfly2/nshnet/defconfig @@ -148,6 +148,9 @@ CONFIG_ARMV7M_HAVE_STACKCHECK=y # CONFIG_ARMV7M_STACKCHECK is not set # CONFIG_ARMV7M_ITMSYSLOG is not set # CONFIG_SERIAL_TERMIOS is not set +# CONFIG_USBHOST_BULK_DISABLE is not set +# CONFIG_USBHOST_INT_DISABLE is not set +# CONFIG_USBHOST_ISOC_DISABLE is not set # # STM32 Configuration Options @@ -377,11 +380,12 @@ CONFIG_STM32_ADC1=y # CONFIG_STM32_DAC2 is not set CONFIG_STM32_ETHMAC=y # CONFIG_STM32_I2C1 is not set -# CONFIG_STM32_OTGFS is not set +CONFIG_STM32_OTGFS=y CONFIG_STM32_PWR=y CONFIG_STM32_SPI1=y # CONFIG_STM32_SPI2 is not set # CONFIG_STM32_SPI3 is not set +CONFIG_STM32_SYSCFG=y # CONFIG_STM32_TIM1 is not set # CONFIG_STM32_TIM2 is not set # CONFIG_STM32_TIM3 is not set @@ -474,6 +478,11 @@ CONFIG_STM32_ETH100MBPS=y # # USB FS Host Configuration # +CONFIG_STM32_OTGFS_RXFIFO_SIZE=128 +CONFIG_STM32_OTGFS_NPTXFIFO_SIZE=96 +CONFIG_STM32_OTGFS_PTXFIFO_SIZE=128 +CONFIG_STM32_OTGFS_DESCSIZE=128 +# CONFIG_STM32_OTGFS_SOFINTR is not set # # USB HS Host Configuration @@ -865,7 +874,25 @@ CONFIG_USART2_2STOP=0 # CONFIG_USART2_DMA is not set # CONFIG_PSEUDOTERM is not set # CONFIG_USBDEV is not set -# CONFIG_USBHOST is not set +CONFIG_USBHOST=y +CONFIG_USBHOST_NPREALLOC=4 +CONFIG_USBHOST_HAVE_ASYNCH=y +# CONFIG_USBHOST_ASYNCH is not set +# CONFIG_USBHOST_HUB is not set +CONFIG_USBHOST_MSC=y +# CONFIG_USBHOST_CDCACM is not set +CONFIG_USBHOST_HIDKBD=y +CONFIG_HIDKBD_POLLUSEC=100000 +CONFIG_HIDKBD_DEFPRIO=50 +CONFIG_HIDKBD_STACKSIZE=1024 +CONFIG_HIDKBD_BUFSIZE=64 +CONFIG_HIDKBD_NPOLLWAITERS=2 +# CONFIG_HIDKBD_RAWSCANCODES is not set +# CONFIG_HIDKBD_ALLSCANCODES is not set +# CONFIG_HIDKBD_NODEBOUNCE is not set +# CONFIG_USBHOST_HIDMOUSE is not set +# CONFIG_USBHOST_RTL8187 is not set +# CONFIG_USBHOST_TRACE is not set # CONFIG_HAVE_USBTRACE is not set # CONFIG_DRIVERS_WIRELESS is not set @@ -1162,7 +1189,10 @@ CONFIG_EXAMPLES_BUTTONS_MAX=4 # CONFIG_EXAMPLES_FTPC is not set # CONFIG_EXAMPLES_FTPD is not set # CONFIG_EXAMPLES_HELLO is not set -# CONFIG_EXAMPLES_HIDKBD is not set +CONFIG_EXAMPLES_HIDKBD=y +CONFIG_EXAMPLES_HIDKBD_DEFPRIO=50 +CONFIG_EXAMPLES_HIDKBD_STACKSIZE=1024 +CONFIG_EXAMPLES_HIDKBD_DEVNAME="/dev/kbda" # CONFIG_EXAMPLES_IGMP is not set # CONFIG_EXAMPLES_JSON is not set # CONFIG_EXAMPLES_KEYPADTEST is not set @@ -1356,6 +1386,7 @@ CONFIG_NSH_STRERROR=y # CONFIG_NSH_CONSOLE=y # CONFIG_NSH_ALTCONDEV is not set +# CONFIG_NSH_USBKBD is not set CONFIG_NSH_ARCHINIT=y # diff --git a/configs/stm32butterfly2/src/Makefile b/configs/stm32butterfly2/src/Makefile index ad5f6f882d0..35fbfcd375a 100644 --- a/configs/stm32butterfly2/src/Makefile +++ b/configs/stm32butterfly2/src/Makefile @@ -45,6 +45,14 @@ ifeq ($(CONFIG_STM32_SPI1),y) CSRCS += stm32_spi.c endif +ifeq ($(CONFIG_STM32_OTGFS),y) +CSRCS += stm32_usb.c +endif + +ifeq ($(CONFIG_USBHOST),y) +CSRCS += stm32_usbhost.c +endif + ifeq ($(CONFIG_MMCSD),y) CSRCS += stm32_mmcsd.c endif diff --git a/configs/stm32butterfly2/src/stm32_boot.c b/configs/stm32butterfly2/src/stm32_boot.c index 5f5ea750b3a..9bba1b7917e 100644 --- a/configs/stm32butterfly2/src/stm32_boot.c +++ b/configs/stm32butterfly2/src/stm32_boot.c @@ -52,6 +52,7 @@ void stm32_boardinitialize(void) { stm32_led_initialize(); stm32_spidev_initialize(); + stm32_usb_initialize(); } int board_app_initialize(uintptr_t arg) @@ -63,5 +64,11 @@ int board_app_initialize(uintptr_t arg) return rv; } - return 0; + if ((rv = stm32_usbhost_initialize()) < 0) + { + syslog(LOG_ERR, "ERROR: Failed to initialize USB host: %d\n", rv); + return rv; + } + + return 0; } diff --git a/configs/stm32butterfly2/src/stm32_butterfly2.h b/configs/stm32butterfly2/src/stm32_butterfly2.h index dc0c46fbb01..849aed137a2 100644 --- a/configs/stm32butterfly2/src/stm32_butterfly2.h +++ b/configs/stm32butterfly2/src/stm32_butterfly2.h @@ -43,11 +43,18 @@ * Pre-processor Definitions ****************************************************************************/ +/* SD Card pins */ + #define GPIO_SD_CS (GPIO_OUTPUT | GPIO_CNF_OUTPP | GPIO_MODE_50MHz |\ GPIO_OUTPUT_SET | GPIO_PORTA | GPIO_PIN4) #define GPIO_SD_CD (GPIO_INPUT | GPIO_CNF_INFLOAT | GPIO_EXTI |\ GPIO_PORTB | GPIO_PIN9) +/* USB pins */ + +#define GPIO_OTGFS_PWRON (GPIO_OUTPUT | GPIO_CNF_OUTPP | GPIO_MODE_50MHz |\ + GPIO_OUTPUT_SET | GPIO_PORTD | GPIO_PIN15) + /***************************************************************************** * Public Functions ****************************************************************************/ @@ -56,11 +63,11 @@ * Name: stm32_spidev_initialize * * Description: - * Called to configure SPI chip select GPIO pins. + * Called to configure SPI chip select GPIO pins. * * Note: - * Here only CS pins are configured as SPI pins are configured by driver - * itself. + * Here only CS pins are configured as SPI pins are configured by driver + * itself. ****************************************************************************/ void stm32_spidev_initialize(void); @@ -69,9 +76,35 @@ void stm32_spidev_initialize(void); * Name: stm32_sdinitialize * * Description: - * Initializes SPI-based SD card + * Initializes SPI-based SD card * ****************************************************************************/ int stm32_sdinitialize(int minor); +/***************************************************************************** + * Name: stm32_usb_initialize + * + * Description: + * Initializes USB pins + ****************************************************************************/ + +#ifdef CONFIG_STM32_OTGFS +void stm32_usb_initialize(void); +#else +static inline void stm32_usb_initialize(void) {} +#endif + +/***************************************************************************** + * Name: stm32_usbhost_initialize + * + * Description: + * Initializes USB host functionality. + ****************************************************************************/ + +#ifdef CONFIG_USBHOST +int stm32_usbhost_initialize(void); +#else +static inline int stm32_usbhost_initialize(void) {} +#endif + diff --git a/configs/stm32butterfly2/src/stm32_usb.c b/configs/stm32butterfly2/src/stm32_usb.c new file mode 100644 index 00000000000..b9185f58874 --- /dev/null +++ b/configs/stm32butterfly2/src/stm32_usb.c @@ -0,0 +1,61 @@ +/***************************************************************************** + * configs/stm32butterfly2/src/stm32_usb.c + * + * Copyright (C) 2016 Michał Łyszczek. All rights reserved. + * Author: Michał Łyszczek + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/***************************************************************************** + * Include Files + ****************************************************************************/ + +#include +#include + +#include "stm32_butterfly2.h" + +/***************************************************************************** + * Public Functions + ****************************************************************************/ + +/***************************************************************************** + * Name: stm32_usb_initialize + * + * Description: + * Initializes USB pins + ****************************************************************************/ + +void stm32_usb_initialize(void) +{ + stm32_configgpio(GPIO_OTGFS_VBUS); + stm32_configgpio(GPIO_OTGFS_PWRON); +} + diff --git a/configs/stm32butterfly2/src/stm32_usbhost.c b/configs/stm32butterfly2/src/stm32_usbhost.c new file mode 100644 index 00000000000..aff168ac915 --- /dev/null +++ b/configs/stm32butterfly2/src/stm32_usbhost.c @@ -0,0 +1,170 @@ +/***************************************************************************** + * configs/stm32butterfly2/src/stm32_usb.c + * + * Copyright (C) 2016 Michał Łyszczek. All rights reserved. + * Author: Michał Łyszczek + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/***************************************************************************** + * Include Files + ****************************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "stm32.h" +#include "stm32_butterfly2.h" +#include "stm32_otgfs.h" + +/***************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#ifndef CONFIG_STM32_OTGFS +#error "CONFIG_USBHOST requires CONFIG_STM32_OTGFS to be enabled" +#endif + +/***************************************************************************** + * Private Data + ****************************************************************************/ + +static struct usbhost_connection_s *g_usbconn; + +/***************************************************************************** + * Private Functions + ****************************************************************************/ + +/***************************************************************************** + * Name: usbhost_detect + * + * Description: + * Wait for USB devices to be connected. + ****************************************************************************/ + +static void* usbhost_detect(void *arg) +{ + (void)arg; + + struct usbhost_hubport_s *hport; + + for (;;) + { + CONN_WAIT(g_usbconn, &hport); + + if (hport->connected) + { + CONN_ENUMERATE(g_usbconn, hport); + } + } + + return 0; +} + +/***************************************************************************** + * Public Functions + ****************************************************************************/ + +/***************************************************************************** + * Name: stm32_usbhost_initialize + * + * Description: + * Initializes USB host functionality. + ****************************************************************************/ + +int stm32_usbhost_initialize(void) +{ + int rv; + +#ifdef CONFIG_USBHOST_MSC + if ((rv = usbhost_msc_initialize()) < 0) + { + uerr("ERROR: Failed to register mass storage class: %d\n", rv); + } +#endif + +#ifdef CONFIG_USBHOST_CDACM + if ((rv = usbhost_cdacm_initialize()) < 0) + { + uerr("ERROR: Failed to register CDC/ACM serial class: %d\n", rv); + } +#endif + +#ifdef CONFIG_USBHOST_HIDKBD + if ((rv = usbhost_kbdinit()) < 0) + { + uerr("ERROR: Failed to register the KBD class\n"); + } +#endif + + + if ((g_usbconn = stm32_otgfshost_initialize(0))) + { + pthread_attr_t pattr; + pthread_attr_init(&pattr); + pthread_attr_setstacksize(&pattr, 2048); + return pthread_create(NULL, &pattr, usbhost_detect, NULL); + } + + return -ENODEV; +} + +/***************************************************************************** + * Name: stm32_usbhost_vbusdrive + * + * Description: + * Enable/disable driving of VBUS 5V output. + * + * The application uses this field to control power to this port, and the + * core clears this bit on an overcurrent condition. + * + * Input Parameters: + * iface - For future growth to handle multiple USB host interface. + * Should be zero. + * enable - true: enable VBUS power; false: disable VBUS power + * + * Returned Value: + * None + ****************************************************************************/ + +void stm32_usbhost_vbusdrive(int iface, bool enable) +{ + DEBUGASSERT(iface == 0); + + stm32_gpiowrite(GPIO_OTGFS_PWRON, enable); +} +