summaryrefslogtreecommitdiff
path: root/ucoo
diff options
context:
space:
mode:
authorNicolas Schodet2016-01-14 14:06:02 +0100
committerNicolas Schodet2019-10-07 00:44:57 +0200
commit4295b92ef9725acceb655bc551643c91773a00c6 (patch)
tree980534bb677aaedbbfb5e2010c16c5577f161db3 /ucoo
parente6c2060483a75dbfd266ab13571af721e9cdd2fa (diff)
ucoo/hal/usb: add enable and disable functions
Diffstat (limited to 'ucoo')
-rw-r--r--ucoo/base/test/test.stm32.cc1
-rw-r--r--ucoo/hal/usb/test/test_usb.cc1
-rw-r--r--ucoo/hal/usb/usb.stm32.cc75
-rw-r--r--ucoo/hal/usb/usb.stm32.hh8
4 files changed, 68 insertions, 17 deletions
diff --git a/ucoo/base/test/test.stm32.cc b/ucoo/base/test/test.stm32.cc
index 912d49c..f8a6648 100644
--- a/ucoo/base/test/test.stm32.cc
+++ b/ucoo/base/test/test.stm32.cc
@@ -40,6 +40,7 @@ test_stream (void)
#if CONFIG_UCOO_BASE_TEST_TEST_STREAM_UART == -1
static UsbStreamControl usc ("APBTeam", "test");
static UsbStream us (usc, 0);
+ usc.enable ();
return us;
#else
static Uart u (CONFIG_UCOO_BASE_TEST_TEST_STREAM_UART);
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.