]> git.tdb.fi Git - model-railway-devices.git/blob - arducontrol/arducontrol.c
882f280b9e432b3dd3399f0230a011b4404d34d4
[model-railway-devices.git] / arducontrol / arducontrol.c
1 /*
2 Firmware for model railway control interface
3
4 ATMega pinout:
5 D0 - serial RX
6 D1 - serial TX
7 D2 - Polarity control
8 D3 - Power control
9
10 Connections for Pololu high-powered motor driver:
11 D2 <-> DIR
12 D3 <-> PWM
13
14 ADC connections:
15 ADC0 - current sensor (adjusted for 185 mV/A, centered at Vcc/2)
16 ADC1 - input voltage (adjusted for divisor of 11)
17
18 This is not a complete control device.  Rather, it is a low-level bus driver,
19 intended to be controlled with a computer program.  In particular, no state is
20 kept about locomotives or solenoids.  It is up to the controlling program to
21 maintain a refresh cycle, turn off solenoids after a sufficient delay, etc.
22
23 Commands are received as a packetized stream over the serial interface.  Each
24 packet begins with a header byte, which is bitwise not of the packet's length,
25 not including the header byte itself.  The longest allowed packet is 15 bytes.
26 Thus, the high four bits of the header byte are always ones.  The first data
27 byte specifies the command, and the rest of the packet is command arguments.
28
29 After a command has been processed, a similarly packetized response is sent.
30
31 Track output is asynchronous up to a single packet.  If an output command is
32 received while a previous packet is still being sent to the track, execution
33 blocks until the operation completes.  The command returns as soon as sending
34 its data to the track can start.
35 */
36 #include <avr/io.h>
37 #include <avr/interrupt.h>
38 #include "interface.h"
39 #include "monitor.h"
40 #include "output.h"
41
42 int main()
43 {
44         output_init();
45         interface_init();
46         monitor_init();
47
48         sei();
49
50         while(1)
51         {
52                 interface_check();
53                 monitor_check();
54         }
55
56         return 0;
57 }