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