I want to power a circuit I have from a dedicated power supply. But at the same time, I have a USB port that can also supply power but only when I decide to upload new firmware to a atmega32u2. Here is my circuit.
J2 is where my main voltage comes in and can be anywhere from +3.3V to +5V. Q1 acts as an ideal diode. Q2 is my load switch. R22 will pull the gate to ground. J4 is the USB port my circuit uses to connect to a PC. D1 isolates the USB voltage from the circuit's operating voltage. C3 is the bypass cap for my circuit. R21 and C1 mimic a tantulum cap. R15 and R16 are my current limiting series resistors for the USB data lines.
The idea here is to disable the use of J2's voltage whenever J4 receives voltage from the USB port. In other words, when +VUSB is present, it will go through D1 and supply about +4.7V which is good enough for communicating back to the PC and enough for my circuit to operate to receive code. +VUSB will have Q2's gate to +5V causing Vgs to be positive or zero volts, turning it off, and thus having +V only sourced from J4, despite J2 still supplying voltage.
Or at least I think so! I am basically asking for a design check to see if my understanding is correct. I am aware of power mux ICs but they are too expensive and I want to use "common parts" for this. In case this is needed, Q1,Q2 = DMP3056LDM-7 and D1 = SD1206S100S1R0. Current draw of the circuit is no more than 10mA.
EDIT 11/1/20: User Ste Kulov informed me of a cheaper kind of power mux. I like the solution. Here is the new circuit. It uses the TPS2104DBVT. Q1 is still DMP3056LDM. Note that when one of the voltage inputs are disconnected, the potential is floating. I first had a pulldown resistor but the datasheet informs to not ground the INx pin if not used. I took that as no pulldown resistor as well.