Tue, 13 Dec 2011 18:40:20 +0100
added sound triggers to live mode
2
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
1 | #include <avr/interrupt.h> |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
2 | #include <avr/pgmspace.h> |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
3 | #include <util/delay.h> |
0
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
4 | #include "stdint.h" |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
5 | #include "main.h" |
3 | 6 | #include "driver/adc.h" |
2
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
7 | #include "driver/rs232.h" |
3 | 8 | #include "lowlevel.h" |
0
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
9 | |
34 | 10 | void blinkdelay(void) { |
11 | _delay_ms(50); | |
12 | } | |
13 | ||
0
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
14 | void LED(uint8_t num, uint8_t state) { |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
15 | switch (num) { |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
16 | case 1: switch (state) { |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
17 | case 0: LED1_PORT &= ~_BV(LED1); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
18 | case 1: LED1_PORT |= _BV(LED1); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
19 | case 2: LED1_PORT ^= _BV(LED1); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
20 | } break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
21 | |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
22 | case 2: switch (state) { |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
23 | case 0: LED2_PORT &= ~_BV(LED2); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
24 | case 1: LED2_PORT |= _BV(LED2); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
25 | case 2: LED2_PORT ^= _BV(LED2); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
26 | } break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
27 | |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
28 | case 3: switch (state) { |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
29 | case 0: LED3_PORT &= ~_BV(LED3); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
30 | case 1: LED3_PORT |= _BV(LED3); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
31 | case 2: LED3_PORT ^= _BV(LED3); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
32 | } break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
33 | |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
34 | case 4: switch (state) { |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
35 | case 0: LED4_PORT &= ~_BV(LED4); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
36 | case 1: LED4_PORT |= _BV(LED4); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
37 | case 2: LED4_PORT ^= _BV(LED4); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
38 | } break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
39 | |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
40 | case 5: switch (state) { |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
41 | case 0: LED5_PORT &= ~_BV(LED5); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
42 | case 1: LED5_PORT |= _BV(LED5); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
43 | case 2: LED5_PORT ^= _BV(LED5); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
44 | } break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
45 | } |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
46 | } |
2
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
47 | |
37
136a79772098
implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents:
34
diff
changeset
|
48 | void LEDS_OFF(void) { |
136a79772098
implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents:
34
diff
changeset
|
49 | LED1_PORT &= ~_BV(LED1); |
136a79772098
implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents:
34
diff
changeset
|
50 | LED2_PORT &= ~_BV(LED1); |
136a79772098
implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents:
34
diff
changeset
|
51 | LED3_PORT &= ~_BV(LED1); |
136a79772098
implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents:
34
diff
changeset
|
52 | LED4_PORT &= ~_BV(LED1); |
136a79772098
implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents:
34
diff
changeset
|
53 | LED5_PORT &= ~_BV(LED1); |
136a79772098
implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents:
34
diff
changeset
|
54 | } |
136a79772098
implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents:
34
diff
changeset
|
55 | |
3 | 56 | void init_hardware(void) { |
7
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
57 | // reset all ports to input, no pullup |
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
58 | DDRA = 0; PORTA = 0; |
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
59 | DDRB = 0; PORTB = 0; |
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
60 | DDRC = 0; PORTC = 0; |
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
61 | DDRD = 0; PORTD = 0; |
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
62 | |
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
63 | |
34 | 64 | RS232_init(); // initialize RS232 interface |
3 | 65 | initADC(); |
7
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
66 | SFIOR = 0; |
3 | 67 | |
68 | // set LED output | |
69 | DDR(LED1_PORT) |= _BV(LED1); | |
70 | DDR(LED2_PORT) |= _BV(LED2); | |
71 | DDR(LED3_PORT) |= _BV(LED3); | |
72 | DDR(LED4_PORT) |= _BV(LED4); | |
73 | DDR(LED5_PORT) |= _BV(LED5); | |
74 | ||
75 | // set Controller Input Pull-UPs | |
76 | CONTROLLER_PORT |= (_BV(CONTROLLER1_SW) | _BV(CONTROLLER2_SW) | _BV(CONTROLLER3_SW) | _BV(CONTROLLER4_SW)); | |
77 | ||
78 | // switch pull-ups | |
79 | SW_FUEL_PORT |= _BV(SW_FUEL); | |
7
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
80 | SW_START_PORT |= _BV(SW_START); |
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
81 | SW_PACECAR_PORT |= _BV(SW_PACECAR); |
3 | 82 | |
7
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
83 | // control outputs |
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
84 | DDR(LAP_COUNTER_PORT) |= _BV(LAP_COUNTER); |
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
85 | |
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
86 | // setup rail control |
3 | 87 | //RAIL_DETECT_PORT |= _BV(RAIL_DETECT); // enable internal pull-up |
88 | DDR(RAIL_POWER_PORT) |= _BV(RAIL_POWER); | |
89 | ||
42
018d4d63ad3b
implemented 500ns timer and set jumpstart time
Malte Bayer <mbayer@neo-soft.org>
parents:
41
diff
changeset
|
90 | // setup debut output on i2c pins |
018d4d63ad3b
implemented 500ns timer and set jumpstart time
Malte Bayer <mbayer@neo-soft.org>
parents:
41
diff
changeset
|
91 | DDR(PORTC) |= _BV(PC0) | _BV(PC1); |
018d4d63ad3b
implemented 500ns timer and set jumpstart time
Malte Bayer <mbayer@neo-soft.org>
parents:
41
diff
changeset
|
92 | |
7
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
93 | // display init sequence on LEDs |
34 | 94 | LED(1, 1); blinkdelay(); |
95 | LED(2, 1); blinkdelay(); | |
96 | LED(3, 1); blinkdelay(); | |
97 | LED(4, 1); blinkdelay(); | |
98 | LED(5, 1); blinkdelay(); | |
99 | LED(1, 0); blinkdelay(); | |
100 | LED(2, 0); blinkdelay(); | |
101 | LED(3, 0); blinkdelay(); | |
102 | LED(4, 0); blinkdelay(); | |
103 | LED(5, 0); | |
3 | 104 | |
6
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
4
diff
changeset
|
105 | |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
4
diff
changeset
|
106 | // setup response receiver timer |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
4
diff
changeset
|
107 | TCCR0 = (1<<CS01); //divide by 8 |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
4
diff
changeset
|
108 | // interrupt enable + tcnt0 set in timer2 |
3 | 109 | |
110 | ||
41
9857c18c5e64
implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents:
37
diff
changeset
|
111 | // setup data bit + carid timer |
7
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
112 | TCCR2 = (1<<CS21) | (1<<WGM21); //divide by 8, set compare match |
3 | 113 | OCR2 = TIMER2_50US; |
69
27c0c0095e26
implemented responsewire in CU firmware
Malte Bayer <mbayer@neo-soft.org>
parents:
42
diff
changeset
|
114 | TIMSK |= _BV(OCIE2); //enable timer2 interrupt |
3 | 115 | |
41
9857c18c5e64
implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents:
37
diff
changeset
|
116 | // enable carid interrupts |
9857c18c5e64
implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents:
37
diff
changeset
|
117 | MCUCR = _BV(ISC00) | _BV(ISC01) | _BV(ISC10) | _BV(ISC11); // INT0/1 rising edge |
9857c18c5e64
implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents:
37
diff
changeset
|
118 | GICR = _BV(INT0) | _BV(INT1) ; // Enable INT0 + INT1 |
9857c18c5e64
implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents:
37
diff
changeset
|
119 | |
9857c18c5e64
implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents:
37
diff
changeset
|
120 | |
9857c18c5e64
implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents:
37
diff
changeset
|
121 | |
3 | 122 | // setup data packet timer |
41
9857c18c5e64
implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents:
37
diff
changeset
|
123 | TCCR1B = (1<<CS11) | (1<<WGM12); //divide by 8, set compare match (1mhz clock) |
42
018d4d63ad3b
implemented 500ns timer and set jumpstart time
Malte Bayer <mbayer@neo-soft.org>
parents:
41
diff
changeset
|
124 | OCR1A = TIMER1_500NS; |
018d4d63ad3b
implemented 500ns timer and set jumpstart time
Malte Bayer <mbayer@neo-soft.org>
parents:
41
diff
changeset
|
125 | //OCR1A = TIMER1_7500NS; |
69
27c0c0095e26
implemented responsewire in CU firmware
Malte Bayer <mbayer@neo-soft.org>
parents:
42
diff
changeset
|
126 | TIMSK |= _BV(OCIE1A); //enable timer1 interrupt |
3 | 127 | |
37
136a79772098
implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents:
34
diff
changeset
|
128 | RS232_puts_p(PSTR("FreeSlot Blackbox v")); |
136a79772098
implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents:
34
diff
changeset
|
129 | RS232_puts(VERSION); |
136a79772098
implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents:
34
diff
changeset
|
130 | RS232_putc('\n'); |
3 | 131 | |
132 | } | |
133 | ||
134 | ||
135 | ||
69
27c0c0095e26
implemented responsewire in CU firmware
Malte Bayer <mbayer@neo-soft.org>
parents:
42
diff
changeset
|
136 | uint8_t check_rails_shortcut(void) { |
2
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
137 | // check for short circuit on the rails |
10 | 138 | uint8_t i = 100; |
2
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
139 | if ((PIN(RAIL_DETECT_PORT) & _BV(RAIL_DETECT)) == 0) { |
10 | 140 | while (i>0) { |
141 | if ((PIN(RAIL_DETECT_PORT) & _BV(RAIL_DETECT)) != 0) return 0; | |
142 | _delay_us(20); | |
33 | 143 | i--; |
10 | 144 | } |
2
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
145 | if ((PIN(RAIL_DETECT_PORT) & _BV(RAIL_DETECT)) == 0) { |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
146 | cli(); // disable ALL Interrupts |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
147 | RAIL_POWER_PORT &= ~_BV(RAIL_POWER); // disable rails power |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
148 | RS232_puts_p(PSTR("!!! SHORT CIRCUIT ON RAILS, POWERED OFF !!!\n")); |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
149 | LED(1, 1); |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
150 | LED(2, 1); |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
151 | LED(3, 0); |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
152 | LED(4, 0); |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
153 | LED(5, 0); |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
154 | while (1) { |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
155 | LED(1, 2); |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
156 | LED(2, 2); |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
157 | LED(4, 2); |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
158 | LED(5, 2); |
33 | 159 | blinkdelay(); |
2
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
160 | LED(3, 2); |
33 | 161 | blinkdelay(); |
2
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
162 | LED(3, 2); |
33 | 163 | blinkdelay(); |
2
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
164 | LED(3, 2); |
33 | 165 | blinkdelay(); |
2
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
166 | LED(3, 2); |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
167 | } |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
168 | } |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
169 | } |
69
27c0c0095e26
implemented responsewire in CU firmware
Malte Bayer <mbayer@neo-soft.org>
parents:
42
diff
changeset
|
170 | return 0; |
2
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
171 | } |