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