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