Rev 153 | Rev 155 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 153 | Rev 154 | ||
---|---|---|---|
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 "config.h" |
|
77 | #include "main.h" |
78 | #include "main.h" |
78 | 79 | ||
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 | s16 GeoMagDec = 0; // local magnetic declination in 0.1 deg |
84 | s16 GeoMagDec = 0; // local magnetic declination in 0.1 deg |
84 | 85 | ||
85 | u8 ClearMKFlags = 0; |
86 | u8 ClearMKFlags = 0; |
86 | u8 StopNavigation = 0; |
87 | u8 StopNavigation = 0; |
87 | Param_t Parameter; |
88 | Param_t Parameter; |
88 | volatile FC_t FC; |
89 | volatile FC_t FC; |
89 | 90 | ||
90 | s8 ErrorMSG[25]; |
91 | s8 ErrorMSG[25]; |
91 | 92 | ||
92 | //---------------------------------------------------------------------------------------------------- |
93 | //---------------------------------------------------------------------------------------------------- |
93 | void SCU_Config(void) |
94 | void SCU_Config(void) |
94 | { |
95 | { |
95 | /* configure PLL and set it as master clock source */ |
96 | /* configure PLL and set it as master clock source */ |
96 | 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 |
97 | SCU_PLLCmd(DISABLE); // now disable the PLL |
98 | SCU_PLLCmd(DISABLE); // now disable the PLL |
98 | #ifdef MCLK96MHZ |
99 | #ifdef MCLK96MHZ |
99 | SCU_BRCLKDivisorConfig(SCU_BRCLK_Div2); // set BRCLK to MCLK/2 = 48MHz |
100 | SCU_BRCLKDivisorConfig(SCU_BRCLK_Div2); // set BRCLK to MCLK/2 = 48MHz |
100 | 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) |
101 | 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) |
102 | 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) |
103 | 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 |
104 | #else |
105 | #else |
105 | SCU_BRCLKDivisorConfig(SCU_BRCLK_Div1); // set BRCLK to MCLK = 48MHz |
106 | SCU_BRCLKDivisorConfig(SCU_BRCLK_Div1); // set BRCLK to MCLK = 48MHz |
106 | 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) |
107 | 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) |
108 | 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) |
109 | 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 |
110 | #endif |
111 | #endif |
111 | SCU_PLLCmd(ENABLE); // Enable PLL (is disabled by SCU_PLLFactorsConfig) |
112 | SCU_PLLCmd(ENABLE); // Enable PLL (is disabled by SCU_PLLFactorsConfig) |
112 | SCU_MCLKSourceConfig(SCU_MCLK_PLL); // set master clock source to PLL |
113 | SCU_MCLKSourceConfig(SCU_MCLK_PLL); // set master clock source to PLL |
113 | } |
114 | } |
114 | 115 | ||
115 | //---------------------------------------------------------------------------------------------------- |
116 | //---------------------------------------------------------------------------------------------------- |
116 | void GetNaviCtrlVersion(void) |
117 | void GetNaviCtrlVersion(void) |
117 | { |
118 | { |
118 | u8 msg[25]; |
119 | u8 msg[25]; |
119 | 120 | ||
120 | 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); |
121 | UART1_PutString(msg); |
122 | UART1_PutString(msg); |
122 | } |
123 | } |
123 | 124 | ||
124 | //---------------------------------------------------------------------------------------------------- |
125 | //---------------------------------------------------------------------------------------------------- |
125 | void CheckErrors(void) |
126 | void CheckErrors(void) |
126 | { |
127 | { |
127 | if(CheckDelay(SPI0_Timeout)) |
128 | if(CheckDelay(SPI0_Timeout)) |
128 | { |
129 | { |
129 | LED_RED_ON; |
130 | LED_RED_ON; |
130 | sprintf(ErrorMSG,"no FC communication "); |
131 | sprintf(ErrorMSG,"no FC communication "); |
131 | ErrorCode = 3; |
132 | ErrorCode = 3; |
132 | StopNavigation = 1; |
133 | StopNavigation = 1; |
133 | } |
134 | } |
134 | else if(CheckDelay(I2C1_Timeout)) |
135 | else if(CheckDelay(I2C1_Timeout)) |
135 | { |
136 | { |
136 | LED_RED_ON; |
137 | LED_RED_ON; |
137 | sprintf(ErrorMSG,"no MK3Mag communication "); |
138 | sprintf(ErrorMSG,"no MK3Mag communication "); |
138 | //Reset I2CBus |
139 | //Reset I2CBus |
139 | I2C1_Deinit(); |
140 | I2C1_Deinit(); |
140 | I2C1_Init(); |
141 | I2C1_Init(); |
141 | ErrorCode = 4; |
142 | ErrorCode = 4; |
142 | StopNavigation = 1; |
143 | StopNavigation = 1; |
143 | } |
144 | } |
144 | else if(FC_Version.Compatible != FC_SPI_COMPATIBLE) |
145 | else if(FC_Version.Compatible != FC_SPI_COMPATIBLE) |
145 | { |
146 | { |
146 | LED_RED_ON; |
147 | LED_RED_ON; |
147 | sprintf(ErrorMSG,"FC not compatible "); |
148 | sprintf(ErrorMSG,"FC not compatible "); |
148 | ErrorCode = 1; |
149 | ErrorCode = 1; |
149 | StopNavigation = 1; |
150 | StopNavigation = 1; |
150 | } |
151 | } |
151 | else if(MK3MAG_Version.Compatible != MK3MAG_I2C_COMPATIBLE) |
152 | else if(MK3MAG_Version.Compatible != MK3MAG_I2C_COMPATIBLE) |
152 | { |
153 | { |
153 | sprintf(ErrorMSG,"MK3Mag not compatible "); |
154 | sprintf(ErrorMSG,"MK3Mag not compatible "); |
154 | LED_RED_ON; |
155 | LED_RED_ON; |
155 | ErrorCode = 2; |
156 | ErrorCode = 2; |
156 | StopNavigation = 1; |
157 | StopNavigation = 1; |
157 | } |
158 | } |
158 | else if(CheckDelay(UBX_Timeout)) |
159 | else if(CheckDelay(UBX_Timeout)) |
159 | { |
160 | { |
160 | LED_RED_ON; |
161 | LED_RED_ON; |
161 | sprintf(ErrorMSG,"no GPS communication "); |
162 | sprintf(ErrorMSG,"no GPS communication "); |
162 | ErrorCode = 5; |
163 | ErrorCode = 5; |
163 | StopNavigation = 1; |
164 | StopNavigation = 1; |
164 | } |
165 | } |
165 | else if(I2C_Heading.Heading < 0) |
166 | else if(I2C_Heading.Heading < 0) |
166 | { |
167 | { |
167 | LED_RED_ON; |
168 | LED_RED_ON; |
168 | sprintf(ErrorMSG,"bad compass value "); |
169 | sprintf(ErrorMSG,"bad compass value "); |
169 | ErrorCode = 6; |
170 | ErrorCode = 6; |
170 | StopNavigation = 1; |
171 | StopNavigation = 1; |
171 | } |
172 | } |
172 | else if(FC.RC_Quality < 100) |
173 | else if(FC.RC_Quality < 100) |
173 | { |
174 | { |
174 | LED_RED_ON; |
175 | LED_RED_ON; |
175 | sprintf(ErrorMSG,"RC Signal lost "); |
176 | sprintf(ErrorMSG,"RC Signal lost "); |
176 | ErrorCode = 7; |
177 | ErrorCode = 7; |
177 | } |
178 | } |
178 | else // no error occured |
179 | else // no error occured |
179 | { |
180 | { |
180 | sprintf(ErrorMSG,"No Error "); |
181 | sprintf(ErrorMSG,"No Error "); |
181 | ErrorCode = 0; |
182 | ErrorCode = 0; |
182 | StopNavigation = 0; |
183 | StopNavigation = 0; |
183 | LED_RED_OFF; |
184 | LED_RED_OFF; |
184 | } |
185 | } |
185 | } |
186 | } |
- | 187 | ||
- | 188 | // the handler will be cyclic called by the timer 1 ISR |
|
- | 189 | // used is for critical timing parts that normaly would handled |
|
- | 190 | // within the main loop that could block longer at logging activities |
|
- | 191 | void EXTIT3_IRQHandler(void) |
|
- | 192 | { |
|
- | 193 | VIC_ITCmd(EXTIT3_ITLine,DISABLE); // disable irq |
|
- | 194 | ||
- | 195 | I2C1_UpdateCompass(); // update compass communication |
|
- | 196 | UART0_ProcessRxData(); // process request |
|
- | 197 | UART1_ProcessRxData(); // process request |
|
- | 198 | USB_ProcessRxData(); // process request |
|
- | 199 | ||
- | 200 | SPI0_UpdateBuffer(); // handle new SPI Data |
|
- | 201 | Analog_Update(); // get new ADC values |
|
- | 202 | ||
- | 203 | VIC_SWITCmd(EXTIT3_ITLine,DISABLE); // clear pending bit |
|
186 | 204 | VIC_ITCmd(EXTIT3_ITLine, ENABLE); // enable irq |
|
187 | 205 | } |
|
188 | 206 | ||
189 | //---------------------------------------------------------------------------------------------------- |
207 | //---------------------------------------------------------------------------------------------------- |
190 | int main(void) |
208 | int main(void) |
191 | { |
209 | { |
192 | /* Configure the system clocks */ |
210 | /* Configure the system clocks */ |
193 | SCU_Config(); |
211 | SCU_Config(); |
194 | /* init VIC (Vectored Interrupt Controller) */ |
212 | /* init VIC (Vectored Interrupt Controller) */ |
195 | SCU_AHBPeriphClockConfig(__VIC,ENABLE); // enable AHB bus clock for VIC |
213 | SCU_AHBPeriphClockConfig(__VIC,ENABLE); // enable AHB bus clock for VIC |
196 | SCU_AHBPeriphReset(__VIC, DISABLE); // disable reset state for VIC |
214 | SCU_AHBPeriphReset(__VIC, DISABLE); // disable reset state for VIC |
197 | VIC_DeInit(); // deinitializes the VIC module registers to their default reset values. |
215 | VIC_DeInit(); // deinitializes the VIC module registers to their default reset values. |
198 | // initialize the interrupt handler |
216 | // initialize the interrupt handler |
199 | Interrupt_Init(); |
217 | Interrupt_Init(); |
200 | // initialize timer 1 for System Clock and delay rountines |
218 | // initialize timer 1 for System Clock and delay rountines |
201 | TIMER1_Init(); |
219 | TIMER1_Init(); |
202 | // initialize the LEDs (needs Timer 1) |
220 | // initialize the LEDs (needs Timer 1) |
203 | Led_Init(); |
221 | Led_Init(); |
204 | // initialize the debug UART1 |
222 | // initialize the debug UART1 |
205 | UART1_Init(); |
223 | UART1_Init(); |
206 | UART1_PutString("\r\n---------------------------------------------"); |
224 | UART1_PutString("\r\n---------------------------------------------"); |
207 | // initialize timer 2 for servo outputs |
225 | // initialize timer 2 for servo outputs |
208 | TIMER2_Init(); |
226 | TIMER2_Init(); |
209 | // initialize UART2 to FLIGHTCTRL |
227 | // initialize UART2 to FLIGHTCTRL |
210 | UART2_Init(); |
228 | UART2_Init(); |
211 | // initialize UART0 (to MKGPS or MK3MAG) |
229 | // initialize UART0 (to MKGPS or MK3MAG) |
212 | UART0_Init(); |
230 | UART0_Init(); |
213 | // initialize adc |
231 | // initialize adc |
214 | Analog_Init(); |
232 | Analog_Init(); |
215 | // initialize usb |
233 | // initialize usb |
216 | //USB_ConfigInit(); |
234 | //USB_ConfigInit(); |
217 | // initialize SPI0 to FC |
235 | // initialize SPI0 to FC |
218 | SPI0_Init(); |
236 | SPI0_Init(); |
219 | // initialize i2c bus to MK3MAG (needs Timer 1) |
237 | // initialize i2c bus to MK3MAG (needs Timer 1) |
220 | I2C1_Init(); |
238 | I2C1_Init(); |
221 | // initialize the gps position controller (needs Timer 1) |
239 | // initialize the gps position controller (needs Timer 1) |
222 | GPS_Init(); |
240 | GPS_Init(); |
223 | // initialize fat16 partition on sd card (needs Timer 1) |
241 | // initialize fat16 partition on sd card (needs Timer 1) |
224 | Fat16_Init(); |
242 | Fat16_Init(); |
225 | // initialize the settings |
243 | // initialize the settings |
226 | Settings_Init(); |
244 | Settings_Init(); |
227 | // initialize logging (needs settings) |
245 | // initialize logging (needs settings) |
228 | Logging_Init(); |
246 | Logging_Init(); |
229 | 247 | ||
230 | TimerCheckError = SetDelay(3000); |
248 | TimerCheckError = SetDelay(3000); |
231 | UART1_PutString("\r\n---------------------------------------------"); |
249 | UART1_PutString("\r\n---------------------------------------------"); |
232 | UART1_PutString("\n\r Version information:"); |
250 | UART1_PutString("\n\r Version information:"); |
233 | 251 | ||
234 | GetNaviCtrlVersion(); |
252 | GetNaviCtrlVersion(); |
235 | 253 | ||
236 | I2C1_GetMK3MagVersion(); |
254 | I2C1_GetMK3MagVersion(); |
237 | if(MK3MAG_Version.Compatible != MK3MAG_I2C_COMPATIBLE) |
255 | if(MK3MAG_Version.Compatible != MK3MAG_I2C_COMPATIBLE) |
238 | { |
256 | { |
239 | UART1_PutString("\n\r MK3Mag not compatible"); |
257 | UART1_PutString("\n\r MK3Mag not compatible"); |
240 | LED_RED_ON; |
258 | LED_RED_ON; |
241 | } |
259 | } |
242 | 260 | ||
243 | SPI0_GetFlightCtrlVersion(); |
261 | SPI0_GetFlightCtrlVersion(); |
244 | if(FC_Version.Compatible != FC_SPI_COMPATIBLE) |
262 | if(FC_Version.Compatible != FC_SPI_COMPATIBLE) |
245 | { |
263 | { |
246 | UART1_PutString("\n\r Flight-Ctrl not compatible"); |
264 | UART1_PutString("\n\r Flight-Ctrl not compatible"); |
247 | LED_RED_ON; |
265 | LED_RED_ON; |
248 | } |
266 | } |
249 | 267 | ||
250 | UART0_GetMKOSDVersion(); |
268 | UART0_GetMKOSDVersion(); |
251 | 269 | ||
252 | UART1_PutString("\r\n---------------------------------------------\r\n\r\n"); |
270 | UART1_PutString("\r\n---------------------------------------------\r\n\r\n"); |
253 | 271 | ||
254 | for (;;) |
- | |
255 | { |
272 | // ---------- Prepare the isr driven |
- | 273 | // set to absolute lowest priority |
|
- | 274 | VIC_Config(EXTIT3_ITLine, VIC_IRQ, PRIORITY_SW); |
|
256 | UART1_ProcessRxData(); // process request |
275 | // enanble interrupts |
257 | UART1_TransmitTxData(); // send answer |
276 | VIC_ITCmd(EXTIT3_ITLine, ENABLE); |
- | 277 | ||
258 | 278 | for (;;) // the endless main loop |
|
259 | UART0_ProcessRxData(); // process request |
- | |
260 | UART0_TransmitTxData(); // send answer |
279 | { |
261 | 280 | UART0_TransmitTxData(); // send answer |
|
262 | USB_ProcessRxData(); // process request |
- | |
263 | USB_TransmitTxData(); // send answer |
- | |
264 | - | ||
265 | SPI0_UpdateBuffer(); // handle new SPI Data |
- | |
266 | 281 | UART1_TransmitTxData(); // send answer |
|
267 | //I2C1_UpdateCompass(); moved to Timer1 ISR to avoid timing problems |
282 | USB_TransmitTxData(); // send answer |
268 | 283 | ||
269 | // ---------------- Error Check Timing ---------------------------- |
284 | // ---------------- Error Check Timing ---------------------------- |
270 | if(CheckDelay(TimerCheckError)) |
285 | if(CheckDelay(TimerCheckError)) |
271 | { |
286 | { |
272 | TimerCheckError = SetDelay(1000); |
287 | TimerCheckError = SetDelay(1000); |
273 | if(CheckDelay(SPI0_Timeout)) GPS_Navigation(); // process the GPS data even if the FC is not connected |
288 | if(CheckDelay(SPI0_Timeout)) GPS_Navigation(); // process the GPS data even if the FC is not connected |
274 | CheckErrors(); |
289 | CheckErrors(); |
275 | if(FC.MKFlags & MKFLAG_FLY) NaviData.FlyingTime++; // we want to count the battery-time |
290 | if(FC.MKFlags & MKFLAG_FLY) NaviData.FlyingTime++; // we want to count the battery-time |
276 | // else NaviData.FlyingTime = 0; // not the time per flight |
291 | // else NaviData.FlyingTime = 0; // not the time per flight |
277 | UART1_Request_SendFollowMe = TRUE; |
292 | UART1_Request_SendFollowMe = TRUE; |
278 | if(SerialLinkOkay) SerialLinkOkay--; |
293 | if(SerialLinkOkay) SerialLinkOkay--; |
279 | if(SerialLinkOkay < 250 - 5) NCFlags |= NC_FLAG_NOSERIALLINK; // 5 seconds timeout for serial communication |
294 | if(SerialLinkOkay < 250 - 5) NCFlags |= NC_FLAG_NOSERIALLINK; // 5 seconds timeout for serial communication |
280 | else NCFlags &= ~NC_FLAG_NOSERIALLINK; |
295 | else NCFlags &= ~NC_FLAG_NOSERIALLINK; |
281 | } |
296 | } |
282 | // ---------------- Logging --------------------------------------- |
297 | // ---------------- Logging --------------------------------------- |
283 | Logging_Update(); // could be block some time for at max. 2 seconds, therefore move time critical part of the mainloop into the ISR of timer 1 |
298 | Logging_Update(); // could be block some time for at max. 2 seconds, therefore move time critical part of the mainloop into the ISR of timer 1 |
284 | // ---------------- ADC readings ----------------------------------- |
299 | // ---------------- ADC readings ----------------------------------- |
285 | Analog_Update(); |
- | |
286 | } |
300 | } |
287 | } |
301 | } |
288 | 302 | ||
289 | 303 | ||
290 | 304 |