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