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