The structure of ATP synthase consists of two rotary motors, labeled F1 and Fo, that are connected by a flexible shaft. Under normal operation, the Fo motor uses the energy stored in a transmembrane ion gradient to drive the F1 motor in reverse so that ATP is synthesized from ADP and phosphate. In bacteria, anerobic conditions wipe out the ion gradient whereupon the F1 part becomes a motor, using the energy of ATP hydrolysis to turn the Fo part in reverse so that it functions as an ion pump.
Oster, G., and Wang, H. (2000). Reverse engineering a protein: The mechanochemistry of ATP synthase. Biochimica et Biophysica Acta 1458, 482-510.