blackbox/main.c

Tue, 15 Nov 2011 10:47:16 +0100

author
Malte Bayer <mbayer@neo-soft.org>
date
Tue, 15 Nov 2011 10:47:16 +0100
changeset 5
7319de27d44a
parent 4
58529e14c45c
child 6
2405aff29a51
permissions
-rw-r--r--

added queue busy response on command injection

0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
1 #include <avr/interrupt.h>
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
2 #include <avr/io.h>
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
3 #include <avr/wdt.h>
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
4 #include <avr/eeprom.h>
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
5 #include <stdlib.h>
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
6 #include <stdint.h>
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
7 #include <avr/pgmspace.h>
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
8 #include <util/delay.h>
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
9
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
10 #include "driver/rs232.h"
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
11 #include "driver/adc.h"
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
12
2
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
13 #include "main.h"
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
14 #include "lowlevel.h"
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
15
4
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
16 volatile uint8_t program_count = 0;
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
17 volatile uint8_t program_id;
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
18 volatile uint8_t program_command;
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
19 volatile uint8_t program_parameter;
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
20
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
21 volatile uint8_t datalen = 0;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
22 char data[10]; // 8 bytes data buffer + string termination
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
23
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
24 static char buffer[RS232_BUFSIZE+1];
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
25 static uint8_t buffer_len;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
26
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
27 // USART0 RX interrupt
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
28 ISR ( USART_RXC_vect ) {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
29 char c = UDR;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
30
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
31 // check for buffer overflow
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
32 if (buffer_len==sizeof(buffer)) {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
33 buffer_len=0;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
34 } else {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
35 // collect characters until end of line
4
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
36 if ( (c==0x0A) ) {
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
37 buffer[buffer_len]=0;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
38
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
39 // packet end received, parse the received packet
4
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
40 switch (buffer[0]) {
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
41 case 'P': // inject a program data word to the rails
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
42 // TODO: at the moment only parameters 0..9 supported
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
43 // needs to build a "human" parser
5
7319de27d44a added queue busy response on command injection
Malte Bayer <mbayer@neo-soft.org>
parents: 4
diff changeset
44 if (program_count == 0) {
7319de27d44a added queue busy response on command injection
Malte Bayer <mbayer@neo-soft.org>
parents: 4
diff changeset
45 program_id = buffer[3]-'0';
7319de27d44a added queue busy response on command injection
Malte Bayer <mbayer@neo-soft.org>
parents: 4
diff changeset
46 program_command = buffer[1]-'0';
7319de27d44a added queue busy response on command injection
Malte Bayer <mbayer@neo-soft.org>
parents: 4
diff changeset
47 program_parameter = buffer[2]-'0';
7319de27d44a added queue busy response on command injection
Malte Bayer <mbayer@neo-soft.org>
parents: 4
diff changeset
48 if (program_command < 4)
7319de27d44a added queue busy response on command injection
Malte Bayer <mbayer@neo-soft.org>
parents: 4
diff changeset
49 program_count = 0x02; // send commands twice (fuel, speed, brake)
7319de27d44a added queue busy response on command injection
Malte Bayer <mbayer@neo-soft.org>
parents: 4
diff changeset
50 else
7319de27d44a added queue busy response on command injection
Malte Bayer <mbayer@neo-soft.org>
parents: 4
diff changeset
51 program_count = 0x01;
7319de27d44a added queue busy response on command injection
Malte Bayer <mbayer@neo-soft.org>
parents: 4
diff changeset
52 RS232_puts_p(PSTR("OK\n"));
7319de27d44a added queue busy response on command injection
Malte Bayer <mbayer@neo-soft.org>
parents: 4
diff changeset
53 } else RS232_puts_p(PSTR("BUSY\n"));
7319de27d44a added queue busy response on command injection
Malte Bayer <mbayer@neo-soft.org>
parents: 4
diff changeset
54 break;
4
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
55
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
56 }
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
57
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
58 // wait for the next packet
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
59 buffer_len=0;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
60 } else {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
61 buffer[buffer_len++]=c;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
62 }
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
63 }
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
64 }
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
65
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
66
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
67 volatile uint16_t transmit_buffer;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
68 volatile uint16_t transmit_buffer_queue;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
69 volatile uint8_t transmit_len;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
70 volatile uint8_t transmit_len_next;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
71 volatile uint8_t transmit_len_queue;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
72 ISR ( TIMER2_COMP_vect ) {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
73 //OCR2 = TIMER2_50US; // make sure that timer2 is 50µs !!!
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
74 // data packet timer 100µs pro bit...
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
75 if (transmit_len >= 0xFE) {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
76 if (transmit_len != 0xFF) {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
77 RAIL_POWER_PORT |= _BV(RAIL_POWER); // end of transmission
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
78 transmit_len = 0xFF;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
79 transmit_buffer = transmit_buffer_queue;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
80 transmit_buffer_queue = 0;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
81 transmit_len_next = transmit_len_queue;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
82 }
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
83 } else {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
84 uint16_t bit = (1<<(transmit_len & 0b01111111));
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
85 uint16_t clock;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
86 if ((transmit_len & 0b10000000) == 0) clock = 0; else clock = 0xffff;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
87 if ( ((transmit_buffer ^ clock) & bit) != 0 )
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
88 RAIL_POWER_PORT |= _BV(RAIL_POWER); else
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
89 RAIL_POWER_PORT &= ~_BV(RAIL_POWER);
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
90 if ( (transmit_len & 0b10000000) == 0 ) {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
91 // block 0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
92 //if (transmit_len == 0) transmit_len = 0xFF; else transmit_len |= 0b10000000; // set clock
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
93 transmit_len |= 0b10000000; // set clock
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
94 } else {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
95 // block 1, output the current bit
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
96 transmit_len &= 0b01111111; // reset clock
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
97 //if (transmit_len != 0) transmit_len--; // next bit
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
98 if (transmit_len == 0) transmit_len = 0xFE; else transmit_len--; // next bit
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
99 }
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
100 }
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
101 }
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
102
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
103 int insert_queue(uint16_t tmp, uint8_t len) {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
104 if (transmit_buffer_queue == 0) {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
105 transmit_buffer_queue = tmp;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
106 transmit_len_queue = len;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
107 return 1;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
108 }
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
109 return 0;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
110 }
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
111
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
112
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
113 int do_controller(uint8_t controller) {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
114 // read controller X speed & encode controller data packet
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
115 uint16_t tmp;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
116 switch (controller) {
3
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
117 case 0:
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
118 tmp = ((getADC(CONTROLLER1_SPEED) / CONTROLLER_DIVISOR) & 0xF) << 1;
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
119 if ( (PIN(CONTROLLER_PORT) & _BV(CONTROLLER1_SW)) != 0) tmp |= (1<<5);
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
120 break;
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
121 case 1:
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
122 tmp = ((getADC(CONTROLLER2_SPEED) / CONTROLLER_DIVISOR) & 0xF) << 1;
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
123 if ( (PIN(CONTROLLER_PORT) & _BV(CONTROLLER2_SW)) != 0) tmp |= (1<<5);
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
124 break;
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
125 case 2:
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
126 tmp = ((getADC(CONTROLLER3_SPEED) / CONTROLLER_DIVISOR) & 0xF) << 1;
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
127 if ( (PIN(CONTROLLER_PORT) & _BV(CONTROLLER3_SW)) != 0) tmp |= (1<<5);
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
128 break;
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
129 case 3:
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
130 tmp = ((getADC(CONTROLLER4_SPEED) / CONTROLLER_DIVISOR) & 0xF) << 1;
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
131 if ( (PIN(CONTROLLER_PORT) & _BV(CONTROLLER4_SW)) != 0) tmp |= (1<<5);
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
132 break;
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
133 case 4: tmp = (1<<5); break; // todo regler 5
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
134 case 5: tmp = (1<<5); break; // todo regler 6
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
135 }
3
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
136 tmp |= 0b1000000000 | (controller << 6);
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
137 if ( (PIN(SW_FUEL_PORT) & _BV(SW_FUEL)) != 0) tmp |= 1; // benzinstand aktiv - tankmodusschalter
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
138
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
139 return insert_queue(tmp, 9);
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
140 }
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
141
4
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
142 uint8_t mirror( uint8_t n ) {
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
143 n = ((n >> 1) & 0x55) | ((n << 1) & 0xaa);
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
144 n = ((n >> 2) & 0x33) | ((n << 2) & 0xcc);
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
145 n = ((n >> 4) & 0x0f) | ((n << 4) & 0xf0);
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
146 return n;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
147 }
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
148
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
149 int do_program(uint8_t controller, uint8_t command, uint8_t parameter) {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
150 // send program data packet
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
151 uint16_t tmp;
4
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
152 parameter = mirror(parameter);
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
153 controller = mirror(controller);
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
154 command = mirror(command);
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
155 tmp = 0b1000000000000 | (parameter << 4) | command | (controller >> 5);
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
156 return insert_queue(tmp, 12);
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
157 }
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
158
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
159 int do_active(void) {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
160 // send controller active data packet
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
161 uint16_t tmp = 0b10000000;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
162 if ((getADC(CONTROLLER1_SPEED) / CONTROLLER_DIVISOR) > 0) tmp |= 0b11000001;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
163 if ((getADC(CONTROLLER2_SPEED) / CONTROLLER_DIVISOR) > 0) tmp |= 0b10100001;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
164 if ((getADC(CONTROLLER3_SPEED) / CONTROLLER_DIVISOR) > 0) tmp |= 0b10010001;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
165 if ((getADC(CONTROLLER4_SPEED) / CONTROLLER_DIVISOR) > 0) tmp |= 0b10001001;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
166 // todo: regler 5 und 6
4
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
167 // todo: wenn Daten enpfangen wurden hier eine Quittierung senden anstatt dem Active Word
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
168
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
169 return insert_queue(tmp, 7);
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
170 }
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
171
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
172 int do_pace_ghost(void) {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
173 // send ghost and pacecar data packet
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
174 // todo: at the moment, both disabled!
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
175 uint16_t tmp = 0b1111100000;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
176 if ( (PIN(SW_FUEL_PORT) & _BV(SW_FUEL)) != 0) tmp |= 1; // benzinstand aktiv - tankmodusschalter
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
177 // todo: PC, NH, TK, (KFR, FR)
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
178
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
179 return insert_queue(tmp, 9);
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
180 }
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
181
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
182 ISR ( TIMER1_COMPA_vect ) {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
183 //OCR2 = 0xFF; // make sure that timer2 is synced to timer1, give enough cycles to prepare
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
184 LED(4,2);
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
185 // trigger packet transfer:
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
186 transmit_len = transmit_len_next;
2
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
187
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
188 // here is some more time to do something else...
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
189 }
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
190
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
191
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
192 int main(void)
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
193 {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
194
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
195 unsigned char s[30];
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
196 uint16_t tmp;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
197 uint8_t packet_index = 1;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
198
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
199
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
200 init_hardware();
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
201
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
202 // switch on rails power
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
203 RAIL_POWER_PORT |= _BV(RAIL_POWER);
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
204
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
205 while (1) {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
206 // check for short circuit on the rails
2
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
207 check_rails_shortcut();
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
208
2
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
209 switch (packet_index) {
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
210 case 1:
4
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
211 if (program_count > 0) {
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
212 // command in queue
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
213 if (do_program(program_id, program_command, program_parameter)) {
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
214 packet_index++;
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
215 program_count--;
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
216 }
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
217 } else {
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
218 // output idle command
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
219 if (do_program(7, 19, 0)) packet_index++; // reset
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
220 }
2
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
221 break;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
222 case 2:
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
223 if (do_pace_ghost()) packet_index++;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
224 break;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
225 case 3:
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
226 if (do_active()) packet_index++;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
227 break;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
228 case 4:
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
229 if (do_controller(0)) packet_index++;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
230 break;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
231 case 5:
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
232 if (do_controller(4)) packet_index++;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
233 break;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
234 case 6:
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
235 if (do_controller(1)) packet_index++;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
236 break;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
237 case 7:
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
238 if (do_controller(5)) packet_index++;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
239 break;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
240 case 8:
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
241 if (do_controller(2)) packet_index++;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
242 break;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
243 case 9:
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
244 if (do_active()) packet_index++;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
245 break;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
246 case 10:
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
247 if (do_controller(3)) packet_index = 1;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
248 // last packet, so reset packet index
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
249 break;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
250 }
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
251
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
252
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
253 } // main loop end
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
254 };
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
255

mercurial