# Tiny Tapeout 4 Model railway turntable polarity controller

GENERAL
A controller that automatically switches the polarity of DC-type model railway turntables, to prevent short-circuits when rotating the turntable along DC-powered tracks.
Every track that connects the turntable to the main tracks must include a short, isolated 'sensing track' between the normal tracks and the turntable.
When the turntable rotates and makes contact with any of the tracks, it powers the sensing element. If the polarity does not match that of the main tracks,
the turntable polarity controller will invert it.

This controller is designed to work with a Fleischmann 6152 turntable, using the connecting elements 6153 as sensing elements. The sensing elements must
be isolated from the main tracks.

The polarity controller assumes that
- Every sensing element has two tracks. Both are connected with a high-impedance resistor to the main track they respectively connect to.
e.g. 
      
      ---------<MAIN TRACK LEFT>---- [ISOLATING CONNECTOR] -----<SENSING TRACK LEFT>------ [TURNTABLE SLDING CONTACTOR] ------<TURNTABLE LEFT TRACK>
       |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   
      ---------<MAIN TRACK RIGHT>--- [ISOLATING CONNECTOR] -----<SENSING TRACK RIGHT>----- [TURNTABLE SLDING CONTACTOR] ------<TURNTABLE RIGHT TRACK>
                                ___
      MAIN TRACK R or L -------|    \
                         |     |     \
                      High R   | XOR  |-----Polarity switch signal
                         |     |     /
      SENSING TRACK R/L--------|___ /

      
- Every track and corresponding sensing element form two inputs for an external XOR port. when polarity is not equal, the XOR output will
  go from 0 -> 1, which is fed into the polarity controller [IN0-IN7, D1-D4]. Any of the XOR ports can command a change in polarity.
- The tracks are suitably connected through resistors and (zener) diodes to the XOR ports, to convert to digital signals and prevent an overvoltage on its inputs
- If the polarity controller receives a high input for prolonged periods of time, it assumes an error state where the turntable is not powered.
  To this end, connect [OUT6], the change-polarity control signal through a suitable RC delay to [D0] to trigger an error.
- The turntable polarity controller controls a full-bridge driver that connects the turntable to the main tracks.
- In the original design, the polarity controller uses digital control signals to control the full-bridge driver. The setup uses N-channel mosfets,
  where the gate voltage supply is generated by boosting the track voltage. 
- XOR input circuitry, XOR ports, Full bridge driver, mosfets and gate-voltage supply are all external to
  the polarity controller, but must be implemented if this design is to be used.
- The clock signal can be generated by the controller. Connect IO port [D6] and [D7] using an RC network to tune the frequency.
- The controller assumes a reset signal is always activated before the controller is used. An RC network to VCC can keep the reset signal active while powering up.
  Make sure to connect a switch that can ground the reset input to be able to activate the controller after an error.

POLARITY CONTROLLER
The inputs that can trigger a polarity change are connected through OR ports, hence every input can trigger a polarity change.

The trigger is used to drive a binary counter that counts between 0..3 on the clock signal, which in turn generates signals for the full bridge driver, which is controlled by OUT2 and OUT3.
To avoid shoot-through while switching in the FETs that connect tracks to either polarity, the outputs are only enabled on counts 1 and 3, 
using counts 0 and 2 to turn all FETS off. The counter generates a trigger when at value 0 and 2, so as to always end up in count 1 or 3.
For miscellaneous purposes, the counter signal is provided on OUT0 and OUT1.

The counter trigger signal is provided on output OUT6, which can be connected through an RC delay on D0 to trigger an error when the trigger
is active for an unusually long period of time. This may happen in case of short circuits at the tracks or other electrical issues. All driver outputs are disabled while an error is active.

The reset signal removes errors and disables the counter and bridge driver outputs.

An inverter network can be used to generate a clock by the IC. To this end, connect D6 and D7 through an RC network. The delay will tune the clock frequency.
10k
D0
D1
D2
D3
D4
D5
D6
D7
Bidirectional I/O pins
SR

ERC Warnings

flop4:CLK: Clock driven by combinatorial logic
flop1:CLK: Clock driven by combinatorial logic