Rev 790 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 790 | Rev 898 | ||
---|---|---|---|
1 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 | // + Copyright (c) 04.2007 Holger Buss |
2 | // + Copyright (c) 04.2007 Holger Buss |
3 | // + only for non-profit use |
3 | // + only for non-profit use |
4 | // + www.MikroKopter.com |
4 | // + www.MikroKopter.com |
5 | // + see the File "License.txt" for further Informations |
5 | // + see the File "License.txt" for further Informations |
6 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
6 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
7 | 7 | ||
8 | #include <avr/io.h> |
8 | #include <avr/io.h> |
9 | #include <avr/interrupt.h> |
9 | #include <avr/interrupt.h> |
10 | #include <avr/wdt.h> |
10 | #include <avr/wdt.h> |
11 | 11 | ||
12 | #include "eeprom.h" |
12 | #include "eeprom.h" |
13 | #include "main.h" |
13 | #include "main.h" |
14 | #include "menu.h" |
14 | #include "menu.h" |
15 | #include "timer0.h" |
15 | #include "timer0.h" |
16 | #include "uart.h" |
16 | #include "uart.h" |
17 | #include "fc.h" |
17 | #include "fc.h" |
18 | #include "_Settings.h" |
18 | #include "_Settings.h" |
19 | #include "rc.h" |
19 | #include "rc.h" |
20 | #include "ubx.h" |
20 | #include "ubx.h" |
21 | 21 | ||
22 | 22 | ||
23 | 23 | ||
24 | 24 | ||
25 | #define FALSE 0 |
25 | #define FALSE 0 |
26 | #define TRUE 1 |
26 | #define TRUE 1 |
27 | 27 | ||
28 | //int8_t test __attribute__ ((section (".noinit"))); |
28 | //int8_t test __attribute__ ((section (".noinit"))); |
29 | 29 | ||
30 | uint8_t DebugGetRequest = 0, DebugDisplayRequest = 0, DebugDataRequest = 0, GetVersionRequest = 0; |
30 | uint8_t DebugGetRequest = 0, DebugDisplayRequest = 0, DebugDataRequest = 0, GetVersionRequest = 0; |
31 | 31 | ||
32 | volatile uint8_t txd_buffer[TXD_BUFFER_LEN]; |
32 | volatile uint8_t txd_buffer[TXD_BUFFER_LEN]; |
33 | volatile uint8_t rxd_buffer_locked = FALSE; |
33 | volatile uint8_t rxd_buffer_locked = FALSE; |
34 | volatile uint8_t rxd_buffer[RXD_BUFFER_LEN]; |
34 | volatile uint8_t rxd_buffer[RXD_BUFFER_LEN]; |
35 | volatile uint8_t txd_complete = TRUE; |
35 | volatile uint8_t txd_complete = TRUE; |
36 | volatile uint8_t ReceivedBytes = 0; |
36 | volatile uint8_t ReceivedBytes = 0; |
37 | 37 | ||
38 | uint8_t RemotePollDisplayLine = 0; |
38 | uint8_t RemotePollDisplayLine = 0; |
39 | uint8_t NurKanalAnforderung = 0; |
39 | uint8_t NurKanalAnforderung = 0; |
40 | uint8_t DebugTextAnforderung = 255; |
40 | uint8_t DebugTextAnforderung = 255; |
41 | uint8_t PcAccess = 100; |
41 | uint8_t PcAccess = 100; |
42 | uint8_t MotorTest[4] = {0,0,0,0}; |
42 | uint8_t MotorTest[4] = {0,0,0,0}; |
43 | uint8_t DubWiseKeys[4] = {0,0,0,0}; |
43 | uint8_t DubWiseKeys[4] = {0,0,0,0}; |
44 | uint8_t MySlaveAddr = 0; |
44 | uint8_t MySlaveAddr = 0; |
45 | uint8_t ConfirmFrame; |
45 | uint8_t ConfirmFrame; |
46 | 46 | ||
47 | DebugOut_t DebugOut; |
47 | DebugOut_t DebugOut; |
48 | ExternControl_t ExternControl; |
48 | ExternControl_t ExternControl; |
49 | VersionInfo_t VersionInfo; |
49 | VersionInfo_t VersionInfo; |
50 | 50 | ||
51 | const uint8_t ANALOG_TEXT[32][16] = |
51 | const uint8_t ANALOG_TEXT[32][16] = |
52 | { |
52 | { |
53 | //1234567890123456 |
53 | //1234567890123456 |
54 | "IntegralPitch ", //0 |
54 | "IntegralPitch ", //0 |
55 | "IntegralRoll ", |
55 | "IntegralRoll ", |
56 | "AccPitch ", |
56 | "AccPitch ", |
57 | "AccRoll ", |
57 | "AccRoll ", |
58 | "GyroYaw ", |
58 | "GyroYaw ", |
59 | "ReadingHeight ", //5 |
59 | "ReadingHeight ", //5 |
60 | "AccZ ", |
60 | "AccZ ", |
61 | "Thrust ", |
61 | "Thrust ", |
62 | "CompassHeading ", |
62 | "CompassHeading ", |
63 | "Voltage ", |
63 | "Voltage ", |
64 | "Receiver Level ", //10 |
64 | "Receiver Level ", //10 |
65 | "AnalogOut11 ", |
65 | "AnalogOut11 ", |
66 | "GPSDevNorth ", |
66 | "Motor VL ", |
67 | "GPSDevEast ", |
67 | "Motor RR ", |
68 | "GPS_Pitch ", |
68 | "Motor VR ", |
69 | "GPS_Roll ", //15 |
69 | "Motor RL ", //15 |
70 | "Acc_Z ", |
70 | "Motor Left ", |
71 | "MeanAccPitch ", |
71 | "Motor Right ", |
72 | "MeanAccRoll ", |
72 | "MeanAccRoll ", |
73 | "IntegralErrPitch", |
73 | "IntegralErrPitch", |
74 | "IntegralErrRoll ", //20 |
74 | "IntegralErrRoll ", //20 |
75 | "MeanIntPitch ", |
75 | "MeanIntPitch ", |
76 | "MeanIntRoll ", |
76 | "MeanIntRoll ", |
77 | "NeutralPitch ", |
77 | "NeutralPitch ", |
78 | "RollOffset ", |
78 | "RollOffset ", |
79 | "IntRoll*Factor ", //25 |
79 | "IntRoll*Factor ", //25 |
80 | "ReadingGyroPitch", |
80 | "ReadingGyroPitch", |
81 | "DirectCorrRoll ", |
81 | "DirectCorrRoll ", |
82 | "ReadingGyroRoll ", |
82 | "ReadingGyroRoll ", |
83 | "CorrectionRoll ", |
83 | "CorrectionRoll ", |
84 | "I-AttRoll ", //30 |
84 | "I-AttRoll ", //30 |
85 | "StickRoll " |
85 | "StickRoll " |
86 | }; |
86 | }; |
87 | 87 | ||
88 | 88 | ||
89 | 89 | ||
90 | /****************************************************************/ |
90 | /****************************************************************/ |
91 | /* Initialization of the USART0 */ |
91 | /* Initialization of the USART0 */ |
92 | /****************************************************************/ |
92 | /****************************************************************/ |
93 | void USART0_Init (void) |
93 | void USART0_Init (void) |
94 | { |
94 | { |
95 | uint8_t sreg = SREG; |
95 | uint8_t sreg = SREG; |
96 | uint16_t ubrr = (uint16_t) ((uint32_t) SYSCLK/(8 * USART0_BAUD) - 1); |
96 | uint16_t ubrr = (uint16_t) ((uint32_t) SYSCLK/(8 * USART0_BAUD) - 1); |
97 | 97 | ||
98 | // disable all interrupts before configuration |
98 | // disable all interrupts before configuration |
99 | cli(); |
99 | cli(); |
100 | 100 | ||
101 | // disable RX-Interrupt |
101 | // disable RX-Interrupt |
102 | UCSR0B &= ~(1 << RXCIE0); |
102 | UCSR0B &= ~(1 << RXCIE0); |
103 | // disable TX-Interrupt |
103 | // disable TX-Interrupt |
104 | UCSR0B &= ~(1 << TXCIE0); |
104 | UCSR0B &= ~(1 << TXCIE0); |
105 | 105 | ||
106 | // set direction of RXD0 and TXD0 pins |
106 | // set direction of RXD0 and TXD0 pins |
107 | // set RXD0 (PD0) as an input pin |
107 | // set RXD0 (PD0) as an input pin |
108 | PORTD |= (1 << PORTD0); |
108 | PORTD |= (1 << PORTD0); |
109 | DDRD &= ~(1 << DDD0); |
109 | DDRD &= ~(1 << DDD0); |
110 | // set TXD0 (PD1) as an output pin |
110 | // set TXD0 (PD1) as an output pin |
111 | PORTD |= (1 << PORTD1); |
111 | PORTD |= (1 << PORTD1); |
112 | DDRD |= (1 << DDD1); |
112 | DDRD |= (1 << DDD1); |
113 | 113 | ||
114 | // USART0 Baud Rate Register |
114 | // USART0 Baud Rate Register |
115 | // set clock divider |
115 | // set clock divider |
116 | UBRR0H = (uint8_t)(ubrr >> 8); |
116 | UBRR0H = (uint8_t)(ubrr >> 8); |
117 | UBRR0L = (uint8_t)ubrr; |
117 | UBRR0L = (uint8_t)ubrr; |
118 | 118 | ||
119 | // USART0 Control and Status Register A, B, C |
119 | // USART0 Control and Status Register A, B, C |
120 | 120 | ||
121 | // enable double speed operation in |
121 | // enable double speed operation in |
122 | UCSR0A |= (1 << U2X0); |
122 | UCSR0A |= (1 << U2X0); |
123 | // enable receiver and transmitter in |
123 | // enable receiver and transmitter in |
124 | UCSR0B = (1 << TXEN0) | (1 << RXEN0); |
124 | UCSR0B = (1 << TXEN0) | (1 << RXEN0); |
125 | // set asynchronous mode |
125 | // set asynchronous mode |
126 | UCSR0C &= ~(1 << UMSEL01); |
126 | UCSR0C &= ~(1 << UMSEL01); |
127 | UCSR0C &= ~(1 << UMSEL00); |
127 | UCSR0C &= ~(1 << UMSEL00); |
128 | // no parity |
128 | // no parity |
129 | UCSR0C &= ~(1 << UPM01); |
129 | UCSR0C &= ~(1 << UPM01); |
130 | UCSR0C &= ~(1 << UPM00); |
130 | UCSR0C &= ~(1 << UPM00); |
131 | // 1 stop bit |
131 | // 1 stop bit |
132 | UCSR0C &= ~(1 << USBS0); |
132 | UCSR0C &= ~(1 << USBS0); |
133 | // 8-bit |
133 | // 8-bit |
134 | UCSR0B &= ~(1 << UCSZ02); |
134 | UCSR0B &= ~(1 << UCSZ02); |
135 | UCSR0C |= (1 << UCSZ01); |
135 | UCSR0C |= (1 << UCSZ01); |
136 | UCSR0C |= (1 << UCSZ00); |
136 | UCSR0C |= (1 << UCSZ00); |
137 | 137 | ||
138 | // flush receive buffer |
138 | // flush receive buffer |
139 | while ( UCSR0A & (1<<RXC0) ) UDR0; |
139 | while ( UCSR0A & (1<<RXC0) ) UDR0; |
140 | 140 | ||
141 | // enable interrupts at the end |
141 | // enable interrupts at the end |
142 | // enable RX-Interrupt |
142 | // enable RX-Interrupt |
143 | UCSR0B |= (1 << RXCIE0); |
143 | UCSR0B |= (1 << RXCIE0); |
144 | // enable TX-Interrupt |
144 | // enable TX-Interrupt |
145 | UCSR0B |= (1 << TXCIE0); |
145 | UCSR0B |= (1 << TXCIE0); |
146 | 146 | ||
147 | rxd_buffer_locked = FALSE; |
147 | rxd_buffer_locked = FALSE; |
148 | // restore global interrupt flags |
148 | // restore global interrupt flags |
149 | SREG = sreg; |
149 | SREG = sreg; |
150 | } |
150 | } |
151 | 151 | ||
152 | /****************************************************************/ |
152 | /****************************************************************/ |
153 | /* USART0 transmitter ISR */ |
153 | /* USART0 transmitter ISR */ |
154 | /****************************************************************/ |
154 | /****************************************************************/ |
155 | ISR(USART0_TX_vect) |
155 | ISR(USART0_TX_vect) |
156 | { |
156 | { |
157 | static uint16_t ptr_txd_buffer = 0; |
157 | static uint16_t ptr_txd_buffer = 0; |
158 | uint8_t tmp_tx; |
158 | uint8_t tmp_tx; |
159 | if(!txd_complete) // transmission not completed |
159 | if(!txd_complete) // transmission not completed |
160 | { |
160 | { |
161 | ptr_txd_buffer++; // die [0] wurde schon gesendet |
161 | ptr_txd_buffer++; // die [0] wurde schon gesendet |
162 | tmp_tx = txd_buffer[ptr_txd_buffer]; |
162 | tmp_tx = txd_buffer[ptr_txd_buffer]; |
163 | // if terminating character or end of txd buffer was reached |
163 | // if terminating character or end of txd buffer was reached |
164 | if((tmp_tx == '\r') || (ptr_txd_buffer == TXD_BUFFER_LEN)) |
164 | if((tmp_tx == '\r') || (ptr_txd_buffer == TXD_BUFFER_LEN)) |
165 | { |
165 | { |
166 | ptr_txd_buffer = 0; // reset txd pointer |
166 | ptr_txd_buffer = 0; // reset txd pointer |
167 | txd_complete = 1; // stop transmission |
167 | txd_complete = 1; // stop transmission |
168 | } |
168 | } |
169 | UDR0 = tmp_tx; // send current byte will trigger this ISR again |
169 | UDR0 = tmp_tx; // send current byte will trigger this ISR again |
170 | } |
170 | } |
171 | // transmission completed |
171 | // transmission completed |
172 | else ptr_txd_buffer = 0; |
172 | else ptr_txd_buffer = 0; |
173 | } |
173 | } |
174 | 174 | ||
175 | /****************************************************************/ |
175 | /****************************************************************/ |
176 | /* USART0 receiver ISR */ |
176 | /* USART0 receiver ISR */ |
177 | /****************************************************************/ |
177 | /****************************************************************/ |
178 | ISR(USART0_RX_vect) |
178 | ISR(USART0_RX_vect) |
179 | { |
179 | { |
180 | static uint16_t crc; |
180 | static uint16_t crc; |
181 | static uint8_t ptr_rxd_buffer = 0; |
181 | static uint8_t ptr_rxd_buffer = 0; |
182 | uint8_t crc1, crc2; |
182 | uint8_t crc1, crc2; |
183 | uint8_t c; |
183 | uint8_t c; |
184 | 184 | ||
185 | c = UDR0; // catch the received byte |
185 | c = UDR0; // catch the received byte |
186 | 186 | ||
187 | // If the FC 1.0 cpu is used the ublox module should be conneced to rxd of the 1st uart. |
187 | // If the FC 1.0 cpu is used the ublox module should be conneced to rxd of the 1st uart. |
188 | // The FC 1.1 /1.2 has the ATMEGA644p cpu with a 2nd uart to which the ublox should be connected. |
188 | // The FC 1.1 /1.2 has the ATMEGA644p cpu with a 2nd uart to which the ublox should be connected. |
189 | #if defined (__AVR_ATmega644P__) |
189 | #if defined (__AVR_ATmega644P__) |
190 | if(BoardRelease == 10) ubx_parser(c); |
190 | if(BoardRelease == 10) ubx_parser(c); |
191 | #else |
191 | #else |
192 | ubx_parser(c); |
192 | ubx_parser(c); |
193 | #endif |
193 | #endif |
194 | 194 | ||
195 | if(rxd_buffer_locked) return; // if txd buffer is locked immediately return |
195 | if(rxd_buffer_locked) return; // if txd buffer is locked immediately return |
196 | 196 | ||
197 | // the rxd buffer is unlocked |
197 | // the rxd buffer is unlocked |
198 | if((ptr_rxd_buffer == 0) && (c == '#')) // if rxd buffer is empty and syncronisation character is received |
198 | if((ptr_rxd_buffer == 0) && (c == '#')) // if rxd buffer is empty and syncronisation character is received |
199 | { |
199 | { |
200 | rxd_buffer[ptr_rxd_buffer++] = c; // copy 1st byte to buffer |
200 | rxd_buffer[ptr_rxd_buffer++] = c; // copy 1st byte to buffer |
201 | crc = c; // init crc |
201 | crc = c; // init crc |
202 | } |
202 | } |
203 | #if 0 |
203 | #if 0 |
204 | else if (ptr_rxd_buffer == 1) // handle address |
204 | else if (ptr_rxd_buffer == 1) // handle address |
205 | { |
205 | { |
206 | rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer |
206 | rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer |
207 | crc += c; // update crc |
207 | crc += c; // update crc |
208 | } |
208 | } |
209 | #endif |
209 | #endif |
210 | else if (ptr_rxd_buffer < RXD_BUFFER_LEN) // collect incomming bytes |
210 | else if (ptr_rxd_buffer < RXD_BUFFER_LEN) // collect incomming bytes |
211 | { |
211 | { |
212 | if(c != '\r') // no termination character |
212 | if(c != '\r') // no termination character |
213 | { |
213 | { |
214 | rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer |
214 | rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer |
215 | crc += c; // update crc |
215 | crc += c; // update crc |
216 | } |
216 | } |
217 | else // termination character was received |
217 | else // termination character was received |
218 | { |
218 | { |
219 | // the last 2 bytes are no subject for checksum calculation |
219 | // the last 2 bytes are no subject for checksum calculation |
220 | // they are the checksum itself |
220 | // they are the checksum itself |
221 | crc -= rxd_buffer[ptr_rxd_buffer-2]; |
221 | crc -= rxd_buffer[ptr_rxd_buffer-2]; |
222 | crc -= rxd_buffer[ptr_rxd_buffer-1]; |
222 | crc -= rxd_buffer[ptr_rxd_buffer-1]; |
223 | // calculate checksum from transmitted data |
223 | // calculate checksum from transmitted data |
224 | crc %= 4096; |
224 | crc %= 4096; |
225 | crc1 = '=' + crc / 64; |
225 | crc1 = '=' + crc / 64; |
226 | crc2 = '=' + crc % 64; |
226 | crc2 = '=' + crc % 64; |
227 | // compare checksum to transmitted checksum bytes |
227 | // compare checksum to transmitted checksum bytes |
228 | if((crc1 == rxd_buffer[ptr_rxd_buffer-2]) && (crc2 == rxd_buffer[ptr_rxd_buffer-1])) |
228 | if((crc1 == rxd_buffer[ptr_rxd_buffer-2]) && (crc2 == rxd_buffer[ptr_rxd_buffer-1])) |
229 | { // checksum valid |
229 | { // checksum valid |
230 | rxd_buffer_locked = TRUE; // lock the rxd buffer |
230 | rxd_buffer_locked = TRUE; // lock the rxd buffer |
231 | ReceivedBytes = ptr_rxd_buffer; // store number of received bytes |
231 | ReceivedBytes = ptr_rxd_buffer; // store number of received bytes |
232 | rxd_buffer[ptr_rxd_buffer] = '\r'; // set termination character |
232 | rxd_buffer[ptr_rxd_buffer] = '\r'; // set termination character |
233 | // if 2nd byte is an 'R' enable watchdog that will result in an reset |
233 | // if 2nd byte is an 'R' enable watchdog that will result in an reset |
234 | if(rxd_buffer[2] == 'R') {wdt_enable(WDTO_250MS);} // Reset-Commando |
234 | if(rxd_buffer[2] == 'R') {wdt_enable(WDTO_250MS);} // Reset-Commando |
235 | } |
235 | } |
236 | else |
236 | else |
237 | { // checksum invalid |
237 | { // checksum invalid |
238 | rxd_buffer_locked = FALSE; // unlock rxd buffer |
238 | rxd_buffer_locked = FALSE; // unlock rxd buffer |
239 | } |
239 | } |
240 | ptr_rxd_buffer = 0; // reset txd buffer |
240 | ptr_rxd_buffer = 0; // reset txd buffer |
241 | } |
241 | } |
242 | } // buffer overrun |
242 | } // buffer overrun |
243 | else |
243 | else |
244 | { |
244 | { |
245 | ptr_rxd_buffer = 0; // reset rxd buffer |
245 | ptr_rxd_buffer = 0; // reset rxd buffer |
246 | rxd_buffer_locked = FALSE; // unlock rxd buffer |
246 | rxd_buffer_locked = FALSE; // unlock rxd buffer |
247 | } |
247 | } |
248 | 248 | ||
249 | } |
249 | } |
250 | 250 | ||
251 | 251 | ||
252 | // -------------------------------------------------------------------------- |
252 | // -------------------------------------------------------------------------- |
253 | void AddCRC(uint16_t datalen) |
253 | void AddCRC(uint16_t datalen) |
254 | { |
254 | { |
255 | uint16_t tmpCRC = 0,i; |
255 | uint16_t tmpCRC = 0,i; |
256 | for(i = 0; i < datalen;i++) |
256 | for(i = 0; i < datalen;i++) |
257 | { |
257 | { |
258 | tmpCRC += txd_buffer[i]; |
258 | tmpCRC += txd_buffer[i]; |
259 | } |
259 | } |
260 | tmpCRC %= 4096; |
260 | tmpCRC %= 4096; |
261 | txd_buffer[i++] = '=' + tmpCRC / 64; |
261 | txd_buffer[i++] = '=' + tmpCRC / 64; |
262 | txd_buffer[i++] = '=' + tmpCRC % 64; |
262 | txd_buffer[i++] = '=' + tmpCRC % 64; |
263 | txd_buffer[i++] = '\r'; |
263 | txd_buffer[i++] = '\r'; |
264 | txd_complete = 0; |
264 | txd_complete = 0; |
265 | UDR0 = txd_buffer[0]; // initiates the transmittion |
265 | UDR0 = txd_buffer[0]; // initiates the transmittion |
266 | } |
266 | } |
267 | 267 | ||
268 | 268 | ||
269 | 269 | ||
270 | // -------------------------------------------------------------------------- |
270 | // -------------------------------------------------------------------------- |
271 | void SendOutData(uint8_t cmd,uint8_t module, uint8_t *snd, uint8_t len) |
271 | void SendOutData(uint8_t cmd,uint8_t module, uint8_t *snd, uint8_t len) |
272 | { |
272 | { |
273 | uint16_t pt = 0; |
273 | uint16_t pt = 0; |
274 | uint8_t a,b,c; |
274 | uint8_t a,b,c; |
275 | uint8_t ptr = 0; |
275 | uint8_t ptr = 0; |
276 | 276 | ||
277 | txd_buffer[pt++] = '#'; // Start character |
277 | txd_buffer[pt++] = '#'; // Start character |
278 | txd_buffer[pt++] = module; // Address (a=0; b=1,...) |
278 | txd_buffer[pt++] = module; // Address (a=0; b=1,...) |
279 | txd_buffer[pt++] = cmd; // Command |
279 | txd_buffer[pt++] = cmd; // Command |
280 | 280 | ||
281 | while(len) |
281 | while(len) |
282 | { |
282 | { |
283 | if(len) { a = snd[ptr++]; len--;} else a = 0; |
283 | if(len) { a = snd[ptr++]; len--;} else a = 0; |
284 | if(len) { b = snd[ptr++]; len--;} else b = 0; |
284 | if(len) { b = snd[ptr++]; len--;} else b = 0; |
285 | if(len) { c = snd[ptr++]; len--;} else c = 0; |
285 | if(len) { c = snd[ptr++]; len--;} else c = 0; |
286 | txd_buffer[pt++] = '=' + (a >> 2); |
286 | txd_buffer[pt++] = '=' + (a >> 2); |
287 | txd_buffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4)); |
287 | txd_buffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4)); |
288 | txd_buffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6)); |
288 | txd_buffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6)); |
289 | txd_buffer[pt++] = '=' + ( c & 0x3f); |
289 | txd_buffer[pt++] = '=' + ( c & 0x3f); |
290 | } |
290 | } |
291 | AddCRC(pt); // add checksum after data block and initates the transmission |
291 | AddCRC(pt); // add checksum after data block and initates the transmission |
292 | } |
292 | } |
293 | 293 | ||
294 | 294 | ||
295 | // -------------------------------------------------------------------------- |
295 | // -------------------------------------------------------------------------- |
296 | void Decode64(uint8_t *ptrOut, uint8_t len, uint8_t ptrIn, uint8_t max) |
296 | void Decode64(uint8_t *ptrOut, uint8_t len, uint8_t ptrIn, uint8_t max) |
297 | { |
297 | { |
298 | uint8_t a,b,c,d; |
298 | uint8_t a,b,c,d; |
299 | uint8_t ptr = 0; |
299 | uint8_t ptr = 0; |
300 | uint8_t x,y,z; |
300 | uint8_t x,y,z; |
301 | while(len) |
301 | while(len) |
302 | { |
302 | { |
303 | a = rxd_buffer[ptrIn++] - '='; |
303 | a = rxd_buffer[ptrIn++] - '='; |
304 | b = rxd_buffer[ptrIn++] - '='; |
304 | b = rxd_buffer[ptrIn++] - '='; |
305 | c = rxd_buffer[ptrIn++] - '='; |
305 | c = rxd_buffer[ptrIn++] - '='; |
306 | d = rxd_buffer[ptrIn++] - '='; |
306 | d = rxd_buffer[ptrIn++] - '='; |
307 | if(ptrIn > max - 2) break; |
307 | if(ptrIn > max - 2) break; |
308 | 308 | ||
309 | x = (a << 2) | (b >> 4); |
309 | x = (a << 2) | (b >> 4); |
310 | y = ((b & 0x0f) << 4) | (c >> 2); |
310 | y = ((b & 0x0f) << 4) | (c >> 2); |
311 | z = ((c & 0x03) << 6) | d; |
311 | z = ((c & 0x03) << 6) | d; |
312 | 312 | ||
313 | if(len--) ptrOut[ptr++] = x; else break; |
313 | if(len--) ptrOut[ptr++] = x; else break; |
314 | if(len--) ptrOut[ptr++] = y; else break; |
314 | if(len--) ptrOut[ptr++] = y; else break; |
315 | if(len--) ptrOut[ptr++] = z; else break; |
315 | if(len--) ptrOut[ptr++] = z; else break; |
316 | } |
316 | } |
317 | } |
317 | } |
318 | 318 | ||
319 | 319 | ||
320 | // -------------------------------------------------------------------------- |
320 | // -------------------------------------------------------------------------- |
321 | void ProcessRxData(void) |
321 | void ProcessRxData(void) |
322 | { |
322 | { |
323 | // if data in the rxd buffer are not locked immediately return |
323 | // if data in the rxd buffer are not locked immediately return |
324 | if(!rxd_buffer_locked) return; |
324 | if(!rxd_buffer_locked) return; |
325 | 325 | ||
326 | uint8_t tmp_char_arr2[2]; // local buffer |
326 | uint8_t tmp_char_arr2[2]; // local buffer |
327 | 327 | ||
328 | PcAccess = 255; |
328 | PcAccess = 255; |
329 | switch(rxd_buffer[2]) |
329 | switch(rxd_buffer[2]) |
330 | { |
330 | { |
331 | case 'a':// Labels of the Analog Deboug outputs |
331 | case 'a':// Labels of the Analog Deboug outputs |
332 | Decode64((uint8_t *) &tmp_char_arr2[0], sizeof(tmp_char_arr2), 3, ReceivedBytes); |
332 | Decode64((uint8_t *) &tmp_char_arr2[0], sizeof(tmp_char_arr2), 3, ReceivedBytes); |
333 | DebugTextAnforderung = tmp_char_arr2[0]; |
333 | DebugTextAnforderung = tmp_char_arr2[0]; |
334 | break; |
334 | break; |
335 | case 'b': // extern control |
335 | case 'b': // extern control |
336 | Decode64((uint8_t *) &ExternControl,sizeof(ExternControl), 3, ReceivedBytes); |
336 | Decode64((uint8_t *) &ExternControl,sizeof(ExternControl), 3, ReceivedBytes); |
337 | RemoteButtons |= ExternControl.RemoteButtons; |
337 | RemoteButtons |= ExternControl.RemoteButtons; |
338 | ConfirmFrame = ExternControl.Frame; |
338 | ConfirmFrame = ExternControl.Frame; |
339 | break; |
339 | break; |
340 | case 'c': // extern control with debug request |
340 | case 'c': // extern control with debug request |
341 | Decode64((uint8_t *) &ExternControl,sizeof(ExternControl),3,ReceivedBytes); |
341 | Decode64((uint8_t *) &ExternControl,sizeof(ExternControl),3,ReceivedBytes); |
342 | RemoteButtons |= ExternControl.RemoteButtons; |
342 | RemoteButtons |= ExternControl.RemoteButtons; |
343 | ConfirmFrame = ExternControl.Frame; |
343 | ConfirmFrame = ExternControl.Frame; |
344 | DebugDataRequest = 1; |
344 | DebugDataRequest = 1; |
345 | break; |
345 | break; |
346 | case 'h':// x-1 display columns |
346 | case 'h':// x-1 display columns |
347 | Decode64((uint8_t *) &tmp_char_arr2[0],sizeof(tmp_char_arr2),3,ReceivedBytes); |
347 | Decode64((uint8_t *) &tmp_char_arr2[0],sizeof(tmp_char_arr2),3,ReceivedBytes); |
348 | RemoteButtons |= tmp_char_arr2[0]; |
348 | RemoteButtons |= tmp_char_arr2[0]; |
349 | if(tmp_char_arr2[1] == 255) NurKanalAnforderung = 1; |
349 | if(tmp_char_arr2[1] == 255) NurKanalAnforderung = 1; |
350 | else NurKanalAnforderung = 0; // keine Displaydaten |
350 | else NurKanalAnforderung = 0; // keine Displaydaten |
351 | DebugDisplayRequest = 1; |
351 | DebugDisplayRequest = 1; |
352 | break; |
352 | break; |
353 | case 't':// motor test |
353 | case 't':// motor test |
354 | Decode64((uint8_t *) &MotorTest[0],sizeof(MotorTest),3,ReceivedBytes); |
354 | Decode64((uint8_t *) &MotorTest[0],sizeof(MotorTest),3,ReceivedBytes); |
355 | break; |
355 | break; |
356 | case 'k':// keys from DubWise |
356 | case 'k':// keys from DubWise |
357 | Decode64((uint8_t *) &DubWiseKeys[0],sizeof(DubWiseKeys),3,ReceivedBytes); |
357 | Decode64((uint8_t *) &DubWiseKeys[0],sizeof(DubWiseKeys),3,ReceivedBytes); |
358 | ConfirmFrame = DubWiseKeys[3]; |
358 | ConfirmFrame = DubWiseKeys[3]; |
359 | break; |
359 | break; |
360 | case 'v': // get version and board release |
360 | case 'v': // get version and board release |
361 | GetVersionRequest = 1; |
361 | GetVersionRequest = 1; |
362 | break; |
362 | break; |
363 | case 'g':// get debug data |
363 | case 'g':// get debug data |
364 | DebugGetRequest = 1; |
364 | DebugGetRequest = 1; |
365 | break; |
365 | break; |
366 | case 'q':// get settings |
366 | case 'q':// get settings |
367 | Decode64((uint8_t *) &tmp_char_arr2[0],sizeof(tmp_char_arr2),3,ReceivedBytes); |
367 | Decode64((uint8_t *) &tmp_char_arr2[0],sizeof(tmp_char_arr2),3,ReceivedBytes); |
368 | if(tmp_char_arr2[0] != 0xff) |
368 | if(tmp_char_arr2[0] != 0xff) |
369 | { |
369 | { |
370 | if(tmp_char_arr2[0] > 5) tmp_char_arr2[0] = 5; // limit to 5 |
370 | if(tmp_char_arr2[0] > 5) tmp_char_arr2[0] = 5; // limit to 5 |
371 | // load requested parameter set |
371 | // load requested parameter set |
372 | ParamSet_ReadFromEEProm(tmp_char_arr2[0]); |
372 | ParamSet_ReadFromEEProm(tmp_char_arr2[0]); |
373 | SendOutData('L' + tmp_char_arr2[0] -1,MySlaveAddr,(uint8_t *) &ParamSet.ChannelAssignment[0],PARAMSET_STRUCT_LEN); |
373 | SendOutData('L' + tmp_char_arr2[0] -1,MySlaveAddr,(uint8_t *) &ParamSet.ChannelAssignment[0],PARAMSET_STRUCT_LEN); |
374 | } |
374 | } |
375 | else // send active parameter set |
375 | else // send active parameter set |
376 | SendOutData('L' + GetParamByte(PID_ACTIVE_SET)-1,MySlaveAddr,(uint8_t *) &ParamSet.ChannelAssignment[0],PARAMSET_STRUCT_LEN); |
376 | SendOutData('L' + GetParamByte(PID_ACTIVE_SET)-1,MySlaveAddr,(uint8_t *) &ParamSet.ChannelAssignment[0],PARAMSET_STRUCT_LEN); |
377 | 377 | ||
378 | break; |
378 | break; |
379 | 379 | ||
380 | case 'l': |
380 | case 'l': |
381 | case 'm': |
381 | case 'm': |
382 | case 'n': |
382 | case 'n': |
383 | case 'o': |
383 | case 'o': |
384 | case 'p': // save parameterset |
384 | case 'p': // save parameterset |
385 | Decode64((uint8_t *) &ParamSet.ChannelAssignment[0],PARAMSET_STRUCT_LEN,3,ReceivedBytes); |
385 | Decode64((uint8_t *) &ParamSet.ChannelAssignment[0],PARAMSET_STRUCT_LEN,3,ReceivedBytes); |
386 | ParamSet_WriteToEEProm(rxd_buffer[2] - 'l' + 1); |
386 | ParamSet_WriteToEEProm(rxd_buffer[2] - 'l' + 1); |
387 | TurnOver180Pitch = (int32_t) ParamSet.AngleTurnOverPitch * 2500L; |
387 | TurnOver180Pitch = (int32_t) ParamSet.AngleTurnOverPitch * 2500L; |
388 | TurnOver180Roll = (int32_t) ParamSet.AngleTurnOverRoll * 2500L; |
388 | TurnOver180Roll = (int32_t) ParamSet.AngleTurnOverRoll * 2500L; |
389 | Beep(GetActiveParamSet()); |
389 | Beep(GetActiveParamSet()); |
390 | break; |
390 | break; |
391 | 391 | ||
392 | 392 | ||
393 | } |
393 | } |
394 | // unlock the rxd buffer after processing |
394 | // unlock the rxd buffer after processing |
395 | rxd_buffer_locked = FALSE; |
395 | rxd_buffer_locked = FALSE; |
396 | } |
396 | } |
397 | 397 | ||
398 | //############################################################################ |
398 | //############################################################################ |
399 | //Routine für die Serielle Ausgabe |
399 | //Routine für die Serielle Ausgabe |
400 | int16_t uart_putchar (int8_t c) |
400 | int16_t uart_putchar (int8_t c) |
401 | //############################################################################ |
401 | //############################################################################ |
402 | { |
402 | { |
403 | if (c == '\n') |
403 | if (c == '\n') |
404 | uart_putchar('\r'); |
404 | uart_putchar('\r'); |
405 | // wait until previous character was send |
405 | // wait until previous character was send |
406 | loop_until_bit_is_set(UCSR0A, UDRE0); |
406 | loop_until_bit_is_set(UCSR0A, UDRE0); |
407 | //Ausgabe des Zeichens |
407 | //Ausgabe des Zeichens |
408 | UDR0 = c; |
408 | UDR0 = c; |
409 | 409 | ||
410 | return (0); |
410 | return (0); |
411 | } |
411 | } |
412 | 412 | ||
413 | 413 | ||
414 | //--------------------------------------------------------------------------------------------- |
414 | //--------------------------------------------------------------------------------------------- |
415 | void TransmitTxData(void) |
415 | void TransmitTxData(void) |
416 | { |
416 | { |
417 | static int16_t Debug_Timer = 0; |
417 | static int16_t Debug_Timer = 0; |
418 | if(!txd_complete) return; |
418 | if(!txd_complete) return; |
419 | 419 | ||
420 | if(DebugGetRequest && txd_complete) // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen |
420 | if(DebugGetRequest && txd_complete) // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen |
421 | { |
421 | { |
422 | SendOutData('G',MySlaveAddr,(uint8_t *) &ExternControl,sizeof(ExternControl)); |
422 | SendOutData('G',MySlaveAddr,(uint8_t *) &ExternControl,sizeof(ExternControl)); |
423 | DebugGetRequest = 0; |
423 | DebugGetRequest = 0; |
424 | } |
424 | } |
425 | 425 | ||
426 | if((CheckDelay(Debug_Timer) || DebugDataRequest) && txd_complete) |
426 | if((CheckDelay(Debug_Timer) || DebugDataRequest) && txd_complete) |
427 | { |
427 | { |
428 | SendOutData('D',MySlaveAddr,(uint8_t *) &DebugOut,sizeof(DebugOut)); |
428 | SendOutData('D',MySlaveAddr,(uint8_t *) &DebugOut,sizeof(DebugOut)); |
429 | DebugDataRequest = 0; |
429 | DebugDataRequest = 0; |
430 | Debug_Timer = SetDelay(MIN_DEBUG_INTERVALL); |
430 | Debug_Timer = SetDelay(MIN_DEBUG_INTERVALL); |
431 | } |
431 | } |
432 | if(DebugTextAnforderung != 255) // Texte für die Analogdaten |
432 | if(DebugTextAnforderung != 255) // Texte für die Analogdaten |
433 | { |
433 | { |
434 | SendOutData('A',DebugTextAnforderung + '0',(uint8_t *) ANALOG_TEXT[DebugTextAnforderung],16); |
434 | SendOutData('A',DebugTextAnforderung + '0',(uint8_t *) ANALOG_TEXT[DebugTextAnforderung],16); |
435 | DebugTextAnforderung = 255; |
435 | DebugTextAnforderung = 255; |
436 | } |
436 | } |
437 | if(ConfirmFrame && txd_complete) // Datensatz ohne CRC bestätigen |
437 | if(ConfirmFrame && txd_complete) // Datensatz ohne CRC bestätigen |
438 | { |
438 | { |
439 | txd_buffer[0] = '#'; |
439 | txd_buffer[0] = '#'; |
440 | txd_buffer[1] = ConfirmFrame; |
440 | txd_buffer[1] = ConfirmFrame; |
441 | txd_buffer[2] = '\r'; |
441 | txd_buffer[2] = '\r'; |
442 | txd_complete = 0; |
442 | txd_complete = 0; |
443 | ConfirmFrame = 0; |
443 | ConfirmFrame = 0; |
444 | UDR0 = txd_buffer[0]; |
444 | UDR0 = txd_buffer[0]; |
445 | } |
445 | } |
446 | if(DebugDisplayRequest && txd_complete) |
446 | if(DebugDisplayRequest && txd_complete) |
447 | { |
447 | { |
448 | LCD_PrintMenu(); |
448 | LCD_PrintMenu(); |
449 | DebugDisplayRequest = 0; |
449 | DebugDisplayRequest = 0; |
450 | if(++RemotePollDisplayLine == 4 || NurKanalAnforderung) |
450 | if(++RemotePollDisplayLine == 4 || NurKanalAnforderung) |
451 | { |
451 | { |
452 | SendOutData('4',0,(uint8_t *)&PPM_in,sizeof(PPM_in)); // DisplayZeile übertragen |
452 | SendOutData('4',0,(uint8_t *)&PPM_in,sizeof(PPM_in)); // DisplayZeile übertragen |
453 | RemotePollDisplayLine = -1; |
453 | RemotePollDisplayLine = -1; |
454 | } |
454 | } |
455 | else SendOutData('0' + RemotePollDisplayLine,0,(uint8_t *)&DisplayBuff[20 * RemotePollDisplayLine],20); // DisplayZeile übertragen |
455 | else SendOutData('0' + RemotePollDisplayLine,0,(uint8_t *)&DisplayBuff[20 * RemotePollDisplayLine],20); // DisplayZeile übertragen |
456 | } |
456 | } |
457 | if(GetVersionRequest && txd_complete) |
457 | if(GetVersionRequest && txd_complete) |
458 | { |
458 | { |
459 | SendOutData('V',MySlaveAddr,(uint8_t *) &VersionInfo,sizeof(VersionInfo)); |
459 | SendOutData('V',MySlaveAddr,(uint8_t *) &VersionInfo,sizeof(VersionInfo)); |
460 | GetVersionRequest = 0; |
460 | GetVersionRequest = 0; |
461 | } |
461 | } |
462 | 462 | ||
463 | } |
463 | } |
464 | 464 | ||
465 | 465 |