3. Can you please elaborate a little more on making it switching. Can you modify my schematic? Like I said before I know very little about how this should all work and dont want to make boards and they not work
The pull-up resistor is detected by the usb host and the host uses it as a presence signal. Both data pins have pull-down resistors on host side and the device pulls one or both of them up with a bit stronger resistor. When the data is sent, the drivers have much lower impedance and overdrive both resistors. The actual resistor placement (d+, d- or both) tells the host, what speed it needs to talk. The STM32 supports only low speed, so don't change that.
If you add a transistor between the 3.3V and the resistor, you can make the device disappear. Sometimes you need that. 1) if your intialization code takes too much time, the host may time out before you are ready. You can work around this by starting the usb earlier, but it is not always an option. Also, some usb stacks may need main loop to be operational, so initializing slow peripherals while polling USB handler gets more difficult. In this case, you can switch on the transistor only after you are ready. 2) Restarts. You may need to restart the uc. The host does not know, that the uc has restarted and problems happen. It is better to disconnect the usb before restarting. 3) bootloaders and other re-enumeration issues. Sometimes, you need your own bootloader in front of your own application. For example, encrypted fw updates. If the bootloader speaks usb, then you need to disconnect the usb when bootloader finishes and app starts. If the bootloader does not speak usb, the resistor shall be switched off so that the host would not detect an error (windows "this device has malfunctioned" errors etc).
For very simple devices, that have the USB always ready for the host, the resistor may always be present. Or if the usb is only connected for fw updates and other not frequent maintenance operations and you just ignore the problems and tell the user to try again if something fails.
There are a lot of examples how to switch the pullup resistor. Some do it with one transistor, some use two (as the default state of gpio is pullup, you may want to have default-on or default-off depending on your solution). For example
https://www.olimex.com/Products/ARM/ST/STM32-H103/resources/STM32-H103-sch.gifThe same goes for most of the uc-s (but some have the resistors integrated, ie STM32F2). Other uc vendors have same information, ie
http://www.nxp.com/documents/application_note/AN11392.pdf