Rev 432 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | ingob | 1 | /*#######################################################################################*/ |
2 | /* !!! THIS IS NOT FREE SOFTWARE !!! */ |
||
3 | /*#######################################################################################*/ |
||
4 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
5 | // + www.MikroKopter.com |
||
6 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
360 | holgerb | 7 | // + Software Nutzungsbedingungen (english version: see below) |
8 | // + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt - |
||
9 | // + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den |
||
10 | // + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool |
||
11 | // + - nachfolgend Software genannt - nur für private Zwecke zu nutzen. |
||
12 | // + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig. |
||
1 | ingob | 13 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
360 | holgerb | 14 | // + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im |
15 | // + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu. |
||
16 | // + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie |
||
17 | // + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden. |
||
18 | // + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren |
||
19 | // + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt. |
||
20 | // + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren |
||
21 | // + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand |
||
22 | // + des Mitverschuldens offen. |
||
23 | // + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet. |
||
24 | // + Er wird die Software gründlich auf deren Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt. |
||
25 | // + Der Kunde wird er seine Daten vor Einsatz der Software nach dem Stand der Technik sichern. |
||
26 | // + Der Kunde ist darüber unterrichtet, dass der Lizenzgeber seine Daten im zur Vertragsdurchführung erforderlichen Umfang |
||
27 | // + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt. |
||
28 | // + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software. |
||
29 | // + #### ENDE DER NUTZUNGSBEDINGUNGEN ####' |
||
30 | // + Hinweis: Informationen über erweiterte Nutzungsrechte (wie z.B. Nutzung für nicht-private Zwecke) sind auf Anfrage per Email an info(@)hisystems.de verfügbar. |
||
1 | ingob | 31 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
360 | holgerb | 32 | // + Software LICENSING TERMS |
1 | ingob | 33 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
360 | holgerb | 34 | // + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor - |
35 | // + The Licensor grants the customer a non-exclusive license to use the microcontroller firmware of the Flight-Ctrl, Navi-Ctrl, BL-Ctrl, and MK3Mag hardware |
||
36 | // + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*. |
||
37 | // + The Software may only be used with the Licensor's products. |
||
38 | // + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this |
||
39 | // + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this |
||
40 | // + agreement shall be the property of the Licensor. |
||
41 | // + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other |
||
42 | // + features that can be used to identify the program may not be altered or defaced by the customer. |
||
43 | // + The customer shall be responsible for taking reasonable precautions |
||
44 | // + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the |
||
45 | // + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and |
||
46 | // + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product |
||
47 | // + liability. However, the Licensor shall be entitled to the defense of contributory negligence. |
||
48 | // + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test |
||
49 | // + the software for his purpose before any operational usage. The customer will backup his data before using the software. |
||
50 | // + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data |
||
51 | // + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations. |
||
52 | // + *) The territory aspect only refers to the place where the Software is used, not its programmed range. |
||
53 | // + #### END OF LICENSING TERMS #### |
||
54 | // + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de. |
||
1 | ingob | 55 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
41 | ingob | 56 | //#define MCLK96MHZ |
1 | ingob | 57 | const unsigned long _Main_Crystal = 25000; |
41 | ingob | 58 | #include <stdio.h> |
59 | #include "91x_lib.h" |
||
60 | #include "led.h" |
||
61 | #include "uart0.h" |
||
62 | #include "uart1.h" |
||
63 | #include "uart2.h" |
||
215 | killagreg | 64 | #include "gps.h" |
41 | ingob | 65 | #include "i2c.h" |
242 | killagreg | 66 | #include "compass.h" |
426 | holgerb | 67 | #include "ncmag.h" |
119 | killagreg | 68 | #include "timer1.h" |
69 | #include "timer2.h" |
||
134 | killagreg | 70 | #include "analog.h" |
41 | ingob | 71 | #include "spi_slave.h" |
72 | #include "fat16.h" |
||
73 | #include "usb.h" |
||
74 | #include "sdc.h" |
||
82 | killagreg | 75 | #include "logging.h" |
156 | killagreg | 76 | #include "params.h" |
88 | killagreg | 77 | #include "settings.h" |
154 | killagreg | 78 | #include "config.h" |
1 | ingob | 79 | #include "main.h" |
250 | ingob | 80 | #include "debug.h" |
254 | killagreg | 81 | #include "eeprom.h" |
350 | holgerb | 82 | #include "ssc.h" |
362 | holgerb | 83 | #include "sdc.h" |
380 | holgerb | 84 | #include "uart1.h" |
254 | killagreg | 85 | |
427 | killagreg | 86 | |
87 | |||
314 | killagreg | 88 | #ifdef FOLLOW_ME |
89 | u8 TransmitAlsoToFC = 0; |
||
90 | #endif |
||
41 | ingob | 91 | u32 TimerCheckError; |
427 | killagreg | 92 | u8 ErrorCode = 0; |
41 | ingob | 93 | u16 BeepTime; |
92 | killagreg | 94 | u8 NCFlags = 0; |
153 | killagreg | 95 | s16 GeoMagDec = 0; // local magnetic declination in 0.1 deg |
328 | holgerb | 96 | u8 ErrorGpsFixLost = 0; |
1 | ingob | 97 | |
255 | killagreg | 98 | u8 ClearFCStatusFlags = 0; |
41 | ingob | 99 | u8 StopNavigation = 0; |
378 | holgerb | 100 | volatile u32 PollingTimeout = 10000; |
41 | ingob | 101 | Param_t Parameter; |
78 | holgerb | 102 | volatile FC_t FC; |
378 | holgerb | 103 | volatile u32 SPIWatchDog = 15000; // stop Navigation if this goes to zero |
400 | holgerb | 104 | volatile u32 SD_WatchDog = 15000; // stop Logging if this goes to zero |
1 | ingob | 105 | |
41 | ingob | 106 | s8 ErrorMSG[25]; |
107 | |||
1 | ingob | 108 | //---------------------------------------------------------------------------------------------------- |
41 | ingob | 109 | void SCU_Config(void) |
1 | ingob | 110 | { |
41 | ingob | 111 | /* configure PLL and set it as master clock source */ |
112 | SCU_MCLKSourceConfig(SCU_MCLK_OSC); // set master clock source to external oscillator clock (25MHz) before diabling the PLL |
||
113 | SCU_PLLCmd(DISABLE); // now disable the PLL |
||
114 | #ifdef MCLK96MHZ |
||
115 | SCU_BRCLKDivisorConfig(SCU_BRCLK_Div2); // set BRCLK to MCLK/2 = 48MHz |
||
116 | SCU_PCLKDivisorConfig(SCU_PCLK_Div4); // set PCLK (APB bus clock) divisor to 4 (half Reference Clock) |
||
117 | SCU_RCLKDivisorConfig(SCU_RCLK_Div2); // set RCLK (Reference Clock) divisor to 1 (full PPL clock) |
||
118 | SCU_HCLKDivisorConfig(SCU_HCLK_Div2); // set HCLK (AHB bus clock) divisor to 1 (full Reference Clock) |
||
119 | SCU_PLLFactorsConfig(192,25,2); // PLL = 96 MHz, Feedback Divider N=192, Pre-Divider M=25, Post-Divider P=2 |
||
120 | #else |
||
121 | SCU_BRCLKDivisorConfig(SCU_BRCLK_Div1); // set BRCLK to MCLK = 48MHz |
||
122 | SCU_PCLKDivisorConfig(SCU_PCLK_Div2); // set PCLK (APB bus clock) divisor to 2 (half Reference Clock) |
||
123 | SCU_RCLKDivisorConfig(SCU_RCLK_Div1); // set RCLK (Reference Clock) divisor to 1 (full PPL clock) |
||
124 | SCU_HCLKDivisorConfig(SCU_HCLK_Div1); // set HCLK (AHB bus clock) divisor to 1 (full Reference Clock) |
||
125 | SCU_PLLFactorsConfig(192,25,3); // PLL = 48 MHz, Feedback Divider N=192, Pre-Divider M=25, Post-Divider P=3 |
||
126 | #endif |
||
127 | SCU_PLLCmd(ENABLE); // Enable PLL (is disabled by SCU_PLLFactorsConfig) |
||
128 | SCU_MCLKSourceConfig(SCU_MCLK_PLL); // set master clock source to PLL |
||
129 | } |
||
1 | ingob | 130 | |
41 | ingob | 131 | //---------------------------------------------------------------------------------------------------- |
132 | void GetNaviCtrlVersion(void) |
||
1 | ingob | 133 | { |
41 | ingob | 134 | u8 msg[25]; |
270 | killagreg | 135 | |
264 | killagreg | 136 | 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); |
110 | killagreg | 137 | UART1_PutString(msg); |
1 | ingob | 138 | } |
139 | |||
140 | //---------------------------------------------------------------------------------------------------- |
||
231 | holgerb | 141 | |
41 | ingob | 142 | void CheckErrors(void) |
143 | { |
||
329 | holgerb | 144 | static s32 no_error_delay = 0; |
328 | holgerb | 145 | s32 newErrorCode = 0; |
232 | killagreg | 146 | UART_VersionInfo.HardwareError[0] = 0; |
231 | holgerb | 147 | |
338 | holgerb | 148 | if(CheckDelay(I2C1_Timeout) || (Compass_Heading < 0)) DebugOut.StatusRed |= AMPEL_COMPASS; |
149 | else DebugOut.StatusRed &= ~AMPEL_COMPASS; // MK3Mag green status |
||
231 | holgerb | 150 | |
338 | holgerb | 151 | if((FC.Error[1] & FC_ERROR1_I2C) || (FC.Error[1] & FC_ERROR1_BL_MISSING)) DebugOut.StatusRed |= AMPEL_BL; |
152 | else DebugOut.StatusRed &= ~AMPEL_BL; // BL-Ctrl green status |
||
232 | killagreg | 153 | |
338 | holgerb | 154 | if(UART_VersionInfo.HardwareError[0] || UART_VersionInfo.HardwareError[1]) DebugOut.StatusRed |= AMPEL_NC; |
155 | else DebugOut.StatusRed &= ~AMPEL_NC; |
||
156 | |||
397 | holgerb | 157 | if((FCCalibActive || CompassCalState) && FC_Version.Hardware) |
392 | holgerb | 158 | { |
159 | sprintf(ErrorMSG,"Calibrate... "); |
||
160 | newErrorCode = 0; |
||
161 | ErrorCode = 0; |
||
162 | no_error_delay = 1; |
||
163 | } |
||
164 | else |
||
146 | killagreg | 165 | if(CheckDelay(SPI0_Timeout)) |
41 | ingob | 166 | { |
380 | holgerb | 167 | LED_RED_ON; |
41 | ingob | 168 | sprintf(ErrorMSG,"no FC communication "); |
328 | holgerb | 169 | newErrorCode = 3; |
41 | ingob | 170 | StopNavigation = 1; |
338 | holgerb | 171 | DebugOut.StatusGreen &= ~AMPEL_FC; // status of FC Present |
172 | DebugOut.StatusGreen &= ~AMPEL_BL; // status of BL Present |
||
256 | killagreg | 173 | UART_VersionInfo.HardwareError[0] |= NC_ERROR0_SPI_RX; |
41 | ingob | 174 | } |
229 | holgerb | 175 | else if(CheckDelay(I2C1_Timeout)) |
41 | ingob | 176 | { |
368 | holgerb | 177 | LED_RED_ON; |
380 | holgerb | 178 | sprintf(ErrorMSG,"no compass communica"); |
149 | killagreg | 179 | //Reset I2CBus |
180 | I2C1_Deinit(); |
||
181 | I2C1_Init(); |
||
328 | holgerb | 182 | newErrorCode = 4; |
41 | ingob | 183 | StopNavigation = 1; |
256 | killagreg | 184 | UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_RX; |
338 | holgerb | 185 | DebugOut.StatusRed |= AMPEL_COMPASS; |
41 | ingob | 186 | } |
187 | else if(FC_Version.Compatible != FC_SPI_COMPATIBLE) |
||
188 | { |
||
189 | LED_RED_ON; |
||
318 | holgerb | 190 | #ifndef FOLLOW_ME |
41 | ingob | 191 | sprintf(ErrorMSG,"FC not compatible "); |
318 | holgerb | 192 | #else |
193 | sprintf(ErrorMSG,"! FollowMe only ! "); |
||
194 | #endif |
||
328 | holgerb | 195 | newErrorCode = 1; |
41 | ingob | 196 | StopNavigation = 1; |
256 | killagreg | 197 | UART_VersionInfo.HardwareError[0] |= NC_ERROR0_FC_INCOMPATIBLE; |
338 | holgerb | 198 | DebugOut.StatusRed |= AMPEL_NC; |
41 | ingob | 199 | } |
231 | holgerb | 200 | |
255 | killagreg | 201 | else if(FC.Error[0] & FC_ERROR0_GYRO_NICK) |
231 | holgerb | 202 | { |
203 | LED_RED_ON; |
||
204 | sprintf(ErrorMSG,"ERR: FC Nick Gyro"); |
||
328 | holgerb | 205 | newErrorCode = 10; |
232 | killagreg | 206 | } |
255 | killagreg | 207 | else if(FC.Error[0] & FC_ERROR0_GYRO_ROLL) |
231 | holgerb | 208 | { |
209 | LED_RED_ON; |
||
210 | sprintf(ErrorMSG,"ERR: FC Roll Gyro"); |
||
328 | holgerb | 211 | newErrorCode = 11; |
232 | killagreg | 212 | } |
255 | killagreg | 213 | else if(FC.Error[0] & FC_ERROR0_GYRO_YAW) |
231 | holgerb | 214 | { |
215 | LED_RED_ON; |
||
216 | sprintf(ErrorMSG,"ERR: FC Yaw Gyro"); |
||
328 | holgerb | 217 | newErrorCode = 12; |
232 | killagreg | 218 | } |
255 | killagreg | 219 | else if(FC.Error[0] & FC_ERROR0_ACC_NICK) |
231 | holgerb | 220 | { |
221 | LED_RED_ON; |
||
222 | sprintf(ErrorMSG,"ERR: FC Nick ACC"); |
||
328 | holgerb | 223 | newErrorCode = 13; |
232 | killagreg | 224 | } |
255 | killagreg | 225 | else if(FC.Error[0] & FC_ERROR0_ACC_ROLL) |
231 | holgerb | 226 | { |
227 | LED_RED_ON; |
||
228 | sprintf(ErrorMSG,"ERR: FC Roll ACC"); |
||
328 | holgerb | 229 | newErrorCode = 14; |
232 | killagreg | 230 | } |
255 | killagreg | 231 | else if(FC.Error[0] & FC_ERROR0_ACC_TOP) |
231 | holgerb | 232 | { |
233 | LED_RED_ON; |
||
327 | holgerb | 234 | sprintf(ErrorMSG,"ERR:FC Z-ACC"); |
328 | holgerb | 235 | newErrorCode = 15; |
232 | killagreg | 236 | } |
351 | holgerb | 237 | else if(NC_To_FC_Flags & NC_TO_FC_FLYING_RANGE) |
238 | { |
||
239 | LED_RED_ON; |
||
240 | sprintf(ErrorMSG,"ERR:Flying range!"); |
||
241 | newErrorCode = 28; |
||
242 | } |
||
255 | killagreg | 243 | else if(FC.Error[0] & FC_ERROR0_PRESSURE) |
231 | holgerb | 244 | { |
380 | holgerb | 245 | LED_RED_ON; |
327 | holgerb | 246 | sprintf(ErrorMSG,"ERR:Pressure sensor"); |
328 | holgerb | 247 | newErrorCode = 16; |
232 | killagreg | 248 | } |
255 | killagreg | 249 | else if(FC.Error[1] & FC_ERROR1_I2C) |
231 | holgerb | 250 | { |
251 | LED_RED_ON; |
||
327 | holgerb | 252 | sprintf(ErrorMSG,"ERR:I2C FC to BL"); |
328 | holgerb | 253 | newErrorCode = 17; |
232 | killagreg | 254 | } |
255 | killagreg | 255 | else if(FC.Error[1] & FC_ERROR1_BL_MISSING) |
231 | holgerb | 256 | { |
257 | LED_RED_ON; |
||
258 | sprintf(ErrorMSG,"ERR: Bl Missing"); |
||
328 | holgerb | 259 | newErrorCode = 18; |
232 | killagreg | 260 | } |
255 | killagreg | 261 | else if(FC.Error[1] & FC_ERROR1_MIXER) |
231 | holgerb | 262 | { |
263 | LED_RED_ON; |
||
264 | sprintf(ErrorMSG,"Mixer Error"); |
||
328 | holgerb | 265 | newErrorCode = 19; |
232 | killagreg | 266 | } |
350 | holgerb | 267 | else if(CheckDelay(UBX_Timeout) && Parameter.GlobalConfig & FC_CFG_GPS_AKTIV) |
41 | ingob | 268 | { |
269 | LED_RED_ON; |
||
350 | holgerb | 270 | // if(!(Parameter.GlobalConfig & FC_CFG_GPS_AKTIV)) sprintf(ErrorMSG,"GPS disconnected "); |
271 | // else |
||
380 | holgerb | 272 | { |
273 | sprintf(ErrorMSG,"no GPS communication"); |
||
320 | holgerb | 274 | UART_VersionInfo.HardwareError[0] |= NC_ERROR0_GPS_RX; |
328 | holgerb | 275 | newErrorCode = 5; |
320 | holgerb | 276 | } |
41 | ingob | 277 | StopNavigation = 1; |
320 | holgerb | 278 | // UBX_Timeout = SetDelay(500); |
41 | ingob | 279 | } |
426 | holgerb | 280 | else if(Compass_Heading < 0 && NCMAG_Present && !NCMAG_IsCalibrated) |
281 | { |
||
282 | LED_RED_ON; |
||
283 | sprintf(ErrorMSG,"compass not calibr."); |
||
284 | newErrorCode = 31; |
||
285 | StopNavigation = 1; |
||
286 | UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_VALUE; |
||
287 | } |
||
254 | killagreg | 288 | else if(Compass_Heading < 0) |
41 | ingob | 289 | { |
290 | LED_RED_ON; |
||
291 | sprintf(ErrorMSG,"bad compass value "); |
||
328 | holgerb | 292 | newErrorCode = 6; |
41 | ingob | 293 | StopNavigation = 1; |
256 | killagreg | 294 | UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_VALUE; |
41 | ingob | 295 | } |
255 | killagreg | 296 | else if((FC.Error[1] & FC_ERROR1_SPI_RX)) |
199 | killagreg | 297 | { |
298 | LED_RED_ON; |
||
299 | sprintf(ErrorMSG,"FC spi rx error "); |
||
328 | holgerb | 300 | newErrorCode = 8; |
213 | killagreg | 301 | StopNavigation = 1; |
199 | killagreg | 302 | } |
255 | killagreg | 303 | else if(FC.Error[0] & FC_ERROR0_CAREFREE) |
231 | holgerb | 304 | { |
305 | LED_RED_ON; |
||
306 | sprintf(ErrorMSG,"FC: Carefree Error"); |
||
328 | holgerb | 307 | newErrorCode = 20; |
232 | killagreg | 308 | } |
255 | killagreg | 309 | else if(FC.Error[1] & FC_ERROR1_PPM) |
41 | ingob | 310 | { |
311 | LED_RED_ON; |
||
312 | sprintf(ErrorMSG,"RC Signal lost "); |
||
328 | holgerb | 313 | newErrorCode = 7; |
41 | ingob | 314 | } |
328 | holgerb | 315 | else if(ErrorGpsFixLost) |
316 | { |
||
317 | LED_RED_ON; |
||
318 | sprintf(ErrorMSG,"GPS Fix lost "); |
||
319 | newErrorCode = 21; |
||
320 | } |
||
330 | holgerb | 321 | else if(ErrorDisturbedEarthMagnetField) |
322 | { |
||
323 | LED_RED_ON; |
||
324 | sprintf(ErrorMSG,"Magnet error "); |
||
325 | newErrorCode = 22; |
||
338 | holgerb | 326 | DebugOut.StatusRed |= AMPEL_COMPASS | AMPEL_NC; |
327 | UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_VALUE; |
||
330 | holgerb | 328 | } |
338 | holgerb | 329 | else if(BL_MinOfMaxPWM == 40 && (FC.StatusFlags & FC_STATUS_FLY) && !ErrorCode) |
330 | { |
||
331 | LED_RED_ON; |
||
332 | sprintf(ErrorMSG,"ERR:Motor restart "); |
||
333 | newErrorCode = 23; |
||
334 | DebugOut.StatusRed |= AMPEL_BL; |
||
335 | } |
||
432 | holgerb | 336 | else if(BL_MinOfMaxPWM != 255 && (BL_MinOfMaxPWM != 250) && (FC.StatusFlags & FC_STATUS_FLY) && !ErrorCode) |
338 | holgerb | 337 | { |
338 | LED_RED_ON; |
||
339 | sprintf(ErrorMSG,"ERR:BL Limitation "); |
||
340 | newErrorCode = 24; |
||
341 | DebugOut.StatusRed |= AMPEL_BL; |
||
342 | } |
||
348 | holgerb | 343 | else if(NCFlags & NC_FLAG_RANGE_LIMIT && (FC.StatusFlags & FC_STATUS_FLY) && !ErrorCode) |
344 | { |
||
345 | LED_RED_ON; |
||
351 | holgerb | 346 | sprintf(ErrorMSG,"ERR:GPS range "); |
348 | holgerb | 347 | newErrorCode = 25; |
348 | DebugOut.StatusRed |= AMPEL_NC; |
||
349 | } |
||
397 | holgerb | 350 | else if((!SD_SWITCH || (SDCardInfo.Valid == 0)) && Parameter.GlobalConfig3 & CFG3_NO_SDCARD_NO_START && !(FC.StatusFlags & FC_STATUS_FLY)) |
350 | holgerb | 351 | { |
352 | LED_RED_ON; |
||
353 | sprintf(ErrorMSG,"ERR:No SD-Card "); |
||
354 | newErrorCode = 26; |
||
355 | DebugOut.StatusRed |= AMPEL_NC; |
||
356 | } |
||
383 | holgerb | 357 | else if((SD_LoggingError || (SD_WatchDog < 2000 && SD_WatchDog != 0)) && Parameter.GlobalConfig3 & CFG3_NO_SDCARD_NO_START) |
351 | holgerb | 358 | { |
368 | holgerb | 359 | LED_RED_ON; |
380 | holgerb | 360 | sprintf(ErrorMSG,"ERR:SD Logging abort"); |
351 | holgerb | 361 | newErrorCode = 27; |
362 | DebugOut.StatusRed |= AMPEL_NC; |
||
363 | SD_LoggingError = 0; |
||
364 | } |
||
355 | holgerb | 365 | else if(((AbsoluteFlyingAltitude) && (NaviData.Altimeter / 20 >= AbsoluteFlyingAltitude)) && (FC.StatusFlags & FC_STATUS_FLY)) |
366 | { |
||
367 | LED_RED_ON; |
||
368 | sprintf(ErrorMSG,"ERR:Max Altitude "); |
||
369 | newErrorCode = 29; |
||
370 | DebugOut.StatusRed |= AMPEL_NC; |
||
371 | } |
||
420 | holgerb | 372 | else if(Parameter.GlobalConfig3 & CFG3_NO_GPSFIX_NO_START && !(NCFlags & NC_FLAG_GPS_OK) && ((FC.StatusFlags & (FC_STATUS_START | FC_STATUS_MOTOR_RUN)) || (FC.StickGas < -50 && FC.StickYaw < -50))) |
398 | holgerb | 373 | { |
374 | LED_RED_ON; |
||
375 | sprintf(ErrorMSG,"No GPS Fix "); |
||
376 | newErrorCode = 30; |
||
377 | } |
||
41 | ingob | 378 | else // no error occured |
379 | { |
||
380 | StopNavigation = 0; |
||
381 | LED_RED_OFF; |
||
328 | holgerb | 382 | if(no_error_delay) { no_error_delay--; } |
383 | else |
||
380 | holgerb | 384 | { |
385 | sprintf(ErrorMSG,"No Error "); |
||
328 | holgerb | 386 | ErrorCode = 0; |
387 | } |
||
41 | ingob | 388 | } |
328 | holgerb | 389 | |
390 | if(newErrorCode) |
||
391 | { |
||
339 | holgerb | 392 | if(FC.StatusFlags & FC_STATUS_MOTOR_RUN) no_error_delay = 8; // delay the errors if the motors are running |
328 | holgerb | 393 | ErrorCode = newErrorCode; |
394 | } |
||
395 | FC.Error[0] = 0; |
||
396 | FC.Error[1] = 0; |
||
397 | FC.Error[2] = 0; |
||
398 | FC.Error[3] = 0; |
||
399 | FC.Error[4] = 0; |
||
400 | ErrorGpsFixLost = 0; |
||
41 | ingob | 401 | } |
24 | StephanB | 402 | |
378 | holgerb | 403 | |
404 | |||
405 | void Polling(void) |
||
406 | { |
||
398 | holgerb | 407 | static u8 running = 0, oldFcFlags = 0; |
378 | holgerb | 408 | if(running) return; |
409 | running = 1; |
||
410 | SPI0_UpdateBuffer(); // also calls the GPS-functions |
||
411 | UART0_ProcessRxData(); // GPS process request |
||
412 | UART0_TransmitTxData(); // GPS send answer |
||
380 | holgerb | 413 | UART1_ProcessRxData(); // PC process request |
378 | holgerb | 414 | UART1_TransmitTxData(); // PC send answer |
415 | UART2_TransmitTxData(); // FC send answer |
||
416 | CalcHeadFree(); |
||
380 | holgerb | 417 | // ---------------- Error Check Timing ---------------------------- |
398 | holgerb | 418 | if(CheckDelay(TimerCheckError) || (FC.StatusFlags & FC_STATUS_START && !(oldFcFlags & FC_STATUS_START))) // Timer or FY wants to start |
380 | holgerb | 419 | { |
420 | TimerCheckError = SetDelay(1000); |
||
398 | holgerb | 421 | oldFcFlags = FC.StatusFlags; |
380 | holgerb | 422 | if(CheckDelay(SPI0_Timeout) && (DebugUART == UART1)) GPS_Navigation(&GPSData, &(ToFlightCtrl.GPSStick)); // process the GPS data even if the FC is not connected |
423 | |||
424 | if(!CheckDelay(SPI0_Timeout) || (DebugUART == UART1)) CheckErrors(); |
||
425 | |||
426 | if(FC.StatusFlags & FC_STATUS_FLY) NaviData.FlyingTime++; // we want to count the battery-time |
||
427 | // else NaviData.FlyingTime = 0; // not the time per flight |
||
428 | if(SerialLinkOkay) SerialLinkOkay--; |
||
429 | if(SerialLinkOkay < 250 - 5) NCFlags |= NC_FLAG_NOSERIALLINK; // 5 seconds timeout for serial communication |
||
430 | else NCFlags &= ~NC_FLAG_NOSERIALLINK; |
||
431 | if(StopNavigation && (Parameter.NaviGpsModeControl >= 50) && (Parameter.GlobalConfig & FC_CFG_GPS_AKTIV)) BeepTime = 1000; |
||
432 | } |
||
378 | holgerb | 433 | running = 0; |
434 | } |
||
435 | |||
380 | holgerb | 436 | // the handler will be cyclic called by the timer 1 ISR |
437 | // used is for critical timing parts that normaly would handled |
||
438 | // within the main loop that could block longer at logging activities |
||
154 | killagreg | 439 | void EXTIT3_IRQHandler(void) |
440 | { |
||
195 | killagreg | 441 | IENABLE; |
154 | killagreg | 442 | VIC_ITCmd(EXTIT3_ITLine,DISABLE); // disable irq |
292 | killagreg | 443 | Compass_Update(); // update compass communication |
154 | killagreg | 444 | Analog_Update(); // get new ADC values |
378 | holgerb | 445 | |
380 | holgerb | 446 | if(!PollingTimeout) |
447 | { |
||
448 | PollingTimeout = 5; |
||
449 | Polling(); |
||
450 | } |
||
378 | holgerb | 451 | |
452 | VIC_SWITCmd(EXTIT3_ITLine,DISABLE); // clear pending bit |
||
195 | killagreg | 453 | VIC_ITCmd(EXTIT3_ITLine, ENABLE); // enable irq |
454 | IDISABLE; |
||
378 | holgerb | 455 | VIC1->VAR = 0xFF; // write any value to VIC0 Vector address register |
154 | killagreg | 456 | } |
457 | |||
41 | ingob | 458 | //---------------------------------------------------------------------------------------------------- |
459 | int main(void) |
||
460 | { |
||
431 | killagreg | 461 | |
324 | killagreg | 462 | static u32 ftimer =0; |
463 | static u8 fstate = 0; |
||
464 | static File_t* f = NULL; |
||
465 | |||
431 | killagreg | 466 | |
41 | ingob | 467 | /* Configure the system clocks */ |
468 | SCU_Config(); |
||
469 | /* init VIC (Vectored Interrupt Controller) */ |
||
470 | SCU_AHBPeriphClockConfig(__VIC,ENABLE); // enable AHB bus clock for VIC |
||
471 | SCU_AHBPeriphReset(__VIC, DISABLE); // disable reset state for VIC |
||
472 | VIC_DeInit(); // deinitializes the VIC module registers to their default reset values. |
||
196 | killagreg | 473 | VIC_InitDefaultVectors(); |
474 | |||
119 | killagreg | 475 | // initialize timer 1 for System Clock and delay rountines |
41 | ingob | 476 | TIMER1_Init(); |
477 | // initialize the LEDs (needs Timer 1) |
||
478 | Led_Init(); |
||
479 | // initialize the debug UART1 |
||
480 | UART1_Init(); |
||
110 | killagreg | 481 | UART1_PutString("\r\n---------------------------------------------"); |
275 | killagreg | 482 | // initialize usb |
483 | USB_ConfigInit(); |
||
120 | killagreg | 484 | // initialize timer 2 for servo outputs |
180 | killagreg | 485 | //TIMER2_Init(); |
41 | ingob | 486 | // initialize UART2 to FLIGHTCTRL |
487 | UART2_Init(); |
||
488 | // initialize UART0 (to MKGPS or MK3MAG) |
||
489 | UART0_Init(); |
||
134 | killagreg | 490 | // initialize adc |
491 | Analog_Init(); |
||
41 | ingob | 492 | // initialize SPI0 to FC |
493 | SPI0_Init(); |
||
242 | killagreg | 494 | // initialize i2c bus (needs Timer 1) |
41 | ingob | 495 | I2C1_Init(); |
247 | killagreg | 496 | // initialize fat16 partition on sd card (needs Timer 1) |
41 | ingob | 497 | Fat16_Init(); |
156 | killagreg | 498 | // initialize NC params |
499 | NCParams_Init(); |
||
88 | killagreg | 500 | // initialize the settings |
501 | Settings_Init(); |
||
92 | killagreg | 502 | // initialize logging (needs settings) |
82 | killagreg | 503 | Logging_Init(); |
1 | ingob | 504 | |
362 | holgerb | 505 | LED_GRN_ON; |
41 | ingob | 506 | TimerCheckError = SetDelay(3000); |
175 | holgerb | 507 | UART1_PutString("\r\n++++++++++++++++++++++++++++++++++++++++++"); |
110 | killagreg | 508 | UART1_PutString("\n\r Version information:"); |
116 | killagreg | 509 | |
41 | ingob | 510 | GetNaviCtrlVersion(); |
338 | holgerb | 511 | DebugOut.StatusGreen = AMPEL_NC | AMPEL_COMPASS; // NC and MK3Mag |
512 | DebugOut.StatusRed = 0x00; |
||
400 | holgerb | 513 | UART1_PutString("\r\n++++++++++++++++++++++++++++++++++++++++++"); |
149 | killagreg | 514 | |
242 | killagreg | 515 | Compass_Init(); |
270 | killagreg | 516 | |
400 | holgerb | 517 | GPS_Init(); |
518 | |||
314 | killagreg | 519 | #ifdef FOLLOW_ME |
520 | TransmitAlsoToFC = 1; |
||
521 | UART1_PutString("\r\n++++++++++++++++++++++++++++++++++++++++++"); |
||
522 | UART1_PutString("\n\r FOLLOW-ME Transmitter only!"); |
||
523 | UART1_PutString("\r\n++++++++++++++++++++++++++++++++++++++++++\r\n\r\n"); |
||
524 | TransmitAlsoToFC = 0; |
||
525 | #else |
||
150 | killagreg | 526 | SPI0_GetFlightCtrlVersion(); |
527 | if(FC_Version.Compatible != FC_SPI_COMPATIBLE) |
||
41 | ingob | 528 | { |
150 | killagreg | 529 | UART1_PutString("\n\r Flight-Ctrl not compatible"); |
41 | ingob | 530 | LED_RED_ON; |
531 | } |
||
314 | killagreg | 532 | #endif |
213 | killagreg | 533 | // ---------- Prepare the isr driven |
154 | killagreg | 534 | // set to absolute lowest priority |
535 | VIC_Config(EXTIT3_ITLine, VIC_IRQ, PRIORITY_SW); |
||
195 | killagreg | 536 | // enable interrupts |
154 | killagreg | 537 | VIC_ITCmd(EXTIT3_ITLine, ENABLE); |
244 | killagreg | 538 | |
250 | ingob | 539 | Debug_OK("START"); |
362 | holgerb | 540 | UART1_PutString("\r\n"); |
380 | holgerb | 541 | fifo_purge(&UART1_rx_fifo); // flush the whole fifo init buffer |
542 | LED_GRN_ON; |
||
543 | LED_RED_OFF; |
||
154 | killagreg | 544 | for (;;) // the endless main loop |
41 | ingob | 545 | { |
431 | killagreg | 546 | Polling(); |
378 | holgerb | 547 | PollingTimeout = 15; |
431 | killagreg | 548 | // ---------------- Logging --------------------------------------- |
380 | holgerb | 549 | if(SD_WatchDog) |
431 | killagreg | 550 | { |
551 | SD_WatchDog = 30000; |
||
552 | if(SDCardInfo.Valid == 1) 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 |
||
553 | else if(FC.StatusFlags & FC_STATUS_START) SD_LoggingError = 100; |
||
554 | if(!SD_WatchDog) UART1_PutString("\n\rSD-Watchdog - Logging aborted\n\r"); |
||
555 | } |
||
380 | holgerb | 556 | |
432 | holgerb | 557 | /* |
324 | killagreg | 558 | if(CheckDelay(ftimer)) |
559 | { |
||
560 | |||
431 | killagreg | 561 | static s8 filename[35]; |
562 | static u8 i = 0; |
||
563 | s8 dbgmsg[40]; |
||
564 | |||
565 | |||
324 | killagreg | 566 | |
567 | ftimer = SetDelay(100); |
||
568 | if(FC.Poti[3]>100 && fstate == 0) |
||
569 | { |
||
570 | fstate = 1; |
||
431 | killagreg | 571 | sprintf(filename, "/toast/toasta/toast%02i.txt",i++); |
324 | killagreg | 572 | } |
573 | else if(FC.Poti[3]<100 && fstate == 2) |
||
574 | { |
||
575 | fstate = 3; |
||
576 | } |
||
577 | |||
578 | switch(fstate) |
||
579 | { |
||
580 | case 1: |
||
431 | killagreg | 581 | sprintf(dbgmsg,"\r\nStart writing file: %s", filename); |
582 | Debug(dbgmsg); |
||
324 | killagreg | 583 | f = fopen_(filename, 'a'); |
584 | if(f== NULL) Fat16_Init(); |
||
585 | fstate = 2; |
||
586 | break; |
||
587 | |||
588 | case 2: |
||
589 | fputs_("\r\ntest edins sdv dsivbds iv dsivb disbv idsv bisd bv d suiv dsibsivbdis fbvisdöb visdbvisdb vidbfibds ibv", f); |
||
590 | break; |
||
591 | |||
592 | case 3: |
||
431 | killagreg | 593 | sprintf(dbgmsg,"\r\nClosing file: %s", filename); |
594 | Debug(dbgmsg); |
||
324 | killagreg | 595 | fclose_(f); |
596 | fstate = 0; |
||
597 | break; |
||
598 | |||
599 | default: |
||
600 | break; |
||
601 | } |
||
602 | } |
||
432 | holgerb | 603 | */ |
24 | StephanB | 604 | } |
1 | ingob | 605 | } |
438 | holgerb | 606 | //DebugOut.Analog[16] |