Rev 228 | Rev 231 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 228 | Rev 229 | ||
---|---|---|---|
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 / NON-COMMERCIAL USE ONLY |
6 | // + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY |
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 oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
28 | // + Die Portierung oder Nutzung 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 the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed |
44 | // + * porting the sources to other systems or using the software on other systems (except 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 "params.h" |
76 | #include "params.h" |
77 | #include "settings.h" |
77 | #include "settings.h" |
78 | #include "config.h" |
78 | #include "config.h" |
79 | #include "main.h" |
79 | #include "main.h" |
80 | 80 | ||
81 | u8 BoardRelease = 0; |
81 | u8 BoardRelease = 0; |
82 | u32 TimerCheckError; |
82 | u32 TimerCheckError; |
83 | u32 ErrorCode = 0; |
83 | u32 ErrorCode = 0; |
84 | u16 BeepTime; |
84 | u16 BeepTime; |
85 | u8 NCFlags = 0; |
85 | u8 NCFlags = 0; |
86 | s16 GeoMagDec = 0; // local magnetic declination in 0.1 deg |
86 | s16 GeoMagDec = 0; // local magnetic declination in 0.1 deg |
87 | 87 | ||
88 | u8 ClearFCFlags = 0; |
88 | u8 ClearFCFlags = 0; |
89 | u8 StopNavigation = 0; |
89 | u8 StopNavigation = 0; |
90 | Param_t Parameter; |
90 | Param_t Parameter; |
91 | volatile FC_t FC; |
91 | volatile FC_t FC; |
92 | 92 | ||
93 | s8 ErrorMSG[25]; |
93 | s8 ErrorMSG[25]; |
94 | 94 | ||
95 | //---------------------------------------------------------------------------------------------------- |
95 | //---------------------------------------------------------------------------------------------------- |
96 | void SCU_Config(void) |
96 | void SCU_Config(void) |
97 | { |
97 | { |
98 | /* configure PLL and set it as master clock source */ |
98 | /* configure PLL and set it as master clock source */ |
99 | SCU_MCLKSourceConfig(SCU_MCLK_OSC); // set master clock source to external oscillator clock (25MHz) before diabling the PLL |
99 | SCU_MCLKSourceConfig(SCU_MCLK_OSC); // set master clock source to external oscillator clock (25MHz) before diabling the PLL |
100 | SCU_PLLCmd(DISABLE); // now disable the PLL |
100 | SCU_PLLCmd(DISABLE); // now disable the PLL |
101 | #ifdef MCLK96MHZ |
101 | #ifdef MCLK96MHZ |
102 | SCU_BRCLKDivisorConfig(SCU_BRCLK_Div2); // set BRCLK to MCLK/2 = 48MHz |
102 | SCU_BRCLKDivisorConfig(SCU_BRCLK_Div2); // set BRCLK to MCLK/2 = 48MHz |
103 | SCU_PCLKDivisorConfig(SCU_PCLK_Div4); // set PCLK (APB bus clock) divisor to 4 (half Reference Clock) |
103 | SCU_PCLKDivisorConfig(SCU_PCLK_Div4); // set PCLK (APB bus clock) divisor to 4 (half Reference Clock) |
104 | SCU_RCLKDivisorConfig(SCU_RCLK_Div2); // set RCLK (Reference Clock) divisor to 1 (full PPL clock) |
104 | SCU_RCLKDivisorConfig(SCU_RCLK_Div2); // set RCLK (Reference Clock) divisor to 1 (full PPL clock) |
105 | SCU_HCLKDivisorConfig(SCU_HCLK_Div2); // set HCLK (AHB bus clock) divisor to 1 (full Reference Clock) |
105 | SCU_HCLKDivisorConfig(SCU_HCLK_Div2); // set HCLK (AHB bus clock) divisor to 1 (full Reference Clock) |
106 | SCU_PLLFactorsConfig(192,25,2); // PLL = 96 MHz, Feedback Divider N=192, Pre-Divider M=25, Post-Divider P=2 |
106 | SCU_PLLFactorsConfig(192,25,2); // PLL = 96 MHz, Feedback Divider N=192, Pre-Divider M=25, Post-Divider P=2 |
107 | #else |
107 | #else |
108 | SCU_BRCLKDivisorConfig(SCU_BRCLK_Div1); // set BRCLK to MCLK = 48MHz |
108 | SCU_BRCLKDivisorConfig(SCU_BRCLK_Div1); // set BRCLK to MCLK = 48MHz |
109 | SCU_PCLKDivisorConfig(SCU_PCLK_Div2); // set PCLK (APB bus clock) divisor to 2 (half Reference Clock) |
109 | SCU_PCLKDivisorConfig(SCU_PCLK_Div2); // set PCLK (APB bus clock) divisor to 2 (half Reference Clock) |
110 | SCU_RCLKDivisorConfig(SCU_RCLK_Div1); // set RCLK (Reference Clock) divisor to 1 (full PPL clock) |
110 | SCU_RCLKDivisorConfig(SCU_RCLK_Div1); // set RCLK (Reference Clock) divisor to 1 (full PPL clock) |
111 | SCU_HCLKDivisorConfig(SCU_HCLK_Div1); // set HCLK (AHB bus clock) divisor to 1 (full Reference Clock) |
111 | SCU_HCLKDivisorConfig(SCU_HCLK_Div1); // set HCLK (AHB bus clock) divisor to 1 (full Reference Clock) |
112 | SCU_PLLFactorsConfig(192,25,3); // PLL = 48 MHz, Feedback Divider N=192, Pre-Divider M=25, Post-Divider P=3 |
112 | SCU_PLLFactorsConfig(192,25,3); // PLL = 48 MHz, Feedback Divider N=192, Pre-Divider M=25, Post-Divider P=3 |
113 | #endif |
113 | #endif |
114 | SCU_PLLCmd(ENABLE); // Enable PLL (is disabled by SCU_PLLFactorsConfig) |
114 | SCU_PLLCmd(ENABLE); // Enable PLL (is disabled by SCU_PLLFactorsConfig) |
115 | SCU_MCLKSourceConfig(SCU_MCLK_PLL); // set master clock source to PLL |
115 | SCU_MCLKSourceConfig(SCU_MCLK_PLL); // set master clock source to PLL |
116 | } |
116 | } |
117 | 117 | ||
118 | //---------------------------------------------------------------------------------------------------- |
118 | //---------------------------------------------------------------------------------------------------- |
119 | void GetNaviCtrlVersion(void) |
119 | void GetNaviCtrlVersion(void) |
120 | { |
120 | { |
121 | u8 msg[25]; |
121 | u8 msg[25]; |
122 | 122 | ||
123 | sprintf(msg,"\n\r NaviCtrl V%d.%d%c", VERSION_MAJOR, VERSION_MINOR, 'a'+ VERSION_PATCH); |
123 | sprintf(msg,"\n\r NaviCtrl V%d.%d%c", VERSION_MAJOR, VERSION_MINOR, 'a'+ VERSION_PATCH); |
124 | UART1_PutString(msg); |
124 | UART1_PutString(msg); |
125 | } |
125 | } |
126 | 126 | ||
127 | //---------------------------------------------------------------------------------------------------- |
127 | //---------------------------------------------------------------------------------------------------- |
128 | void CheckErrors(void) |
128 | void CheckErrors(void) |
129 | { |
129 | { |
130 | if(CheckDelay(SPI0_Timeout)) |
130 | if(CheckDelay(SPI0_Timeout)) |
131 | { |
131 | { |
132 | LED_RED_ON; |
132 | LED_RED_ON; |
133 | sprintf(ErrorMSG,"no FC communication "); |
133 | sprintf(ErrorMSG,"no FC communication "); |
134 | ErrorCode = 3; |
134 | ErrorCode = 3; |
135 | StopNavigation = 1; |
135 | StopNavigation = 1; |
136 | } |
136 | } |
137 | else if((CheckDelay(I2C1_Timeout)) || (I2C_Heading.Heading == -2)) |
137 | else if(CheckDelay(I2C1_Timeout)) |
138 | { |
138 | { |
139 | LED_RED_ON; |
139 | LED_RED_ON; |
140 | sprintf(ErrorMSG,"no MK3Mag communication "); |
140 | sprintf(ErrorMSG,"no MK3Mag communication "); |
141 | //Reset I2CBus |
141 | //Reset I2CBus |
142 | I2C1_Deinit(); |
142 | I2C1_Deinit(); |
143 | I2C1_Init(); |
143 | I2C1_Init(); |
144 | ErrorCode = 4; |
144 | ErrorCode = 4; |
145 | StopNavigation = 1; |
145 | StopNavigation = 1; |
146 | } |
146 | } |
147 | else if(FC_Version.Compatible != FC_SPI_COMPATIBLE) |
147 | else if(FC_Version.Compatible != FC_SPI_COMPATIBLE) |
148 | { |
148 | { |
149 | LED_RED_ON; |
149 | LED_RED_ON; |
150 | sprintf(ErrorMSG,"FC not compatible "); |
150 | sprintf(ErrorMSG,"FC not compatible "); |
151 | ErrorCode = 1; |
151 | ErrorCode = 1; |
152 | StopNavigation = 1; |
152 | StopNavigation = 1; |
153 | } |
153 | } |
154 | else if(MK3MAG_Version.Compatible != MK3MAG_I2C_COMPATIBLE) |
154 | else if(MK3MAG_Version.Compatible != MK3MAG_I2C_COMPATIBLE) |
155 | { |
155 | { |
156 | sprintf(ErrorMSG,"MK3Mag not compatible "); |
156 | sprintf(ErrorMSG,"MK3Mag not compatible "); |
157 | LED_RED_ON; |
157 | LED_RED_ON; |
158 | ErrorCode = 2; |
158 | ErrorCode = 2; |
159 | StopNavigation = 1; |
159 | StopNavigation = 1; |
160 | } |
160 | } |
161 | else if(CheckDelay(UBX_Timeout)) |
161 | else if(CheckDelay(UBX_Timeout)) |
162 | { |
162 | { |
163 | LED_RED_ON; |
163 | LED_RED_ON; |
164 | sprintf(ErrorMSG,"no GPS communication "); |
164 | sprintf(ErrorMSG,"no GPS communication "); |
165 | ErrorCode = 5; |
165 | ErrorCode = 5; |
166 | StopNavigation = 1; |
166 | StopNavigation = 1; |
167 | } |
167 | } |
168 | else if(I2C_Heading.Heading < 0) |
168 | else if(I2C_Heading.Heading < 0) |
169 | { |
169 | { |
170 | LED_RED_ON; |
170 | LED_RED_ON; |
171 | sprintf(ErrorMSG,"bad compass value "); |
171 | sprintf(ErrorMSG,"bad compass value "); |
172 | ErrorCode = 6; |
172 | ErrorCode = 6; |
173 | StopNavigation = 1; |
173 | StopNavigation = 1; |
174 | } |
174 | } |
175 | else if(FC.Flags & FCFLAG_SPI_RX_ERR) |
175 | else if(FC.Flags & FCFLAG_SPI_RX_ERR) |
176 | { |
176 | { |
177 | LED_RED_ON; |
177 | LED_RED_ON; |
178 | sprintf(ErrorMSG,"FC spi rx error "); |
178 | sprintf(ErrorMSG,"FC spi rx error "); |
179 | ErrorCode = 8; |
179 | ErrorCode = 8; |
180 | StopNavigation = 1; |
180 | StopNavigation = 1; |
181 | } |
181 | } |
182 | else if(FC.RC_Quality < 100) |
182 | else if(FC.RC_Quality < 100) |
183 | { |
183 | { |
184 | LED_RED_ON; |
184 | LED_RED_ON; |
185 | sprintf(ErrorMSG,"RC Signal lost "); |
185 | sprintf(ErrorMSG,"RC Signal lost "); |
186 | ErrorCode = 7; |
186 | ErrorCode = 7; |
187 | } |
187 | } |
188 | else // no error occured |
188 | else // no error occured |
189 | { |
189 | { |
190 | sprintf(ErrorMSG,"No Error "); |
190 | sprintf(ErrorMSG,"No Error "); |
191 | ErrorCode = 0; |
191 | ErrorCode = 0; |
192 | StopNavigation = 0; |
192 | StopNavigation = 0; |
193 | LED_RED_OFF; |
193 | LED_RED_OFF; |
194 | } |
194 | } |
195 | } |
195 | } |
196 | 196 | ||
197 | // the handler will be cyclic called by the timer 1 ISR |
197 | // the handler will be cyclic called by the timer 1 ISR |
198 | // used is for critical timing parts that normaly would handled |
198 | // used is for critical timing parts that normaly would handled |
199 | // within the main loop that could block longer at logging activities |
199 | // within the main loop that could block longer at logging activities |
200 | void EXTIT3_IRQHandler(void) |
200 | void EXTIT3_IRQHandler(void) |
201 | { |
201 | { |
202 | IENABLE; |
202 | IENABLE; |
203 | 203 | ||
204 | VIC_ITCmd(EXTIT3_ITLine,DISABLE); // disable irq |
204 | VIC_ITCmd(EXTIT3_ITLine,DISABLE); // disable irq |
205 | VIC_SWITCmd(EXTIT3_ITLine,DISABLE); // clear pending bit |
205 | VIC_SWITCmd(EXTIT3_ITLine,DISABLE); // clear pending bit |
206 | I2C1_UpdateCompass(); // update compass communication |
206 | I2C1_UpdateCompass(); // update compass communication |
207 | Analog_Update(); // get new ADC values |
207 | Analog_Update(); // get new ADC values |
208 | VIC_ITCmd(EXTIT3_ITLine, ENABLE); // enable irq |
208 | VIC_ITCmd(EXTIT3_ITLine, ENABLE); // enable irq |
209 | 209 | ||
210 | IDISABLE; |
210 | IDISABLE; |
211 | } |
211 | } |
212 | 212 | ||
213 | //---------------------------------------------------------------------------------------------------- |
213 | //---------------------------------------------------------------------------------------------------- |
214 | int main(void) |
214 | int main(void) |
215 | { |
215 | { |
216 | /* Configure the system clocks */ |
216 | /* Configure the system clocks */ |
217 | SCU_Config(); |
217 | SCU_Config(); |
218 | /* init VIC (Vectored Interrupt Controller) */ |
218 | /* init VIC (Vectored Interrupt Controller) */ |
219 | SCU_AHBPeriphClockConfig(__VIC,ENABLE); // enable AHB bus clock for VIC |
219 | SCU_AHBPeriphClockConfig(__VIC,ENABLE); // enable AHB bus clock for VIC |
220 | SCU_AHBPeriphReset(__VIC, DISABLE); // disable reset state for VIC |
220 | SCU_AHBPeriphReset(__VIC, DISABLE); // disable reset state for VIC |
221 | VIC_DeInit(); // deinitializes the VIC module registers to their default reset values. |
221 | VIC_DeInit(); // deinitializes the VIC module registers to their default reset values. |
222 | VIC_InitDefaultVectors(); |
222 | VIC_InitDefaultVectors(); |
223 | 223 | ||
224 | // initialize timer 1 for System Clock and delay rountines |
224 | // initialize timer 1 for System Clock and delay rountines |
225 | TIMER1_Init(); |
225 | TIMER1_Init(); |
226 | // initialize the LEDs (needs Timer 1) |
226 | // initialize the LEDs (needs Timer 1) |
227 | Led_Init(); |
227 | Led_Init(); |
228 | // initialize the debug UART1 |
228 | // initialize the debug UART1 |
229 | UART1_Init(); |
229 | UART1_Init(); |
230 | UART1_PutString("\r\n---------------------------------------------"); |
230 | UART1_PutString("\r\n---------------------------------------------"); |
231 | // initialize timer 2 for servo outputs |
231 | // initialize timer 2 for servo outputs |
232 | //TIMER2_Init(); |
232 | //TIMER2_Init(); |
233 | // initialize UART2 to FLIGHTCTRL |
233 | // initialize UART2 to FLIGHTCTRL |
234 | UART2_Init(); |
234 | UART2_Init(); |
235 | // initialize UART0 (to MKGPS or MK3MAG) |
235 | // initialize UART0 (to MKGPS or MK3MAG) |
236 | UART0_Init(); |
236 | UART0_Init(); |
237 | // initialize adc |
237 | // initialize adc |
238 | Analog_Init(); |
238 | Analog_Init(); |
239 | // initialize usb |
239 | // initialize usb |
240 | //USB_ConfigInit(); |
240 | //USB_ConfigInit(); |
241 | // initialize SPI0 to FC |
241 | // initialize SPI0 to FC |
242 | SPI0_Init(); |
242 | SPI0_Init(); |
243 | // initialize i2c bus to MK3MAG (needs Timer 1) |
243 | // initialize i2c bus to MK3MAG (needs Timer 1) |
244 | I2C_Heading.Heading = -1; |
244 | I2C_Heading.Heading = -1; |
245 | I2C1_Init(); |
245 | I2C1_Init(); |
246 | // initialize the gps position controller (needs Timer 1) |
246 | // initialize the gps position controller (needs Timer 1) |
247 | Fat16_Init(); |
247 | Fat16_Init(); |
248 | // initialize NC params |
248 | // initialize NC params |
249 | NCParams_Init(); |
249 | NCParams_Init(); |
250 | // initialize the settings |
250 | // initialize the settings |
251 | Settings_Init(); |
251 | Settings_Init(); |
252 | // initialize logging (needs settings) |
252 | // initialize logging (needs settings) |
253 | Logging_Init(); |
253 | Logging_Init(); |
254 | 254 | ||
255 | TimerCheckError = SetDelay(3000); |
255 | TimerCheckError = SetDelay(3000); |
256 | UART1_PutString("\r\n++++++++++++++++++++++++++++++++++++++++++"); |
256 | UART1_PutString("\r\n++++++++++++++++++++++++++++++++++++++++++"); |
257 | UART1_PutString("\n\r Version information:"); |
257 | UART1_PutString("\n\r Version information:"); |
258 | 258 | ||
259 | GetNaviCtrlVersion(); |
259 | GetNaviCtrlVersion(); |
260 | 260 | ||
261 | I2C1_GetMK3MagVersion(); |
261 | I2C1_GetMK3MagVersion(); |
262 | if(MK3MAG_Version.Compatible != MK3MAG_I2C_COMPATIBLE) |
262 | if(MK3MAG_Version.Compatible != MK3MAG_I2C_COMPATIBLE) |
263 | { |
263 | { |
264 | UART1_PutString("\n\r MK3Mag not compatible"); |
264 | UART1_PutString("\n\r MK3Mag not compatible"); |
265 | LED_RED_ON; |
265 | LED_RED_ON; |
266 | } |
266 | } |
267 | 267 | ||
268 | SPI0_GetFlightCtrlVersion(); |
268 | SPI0_GetFlightCtrlVersion(); |
269 | if(FC_Version.Compatible != FC_SPI_COMPATIBLE) |
269 | if(FC_Version.Compatible != FC_SPI_COMPATIBLE) |
270 | { |
270 | { |
271 | UART1_PutString("\n\r Flight-Ctrl not compatible"); |
271 | UART1_PutString("\n\r Flight-Ctrl not compatible"); |
272 | LED_RED_ON; |
272 | LED_RED_ON; |
273 | } |
273 | } |
274 | 274 | ||
275 | UART0_GetMKOSDVersion(); |
275 | UART0_GetMKOSDVersion(); |
276 | 276 | ||
277 | GPS_Init(); |
277 | GPS_Init(); |
278 | // initialize fat16 partition on sd card (needs Timer 1) |
278 | // initialize fat16 partition on sd card (needs Timer 1) |
279 | 279 | ||
280 | // ---------- Prepare the isr driven |
280 | // ---------- Prepare the isr driven |
281 | // set to absolute lowest priority |
281 | // set to absolute lowest priority |
282 | VIC_Config(EXTIT3_ITLine, VIC_IRQ, PRIORITY_SW); |
282 | VIC_Config(EXTIT3_ITLine, VIC_IRQ, PRIORITY_SW); |
283 | // enable interrupts |
283 | // enable interrupts |
284 | VIC_ITCmd(EXTIT3_ITLine, ENABLE); |
284 | VIC_ITCmd(EXTIT3_ITLine, ENABLE); |
285 | I2C1_Timeout = SetDelay(1000); |
- | |
286 | for (;;) // the endless main loop |
285 | for (;;) // the endless main loop |
287 | { |
286 | { |
288 | UART0_ProcessRxData(); // process request |
287 | UART0_ProcessRxData(); // process request |
289 | UART1_ProcessRxData(); // process request |
288 | UART1_ProcessRxData(); // process request |
290 | USB_ProcessRxData(); // process request |
289 | USB_ProcessRxData(); // process request |
291 | UART0_TransmitTxData(); // send answer |
290 | UART0_TransmitTxData(); // send answer |
292 | UART1_TransmitTxData(); // send answer |
291 | UART1_TransmitTxData(); // send answer |
293 | UART2_TransmitTxData(); // send answer |
292 | UART2_TransmitTxData(); // send answer |
294 | USB_TransmitTxData(); // send answer |
293 | USB_TransmitTxData(); // send answer |
295 | 294 | ||
296 | SPI0_UpdateBuffer(); // handle new SPI Data |
295 | SPI0_UpdateBuffer(); // handle new SPI Data |
297 | // ---------------- Error Check Timing ---------------------------- |
296 | // ---------------- Error Check Timing ---------------------------- |
298 | if(CheckDelay(TimerCheckError)) |
297 | if(CheckDelay(TimerCheckError)) |
299 | { |
298 | { |
300 | TimerCheckError = SetDelay(1000); |
299 | TimerCheckError = SetDelay(1000); |
301 | if(CheckDelay(SPI0_Timeout)) GPS_Navigation(&GPSData, &(ToFlightCtrl.GPSStick)); // process the GPS data even if the FC is not connected |
300 | if(CheckDelay(SPI0_Timeout)) GPS_Navigation(&GPSData, &(ToFlightCtrl.GPSStick)); // process the GPS data even if the FC is not connected |
302 | CheckErrors(); |
301 | CheckErrors(); |
303 | if(FC.Flags & FCFLAG_FLY) NaviData.FlyingTime++; // we want to count the battery-time |
302 | if(FC.Flags & FCFLAG_FLY) NaviData.FlyingTime++; // we want to count the battery-time |
304 | // else NaviData.FlyingTime = 0; // not the time per flight |
303 | // else NaviData.FlyingTime = 0; // not the time per flight |
305 | if(SerialLinkOkay) SerialLinkOkay--; |
304 | if(SerialLinkOkay) SerialLinkOkay--; |
306 | if(SerialLinkOkay < 250 - 5) NCFlags |= NC_FLAG_NOSERIALLINK; // 5 seconds timeout for serial communication |
305 | if(SerialLinkOkay < 250 - 5) NCFlags |= NC_FLAG_NOSERIALLINK; // 5 seconds timeout for serial communication |
307 | else NCFlags &= ~NC_FLAG_NOSERIALLINK; |
306 | else NCFlags &= ~NC_FLAG_NOSERIALLINK; |
308 | if(StopNavigation && (Parameter.NaviGpsModeControl >= 50)) BeepTime = 1000; |
307 | if(StopNavigation && (Parameter.NaviGpsModeControl >= 50)) BeepTime = 1000; |
309 | } |
308 | } |
310 | // ---------------- Logging --------------------------------------- |
309 | // ---------------- Logging --------------------------------------- |
311 | 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 |
310 | 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 |
312 | } |
311 | } |
313 | } |
312 | } |
314 | 313 | ||
315 | 314 | ||
316 | 315 |