Skip to content

Commit 61abf8d

Browse files
authored
Merge pull request #7 from UT2UH/VBUS
No VBUS detection support
2 parents d2bb5fc + a84a0d6 commit 61abf8d

File tree

9 files changed

+56
-37
lines changed

9 files changed

+56
-37
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.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,6 @@ void init( void )
6464

6565
stm32l0_dma_configure(STM32L0_ADC_IRQ_PRIORITY, STM32L0_UART_IRQ_PRIORITY, STM32L0_UART_IRQ_PRIORITY);
6666

67-
#if defined(STM32L0_CONFIG_PIN_VBUS)
68-
if (STM32L0_CONFIG_PIN_VBUS != STM32L0_GPIO_PIN_NONE) {
69-
stm32l0_gpio_pin_configure(STM32L0_CONFIG_PIN_VBUS, (STM32L0_GPIO_PARK_HIZ | STM32L0_GPIO_PUPD_PULLDOWN | STM32L0_GPIO_OSPEED_LOW | STM32L0_GPIO_OTYPE_PUSHPULL | STM32L0_GPIO_MODE_INPUT));
70-
}
71-
#endif
72-
7367
#if defined(STM32L0_CONFIG_PIN_VBAT)
7468
stm32l0_gpio_pin_configure(STM32L0_CONFIG_PIN_VBAT, (STM32L0_GPIO_PARK_NONE | STM32L0_GPIO_MODE_ANALOG));
7569
#endif

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/Lib/libstm32l052xx.a

60.5 KB
Binary file not shown.

system/STM32L0xx/Lib/libstm32l072xx.a

81 KB
Binary file not shown.

system/STM32L0xx/Lib/libstm32l082xx.a

37.8 KB
Binary file not shown.

system/STM32L0xx/Source/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
_out
2+
*.a

system/STM32L0xx/Source/USB/HAL/Src/stm32l0xx_hal_pcd.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ HAL_StatusTypeDef HAL_PCD_DeInit(PCD_HandleTypeDef *hpcd)
240240
hpcd->State = HAL_PCD_STATE_BUSY;
241241

242242
/* Stop Device */
243-
(void)HAL_PCD_Stop(hpcd);
243+
(void)USB_StopDevice(hpcd->Instance);
244244

245245
#if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
246246
if (hpcd->MspDeInitCallback == NULL)
@@ -998,7 +998,7 @@ HAL_StatusTypeDef HAL_PCD_Stop(PCD_HandleTypeDef *hpcd)
998998
__HAL_LOCK(hpcd);
999999
__HAL_PCD_DISABLE(hpcd);
10001000

1001-
(void)USB_StopDevice(hpcd->Instance);
1001+
(void)USB_DevDisconnect(hpcd->Instance);
10021002

10031003
__HAL_UNLOCK(hpcd);
10041004

system/STM32L0xx/Source/USB/usbd_conf.c

Lines changed: 41 additions & 27 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)
@@ -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,32 @@ 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+
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);
220+
221+
if (stm32l0_gpio_pin_read(usbd_pin_vbus))
222+
{
223+
stm32l0_lptim_timeout_start(&USBD_VBUSTimeout, stm32l0_lptim_millis_to_ticks(40), (stm32l0_lptim_callback_t)USBD_VBUSTimeoutIrq); /* 40ms */
224+
}
225+
226+
stm32l0_exti_attach(usbd_pin_vbus,
227+
(STM32L0_EXTI_CONTROL_NOWAKEUP | STM32L0_EXTI_CONTROL_PRIORITY_LOW | STM32L0_EXTI_CONTROL_EDGE_RISING | STM32L0_EXTI_CONTROL_EDGE_FALLING),
228+
(stm32l0_exti_callback_t)USBD_VBUSChangedIrq, NULL);
229+
} else {
230+
// No VBUS pin, assume VBUS is always present
231+
usbd_attached = true;
232+
usbd_mask_vbus = 0;
220233
}
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);
234+
225235

226236
return true;
227237
}
@@ -230,11 +240,13 @@ void USBD_Teardown()
230240
{
231241
USBD_Detach();
232242

233-
stm32l0_exti_detach(usbd_pin_vbus);
243+
if (usbd_pin_vbus != STM32L0_GPIO_PIN_NONE) {
244+
stm32l0_exti_detach(usbd_pin_vbus);
234245

235-
stm32l0_lptim_timeout_stop(&USBD_VBUSTimeout);
246+
stm32l0_lptim_timeout_stop(&USBD_VBUSTimeout);
236247

237-
stm32l0_gpio_pin_configure(usbd_pin_vbus, STM32L0_GPIO_MODE_ANALOG);
248+
stm32l0_gpio_pin_configure(usbd_pin_vbus, STM32L0_GPIO_MODE_ANALOG);
249+
}
238250

239251
usbd_wakeup = false;
240252
usbd_enabled = false;
@@ -370,16 +382,18 @@ void USBD_SetupVBUS(bool wakeup)
370382
{
371383
usbd_wakeup = wakeup;
372384

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);
385+
if (usbd_pin_vbus != STM32L0_GPIO_PIN_NONE) {
386+
stm32l0_exti_detach(usbd_pin_vbus);
387+
388+
stm32l0_gpio_pin_configure(usbd_pin_vbus,
389+
(usbd_wakeup ? STM32L0_GPIO_PARK_NONE : STM32L0_GPIO_PARK_HIZ) |
390+
(STM32L0_GPIO_PUPD_PULLDOWN | STM32L0_GPIO_OSPEED_LOW | STM32L0_GPIO_OTYPE_PUSHPULL | STM32L0_GPIO_MODE_INPUT));
391+
392+
stm32l0_exti_attach(usbd_pin_vbus,
393+
((usbd_wakeup ? 0 : STM32L0_EXTI_CONTROL_NOWAKEUP) |
394+
(STM32L0_EXTI_CONTROL_PRIORITY_LOW | STM32L0_EXTI_CONTROL_EDGE_RISING | STM32L0_EXTI_CONTROL_EDGE_FALLING)),
395+
(stm32l0_exti_callback_t)USBD_VBUSChangedIrq, NULL);
396+
}
383397
}
384398
}
385399

0 commit comments

Comments
 (0)