Rev 292 | Rev 318 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 292 | Rev 314 | ||
---|---|---|---|
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 "compass.h" |
68 | #include "compass.h" |
69 | #include "timer1.h" |
69 | #include "timer1.h" |
70 | #include "timer2.h" |
70 | #include "timer2.h" |
71 | #include "analog.h" |
71 | #include "analog.h" |
72 | #include "spi_slave.h" |
72 | #include "spi_slave.h" |
73 | #include "fat16.h" |
73 | #include "fat16.h" |
74 | #include "usb.h" |
74 | #include "usb.h" |
75 | #include "sdc.h" |
75 | #include "sdc.h" |
76 | #include "logging.h" |
76 | #include "logging.h" |
77 | #include "params.h" |
77 | #include "params.h" |
78 | #include "settings.h" |
78 | #include "settings.h" |
79 | #include "config.h" |
79 | #include "config.h" |
80 | #include "main.h" |
80 | #include "main.h" |
81 | #include "debug.h" |
81 | #include "debug.h" |
82 | - | ||
83 | #include "eeprom.h" |
82 | #include "eeprom.h" |
- | 83 | ||
- | 84 | #ifdef FOLLOW_ME |
|
- | 85 | u8 TransmitAlsoToFC = 0; |
|
84 | 86 | #endif |
|
85 | u32 TimerCheckError; |
87 | u32 TimerCheckError; |
86 | u8 ErrorCode = 0; |
88 | u8 ErrorCode = 0; |
87 | u16 BeepTime; |
89 | u16 BeepTime; |
88 | u8 NCFlags = 0; |
90 | u8 NCFlags = 0; |
89 | s16 GeoMagDec = 0; // local magnetic declination in 0.1 deg |
91 | s16 GeoMagDec = 0; // local magnetic declination in 0.1 deg |
90 | 92 | ||
91 | u8 ClearFCStatusFlags = 0; |
93 | u8 ClearFCStatusFlags = 0; |
92 | u8 StopNavigation = 0; |
94 | u8 StopNavigation = 0; |
93 | Param_t Parameter; |
95 | Param_t Parameter; |
94 | volatile FC_t FC; |
96 | volatile FC_t FC; |
95 | 97 | ||
96 | s8 ErrorMSG[25]; |
98 | s8 ErrorMSG[25]; |
97 | 99 | ||
98 | //---------------------------------------------------------------------------------------------------- |
100 | //---------------------------------------------------------------------------------------------------- |
99 | void SCU_Config(void) |
101 | void SCU_Config(void) |
100 | { |
102 | { |
101 | /* configure PLL and set it as master clock source */ |
103 | /* configure PLL and set it as master clock source */ |
102 | SCU_MCLKSourceConfig(SCU_MCLK_OSC); // set master clock source to external oscillator clock (25MHz) before diabling the PLL |
104 | SCU_MCLKSourceConfig(SCU_MCLK_OSC); // set master clock source to external oscillator clock (25MHz) before diabling the PLL |
103 | SCU_PLLCmd(DISABLE); // now disable the PLL |
105 | SCU_PLLCmd(DISABLE); // now disable the PLL |
104 | #ifdef MCLK96MHZ |
106 | #ifdef MCLK96MHZ |
105 | SCU_BRCLKDivisorConfig(SCU_BRCLK_Div2); // set BRCLK to MCLK/2 = 48MHz |
107 | SCU_BRCLKDivisorConfig(SCU_BRCLK_Div2); // set BRCLK to MCLK/2 = 48MHz |
106 | SCU_PCLKDivisorConfig(SCU_PCLK_Div4); // set PCLK (APB bus clock) divisor to 4 (half Reference Clock) |
108 | SCU_PCLKDivisorConfig(SCU_PCLK_Div4); // set PCLK (APB bus clock) divisor to 4 (half Reference Clock) |
107 | SCU_RCLKDivisorConfig(SCU_RCLK_Div2); // set RCLK (Reference Clock) divisor to 1 (full PPL clock) |
109 | SCU_RCLKDivisorConfig(SCU_RCLK_Div2); // set RCLK (Reference Clock) divisor to 1 (full PPL clock) |
108 | SCU_HCLKDivisorConfig(SCU_HCLK_Div2); // set HCLK (AHB bus clock) divisor to 1 (full Reference Clock) |
110 | SCU_HCLKDivisorConfig(SCU_HCLK_Div2); // set HCLK (AHB bus clock) divisor to 1 (full Reference Clock) |
109 | SCU_PLLFactorsConfig(192,25,2); // PLL = 96 MHz, Feedback Divider N=192, Pre-Divider M=25, Post-Divider P=2 |
111 | SCU_PLLFactorsConfig(192,25,2); // PLL = 96 MHz, Feedback Divider N=192, Pre-Divider M=25, Post-Divider P=2 |
110 | #else |
112 | #else |
111 | SCU_BRCLKDivisorConfig(SCU_BRCLK_Div1); // set BRCLK to MCLK = 48MHz |
113 | SCU_BRCLKDivisorConfig(SCU_BRCLK_Div1); // set BRCLK to MCLK = 48MHz |
112 | SCU_PCLKDivisorConfig(SCU_PCLK_Div2); // set PCLK (APB bus clock) divisor to 2 (half Reference Clock) |
114 | SCU_PCLKDivisorConfig(SCU_PCLK_Div2); // set PCLK (APB bus clock) divisor to 2 (half Reference Clock) |
113 | SCU_RCLKDivisorConfig(SCU_RCLK_Div1); // set RCLK (Reference Clock) divisor to 1 (full PPL clock) |
115 | SCU_RCLKDivisorConfig(SCU_RCLK_Div1); // set RCLK (Reference Clock) divisor to 1 (full PPL clock) |
114 | SCU_HCLKDivisorConfig(SCU_HCLK_Div1); // set HCLK (AHB bus clock) divisor to 1 (full Reference Clock) |
116 | SCU_HCLKDivisorConfig(SCU_HCLK_Div1); // set HCLK (AHB bus clock) divisor to 1 (full Reference Clock) |
115 | SCU_PLLFactorsConfig(192,25,3); // PLL = 48 MHz, Feedback Divider N=192, Pre-Divider M=25, Post-Divider P=3 |
117 | SCU_PLLFactorsConfig(192,25,3); // PLL = 48 MHz, Feedback Divider N=192, Pre-Divider M=25, Post-Divider P=3 |
116 | #endif |
118 | #endif |
117 | SCU_PLLCmd(ENABLE); // Enable PLL (is disabled by SCU_PLLFactorsConfig) |
119 | SCU_PLLCmd(ENABLE); // Enable PLL (is disabled by SCU_PLLFactorsConfig) |
118 | SCU_MCLKSourceConfig(SCU_MCLK_PLL); // set master clock source to PLL |
120 | SCU_MCLKSourceConfig(SCU_MCLK_PLL); // set master clock source to PLL |
119 | } |
121 | } |
120 | 122 | ||
121 | //---------------------------------------------------------------------------------------------------- |
123 | //---------------------------------------------------------------------------------------------------- |
122 | void GetNaviCtrlVersion(void) |
124 | void GetNaviCtrlVersion(void) |
123 | { |
125 | { |
124 | u8 msg[25]; |
126 | u8 msg[25]; |
125 | 127 | ||
126 | sprintf(msg,"\n\r NaviCtrl HW: V%d.%d SW: V%d.%d%c", Version_HW/10, Version_HW%10, VERSION_MAJOR, VERSION_MINOR, 'a'+ VERSION_PATCH); |
128 | sprintf(msg,"\n\r NaviCtrl HW: V%d.%d SW: V%d.%d%c", Version_HW/10, Version_HW%10, VERSION_MAJOR, VERSION_MINOR, 'a'+ VERSION_PATCH); |
127 | UART1_PutString(msg); |
129 | UART1_PutString(msg); |
128 | } |
130 | } |
129 | 131 | ||
130 | //---------------------------------------------------------------------------------------------------- |
132 | //---------------------------------------------------------------------------------------------------- |
131 | 133 | ||
132 | void CheckErrors(void) |
134 | void CheckErrors(void) |
133 | { |
135 | { |
134 | UART_VersionInfo.HardwareError[0] = 0; |
136 | UART_VersionInfo.HardwareError[0] = 0; |
135 | 137 | ||
136 | if(CheckDelay(I2C1_Timeout) || (Compass_Heading < 0)) DebugOut.Status[1] |= 0x08; |
138 | if(CheckDelay(I2C1_Timeout) || (Compass_Heading < 0)) DebugOut.Status[1] |= 0x08; |
137 | else DebugOut.Status[1] &= ~0x08; // MK3Mag green status |
139 | else DebugOut.Status[1] &= ~0x08; // MK3Mag green status |
138 | 140 | ||
139 | if((FC.Error[1] & FC_ERROR1_I2C) || (FC.Error[1] & FC_ERROR1_BL_MISSING)) DebugOut.Status[1] |= 0x02; |
141 | if((FC.Error[1] & FC_ERROR1_I2C) || (FC.Error[1] & FC_ERROR1_BL_MISSING)) DebugOut.Status[1] |= 0x02; |
140 | else DebugOut.Status[1] &= ~0x02; // BL-Ctrl green status |
142 | else DebugOut.Status[1] &= ~0x02; // BL-Ctrl green status |
141 | 143 | ||
142 | if(CheckDelay(SPI0_Timeout)) |
144 | if(CheckDelay(SPI0_Timeout)) |
143 | { |
145 | { |
144 | LED_RED_ON; |
146 | LED_RED_ON; |
145 | sprintf(ErrorMSG,"no FC communication "); |
147 | sprintf(ErrorMSG,"no FC communication "); |
146 | ErrorCode = 3; |
148 | ErrorCode = 3; |
147 | StopNavigation = 1; |
149 | StopNavigation = 1; |
148 | DebugOut.Status[0] &= ~0x01; // status of FC Present |
150 | DebugOut.Status[0] &= ~0x01; // status of FC Present |
149 | DebugOut.Status[0] &= ~0x02; // status of BL Present |
151 | DebugOut.Status[0] &= ~0x02; // status of BL Present |
150 | UART_VersionInfo.HardwareError[0] |= NC_ERROR0_SPI_RX; |
152 | UART_VersionInfo.HardwareError[0] |= NC_ERROR0_SPI_RX; |
151 | } |
153 | } |
152 | else if(CheckDelay(I2C1_Timeout)) |
154 | else if(CheckDelay(I2C1_Timeout)) |
153 | { |
155 | { |
154 | LED_RED_ON; |
156 | LED_RED_ON; |
155 | sprintf(ErrorMSG,"no compass communication "); |
157 | sprintf(ErrorMSG,"no compass communication "); |
156 | //Reset I2CBus |
158 | //Reset I2CBus |
157 | I2C1_Deinit(); |
159 | I2C1_Deinit(); |
158 | I2C1_Init(); |
160 | I2C1_Init(); |
159 | ErrorCode = 4; |
161 | ErrorCode = 4; |
160 | StopNavigation = 1; |
162 | StopNavigation = 1; |
161 | UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_RX; |
163 | UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_RX; |
162 | DebugOut.Status[1] |= 0x08; |
164 | DebugOut.Status[1] |= 0x08; |
163 | } |
165 | } |
164 | else if(FC_Version.Compatible != FC_SPI_COMPATIBLE) |
166 | else if(FC_Version.Compatible != FC_SPI_COMPATIBLE) |
165 | { |
167 | { |
166 | LED_RED_ON; |
168 | LED_RED_ON; |
167 | sprintf(ErrorMSG,"FC not compatible "); |
169 | sprintf(ErrorMSG,"FC not compatible "); |
168 | ErrorCode = 1; |
170 | ErrorCode = 1; |
169 | StopNavigation = 1; |
171 | StopNavigation = 1; |
170 | UART_VersionInfo.HardwareError[0] |= NC_ERROR0_FC_INCOMPATIBLE; |
172 | UART_VersionInfo.HardwareError[0] |= NC_ERROR0_FC_INCOMPATIBLE; |
171 | } |
173 | } |
172 | 174 | ||
173 | else if(FC.Error[0] & FC_ERROR0_GYRO_NICK) |
175 | else if(FC.Error[0] & FC_ERROR0_GYRO_NICK) |
174 | { |
176 | { |
175 | LED_RED_ON; |
177 | LED_RED_ON; |
176 | sprintf(ErrorMSG,"ERR: FC Nick Gyro"); |
178 | sprintf(ErrorMSG,"ERR: FC Nick Gyro"); |
177 | ErrorCode = 10; |
179 | ErrorCode = 10; |
178 | } |
180 | } |
179 | else if(FC.Error[0] & FC_ERROR0_GYRO_ROLL) |
181 | else if(FC.Error[0] & FC_ERROR0_GYRO_ROLL) |
180 | { |
182 | { |
181 | LED_RED_ON; |
183 | LED_RED_ON; |
182 | sprintf(ErrorMSG,"ERR: FC Roll Gyro"); |
184 | sprintf(ErrorMSG,"ERR: FC Roll Gyro"); |
183 | ErrorCode = 11; |
185 | ErrorCode = 11; |
184 | } |
186 | } |
185 | else if(FC.Error[0] & FC_ERROR0_GYRO_YAW) |
187 | else if(FC.Error[0] & FC_ERROR0_GYRO_YAW) |
186 | { |
188 | { |
187 | LED_RED_ON; |
189 | LED_RED_ON; |
188 | sprintf(ErrorMSG,"ERR: FC Yaw Gyro"); |
190 | sprintf(ErrorMSG,"ERR: FC Yaw Gyro"); |
189 | ErrorCode = 12; |
191 | ErrorCode = 12; |
190 | } |
192 | } |
191 | else if(FC.Error[0] & FC_ERROR0_ACC_NICK) |
193 | else if(FC.Error[0] & FC_ERROR0_ACC_NICK) |
192 | { |
194 | { |
193 | LED_RED_ON; |
195 | LED_RED_ON; |
194 | sprintf(ErrorMSG,"ERR: FC Nick ACC"); |
196 | sprintf(ErrorMSG,"ERR: FC Nick ACC"); |
195 | ErrorCode = 13; |
197 | ErrorCode = 13; |
196 | } |
198 | } |
197 | else if(FC.Error[0] & FC_ERROR0_ACC_ROLL) |
199 | else if(FC.Error[0] & FC_ERROR0_ACC_ROLL) |
198 | { |
200 | { |
199 | LED_RED_ON; |
201 | LED_RED_ON; |
200 | sprintf(ErrorMSG,"ERR: FC Roll ACC"); |
202 | sprintf(ErrorMSG,"ERR: FC Roll ACC"); |
201 | ErrorCode = 14; |
203 | ErrorCode = 14; |
202 | } |
204 | } |
203 | else if(FC.Error[0] & FC_ERROR0_ACC_TOP) |
205 | else if(FC.Error[0] & FC_ERROR0_ACC_TOP) |
204 | { |
206 | { |
205 | LED_RED_ON; |
207 | LED_RED_ON; |
206 | sprintf(ErrorMSG,"ERR: FC Z-ACC"); |
208 | sprintf(ErrorMSG,"ERR: FC Z-ACC"); |
207 | ErrorCode = 15; |
209 | ErrorCode = 15; |
208 | } |
210 | } |
209 | else if(FC.Error[0] & FC_ERROR0_PRESSURE) |
211 | else if(FC.Error[0] & FC_ERROR0_PRESSURE) |
210 | { |
212 | { |
211 | LED_RED_ON; |
213 | LED_RED_ON; |
212 | sprintf(ErrorMSG,"ERR: Pressure sensor"); |
214 | sprintf(ErrorMSG,"ERR: Pressure sensor"); |
213 | ErrorCode = 16; |
215 | ErrorCode = 16; |
214 | } |
216 | } |
215 | else if(FC.Error[1] & FC_ERROR1_I2C) |
217 | else if(FC.Error[1] & FC_ERROR1_I2C) |
216 | { |
218 | { |
217 | LED_RED_ON; |
219 | LED_RED_ON; |
218 | sprintf(ErrorMSG,"ERR: FC I2C"); |
220 | sprintf(ErrorMSG,"ERR: FC I2C"); |
219 | ErrorCode = 17; |
221 | ErrorCode = 17; |
220 | } |
222 | } |
221 | else if(FC.Error[1] & FC_ERROR1_BL_MISSING) |
223 | else if(FC.Error[1] & FC_ERROR1_BL_MISSING) |
222 | { |
224 | { |
223 | LED_RED_ON; |
225 | LED_RED_ON; |
224 | sprintf(ErrorMSG,"ERR: Bl Missing"); |
226 | sprintf(ErrorMSG,"ERR: Bl Missing"); |
225 | ErrorCode = 18; |
227 | ErrorCode = 18; |
226 | } |
228 | } |
227 | else if(FC.Error[1] & FC_ERROR1_MIXER) |
229 | else if(FC.Error[1] & FC_ERROR1_MIXER) |
228 | { |
230 | { |
229 | LED_RED_ON; |
231 | LED_RED_ON; |
230 | sprintf(ErrorMSG,"Mixer Error"); |
232 | sprintf(ErrorMSG,"Mixer Error"); |
231 | ErrorCode = 19; |
233 | ErrorCode = 19; |
232 | } |
234 | } |
233 | else if(CheckDelay(UBX_Timeout)) |
235 | else if(CheckDelay(UBX_Timeout)) |
234 | { |
236 | { |
235 | LED_RED_ON; |
237 | LED_RED_ON; |
236 | sprintf(ErrorMSG,"no GPS communication "); |
238 | sprintf(ErrorMSG,"no GPS communication "); |
237 | ErrorCode = 5; |
239 | ErrorCode = 5; |
238 | StopNavigation = 1; |
240 | StopNavigation = 1; |
239 | UART_VersionInfo.HardwareError[0] |= NC_ERROR0_GPS_RX; |
241 | UART_VersionInfo.HardwareError[0] |= NC_ERROR0_GPS_RX; |
240 | } |
242 | } |
241 | else if(Compass_Heading < 0) |
243 | else if(Compass_Heading < 0) |
242 | { |
244 | { |
243 | LED_RED_ON; |
245 | LED_RED_ON; |
244 | sprintf(ErrorMSG,"bad compass value "); |
246 | sprintf(ErrorMSG,"bad compass value "); |
245 | ErrorCode = 6; |
247 | ErrorCode = 6; |
246 | StopNavigation = 1; |
248 | StopNavigation = 1; |
247 | UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_VALUE; |
249 | UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_VALUE; |
248 | } |
250 | } |
249 | else if((FC.Error[1] & FC_ERROR1_SPI_RX)) |
251 | else if((FC.Error[1] & FC_ERROR1_SPI_RX)) |
250 | { |
252 | { |
251 | LED_RED_ON; |
253 | LED_RED_ON; |
252 | sprintf(ErrorMSG,"FC spi rx error "); |
254 | sprintf(ErrorMSG,"FC spi rx error "); |
253 | ErrorCode = 8; |
255 | ErrorCode = 8; |
254 | StopNavigation = 1; |
256 | StopNavigation = 1; |
255 | } |
257 | } |
256 | else if(FC.Error[0] & FC_ERROR0_CAREFREE) |
258 | else if(FC.Error[0] & FC_ERROR0_CAREFREE) |
257 | { |
259 | { |
258 | LED_RED_ON; |
260 | LED_RED_ON; |
259 | sprintf(ErrorMSG,"FC: Carefree Error"); |
261 | sprintf(ErrorMSG,"FC: Carefree Error"); |
260 | ErrorCode = 20; |
262 | ErrorCode = 20; |
261 | } |
263 | } |
262 | else if(FC.Error[1] & FC_ERROR1_PPM) |
264 | else if(FC.Error[1] & FC_ERROR1_PPM) |
263 | { |
265 | { |
264 | LED_RED_ON; |
266 | LED_RED_ON; |
265 | sprintf(ErrorMSG,"RC Signal lost "); |
267 | sprintf(ErrorMSG,"RC Signal lost "); |
266 | ErrorCode = 7; |
268 | ErrorCode = 7; |
267 | } |
269 | } |
268 | else // no error occured |
270 | else // no error occured |
269 | { |
271 | { |
270 | sprintf(ErrorMSG,"No Error "); |
272 | sprintf(ErrorMSG,"No Error "); |
271 | ErrorCode = 0; |
273 | ErrorCode = 0; |
272 | StopNavigation = 0; |
274 | StopNavigation = 0; |
273 | LED_RED_OFF; |
275 | LED_RED_OFF; |
274 | } |
276 | } |
275 | if(UART_VersionInfo.HardwareError[0] || UART_VersionInfo.HardwareError[1]) DebugOut.Status[1] |= 0x04; |
277 | if(UART_VersionInfo.HardwareError[0] || UART_VersionInfo.HardwareError[1]) DebugOut.Status[1] |= 0x04; |
276 | else DebugOut.Status[1] &= ~0x04; |
278 | else DebugOut.Status[1] &= ~0x04; |
277 | } |
279 | } |
278 | 280 | ||
279 | // the handler will be cyclic called by the timer 1 ISR |
281 | // the handler will be cyclic called by the timer 1 ISR |
280 | // used is for critical timing parts that normaly would handled |
282 | // used is for critical timing parts that normaly would handled |
281 | // within the main loop that could block longer at logging activities |
283 | // within the main loop that could block longer at logging activities |
282 | void EXTIT3_IRQHandler(void) |
284 | void EXTIT3_IRQHandler(void) |
283 | { |
285 | { |
284 | IENABLE; |
286 | IENABLE; |
285 | 287 | ||
286 | VIC_ITCmd(EXTIT3_ITLine,DISABLE); // disable irq |
288 | VIC_ITCmd(EXTIT3_ITLine,DISABLE); // disable irq |
287 | VIC_SWITCmd(EXTIT3_ITLine,DISABLE); // clear pending bit |
289 | VIC_SWITCmd(EXTIT3_ITLine,DISABLE); // clear pending bit |
288 | Compass_Update(); // update compass communication |
290 | Compass_Update(); // update compass communication |
289 | Analog_Update(); // get new ADC values |
291 | Analog_Update(); // get new ADC values |
290 | VIC_ITCmd(EXTIT3_ITLine, ENABLE); // enable irq |
292 | VIC_ITCmd(EXTIT3_ITLine, ENABLE); // enable irq |
291 | 293 | ||
292 | IDISABLE; |
294 | IDISABLE; |
293 | } |
295 | } |
294 | 296 | ||
295 | //---------------------------------------------------------------------------------------------------- |
297 | //---------------------------------------------------------------------------------------------------- |
296 | int main(void) |
298 | int main(void) |
297 | { |
299 | { |
298 | /* Configure the system clocks */ |
300 | /* Configure the system clocks */ |
299 | SCU_Config(); |
301 | SCU_Config(); |
300 | /* init VIC (Vectored Interrupt Controller) */ |
302 | /* init VIC (Vectored Interrupt Controller) */ |
301 | SCU_AHBPeriphClockConfig(__VIC,ENABLE); // enable AHB bus clock for VIC |
303 | SCU_AHBPeriphClockConfig(__VIC,ENABLE); // enable AHB bus clock for VIC |
302 | SCU_AHBPeriphReset(__VIC, DISABLE); // disable reset state for VIC |
304 | SCU_AHBPeriphReset(__VIC, DISABLE); // disable reset state for VIC |
303 | VIC_DeInit(); // deinitializes the VIC module registers to their default reset values. |
305 | VIC_DeInit(); // deinitializes the VIC module registers to their default reset values. |
304 | VIC_InitDefaultVectors(); |
306 | VIC_InitDefaultVectors(); |
305 | 307 | ||
306 | // initialize timer 1 for System Clock and delay rountines |
308 | // initialize timer 1 for System Clock and delay rountines |
307 | TIMER1_Init(); |
309 | TIMER1_Init(); |
308 | // initialize the LEDs (needs Timer 1) |
310 | // initialize the LEDs (needs Timer 1) |
309 | Led_Init(); |
311 | Led_Init(); |
310 | // initialize the debug UART1 |
312 | // initialize the debug UART1 |
311 | UART1_Init(); |
313 | UART1_Init(); |
312 | UART1_PutString("\r\n---------------------------------------------"); |
314 | UART1_PutString("\r\n---------------------------------------------"); |
313 | // initialize usb |
315 | // initialize usb |
314 | USB_ConfigInit(); |
316 | USB_ConfigInit(); |
315 | // initialize timer 2 for servo outputs |
317 | // initialize timer 2 for servo outputs |
316 | //TIMER2_Init(); |
318 | //TIMER2_Init(); |
317 | // initialize UART2 to FLIGHTCTRL |
319 | // initialize UART2 to FLIGHTCTRL |
318 | UART2_Init(); |
320 | UART2_Init(); |
319 | // initialize UART0 (to MKGPS or MK3MAG) |
321 | // initialize UART0 (to MKGPS or MK3MAG) |
320 | UART0_Init(); |
322 | UART0_Init(); |
321 | // initialize adc |
323 | // initialize adc |
322 | Analog_Init(); |
324 | Analog_Init(); |
323 | // initialize SPI0 to FC |
325 | // initialize SPI0 to FC |
324 | SPI0_Init(); |
326 | SPI0_Init(); |
325 | // initialize i2c bus (needs Timer 1) |
327 | // initialize i2c bus (needs Timer 1) |
326 | I2C1_Init(); |
328 | I2C1_Init(); |
327 | // initialize fat16 partition on sd card (needs Timer 1) |
329 | // initialize fat16 partition on sd card (needs Timer 1) |
328 | Fat16_Init(); |
330 | Fat16_Init(); |
329 | // initialize NC params |
331 | // initialize NC params |
330 | NCParams_Init(); |
332 | NCParams_Init(); |
331 | // initialize the settings |
333 | // initialize the settings |
332 | Settings_Init(); |
334 | Settings_Init(); |
333 | // initialize logging (needs settings) |
335 | // initialize logging (needs settings) |
334 | Logging_Init(); |
336 | Logging_Init(); |
335 | 337 | ||
336 | TimerCheckError = SetDelay(3000); |
338 | TimerCheckError = SetDelay(3000); |
337 | UART1_PutString("\r\n++++++++++++++++++++++++++++++++++++++++++"); |
339 | UART1_PutString("\r\n++++++++++++++++++++++++++++++++++++++++++"); |
338 | UART1_PutString("\n\r Version information:"); |
340 | UART1_PutString("\n\r Version information:"); |
339 | 341 | ||
340 | GetNaviCtrlVersion(); |
342 | GetNaviCtrlVersion(); |
341 | DebugOut.Status[0] = 0x04 | 0x08; // NC and MK3Mag |
343 | DebugOut.Status[0] = 0x04 | 0x08; // NC and MK3Mag |
342 | DebugOut.Status[1] = 0x00; |
344 | DebugOut.Status[1] = 0x00; |
343 | 345 | ||
344 | Compass_Init(); |
346 | Compass_Init(); |
- | 347 | ||
- | 348 | #ifdef FOLLOW_ME |
|
- | 349 | TransmitAlsoToFC = 1; |
|
- | 350 | UART1_PutString("\r\n++++++++++++++++++++++++++++++++++++++++++"); |
|
- | 351 | UART1_PutString("\n\r FOLLOW-ME Transmitter only!"); |
|
- | 352 | UART1_PutString("\r\n++++++++++++++++++++++++++++++++++++++++++\r\n\r\n"); |
|
- | 353 | TransmitAlsoToFC = 0; |
|
345 | 354 | #else |
|
346 | SPI0_GetFlightCtrlVersion(); |
355 | SPI0_GetFlightCtrlVersion(); |
347 | if(FC_Version.Compatible != FC_SPI_COMPATIBLE) |
356 | if(FC_Version.Compatible != FC_SPI_COMPATIBLE) |
348 | { |
357 | { |
349 | UART1_PutString("\n\r Flight-Ctrl not compatible"); |
358 | UART1_PutString("\n\r Flight-Ctrl not compatible"); |
350 | LED_RED_ON; |
359 | LED_RED_ON; |
351 | } |
360 | } |
- | 361 | #endif |
|
352 | 362 | ||
353 | GPS_Init(); |
363 | GPS_Init(); |
354 | 364 | ||
355 | // ---------- Prepare the isr driven |
365 | // ---------- Prepare the isr driven |
356 | // set to absolute lowest priority |
366 | // set to absolute lowest priority |
357 | VIC_Config(EXTIT3_ITLine, VIC_IRQ, PRIORITY_SW); |
367 | VIC_Config(EXTIT3_ITLine, VIC_IRQ, PRIORITY_SW); |
358 | // enable interrupts |
368 | // enable interrupts |
359 | VIC_ITCmd(EXTIT3_ITLine, ENABLE); |
369 | VIC_ITCmd(EXTIT3_ITLine, ENABLE); |
360 | 370 | ||
361 | Debug_OK("START"); |
371 | Debug_OK("START"); |
362 | 372 | ||
363 | for (;;) // the endless main loop |
373 | for (;;) // the endless main loop |
364 | { |
374 | { |
365 | UART0_ProcessRxData(); // process request |
375 | UART0_ProcessRxData(); // process request |
366 | UART1_ProcessRxData(); // process request |
376 | UART1_ProcessRxData(); // process request |
367 | USB_ProcessRxData(); // process request |
377 | USB_ProcessRxData(); // process request |
368 | UART0_TransmitTxData(); // send answer |
378 | UART0_TransmitTxData(); // send answer |
369 | UART1_TransmitTxData(); // send answer |
379 | UART1_TransmitTxData(); // send answer |
370 | UART2_TransmitTxData(); // send answer |
380 | UART2_TransmitTxData(); // send answer |
371 | USB_TransmitTxData(); // send answer |
381 | USB_TransmitTxData(); // send answer |
372 | SPI0_UpdateBuffer(); // handle new SPI Data |
382 | SPI0_UpdateBuffer(); // handle new SPI Data |
373 | // ---------------- Error Check Timing ---------------------------- |
383 | // ---------------- Error Check Timing ---------------------------- |
374 | if(CheckDelay(TimerCheckError)) |
384 | if(CheckDelay(TimerCheckError)) |
375 | { |
385 | { |
376 | TimerCheckError = SetDelay(1000); |
386 | TimerCheckError = SetDelay(1000); |
377 | if(CheckDelay(SPI0_Timeout)) GPS_Navigation(&GPSData, &(ToFlightCtrl.GPSStick)); // process the GPS data even if the FC is not connected |
387 | if(CheckDelay(SPI0_Timeout)) GPS_Navigation(&GPSData, &(ToFlightCtrl.GPSStick)); // process the GPS data even if the FC is not connected |
378 | CheckErrors(); |
388 | CheckErrors(); |
379 | if(FC.StatusFlags & FC_STATUS_FLY) NaviData.FlyingTime++; // we want to count the battery-time |
389 | if(FC.StatusFlags & FC_STATUS_FLY) NaviData.FlyingTime++; // we want to count the battery-time |
380 | // else NaviData.FlyingTime = 0; // not the time per flight |
390 | // else NaviData.FlyingTime = 0; // not the time per flight |
381 | if(SerialLinkOkay) SerialLinkOkay--; |
391 | if(SerialLinkOkay) SerialLinkOkay--; |
382 | if(SerialLinkOkay < 250 - 5) NCFlags |= NC_FLAG_NOSERIALLINK; // 5 seconds timeout for serial communication |
392 | if(SerialLinkOkay < 250 - 5) NCFlags |= NC_FLAG_NOSERIALLINK; // 5 seconds timeout for serial communication |
383 | else NCFlags &= ~NC_FLAG_NOSERIALLINK; |
393 | else NCFlags &= ~NC_FLAG_NOSERIALLINK; |
384 | if(StopNavigation && (Parameter.NaviGpsModeControl >= 50)) BeepTime = 1000; |
394 | if(StopNavigation && (Parameter.NaviGpsModeControl >= 50)) BeepTime = 1000; |
385 | } |
395 | } |
386 | // ---------------- Logging --------------------------------------- |
396 | // ---------------- Logging --------------------------------------- |
387 | 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 |
397 | 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 |
388 | } |
398 | } |
389 | } |
399 | } |
390 | 400 | ||
391 | 401 | ||
392 | 402 |