Skip to content

Commit 2c0d86c

Browse files
Support boards without VBUS pin again
Since commit bdd33df (Refresh USB logic; callback for events; hotplug in STOP mode; STOP mode in USB SUSPEND; bunch of race conditions in the USB/CDC code; avoid SOF interrupt; 500mA max power in descriptiors), the USBD code would assume a VBUS pin is available and only initialize the USB stack when a VBUS voltage is present. This commit restores the older behavior of, when `STM32L0_CONFIG_PIN_VBUS` (so indirectly also `usbd_pin_vbus`) is `STM32L0_GPIO_PIN_NONE`, assuming VBUS is always present. USB initialization can still be controlled using `USBDevice.attach()`.
1 parent b1cf1cd commit 2c0d86c

File tree

4 files changed

+37
-26
lines changed

4 files changed

+37
-26
lines changed

system/STM32L0xx/Lib/libstm32l052xx.a

2.91 KB
Binary file not shown.

system/STM32L0xx/Lib/libstm32l072xx.a

2.91 KB
Binary file not shown.

system/STM32L0xx/Lib/libstm32l082xx.a

2.91 KB
Binary file not shown.

system/STM32L0xx/Source/USB/usbd_conf.c

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,6 @@ bool USBD_Initialize(uint16_t vid, uint16_t pid, const uint8_t *manufacturer, co
198198
USBD_ProductString = product;
199199
USBD_ClassInitialize = initialize;
200200

201-
usbd_pin_vbus = pin_vbus;
202-
usbd_mask_vbus = 1 << ((pin_vbus & STM32L0_GPIO_PIN_INDEX_MASK) >> STM32L0_GPIO_PIN_INDEX_SHIFT);
203-
204201
/* Set USB Interrupt priority */
205202
NVIC_SetPriority(USB_IRQn, priority);
206203

@@ -209,19 +206,29 @@ bool USBD_Initialize(uint16_t vid, uint16_t pid, const uint8_t *manufacturer, co
209206
usbd_suspend_callback = suspend_callback;
210207
usbd_resume_callback = resume_callback;
211208

212-
stm32l0_lptim_timeout_create(&USBD_VBUSTimeout);
213-
214209
/* Configure USB FS GPIOs */
215-
stm32l0_gpio_pin_configure(usbd_pin_vbus, (STM32L0_GPIO_PARK_HIZ | STM32L0_GPIO_PUPD_PULLDOWN | STM32L0_GPIO_OSPEED_LOW | STM32L0_GPIO_OTYPE_PUSHPULL | STM32L0_GPIO_MODE_INPUT));
210+
usbd_pin_vbus = pin_vbus;
211+
if (pin_vbus != STM32L0_GPIO_PIN_NONE) {
212+
usbd_mask_vbus = 1 << ((pin_vbus & STM32L0_GPIO_PIN_INDEX_MASK) >> STM32L0_GPIO_PIN_INDEX_SHIFT);
216213

217-
if (stm32l0_gpio_pin_read(usbd_pin_vbus))
218-
{
219-
stm32l0_lptim_timeout_start(&USBD_VBUSTimeout, stm32l0_lptim_millis_to_ticks(40), (stm32l0_lptim_callback_t)USBD_VBUSTimeoutIrq); /* 40ms */
214+
stm32l0_lptim_timeout_create(&USBD_VBUSTimeout);
215+
216+
stm32l0_gpio_pin_configure(usbd_pin_vbus, (STM32L0_GPIO_PARK_HIZ | STM32L0_GPIO_PUPD_PULLDOWN | STM32L0_GPIO_OSPEED_LOW | STM32L0_GPIO_OTYPE_PUSHPULL | STM32L0_GPIO_MODE_INPUT));
217+
218+
if (stm32l0_gpio_pin_read(usbd_pin_vbus))
219+
{
220+
stm32l0_lptim_timeout_start(&USBD_VBUSTimeout, stm32l0_lptim_millis_to_ticks(40), (stm32l0_lptim_callback_t)USBD_VBUSTimeoutIrq); /* 40ms */
221+
}
222+
223+
stm32l0_exti_attach(usbd_pin_vbus,
224+
(STM32L0_EXTI_CONTROL_NOWAKEUP | STM32L0_EXTI_CONTROL_PRIORITY_LOW | STM32L0_EXTI_CONTROL_EDGE_RISING | STM32L0_EXTI_CONTROL_EDGE_FALLING),
225+
(stm32l0_exti_callback_t)USBD_VBUSChangedIrq, NULL);
226+
} else {
227+
// No VBUS pin, assume VBUS is always present
228+
usbd_attached = true;
229+
usbd_mask_vbus = 0;
220230
}
221-
222-
stm32l0_exti_attach(usbd_pin_vbus,
223-
(STM32L0_EXTI_CONTROL_NOWAKEUP | STM32L0_EXTI_CONTROL_PRIORITY_LOW | STM32L0_EXTI_CONTROL_EDGE_RISING | STM32L0_EXTI_CONTROL_EDGE_FALLING),
224-
(stm32l0_exti_callback_t)USBD_VBUSChangedIrq, NULL);
231+
225232

226233
return true;
227234
}
@@ -230,11 +237,13 @@ void USBD_Teardown()
230237
{
231238
USBD_Detach();
232239

233-
stm32l0_exti_detach(usbd_pin_vbus);
240+
if (usbd_pin_vbus != STM32L0_GPIO_PIN_NONE) {
241+
stm32l0_exti_detach(usbd_pin_vbus);
234242

235-
stm32l0_lptim_timeout_stop(&USBD_VBUSTimeout);
243+
stm32l0_lptim_timeout_stop(&USBD_VBUSTimeout);
236244

237-
stm32l0_gpio_pin_configure(usbd_pin_vbus, STM32L0_GPIO_MODE_ANALOG);
245+
stm32l0_gpio_pin_configure(usbd_pin_vbus, STM32L0_GPIO_MODE_ANALOG);
246+
}
238247

239248
usbd_wakeup = false;
240249
usbd_enabled = false;
@@ -370,16 +379,18 @@ void USBD_SetupVBUS(bool wakeup)
370379
{
371380
usbd_wakeup = wakeup;
372381

373-
stm32l0_exti_detach(usbd_pin_vbus);
374-
375-
stm32l0_gpio_pin_configure(usbd_pin_vbus,
376-
(usbd_wakeup ? STM32L0_GPIO_PARK_NONE : STM32L0_GPIO_PARK_HIZ) |
377-
(STM32L0_GPIO_PUPD_PULLDOWN | STM32L0_GPIO_OSPEED_LOW | STM32L0_GPIO_OTYPE_PUSHPULL | STM32L0_GPIO_MODE_INPUT));
378-
379-
stm32l0_exti_attach(usbd_pin_vbus,
380-
((usbd_wakeup ? 0 : STM32L0_EXTI_CONTROL_NOWAKEUP) |
381-
(STM32L0_EXTI_CONTROL_PRIORITY_LOW | STM32L0_EXTI_CONTROL_EDGE_RISING | STM32L0_EXTI_CONTROL_EDGE_FALLING)),
382-
(stm32l0_exti_callback_t)USBD_VBUSChangedIrq, NULL);
382+
if (usbd_pin_vbus != STM32L0_GPIO_PIN_NONE) {
383+
stm32l0_exti_detach(usbd_pin_vbus);
384+
385+
stm32l0_gpio_pin_configure(usbd_pin_vbus,
386+
(usbd_wakeup ? STM32L0_GPIO_PARK_NONE : STM32L0_GPIO_PARK_HIZ) |
387+
(STM32L0_GPIO_PUPD_PULLDOWN | STM32L0_GPIO_OSPEED_LOW | STM32L0_GPIO_OTYPE_PUSHPULL | STM32L0_GPIO_MODE_INPUT));
388+
389+
stm32l0_exti_attach(usbd_pin_vbus,
390+
((usbd_wakeup ? 0 : STM32L0_EXTI_CONTROL_NOWAKEUP) |
391+
(STM32L0_EXTI_CONTROL_PRIORITY_LOW | STM32L0_EXTI_CONTROL_EDGE_RISING | STM32L0_EXTI_CONTROL_EDGE_FALLING)),
392+
(stm32l0_exti_callback_t)USBD_VBUSChangedIrq, NULL);
393+
}
383394
}
384395
}
385396

0 commit comments

Comments
 (0)