Rev 783 | Rev 800 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
321 | cascade | 1 | /**************************************************************************** |
728 | cascade | 2 | * Copyright (C) 2009-2010 by Claas Anders "CaScAdE" Rathje * |
321 | cascade | 3 | * admiralcascade@gmail.com * |
4 | * Project-URL: http://www.mylifesucks.de/oss/c-osd/ * |
||
5 | * * |
||
6 | * This program is free software; you can redistribute it and/or modify * |
||
7 | * it under the terms of the GNU General Public License as published by * |
||
8 | * the Free Software Foundation; either version 2 of the License. * |
||
9 | * * |
||
10 | * This program is distributed in the hope that it will be useful, * |
||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
||
13 | * GNU General Public License for more details. * |
||
14 | * * |
||
15 | * You should have received a copy of the GNU General Public License * |
||
16 | * along with this program; if not, write to the * |
||
17 | * Free Software Foundation, Inc., * |
||
18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
||
19 | * * |
||
20 | * * |
||
21 | * Credits to: * |
||
471 | cascade | 22 | * Holger Buss & Ingo Busker from mikrokopter.de for the MK project + SVN * |
321 | cascade | 23 | * Gregor "killagreg" Stobrawa for making the MK code readable * |
24 | * Klaus "akku" Buettner for the hardware * |
||
25 | * Manuel "KeyOz" Schrape for explaining the MK protocol to me * |
||
26 | ****************************************************************************/ |
||
27 | |||
28 | #include <avr/io.h> |
||
29 | #include <avr/interrupt.h> |
||
30 | #include <util/delay.h> |
||
455 | cascade | 31 | #include <avr/pgmspace.h> |
346 | cascade | 32 | #include "main.h" |
33 | #include "max7456_software_spi.h" |
||
772 | - | 34 | #ifdef ANTENNATRACKTEST |
35 | #include "usart0.h" |
||
36 | #endif |
||
331 | cascade | 37 | #include "usart1.h" |
389 | cascade | 38 | #include "osd_helpers.h" |
471 | cascade | 39 | #include "config.h" |
40 | #include "spi.h" |
||
41 | #include "buttons.h" |
||
477 | cascade | 42 | #include "ppm.h" |
497 | cascade | 43 | #include "osd_ncmode_default.h" |
44 | #include "osd_ncmode_minimal.h" |
||
45 | #include "osd_fcmode_default.h" |
||
685 | cascade | 46 | #include "osd_fcmode_jopl.h" |
514 | cascade | 47 | |
48 | #if WRITECHARS != -1 |
||
49 | #include "characters.h" |
||
497 | cascade | 50 | #endif |
321 | cascade | 51 | |
52 | /* TODO: |
||
53 | * - verifiy correctness of values |
||
324 | cascade | 54 | * - clean up code :) |
321 | cascade | 55 | */ |
56 | |||
57 | |||
58 | /* ########################################################################## |
||
59 | * global definitions and global vars |
||
60 | * ##########################################################################*/ |
||
762 | - | 61 | #if !(ALLCHARSDEBUG|(WRITECHARS != -1)) |
346 | cascade | 62 | |
321 | cascade | 63 | volatile NaviData_t naviData; |
783 | - | 64 | volatile str_DebugOut debugData; |
321 | cascade | 65 | |
66 | // cache old vars for blinking attribute, checkup is faster than full |
||
67 | // attribute write each time |
||
68 | volatile uint8_t last_UBat = 255; |
||
69 | volatile uint8_t last_RC_Quality = 255; |
||
70 | |||
71 | // 16bit should be enough, normal LiPos don't last that long |
||
72 | volatile uint16_t uptime = 0; |
||
73 | volatile uint16_t timer = 0; |
||
685 | cascade | 74 | volatile uint16_t flytime_fc = 0; |
321 | cascade | 75 | |
331 | cascade | 76 | // remember last time data was received |
77 | volatile uint8_t seconds_since_last_data = 0; |
||
78 | |||
497 | cascade | 79 | // general PAL|NTSC distingiusch stuff |
80 | uint8_t top_line = 1; |
||
81 | uint8_t bottom_line = 14; |
||
82 | |||
83 | // battery voltages |
||
84 | uint8_t min_voltage = 0; |
||
85 | uint8_t max_voltage = 0; |
||
86 | |||
87 | // Flags |
||
528 | cascade | 88 | uint8_t COSD_FLAGS_MODES = 0, COSD_FLAGS_CONFIG = 0, COSD_FLAGS_RUNTIME = 0, COSD_DISPLAYMODE = 0; |
497 | cascade | 89 | |
514 | cascade | 90 | |
762 | - | 91 | |
497 | cascade | 92 | // stats for after flight |
93 | int16_t max_Altimeter = 0; |
||
94 | uint8_t min_UBat = 255; |
||
95 | uint16_t max_GroundSpeed = 0; |
||
96 | int16_t max_Distance = 0; |
||
97 | uint16_t max_FlyingTime = 0; |
||
98 | |||
99 | // flags from last round to check for changes |
||
100 | uint8_t old_MKFlags = 0; |
||
101 | |||
346 | cascade | 102 | // store stats description in progmem to save space |
489 | woggle | 103 | const char stats_item_0[] PROGMEM = "max Altitude:"; |
104 | const char stats_item_1[] PROGMEM = "max Speed :"; |
||
105 | const char stats_item_2[] PROGMEM = "max Distance:"; |
||
106 | const char stats_item_3[] PROGMEM = "min Voltage :"; |
||
107 | const char stats_item_4[] PROGMEM = "max Time :"; |
||
108 | const char stats_item_5[] PROGMEM = "longitude :"; |
||
109 | const char stats_item_6[] PROGMEM = "latitude :"; |
||
110 | const char stats_item_7[] PROGMEM = "max current :"; |
||
499 | cascade | 111 | const char *stats_item_pointers[8] PROGMEM = {stats_item_0, stats_item_1, stats_item_2, |
467 | cascade | 112 | stats_item_3, stats_item_4, stats_item_5, stats_item_6, stats_item_7}; |
346 | cascade | 113 | |
489 | woggle | 114 | //char* directions[8] = {"NE", "E ", "SE", "S ", "SW", "W ", "NW", "N "}; |
761 | - | 115 | //char arrowdir[8] = {218, 217, 224, 223, 222, 221, 220, 219}; |
489 | woggle | 116 | const char str_NE[] PROGMEM = "NE"; |
761 | - | 117 | const char str_E[] PROGMEM = "E "; |
489 | woggle | 118 | const char str_SE[] PROGMEM = "SE"; |
761 | - | 119 | const char str_S[] PROGMEM = "S "; |
489 | woggle | 120 | const char str_SW[] PROGMEM = "SW"; |
761 | - | 121 | const char str_W[] PROGMEM = "W "; |
489 | woggle | 122 | const char str_NW[] PROGMEM = "NW"; |
761 | - | 123 | const char str_N[] PROGMEM = "N "; |
489 | woggle | 124 | const char *directions[8] PROGMEM = { |
761 | - | 125 | str_NE, |
126 | str_E, |
||
127 | str_SE, |
||
128 | str_S, |
||
129 | str_SW, |
||
130 | str_W, |
||
131 | str_NW, |
||
132 | str_N |
||
133 | }; |
||
489 | woggle | 134 | |
497 | cascade | 135 | /* ########################################################################## |
136 | * Different display mode function pointers |
||
137 | * ##########################################################################*/ |
||
761 | - | 138 | const char str_1[] PROGMEM = "default"; |
139 | const char str_2[] PROGMEM = "minimal"; |
||
140 | const char str_3[] PROGMEM = " jopl"; |
||
326 | cascade | 141 | |
783 | - | 142 | #if FCONLY |
497 | cascade | 143 | const displaymode_t fcdisplaymodes[] PROGMEM = { |
761 | - | 144 | { osd_fcmode_default, (char *)str_1}, |
145 | { osd_fcmode_jopl, (char *)str_3} |
||
497 | cascade | 146 | }; |
783 | - | 147 | int (*osd_fcmode)(void) = (int(*)(void)) & osd_fcmode_default; |
148 | #else |
||
453 | cascade | 149 | |
783 | - | 150 | const displaymode_t ncdisplaymodes[] PROGMEM = { |
151 | { osd_ncmode_default, (char *)str_1}, |
||
152 | { osd_ncmode_minimal, (char *)str_2} |
||
153 | }; |
||
761 | - | 154 | int (*osd_ncmode)(void) = (int(*)(void)) & osd_ncmode_default; |
497 | cascade | 155 | #endif |
326 | cascade | 156 | |
783 | - | 157 | |
158 | #endif |
||
159 | |||
321 | cascade | 160 | /* ########################################################################## |
389 | cascade | 161 | * Interrupt handler |
162 | * ##########################################################################*/ |
||
455 | cascade | 163 | |
321 | cascade | 164 | /** |
389 | cascade | 165 | * handler for undefined Interrupts |
166 | * if not defined AVR will reset in case any unhandled interrupts occur |
||
321 | cascade | 167 | */ |
389 | cascade | 168 | ISR(__vector_default) { |
455 | cascade | 169 | asm("nop"); |
389 | cascade | 170 | } |
321 | cascade | 171 | |
389 | cascade | 172 | #if !(ALLCHARSDEBUG|(WRITECHARS != -1)) |
321 | cascade | 173 | /* ########################################################################## |
174 | * timer stuff |
||
175 | * ##########################################################################*/ |
||
455 | cascade | 176 | |
507 | cascade | 177 | static uint8_t delay_spi = 0; |
178 | |||
466 | cascade | 179 | /** |
331 | cascade | 180 | * timer kicks in every 1000uS ^= 1ms |
321 | cascade | 181 | */ |
489 | woggle | 182 | ISR(TIMER0_COMP_vect) { |
321 | cascade | 183 | if (!timer--) { |
184 | uptime++; |
||
685 | cascade | 185 | |
761 | - | 186 | #if FCONLY |
187 | if (debugData.Analog[12] > 10) { |
||
188 | flytime_fc++; |
||
189 | } |
||
190 | #endif |
||
685 | cascade | 191 | |
321 | cascade | 192 | timer = 999; |
455 | cascade | 193 | seconds_since_last_data++; |
321 | cascade | 194 | } |
466 | cascade | 195 | // in case there is still some spi data to send do it now |
761 | - | 196 | // delay to give the slave some time to compute values |
466 | cascade | 197 | if (spi_ready && icnt) { |
761 | - | 198 | if (!delay_spi--) { |
199 | delay_spi = 8; |
||
200 | spi_send_next(); |
||
201 | } |
||
466 | cascade | 202 | } |
321 | cascade | 203 | } |
762 | - | 204 | #endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1)) |
455 | cascade | 205 | |
321 | cascade | 206 | /* ########################################################################## |
207 | * MAIN |
||
208 | * ##########################################################################*/ |
||
209 | int main(void) { |
||
761 | - | 210 | // set up FLAGS |
211 | COSD_FLAGS_MODES = 0, COSD_FLAGS_CONFIG = 0, COSD_FLAGS_RUNTIME = 0; |
||
212 | #if NTSC |
||
213 | COSD_FLAGS_CONFIG |= COSD_FLAG_NTSC; |
||
214 | #endif |
||
215 | #if HUD |
||
216 | COSD_FLAGS_MODES |= COSD_FLAG_HUD; |
||
217 | #endif |
||
218 | #if ARTHORIZON |
||
219 | COSD_FLAGS_MODES |= COSD_FLAG_ARTHORIZON; |
||
220 | #endif |
||
221 | #if BIGVARIO |
||
222 | COSD_FLAGS_MODES |= COSD_FLAG_BIGARIO; |
||
223 | #endif |
||
224 | #if STATS |
||
225 | COSD_FLAGS_MODES |= COSD_FLAG_STATS; |
||
226 | #endif |
||
227 | #if WARNINGS |
||
228 | COSD_FLAGS_MODES |= COSD_FLAG_WARNINGS; |
||
229 | #endif |
||
230 | #if FCONLY |
||
231 | COSD_FLAGS_CONFIG |= COSD_FLAG_FCMODE; |
||
232 | #endif |
||
466 | cascade | 233 | |
324 | cascade | 234 | |
339 | cascade | 235 | // set up Atmega162 Ports |
772 | - | 236 | DDRA |= ((1 << PA1) | (1 << PA2) | (1 << PA3) | (1 << PA5)); // PA1 output (/CS) | PA2 output (SDIN) | PA3 output (SCLK) | PA5 output (RESET) |
321 | cascade | 237 | MAX_CS_HIGH |
238 | MAX_SDIN_LOW |
||
239 | MAX_SCLK_LOW |
||
240 | MAX_RESET_HIGH |
||
241 | |||
772 | - | 242 | DDRC |= ((1 << PC0) | (1 << PC1) | (1 << PC2) | (1 << PC3)); // PC0 output (LED1 gn) | PC1 output (LED2 rt) | PC2 output (LED3 gn) | PC3 output (LED4 rt) |
321 | cascade | 243 | LED1_OFF |
244 | LED2_OFF |
||
245 | LED3_OFF |
||
246 | LED4_OFF |
||
247 | |||
772 | - | 248 | DDRC &= ~((1 << PC4) | (1 << PC5)); // PC4 input (MODE) | PC5 input (SET) |
249 | PORTC |= ((1 << PC4) | (1 << PC5)); // pullup |
||
321 | cascade | 250 | |
339 | cascade | 251 | // reset the MAX7456 to be sure any undefined states do no harm |
321 | cascade | 252 | MAX_RESET_LOW |
253 | MAX_RESET_HIGH |
||
254 | |||
255 | // give the FC/NC and the maxim time to come up |
||
256 | LED4_ON |
||
736 | cascade | 257 | _delay_ms(1000); |
321 | cascade | 258 | LED4_OFF |
259 | |||
339 | cascade | 260 | //Pushing NEW chars to the MAX7456 |
329 | cascade | 261 | #if (WRITECHARS != -1) |
761 | - | 262 | // DISABLE display (VM0) |
263 | spi_send_byte(0x00, 0b00000000); |
||
264 | learn_all_chars_pgm(); |
||
474 | cascade | 265 | #else |
761 | - | 266 | // read out config for NTSC/PAL distinguishing |
267 | get_eeprom(0); |
||
455 | cascade | 268 | #endif |
321 | cascade | 269 | |
761 | - | 270 | // Setup Video Mode |
271 | if (COSD_FLAGS_CONFIG & COSD_FLAG_NTSC) { |
||
272 | // NTSC + enable display immediately (VM0) |
||
273 | spi_send_byte(0x00, 0b00001000); |
||
321 | cascade | 274 | |
761 | - | 275 | bottom_line = 12; |
276 | } else { |
||
277 | // PAL + enable display immediately (VM0) |
||
278 | spi_send_byte(0x00, 0b01001000); |
||
474 | cascade | 279 | |
761 | - | 280 | bottom_line = 14; |
281 | } |
||
474 | cascade | 282 | |
404 | cascade | 283 | /*// clear all display-mem (DMM) |
403 | cascade | 284 | spi_send_byte(0x04, 0b00000100); |
285 | |||
286 | // clearing takes 12uS according to maxim so lets wait longer |
||
287 | _delay_us(120); |
||
288 | |||
321 | cascade | 289 | // 8bit mode |
404 | cascade | 290 | spi_send_byte(0x04, 0b01000000);*/ |
321 | cascade | 291 | |
404 | cascade | 292 | // clear display memory and set to 8bit mode |
321 | cascade | 293 | clear(); |
294 | |||
329 | cascade | 295 | #if !(ALLCHARSDEBUG|(WRITECHARS != -1)) |
321 | cascade | 296 | // init usart |
297 | usart1_init(); |
||
298 | |||
772 | - | 299 | |
300 | |||
301 | #ifdef ANTENNATRACKTEST |
||
302 | usart0_init(); |
||
303 | #endif |
||
304 | |||
738 | cascade | 305 | // keep serial port clean |
306 | usart1_DisableTXD(); |
||
307 | |||
321 | cascade | 308 | // set up timer |
761 | - | 309 | // CTC, Prescaler /64 |
310 | TCCR0 = (1 << WGM01) | (0 << WGM00) | (0 << CS02) | (1 << CS01) | (1 << CS00); |
||
321 | cascade | 311 | |
761 | - | 312 | TCNT0 = 0; |
313 | OCR0 = 250; |
||
489 | woggle | 314 | |
761 | - | 315 | // enable timer output compare interrupt |
316 | TIMSK &= ~(1 << TOIE0); |
||
317 | TIMSK |= (1 << OCIE0); |
||
489 | woggle | 318 | |
761 | - | 319 | // SPI setup |
466 | cascade | 320 | DDRD |= (1 << PD2); // PD2 output (INT0) |
321 | SpiMasterInit(); |
||
465 | cascade | 322 | |
761 | - | 323 | // PPM detection setup |
324 | ppm_init(); |
||
477 | cascade | 325 | |
326 | cascade | 326 | // enable interrupts |
321 | cascade | 327 | sei(); |
328 | |||
762 | - | 329 | |
321 | cascade | 330 | //write_ascii_string(2, 7, " CaScAdE "); |
331 | //write_ascii_string(2, 8, "is TESTING his open source"); |
||
332 | //write_ascii_string(2, 9, " EPi OSD Firmware"); |
||
333 | |||
334 | // we are ready |
||
335 | LED3_ON |
||
336 | |||
477 | cascade | 337 | |
762 | - | 338 | // clear serial screen |
339 | //usart1_puts("\x1B[2J\x1B[H"); |
||
477 | cascade | 340 | |
787 | - | 341 | #if !FCONLY |
342 | usart1_request_nc_uart(); |
||
343 | #endif |
||
344 | |||
321 | cascade | 345 | while (1) { |
466 | cascade | 346 | // in case SPI is ready and there is nothing to send right now |
347 | if (!icnt && spi_ready) { |
||
348 | // correct transfer ends with d (done) |
||
507 | cascade | 349 | if (SPI_buffer.buffer.chk == 'd') { |
761 | - | 350 | ampere = SPI_buffer.data.ampere; |
351 | ampere_wasted = SPI_buffer.data.mah; |
||
352 | s_volt = SPI_buffer.data.volt; |
||
507 | cascade | 353 | |
761 | - | 354 | // if this is the first receival we should print the small A |
355 | if (!(COSD_FLAGS_RUNTIME & COSD_FLAG_STROMREC)) { |
||
356 | clear(); |
||
357 | COSD_FLAGS_RUNTIME &= ~COSD_ICONS_WRITTEN; |
||
358 | // update this flag |
||
359 | COSD_FLAGS_RUNTIME |= COSD_FLAG_STROMREC; |
||
360 | } |
||
466 | cascade | 361 | } else { |
362 | // update flags |
||
528 | cascade | 363 | COSD_FLAGS_RUNTIME &= ~COSD_FLAG_STROMREC; |
466 | cascade | 364 | } |
761 | - | 365 | StartTransfer(9); |
466 | cascade | 366 | } |
321 | cascade | 367 | if (rxd_buffer_locked) { |
454 | cascade | 368 | #if FCONLY |
471 | cascade | 369 | if (rxd_buffer[2] == 'D') { // FC Data |
370 | Decode64(); |
||
783 | - | 371 | debugData = *((str_DebugOut*)pRxData); |
454 | cascade | 372 | |
471 | cascade | 373 | // init on first data retrival, distinguished by last battery :) |
374 | if (last_UBat == 255) { |
||
761 | - | 375 | if (debugData.Analog[9] > 40) { |
376 | // fix for min_UBat |
||
377 | min_UBat = debugData.Analog[9]; |
||
378 | last_UBat = debugData.Analog[9]; |
||
379 | init_cosd(last_UBat); |
||
380 | } |
||
677 | cascade | 381 | } else { |
761 | - | 382 | osd_fcmode(); |
383 | } |
||
384 | seconds_since_last_data = 0; |
||
471 | cascade | 385 | } |
454 | cascade | 386 | #else |
471 | cascade | 387 | if (rxd_buffer[2] == 'O') { // NC OSD Data |
388 | Decode64(); |
||
761 | - | 389 | naviData = *((NaviData_t*)pRxData); |
453 | cascade | 390 | |
471 | cascade | 391 | // init on first data retrival, distinguished by last battery :) |
392 | if (last_UBat == 255) { |
||
761 | - | 393 | if (naviData.UBat > 40) { |
394 | // fix for min_UBat |
||
395 | min_UBat = naviData.UBat; |
||
396 | last_UBat = naviData.UBat; |
||
397 | init_cosd(last_UBat); |
||
398 | } |
||
677 | cascade | 399 | } else { |
761 | - | 400 | osd_ncmode(); |
401 | } |
||
402 | //seconds_since_last_data = 0; |
||
471 | cascade | 403 | } |
454 | cascade | 404 | #endif |
772 | - | 405 | |
406 | // ONLY FOR TESTING |
||
407 | #ifdef ANTENNATRACKTEST |
||
408 | #include <stdlib.h> |
||
409 | //#include <float.h> |
||
410 | //#include <math.h> |
||
411 | |||
412 | //usart0_puts("\x1B[2J\x1B[H"); |
||
413 | |||
414 | |||
415 | /* |
||
416 | naviData.HomePositionDeviation.Distance = 23 * 100; // 23m away (cm * 100) |
||
417 | naviData.HomePositionDeviation.Bearing = 35; // 35° |
||
418 | altimeter_offset = 50; // 50m start height |
||
419 | naviData.CurrentPosition.Altitude = (int32_t) ((int32_t)250 * (int32_t)1000); // 250m height (mm * 1000) |
||
420 | */ |
||
421 | |||
422 | static char conv_array[7]; |
||
423 | |||
424 | // should be float |
||
425 | int tanheight = (naviData.HomePositionDeviation.Distance * 100) / (naviData.CurrentPosition.Altitude - altimeter_offset); |
||
426 | |||
427 | // we need math.h and some faster AVR :) |
||
428 | //tanheight = rad2deg(atan(tanheight)); |
||
429 | |||
430 | itoa((naviData.HomePositionDeviation.Bearing + 180) % 360, conv_array, 10); |
||
431 | usart0_puts("Bearing: "); |
||
432 | usart0_puts(conv_array); |
||
433 | usart0_puts("\tHeightangle: "); |
||
434 | itoa(tanheight, conv_array, 10); |
||
435 | usart0_puts(conv_array); |
||
436 | usart0_puts("\r\n"); |
||
437 | #endif |
||
438 | |||
439 | |||
321 | cascade | 440 | rxd_buffer_locked = 0; |
441 | } |
||
442 | // handle keypress |
||
326 | cascade | 443 | if (s1_pressed()) { |
339 | cascade | 444 | config_menu(); |
321 | cascade | 445 | } |
738 | cascade | 446 | if (seconds_since_last_data > 0) { |
761 | - | 447 | usart1_EnableTXD(); |
448 | //usart1_puts_pgm(PSTR("zu alt\r\n")); |
||
454 | cascade | 449 | #if FCONLY |
455 | cascade | 450 | // request data ever 100ms from FC; |
738 | cascade | 451 | //usart1_request_mk_data(0, 'd', 100); |
761 | - | 452 | usart1_puts_pgm(PSTR(REQUEST_DBG_DATA)); |
783 | - | 453 | #else |
455 | cascade | 454 | // request OSD Data from NC every 100ms |
738 | cascade | 455 | //usart1_request_mk_data(1, 'o', 100); |
761 | - | 456 | usart1_puts_pgm(PSTR(REQUEST_OSD_DATA)); |
454 | cascade | 457 | |
455 | cascade | 458 | // and disable debug... |
738 | cascade | 459 | //usart1_request_mk_data(0, 'd', 0); |
460 | #endif |
||
761 | - | 461 | // reset last time counter |
462 | seconds_since_last_data = 0; |
||
463 | usart1_DisableTXD(); |
||
339 | cascade | 464 | } |
321 | cascade | 465 | } |
762 | - | 466 | |
467 | #else // character flashing... |
||
468 | clear(); |
||
469 | write_all_chars(); |
||
470 | LED1_ON |
||
471 | LED2_ON |
||
472 | LED3_ON |
||
473 | LED4_ON |
||
474 | while(1) { |
||
475 | |||
476 | }; |
||
321 | cascade | 477 | #endif |
762 | - | 478 | |
479 | |||
321 | cascade | 480 | return 0; |
481 | } |