From 4295b92ef9725acceb655bc551643c91773a00c6 Mon Sep 17 00:00:00 2001 From: Nicolas Schodet Date: Thu, 14 Jan 2016 14:06:02 +0100 Subject: ucoo/hal/usb: add enable and disable functions --- ucoo/hal/usb/test/test_usb.cc | 1 + ucoo/hal/usb/usb.stm32.cc | 75 +++++++++++++++++++++++++++++++++---------- ucoo/hal/usb/usb.stm32.hh | 8 +++++ 3 files changed, 67 insertions(+), 17 deletions(-) (limited to 'ucoo/hal/usb') diff --git a/ucoo/hal/usb/test/test_usb.cc b/ucoo/hal/usb/test/test_usb.cc index 914e084..eb2eb12 100644 --- a/ucoo/hal/usb/test/test_usb.cc +++ b/ucoo/hal/usb/test/test_usb.cc @@ -29,6 +29,7 @@ main (int argc, const char **argv) { ucoo::arch_init (argc, argv); ucoo::UsbStreamControl usc ("APBTeam", "USB test"); + usc.enable (); ucoo::UsbStream us[] = { ucoo::UsbStream (usc, 0), #if CONFIG_UCOO_HAL_USB_STREAM_NB >= 2 diff --git a/ucoo/hal/usb/usb.stm32.cc b/ucoo/hal/usb/usb.stm32.cc index 1d88961..e37d25b 100644 --- a/ucoo/hal/usb/usb.stm32.cc +++ b/ucoo/hal/usb/usb.stm32.cc @@ -120,37 +120,78 @@ UsbStreamControl::RxBuffer::RxBuffer (void) } UsbStreamControl::UsbStreamControl (const char *vendor, const char *product) - : configured_ (false) + : enabled_ (false), configured_ (false) { assert (!instance_); instance_ = this; strings[0] = vendor; strings[1] = product; +} + +UsbStreamControl::~UsbStreamControl () +{ + disable (); +} + +void +UsbStreamControl::enable () +{ + if (!enabled_) + { #if defined (TARGET_stm32f4) # if CONFIG_UCOO_HAL_USB_DRIVER_HS - rcc_periph_clock_enable (RCC_OTGHS); - rcc_periph_clock_enable (RCC_GPIOB); - gpio_mode_setup (GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO14 | GPIO15); - gpio_set_af (GPIOB, GPIO_AF12, GPIO14 | GPIO15); + rcc_periph_clock_enable (RCC_OTGHS); + rcc_periph_clock_enable (RCC_GPIOB); + gpio_mode_setup (GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO14 | GPIO15); + gpio_set_af (GPIOB, GPIO_AF12, GPIO14 | GPIO15); # else - rcc_periph_clock_enable (RCC_OTGFS); - rcc_periph_clock_enable (RCC_GPIOA); - gpio_mode_setup (GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO11 | GPIO12); - gpio_set_af (GPIOA, GPIO_AF10, GPIO11 | GPIO12); + rcc_periph_clock_enable (RCC_OTGFS); + rcc_periph_clock_enable (RCC_GPIOA); + gpio_mode_setup (GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO11 | GPIO12); + gpio_set_af (GPIOA, GPIO_AF10, GPIO11 | GPIO12); # endif #elif defined (TARGET_stm32f1) - rcc_periph_clock_enable (RCC_OTGFS); - rcc_periph_clock_enable (RCC_GPIOA); + rcc_periph_clock_enable (RCC_OTGFS); + rcc_periph_clock_enable (RCC_GPIOA); +#endif + usbdev = usbd_init (&usb_driver, &usb_desc_dev, &usb_desc_config, + strings, lengthof (strings), + usb_control_buffer, sizeof (usb_control_buffer)); + usbd_register_set_config_callback (usbdev, set_config); +#if CONFIG_UCOO_HAL_USB_DRIVER_HS + nvic_enable_irq (NVIC_OTG_HS_IRQ); +#else + nvic_enable_irq (NVIC_OTG_FS_IRQ); #endif - usbdev = usbd_init (&usb_driver, &usb_desc_dev, &usb_desc_config, - strings, lengthof (strings), - usb_control_buffer, sizeof (usb_control_buffer)); - usbd_register_set_config_callback (usbdev, set_config); + enabled_ = true; + } +} + +void +UsbStreamControl::disable () +{ + if (enabled_) + { + enabled_ = false; + configured_ = false; #if CONFIG_UCOO_HAL_USB_DRIVER_HS - nvic_enable_irq (NVIC_OTG_HS_IRQ); + nvic_disable_irq (NVIC_OTG_HS_IRQ); #else - nvic_enable_irq (NVIC_OTG_FS_IRQ); + nvic_disable_irq (NVIC_OTG_FS_IRQ); +#endif + usbd_disconnect (usbdev, true); +#if defined (TARGET_stm32f4) +# if CONFIG_UCOO_HAL_USB_DRIVER_HS + rcc_periph_clock_disable (RCC_OTGHS); + gpio_mode_setup (GPIOB, GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO14 | GPIO15); +# else + rcc_periph_clock_disable (RCC_OTGFS); + gpio_mode_setup (GPIOA, GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO11 | GPIO12); +# endif +#elif defined (TARGET_stm32f1) + rcc_periph_clock_disable (RCC_OTGFS); #endif + } } void diff --git a/ucoo/hal/usb/usb.stm32.hh b/ucoo/hal/usb/usb.stm32.hh index 11ce16c..5c44e2f 100644 --- a/ucoo/hal/usb/usb.stm32.hh +++ b/ucoo/hal/usb/usb.stm32.hh @@ -43,6 +43,12 @@ class UsbStreamControl /// Construct a new USB control object, with given VENDOR and PRODUCT /// strings. UsbStreamControl (const char *vendor, const char *product); + /// Destruct and disable. + ~UsbStreamControl (); + /// Enable device. + void enable (); + /// Disable device. + void disable (); /// Return true if the device is configured. This means that the /// connection is done with the host and data can be exchanged. bool is_configured () const { return configured_; } @@ -59,6 +65,8 @@ class UsbStreamControl static const int stream_nb_ = CONFIG_UCOO_HAL_USB_STREAM_NB; /// Pointer to the one and only instance. static UsbStreamControl *instance_; + /// Is currently enabled? + bool enabled_; /// Whether device is currently configured. bool configured_; /// Internal RX buffer type. -- cgit v1.2.3