Fri, 18 Nov 2011 12:16:22 +0100
added carrerashark project-part to separate subdirectory
9 | 1 | #include <avr/interrupt.h> |
2 | #include <avr/io.h> | |
3 | #include <avr/wdt.h> | |
4 | #include <avr/eeprom.h> | |
5 | #include <stdlib.h> | |
6 | #include <stdint.h> | |
7 | #include <avr/pgmspace.h> | |
8 | ||
9 | #include "main.h" | |
10 | ||
11 | #include "driver/rs232.h" | |
11
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
12 | #include "util/delay.h" |
9 | 13 | |
14 | ||
15 | static char buffer[RS232_BUFSIZE+1]; | |
16 | static uint8_t buffer_len; | |
17 | ||
18 | ||
19 | ||
20 | ||
21 | // USART0 RX interrupt | |
22 | ISR ( USART_RXC_vect ) { | |
23 | char c = UDR; | |
24 | ||
25 | // check for buffer overflow | |
26 | if (buffer_len==sizeof(buffer)) { | |
27 | buffer_len=0; | |
28 | if (c == 27) { | |
29 | // escape sequence, store to empty buffer | |
30 | buffer[buffer_len++] = c; | |
31 | } | |
32 | } else { | |
33 | // collect characters until end of line | |
34 | if (c == 27) { | |
35 | // escape sequence, clear buffer | |
36 | buffer_len = 0; | |
37 | buffer[buffer_len++] = c; | |
38 | } else if ( (c==0xff) && (buffer_len > 3) ) { | |
39 | buffer[buffer_len]=0; | |
40 | ||
41 | // packet end received, parse the received packet | |
42 | ||
43 | // wait for the next packet | |
44 | buffer_len=0; | |
45 | } else { | |
46 | buffer[buffer_len++]=c; | |
47 | } | |
48 | } | |
49 | } | |
50 | ||
51 | ||
11
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
52 | #define PULSE_PORT PORTD |
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
53 | #define PULSE_BIT PD2 |
9 | 54 | |
11
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
55 | volatile uint16_t data = 0; |
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
56 | volatile uint8_t data_len = 0; |
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
57 | volatile uint8_t bitbuf_len = 0; |
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
58 | volatile uint16_t bitbuf = 0; |
9 | 59 | |
60 | ISR ( INT0_vect ) { | |
11
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
61 | writeBit(PORTD, 5, 1); |
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
62 | GICR &= ~_BV(INT0) ; // Disable INT0 |
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
63 | // Startsignal erkannt, ab hier den Timer2 starten, |
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
64 | // der liest dann alle 50µs den Zustand ein und schreibt das |
9 | 65 | // empfangene Bit in den Puffer |
11
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
66 | bitbuf = 0; // init |
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
67 | bitbuf_len = 0b10000000; // init 1 pulse received |
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
68 | TCNT2 = 0; |
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
69 | TIMSK |= _BV(OCIE2); //enable timer2 interrupt |
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
70 | writeBit(PORTD, 5, 0); |
9 | 71 | } |
72 | ||
11
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
73 | |
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
74 | ISR ( TIMER2_COMP_vect ) { |
9 | 75 | writeBit(PORTD, 4, 0); |
11
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
76 | |
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
77 | uint8_t clock; |
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
78 | uint8_t state; |
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
79 | uint8_t state2; |
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
80 | if ((bitbuf_len & 0b10000000) == 0) clock = 0; else clock = 0xff; |
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
81 | if ((bitbuf_len & 0b01000000) == 0) state = 0; else state = 0xff; |
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
82 | if ((PIN(PULSE_PORT) & _BV(PULSE_BIT)) == 0) state2 = 0; else state2 = 0xff; |
9 | 83 | |
11
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
84 | if (clock) { |
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
85 | // second pulse of bit |
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
86 | bitbuf_len &= ~_BV(7); // switch clock to low |
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
87 | if ((state==state2) & state2) { |
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
88 | // two cycles high: packet end received |
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
89 | data_len = (bitbuf_len & 0b00111111); |
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
90 | if (data_len == 13) PORTD ^= _BV(6); // debug sync output on program packets |
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
91 | TIMSK &= ~_BV(OCIE2); //disable timer2 interrupt |
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
92 | GICR |= _BV(INT0) ; // Enable INT0 |
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
93 | data = bitbuf; // output data |
9 | 94 | } else { |
11
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
95 | bitbuf_len++; // increment bit counter |
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
96 | bitbuf = bitbuf << 1; // shift bits |
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
97 | if (state2 == 0) bitbuf |= 1; // receive logic one |
9 | 98 | } |
99 | } else { | |
11
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
100 | // first pulse of bit |
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
101 | bitbuf_len |= _BV(7); // switch clock to high |
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
102 | if (state2) { |
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
103 | bitbuf_len |= _BV(6); // store new state |
9 | 104 | } else { |
11
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
105 | bitbuf_len &= ~_BV(6); // store new state |
9 | 106 | } |
107 | } | |
11
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
108 | |
9 | 109 | |
110 | writeBit(PORTD, 4, 1); | |
111 | } | |
112 | ||
113 | ||
114 | ||
115 | int main(void) | |
116 | { | |
11
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
117 | uint8_t i; |
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
118 | unsigned char s[30]; |
9 | 119 | |
11
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
120 | // setup data bit timer |
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
121 | TCCR2 = (1<<CS21) | (1<<WGM21); //divide by 8, set compare match |
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
122 | OCR2 = TIMER2_50US; |
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
123 | TIMSK |= 1<<OCIE2; //enable timer2 interrupt |
9 | 124 | |
11
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
125 | MCUCR = _BV(ISC00); // falling edge |
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
126 | GICR = _BV(INT0) ; // Enable INT0 |
9 | 127 | |
128 | writeBit(DDRD, 4, 1); | |
11
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
129 | writeBit(DDRD, 5, 1); |
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
130 | writeBit(DDRD, 6, 1); |
9 | 131 | |
132 | ||
10 | 133 | RS232_init(); // initialize RS232 interface |
11
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
134 | RS232_puts_p(PSTR("CarreraShark 1.0 - INIT OK\n")); |
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
135 | //RS232_puts_p(PSTR("Receiving one complete cycle:\n")); |
9 | 136 | |
11
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
137 | sei(); |
9 | 138 | i = 0; |
139 | while (1) { | |
140 | // main loop | |
141 | ||
11
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
142 | if (data != 0) { |
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
143 | if (data_len > 5) { |
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
144 | if (data_len == 13) { // sync to first packet |
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
145 | i = 1; |
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
146 | RS232_puts("\n"); |
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
147 | } else if (i!=0) i++; |
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
148 | if (i>0) { |
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
149 | itoa( data, s, 16); |
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
150 | data = 0; |
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
151 | RS232_puts("0x"); |
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
152 | RS232_puts( s ); |
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
153 | RS232_putc(' '); |
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
154 | } |
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
155 | } |
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
156 | //if (i==10) for (;;); |
9 | 157 | } |
158 | } // main loop end | |
159 | }; | |
160 |