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