Skip to content

Commit a6cddb4

Browse files
Support boards with internal *and* external pullups again
These boards were broken in commit e1d409f Refactor USB pullup handling. Before that commit, all boards without external controllable pullups were assumed to have fixed external pullups and use the DP write trick. Since that commit, boards that have internal pullups are assumed to *not* have any external pullups and the internal pullups are automatically used. It turns out there exist some boards that have internal pullups in the chips, but also have an external fixed pullup. This can probably be considered a hardware bug, but since the boards exist, we should support them. This commit allows variants to define USBD_FIXED_PULLUP to explicitly state they have a fixed pullup on the D+ line. This will cause the D+ output trick to be applied even when internal pullups are present, fixing these boards. This define is only needed on these specific boards, but it can also be defined on boards with a fixed external pullup without internal pullups. This problem was prompted by the "Black F407VE" board, which has the problematic pullup. All other F4 boards were checked and one other was found to have the pullup, all others seem ok. None of the other series have been checked, so there might still be board broken. See also STM32-base/STM32-base.github.io#26 for some additional inventarisation of this problem. This fixes stm32duino#1029.
1 parent 1eac7c5 commit a6cddb4

File tree

4 files changed

+37
-9
lines changed

4 files changed

+37
-9
lines changed

cores/arduino/stm32/usb/usbd_if.c

+12-8
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@
6868
#if defined(USBD_DETACH_PIN) && !defined(USBD_DETACH_LEVEL)
6969
#error "USBD_DETACH_PIN also needs USBD_DETACH_LEVEL defined"
7070
#endif /* defined(USBD_DETACH_PIN) && !defined(USBD_DETACH_LEVEL) */
71+
#if (defined(USBD_DETACH_PIN) || defined(USBD_ATTACH_PIN)) && defined(USBD_FIXED_PULLUP)
72+
#error "Cannot define both USBD_FIXED_PULLUP and USBD_ATTACH_PIN or USBD_DETACH_PIN"
73+
#endif /* (defined(USBD_DETACH_PIN) || defined(USBD_ATTACH_PIN)) && defined(USBD_FIXED_PULLUP) */
7174

7275
/* Either of these bits indicate that there are internal pullups */
7376
#if defined(USB_BCDR_DPPU) || defined(USB_OTG_DCTL_SDIS)
@@ -78,10 +81,13 @@
7881
* in USBD_LL_Init in usbd_conf.c. */
7982
#if defined(USE_USB_HS)
8083
#define USBD_USB_INSTANCE USB_OTG_HS
84+
#define USBD_DP_PINNAME USB_OTG_HS_DP
8185
#elif defined(USB_OTG_FS)
8286
#define USBD_USB_INSTANCE USB_OTG_FS
87+
#define USBD_DP_PINNAME USB_OTG_FS_DP
8388
#elif defined(USB)
8489
#define USBD_USB_INSTANCE USB
90+
#define USBD_DP_PINNAME USB_DP
8591
#endif
8692

8793
/*
@@ -97,15 +103,13 @@
97103
#elif defined(USBD_DETACH_PIN)
98104
#define USBD_PULLUP_CONTROL_PINNAME digitalPinToPinName(USBD_DETACH_PIN)
99105
#define USBD_ATTACH_LEVEL !(USBD_DETACH_LEVEL)
100-
#elif !defined(USBD_HAVE_INTERNAL_PULLUPS)
101-
/* When no USB attach and detach pins were defined, and there are also
106+
#elif !defined(USBD_HAVE_INTERNAL_PULLUPS) || defined(USBD_FIXED_PULLUP)
107+
/* When no USB attach and detach pins were defined, and there are also
102108
* no internal pullups, assume there is a fixed external pullup and apply
103-
* the D+ trick. This should happen only for the USB peripheral, since
104-
* USB_OTG_HS and USB_OTG_FS always have internal pullups. */
105-
#if !defined(USB)
106-
#error "Unexpected USB configuration"
107-
#endif
108-
#define USBD_PULLUP_CONTROL_PINNAME USB_DP
109+
* the D+ trick. Also do this when there are internal *and* external
110+
* pulups (which is a hardware bug, but there are boards out there with
111+
* this). */
112+
#define USBD_PULLUP_CONTROL_PINNAME USBD_DP_PINNAME
109113
#define USBD_DETACH_LEVEL LOW
110114
// USBD_ATTACH_LEVEL not needed.
111115
#define USBD_DP_TRICK

variants/BLACK_F407XX/variant.h

+7
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,13 @@ extern "C" {
301301
#define HAL_DAC_MODULE_ENABLED
302302
#define HAL_SD_MODULE_ENABLED
303303

304+
// This indicates that there is an external and fixed 1.5k pullup
305+
// on the D+ line. This define is only needed on boards that have
306+
// internal pullups *and* an external pullup. Note that it would have
307+
// been better to omit the pullup and exclusively use the internal
308+
// pullups instead.
309+
#define USBD_FIXED_PULLUP
310+
304311
#ifdef __cplusplus
305312
} // extern "C"
306313
#endif

variants/BLUE_F407VE_Mini/variant.h

+7
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,13 @@ extern "C" {
184184
#define HAL_DAC_MODULE_ENABLED
185185
#define HAL_SD_MODULE_ENABLED
186186

187+
// This indicates that there is an external and fixed 1.5k pullup
188+
// on the D+ line. This define is only needed on boards that have
189+
// internal pullups *and* an external pullup. Note that it would have
190+
// been better to omit the pullup and exclusively use the internal
191+
// pullups instead.
192+
#define USBD_FIXED_PULLUP
193+
187194
#ifdef __cplusplus
188195
} // extern "C"
189196
#endif

variants/board_template/variant.h

+11-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,17 @@ extern "C" {
174174
//#define USBD_ATTACH_LEVEL LOW
175175
//#define USBD_DETACH_PIN x
176176
//#define USBD_DETACH_LEVEL LOW
177-
177+
//
178+
// This indicates that there is an external and fixed 1.5k pullup
179+
// on the D+ line. This define is not normally needed, since a
180+
// fixed pullup is assumed by default. It is only required when
181+
// the USB peripheral has an internal pullup *and* an external
182+
// fixed pullup is present (which is actually a hardware bug, since just
183+
// the internal pullup is sufficient and having two pullups violates the
184+
// USB specification). In this case, defining this forces
185+
// the "write D+ LOW"-trick to be used. In the future, it might also
186+
// disable the internal pullups, but this is not currently implemented.
187+
// #define USBD_FIXED_PULLUP
178188
#ifdef __cplusplus
179189
} // extern "C"
180190
#endif

0 commit comments

Comments
 (0)