Rev 77 | Rev 79 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 77 | Rev 78 | ||
---|---|---|---|
1 | /*#######################################################################################*/ |
1 | /*#######################################################################################*/ |
2 | /* !!! THIS IS NOT FREE SOFTWARE !!! */ |
2 | /* !!! THIS IS NOT FREE SOFTWARE !!! */ |
3 | /*#######################################################################################*/ |
3 | /*#######################################################################################*/ |
4 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
4 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
5 | // + Copyright (c) 2008 Ingo Busker, Holger Buss |
5 | // + Copyright (c) 2008 Ingo Busker, Holger Buss |
6 | // + Nur für den privaten Gebrauch |
6 | // + Nur für den privaten Gebrauch |
7 | // + FOR NON COMMERCIAL USE ONLY |
7 | // + FOR NON COMMERCIAL USE ONLY |
8 | // + www.MikroKopter.com |
8 | // + www.MikroKopter.com |
9 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
9 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
10 | // + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), |
10 | // + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), |
11 | // + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist. |
11 | // + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist. |
12 | // + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt |
12 | // + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt |
13 | // + bzgl. der Nutzungsbedingungen aufzunehmen. |
13 | // + bzgl. der Nutzungsbedingungen aufzunehmen. |
14 | // + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen, |
14 | // + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen, |
15 | // + Verkauf von Luftbildaufnahmen, usw. |
15 | // + Verkauf von Luftbildaufnahmen, usw. |
16 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
16 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
17 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
17 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
18 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen |
18 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen |
19 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
19 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
20 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
20 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
21 | // + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
21 | // + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
22 | // + eindeutig als Ursprung verlinkt werden |
22 | // + eindeutig als Ursprung verlinkt werden |
23 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
23 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
24 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
24 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
25 | // + Benutzung auf eigene Gefahr |
25 | // + Benutzung auf eigene Gefahr |
26 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
26 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
27 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
27 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
28 | // + Die PORTIERUNG der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
28 | // + Die PORTIERUNG der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
29 | // + mit unserer Zustimmung zulässig |
29 | // + mit unserer Zustimmung zulässig |
30 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
30 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
31 | // + Teile, die nicht eindeutig mit unserer Lizenzvereinbarung gekennzeichnet sind, unterliegen u.U. eigenen Vereinbarungen: |
31 | // + Teile, die nicht eindeutig mit unserer Lizenzvereinbarung gekennzeichnet sind, unterliegen u.U. eigenen Vereinbarungen: |
32 | // + z.B. Die Funktion printf_P() unterliegt ihrer eigenen Lizenz |
32 | // + z.B. Die Funktion printf_P() unterliegt ihrer eigenen Lizenz |
33 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
33 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
34 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
34 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
35 | // + this list of conditions and the following disclaimer. |
35 | // + this list of conditions and the following disclaimer. |
36 | // + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived |
36 | // + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived |
37 | // + from this software without specific prior written permission. |
37 | // + from this software without specific prior written permission. |
38 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permitted |
38 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permitted |
39 | // + for non-commercial use (directly or indirectly) |
39 | // + for non-commercial use (directly or indirectly) |
40 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
40 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
41 | // + with our written permission |
41 | // + with our written permission |
42 | // + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
42 | // + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
43 | // + clearly linked as origin |
43 | // + clearly linked as origin |
44 | // + * PORTING this software (or part of it) to systems (other than hardware from www.mikrokopter.de) is NOT allowed |
44 | // + * PORTING this software (or part of it) to systems (other than hardware from www.mikrokopter.de) is NOT allowed |
45 | // |
45 | // |
46 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
46 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
47 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
47 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
48 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
48 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
49 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
49 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
50 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
50 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
51 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
51 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
52 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
52 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
53 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
53 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
54 | // + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
54 | // + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
55 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
55 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
56 | // + POSSIBILITY OF SUCH DAMAGE. |
56 | // + POSSIBILITY OF SUCH DAMAGE. |
57 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
57 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
58 | //#define MCLK96MHZ |
58 | //#define MCLK96MHZ |
59 | const unsigned long _Main_Crystal = 25000; |
59 | const unsigned long _Main_Crystal = 25000; |
60 | #include <stdio.h> |
60 | #include <stdio.h> |
61 | #include "91x_lib.h" |
61 | #include "91x_lib.h" |
62 | #include "led.h" |
62 | #include "led.h" |
63 | #include "uart0.h" |
63 | #include "uart0.h" |
64 | #include "uart1.h" |
64 | #include "uart1.h" |
65 | #include "uart2.h" |
65 | #include "uart2.h" |
66 | #include "GPS.h" |
66 | #include "GPS.h" |
67 | #include "i2c.h" |
67 | #include "i2c.h" |
68 | #include "timer.h" |
68 | #include "timer.h" |
69 | #include "spi_slave.h" |
69 | #include "spi_slave.h" |
70 | #include "fat16.h" |
70 | #include "fat16.h" |
71 | #include "usb.h" |
71 | #include "usb.h" |
72 | #include "sdc.h" |
72 | #include "sdc.h" |
73 | #include "kml.h" |
73 | #include "kml.h" |
74 | #include "main.h" |
74 | #include "main.h" |
75 | 75 | ||
76 | 76 | ||
77 | typedef enum |
77 | typedef enum |
78 | { |
78 | { |
79 | LOGFILE_IDLE, |
79 | LOGFILE_IDLE, |
- | 80 | LOGFILE_START, |
|
80 | LOGFILE_CLOSED, |
81 | LOGFILE_CLOSED, |
81 | LOGFILE_OPENED, |
82 | LOGFILE_OPENED, |
82 | LOGFILE_ERROR, |
83 | LOGFILE_ERROR, |
83 | } logfilestate_t; |
84 | } logfilestate_t; |
84 | 85 | ||
85 | u32 TimerCompassUpdate; |
86 | u32 TimerCompassUpdate; |
86 | u32 TimerKmlAddPointDelay; |
87 | u32 TimerKmlAddPointDelay; |
87 | u32 TimerCheckError; |
88 | u32 TimerCheckError; |
88 | u32 ErrorCode = 0; |
89 | u32 ErrorCode = 0; |
89 | u16 BeepTime; |
90 | u16 BeepTime; |
90 | 91 | ||
91 | u8 ClearMKFlags = 0; |
92 | u8 ClearMKFlags = 0; |
92 | u8 StopNavigation = 0; |
93 | u8 StopNavigation = 0; |
93 | Param_t Parameter; |
94 | Param_t Parameter; |
94 | FC_t FC; |
95 | volatile FC_t FC; |
95 | 96 | ||
96 | s8 ErrorMSG[25]; |
97 | s8 ErrorMSG[25]; |
97 | 98 | ||
98 | //---------------------------------------------------------------------------------------------------- |
99 | //---------------------------------------------------------------------------------------------------- |
99 | void SCU_Config(void) |
100 | void SCU_Config(void) |
100 | { |
101 | { |
101 | /* configure PLL and set it as master clock source */ |
102 | /* configure PLL and set it as master clock source */ |
102 | SCU_MCLKSourceConfig(SCU_MCLK_OSC); // set master clock source to external oscillator clock (25MHz) before diabling the PLL |
103 | SCU_MCLKSourceConfig(SCU_MCLK_OSC); // set master clock source to external oscillator clock (25MHz) before diabling the PLL |
103 | SCU_PLLCmd(DISABLE); // now disable the PLL |
104 | SCU_PLLCmd(DISABLE); // now disable the PLL |
104 | #ifdef MCLK96MHZ |
105 | #ifdef MCLK96MHZ |
105 | SCU_BRCLKDivisorConfig(SCU_BRCLK_Div2); // set BRCLK to MCLK/2 = 48MHz |
106 | SCU_BRCLKDivisorConfig(SCU_BRCLK_Div2); // set BRCLK to MCLK/2 = 48MHz |
106 | SCU_PCLKDivisorConfig(SCU_PCLK_Div4); // set PCLK (APB bus clock) divisor to 4 (half Reference Clock) |
107 | SCU_PCLKDivisorConfig(SCU_PCLK_Div4); // set PCLK (APB bus clock) divisor to 4 (half Reference Clock) |
107 | SCU_RCLKDivisorConfig(SCU_RCLK_Div2); // set RCLK (Reference Clock) divisor to 1 (full PPL clock) |
108 | SCU_RCLKDivisorConfig(SCU_RCLK_Div2); // set RCLK (Reference Clock) divisor to 1 (full PPL clock) |
108 | SCU_HCLKDivisorConfig(SCU_HCLK_Div2); // set HCLK (AHB bus clock) divisor to 1 (full Reference Clock) |
109 | SCU_HCLKDivisorConfig(SCU_HCLK_Div2); // set HCLK (AHB bus clock) divisor to 1 (full Reference Clock) |
109 | SCU_PLLFactorsConfig(192,25,2); // PLL = 96 MHz, Feedback Divider N=192, Pre-Divider M=25, Post-Divider P=2 |
110 | SCU_PLLFactorsConfig(192,25,2); // PLL = 96 MHz, Feedback Divider N=192, Pre-Divider M=25, Post-Divider P=2 |
110 | #else |
111 | #else |
111 | SCU_BRCLKDivisorConfig(SCU_BRCLK_Div1); // set BRCLK to MCLK = 48MHz |
112 | SCU_BRCLKDivisorConfig(SCU_BRCLK_Div1); // set BRCLK to MCLK = 48MHz |
112 | SCU_PCLKDivisorConfig(SCU_PCLK_Div2); // set PCLK (APB bus clock) divisor to 2 (half Reference Clock) |
113 | SCU_PCLKDivisorConfig(SCU_PCLK_Div2); // set PCLK (APB bus clock) divisor to 2 (half Reference Clock) |
113 | SCU_RCLKDivisorConfig(SCU_RCLK_Div1); // set RCLK (Reference Clock) divisor to 1 (full PPL clock) |
114 | SCU_RCLKDivisorConfig(SCU_RCLK_Div1); // set RCLK (Reference Clock) divisor to 1 (full PPL clock) |
114 | SCU_HCLKDivisorConfig(SCU_HCLK_Div1); // set HCLK (AHB bus clock) divisor to 1 (full Reference Clock) |
115 | SCU_HCLKDivisorConfig(SCU_HCLK_Div1); // set HCLK (AHB bus clock) divisor to 1 (full Reference Clock) |
115 | SCU_PLLFactorsConfig(192,25,3); // PLL = 48 MHz, Feedback Divider N=192, Pre-Divider M=25, Post-Divider P=3 |
116 | SCU_PLLFactorsConfig(192,25,3); // PLL = 48 MHz, Feedback Divider N=192, Pre-Divider M=25, Post-Divider P=3 |
116 | #endif |
117 | #endif |
117 | SCU_PLLCmd(ENABLE); // Enable PLL (is disabled by SCU_PLLFactorsConfig) |
118 | SCU_PLLCmd(ENABLE); // Enable PLL (is disabled by SCU_PLLFactorsConfig) |
118 | SCU_MCLKSourceConfig(SCU_MCLK_PLL); // set master clock source to PLL |
119 | SCU_MCLKSourceConfig(SCU_MCLK_PLL); // set master clock source to PLL |
119 | } |
120 | } |
120 | 121 | ||
121 | //---------------------------------------------------------------------------------------------------- |
122 | //---------------------------------------------------------------------------------------------------- |
122 | void GetNaviCtrlVersion(void) |
123 | void GetNaviCtrlVersion(void) |
123 | { |
124 | { |
124 | u8 msg[25]; |
125 | u8 msg[25]; |
125 | 126 | ||
126 | sprintf(msg,"\n\r NaviCtrl V%d.%d%c", VERSION_MAJOR, VERSION_MINOR, 'a'+ VERSION_PATCH); |
127 | sprintf(msg,"\n\r NaviCtrl V%d.%d%c", VERSION_MAJOR, VERSION_MINOR, 'a'+ VERSION_PATCH); |
127 | SerialPutString(msg); |
128 | SerialPutString(msg); |
128 | } |
129 | } |
129 | 130 | ||
130 | 131 | ||
131 | //---------------------------------------------------------------------------------------------------- |
132 | //---------------------------------------------------------------------------------------------------- |
132 | s8* GenerateGPSLogFileName(void) |
133 | s8* GenerateGPSLogFileName(void) |
133 | { |
134 | { |
134 | static u16 filenum = 0; // file name counter |
135 | static u16 filenum = 0; // file name counter |
135 | static s8 filename[30]; |
136 | static s8 filename[30]; |
136 | static DateTime_t LastTime = {0,0,0,0,0,0,0,0}; |
137 | static DateTime_t LastTime = {0,0,0,0,0,0,0,0}; |
137 | 138 | ||
138 | if(SystemTime.Valid) |
139 | if(SystemTime.Valid) |
139 | { |
140 | { |
140 | // if the day has been changed |
141 | // if the day has been changed |
141 | if((LastTime.Year != SystemTime.Year) || (LastTime.Month != SystemTime.Month) || (LastTime.Day != SystemTime.Day)) |
142 | if((LastTime.Year != SystemTime.Year) || (LastTime.Month != SystemTime.Month) || (LastTime.Day != SystemTime.Day)) |
142 | { |
143 | { |
143 | LastTime.Year = SystemTime.Year; |
144 | LastTime.Year = SystemTime.Year; |
144 | LastTime.Month = SystemTime.Month; |
145 | LastTime.Month = SystemTime.Month; |
145 | LastTime.Day = SystemTime.Day; |
146 | LastTime.Day = SystemTime.Day; |
146 | LastTime.Valid = 1; |
147 | LastTime.Valid = 1; |
147 | filenum = 0; // reset file counter |
148 | filenum = 0; // reset file counter |
148 | } |
149 | } |
149 | sprintf(filename, "LOG/%04i%02i%02i/GPS%05i.KML", SystemTime.Year, SystemTime.Month, SystemTime.Day, filenum); |
150 | sprintf(filename, "LOG/%04i%02i%02i/GPS%05i.KML", SystemTime.Year, SystemTime.Month, SystemTime.Day, filenum); |
150 | filenum++; |
151 | filenum++; |
151 | return filename; |
152 | return filename; |
152 | } |
153 | } |
153 | else return NULL; |
154 | else return NULL; |
154 | } |
155 | } |
155 | 156 | ||
156 | 157 | ||
157 | //---------------------------------------------------------------------------------------------------- |
158 | //---------------------------------------------------------------------------------------------------- |
158 | void CheckErrors(void) |
159 | void CheckErrors(void) |
159 | { |
160 | { |
160 | if(!CheckSPIOkay) |
161 | if(!CheckSPIOkay) |
161 | { |
162 | { |
162 | LED_RED_ON; |
163 | LED_RED_ON; |
163 | sprintf(ErrorMSG,"no FC communication "); |
164 | sprintf(ErrorMSG,"no FC communication "); |
164 | ErrorCode = 3; |
165 | ErrorCode = 3; |
165 | StopNavigation = 1; |
166 | StopNavigation = 1; |
166 | } |
167 | } |
167 | else if(!CheckI2COkay) |
168 | else if(!CheckI2COkay) |
168 | { |
169 | { |
169 | LED_RED_ON; |
170 | LED_RED_ON; |
170 | sprintf(ErrorMSG,"no MK3Mag communication "); |
171 | sprintf(ErrorMSG,"no MK3Mag communication "); |
171 | ErrorCode = 4; |
172 | ErrorCode = 4; |
172 | StopNavigation = 1; |
173 | StopNavigation = 1; |
173 | } |
174 | } |
174 | else if(FC_Version.Compatible != FC_SPI_COMPATIBLE) |
175 | else if(FC_Version.Compatible != FC_SPI_COMPATIBLE) |
175 | { |
176 | { |
176 | LED_RED_ON; |
177 | LED_RED_ON; |
177 | sprintf(ErrorMSG,"FC not compatible "); |
178 | sprintf(ErrorMSG,"FC not compatible "); |
178 | ErrorCode = 1; |
179 | ErrorCode = 1; |
179 | StopNavigation = 1; |
180 | StopNavigation = 1; |
180 | } |
181 | } |
181 | else if(MK3MAG_Version.Compatible != MK3MAG_I2C_COMPATIBLE) |
182 | else if(MK3MAG_Version.Compatible != MK3MAG_I2C_COMPATIBLE) |
182 | { |
183 | { |
183 | sprintf(ErrorMSG,"MK3Mag not compatible "); |
184 | sprintf(ErrorMSG,"MK3Mag not compatible "); |
184 | LED_RED_ON; |
185 | LED_RED_ON; |
185 | ErrorCode = 2; |
186 | ErrorCode = 2; |
186 | StopNavigation = 1; |
187 | StopNavigation = 1; |
187 | } |
188 | } |
188 | else if(!CheckGPSOkay) |
189 | else if(!CheckGPSOkay) |
189 | { |
190 | { |
190 | LED_RED_ON; |
191 | LED_RED_ON; |
191 | sprintf(ErrorMSG,"no GPS communication "); |
192 | sprintf(ErrorMSG,"no GPS communication "); |
192 | ErrorCode = 5; |
193 | ErrorCode = 5; |
193 | StopNavigation = 1; |
194 | StopNavigation = 1; |
194 | } |
195 | } |
195 | else if(I2C_Heading.Heading < 0) |
196 | else if(I2C_Heading.Heading < 0) |
196 | { |
197 | { |
197 | LED_RED_ON; |
198 | LED_RED_ON; |
198 | sprintf(ErrorMSG,"bad compass value "); |
199 | sprintf(ErrorMSG,"bad compass value "); |
199 | ErrorCode = 6; |
200 | ErrorCode = 6; |
200 | StopNavigation = 1; |
201 | StopNavigation = 1; |
201 | } |
202 | } |
202 | else if(FC.RC_Quality < 100) |
203 | else if(FC.RC_Quality < 100) |
203 | { |
204 | { |
204 | LED_RED_ON; |
205 | LED_RED_ON; |
205 | sprintf(ErrorMSG,"RC Signal lost "); |
206 | sprintf(ErrorMSG,"RC Signal lost "); |
206 | ErrorCode = 7; |
207 | ErrorCode = 7; |
207 | } |
208 | } |
208 | else // no error occured |
209 | else // no error occured |
209 | { |
210 | { |
210 | sprintf(ErrorMSG,"No Error "); |
211 | sprintf(ErrorMSG,"No Error "); |
211 | ErrorCode = 0; |
212 | ErrorCode = 0; |
212 | StopNavigation = 0; |
213 | StopNavigation = 0; |
213 | LED_RED_OFF; |
214 | LED_RED_OFF; |
214 | } |
215 | } |
215 | CheckSPIOkay = 0; |
216 | CheckSPIOkay = 0; |
216 | CheckGPSOkay = 0; |
217 | CheckGPSOkay = 0; |
217 | CheckI2COkay = 0; |
218 | CheckI2COkay = 0; |
218 | } |
219 | } |
219 | 220 | ||
220 | 221 | ||
221 | 222 | ||
222 | //---------------------------------------------------------------------------------------------------- |
223 | //---------------------------------------------------------------------------------------------------- |
223 | int main(void) |
224 | int main(void) |
224 | { |
225 | { |
225 | volatile logfilestate_t logfilestate = LOGFILE_IDLE; |
- | |
226 | KML_Document_t gpslogfile; |
226 | KML_Document_t gpslogfile; |
- | 227 | logfilestate_t logfilestate = LOGFILE_IDLE; |
|
227 | s8* gpslogfilename = NULL; |
228 | s8* gpslogfilename = NULL; |
228 | - | ||
229 | /* Configure the system clocks */ |
229 | /* Configure the system clocks */ |
230 | SCU_Config(); |
230 | SCU_Config(); |
231 | - | ||
232 | KML_DocumentInit(&gpslogfile); // Initialize the new kml-document for further use. |
231 | KML_DocumentInit(&gpslogfile); // Initialize the new kml-document for further use. |
233 | - | ||
234 | /* init VIC (Vectored Interrupt Controller) */ |
232 | /* init VIC (Vectored Interrupt Controller) */ |
235 | SCU_AHBPeriphClockConfig(__VIC,ENABLE); // enable AHB bus clock for VIC |
233 | SCU_AHBPeriphClockConfig(__VIC,ENABLE); // enable AHB bus clock for VIC |
236 | SCU_AHBPeriphReset(__VIC, DISABLE); // disable reset state for VIC |
234 | SCU_AHBPeriphReset(__VIC, DISABLE); // disable reset state for VIC |
237 | VIC_DeInit(); // deinitializes the VIC module registers to their default reset values. |
235 | VIC_DeInit(); // deinitializes the VIC module registers to their default reset values. |
238 | // initialize the interrupt handler |
236 | // initialize the interrupt handler |
239 | Interrupt_Init(); |
237 | Interrupt_Init(); |
240 | // initialize timer 1 |
238 | // initialize timer 1 |
241 | TIMER1_Init(); |
239 | TIMER1_Init(); |
242 | // initialize the LEDs (needs Timer 1) |
240 | // initialize the LEDs (needs Timer 1) |
243 | Led_Init(); |
241 | Led_Init(); |
244 | // initialize the debug UART1 |
242 | // initialize the debug UART1 |
245 | UART1_Init(); |
243 | UART1_Init(); |
246 | SerialPutString("\r\n---------------------------------------------"); |
244 | SerialPutString("\r\n---------------------------------------------"); |
247 | - | ||
248 | // initialize UART2 to FLIGHTCTRL |
245 | // initialize UART2 to FLIGHTCTRL |
249 | UART2_Init(); |
246 | UART2_Init(); |
250 | // initialize UART0 (to MKGPS or MK3MAG) |
247 | // initialize UART0 (to MKGPS or MK3MAG) |
251 | UART0_Init(); |
248 | UART0_Init(); |
252 | // initialize usb |
249 | // initialize usb |
253 | USB_ConfigInit(); |
250 | USB_ConfigInit(); |
254 | // initialize SPI0 to FC |
251 | // initialize SPI0 to FC |
255 | SPI0_Init(); |
252 | SPI0_Init(); |
256 | // initialize i2c bus to MK3MAG (needs Timer 1) |
253 | // initialize i2c bus to MK3MAG (needs Timer 1) |
257 | I2C1_Init(); |
254 | I2C1_Init(); |
258 | // initialize the gps position controller (needs Timer 1) |
255 | // initialize the gps position controller (needs Timer 1) |
259 | GPS_Init(); |
256 | GPS_Init(); |
260 | // initialize fat16 partition on sd card (needs Timer 1) |
257 | // initialize fat16 partition on sd card (needs Timer 1) |
261 | Fat16_Init(); |
258 | Fat16_Init(); |
262 | - | ||
263 | 259 | ||
264 | TimerCompassUpdate = SetDelay(5); |
260 | TimerCompassUpdate = SetDelay(5); |
265 | TimerKmlAddPointDelay = SetDelay(250); |
261 | TimerKmlAddPointDelay = SetDelay(250); |
266 | TimerCheckError = SetDelay(3000); |
262 | TimerCheckError = SetDelay(3000); |
267 | - | ||
268 | SerialPutString("\r\n---------------------------------------------"); |
263 | SerialPutString("\r\n---------------------------------------------"); |
269 | SerialPutString("\n\r Version information:"); |
264 | SerialPutString("\n\r Version information:"); |
270 | GetNaviCtrlVersion(); |
265 | GetNaviCtrlVersion(); |
271 | SPI0_GetFlightCtrlVersion(); |
266 | SPI0_GetFlightCtrlVersion(); |
272 | if(FC_Version.Compatible != FC_SPI_COMPATIBLE) |
267 | if(FC_Version.Compatible != FC_SPI_COMPATIBLE) |
273 | { |
268 | { |
274 | //sprintf(ErrorMSG,"\r\n %d <-> %d", FlightCtrlVersion.NaviCompatible, FC_SPI_COMPATIBLE); |
269 | //sprintf(ErrorMSG,"\r\n %d <-> %d", FlightCtrlVersion.NaviCompatible, FC_SPI_COMPATIBLE); |
275 | //SerialPutString(ErrorMSG); |
270 | //SerialPutString(ErrorMSG); |
276 | SerialPutString("\n\r Flight-Ctrl not compatible"); |
271 | SerialPutString("\n\r Flight-Ctrl not compatible"); |
277 | LED_RED_ON; |
272 | LED_RED_ON; |
278 | } |
273 | } |
- | 274 | ||
279 | I2C1_GetMK3MagVersion(); |
275 | I2C1_GetMK3MagVersion(); |
280 | if(MK3MAG_Version.Compatible != MK3MAG_I2C_COMPATIBLE) |
276 | if(MK3MAG_Version.Compatible != MK3MAG_I2C_COMPATIBLE) |
281 | { |
277 | { |
282 | SerialPutString("\n\r MK3Mag not compatible"); |
278 | SerialPutString("\n\r MK3Mag not compatible"); |
283 | LED_RED_ON; |
279 | LED_RED_ON; |
284 | } |
280 | } |
285 | 281 | ||
286 | /* |
282 | /* |
287 | ReadSetting(1); |
283 | ReadSetting(1); |
288 | */ |
284 | */ |
289 | SerialPutString("\r\n---------------------------------------------\r\n\r\n"); |
285 | SerialPutString("\r\n---------------------------------------------\r\n\r\n "); |
290 | 286 | ||
291 | for (;;) |
287 | for (;;) |
292 | { |
288 | { |
293 | if(rxd_buffer_locked) // new incomming command |
289 | if(rxd_buffer_locked) // new incomming command |
294 | { |
290 | { |
295 | UART1_ProcessRxData(); // process request |
291 | UART1_ProcessRxData(); // process request |
296 | } |
292 | } |
297 | UART1_TransmitTxData(); // send answer |
293 | UART1_TransmitTxData(); // send answer |
298 | 294 | ||
299 | SPI0_UpdateBuffer(); |
295 | SPI0_UpdateBuffer(); |
300 | UART0_Transmit(); // empty txd buffer |
296 | UART0_Transmit(); // empty txd buffer |
301 | UART1_Transmit(); // empty txd buffer |
297 | UART1_Transmit(); // empty txd buffer |
302 | 298 | ||
303 | // ------------------------- I2C Timing -------------------------------- |
299 | // ------------------------- I2C Timing -------------------------------- |
304 | if(I2C_State != I2C_OFF) |
300 | if(I2C_State != I2C_OFF) |
305 | { |
301 | { |
306 | if (CheckDelay(TimerCompassUpdate)) |
302 | if (CheckDelay(TimerCompassUpdate)) |
307 | { |
303 | { |
308 | // check for hanging I2C bus |
304 | // check for hanging I2C bus |
309 | if(CheckDelay(I2C1_Timeout)) |
305 | if(CheckDelay(I2C1_Timeout)) |
310 | { // reset I2C |
306 | { // reset I2C |
311 | SerialPutString("\n\rCommunication-Error: MK3Mag\n\r"); |
307 | SerialPutString("\n\rCommunication-Error: MK3Mag\n\r"); |
312 | I2C1_Deinit(); |
308 | I2C1_Deinit(); |
313 | I2C1_Init(); |
309 | I2C1_Init(); |
314 | } |
310 | } |
315 | else |
311 | else |
316 | { // check for incomming compass calibration request |
312 | { // check for incomming compass calibration request |
317 | // update CalByte from spi input queue |
313 | // update CalByte from spi input queue |
318 | fifo_get(&CompassCalcStateFiFo, (u8 *)&(I2C_WriteCal.CalByte)); |
314 | fifo_get(&CompassCalcStateFiFo, (u8 *)&(I2C_WriteCal.CalByte)); |
319 | // send new calstate |
315 | // send new calstate |
320 | if(I2C_ReadCal.CalByte != I2C_WriteCal.CalByte) |
316 | if(I2C_ReadCal.CalByte != I2C_WriteCal.CalByte) |
321 | { |
317 | { |
322 | I2C1_SendCommand(I2C_CMD_WRITE_CAL); |
318 | I2C1_SendCommand(I2C_CMD_WRITE_CAL); |
323 | } |
319 | } |
324 | else // request current heading |
320 | else // request current heading |
325 | { |
321 | { |
326 | I2C1_SendCommand(I2C_CMD_READ_HEADING); |
322 | I2C1_SendCommand(I2C_CMD_READ_HEADING); |
327 | } |
323 | } |
328 | TimerCompassUpdate = SetDelay(25); // every 25 ms |
324 | TimerCompassUpdate = SetDelay(25); // every 25 ms |
329 | } |
325 | } |
330 | 326 | ||
331 | } |
327 | } |
332 | } |
328 | } |
333 | 329 | ||
334 | // ---------------- Error Check Timing ---------------------------- |
330 | // ---------------- Error Check Timing ---------------------------- |
335 | if(CheckDelay(TimerCheckError)) |
331 | if(CheckDelay(TimerCheckError)) |
336 | { |
332 | { |
337 | if(!CheckSPIOkay) GPS_Navigation(); // process the GPS data even if the FC is not connected |
333 | if(!CheckSPIOkay) GPS_Navigation(); // process the GPS data even if the FC is not connected |
338 | CheckErrors(); |
334 | CheckErrors(); |
339 | if(FC.MKFlags & MKFLAG_FLY) NaviData.FlyingTime++; // we want to count the battery-time |
335 | if(FC.MKFlags & MKFLAG_FLY) NaviData.FlyingTime++; // we want to count the battery-time |
340 | // else NaviData.FlyingTime = 0; |
336 | // else NaviData.FlyingTime = 0; |
341 | TimerCheckError = SetDelay(1000); |
337 | TimerCheckError = SetDelay(1000); |
342 | Request_SendFollowMe = TRUE; |
338 | Request_SendFollowMe = TRUE; |
343 | } |
339 | } |
344 | // ---------------- KML Timing ------------------------------------ |
340 | // ---------------- KML Timing ------------------------------------ |
345 | if(CheckDelay(TimerKmlAddPointDelay)) |
341 | if(CheckDelay(TimerKmlAddPointDelay)) |
346 | { |
342 | { |
347 | TimerKmlAddPointDelay = SetDelay(500); // every 500 ms |
343 | TimerKmlAddPointDelay = SetDelay(500); // every 500 ms |
- | 344 | ||
348 | if(FC.MKFlags & MKFLAG_FLY) // model is flying |
345 | if(FC.MKFlags & MKFLAG_MOTOR_RUN) |
349 | { |
346 | { |
350 | switch(logfilestate) |
347 | switch(logfilestate) |
351 | { |
348 | { |
352 | case LOGFILE_IDLE: |
349 | case LOGFILE_IDLE: |
353 | case LOGFILE_CLOSED: |
350 | case LOGFILE_CLOSED: |
- | 351 | if((GPSData.Status != INVALID) && (GPSData.Flags & FLAG_GPSFIXOK) && (GPSData.SatFix == SATFIX_3D) && (FC.MKFlags & MKFLAG_FLY)) |
|
- | 352 | logfilestate = LOGFILE_START; |
|
- | 353 | break; |
|
- | 354 | case LOGFILE_START: |
|
354 | // find unused logfile name |
355 | // find unused logfile name |
355 | do |
356 | do |
356 | { // try to generate a new logfile name |
357 | { // try to generate a new logfile name |
357 | gpslogfilename = GenerateGPSLogFileName(); |
358 | gpslogfilename = GenerateGPSLogFileName(); |
358 | }while((gpslogfilename != NULL) && fexist_(gpslogfilename)); |
359 | }while((gpslogfilename != NULL) && fexist_(gpslogfilename)); |
359 | // if logfilename exist |
360 | // if logfilename exist |
360 | if(gpslogfilename != NULL) |
361 | if(gpslogfilename != NULL) |
361 | { |
362 | { |
362 | // try to create the log file |
363 | // try to create the log file |
363 | if(KML_DocumentOpen(gpslogfilename, &gpslogfile)) |
364 | if(KML_DocumentOpen(gpslogfilename, &gpslogfile)) |
364 | { |
365 | { |
365 | logfilestate = LOGFILE_OPENED; // goto next step |
366 | logfilestate = LOGFILE_OPENED; // goto next step |
366 | SerialPutString("\r\nOpening kml-file:"); |
367 | SerialPutString("\r\nOpening kml-file:"); |
367 | SerialPutString(gpslogfilename); |
368 | SerialPutString(gpslogfilename); |
368 | SerialPutString("\r\n"); |
369 | SerialPutString("\r\n"); |
369 | } |
370 | } |
370 | else |
371 | else |
371 | { |
372 | { |
372 | logfilestate = LOGFILE_ERROR; |
373 | logfilestate = LOGFILE_ERROR; |
373 | SerialPutString("\r\nError opening kml-file: "); |
374 | SerialPutString("\r\nError opening kml-file: "); |
374 | SerialPutString(gpslogfilename); |
375 | SerialPutString(gpslogfilename); |
375 | SerialPutString("\r\n"); |
376 | SerialPutString("\r\n"); |
376 | TimerKmlAddPointDelay = SetDelay(10); // try again in open logfile in 10 mili sec |
377 | TimerKmlAddPointDelay = SetDelay(10); // try again in open logfile in 10 mili sec |
377 | } |
378 | } |
378 | } |
379 | } |
379 | // else retry in next loop |
380 | // else retry in next loop |
380 | break; |
381 | break; |
381 | case LOGFILE_OPENED: |
382 | case LOGFILE_OPENED: |
382 | // append new gps log data |
383 | // append new gps log data |
383 | if((GPSData.Status != INVALID) && (GPSData.Flags & FLAG_GPSFIXOK) && (GPSData.SatFix == SATFIX_3D)) |
384 | if((GPSData.Status != INVALID) && (GPSData.Flags & FLAG_GPSFIXOK) && (GPSData.SatFix == SATFIX_3D)) |
384 | { |
385 | { |
385 | if(!KML_LoggGPSCoordinates(&(GPSData.Position), &gpslogfile)) |
386 | if(!KML_LoggGPSCoordinates(&(GPSData.Position), &gpslogfile)) |
386 | { // error logging data |
387 | { // error logging data |
387 | SerialPutString("\r\nError Logging GPS data\r\n"); |
388 | SerialPutString("\r\nError Logging GPS data\r\n"); |
388 | KML_DocumentClose(&gpslogfile); |
389 | KML_DocumentClose(&gpslogfile); |
389 | logfilestate = LOGFILE_ERROR; |
390 | logfilestate = LOGFILE_ERROR; |
390 | } |
391 | } |
391 | else DebugOut.Analog[26]++; |
392 | else DebugOut.Analog[26]++; // count SD-Logs |
392 | } |
393 | } |
393 | break; |
394 | break; |
394 | 395 | ||
395 | case LOGFILE_ERROR: |
396 | case LOGFILE_ERROR: |
396 | // try to reinitialize the fat16 filesystem |
397 | // try to reinitialize the fat16 filesystem |
397 | if(0 == Fat16_Init()) |
398 | if(0 == Fat16_Init()) |
398 | { |
399 | { |
399 | logfilestate = LOGFILE_IDLE; |
400 | logfilestate = LOGFILE_IDLE; |
400 | TimerKmlAddPointDelay = SetDelay(10); // try again in open logfile in 10 mili sec |
401 | TimerKmlAddPointDelay = SetDelay(10); // try again in open logfile in 10 mili sec |
401 | } |
402 | } |
402 | else |
403 | else |
403 | { |
404 | { |
404 | logfilestate = LOGFILE_ERROR; |
405 | logfilestate = LOGFILE_ERROR; |
405 | TimerKmlAddPointDelay = SetDelay(5000); // try again in 5 sec |
406 | TimerKmlAddPointDelay = SetDelay(5000); // try again in 5 sec |
406 | } |
407 | } |
407 | break; |
408 | break; |
408 | 409 | ||
409 | default: |
410 | default: |
410 | break; |
411 | break; |
411 | } |
412 | } |
412 | } // EOF model is flying |
413 | } // EOF model is flying |
413 | else // model is not flying |
414 | else // model is not flying |
414 | { // close log file if opened |
415 | { // close log file if opened |
415 | if(logfilestate == LOGFILE_OPENED) |
416 | if(logfilestate == LOGFILE_OPENED) |
416 | { |
417 | { |
417 | if(KML_DocumentClose(&gpslogfile)) |
418 | if(KML_DocumentClose(&gpslogfile)) |
418 | { |
419 | { |
419 | SerialPutString("\r\nClosing kml-file\r\n"); |
420 | SerialPutString("\r\nClosing kml-file\r\n"); |
420 | logfilestate = LOGFILE_CLOSED; |
421 | logfilestate = LOGFILE_CLOSED; |
421 | } |
422 | } |
422 | else |
423 | else |
423 | { |
424 | { |
424 | logfilestate = LOGFILE_ERROR; |
425 | logfilestate = LOGFILE_ERROR; |
425 | } |
426 | } |
426 | } |
427 | } |
427 | } //EOF model is not flying |
428 | } //EOF model is not flying |
428 | } |
429 | } |
429 | } |
430 | } |
430 | } |
431 | } |
431 | 432 | ||
432 | 433 | ||
433 | 434 |