Bootloader – a pin for the usb d+ disconnect

Pito
Fri Aug 12, 2016 4:39 pm
I’ve learned how to compile a bootloader with the green LED placed on a pin of choice, but I have not understood fully how it is with the PIN which disconnects the D+ for the usb enumeration.
In the hardware.c there is a comment “we do not do it with PC12” (even I saw there was a setPin(C,12) before in the source), maple mini uses PB9 for the disconnect, and Jaromir in his excellent soldering guide is using PB10 – why?
Also I’ve read somewhere we do it via PA12 (D+) today (no external transistor needed).
So where in the sources we can set a pinXY for the usb disconnect (with the external transistor)?
PS: I want to use PB2 (boot1) for that..

Pito
Fri Aug 12, 2016 6:20 pm
Ok, in usb.c lines 54-55 is the def for the BP disconnect pins.

BTW, a closer look at the MapleMini 2 transistors disconnect switch reveals there is a nice trick built in – the base of Q2 is powered from USB_5V (VUSB pin on the usb connector) – thus the Q2 cannot toggle the 1k5 resistor when the USB cable is disconnected. The resistor is then floating, for any DISC voltage level. So the circuit is fully off and you can toggle DISC as you wish.
8-)

With the BP’s high-side Q1 pMosfet switch it may work similarly as the MM when the source/emitter of the transistor Q1 will be connected to VUSB (what is not the case when connected to 5V or 3.3V Vcc). So when connected to 3.3Vcc any logic change at the DISC signal will toggle the 1k5 resistor, not good.. :(

MM USB Disconnect.JPG
MM USB Disconnect.JPG (41.23 KiB) Viewed 1598 times

RogerClark
Fri Aug 12, 2016 10:05 pm
Can you post a the part of the BluePill schematic you are referring to, as I didnt think it had any USB reset hardware.
What we do to reset the USB is basically a hack ( but thats the only option unless you change the BP hardware)

Pito
Sat Aug 13, 2016 12:13 am
@Roger – for BP’s external DISC circuits I refer
the fredbox’s bipolar:
http://www.stm32duino.com/viewtopic.php?t=780
and Jaromir’s pmosfet hack:
http://www.stm32duino.com/viewtopic.php … mos#p12933

A difficult to understand settings and processes with DISC pins, even weird imho :)

Basically we have two places where we handle the 1k5 resistor at USB’s D+ :

1. in the bootloader, DISC pin defined in config.h (maples) or in usb.c (generics) – line 40+, DISC used in usb.c
===============================================================================================
– for Maple we: set the DISC pin as OpenDrain, then set it to 1, set it to 0, and that is it..
– for MM it is PB9

– for generic we: set DISC pin as PushPull, then set it to 0, wait about 50 useconds (??), set it to Input (let it float), and that is it..
– for generic it is PA12 (D+)

Signaling based on DISC pin level:
A. MapleMini:
log0: we pull D+ weak high via 1k5
log1: we let the D+ float (1k5 disconnected from Vcc)
B. Generic:
log0: we pull D+ hard low
Input: we let D+ be pulled weak high via 1k5

2. in the core – DISC pin defined in board.h – DISC pin is used for cdc connect and disconnect in usb_serial.cpp – line 60-90
===========================================================================================================
– for MM it is PB9
– for generic it is PB10 (??)

Signaling based on DISC pin level:
A. for MM we pull D+ weak high via 1k5, or, we let the D+ float (1k5 disconnected from Vcc)
B. for generic we pull D+ hard low, or, we let D+ be pulled weak high via 1k5

:?


RogerClark
Sat Aug 13, 2016 12:35 am
I have not looked in detail at the USB reset, but the MM is definitely different from the Generic method

In the sketch, I think MM sets DISC (High) to reset the USB, this pulls D+ to USB (Vdd). MM then leaves DISC set to 1 .
I presume USB DP is pulled low by the host (PC)

The Generic does not have a way to disconnect the 1.5k from Vdd as its permanently pulled high, so the trick is to set the pin as GPIO and drive it LOW, then release the GPIO pin, so that USB DP is pulled high by the 1.5k

The difference between these 2 methods, will be that on the Generic board, as soon as it is plugged in USB DP will be High (on the MM it will initially be Low). After some “setup” time USB DP is driven low and then released.

So there is a delay between the board being connected and USB DP going from Low to High.

I can’t remember what the bootloader does. But I presume it must reset DISC (on MM) to Low, for some time, then set DISC so that USB DP can be pulled high via the transistor.

BTW. I think the USB spec says that USB DP should not be driven low by the target, but we don’t have any choice – and normally it works OK.


Pito
Sat Aug 13, 2016 12:55 am
With an external pnp transistor or pmosfet, which switches the 1k5 resistor against Vcc ( so called high-side switch) there are only 2 states with D+ , based on DISC pin signal level:

1. D+ is pulled “weak” high via the 1k5 resistor to Vcc,

2. D+ is not pulled weak high, D+ does not see the 1k5 resistor (a high impedance), as the 1k5 resistor is disconnected from Vcc.

There are none such DISC pin levels where the D+ could be pulled hard low, weak low, or hard high..


RogerClark
Sat Aug 13, 2016 3:45 am
@pito

Yes. I suspect using a FET is the best system. I did try to modify a GD32 board to add a P Channel FET for Reset but didnt manage to make it work, however I suspect I had some problem with the clock speed on the GD32 vs the STM32 wen trying to use the MM code in the bootloader


Pito
Sat Aug 13, 2016 9:39 am
I’ve been messing with the PB2 as the DISC pin.
It has got a 100k resistor builtin on the pcb (R4) in series with the signal, but it must not be a problem with a pmosfet (provided we do not talk about 15ns long pulse, the RC of this circuit is about 40usecs).
However, I cannot get it working on PB2.
Built a new bootloader with PB2 and with maple hardware option. It does not find the DFU..yet..

PB2 disconnect.JPG
PB2 disconnect.JPG (12 KiB) Viewed 1571 times

RogerClark
Sat Aug 13, 2016 10:34 am
@pito

I think I had similar problems, I tried using a P FET to reset the USB on a GD32, but it didnt work.

And I didnt have time to debug the hardware or software.

I still have the GD32 board with the PFET on it, so I will try to look at the waveform’s tomorrow with my oscilloscope to see it I can see why it doesnt work


Pito
Sat Aug 13, 2016 11:14 am
I’ve changed the DISC pin to PB12 to make the stuff easier to debug. No 100k resistor in series with DISC signal now. The 10k resistor (D+, Vcc) on the BP’s pcb removed. I also have replaced the 1Meg resistor with 68k to speed up the circuit, but no change.

After I press hw reset (during the reset button is pressed down the 1k5 is disconnected):
1. it switches the 1k5 to 3.3V
2. Windows bells and enumerates immediately and in the device manager I see the DFU driver, green LED blinks aprox 3Hz
3. it keeps 1k5 switched to 3.3V for another 8 secs, DFU still in dev manager visible, green LED still blinks
4. after 8 secs it disconnects the 1k5, win bells and the DFU disappears from the dev manager, green LED stops blinking.

Upload from arduino does not work, it cannot find DFU.
:?


Pito
Sat Aug 13, 2016 11:36 am
There is a 120ns long blip 500ns after the DISC goes first time low after the reset.

disc PB12 2.JPG
disc PB12 2.JPG (46.91 KiB) Viewed 784 times

Pito
Sat Aug 13, 2016 12:40 pm
And the overall picture (the usb data are not fully visible with this sampling rate).

disc PB12 4.JPG
disc PB12 4.JPG (52.28 KiB) Viewed 782 times

Pito
Sat Aug 13, 2016 3:42 pm
I took the bootloader binary from fredbox (dec2016) – he configured DISC at PB9.
http://www.stm32duino.com/viewtopic.php?t=780
So I flashed his binary in and solder DISC to PB9. Using the PMOS as above. Also edited the board.h.
The led blinks only couple of seconds before it goes to userland, so there is a difference between his and mine source/binary.
No go.
I’ve edited the generics core files where applicable – to remove the PA12 gpio disconnect stuff (hopefully).
No go.

So finally I set MapleMini in the IDE as the board of choice and it works PERFECTLY now. No issues with upload from IDE. :twisted:
It seems to me in the current generics –
1. the bootloader source has been changed since December, and
2. also the generics BP core files still include incompabilities with the external disconnect hardware..

Now, how to consolidate all the stuff such we can build a bootloader for BPill (DISC pin as the option) and have working core files for Generic_BP_EXDISC??
:)


Pito
Sun Aug 14, 2016 10:39 am
I took the bootloader source again, and compiled for PB2/boot1 (mind there is 100k in series with the DISC signal), with pmos (and 1Meg from g to Vcc) – it does not enumerate. The RC of the large resistor’s values and Cin capacitance of the Q1 (45pf) is too large for short pulsing obviously. So you may try with PB2 when you replace the 100k resistor on the BP’s pcb.

I compiled the bootloader again for PB12 DISC (pmos, 68k from g to Vcc), it enumerates. I set MapleMini profile in IDE (and set PB12 in maple’s /board/board.h).
First attempts it did not find the DFU.
After few plug out/ins during the upload the DFU was found and it uploaded the sketch (maple com appeared).
Afterwards it finds the DFu each time and it uploads the sketch fine.

The only issue I see it keeps the DISC active for 8secs (see the LA traces) so after upload it takes several seconds it ends up (you cannot open serial monitor till the final bell).
The old fredbox version ends immediately after upload – we probably missing to set DISC high somewhere at the end in the latest source.. Or I am not able to compile the bootloader properly..
This is the schematics which works here:

PMOS DISC.JPG
PMOS DISC.JPG (19.31 KiB) Viewed 764 times

Pito
Sun Aug 14, 2016 11:28 am
Ok, I went back to generic BP’s profile – I edited the board/board.h for PB12 DISC and, in /wirish/boards_setup.cpp I commented out the lines 86-91 ).
Now it uploads fine. There is still the 8secs issue (the LED indicates for 6secs after the upload finishes it is in bootloader, then bells and goes to userland). MapleMini goes immediately after upload finishes.

Pito
Sun Aug 14, 2016 8:27 pm
Grrh, I’ve found the issue with 8seconds long waiting in bootloader – the BUTTON_BANK must be defined in order to get BOOTLOADER_WAIT “6”, otherwise the BOOTLOADER_WAIT is “30”.. :evil:

So the generic target for BluePill with DISC pin at PB2 may look like this in config.h:

..
#elif defined TARGET_GENERIC_F103_DISC_PB12 // BluePill

#define HAS_MAPLE_HARDWARE 1

#define LED_BANK GPIOC
#define LED_PIN 13
#define LED_ON_STATE 0

#define BUTTON_BANK 1

// DISCONNECT PIN (drives an highside PMOS/PNP)
// mind the DISC pin is configured Open Drain
#define USB_DISC_BANK GPIOB
#define USB_DISC_PIN 2
..


Pito
Mon Aug 15, 2016 10:34 am
And my BluePill disconnect via PB2 (Boot1) mod – the Boot0/1 header removed and the pads used for soldering the SMD pmosfet (BSS223), and gate-Vcc resistor (here 47k). On the bottom pcb side, the original R10=10k disconnect resistor removed, R4=100k removed and replaced with the 10k, R3=100k removed, R3 pads used for the new disconnect resistor 1k5 (here 2k2 from my junk box and it works, will be replaced with 1k5) and 2 short wires soldered in at the right places :)
After the mod the USB DISC pin is PB2, Boot0 = 0, the config files mods as above, IDE board generic BPill.

DISC mod 1.JPG
DISC mod 1.JPG (44.52 KiB) Viewed 708 times

RogerClark
Mon Aug 15, 2016 9:22 pm
Did you use a FET in your latest modification?
I could not see a FET in the photos.

Can you draw a schematic of these latest changes ( e.g. just draw it on paper and take a photo)


Pito
Tue Aug 16, 2016 10:06 am
Yes the pmosfet is there – on the first picture 2mm right from the 47k resistor :)
I have got problem with my attachment quota – I deleted almost all pictures from the past and still off the quota:(.
==========================
The schematics is the same as the above PMOS DISC.JPG:
disconnect pin PB2
sch.R3 1k..10k (a good engineering practice is to have one there)
sch.R2 1k5 (must be)
sch.R1 10k..100k (must be)
Q1 – pmosfet with low Vgs trigger voltage (around 1.5V)
or
Q1 – pnp bipolar, any general purpose type
==========================
The actual implementation is depicted on the shots above – on the BPill PCB you mess with pcb.R3, pcb.R4, pcb.R10 :)
Boot0/1 Header – removed
pcb.R4 = sch.R3
pcb.R3 = removed
pcb.R10 = removed

RogerClark
Tue Aug 16, 2016 11:22 pm
Hi pito

I didnt realise there was a user quota on images.

I will see if I can change it, or perhaps rescale your images ( though I would have to find and download them and upload again)


Pito
Wed Aug 17, 2016 9:09 am
It seems <1MB is the quota for attachments (at least in my profile). Rather small amount in 2016. Pls be so kind and increase it when possible. I’ve already deleted a dozen of my pictures to stay in the quota. I have currently about 800kB of jpegs there and cannot upload a new one. My jpegs are in average 50kB.

RogerClark
Wed Aug 17, 2016 11:07 am
Pito

I don’t know if this is a user quota issue of an overall site quota.

The there is a setting called “Quota” which seems to be set at 52Mb. But I just checked and this seems to be the actual total size of all files rather than being a limit.

I can’t find anything which says there is a limit to the total size of attachments a user can upload, but there is a limit per post I think

https://www.inmotionhosting.com/support … t-settings

Are you sure you can’t attach images to any post ?


Pito
Wed Aug 17, 2016 3:53 pm
Currently I cannot attach the pictures I deleted from my previous post, it shows an yellow triangle with exclamation mark – “Sorry, the board attachment quota has been reached.”
The same when I want to add a picture to a new post.

RogerClark
Wed Aug 17, 2016 9:28 pm
Try again now.

I think I found the setting and I have increased it ;-)


Pito
Wed Aug 17, 2016 10:15 pm
Nope, tried with a 31kB jpg – status: yellow triangle “Sorry, the board attachment quota has been reached.”
665kB of attachments here.

RogerClark
Wed Aug 17, 2016 11:19 pm
Umm.

OK.

I wonder if I need to reset something to get it to notice the change

I’ll get back to you on this one.


Pito
Fri Aug 19, 2016 2:59 pm
Ok I updated the pictures with the actual wiring above. Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *