Rev 502 | Rev 514 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
321 | cascade | 1 | /**************************************************************************** |
2 | * Copyright (C) 2009 by Claas Anders "CaScAdE" Rathje * |
||
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" |
||
331 | cascade | 34 | #include "usart1.h" |
389 | cascade | 35 | #include "osd_helpers.h" |
471 | cascade | 36 | #include "config.h" |
37 | #include "spi.h" |
||
38 | #include "buttons.h" |
||
477 | cascade | 39 | #include "ppm.h" |
497 | cascade | 40 | #if !(ALLCHARSDEBUG|(WRITECHARS != -1)) |
41 | #include "osd_ncmode_default.h" |
||
42 | #include "osd_ncmode_minimal.h" |
||
43 | #include "osd_fcmode_default.h" |
||
44 | #endif |
||
321 | cascade | 45 | |
46 | /* TODO: |
||
47 | * - verifiy correctness of values |
||
324 | cascade | 48 | * - clean up code :) |
321 | cascade | 49 | */ |
50 | |||
51 | |||
52 | /* ########################################################################## |
||
53 | * global definitions and global vars |
||
54 | * ##########################################################################*/ |
||
346 | cascade | 55 | |
321 | cascade | 56 | volatile uint16_t setsReceived = 0; |
57 | |||
58 | volatile NaviData_t naviData; |
||
59 | volatile DebugOut_t debugData; |
||
60 | |||
61 | // cache old vars for blinking attribute, checkup is faster than full |
||
62 | // attribute write each time |
||
63 | volatile uint8_t last_UBat = 255; |
||
64 | volatile uint8_t last_RC_Quality = 255; |
||
65 | |||
66 | // 16bit should be enough, normal LiPos don't last that long |
||
67 | volatile uint16_t uptime = 0; |
||
68 | volatile uint16_t timer = 0; |
||
69 | |||
331 | cascade | 70 | // remember last time data was received |
71 | volatile uint8_t seconds_since_last_data = 0; |
||
72 | |||
497 | cascade | 73 | // general PAL|NTSC distingiusch stuff |
74 | uint8_t top_line = 1; |
||
75 | uint8_t bottom_line = 14; |
||
76 | |||
77 | // battery voltages |
||
78 | uint8_t min_voltage = 0; |
||
79 | uint8_t max_voltage = 0; |
||
80 | |||
81 | // Flags |
||
82 | uint8_t COSD_FLAGS = 0, COSD_FLAGS2 = 0; |
||
83 | |||
84 | // stats for after flight |
||
85 | int16_t max_Altimeter = 0; |
||
86 | uint8_t min_UBat = 255; |
||
87 | uint16_t max_GroundSpeed = 0; |
||
88 | int16_t max_Distance = 0; |
||
89 | uint16_t max_FlyingTime = 0; |
||
90 | |||
91 | // flags from last round to check for changes |
||
92 | uint8_t old_MKFlags = 0; |
||
93 | |||
346 | cascade | 94 | // store stats description in progmem to save space |
489 | woggle | 95 | const char stats_item_0[] PROGMEM = "max Altitude:"; |
96 | const char stats_item_1[] PROGMEM = "max Speed :"; |
||
97 | const char stats_item_2[] PROGMEM = "max Distance:"; |
||
98 | const char stats_item_3[] PROGMEM = "min Voltage :"; |
||
99 | const char stats_item_4[] PROGMEM = "max Time :"; |
||
100 | const char stats_item_5[] PROGMEM = "longitude :"; |
||
101 | const char stats_item_6[] PROGMEM = "latitude :"; |
||
102 | const char stats_item_7[] PROGMEM = "max current :"; |
||
499 | cascade | 103 | const char *stats_item_pointers[8] PROGMEM = {stats_item_0, stats_item_1, stats_item_2, |
467 | cascade | 104 | stats_item_3, stats_item_4, stats_item_5, stats_item_6, stats_item_7}; |
346 | cascade | 105 | |
489 | woggle | 106 | //char* directions[8] = {"NE", "E ", "SE", "S ", "SW", "W ", "NW", "N "}; |
107 | //char arrowdir[8] = {218, 217, 224, 223, 222, 221, 220, 219}; |
||
108 | const char str_NE[] PROGMEM = "NE"; |
||
109 | const char str_E[] PROGMEM = "E "; |
||
110 | const char str_SE[] PROGMEM = "SE"; |
||
111 | const char str_S[] PROGMEM = "S "; |
||
112 | const char str_SW[] PROGMEM = "SW"; |
||
113 | const char str_W[] PROGMEM = "W "; |
||
114 | const char str_NW[] PROGMEM = "NW"; |
||
115 | const char str_N[] PROGMEM = "N "; |
||
116 | const char *directions[8] PROGMEM = { |
||
117 | str_NE, |
||
118 | str_E, |
||
119 | str_SE, |
||
120 | str_S, |
||
121 | str_SW, |
||
122 | str_W, |
||
123 | str_NW, |
||
124 | str_N}; |
||
125 | |||
497 | cascade | 126 | /* ########################################################################## |
127 | * Different display mode function pointers |
||
128 | * ##########################################################################*/ |
||
129 | #if !(ALLCHARSDEBUG|(WRITECHARS != -1)) |
||
130 | const char str_1[] PROGMEM = "default"; |
||
131 | const char str_2[] PROGMEM = "minimal"; |
||
326 | cascade | 132 | |
497 | cascade | 133 | const displaymode_t ncdisplaymodes[] PROGMEM = { |
134 | { osd_ncmode_default, (char *)str_1 }, |
||
135 | { osd_ncmode_minimal, (char *)str_2 } |
||
136 | }; |
||
326 | cascade | 137 | |
497 | cascade | 138 | const displaymode_t fcdisplaymodes[] PROGMEM = { |
139 | { osd_fcmode_default, (char *)str_1 }, |
||
140 | }; |
||
453 | cascade | 141 | |
497 | cascade | 142 | int (*osd_ncmode)(void) = (int(*)(void)) &osd_ncmode_default; |
143 | int (*osd_fcmode)(void) = (int(*)(void)) &osd_ncmode_default; |
||
144 | #endif |
||
326 | cascade | 145 | |
321 | cascade | 146 | /* ########################################################################## |
389 | cascade | 147 | * Interrupt handler |
148 | * ##########################################################################*/ |
||
455 | cascade | 149 | |
321 | cascade | 150 | /** |
389 | cascade | 151 | * handler for undefined Interrupts |
152 | * if not defined AVR will reset in case any unhandled interrupts occur |
||
321 | cascade | 153 | */ |
389 | cascade | 154 | ISR(__vector_default) { |
455 | cascade | 155 | asm("nop"); |
389 | cascade | 156 | } |
321 | cascade | 157 | |
389 | cascade | 158 | #if !(ALLCHARSDEBUG|(WRITECHARS != -1)) |
321 | cascade | 159 | /* ########################################################################## |
160 | * timer stuff |
||
161 | * ##########################################################################*/ |
||
455 | cascade | 162 | |
507 | cascade | 163 | static uint8_t delay_spi = 0; |
164 | |||
466 | cascade | 165 | /** |
331 | cascade | 166 | * timer kicks in every 1000uS ^= 1ms |
321 | cascade | 167 | */ |
489 | woggle | 168 | ISR(TIMER0_COMP_vect) { |
321 | cascade | 169 | if (!timer--) { |
170 | uptime++; |
||
171 | timer = 999; |
||
455 | cascade | 172 | seconds_since_last_data++; |
321 | cascade | 173 | } |
466 | cascade | 174 | // in case there is still some spi data to send do it now |
507 | cascade | 175 | // delay to give the slave some time to compute values |
466 | cascade | 176 | if (spi_ready && icnt) { |
507 | cascade | 177 | if (!delay_spi--) { |
178 | delay_spi = 8; |
||
179 | spi_send_next(); |
||
180 | } |
||
466 | cascade | 181 | } |
321 | cascade | 182 | } |
465 | cascade | 183 | #endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1))#endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1)) |
455 | cascade | 184 | |
321 | cascade | 185 | /* ########################################################################## |
186 | * MAIN |
||
187 | * ##########################################################################*/ |
||
188 | int main(void) { |
||
466 | cascade | 189 | |
339 | cascade | 190 | // set up FLAGS, compiler should flatten this one |
191 | COSD_FLAGS = (NTSC << 0); |
||
192 | COSD_FLAGS |= (HUD << 1); |
||
193 | COSD_FLAGS |= (ARTHORIZON << 2); |
||
455 | cascade | 194 | COSD_FLAGS |= (BIGVARIO << 3); |
195 | COSD_FLAGS |= (STATS << 4); |
||
383 | cascade | 196 | COSD_FLAGS |= (WARNINGS << 5); |
324 | cascade | 197 | |
339 | cascade | 198 | // set up Atmega162 Ports |
321 | cascade | 199 | DDRA |= (1 << PA1); // PA1 output (/CS) |
200 | MAX_CS_HIGH |
||
201 | DDRA |= (1 << PA2); // PA2 output (SDIN) |
||
202 | MAX_SDIN_LOW |
||
203 | DDRA |= (1 << PA3); // PA3 output (SCLK) |
||
204 | MAX_SCLK_LOW |
||
205 | DDRA |= (1 << PA5); // PA5 output (RESET) |
||
206 | MAX_RESET_HIGH |
||
207 | |||
208 | DDRC |= (1 << PC0); // PC0 output (LED1 gn) |
||
209 | LED1_OFF |
||
210 | DDRC |= (1 << PC1); // PC1 output (LED2 rt) |
||
211 | LED2_OFF |
||
212 | DDRC |= (1 << PC2); // PC2 output (LED3 gn) |
||
213 | LED3_OFF |
||
214 | DDRC |= (1 << PC3); // PC3 output (LED4 rt) |
||
215 | LED4_OFF |
||
216 | |||
217 | DDRC &= ~(1 << PC4); // PC4 input (MODE) |
||
218 | PORTC |= (1 << PC4); // pullup |
||
219 | DDRC &= ~(1 << PC5); // PC5 input (SET) |
||
220 | PORTC |= (1 << PC5); // pullup |
||
221 | |||
339 | cascade | 222 | // reset the MAX7456 to be sure any undefined states do no harm |
321 | cascade | 223 | MAX_RESET_LOW |
224 | MAX_RESET_HIGH |
||
225 | |||
226 | // give the FC/NC and the maxim time to come up |
||
227 | LED4_ON |
||
228 | _delay_ms(2000); |
||
229 | LED4_OFF |
||
230 | |||
339 | cascade | 231 | //Pushing NEW chars to the MAX7456 |
329 | cascade | 232 | #if (WRITECHARS != -1) |
474 | cascade | 233 | // DISABLE display (VM0) |
234 | spi_send_byte(0x00, 0b00000000); |
||
466 | cascade | 235 | #include "characters.c" |
474 | cascade | 236 | #else |
237 | // read out config for NTSC/PAL distinguishing |
||
238 | get_eeprom(0); |
||
455 | cascade | 239 | #endif |
321 | cascade | 240 | |
474 | cascade | 241 | // Setup Video Mode |
242 | if (COSD_FLAGS & COSD_FLAG_NTSC) { |
||
243 | // NTSC + enable display immediately (VM0) |
||
244 | spi_send_byte(0x00, 0b00001000); |
||
321 | cascade | 245 | |
474 | cascade | 246 | bottom_line = 12; |
247 | } else { |
||
248 | // PAL + enable display immediately (VM0) |
||
249 | spi_send_byte(0x00, 0b01001000); |
||
250 | |||
251 | bottom_line = 14; |
||
252 | } |
||
253 | |||
404 | cascade | 254 | /*// clear all display-mem (DMM) |
403 | cascade | 255 | spi_send_byte(0x04, 0b00000100); |
256 | |||
257 | // clearing takes 12uS according to maxim so lets wait longer |
||
258 | _delay_us(120); |
||
259 | |||
321 | cascade | 260 | // 8bit mode |
404 | cascade | 261 | spi_send_byte(0x04, 0b01000000);*/ |
321 | cascade | 262 | |
404 | cascade | 263 | // clear display memory and set to 8bit mode |
321 | cascade | 264 | clear(); |
265 | |||
329 | cascade | 266 | #if !(ALLCHARSDEBUG|(WRITECHARS != -1)) |
321 | cascade | 267 | // init usart |
268 | usart1_init(); |
||
269 | |||
270 | // set up timer |
||
489 | woggle | 271 | // CTC, Prescaler /64 |
272 | TCCR0 = (1 << WGM01) | (0 << WGM00) | (0 << CS02) | (1 << CS01) | (1 << CS00); |
||
321 | cascade | 273 | |
489 | woggle | 274 | TCNT0 = 0; |
275 | OCR0 = 250; |
||
276 | |||
277 | // enable timer output compare interrupt |
||
278 | TIMSK &= ~(1 << TOIE0); |
||
279 | TIMSK |= (1 << OCIE0); |
||
280 | |||
477 | cascade | 281 | // SPI setup |
466 | cascade | 282 | DDRD |= (1 << PD2); // PD2 output (INT0) |
283 | SpiMasterInit(); |
||
465 | cascade | 284 | |
477 | cascade | 285 | // PPM detection setup |
286 | ppm_init(); |
||
287 | |||
326 | cascade | 288 | // enable interrupts |
477 | cascade | 289 | |
321 | cascade | 290 | sei(); |
291 | #endif |
||
292 | |||
293 | //write_ascii_string(2, 7, " CaScAdE "); |
||
294 | //write_ascii_string(2, 8, "is TESTING his open source"); |
||
295 | //write_ascii_string(2, 9, " EPi OSD Firmware"); |
||
296 | |||
297 | // we are ready |
||
298 | LED3_ON |
||
299 | |||
329 | cascade | 300 | #if ALLCHARSDEBUG | (WRITECHARS != -1) |
471 | cascade | 301 | clear(); |
502 | cascade | 302 | write_all_chars(); |
303 | LED1_ON |
||
304 | LED2_ON |
||
305 | LED3_ON |
||
306 | LED4_ON |
||
321 | cascade | 307 | #else |
477 | cascade | 308 | |
471 | cascade | 309 | // clear serial screen |
310 | //usart1_puts("\x1B[2J\x1B[H"); |
||
477 | cascade | 311 | //usart1_puts("hello world!123\r\n"); |
312 | |||
454 | cascade | 313 | #if FCONLY |
471 | cascade | 314 | // request data ever 100ms from FC; |
315 | usart1_request_mk_data(0, 'd', 100); |
||
454 | cascade | 316 | #else |
471 | cascade | 317 | // request OSD Data from NC every 100ms |
318 | usart1_request_mk_data(1, 'o', 100); |
||
346 | cascade | 319 | |
339 | cascade | 320 | // and disable debug... |
455 | cascade | 321 | usart1_request_mk_data(0, 'd', 0); |
454 | cascade | 322 | #endif |
321 | cascade | 323 | |
324 | while (1) { |
||
466 | cascade | 325 | // in case SPI is ready and there is nothing to send right now |
326 | if (!icnt && spi_ready) { |
||
327 | // correct transfer ends with d (done) |
||
507 | cascade | 328 | if (SPI_buffer.buffer.chk == 'd') { |
329 | ampere = SPI_buffer.data.ampere; |
||
330 | ampere_wasted = SPI_buffer.data.mah; |
||
331 | s_volt = SPI_buffer.data.volt; |
||
332 | |||
467 | cascade | 333 | // if this is the first receival we should print the small A |
334 | if (!(COSD_FLAGS2 & COSD_FLAG_STROMREC)) { |
||
474 | cascade | 335 | clear(); |
471 | cascade | 336 | COSD_FLAGS2 &= ~COSD_ICONS_WRITTEN; |
467 | cascade | 337 | // update this flag |
338 | COSD_FLAGS2 |= COSD_FLAG_STROMREC; |
||
339 | } |
||
466 | cascade | 340 | } else { |
341 | // update flags |
||
342 | COSD_FLAGS2 &= ~COSD_FLAG_STROMREC; |
||
343 | } |
||
507 | cascade | 344 | StartTransfer(9); |
466 | cascade | 345 | } |
321 | cascade | 346 | if (rxd_buffer_locked) { |
454 | cascade | 347 | #if FCONLY |
471 | cascade | 348 | if (rxd_buffer[2] == 'D') { // FC Data |
349 | Decode64(); |
||
350 | debugData = *((DebugOut_t*) pRxData); |
||
454 | cascade | 351 | |
471 | cascade | 352 | // init on first data retrival, distinguished by last battery :) |
353 | if (last_UBat == 255) { |
||
354 | // fix for min_UBat |
||
355 | min_UBat = debugData.Analog[9]; |
||
356 | init_cosd(debugData.Analog[9]); |
||
497 | cascade | 357 | } |
358 | osd_fcmode(); |
||
471 | cascade | 359 | } |
454 | cascade | 360 | #else |
471 | cascade | 361 | if (rxd_buffer[2] == 'O') { // NC OSD Data |
362 | Decode64(); |
||
363 | naviData = *((NaviData_t*) pRxData); |
||
453 | cascade | 364 | |
471 | cascade | 365 | // init on first data retrival, distinguished by last battery :) |
366 | if (last_UBat == 255) { |
||
367 | // fix for min_UBat |
||
368 | min_UBat = naviData.UBat; |
||
369 | init_cosd(naviData.UBat); |
||
466 | cascade | 370 | } |
497 | cascade | 371 | osd_ncmode(); |
471 | cascade | 372 | } |
454 | cascade | 373 | #endif |
321 | cascade | 374 | rxd_buffer_locked = 0; |
375 | } |
||
376 | // handle keypress |
||
326 | cascade | 377 | if (s1_pressed()) { |
339 | cascade | 378 | config_menu(); |
321 | cascade | 379 | } |
339 | cascade | 380 | if (seconds_since_last_data > 2) { |
454 | cascade | 381 | #if FCONLY |
455 | cascade | 382 | // request data ever 100ms from FC; |
383 | usart1_request_mk_data(0, 'd', 100); |
||
454 | cascade | 384 | #else |
455 | cascade | 385 | // request OSD Data from NC every 100ms |
386 | usart1_request_mk_data(1, 'o', 100); |
||
454 | cascade | 387 | |
455 | cascade | 388 | // and disable debug... |
389 | usart1_request_mk_data(0, 'd', 0); |
||
477 | cascade | 390 | |
391 | // reset last time counter |
||
392 | seconds_since_last_data = 0; |
||
454 | cascade | 393 | #endif |
339 | cascade | 394 | } |
321 | cascade | 395 | } |
396 | #endif |
||
397 | return 0; |
||
398 | } |