Rev 83 | Rev 91 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 83 | Rev 88 | ||
---|---|---|---|
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 "logging.h" |
73 | #include "logging.h" |
- | 74 | #include "settings.h" |
|
74 | #include "main.h" |
75 | #include "main.h" |
75 | 76 | ||
76 | u32 TimerCompassUpdate; |
77 | u32 TimerCompassUpdate; |
77 | u32 TimerCheckError; |
78 | u32 TimerCheckError; |
78 | u32 ErrorCode = 0; |
79 | u32 ErrorCode = 0; |
79 | u16 BeepTime; |
80 | u16 BeepTime; |
80 | 81 | ||
81 | u8 ClearMKFlags = 0; |
82 | u8 ClearMKFlags = 0; |
82 | u8 StopNavigation = 0; |
83 | u8 StopNavigation = 0; |
83 | Param_t Parameter; |
84 | Param_t Parameter; |
84 | volatile FC_t FC; |
85 | volatile FC_t FC; |
85 | 86 | ||
86 | s8 ErrorMSG[25]; |
87 | s8 ErrorMSG[25]; |
87 | 88 | ||
88 | //---------------------------------------------------------------------------------------------------- |
89 | //---------------------------------------------------------------------------------------------------- |
89 | void SCU_Config(void) |
90 | void SCU_Config(void) |
90 | { |
91 | { |
91 | /* configure PLL and set it as master clock source */ |
92 | /* configure PLL and set it as master clock source */ |
92 | 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 |
93 | SCU_PLLCmd(DISABLE); // now disable the PLL |
94 | SCU_PLLCmd(DISABLE); // now disable the PLL |
94 | #ifdef MCLK96MHZ |
95 | #ifdef MCLK96MHZ |
95 | SCU_BRCLKDivisorConfig(SCU_BRCLK_Div2); // set BRCLK to MCLK/2 = 48MHz |
96 | SCU_BRCLKDivisorConfig(SCU_BRCLK_Div2); // set BRCLK to MCLK/2 = 48MHz |
96 | 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) |
97 | 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) |
98 | 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) |
99 | 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 |
100 | #else |
101 | #else |
101 | SCU_BRCLKDivisorConfig(SCU_BRCLK_Div1); // set BRCLK to MCLK = 48MHz |
102 | SCU_BRCLKDivisorConfig(SCU_BRCLK_Div1); // set BRCLK to MCLK = 48MHz |
102 | 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) |
103 | 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) |
104 | 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) |
105 | 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 |
106 | #endif |
107 | #endif |
107 | SCU_PLLCmd(ENABLE); // Enable PLL (is disabled by SCU_PLLFactorsConfig) |
108 | SCU_PLLCmd(ENABLE); // Enable PLL (is disabled by SCU_PLLFactorsConfig) |
108 | SCU_MCLKSourceConfig(SCU_MCLK_PLL); // set master clock source to PLL |
109 | SCU_MCLKSourceConfig(SCU_MCLK_PLL); // set master clock source to PLL |
109 | } |
110 | } |
110 | 111 | ||
111 | //---------------------------------------------------------------------------------------------------- |
112 | //---------------------------------------------------------------------------------------------------- |
112 | void GetNaviCtrlVersion(void) |
113 | void GetNaviCtrlVersion(void) |
113 | { |
114 | { |
114 | u8 msg[25]; |
115 | u8 msg[25]; |
115 | 116 | ||
116 | 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); |
117 | SerialPutString(msg); |
118 | SerialPutString(msg); |
118 | } |
119 | } |
119 | 120 | ||
120 | 121 | ||
121 | //---------------------------------------------------------------------------------------------------- |
122 | //---------------------------------------------------------------------------------------------------- |
122 | s8* GenerateGPSLogFileName(void) |
123 | s8* GenerateGPSLogFileName(void) |
123 | { |
124 | { |
124 | static u16 filenum = 0; // file name counter |
125 | static u16 filenum = 0; // file name counter |
125 | static s8 filename[30]; |
126 | static s8 filename[30]; |
126 | 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}; |
127 | 128 | ||
128 | if(SystemTime.Valid) |
129 | if(SystemTime.Valid) |
129 | { |
130 | { |
130 | // if the day has been changed |
131 | // if the day has been changed |
131 | 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)) |
132 | { |
133 | { |
133 | LastTime.Year = SystemTime.Year; |
134 | LastTime.Year = SystemTime.Year; |
134 | LastTime.Month = SystemTime.Month; |
135 | LastTime.Month = SystemTime.Month; |
135 | LastTime.Day = SystemTime.Day; |
136 | LastTime.Day = SystemTime.Day; |
136 | LastTime.Valid = 1; |
137 | LastTime.Valid = 1; |
137 | filenum = 0; // reset file counter |
138 | filenum = 0; // reset file counter |
138 | } |
139 | } |
139 | 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); |
140 | filenum++; |
141 | filenum++; |
141 | return filename; |
142 | return filename; |
142 | } |
143 | } |
143 | else return NULL; |
144 | else return NULL; |
144 | } |
145 | } |
145 | 146 | ||
146 | 147 | ||
147 | //---------------------------------------------------------------------------------------------------- |
148 | //---------------------------------------------------------------------------------------------------- |
148 | void CheckErrors(void) |
149 | void CheckErrors(void) |
149 | { |
150 | { |
150 | if(!CheckSPIOkay) |
151 | if(!CheckSPIOkay) |
151 | { |
152 | { |
152 | LED_RED_ON; |
153 | LED_RED_ON; |
153 | sprintf(ErrorMSG,"no FC communication "); |
154 | sprintf(ErrorMSG,"no FC communication "); |
154 | ErrorCode = 3; |
155 | ErrorCode = 3; |
155 | StopNavigation = 1; |
156 | StopNavigation = 1; |
156 | } |
157 | } |
157 | else if(!CheckI2COkay) |
158 | else if(!CheckI2COkay) |
158 | { |
159 | { |
159 | LED_RED_ON; |
160 | LED_RED_ON; |
160 | sprintf(ErrorMSG,"no MK3Mag communication "); |
161 | sprintf(ErrorMSG,"no MK3Mag communication "); |
161 | ErrorCode = 4; |
162 | ErrorCode = 4; |
162 | StopNavigation = 1; |
163 | StopNavigation = 1; |
163 | } |
164 | } |
164 | else if(FC_Version.Compatible != FC_SPI_COMPATIBLE) |
165 | else if(FC_Version.Compatible != FC_SPI_COMPATIBLE) |
165 | { |
166 | { |
166 | LED_RED_ON; |
167 | LED_RED_ON; |
167 | sprintf(ErrorMSG,"FC not compatible "); |
168 | sprintf(ErrorMSG,"FC not compatible "); |
168 | ErrorCode = 1; |
169 | ErrorCode = 1; |
169 | StopNavigation = 1; |
170 | StopNavigation = 1; |
170 | } |
171 | } |
171 | else if(MK3MAG_Version.Compatible != MK3MAG_I2C_COMPATIBLE) |
172 | else if(MK3MAG_Version.Compatible != MK3MAG_I2C_COMPATIBLE) |
172 | { |
173 | { |
173 | sprintf(ErrorMSG,"MK3Mag not compatible "); |
174 | sprintf(ErrorMSG,"MK3Mag not compatible "); |
174 | LED_RED_ON; |
175 | LED_RED_ON; |
175 | ErrorCode = 2; |
176 | ErrorCode = 2; |
176 | StopNavigation = 1; |
177 | StopNavigation = 1; |
177 | } |
178 | } |
178 | else if(!CheckGPSOkay) |
179 | else if(!CheckGPSOkay) |
179 | { |
180 | { |
180 | LED_RED_ON; |
181 | LED_RED_ON; |
181 | sprintf(ErrorMSG,"no GPS communication "); |
182 | sprintf(ErrorMSG,"no GPS communication "); |
182 | ErrorCode = 5; |
183 | ErrorCode = 5; |
183 | StopNavigation = 1; |
184 | StopNavigation = 1; |
184 | } |
185 | } |
185 | else if(I2C_Heading.Heading < 0) |
186 | else if(I2C_Heading.Heading < 0) |
186 | { |
187 | { |
187 | LED_RED_ON; |
188 | LED_RED_ON; |
188 | sprintf(ErrorMSG,"bad compass value "); |
189 | sprintf(ErrorMSG,"bad compass value "); |
189 | ErrorCode = 6; |
190 | ErrorCode = 6; |
190 | StopNavigation = 1; |
191 | StopNavigation = 1; |
191 | } |
192 | } |
192 | else if(FC.RC_Quality < 100) |
193 | else if(FC.RC_Quality < 100) |
193 | { |
194 | { |
194 | LED_RED_ON; |
195 | LED_RED_ON; |
195 | sprintf(ErrorMSG,"RC Signal lost "); |
196 | sprintf(ErrorMSG,"RC Signal lost "); |
196 | ErrorCode = 7; |
197 | ErrorCode = 7; |
197 | } |
198 | } |
198 | else // no error occured |
199 | else // no error occured |
199 | { |
200 | { |
200 | sprintf(ErrorMSG,"No Error "); |
201 | sprintf(ErrorMSG,"No Error "); |
201 | ErrorCode = 0; |
202 | ErrorCode = 0; |
202 | StopNavigation = 0; |
203 | StopNavigation = 0; |
203 | LED_RED_OFF; |
204 | LED_RED_OFF; |
204 | } |
205 | } |
205 | CheckSPIOkay = 0; |
206 | CheckSPIOkay = 0; |
206 | CheckGPSOkay = 0; |
207 | CheckGPSOkay = 0; |
207 | CheckI2COkay = 0; |
208 | CheckI2COkay = 0; |
208 | } |
209 | } |
209 | 210 | ||
210 | 211 | ||
211 | 212 | ||
212 | //---------------------------------------------------------------------------------------------------- |
213 | //---------------------------------------------------------------------------------------------------- |
213 | int main(void) |
214 | int main(void) |
214 | { |
215 | { |
215 | /* Configure the system clocks */ |
216 | /* Configure the system clocks */ |
216 | SCU_Config(); |
217 | SCU_Config(); |
217 | /* init VIC (Vectored Interrupt Controller) */ |
218 | /* init VIC (Vectored Interrupt Controller) */ |
218 | SCU_AHBPeriphClockConfig(__VIC,ENABLE); // enable AHB bus clock for VIC |
219 | SCU_AHBPeriphClockConfig(__VIC,ENABLE); // enable AHB bus clock for VIC |
219 | SCU_AHBPeriphReset(__VIC, DISABLE); // disable reset state for VIC |
220 | SCU_AHBPeriphReset(__VIC, DISABLE); // disable reset state for VIC |
220 | 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. |
221 | // initialize the interrupt handler |
222 | // initialize the interrupt handler |
222 | Interrupt_Init(); |
223 | Interrupt_Init(); |
223 | // initialize timer 1 |
224 | // initialize timer 1 |
224 | TIMER1_Init(); |
225 | TIMER1_Init(); |
225 | // initialize the LEDs (needs Timer 1) |
226 | // initialize the LEDs (needs Timer 1) |
226 | Led_Init(); |
227 | Led_Init(); |
227 | // initialize the debug UART1 |
228 | // initialize the debug UART1 |
228 | UART1_Init(); |
229 | UART1_Init(); |
229 | SerialPutString("\r\n---------------------------------------------"); |
230 | SerialPutString("\r\n---------------------------------------------"); |
230 | // initialize UART2 to FLIGHTCTRL |
231 | // initialize UART2 to FLIGHTCTRL |
231 | UART2_Init(); |
232 | UART2_Init(); |
232 | // initialize UART0 (to MKGPS or MK3MAG) |
233 | // initialize UART0 (to MKGPS or MK3MAG) |
233 | UART0_Init(); |
234 | UART0_Init(); |
234 | // initialize usb |
235 | // initialize usb |
235 | USB_ConfigInit(); |
236 | USB_ConfigInit(); |
236 | // initialize SPI0 to FC |
237 | // initialize SPI0 to FC |
237 | SPI0_Init(); |
238 | SPI0_Init(); |
238 | // initialize i2c bus to MK3MAG (needs Timer 1) |
239 | // initialize i2c bus to MK3MAG (needs Timer 1) |
239 | I2C1_Init(); |
240 | I2C1_Init(); |
240 | // initialize the gps position controller (needs Timer 1) |
241 | // initialize the gps position controller (needs Timer 1) |
241 | GPS_Init(); |
242 | GPS_Init(); |
242 | // initialize fat16 partition on sd card (needs Timer 1) |
243 | // initialize fat16 partition on sd card (needs Timer 1) |
243 | Fat16_Init(); |
244 | Fat16_Init(); |
- | 245 | // initialize the settings |
|
- | 246 | Settings_Init(); |
|
244 | // initialize logging |
247 | // initialize logging |
245 | Logging_Init(); |
248 | Logging_Init(); |
246 | 249 | ||
247 | TimerCompassUpdate = SetDelay(5); |
250 | TimerCompassUpdate = SetDelay(5); |
248 | TimerCheckError = SetDelay(3000); |
251 | TimerCheckError = SetDelay(3000); |
249 | SerialPutString("\r\n---------------------------------------------"); |
252 | SerialPutString("\r\n---------------------------------------------"); |
250 | SerialPutString("\n\r Version information:"); |
253 | SerialPutString("\n\r Version information:"); |
251 | GetNaviCtrlVersion(); |
254 | GetNaviCtrlVersion(); |
252 | SPI0_GetFlightCtrlVersion(); |
255 | SPI0_GetFlightCtrlVersion(); |
253 | if(FC_Version.Compatible != FC_SPI_COMPATIBLE) |
256 | if(FC_Version.Compatible != FC_SPI_COMPATIBLE) |
254 | { |
257 | { |
255 | //sprintf(ErrorMSG,"\r\n %d <-> %d", FlightCtrlVersion.NaviCompatible, FC_SPI_COMPATIBLE); |
258 | //sprintf(ErrorMSG,"\r\n %d <-> %d", FlightCtrlVersion.NaviCompatible, FC_SPI_COMPATIBLE); |
256 | //SerialPutString(ErrorMSG); |
259 | //SerialPutString(ErrorMSG); |
257 | SerialPutString("\n\r Flight-Ctrl not compatible"); |
260 | SerialPutString("\n\r Flight-Ctrl not compatible"); |
258 | LED_RED_ON; |
261 | LED_RED_ON; |
259 | } |
262 | } |
260 | 263 | ||
261 | I2C1_GetMK3MagVersion(); |
264 | I2C1_GetMK3MagVersion(); |
262 | if(MK3MAG_Version.Compatible != MK3MAG_I2C_COMPATIBLE) |
265 | if(MK3MAG_Version.Compatible != MK3MAG_I2C_COMPATIBLE) |
263 | { |
266 | { |
264 | SerialPutString("\n\r MK3Mag not compatible"); |
267 | SerialPutString("\n\r MK3Mag not compatible"); |
265 | LED_RED_ON; |
268 | LED_RED_ON; |
266 | } |
269 | } |
267 | - | ||
268 | /* |
- | |
269 | ReadSetting(1); |
- | |
270 | */ |
270 | |
271 | SerialPutString("\r\n---------------------------------------------\r\n\r\n"); |
271 | SerialPutString("\r\n---------------------------------------------\r\n\r\n"); |
272 | 272 | ||
273 | for (;;) |
273 | for (;;) |
274 | { |
274 | { |
275 | if(rxd_buffer_locked) // new incomming command |
275 | if(rxd_buffer_locked) // new incomming command |
276 | { |
276 | { |
277 | UART1_ProcessRxData(); // process request |
277 | UART1_ProcessRxData(); // process request |
278 | } |
278 | } |
279 | UART1_TransmitTxData(); // send answer |
279 | UART1_TransmitTxData(); // send answer |
280 | 280 | ||
281 | SPI0_UpdateBuffer(); |
281 | SPI0_UpdateBuffer(); |
282 | UART0_Transmit(); // empty txd buffer |
282 | UART0_Transmit(); // empty txd buffer |
283 | UART1_Transmit(); // empty txd buffer |
283 | UART1_Transmit(); // empty txd buffer |
284 | 284 | ||
285 | // ------------------------- I2C Timing -------------------------------- |
285 | // ------------------------- I2C Timing -------------------------------- |
286 | if(I2C_State != I2C_OFF) |
286 | if(I2C_State != I2C_OFF) |
287 | { |
287 | { |
288 | if (CheckDelay(TimerCompassUpdate)) |
288 | if (CheckDelay(TimerCompassUpdate)) |
289 | { |
289 | { |
290 | // check for hanging I2C bus |
290 | // check for hanging I2C bus |
291 | if(CheckDelay(I2C1_Timeout)) |
291 | if(CheckDelay(I2C1_Timeout)) |
292 | { // reset I2C |
292 | { // reset I2C |
293 | SerialPutString("\n\rCommunication-Error: MK3Mag\n\r"); |
293 | SerialPutString("\n\rCommunication-Error: MK3Mag\n\r"); |
294 | I2C1_Deinit(); |
294 | I2C1_Deinit(); |
295 | I2C1_Init(); |
295 | I2C1_Init(); |
296 | } |
296 | } |
297 | else |
297 | else |
298 | { // check for incomming compass calibration request |
298 | { // check for incomming compass calibration request |
299 | // update CalByte from spi input queue |
299 | // update CalByte from spi input queue |
300 | fifo_get(&CompassCalcStateFiFo, (u8 *)&(I2C_WriteCal.CalByte)); |
300 | fifo_get(&CompassCalcStateFiFo, (u8 *)&(I2C_WriteCal.CalByte)); |
301 | // send new calstate |
301 | // send new calstate |
302 | if(I2C_ReadCal.CalByte != I2C_WriteCal.CalByte) |
302 | if(I2C_ReadCal.CalByte != I2C_WriteCal.CalByte) |
303 | { |
303 | { |
304 | I2C1_SendCommand(I2C_CMD_WRITE_CAL); |
304 | I2C1_SendCommand(I2C_CMD_WRITE_CAL); |
305 | } |
305 | } |
306 | else // request current heading |
306 | else // request current heading |
307 | { |
307 | { |
308 | I2C1_SendCommand(I2C_CMD_READ_HEADING); |
308 | I2C1_SendCommand(I2C_CMD_READ_HEADING); |
309 | } |
309 | } |
310 | TimerCompassUpdate = SetDelay(25); // every 25 ms |
310 | TimerCompassUpdate = SetDelay(25); // every 25 ms |
311 | } |
311 | } |
312 | 312 | ||
313 | } |
313 | } |
314 | } |
314 | } |
315 | 315 | ||
316 | // ---------------- Error Check Timing ---------------------------- |
316 | // ---------------- Error Check Timing ---------------------------- |
317 | if(CheckDelay(TimerCheckError)) |
317 | if(CheckDelay(TimerCheckError)) |
318 | { |
318 | { |
319 | if(!CheckSPIOkay) GPS_Navigation(); // process the GPS data even if the FC is not connected |
319 | if(!CheckSPIOkay) GPS_Navigation(); // process the GPS data even if the FC is not connected |
320 | CheckErrors(); |
320 | CheckErrors(); |
321 | if(FC.MKFlags & MKFLAG_FLY) NaviData.FlyingTime++; // we want to count the battery-time |
321 | if(FC.MKFlags & MKFLAG_FLY) NaviData.FlyingTime++; // we want to count the battery-time |
322 | // else NaviData.FlyingTime = 0; |
322 | // else NaviData.FlyingTime = 0; |
323 | TimerCheckError = SetDelay(1000); |
323 | TimerCheckError = SetDelay(1000); |
324 | Request_SendFollowMe = TRUE; |
324 | Request_SendFollowMe = TRUE; |
325 | } |
325 | } |
326 | // ---------------- Logging --------------------------------------- |
326 | // ---------------- Logging --------------------------------------- |
327 | Logging_Update(); |
327 | Logging_Update(); |
328 | } |
328 | } |
329 | } |
329 | } |
330 | 330 | ||
331 | 331 | ||
332 | 332 |