Subversion Repositories FlightCtrl

Rev

Rev 2033 | Rev 2039 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1612 dongfang 1
#include <avr/io.h>
2
#include <avr/interrupt.h>
3
#include <avr/wdt.h>
4
#include <avr/pgmspace.h>
5
#include <stdarg.h>
6
#include <string.h>
7
 
8
#include "eeprom.h"
9
#include "menu.h"
10
#include "timer0.h"
11
#include "uart0.h"
12
#include "rc.h"
13
#include "externalControl.h"
1775 - 14
#include "output.h"
1864 - 15
#include "attitude.h"
1612 dongfang 16
 
17
#ifdef USE_MK3MAG
18
#include "mk3mag.h"
19
#endif
20
 
21
#define FC_ADDRESS 1
22
#define NC_ADDRESS 2
23
#define MK3MAG_ADDRESS 3
24
 
25
#define FALSE	0
26
#define TRUE	1
27
//int8_t test __attribute__ ((section (".noinit")));
2018 - 28
uint8_t request_verInfo = FALSE;
29
uint8_t request_externalControl = FALSE;
30
uint8_t request_display = FALSE;
31
uint8_t request_display1 = FALSE;
32
uint8_t request_debugData = FALSE;
33
uint8_t request_data3D = FALSE;
34
uint8_t request_debugLabel = 255;
1821 - 35
uint8_t request_PPMChannels = FALSE;
2018 - 36
uint8_t request_motorTest = FALSE;
1821 - 37
uint8_t request_variables = FALSE;
1775 - 38
 
2018 - 39
uint8_t displayLine = 0;
1612 dongfang 40
 
41
volatile uint8_t txd_buffer[TXD_BUFFER_LEN];
42
volatile uint8_t rxd_buffer_locked = FALSE;
43
volatile uint8_t rxd_buffer[RXD_BUFFER_LEN];
44
volatile uint8_t txd_complete = TRUE;
2018 - 45
volatile uint8_t receivedBytes = 0;
1612 dongfang 46
volatile uint8_t *pRxData = 0;
2018 - 47
volatile uint8_t rxDataLen = 0;
1612 dongfang 48
 
1821 - 49
uint8_t motorTestActive = 0;
50
uint8_t motorTest[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
2018 - 51
uint8_t confirmFrame;
1612 dongfang 52
 
53
typedef struct {
1821 - 54
	int16_t Heading;
55
}__attribute__((packed)) Heading_t;
1612 dongfang 56
 
1955 - 57
DebugOut_t debugOut;
2018 - 58
Data3D_t data3D;
1612 dongfang 59
 
2018 - 60
uint16_t debugData_timer;
61
uint16_t data3D_timer;
62
uint16_t debugData_interval = 0; // in 1ms
63
uint16_t data3D_interval = 0; // in 1ms
1612 dongfang 64
 
65
#ifdef USE_MK3MAG
2018 - 66
int16_t compass_timer;
1612 dongfang 67
#endif
68
 
69
// keep lables in flash to save 512 bytes of sram space
70
const prog_uint8_t ANALOG_LABEL[32][16] = {
1821 - 71
		//1234567890123456
72
		"AnglePitch      ", //0
73
		"AngleRoll       ",
74
		"AngleYaw        ",
1974 - 75
		"GyroPitch       ",
76
		"GyroRoll        ",
77
		"GyroYaw         ", //5
78
		"AccPitch        ",
79
		"AccRoll         ",
80
		"AccZ            ",
1821 - 81
		"AccPitch (angle)",
82
		"AccRoll (angle) ", //10
2033 - 83
		"-               ",
1821 - 84
		"Pitch Term      ",
85
		"Roll Term       ",
1955 - 86
		"Yaw Term        ",
1980 - 87
		"Throttle Term   ", //15
2035 - 88
		"-               ",
1986 - 89
		"ControlAct/10   ",
90
        "Acc. Vector     ",
2035 - 91
		"heightTrottleIn ",
92
		"HeightdThrottle ", //20
2026 - 93
		"Height          ",
2035 - 94
		"TargetHeight    ",
95
		"heightError     ",
96
		"HeightPdThrottle",
97
		"HeightIdThrottle", //25
98
		"HeightDdThrottle",
99
		"HeightPFactor   ",
100
		"HeightIFactor   ",
101
		"HeightDFactor   ",
1970 - 102
		"Altitude        ", //30
1821 - 103
		"AirpressADC     " };
1612 dongfang 104
 
105
/****************************************************************/
106
/*              Initialization of the USART0                    */
107
/****************************************************************/
2018 - 108
void usart0_init(void) {
1821 - 109
	uint8_t sreg = SREG;
110
	uint16_t ubrr = (uint16_t) ((uint32_t) SYSCLK / (8 * USART0_BAUD) - 1);
111
 
112
	// disable all interrupts before configuration
113
	cli();
114
 
115
	// disable RX-Interrupt
116
	UCSR0B &= ~(1 << RXCIE0);
117
	// disable TX-Interrupt
118
	UCSR0B &= ~(1 << TXCIE0);
119
 
120
	// set direction of RXD0 and TXD0 pins
121
	// set RXD0 (PD0) as an input pin
122
	PORTD |= (1 << PORTD0);
123
	DDRD &= ~(1 << DDD0);
124
	// set TXD0 (PD1) as an output pin
125
	PORTD |= (1 << PORTD1);
126
	DDRD |= (1 << DDD1);
127
 
128
	// USART0 Baud Rate Register
129
	// set clock divider
130
	UBRR0H = (uint8_t) (ubrr >> 8);
131
	UBRR0L = (uint8_t) ubrr;
132
 
133
	// USART0 Control and Status Register A, B, C
134
 
135
	// enable double speed operation in
136
	UCSR0A |= (1 << U2X0);
137
	// enable receiver and transmitter in
138
	UCSR0B = (1 << TXEN0) | (1 << RXEN0);
139
	// set asynchronous mode
140
	UCSR0C &= ~(1 << UMSEL01);
141
	UCSR0C &= ~(1 << UMSEL00);
142
	// no parity
143
	UCSR0C &= ~(1 << UPM01);
144
	UCSR0C &= ~(1 << UPM00);
145
	// 1 stop bit
146
	UCSR0C &= ~(1 << USBS0);
147
	// 8-bit
148
	UCSR0B &= ~(1 << UCSZ02);
149
	UCSR0C |= (1 << UCSZ01);
150
	UCSR0C |= (1 << UCSZ00);
151
 
152
	// flush receive buffer
153
	while (UCSR0A & (1 << RXC0))
154
		UDR0;
155
 
156
	// enable interrupts at the end
157
	// enable RX-Interrupt
158
	UCSR0B |= (1 << RXCIE0);
159
	// enable TX-Interrupt
160
	UCSR0B |= (1 << TXCIE0);
161
 
162
	// initialize the debug timer
2018 - 163
	debugData_timer = setDelay(debugData_interval);
1821 - 164
 
165
	// unlock rxd_buffer
166
	rxd_buffer_locked = FALSE;
167
	pRxData = 0;
2018 - 168
	rxDataLen = 0;
1821 - 169
 
170
	// no bytes to send
171
	txd_complete = TRUE;
172
 
1612 dongfang 173
#ifdef USE_MK3MAG
2018 - 174
	compass_timer = setDelay(220);
1612 dongfang 175
#endif
1821 - 176
 
2018 - 177
	versionInfo.SWMajor = VERSION_MAJOR;
178
	versionInfo.SWMinor = VERSION_MINOR;
179
	versionInfo.SWPatch = VERSION_PATCH;
180
	versionInfo.protoMajor = VERSION_SERIAL_MAJOR;
181
	versionInfo.protoMinor = VERSION_SERIAL_MINOR;
1821 - 182
 
183
	// restore global interrupt flags
184
	SREG = sreg;
1612 dongfang 185
}
186
 
187
/****************************************************************/
188
/* USART0 transmitter ISR                                       */
189
/****************************************************************/
2018 - 190
ISR(USART0_TX_vect) {
1821 - 191
	static uint16_t ptr_txd_buffer = 0;
192
	uint8_t tmp_tx;
193
	if (!txd_complete) { // transmission not completed
194
		ptr_txd_buffer++; // die [0] wurde schon gesendet
195
		tmp_tx = txd_buffer[ptr_txd_buffer];
196
		// if terminating character or end of txd buffer was reached
197
		if ((tmp_tx == '\r') || (ptr_txd_buffer == TXD_BUFFER_LEN)) {
198
			ptr_txd_buffer = 0; // reset txd pointer
199
			txd_complete = 1; // stop transmission
200
		}
201
		UDR0 = tmp_tx; // send current byte will trigger this ISR again
202
	}
203
	// transmission completed
204
	else
205
		ptr_txd_buffer = 0;
1612 dongfang 206
}
207
 
208
/****************************************************************/
209
/* USART0 receiver               ISR                            */
210
/****************************************************************/
2018 - 211
ISR(USART0_RX_vect) {
1969 - 212
	static uint16_t checksum;
1821 - 213
	static uint8_t ptr_rxd_buffer = 0;
1969 - 214
	uint8_t checksum1, checksum2;
1821 - 215
	uint8_t c;
1612 dongfang 216
 
1821 - 217
	c = UDR0; // catch the received byte
1612 dongfang 218
 
1821 - 219
	if (rxd_buffer_locked)
220
		return; // if rxd buffer is locked immediately return
1612 dongfang 221
 
1821 - 222
	// the rxd buffer is unlocked
223
	if ((ptr_rxd_buffer == 0) && (c == '#')) { // if rxd buffer is empty and syncronisation character is received
224
		rxd_buffer[ptr_rxd_buffer++] = c; // copy 1st byte to buffer
1969 - 225
		checksum = c; // init checksum
1821 - 226
	}
1612 dongfang 227
#if 0
1821 - 228
	else if (ptr_rxd_buffer == 1) { // handle address
229
		rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer
1969 - 230
		checksum += c; // update checksum
1821 - 231
	}
1612 dongfang 232
#endif
1821 - 233
	else if (ptr_rxd_buffer < RXD_BUFFER_LEN) { // collect incomming bytes
234
		if (c != '\r') { // no termination character
235
			rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer
1969 - 236
			checksum += c; // update checksum
1821 - 237
		} else { // termination character was received
238
			// the last 2 bytes are no subject for checksum calculation
239
			// they are the checksum itself
1969 - 240
			checksum -= rxd_buffer[ptr_rxd_buffer - 2];
241
			checksum -= rxd_buffer[ptr_rxd_buffer - 1];
1821 - 242
			// calculate checksum from transmitted data
1969 - 243
			checksum %= 4096;
244
			checksum1 = '=' + checksum / 64;
245
			checksum2 = '=' + checksum % 64;
1821 - 246
			// compare checksum to transmitted checksum bytes
1969 - 247
			if ((checksum1 == rxd_buffer[ptr_rxd_buffer - 2]) && (checksum2
1821 - 248
					== rxd_buffer[ptr_rxd_buffer - 1])) {
249
				// checksum valid
250
				rxd_buffer[ptr_rxd_buffer] = '\r'; // set termination character
2018 - 251
				receivedBytes = ptr_rxd_buffer + 1;// store number of received bytes
1821 - 252
				rxd_buffer_locked = TRUE; // lock the rxd buffer
253
				// if 2nd byte is an 'R' enable watchdog that will result in an reset
254
				if (rxd_buffer[2] == 'R') {
255
					wdt_enable(WDTO_250MS);
256
				} // Reset-Commando
257
			} else { // checksum invalid
258
				rxd_buffer_locked = FALSE; // unlock rxd buffer
259
			}
260
			ptr_rxd_buffer = 0; // reset rxd buffer pointer
261
		}
262
	} else { // rxd buffer overrun
263
		ptr_rxd_buffer = 0; // reset rxd buffer
264
		rxd_buffer_locked = FALSE; // unlock rxd buffer
265
	}
1612 dongfang 266
}
267
 
268
// --------------------------------------------------------------------------
1969 - 269
void Addchecksum(uint16_t datalen) {
270
	uint16_t tmpchecksum = 0, i;
1821 - 271
	for (i = 0; i < datalen; i++) {
1969 - 272
		tmpchecksum += txd_buffer[i];
1821 - 273
	}
1969 - 274
	tmpchecksum %= 4096;
275
	txd_buffer[i++] = '=' + tmpchecksum / 64;
276
	txd_buffer[i++] = '=' + tmpchecksum % 64;
1821 - 277
	txd_buffer[i++] = '\r';
278
	txd_complete = FALSE;
279
	UDR0 = txd_buffer[0]; // initiates the transmittion (continued in the TXD ISR)
1612 dongfang 280
}
281
 
282
// --------------------------------------------------------------------------
1775 - 283
// application example:
2018 - 284
// sendOutData('A', FC_ADDRESS, 2, (uint8_t *)&request_DebugLabel, sizeof(request_DebugLabel), label, 16);
1775 - 285
/*
2018 - 286
 void sendOutData(uint8_t cmd, uint8_t addr, uint8_t numofbuffers, ...) { // uint8_t *pdata, uint8_t len, ...
1821 - 287
 va_list ap;
288
 uint16_t txd_bufferIndex = 0;
289
 uint8_t *currentBuffer;
290
 uint8_t currentBufferIndex;
291
 uint16_t lengthOfCurrentBuffer;
292
 uint8_t shift = 0;
1775 - 293
 
1821 - 294
 txd_buffer[txd_bufferIndex++] = '#';			// Start character
295
 txd_buffer[txd_bufferIndex++] = 'a' + addr;	        // Address (a=0; b=1,...)
296
 txd_buffer[txd_bufferIndex++] = cmd;			// Command
1775 - 297
 
1821 - 298
 va_start(ap, numofbuffers);
299
 
300
 while(numofbuffers) {
301
 currentBuffer = va_arg(ap, uint8_t*);
302
 lengthOfCurrentBuffer = va_arg(ap, int);
303
 currentBufferIndex = 0;
304
 // Encode data: 3 bytes of data are encoded into 4 bytes,
305
 // where the 2 most significant bits are both 0.
306
 while(currentBufferIndex != lengthOfCurrentBuffer) {
307
 if (!shift) txd_buffer[txd_bufferIndex] = 0;
308
 txd_buffer[txd_bufferIndex]  |= currentBuffer[currentBufferIndex] >> (shift + 2);
309
 txd_buffer[++txd_bufferIndex] = (currentBuffer[currentBufferIndex] << (4 - shift)) & 0b00111111;
310
 shift += 2;
311
 if (shift == 6) { shift=0; txd_bufferIndex++; }
312
 currentBufferIndex++;
313
 }
314
 }
315
 // If the number of data bytes was not divisible by 3, stuff
316
 //  with 0 pseudodata  until length is again divisible by 3.
317
 if (shift == 2) {
318
 // We need to stuff with zero bytes at the end.
319
 txd_buffer[txd_bufferIndex]  &= 0b00110000;
320
 txd_buffer[++txd_bufferIndex] = 0;
321
 shift = 4;
322
 }
323
 if (shift == 4) {
324
 // We need to stuff with zero bytes at the end.
325
 txd_buffer[txd_bufferIndex++] &= 0b00111100;
326
 txd_buffer[txd_bufferIndex]    = 0;
327
 }
328
 va_end(ap);
1969 - 329
 Addchecksum(pt); // add checksum after data block and initates the transmission
1821 - 330
 }
331
 */
332
 
2018 - 333
void sendOutData(uint8_t cmd, uint8_t addr, uint8_t numofbuffers, ...) { // uint8_t *pdata, uint8_t len, ...
1821 - 334
	va_list ap;
335
	uint16_t pt = 0;
336
	uint8_t a, b, c;
337
	uint8_t ptr = 0;
1612 dongfang 338
 
1821 - 339
	uint8_t *pdata = 0;
340
	int len = 0;
1612 dongfang 341
 
1821 - 342
	txd_buffer[pt++] = '#'; // Start character
343
	txd_buffer[pt++] = 'a' + addr; // Address (a=0; b=1,...)
344
	txd_buffer[pt++] = cmd; // Command
345
 
346
	va_start(ap, numofbuffers);
347
 
348
	if (numofbuffers) {
349
		pdata = va_arg(ap, uint8_t*);
350
		len = va_arg(ap, int);
351
		ptr = 0;
352
		numofbuffers--;
353
	}
354
 
355
	while (len) {
356
		if (len) {
357
			a = pdata[ptr++];
358
			len--;
359
			if ((!len) && numofbuffers) {
360
				pdata = va_arg(ap, uint8_t*);
361
				len = va_arg(ap, int);
362
				ptr = 0;
363
				numofbuffers--;
364
			}
365
		} else
366
			a = 0;
367
		if (len) {
368
			b = pdata[ptr++];
369
			len--;
370
			if ((!len) && numofbuffers) {
371
				pdata = va_arg(ap, uint8_t*);
372
				len = va_arg(ap, int);
373
				ptr = 0;
374
				numofbuffers--;
375
			}
376
		} else
377
			b = 0;
378
		if (len) {
379
			c = pdata[ptr++];
380
			len--;
381
			if ((!len) && numofbuffers) {
382
				pdata = va_arg(ap, uint8_t*);
383
				len = va_arg(ap, int);
384
				ptr = 0;
385
				numofbuffers--;
386
			}
387
		} else
388
			c = 0;
389
		txd_buffer[pt++] = '=' + (a >> 2);
390
		txd_buffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4));
391
		txd_buffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6));
392
		txd_buffer[pt++] = '=' + (c & 0x3f);
393
	}
394
	va_end(ap);
1969 - 395
	Addchecksum(pt); // add checksum after data block and initates the transmission
1612 dongfang 396
}
397
 
398
// --------------------------------------------------------------------------
399
void Decode64(void) {
1821 - 400
	uint8_t a, b, c, d;
401
	uint8_t x, y, z;
402
	uint8_t ptrIn = 3;
403
	uint8_t ptrOut = 3;
2018 - 404
	uint8_t len = receivedBytes - 6;
1821 - 405
 
406
	while (len) {
407
		a = rxd_buffer[ptrIn++] - '=';
408
		b = rxd_buffer[ptrIn++] - '=';
409
		c = rxd_buffer[ptrIn++] - '=';
410
		d = rxd_buffer[ptrIn++] - '=';
411
		//if(ptrIn > ReceivedBytes - 3) break;
412
 
413
		x = (a << 2) | (b >> 4);
414
		y = ((b & 0x0f) << 4) | (c >> 2);
415
		z = ((c & 0x03) << 6) | d;
416
 
417
		if (len--)
418
			rxd_buffer[ptrOut++] = x;
419
		else
420
			break;
421
		if (len--)
422
			rxd_buffer[ptrOut++] = y;
423
		else
424
			break;
425
		if (len--)
426
			rxd_buffer[ptrOut++] = z;
427
		else
428
			break;
429
	}
430
	pRxData = &rxd_buffer[3];
2018 - 431
	rxDataLen = ptrOut - 3;
1612 dongfang 432
}
433
 
434
// --------------------------------------------------------------------------
2018 - 435
void usart0_processRxData(void) {
1821 - 436
	// We control the motorTestActive var from here: Count it down.
437
	if (motorTestActive)
438
		motorTestActive--;
439
	// if data in the rxd buffer are not locked immediately return
440
	if (!rxd_buffer_locked)
441
		return;
1980 - 442
	uint8_t tempchar[3];
1821 - 443
	Decode64(); // decode data block in rxd_buffer
1775 - 444
 
1821 - 445
	switch (rxd_buffer[1] - 'a') {
1775 - 446
 
1821 - 447
	case FC_ADDRESS:
448
		switch (rxd_buffer[2]) {
1612 dongfang 449
#ifdef USE_MK3MAG
1821 - 450
		case 'K':// compass value
451
		compassHeading = ((Heading_t *)pRxData)->Heading;
452
		// compassOffCourse = ((540 + compassHeading - compassCourse) % 360) - 180;
453
		break;
1612 dongfang 454
#endif
1821 - 455
		case 't': // motor test
2018 - 456
			if (rxDataLen > 20) {
1821 - 457
				memcpy(&motorTest[0], (uint8_t*) pRxData, sizeof(motorTest));
458
			} else {
459
				memcpy(&motorTest[0], (uint8_t*) pRxData, 4);
460
			}
461
			motorTestActive = 255;
462
			externalControlActive = 255;
463
			break;
1612 dongfang 464
 
1821 - 465
		case 'n':// "Get Mixer Table
466
			while (!txd_complete)
467
				; // wait for previous frame to be sent
2018 - 468
			sendOutData('N', FC_ADDRESS, 1, (uint8_t *) &mixerMatrix, sizeof(mixerMatrix));
1821 - 469
			break;
1612 dongfang 470
 
1821 - 471
		case 'm':// "Set Mixer Table
472
			if (pRxData[0] == EEMIXER_REVISION) {
1960 - 473
				memcpy(&mixerMatrix, (uint8_t*) pRxData, sizeof(mixerMatrix));
474
				mixerMatrix_writeToEEProm();
1821 - 475
				while (!txd_complete)
476
					; // wait for previous frame to be sent
1980 - 477
				tempchar[0] = 1;
1821 - 478
			} else {
1980 - 479
				tempchar[0] = 0;
1821 - 480
			}
2018 - 481
			sendOutData('M', FC_ADDRESS, 1, &tempchar, 1);
1821 - 482
			break;
1612 dongfang 483
 
1821 - 484
		case 'p': // get PPM channels
485
			request_PPMChannels = TRUE;
486
			break;
1612 dongfang 487
 
1821 - 488
		case 'q':// request settings
489
			if (pRxData[0] == 0xFF) {
1960 - 490
				pRxData[0] = getParamByte(PID_ACTIVE_SET);
1821 - 491
			}
492
			// limit settings range
493
			if (pRxData[0] < 1)
494
				pRxData[0] = 1; // limit to 1
495
			else if (pRxData[0] > 5)
496
				pRxData[0] = 5; // limit to 5
497
			// load requested parameter set
1960 - 498
			paramSet_readFromEEProm(pRxData[0]);
1980 - 499
			tempchar[0] = pRxData[0];
500
			tempchar[1] = EEPARAM_REVISION;
501
			tempchar[2] = sizeof(staticParams);
1821 - 502
			while (!txd_complete)
503
				; // wait for previous frame to be sent
2018 - 504
			sendOutData('Q', FC_ADDRESS, 2, &tempchar, 3, (uint8_t *) &staticParams, sizeof(staticParams));
1821 - 505
			break;
1612 dongfang 506
 
1821 - 507
		case 's': // save settings
508
			if (!(MKFlags & MKFLAG_MOTOR_RUN)) // save settings only if motors are off
509
			{
2035 - 510
				if ((1 <= pRxData[0]) && (pRxData[0] <= 5) && (pRxData[1] == EEPARAM_REVISION)) // check for setting to be in range and version of settings
1821 - 511
				{
512
					memcpy(&staticParams, (uint8_t*) &pRxData[2], sizeof(staticParams));
1960 - 513
					paramSet_writeToEEProm(pRxData[0]);
1980 - 514
					tempchar[0] = getActiveParamSet();
515
					beepNumber(tempchar[0]);
1821 - 516
				} else {
1980 - 517
					tempchar[0] = 0; //indicate bad data
1821 - 518
				}
519
				while (!txd_complete)
520
					; // wait for previous frame to be sent
2018 - 521
				sendOutData('S', FC_ADDRESS, 1, &tempchar, 1);
1821 - 522
			}
523
			break;
1612 dongfang 524
 
1821 - 525
		default:
526
			//unsupported command received
527
			break;
528
		} // case FC_ADDRESS:
1612 dongfang 529
 
1821 - 530
	default: // any Slave Address
531
		switch (rxd_buffer[2]) {
532
		case 'a':// request for labels of the analog debug outputs
2018 - 533
			request_debugLabel = pRxData[0];
534
			if (request_debugLabel > 31)
535
				request_debugLabel = 31;
1821 - 536
			break;
1612 dongfang 537
 
1821 - 538
		case 'b': // submit extern control
539
			memcpy(&externalControl, (uint8_t*) pRxData, sizeof(externalControl));
2018 - 540
			confirmFrame = externalControl.frame;
1821 - 541
			externalControlActive = 255;
542
			break;
1612 dongfang 543
 
1821 - 544
		case 'h':// request for display columns
2018 - 545
			remoteKeys |= pRxData[0];
546
			if (remoteKeys)
547
				displayLine = 0;
548
			request_display = TRUE;
1821 - 549
			break;
1612 dongfang 550
 
1821 - 551
		case 'l':// request for display columns
2018 - 552
			menuItem = pRxData[0];
553
			request_display1 = TRUE;
1821 - 554
			break;
1612 dongfang 555
 
1821 - 556
		case 'v': // request for version and board release
2018 - 557
			request_verInfo = TRUE;
1821 - 558
			break;
1775 - 559
 
1821 - 560
		case 'x':
561
			request_variables = TRUE;
562
			break;
1612 dongfang 563
 
1821 - 564
		case 'g':// get external control data
2018 - 565
			request_externalControl = TRUE;
1821 - 566
			break;
1612 dongfang 567
 
1821 - 568
		case 'd': // request for the debug data
2018 - 569
			debugData_interval = (uint16_t) pRxData[0] * 10;
570
			if (debugData_interval > 0)
571
				request_debugData = TRUE;
1821 - 572
			break;
1612 dongfang 573
 
1821 - 574
		case 'c': // request for the 3D data
2018 - 575
			data3D_interval = (uint16_t) pRxData[0] * 10;
576
			if (data3D_interval > 0)
577
				request_data3D = TRUE;
1821 - 578
			break;
579
 
580
		default:
581
			//unsupported command received
582
			break;
583
		}
584
		break; // default:
585
	}
586
	// unlock the rxd buffer after processing
587
	pRxData = 0;
2018 - 588
	rxDataLen = 0;
1821 - 589
	rxd_buffer_locked = FALSE;
1612 dongfang 590
}
591
 
1645 - 592
/************************************************************************/
2035 - 593
/* Routine f�r die Serielle Ausgabe                                     */
1645 - 594
/************************************************************************/
1821 - 595
int16_t uart_putchar(int8_t c) {
596
	if (c == '\n')
597
		uart_putchar('\r');
598
	// wait until previous character was send
599
	loop_until_bit_is_set(UCSR0A, UDRE0);
600
	// send character
601
	UDR0 = c;
602
	return (0);
1612 dongfang 603
}
604
 
605
//---------------------------------------------------------------------------------------------
2018 - 606
void usart0_transmitTxData(void) {
1821 - 607
	if (!txd_complete)
608
		return;
1612 dongfang 609
 
2018 - 610
	if (request_verInfo && txd_complete) {
611
		sendOutData('V', FC_ADDRESS, 1, (uint8_t *) &versionInfo, sizeof(versionInfo));
612
		request_verInfo = FALSE;
1821 - 613
	}
1612 dongfang 614
 
2018 - 615
	if (request_display && txd_complete) {
616
		LCD_printMenu();
617
		sendOutData('H', FC_ADDRESS, 2, &displayLine, sizeof(displayLine),
618
				&displayBuff[displayLine * 20], 20);
619
		displayLine++;
620
		if (displayLine >= 4)
621
			displayLine = 0;
622
		request_display = FALSE;
1821 - 623
	}
1612 dongfang 624
 
2018 - 625
	if (request_display1 && txd_complete) {
626
		LCD_printMenu();
627
		sendOutData('L', FC_ADDRESS, 3, &menuItem, sizeof(menuItem), &maxMenuItem,
628
				sizeof(maxMenuItem), displayBuff, sizeof(displayBuff));
629
		request_display1 = FALSE;
1821 - 630
	}
631
 
2035 - 632
	if (request_debugLabel != 0xFF) { // Texte f�r die Analogdaten
1821 - 633
		uint8_t label[16]; // local sram buffer
2018 - 634
		memcpy_P(label, ANALOG_LABEL[request_debugLabel], 16); // read lable from flash to sram buffer
635
		sendOutData('A', FC_ADDRESS, 2, (uint8_t *) &request_debugLabel,
636
				sizeof(request_debugLabel), label, 16);
637
		request_debugLabel = 0xFF;
1821 - 638
	}
639
 
2035 - 640
	if (confirmFrame && txd_complete) { // Datensatz ohne checksum best�tigen
2018 - 641
		sendOutData('B', FC_ADDRESS, 1, (uint8_t*) &confirmFrame, sizeof(confirmFrame));
642
		confirmFrame = 0;
1821 - 643
	}
644
 
2018 - 645
	if (((debugData_interval && checkDelay(debugData_timer)) || request_debugData)
1821 - 646
			&& txd_complete) {
2018 - 647
		sendOutData('D', FC_ADDRESS, 1, (uint8_t *) &debugOut, sizeof(debugOut));
648
		debugData_timer = setDelay(debugData_interval);
649
		request_debugData = FALSE;
1821 - 650
	}
651
 
2018 - 652
	if (((data3D_interval && checkDelay(data3D_timer)) || request_data3D)
1821 - 653
			&& txd_complete) {
2018 - 654
		sendOutData('C', FC_ADDRESS, 1, (uint8_t *) &data3D, sizeof(data3D));
655
		data3D.anglePitch = (int16_t) ((10 * angle[PITCH])
1821 - 656
				/ GYRO_DEG_FACTOR_PITCHROLL); // convert to multiple of 0.1°
2018 - 657
		data3D.angleRoll = (int16_t) ((10 * angle[ROLL])
1821 - 658
				/ GYRO_DEG_FACTOR_PITCHROLL); // convert to multiple of 0.1°
2018 - 659
		data3D.heading = (int16_t) ((10 * yawGyroHeading) / GYRO_DEG_FACTOR_YAW); // convert to multiple of 0.1°
660
		data3D_timer = setDelay(data3D_interval);
661
		request_data3D = FALSE;
1821 - 662
	}
663
 
2018 - 664
	if (request_externalControl && txd_complete) {
665
		sendOutData('G', FC_ADDRESS, 1, (uint8_t *) &externalControl,
1821 - 666
				sizeof(externalControl));
2018 - 667
		request_externalControl = FALSE;
1821 - 668
	}
669
 
1612 dongfang 670
#ifdef USE_MK3MAG
1887 - 671
	if((checkDelay(Compass_Timer)) && txd_complete) {
1969 - 672
		toMk3Mag.Attitude[0] = (int16_t)((10 * angle[PITCH]) / GYRO_DEG_FACTOR_PITCHROLL); // approx. 0.1 deg
673
		toMk3Mag.Attitude[1] = (int16_t)((10 * angle[ROLL]) / GYRO_DEG_FACTOR_PITCHROLL); // approx. 0.1 deg
674
		toMk3Mag.UserParam[0] = dynamicParams.userParams[0];
675
		toMk3Mag.UserParam[1] = dynamicParams.userParams[1];
676
		toMk3Mag.CalState = compassCalState;
2018 - 677
		sendOutData('w', MK3MAG_ADDRESS, 1,(uint8_t *) &toMk3Mag,sizeof(toMk3Mag));
1821 - 678
		// the last state is 5 and should be send only once to avoid multiple flash writing
679
		if(compassCalState > 4) compassCalState = 0;
2018 - 680
		compass_timer = setDelay(99);
1821 - 681
	}
1612 dongfang 682
#endif
683
 
2018 - 684
	if (request_motorTest && txd_complete) {
685
		sendOutData('T', FC_ADDRESS, 0);
686
		request_motorTest = FALSE;
1821 - 687
	}
1775 - 688
 
1821 - 689
	if (request_PPMChannels && txd_complete) {
2018 - 690
		sendOutData('P', FC_ADDRESS, 1, (uint8_t *) &PPM_in, sizeof(PPM_in));
1821 - 691
		request_PPMChannels = FALSE;
692
	}
693
 
694
	if (request_variables && txd_complete) {
2018 - 695
		sendOutData('X', FC_ADDRESS, 1, (uint8_t *) &variables, sizeof(variables));
1821 - 696
		request_variables = FALSE;
697
	}
1612 dongfang 698
}