Skip to content

Commit 553143d

Browse files
Support boards with a voltage divider on VBUS
This can be useful to allow measuring the actual USB voltage, rather than just detect presence. Previously the USB code would always enable an internal pulldown on the VBUS, which would skew the reading (and, if the divider is fairly high impedance, can even prevent detecting VBUS presence using a digital read). With this commit, boards can indicate they use an external divider using the STM32L0_CONFIG_PIN_VBUS_HAS_DIVIDER macro in their variant.h. When this is defined, the internal pullup is not enabled to prevent it from causing issues and because it is no longer needed (the external divider functions as a pulldown).
1 parent c47898a commit 553143d

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

cores/arduino/USBCore.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,17 @@
5454
bool USBDeviceClass::begin()
5555
{
5656
#if defined(USB_CLASS)
57+
#if defined(STM32L0_CONFIG_PIN_VBUS_HAS_DIVIDER)
58+
// On boards with an external divider (to allow measuring
59+
// voltage rather than just on/off), do not enable the pulldown
60+
// since that influences the divider (and the divider works as
61+
// an external pulldown too).
62+
bool needs_pulldown = false;
63+
#else
64+
bool needs_pulldown = true;
65+
#endif
5766
return USBD_Initialize(USB_VID, USB_PID, (const uint8_t*)USB_MANUFACTURER, (const uint8_t*)USB_PRODUCT, USB_CLASS,
58-
STM32L0_CONFIG_PIN_VBUS, STM32L0_USB_IRQ_PRIORITY,
67+
STM32L0_CONFIG_PIN_VBUS, needs_pulldown, STM32L0_USB_IRQ_PRIORITY,
5968
&USBDeviceClass::connectCallback, &USBDeviceClass::disconnectCallback, &USBDeviceClass::suspendCallback, &USBDeviceClass::resumeCallback);
6069
#endif
6170
return false;

cores/arduino/wiring_private.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ extern void USBD_CDC_Initialize(void *);
5959
extern void USBD_CDC_MSC_Initialize(void *);
6060

6161
extern bool USBD_Initialize(uint16_t vid, uint16_t pid, const uint8_t *manufacturer, const uint8_t *product, void(*initialize)(void *),
62-
unsigned int pin_vbus, unsigned int priority,
62+
unsigned int pin_vbus, bool pin_vbus_needs_pulldown, unsigned int priority,
6363
void(*connect_callback)(void), void(*disconnect_callback)(void), void(*suspend_callback)(void), void(*resume_callback)(void));
6464
extern void USBD_Teardown(void);
6565
extern void USBD_Attach(void);

system/STM32L0xx/Source/USB/usbd_conf.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ static void USBD_VBUSChangedIrq(void)
182182
}
183183

184184
bool USBD_Initialize(uint16_t vid, uint16_t pid, const uint8_t *manufacturer, const uint8_t *product, void(*initialize)(struct _USBD_HandleTypeDef *),
185-
unsigned int pin_vbus, unsigned int priority,
185+
unsigned int pin_vbus, bool pin_vbus_needs_pulldown, unsigned int priority,
186186
void(*connect_callback)(void), void(*disconnect_callback)(void), void(*suspend_callback)(void), void(*resume_callback)(void))
187187
{
188188
if (stm32l0_system_pclk1() < 10000000)
@@ -213,7 +213,10 @@ bool USBD_Initialize(uint16_t vid, uint16_t pid, const uint8_t *manufacturer, co
213213

214214
stm32l0_lptim_timeout_create(&USBD_VBUSTimeout);
215215

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));
216+
uint32_t mode = (STM32L0_GPIO_PARK_HIZ | STM32L0_GPIO_OSPEED_LOW | STM32L0_GPIO_OTYPE_PUSHPULL | STM32L0_GPIO_MODE_INPUT);
217+
if (pin_vbus_needs_pulldown)
218+
mode |= STM32L0_GPIO_PUPD_PULLDOWN;
219+
stm32l0_gpio_pin_configure(usbd_pin_vbus, mode);
217220

218221
if (stm32l0_gpio_pin_read(usbd_pin_vbus))
219222
{

0 commit comments

Comments
 (0)