5 #include <stdlib.h> |
5 #include <stdlib.h> |
6 #include <stdint.h> |
6 #include <stdint.h> |
7 #include <avr/pgmspace.h> |
7 #include <avr/pgmspace.h> |
8 #include <util/delay.h> |
8 #include <util/delay.h> |
9 |
9 |
10 #include "main.h" |
|
11 |
|
12 #include "driver/rs232.h" |
10 #include "driver/rs232.h" |
13 #include "driver/adc.h" |
11 #include "driver/adc.h" |
14 |
12 |
|
13 #include "main.h" |
15 #include "lowlevel.h" |
14 #include "lowlevel.h" |
16 |
|
17 //#include "driver/manchester.h" |
|
18 |
|
19 /* |
|
20 // Hardware config |
|
21 #define LAP_COUNTER_PORT PORTB |
|
22 #define LAP_COUNTER PB2 |
|
23 |
|
24 #define MODUL_PORT PORTD |
|
25 #define MODUL_ST4 PD5 |
|
26 #define MODUL_ST6 PD6 |
|
27 |
|
28 #define I2C_PORT PORTC |
|
29 #define I2C_SDA PC0 |
|
30 #define I2C_SCL PC1 |
|
31 |
|
32 |
|
33 #define SW_PACECAR_PORT PORTC |
|
34 #define SW_START_PORT PORTB |
|
35 #define SW_TANK_PORT PORTB |
|
36 #define SW_PACECAR PC6 |
|
37 #define SW_TANK PB0 |
|
38 #define SW_START PB1 |
|
39 |
|
40 #define SPEAKER_PORT PORTD |
|
41 #define SPEAKER PD7 |
|
42 |
|
43 */ |
|
44 |
|
45 |
15 |
46 void init_hardware(void) { |
16 void init_hardware(void) { |
47 RS232_init(); // initialize RS485 interface |
17 RS232_init(); // initialize RS485 interface |
48 RS232_puts_p(PSTR("CARRERA beta loading\n")); |
18 RS232_puts_p(PSTR("CARRERA beta loading\n")); |
49 |
19 |
243 // todo: PC, NH, TK, (KFR, FR) |
213 // todo: PC, NH, TK, (KFR, FR) |
244 |
214 |
245 return insert_queue(tmp, 9); |
215 return insert_queue(tmp, 9); |
246 } |
216 } |
247 |
217 |
248 /* |
|
249 TODO: DIE TIMER1 ISR DARF DIE ADC NICHT ABFRAGEN, NICHTMAL PAKETE BAUEN, NUR TRIGGERN |
|
250 DIE PAKETE MÜSSEN IN DER MAIN UNIT GEBAUT WERDEN UND IN EINE QUEUE GESCHOBEN WERDEN |
|
251 EINES VORBAUEN IN Q+1, DIE TIMER2 ISR MUSS DANN Q = Q+1 und Q+1 = 0 SETZEN, ERST DANN DARF |
|
252 NEUES PAKET IN Q+1 EINGEFÜGT WERDEN |
|
253 */ |
|
254 |
|
255 |
|
256 ISR ( TIMER1_COMPA_vect ) { |
218 ISR ( TIMER1_COMPA_vect ) { |
257 //OCR2 = 0xFF; // make sure that timer2 is synced to timer1, give enough cycles to prepare |
219 //OCR2 = 0xFF; // make sure that timer2 is synced to timer1, give enough cycles to prepare |
258 LED(4,2); |
220 LED(4,2); |
259 // trigger packet transfer: |
221 // trigger packet transfer: |
260 transmit_len = transmit_len_next; |
222 transmit_len = transmit_len_next; |
261 |
223 |
262 // here is some more time to do something else... |
224 // here is some more time to do something else... |
263 } |
225 } |
264 |
226 |
265 |
227 |
266 int main(void) |
228 int main(void) |
278 |
240 |
279 // switch on rails power |
241 // switch on rails power |
280 RAIL_POWER_PORT |= _BV(RAIL_POWER); |
242 RAIL_POWER_PORT |= _BV(RAIL_POWER); |
281 |
243 |
282 while (1) { |
244 while (1) { |
283 //RS232_putc(PIN(RAIL_DETECT_PORT)); |
|
284 |
|
285 // check for short circuit on the rails |
245 // check for short circuit on the rails |
286 if ((PIN(RAIL_DETECT_PORT) & _BV(RAIL_DETECT)) == 0) { |
246 check_rails_shortcut(); |
287 _delay_ms(1); |
247 |
288 if ((PIN(RAIL_DETECT_PORT) & _BV(RAIL_DETECT)) == 0) { |
248 switch (packet_index) { |
289 cli(); // disable ALL Interrupts |
249 case 1: |
290 RAIL_POWER_PORT &= ~_BV(RAIL_POWER); // disable rails power |
250 if (do_program(7, 19, 0)) packet_index++; // reset |
291 RS232_puts_p(PSTR("!!! SHORT CIRCUIT ON RAILS, POWERED OFF !!!\n")); |
251 //do_program(7, 16, 3); // led an startampel |
292 while (1) { |
252 //do_program(0, 4, 0); // |
293 LED(1, 0); |
253 break; |
294 _delay_ms(100); |
254 case 2: |
295 LED(1, 1); |
255 if (do_pace_ghost()) packet_index++; |
296 _delay_ms(100); |
256 break; |
297 } |
257 case 3: |
298 } |
258 if (do_active()) packet_index++; |
299 } |
259 break; |
300 |
260 case 4: |
301 |
261 if (do_controller(0)) packet_index++; |
302 |
262 break; |
303 |
263 case 5: |
304 switch (packet_index) { |
264 if (do_controller(4)) packet_index++; |
305 case 1: |
265 break; |
306 if (do_program(7, 19, 0)) packet_index++; // reset |
266 case 6: |
307 //do_program(7, 16, 3); // led an startampel |
267 if (do_controller(1)) packet_index++; |
308 //do_program(0, 4, 0); // |
268 break; |
309 break; |
269 case 7: |
310 case 2: |
270 if (do_controller(5)) packet_index++; |
311 if (do_pace_ghost()) packet_index++; |
271 break; |
312 break; |
272 case 8: |
313 case 3: |
273 if (do_controller(2)) packet_index++; |
314 if (do_active()) packet_index++; |
274 break; |
315 break; |
275 case 9: |
316 case 4: |
276 if (do_active()) packet_index++; |
317 if (do_controller(0)) packet_index++; |
277 break; |
318 break; |
278 case 10: |
319 case 5: |
279 if (do_controller(3)) packet_index = 1; |
320 if (do_controller(4)) packet_index++; |
280 // last packet, so reset packet index |
321 break; |
281 break; |
322 case 6: |
282 } |
323 if (do_controller(1)) packet_index++; |
|
324 break; |
|
325 case 7: |
|
326 if (do_controller(5)) packet_index++; |
|
327 break; |
|
328 case 8: |
|
329 if (do_controller(2)) packet_index++; |
|
330 break; |
|
331 case 9: |
|
332 if (do_active()) packet_index++; |
|
333 break; |
|
334 case 10: |
|
335 if (do_controller(3)) packet_index = 1; |
|
336 // last packet, so reset packet index |
|
337 break; |
|
338 } |
|
339 |
|
340 |
|
341 |
|
342 |
|
343 /* |
|
344 do_controller(0); |
|
345 itoa (transmit_len_queue, s, 10); |
|
346 RS232_puts(s); |
|
347 RS232_putc(' '); |
|
348 itoa (transmit_buffer_queue, s, 2); |
|
349 RS232_puts(s); |
|
350 |
|
351 RS232_putc('\n'); |
|
352 */ |
|
353 /* |
|
354 itoa (tmp, s, 2); |
|
355 RS232_puts(s); |
|
356 RS232_putc('\n'); |
|
357 */ |
|
358 |
283 |
359 |
284 |
360 } // main loop end |
285 } // main loop end |
361 }; |
286 }; |
362 |
287 |