Rev 79 | Rev 83 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 79 | Rev 82 | ||
---|---|---|---|
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 "logging.h" |
74 | #include "main.h" |
74 | #include "main.h" |
75 | - | ||
76 | - | ||
77 | typedef enum |
- | |
78 | { |
- | |
79 | LOGFILE_IDLE, |
- | |
80 | LOGFILE_START, |
- | |
81 | LOGFILE_CLOSED, |
- | |
82 | LOGFILE_OPENED, |
- | |
83 | LOGFILE_ERROR, |
- | |
84 | } logfilestate_t; |
- | |
85 | 75 | ||
86 | u32 TimerCompassUpdate; |
76 | u32 TimerCompassUpdate; |
87 | u32 TimerKmlAddPointDelay; |
77 | u32 TimerLogDelay; |
88 | u32 TimerCheckError; |
78 | u32 TimerCheckError; |
89 | u32 ErrorCode = 0; |
79 | u32 ErrorCode = 0; |
90 | u16 BeepTime; |
80 | u16 BeepTime; |
91 | 81 | ||
92 | u8 ClearMKFlags = 0; |
82 | u8 ClearMKFlags = 0; |
93 | u8 StopNavigation = 0; |
83 | u8 StopNavigation = 0; |
94 | Param_t Parameter; |
84 | Param_t Parameter; |
95 | volatile FC_t FC; |
85 | volatile FC_t FC; |
96 | 86 | ||
97 | s8 ErrorMSG[25]; |
87 | s8 ErrorMSG[25]; |
98 | 88 | ||
99 | //---------------------------------------------------------------------------------------------------- |
89 | //---------------------------------------------------------------------------------------------------- |
100 | void SCU_Config(void) |
90 | void SCU_Config(void) |
101 | { |
91 | { |
102 | /* configure PLL and set it as master clock source */ |
92 | /* configure PLL and set it as master clock source */ |
103 | SCU_MCLKSourceConfig(SCU_MCLK_OSC); // set master clock source to external oscillator clock (25MHz) before diabling the PLL |
93 | SCU_MCLKSourceConfig(SCU_MCLK_OSC); // set master clock source to external oscillator clock (25MHz) before diabling the PLL |
104 | SCU_PLLCmd(DISABLE); // now disable the PLL |
94 | SCU_PLLCmd(DISABLE); // now disable the PLL |
105 | #ifdef MCLK96MHZ |
95 | #ifdef MCLK96MHZ |
106 | SCU_BRCLKDivisorConfig(SCU_BRCLK_Div2); // set BRCLK to MCLK/2 = 48MHz |
96 | SCU_BRCLKDivisorConfig(SCU_BRCLK_Div2); // set BRCLK to MCLK/2 = 48MHz |
107 | SCU_PCLKDivisorConfig(SCU_PCLK_Div4); // set PCLK (APB bus clock) divisor to 4 (half Reference Clock) |
97 | SCU_PCLKDivisorConfig(SCU_PCLK_Div4); // set PCLK (APB bus clock) divisor to 4 (half Reference Clock) |
108 | SCU_RCLKDivisorConfig(SCU_RCLK_Div2); // set RCLK (Reference Clock) divisor to 1 (full PPL clock) |
98 | SCU_RCLKDivisorConfig(SCU_RCLK_Div2); // set RCLK (Reference Clock) divisor to 1 (full PPL clock) |
109 | SCU_HCLKDivisorConfig(SCU_HCLK_Div2); // set HCLK (AHB bus clock) divisor to 1 (full Reference Clock) |
99 | SCU_HCLKDivisorConfig(SCU_HCLK_Div2); // set HCLK (AHB bus clock) divisor to 1 (full Reference Clock) |
110 | SCU_PLLFactorsConfig(192,25,2); // PLL = 96 MHz, Feedback Divider N=192, Pre-Divider M=25, Post-Divider P=2 |
100 | SCU_PLLFactorsConfig(192,25,2); // PLL = 96 MHz, Feedback Divider N=192, Pre-Divider M=25, Post-Divider P=2 |
111 | #else |
101 | #else |
112 | SCU_BRCLKDivisorConfig(SCU_BRCLK_Div1); // set BRCLK to MCLK = 48MHz |
102 | SCU_BRCLKDivisorConfig(SCU_BRCLK_Div1); // set BRCLK to MCLK = 48MHz |
113 | SCU_PCLKDivisorConfig(SCU_PCLK_Div2); // set PCLK (APB bus clock) divisor to 2 (half Reference Clock) |
103 | SCU_PCLKDivisorConfig(SCU_PCLK_Div2); // set PCLK (APB bus clock) divisor to 2 (half Reference Clock) |
114 | SCU_RCLKDivisorConfig(SCU_RCLK_Div1); // set RCLK (Reference Clock) divisor to 1 (full PPL clock) |
104 | SCU_RCLKDivisorConfig(SCU_RCLK_Div1); // set RCLK (Reference Clock) divisor to 1 (full PPL clock) |
115 | SCU_HCLKDivisorConfig(SCU_HCLK_Div1); // set HCLK (AHB bus clock) divisor to 1 (full Reference Clock) |
105 | SCU_HCLKDivisorConfig(SCU_HCLK_Div1); // set HCLK (AHB bus clock) divisor to 1 (full Reference Clock) |
116 | SCU_PLLFactorsConfig(192,25,3); // PLL = 48 MHz, Feedback Divider N=192, Pre-Divider M=25, Post-Divider P=3 |
106 | SCU_PLLFactorsConfig(192,25,3); // PLL = 48 MHz, Feedback Divider N=192, Pre-Divider M=25, Post-Divider P=3 |
117 | #endif |
107 | #endif |
118 | SCU_PLLCmd(ENABLE); // Enable PLL (is disabled by SCU_PLLFactorsConfig) |
108 | SCU_PLLCmd(ENABLE); // Enable PLL (is disabled by SCU_PLLFactorsConfig) |
119 | SCU_MCLKSourceConfig(SCU_MCLK_PLL); // set master clock source to PLL |
109 | SCU_MCLKSourceConfig(SCU_MCLK_PLL); // set master clock source to PLL |
120 | } |
110 | } |
121 | 111 | ||
122 | //---------------------------------------------------------------------------------------------------- |
112 | //---------------------------------------------------------------------------------------------------- |
123 | void GetNaviCtrlVersion(void) |
113 | void GetNaviCtrlVersion(void) |
124 | { |
114 | { |
125 | u8 msg[25]; |
115 | u8 msg[25]; |
126 | 116 | ||
127 | sprintf(msg,"\n\r NaviCtrl V%d.%d%c", VERSION_MAJOR, VERSION_MINOR, 'a'+ VERSION_PATCH); |
117 | sprintf(msg,"\n\r NaviCtrl V%d.%d%c", VERSION_MAJOR, VERSION_MINOR, 'a'+ VERSION_PATCH); |
128 | SerialPutString(msg); |
118 | SerialPutString(msg); |
129 | } |
119 | } |
130 | 120 | ||
131 | 121 | ||
132 | //---------------------------------------------------------------------------------------------------- |
122 | //---------------------------------------------------------------------------------------------------- |
133 | s8* GenerateGPSLogFileName(void) |
123 | s8* GenerateGPSLogFileName(void) |
134 | { |
124 | { |
135 | static u16 filenum = 0; // file name counter |
125 | static u16 filenum = 0; // file name counter |
136 | static s8 filename[30]; |
126 | static s8 filename[30]; |
137 | static DateTime_t LastTime = {0,0,0,0,0,0,0,0}; |
127 | static DateTime_t LastTime = {0,0,0,0,0,0,0,0}; |
138 | 128 | ||
139 | if(SystemTime.Valid) |
129 | if(SystemTime.Valid) |
140 | { |
130 | { |
141 | // if the day has been changed |
131 | // if the day has been changed |
142 | if((LastTime.Year != SystemTime.Year) || (LastTime.Month != SystemTime.Month) || (LastTime.Day != SystemTime.Day)) |
132 | if((LastTime.Year != SystemTime.Year) || (LastTime.Month != SystemTime.Month) || (LastTime.Day != SystemTime.Day)) |
143 | { |
133 | { |
144 | LastTime.Year = SystemTime.Year; |
134 | LastTime.Year = SystemTime.Year; |
145 | LastTime.Month = SystemTime.Month; |
135 | LastTime.Month = SystemTime.Month; |
146 | LastTime.Day = SystemTime.Day; |
136 | LastTime.Day = SystemTime.Day; |
147 | LastTime.Valid = 1; |
137 | LastTime.Valid = 1; |
148 | filenum = 0; // reset file counter |
138 | filenum = 0; // reset file counter |
149 | } |
139 | } |
150 | sprintf(filename, "LOG/%04i%02i%02i/GPS%05i.KML", SystemTime.Year, SystemTime.Month, SystemTime.Day, filenum); |
140 | sprintf(filename, "LOG/%04i%02i%02i/GPS%05i.KML", SystemTime.Year, SystemTime.Month, SystemTime.Day, filenum); |
151 | filenum++; |
141 | filenum++; |
152 | return filename; |
142 | return filename; |
153 | } |
143 | } |
154 | else return NULL; |
144 | else return NULL; |
155 | } |
145 | } |
156 | 146 | ||
157 | 147 | ||
158 | //---------------------------------------------------------------------------------------------------- |
148 | //---------------------------------------------------------------------------------------------------- |
159 | void CheckErrors(void) |
149 | void CheckErrors(void) |
160 | { |
150 | { |
161 | if(!CheckSPIOkay) |
151 | if(!CheckSPIOkay) |
162 | { |
152 | { |
163 | LED_RED_ON; |
153 | LED_RED_ON; |
164 | sprintf(ErrorMSG,"no FC communication "); |
154 | sprintf(ErrorMSG,"no FC communication "); |
165 | ErrorCode = 3; |
155 | ErrorCode = 3; |
166 | StopNavigation = 1; |
156 | StopNavigation = 1; |
167 | } |
157 | } |
168 | else if(!CheckI2COkay) |
158 | else if(!CheckI2COkay) |
169 | { |
159 | { |
170 | LED_RED_ON; |
160 | LED_RED_ON; |
171 | sprintf(ErrorMSG,"no MK3Mag communication "); |
161 | sprintf(ErrorMSG,"no MK3Mag communication "); |
172 | ErrorCode = 4; |
162 | ErrorCode = 4; |
173 | StopNavigation = 1; |
163 | StopNavigation = 1; |
174 | } |
164 | } |
175 | else if(FC_Version.Compatible != FC_SPI_COMPATIBLE) |
165 | else if(FC_Version.Compatible != FC_SPI_COMPATIBLE) |
176 | { |
166 | { |
177 | LED_RED_ON; |
167 | LED_RED_ON; |
178 | sprintf(ErrorMSG,"FC not compatible "); |
168 | sprintf(ErrorMSG,"FC not compatible "); |
179 | ErrorCode = 1; |
169 | ErrorCode = 1; |
180 | StopNavigation = 1; |
170 | StopNavigation = 1; |
181 | } |
171 | } |
182 | else if(MK3MAG_Version.Compatible != MK3MAG_I2C_COMPATIBLE) |
172 | else if(MK3MAG_Version.Compatible != MK3MAG_I2C_COMPATIBLE) |
183 | { |
173 | { |
184 | sprintf(ErrorMSG,"MK3Mag not compatible "); |
174 | sprintf(ErrorMSG,"MK3Mag not compatible "); |
185 | LED_RED_ON; |
175 | LED_RED_ON; |
186 | ErrorCode = 2; |
176 | ErrorCode = 2; |
187 | StopNavigation = 1; |
177 | StopNavigation = 1; |
188 | } |
178 | } |
189 | else if(!CheckGPSOkay) |
179 | else if(!CheckGPSOkay) |
190 | { |
180 | { |
191 | LED_RED_ON; |
181 | LED_RED_ON; |
192 | sprintf(ErrorMSG,"no GPS communication "); |
182 | sprintf(ErrorMSG,"no GPS communication "); |
193 | ErrorCode = 5; |
183 | ErrorCode = 5; |
194 | StopNavigation = 1; |
184 | StopNavigation = 1; |
195 | } |
185 | } |
196 | else if(I2C_Heading.Heading < 0) |
186 | else if(I2C_Heading.Heading < 0) |
197 | { |
187 | { |
198 | LED_RED_ON; |
188 | LED_RED_ON; |
199 | sprintf(ErrorMSG,"bad compass value "); |
189 | sprintf(ErrorMSG,"bad compass value "); |
200 | ErrorCode = 6; |
190 | ErrorCode = 6; |
201 | StopNavigation = 1; |
191 | StopNavigation = 1; |
202 | } |
192 | } |
203 | else if(FC.RC_Quality < 100) |
193 | else if(FC.RC_Quality < 100) |
204 | { |
194 | { |
205 | LED_RED_ON; |
195 | LED_RED_ON; |
206 | sprintf(ErrorMSG,"RC Signal lost "); |
196 | sprintf(ErrorMSG,"RC Signal lost "); |
207 | ErrorCode = 7; |
197 | ErrorCode = 7; |
208 | } |
198 | } |
209 | else // no error occured |
199 | else // no error occured |
210 | { |
200 | { |
211 | sprintf(ErrorMSG,"No Error "); |
201 | sprintf(ErrorMSG,"No Error "); |
212 | ErrorCode = 0; |
202 | ErrorCode = 0; |
213 | StopNavigation = 0; |
203 | StopNavigation = 0; |
214 | LED_RED_OFF; |
204 | LED_RED_OFF; |
215 | } |
205 | } |
216 | CheckSPIOkay = 0; |
206 | CheckSPIOkay = 0; |
217 | CheckGPSOkay = 0; |
207 | CheckGPSOkay = 0; |
218 | CheckI2COkay = 0; |
208 | CheckI2COkay = 0; |
219 | } |
209 | } |
220 | 210 | ||
221 | 211 | ||
222 | 212 | ||
223 | //---------------------------------------------------------------------------------------------------- |
213 | //---------------------------------------------------------------------------------------------------- |
224 | int main(void) |
214 | int main(void) |
225 | { |
215 | { |
226 | KML_Document_t gpslogfile; |
- | |
227 | logfilestate_t logfilestate = LOGFILE_IDLE; |
- | |
228 | s8* gpslogfilename = NULL; |
- | |
229 | /* Configure the system clocks */ |
216 | /* Configure the system clocks */ |
230 | SCU_Config(); |
217 | SCU_Config(); |
231 | KML_DocumentInit(&gpslogfile); // Initialize the new kml-document for further use. |
- | |
232 | /* init VIC (Vectored Interrupt Controller) */ |
218 | /* init VIC (Vectored Interrupt Controller) */ |
233 | SCU_AHBPeriphClockConfig(__VIC,ENABLE); // enable AHB bus clock for VIC |
219 | SCU_AHBPeriphClockConfig(__VIC,ENABLE); // enable AHB bus clock for VIC |
234 | SCU_AHBPeriphReset(__VIC, DISABLE); // disable reset state for VIC |
220 | SCU_AHBPeriphReset(__VIC, DISABLE); // disable reset state for VIC |
235 | VIC_DeInit(); // deinitializes the VIC module registers to their default reset values. |
221 | VIC_DeInit(); // deinitializes the VIC module registers to their default reset values. |
236 | // initialize the interrupt handler |
222 | // initialize the interrupt handler |
237 | Interrupt_Init(); |
223 | Interrupt_Init(); |
238 | // initialize timer 1 |
224 | // initialize timer 1 |
239 | TIMER1_Init(); |
225 | TIMER1_Init(); |
240 | // initialize the LEDs (needs Timer 1) |
226 | // initialize the LEDs (needs Timer 1) |
241 | Led_Init(); |
227 | Led_Init(); |
242 | // initialize the debug UART1 |
228 | // initialize the debug UART1 |
243 | UART1_Init(); |
229 | UART1_Init(); |
244 | SerialPutString("\r\n---------------------------------------------"); |
230 | SerialPutString("\r\n---------------------------------------------"); |
245 | // initialize UART2 to FLIGHTCTRL |
231 | // initialize UART2 to FLIGHTCTRL |
246 | UART2_Init(); |
232 | UART2_Init(); |
247 | // initialize UART0 (to MKGPS or MK3MAG) |
233 | // initialize UART0 (to MKGPS or MK3MAG) |
248 | UART0_Init(); |
234 | UART0_Init(); |
249 | // initialize usb |
235 | // initialize usb |
250 | USB_ConfigInit(); |
236 | USB_ConfigInit(); |
251 | // initialize SPI0 to FC |
237 | // initialize SPI0 to FC |
252 | SPI0_Init(); |
238 | SPI0_Init(); |
253 | // initialize i2c bus to MK3MAG (needs Timer 1) |
239 | // initialize i2c bus to MK3MAG (needs Timer 1) |
254 | I2C1_Init(); |
240 | I2C1_Init(); |
255 | // initialize the gps position controller (needs Timer 1) |
241 | // initialize the gps position controller (needs Timer 1) |
256 | GPS_Init(); |
242 | GPS_Init(); |
257 | // initialize fat16 partition on sd card (needs Timer 1) |
243 | // initialize fat16 partition on sd card (needs Timer 1) |
258 | Fat16_Init(); |
244 | Fat16_Init(); |
- | 245 | // initialize logging |
|
- | 246 | Logging_Init(); |
|
259 | 247 | ||
260 | TimerCompassUpdate = SetDelay(5); |
248 | TimerCompassUpdate = SetDelay(5); |
261 | TimerKmlAddPointDelay = SetDelay(250); |
249 | TimerLogDelay = SetDelay(250); |
262 | TimerCheckError = SetDelay(3000); |
250 | TimerCheckError = SetDelay(3000); |
263 | SerialPutString("\r\n---------------------------------------------"); |
251 | SerialPutString("\r\n---------------------------------------------"); |
264 | SerialPutString("\n\r Version information:"); |
252 | SerialPutString("\n\r Version information:"); |
265 | GetNaviCtrlVersion(); |
253 | GetNaviCtrlVersion(); |
266 | SPI0_GetFlightCtrlVersion(); |
254 | SPI0_GetFlightCtrlVersion(); |
267 | if(FC_Version.Compatible != FC_SPI_COMPATIBLE) |
255 | if(FC_Version.Compatible != FC_SPI_COMPATIBLE) |
268 | { |
256 | { |
269 | //sprintf(ErrorMSG,"\r\n %d <-> %d", FlightCtrlVersion.NaviCompatible, FC_SPI_COMPATIBLE); |
257 | //sprintf(ErrorMSG,"\r\n %d <-> %d", FlightCtrlVersion.NaviCompatible, FC_SPI_COMPATIBLE); |
270 | //SerialPutString(ErrorMSG); |
258 | //SerialPutString(ErrorMSG); |
271 | SerialPutString("\n\r Flight-Ctrl not compatible"); |
259 | SerialPutString("\n\r Flight-Ctrl not compatible"); |
272 | LED_RED_ON; |
260 | LED_RED_ON; |
273 | } |
261 | } |
274 | 262 | ||
275 | I2C1_GetMK3MagVersion(); |
263 | I2C1_GetMK3MagVersion(); |
276 | if(MK3MAG_Version.Compatible != MK3MAG_I2C_COMPATIBLE) |
264 | if(MK3MAG_Version.Compatible != MK3MAG_I2C_COMPATIBLE) |
277 | { |
265 | { |
278 | SerialPutString("\n\r MK3Mag not compatible"); |
266 | SerialPutString("\n\r MK3Mag not compatible"); |
279 | LED_RED_ON; |
267 | LED_RED_ON; |
280 | } |
268 | } |
281 | 269 | ||
282 | /* |
270 | /* |
283 | ReadSetting(1); |
271 | ReadSetting(1); |
284 | */ |
272 | */ |
285 | SerialPutString("\r\n---------------------------------------------\r\n\r\n"); |
273 | SerialPutString("\r\n---------------------------------------------\r\n\r\n"); |
286 | 274 | ||
287 | for (;;) |
275 | for (;;) |
288 | { |
276 | { |
289 | if(rxd_buffer_locked) // new incomming command |
277 | if(rxd_buffer_locked) // new incomming command |
290 | { |
278 | { |
291 | UART1_ProcessRxData(); // process request |
279 | UART1_ProcessRxData(); // process request |
292 | } |
280 | } |
293 | UART1_TransmitTxData(); // send answer |
281 | UART1_TransmitTxData(); // send answer |
294 | 282 | ||
295 | SPI0_UpdateBuffer(); |
283 | SPI0_UpdateBuffer(); |
296 | UART0_Transmit(); // empty txd buffer |
284 | UART0_Transmit(); // empty txd buffer |
297 | UART1_Transmit(); // empty txd buffer |
285 | UART1_Transmit(); // empty txd buffer |
298 | 286 | ||
299 | // ------------------------- I2C Timing -------------------------------- |
287 | // ------------------------- I2C Timing -------------------------------- |
300 | if(I2C_State != I2C_OFF) |
288 | if(I2C_State != I2C_OFF) |
301 | { |
289 | { |
302 | if (CheckDelay(TimerCompassUpdate)) |
290 | if (CheckDelay(TimerCompassUpdate)) |
303 | { |
291 | { |
304 | // check for hanging I2C bus |
292 | // check for hanging I2C bus |
305 | if(CheckDelay(I2C1_Timeout)) |
293 | if(CheckDelay(I2C1_Timeout)) |
306 | { // reset I2C |
294 | { // reset I2C |
307 | SerialPutString("\n\rCommunication-Error: MK3Mag\n\r"); |
295 | SerialPutString("\n\rCommunication-Error: MK3Mag\n\r"); |
308 | I2C1_Deinit(); |
296 | I2C1_Deinit(); |
309 | I2C1_Init(); |
297 | I2C1_Init(); |
310 | } |
298 | } |
311 | else |
299 | else |
312 | { // check for incomming compass calibration request |
300 | { // check for incomming compass calibration request |
313 | // update CalByte from spi input queue |
301 | // update CalByte from spi input queue |
314 | fifo_get(&CompassCalcStateFiFo, (u8 *)&(I2C_WriteCal.CalByte)); |
302 | fifo_get(&CompassCalcStateFiFo, (u8 *)&(I2C_WriteCal.CalByte)); |
315 | // send new calstate |
303 | // send new calstate |
316 | if(I2C_ReadCal.CalByte != I2C_WriteCal.CalByte) |
304 | if(I2C_ReadCal.CalByte != I2C_WriteCal.CalByte) |
317 | { |
305 | { |
318 | I2C1_SendCommand(I2C_CMD_WRITE_CAL); |
306 | I2C1_SendCommand(I2C_CMD_WRITE_CAL); |
319 | } |
307 | } |
320 | else // request current heading |
308 | else // request current heading |
321 | { |
309 | { |
322 | I2C1_SendCommand(I2C_CMD_READ_HEADING); |
310 | I2C1_SendCommand(I2C_CMD_READ_HEADING); |
323 | } |
311 | } |
324 | TimerCompassUpdate = SetDelay(25); // every 25 ms |
312 | TimerCompassUpdate = SetDelay(25); // every 25 ms |
325 | } |
313 | } |
326 | 314 | ||
327 | } |
315 | } |
328 | } |
316 | } |
329 | 317 | ||
330 | // ---------------- Error Check Timing ---------------------------- |
318 | // ---------------- Error Check Timing ---------------------------- |
331 | if(CheckDelay(TimerCheckError)) |
319 | if(CheckDelay(TimerCheckError)) |
332 | { |
320 | { |
333 | if(!CheckSPIOkay) GPS_Navigation(); // process the GPS data even if the FC is not connected |
321 | if(!CheckSPIOkay) GPS_Navigation(); // process the GPS data even if the FC is not connected |
334 | CheckErrors(); |
322 | CheckErrors(); |
335 | if(FC.MKFlags & MKFLAG_FLY) NaviData.FlyingTime++; // we want to count the battery-time |
323 | if(FC.MKFlags & MKFLAG_FLY) NaviData.FlyingTime++; // we want to count the battery-time |
336 | // else NaviData.FlyingTime = 0; |
324 | // else NaviData.FlyingTime = 0; |
337 | TimerCheckError = SetDelay(1000); |
325 | TimerCheckError = SetDelay(1000); |
338 | Request_SendFollowMe = TRUE; |
326 | Request_SendFollowMe = TRUE; |
339 | } |
327 | } |
340 | // ---------------- KML Timing ------------------------------------ |
328 | // ---------------- Logging --------------------------------------- |
341 | if(CheckDelay(TimerKmlAddPointDelay)) |
- | |
342 | { |
- | |
343 | TimerKmlAddPointDelay = SetDelay(500); // every 500 ms |
- | |
344 | - | ||
345 | if(FC.MKFlags & MKFLAG_MOTOR_RUN) |
- | |
346 | { |
- | |
347 | switch(logfilestate) |
329 | Logging_Update(); |
348 | { |
- | |
349 | case LOGFILE_IDLE: |
- | |
350 | case LOGFILE_CLOSED: |
- | |
351 | if((GPSData.Status != INVALID) && (GPSData.Flags & FLAG_GPSFIXOK) && (GPSData.SatFix == SATFIX_3D) && (FC.MKFlags & MKFLAG_FLY)) |
- | |
352 | { |
- | |
353 | logfilestate = LOGFILE_START; |
- | |
354 | } |
- | |
355 | break; |
- | |
356 | case LOGFILE_START: |
- | |
357 | // find unused logfile name |
- | |
358 | do |
- | |
359 | { // try to generate a new logfile name |
- | |
360 | gpslogfilename = GenerateGPSLogFileName(); |
- | |
361 | }while((gpslogfilename != NULL) && fexist_(gpslogfilename)); |
- | |
362 | // if logfilename exist |
- | |
363 | if(gpslogfilename != NULL) |
- | |
364 | { |
- | |
365 | // try to create the log file |
- | |
366 | if(KML_DocumentOpen(gpslogfilename, &gpslogfile)) |
- | |
367 | { |
- | |
368 | logfilestate = LOGFILE_OPENED; // goto next step |
- | |
369 | SerialPutString("\r\nOpening kml-file:"); |
- | |
370 | SerialPutString(gpslogfilename); |
- | |
371 | SerialPutString("\r\n"); |
- | |
372 | } |
- | |
373 | else |
- | |
374 | { |
- | |
375 | logfilestate = LOGFILE_ERROR; |
- | |
376 | SerialPutString("\r\nError opening kml-file: "); |
- | |
377 | SerialPutString(gpslogfilename); |
- | |
378 | SerialPutString("\r\n"); |
- | |
379 | TimerKmlAddPointDelay = SetDelay(10); // try again in open logfile in 10 mili sec |
- | |
380 | } |
- | |
381 | } |
- | |
382 | // else retry in next loop |
- | |
383 | break; |
- | |
384 | case LOGFILE_OPENED: |
- | |
385 | // append new gps log data |
- | |
386 | if((GPSData.Status != INVALID) && (GPSData.Flags & FLAG_GPSFIXOK) && (GPSData.SatFix == SATFIX_3D)) |
- | |
387 | { |
- | |
388 | if(!KML_LoggGPSCoordinates(&(GPSData.Position), &gpslogfile)) |
- | |
389 | { // error logging data |
- | |
390 | SerialPutString("\r\nError Logging GPS data\r\n"); |
- | |
391 | KML_DocumentClose(&gpslogfile); |
- | |
392 | logfilestate = LOGFILE_ERROR; |
- | |
393 | } |
- | |
394 | else DebugOut.Analog[26]++; // count SD-Logs |
- | |
395 | } |
- | |
396 | break; |
- | |
397 | - | ||
398 | case LOGFILE_ERROR: |
- | |
399 | // try to reinitialize the fat16 filesystem |
- | |
400 | if(0 == Fat16_Init()) |
- | |
401 | { |
- | |
402 | logfilestate = LOGFILE_IDLE; |
- | |
403 | TimerKmlAddPointDelay = SetDelay(10); // try again in open logfile in 10 mili sec |
- | |
404 | } |
- | |
405 | else |
- | |
406 | { |
- | |
407 | logfilestate = LOGFILE_ERROR; |
- | |
408 | TimerKmlAddPointDelay = SetDelay(5000); // try again in 5 sec |
- | |
409 | } |
- | |
410 | break; |
- | |
411 | - | ||
412 | default: |
- | |
413 | break; |
- | |
414 | } |
- | |
415 | } // EOF model is flying |
- | |
416 | else // model is not flying |
- | |
417 | { // close log file if opened |
- | |
418 | if(logfilestate == LOGFILE_OPENED) |
- | |
419 | { |
- | |
420 | if(KML_DocumentClose(&gpslogfile)) |
- | |
421 | { |
- | |
422 | SerialPutString("\r\nClosing kml-file\r\n"); |
- | |
423 | logfilestate = LOGFILE_CLOSED; |
- | |
424 | } |
- | |
425 | else |
- | |
426 | { |
- | |
427 | logfilestate = LOGFILE_ERROR; |
- | |
428 | } |
- | |
429 | } |
- | |
430 | } //EOF model is not flying |
- | |
431 | } |
- | |
432 | } |
330 | } |
433 | } |
331 | } |
434 | 332 | ||
435 | 333 | ||
436 | 334 |