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