Subversion Repositories NaviCtrl

Rev

Rev 480 | 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"
470 killagreg 65
#include "i2c0.h"
66
#include "i2c1.h"       
242 killagreg 67
#include "compass.h"
426 holgerb 68
#include "ncmag.h"
119 killagreg 69
#include "timer1.h"
70
#include "timer2.h"
134 killagreg 71
#include "analog.h"
41 ingob 72
#include "spi_slave.h"
73
#include "fat16.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"
473 holgerb 85
#include "ncmag.h"
254 killagreg 86
 
427 killagreg 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
457 holgerb 105
u32 CountGpsProcessedIn5Sec = 0,CountNewGpsDataIn5Sec = 0, FreqGpsProcessedIn5Sec = 0, FreqNewGpsDataIn5Sec = 0;
1 ingob 106
 
41 ingob 107
s8 ErrorMSG[25];
108
 
1 ingob 109
//----------------------------------------------------------------------------------------------------
41 ingob 110
void SCU_Config(void)
1 ingob 111
{
41 ingob 112
        /* configure PLL and set it as master clock source */
113
        SCU_MCLKSourceConfig(SCU_MCLK_OSC);             // set master clock source to external oscillator clock (25MHz) before diabling the PLL
114
        SCU_PLLCmd(DISABLE);                                    // now disable the PLL
115
        #ifdef MCLK96MHZ
116
        SCU_BRCLKDivisorConfig(SCU_BRCLK_Div2); // set BRCLK to MCLK/2 = 48MHz
117
        SCU_PCLKDivisorConfig(SCU_PCLK_Div4);   // set PCLK     (APB bus clock) divisor to 4 (half Reference Clock)
118
        SCU_RCLKDivisorConfig(SCU_RCLK_Div2);   // set RCLK     (Reference Clock) divisor to 1 (full PPL clock)
119
        SCU_HCLKDivisorConfig(SCU_HCLK_Div2);   // set HCLK     (AHB bus clock) divisor to 1 (full Reference Clock)
120
        SCU_PLLFactorsConfig(192,25,2);                 // PLL = 96 MHz, Feedback Divider N=192, Pre-Divider M=25, Post-Divider P=2
121
        #else
122
        SCU_BRCLKDivisorConfig(SCU_BRCLK_Div1); // set BRCLK to MCLK = 48MHz
123
        SCU_PCLKDivisorConfig(SCU_PCLK_Div2);   // set PCLK     (APB bus clock) divisor to 2 (half Reference Clock)
124
        SCU_RCLKDivisorConfig(SCU_RCLK_Div1);   // set RCLK     (Reference Clock) divisor to 1 (full PPL clock)
125
        SCU_HCLKDivisorConfig(SCU_HCLK_Div1);   // set HCLK     (AHB bus clock) divisor to 1 (full Reference Clock)
126
        SCU_PLLFactorsConfig(192,25,3);                 // PLL = 48 MHz, Feedback Divider N=192, Pre-Divider M=25, Post-Divider P=3
127
        #endif
128
        SCU_PLLCmd(ENABLE);                                     // Enable PLL (is disabled by SCU_PLLFactorsConfig)
129
        SCU_MCLKSourceConfig(SCU_MCLK_PLL);     // set master clock source to PLL
130
}
1 ingob 131
 
41 ingob 132
//----------------------------------------------------------------------------------------------------
133
void GetNaviCtrlVersion(void)
1 ingob 134
{
41 ingob 135
        u8 msg[25];
270 killagreg 136
 
472 holgerb 137
        sprintf(msg,"\n\r NaviCtrl HW: V%d.%d SW: V%d.%02d%c",  Version_HW/10, Version_HW%10, VERSION_MAJOR,  VERSION_MINOR, 'a'+ VERSION_PATCH);
110 killagreg 138
        UART1_PutString(msg);
1 ingob 139
}
140
 
141
//----------------------------------------------------------------------------------------------------
231 holgerb 142
 
41 ingob 143
void CheckErrors(void)
144
{
329 holgerb 145
    static s32 no_error_delay = 0;
328 holgerb 146
        s32 newErrorCode = 0;
232 killagreg 147
        UART_VersionInfo.HardwareError[0] = 0;
231 holgerb 148
 
472 holgerb 149
        if((I2C_CompassPort == I2C_INTERN_1 && CheckDelay(I2C1_Timeout)) || (I2C_CompassPort == I2C_EXTERN_0 && CheckDelay(I2C0_Timeout)) || (Compass_Heading < 0))
150
 
151
         DebugOut.StatusRed |= AMPEL_COMPASS;
338 holgerb 152
        else DebugOut.StatusRed &= ~AMPEL_COMPASS; // MK3Mag green status
231 holgerb 153
 
338 holgerb 154
        if((FC.Error[1] & FC_ERROR1_I2C) || (FC.Error[1] & FC_ERROR1_BL_MISSING)) DebugOut.StatusRed |= AMPEL_BL;
155
        else DebugOut.StatusRed &= ~AMPEL_BL; // BL-Ctrl green status
232 killagreg 156
 
338 holgerb 157
        if(UART_VersionInfo.HardwareError[0] || UART_VersionInfo.HardwareError[1]) DebugOut.StatusRed |= AMPEL_NC;
158
        else DebugOut.StatusRed &= ~AMPEL_NC;
159
 
480 holgerb 160
if(I2C_CompassPort == I2C_EXTERN_0) LED_RED_OFF_T;
161
 
397 holgerb 162
    if((FCCalibActive || CompassCalState) && FC_Version.Hardware)
392 holgerb 163
     {
164
                sprintf(ErrorMSG,"Calibrate... ");
165
                newErrorCode = 0;
166
                ErrorCode = 0;
167
                no_error_delay = 1;
168
         }
472 holgerb 169
        else if(CheckDelay(I2C1_Timeout) && (I2C_CompassPort == I2C_INTERN_1))
41 ingob 170
        {
380 holgerb 171
                LED_RED_ON;              
472 holgerb 172
                sprintf(ErrorMSG,"no compass communica");
173
                //Reset I2CBus
174
                I2C1_Deinit();
175
                I2C1_Init();
176
                newErrorCode = 4;
41 ingob 177
                StopNavigation = 1;
472 holgerb 178
                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_RX;
179
                DebugOut.StatusRed |= AMPEL_COMPASS;
41 ingob 180
        }
472 holgerb 181
        else if(CheckDelay(I2C0_Timeout) && (I2C_CompassPort == I2C_EXTERN_0))
41 ingob 182
        {
368 holgerb 183
                LED_RED_ON;              
480 holgerb 184
LED_RED_ON_T;
472 holgerb 185
                sprintf(ErrorMSG,"no ext. compass ");
149 killagreg 186
                //Reset I2CBus
472 holgerb 187
                I2C0_Deinit();
188
                I2C0_Init();
480 holgerb 189
                NCMAG_Update(1);
190
                newErrorCode = 33;
41 ingob 191
                StopNavigation = 1;
256 killagreg 192
                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_RX;
338 holgerb 193
                DebugOut.StatusRed |= AMPEL_COMPASS;
41 ingob 194
        }
472 holgerb 195
        else
196
        if(CheckDelay(SPI0_Timeout))
197
        {
198
                LED_RED_ON;              
199
                sprintf(ErrorMSG,"no FC communication ");
200
                newErrorCode = 3;
201
                StopNavigation = 1;
202
                DebugOut.StatusGreen &= ~AMPEL_FC; // status of FC Present
203
                DebugOut.StatusGreen &= ~AMPEL_BL; // status of BL Present
204
                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_SPI_RX;
205
        }
41 ingob 206
        else if(FC_Version.Compatible != FC_SPI_COMPATIBLE)
207
        {
208
                LED_RED_ON;
318 holgerb 209
#ifndef FOLLOW_ME
41 ingob 210
                sprintf(ErrorMSG,"FC not compatible ");
318 holgerb 211
#else
212
                sprintf(ErrorMSG,"! FollowMe only ! ");
213
#endif
328 holgerb 214
                newErrorCode = 1;
41 ingob 215
                StopNavigation = 1;
256 killagreg 216
                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_FC_INCOMPATIBLE;
338 holgerb 217
                DebugOut.StatusRed |= AMPEL_NC;
41 ingob 218
        }
231 holgerb 219
 
255 killagreg 220
        else if(FC.Error[0] & FC_ERROR0_GYRO_NICK)
231 holgerb 221
        {
222
                LED_RED_ON;
223
                sprintf(ErrorMSG,"ERR: FC Nick Gyro");
328 holgerb 224
                newErrorCode = 10;
232 killagreg 225
        }
255 killagreg 226
        else if(FC.Error[0] & FC_ERROR0_GYRO_ROLL)
231 holgerb 227
        {
228
                LED_RED_ON;
229
                sprintf(ErrorMSG,"ERR: FC Roll Gyro");
328 holgerb 230
                newErrorCode = 11;
232 killagreg 231
        }
255 killagreg 232
        else if(FC.Error[0] & FC_ERROR0_GYRO_YAW)
231 holgerb 233
        {
234
                LED_RED_ON;
235
                sprintf(ErrorMSG,"ERR: FC Yaw Gyro");
328 holgerb 236
                newErrorCode = 12;
232 killagreg 237
        }
255 killagreg 238
        else if(FC.Error[0] & FC_ERROR0_ACC_NICK)
231 holgerb 239
        {
240
                LED_RED_ON;
241
                sprintf(ErrorMSG,"ERR: FC Nick ACC");
328 holgerb 242
                newErrorCode = 13;
232 killagreg 243
        }
255 killagreg 244
        else if(FC.Error[0] & FC_ERROR0_ACC_ROLL)
231 holgerb 245
        {
246
                LED_RED_ON;
247
                sprintf(ErrorMSG,"ERR: FC Roll ACC");
328 holgerb 248
                newErrorCode = 14;
232 killagreg 249
        }
255 killagreg 250
        else if(FC.Error[0] & FC_ERROR0_ACC_TOP)
231 holgerb 251
        {
252
                LED_RED_ON;
327 holgerb 253
                sprintf(ErrorMSG,"ERR:FC Z-ACC");
328 holgerb 254
                newErrorCode = 15;
232 killagreg 255
        }
351 holgerb 256
        else if(NC_To_FC_Flags & NC_TO_FC_FLYING_RANGE)
257
        {
258
                LED_RED_ON;
259
                sprintf(ErrorMSG,"ERR:Flying range!");
260
                newErrorCode = 28;
261
        }
255 killagreg 262
        else if(FC.Error[0] & FC_ERROR0_PRESSURE)
231 holgerb 263
        {
380 holgerb 264
                LED_RED_ON;              
327 holgerb 265
                sprintf(ErrorMSG,"ERR:Pressure sensor");
328 holgerb 266
                newErrorCode = 16;
232 killagreg 267
        }
255 killagreg 268
        else if(FC.Error[1] &  FC_ERROR1_I2C)
231 holgerb 269
        {
270
                LED_RED_ON;
327 holgerb 271
                sprintf(ErrorMSG,"ERR:I2C FC to BL");
328 holgerb 272
                newErrorCode = 17;
232 killagreg 273
        }
255 killagreg 274
        else if(FC.Error[1] &  FC_ERROR1_BL_MISSING)
231 holgerb 275
        {
276
                LED_RED_ON;
277
                sprintf(ErrorMSG,"ERR: Bl Missing");
328 holgerb 278
                newErrorCode = 18;
232 killagreg 279
        }
255 killagreg 280
        else if(FC.Error[1] &  FC_ERROR1_MIXER)
231 holgerb 281
        {
282
                LED_RED_ON;
283
                sprintf(ErrorMSG,"Mixer Error");
328 holgerb 284
                newErrorCode = 19;
232 killagreg 285
        }
350 holgerb 286
        else if(CheckDelay(UBX_Timeout) && Parameter.GlobalConfig & FC_CFG_GPS_AKTIV)
41 ingob 287
        {
288
                LED_RED_ON;
350 holgerb 289
//      if(!(Parameter.GlobalConfig & FC_CFG_GPS_AKTIV)) sprintf(ErrorMSG,"GPS disconnected ");
290
//              else 
380 holgerb 291
                 {                                     
292
                  sprintf(ErrorMSG,"no GPS communication");
320 holgerb 293
                  UART_VersionInfo.HardwareError[0] |= NC_ERROR0_GPS_RX;
454 holgerb 294
                  UART_VersionInfo.Flags &= ~NC_VERSION_FLAG_GPS_PRESENT;
328 holgerb 295
              newErrorCode = 5;
320 holgerb 296
                 }
41 ingob 297
                StopNavigation = 1;
320 holgerb 298
//              UBX_Timeout = SetDelay(500);
41 ingob 299
        }
426 holgerb 300
        else if(Compass_Heading < 0 && NCMAG_Present && !NCMAG_IsCalibrated)
301
        {
302
                LED_RED_ON;
303
                sprintf(ErrorMSG,"compass not calibr.");
304
                newErrorCode = 31;
305
                StopNavigation = 1;
306
                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_VALUE;
307
        }
254 killagreg 308
        else if(Compass_Heading < 0)
41 ingob 309
        {
310
                LED_RED_ON;
311
                sprintf(ErrorMSG,"bad compass value ");
328 holgerb 312
                newErrorCode = 6;
41 ingob 313
                StopNavigation = 1;
256 killagreg 314
                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_VALUE;
41 ingob 315
        }
255 killagreg 316
        else if((FC.Error[1] &  FC_ERROR1_SPI_RX))
199 killagreg 317
        {
318
                LED_RED_ON;
319
                sprintf(ErrorMSG,"FC spi rx error ");
328 holgerb 320
                newErrorCode = 8;
213 killagreg 321
                StopNavigation = 1;
199 killagreg 322
        }
255 killagreg 323
        else if(FC.Error[0] &  FC_ERROR0_CAREFREE)
231 holgerb 324
        {
325
                LED_RED_ON;
326
                sprintf(ErrorMSG,"FC: Carefree Error");
328 holgerb 327
                newErrorCode = 20;
232 killagreg 328
        }
255 killagreg 329
        else if(FC.Error[1] &  FC_ERROR1_PPM)
41 ingob 330
        {
331
                LED_RED_ON;
332
                sprintf(ErrorMSG,"RC Signal lost ");
328 holgerb 333
                newErrorCode = 7;
41 ingob 334
        }
328 holgerb 335
        else if(ErrorGpsFixLost)
336
        {
337
         LED_RED_ON;
338
         sprintf(ErrorMSG,"GPS Fix lost    ");
339
         newErrorCode = 21;
340
        }
330 holgerb 341
        else if(ErrorDisturbedEarthMagnetField)
342
        {
343
         LED_RED_ON;
344
         sprintf(ErrorMSG,"Magnet error    ");
345
         newErrorCode = 22;
338 holgerb 346
         DebugOut.StatusRed |= AMPEL_COMPASS | AMPEL_NC;
347
         UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_VALUE;
330 holgerb 348
        }
472 holgerb 349
        else if(((BL_MinOfMaxPWM == 40 && (FC.StatusFlags & FC_STATUS_FLY)) || BL_MinOfMaxPWM == 39) && !ErrorCode)
338 holgerb 350
        {
351
         LED_RED_ON;
352
         sprintf(ErrorMSG,"ERR:Motor restart  ");
353
         newErrorCode = 23;
354
         DebugOut.StatusRed |= AMPEL_BL;
355
        }
472 holgerb 356
        else if(BL_MinOfMaxPWM < 30 && !ErrorCode)
338 holgerb 357
        {
472 holgerb 358
     unsigned int i;
359
         for(i = 0; i < 12; i++) if(Motor[i].MaxPWM == BL_MinOfMaxPWM) break;
360
 
338 holgerb 361
         LED_RED_ON;
472 holgerb 362
         sprintf(ErrorMSG,"ERR:BL%2d Test:%2d ",i+1,BL_MinOfMaxPWM);
363
         newErrorCode = 32;
364
         DebugOut.StatusRed |= AMPEL_BL;
365
        }
366
        else if(BL_MinOfMaxPWM < 248 && (FC.StatusFlags & FC_STATUS_FLY) && !ErrorCode)
367
        {
368
         LED_RED_ON;
338 holgerb 369
         sprintf(ErrorMSG,"ERR:BL Limitation   ");
370
         newErrorCode = 24;
371
         DebugOut.StatusRed |= AMPEL_BL;
372
        }
348 holgerb 373
        else if(NCFlags & NC_FLAG_RANGE_LIMIT && (FC.StatusFlags & FC_STATUS_FLY) && !ErrorCode)
374
        {
375
         LED_RED_ON;
351 holgerb 376
         sprintf(ErrorMSG,"ERR:GPS range  ");
348 holgerb 377
         newErrorCode = 25;
378
         DebugOut.StatusRed |= AMPEL_NC;
379
        }
397 holgerb 380
        else if((!SD_SWITCH || (SDCardInfo.Valid == 0)) && Parameter.GlobalConfig3 & CFG3_NO_SDCARD_NO_START && !(FC.StatusFlags & FC_STATUS_FLY))
350 holgerb 381
        {
382
         LED_RED_ON;
383
         sprintf(ErrorMSG,"ERR:No SD-Card  ");
384
         newErrorCode = 26;
385
         DebugOut.StatusRed |= AMPEL_NC;
386
        }
383 holgerb 387
        else if((SD_LoggingError || (SD_WatchDog < 2000 && SD_WatchDog != 0)) && Parameter.GlobalConfig3 & CFG3_NO_SDCARD_NO_START)
351 holgerb 388
        {
368 holgerb 389
         LED_RED_ON;       
380 holgerb 390
         sprintf(ErrorMSG,"ERR:SD Logging abort");
351 holgerb 391
         newErrorCode = 27;
392
         DebugOut.StatusRed |= AMPEL_NC;
393
         SD_LoggingError = 0;
394
        }
355 holgerb 395
        else if(((AbsoluteFlyingAltitude) && (NaviData.Altimeter / 20 >= AbsoluteFlyingAltitude)) && (FC.StatusFlags & FC_STATUS_FLY))
396
        {
397
         LED_RED_ON;
398
         sprintf(ErrorMSG,"ERR:Max Altitude ");
399
         newErrorCode = 29;
400
         DebugOut.StatusRed |= AMPEL_NC;
401
        }
420 holgerb 402
        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 403
        {                                                                                                                                                  
404
         LED_RED_ON;
405
         sprintf(ErrorMSG,"No GPS Fix      ");
406
         newErrorCode = 30;
407
        }
41 ingob 408
        else // no error occured
409
        {
410
                StopNavigation = 0;
411
                LED_RED_OFF;
328 holgerb 412
                if(no_error_delay) { no_error_delay--;  }
413
                else
380 holgerb 414
                 {                                     
415
                  sprintf(ErrorMSG,"No Error            ");
328 holgerb 416
                  ErrorCode = 0;
417
                 }
41 ingob 418
        }
328 holgerb 419
 
420
    if(newErrorCode)
421
         {
339 holgerb 422
          if(FC.StatusFlags & FC_STATUS_MOTOR_RUN) no_error_delay = 8; // delay the errors if the motors are running
328 holgerb 423
          ErrorCode = newErrorCode;
424
         }
425
 FC.Error[0] = 0;
426
 FC.Error[1] = 0;
427
 FC.Error[2] = 0;
428
 FC.Error[3] = 0;
429
 FC.Error[4] = 0;
430
 ErrorGpsFixLost = 0;
41 ingob 431
}
24 StephanB 432
 
378 holgerb 433
 
434
 
435
void Polling(void)
436
{
453 holgerb 437
 static u8 running = 0, oldFcFlags = 0, count5sec;
378 holgerb 438
 if(running) return;
439
 running = 1;
440
                SPI0_UpdateBuffer();    // also calls the GPS-functions
441
                UART0_ProcessRxData();  // GPS process request
442
                UART0_TransmitTxData(); // GPS send answer
380 holgerb 443
                UART1_ProcessRxData();  // PC process request
378 holgerb 444
                UART1_TransmitTxData(); // PC send answer
445
                UART2_TransmitTxData(); // FC send answer
446
                CalcHeadFree();
380 holgerb 447
                // ---------------- Error Check Timing ----------------------------
398 holgerb 448
                if(CheckDelay(TimerCheckError) || (FC.StatusFlags & FC_STATUS_START && !(oldFcFlags & FC_STATUS_START))) // Timer or FY wants to start
380 holgerb 449
                {
453 holgerb 450
                        if(CheckDelay(TimerCheckError))
451
                         {
452
                          TimerCheckError = SetDelay(1000);
453
                          if(++count5sec == 5)
454
                           {
455
                                count5sec = 0;
457 holgerb 456
                                FreqGpsProcessedIn5Sec = CountGpsProcessedIn5Sec * 2;
457
                                FreqNewGpsDataIn5Sec = CountNewGpsDataIn5Sec * 2;
458
                                CountGpsProcessedIn5Sec = 0;
459
                                CountNewGpsDataIn5Sec = 0;  
453 holgerb 460
                           }
461
                         }
398 holgerb 462
                        oldFcFlags = FC.StatusFlags;
380 holgerb 463
                        if(CheckDelay(SPI0_Timeout) && (DebugUART == UART1)) GPS_Navigation(&GPSData, &(ToFlightCtrl.GPSStick)); // process the GPS data even if the FC is not connected
464
 
465
                        if(!CheckDelay(SPI0_Timeout) || (DebugUART == UART1)) CheckErrors();
466
 
467
                        if(FC.StatusFlags & FC_STATUS_FLY) NaviData.FlyingTime++; // we want to count the battery-time
468
                        if(SerialLinkOkay) SerialLinkOkay--;
469
                        if(SerialLinkOkay < 250 - 5) NCFlags |= NC_FLAG_NOSERIALLINK; // 5 seconds timeout for serial communication
470
                        else NCFlags &= ~NC_FLAG_NOSERIALLINK;
471
                        if(StopNavigation && (Parameter.NaviGpsModeControl >=  50) && (Parameter.GlobalConfig & FC_CFG_GPS_AKTIV)) BeepTime = 1000;
472
                }
378 holgerb 473
 running = 0;
474
}
475
 
380 holgerb 476
// the handler will be cyclic called by the timer 1 ISR
477
// used is for critical timing parts that normaly would handled
478
// within the main loop that could block longer at logging activities
154 killagreg 479
void EXTIT3_IRQHandler(void)
480
{
195 killagreg 481
        IENABLE;
154 killagreg 482
        VIC_ITCmd(EXTIT3_ITLine,DISABLE); // disable irq
292 killagreg 483
        Compass_Update();               // update compass communication
154 killagreg 484
        Analog_Update();                // get new ADC values
378 holgerb 485
 
380 holgerb 486
        if(!PollingTimeout)
487
        {
488
                PollingTimeout = 5;
489
                Polling();
490
        }
378 holgerb 491
 
492
        VIC_SWITCmd(EXTIT3_ITLine,DISABLE); // clear pending bit
195 killagreg 493
        VIC_ITCmd(EXTIT3_ITLine, ENABLE); // enable irq
494
        IDISABLE;
378 holgerb 495
        VIC1->VAR = 0xFF; // write any value to VIC0 Vector address register
154 killagreg 496
}
497
 
41 ingob 498
//----------------------------------------------------------------------------------------------------
499
int main(void)
500
{
431 killagreg 501
 
324 killagreg 502
        static u32 ftimer =0;
503
        static u8 fstate = 0;
456 holgerb 504
//      static File_t* f = NULL;
324 killagreg 505
 
431 killagreg 506
 
41 ingob 507
        /* Configure the system clocks */
508
        SCU_Config();
509
        /* init VIC (Vectored Interrupt Controller)     */
510
        SCU_AHBPeriphClockConfig(__VIC,ENABLE); // enable AHB bus clock for VIC
511
        SCU_AHBPeriphReset(__VIC, DISABLE);             // disable reset state for VIC
512
        VIC_DeInit();                                                   // deinitializes the VIC module registers to their default reset values.
196 killagreg 513
        VIC_InitDefaultVectors();
514
 
119 killagreg 515
        // initialize timer 1 for System Clock and delay rountines
41 ingob 516
        TIMER1_Init();
517
        // initialize the LEDs (needs Timer 1)
518
        Led_Init();
519
        // initialize the debug UART1
520
        UART1_Init();
110 killagreg 521
        UART1_PutString("\r\n---------------------------------------------");
120 killagreg 522
        // initialize timer 2 for servo outputs
180 killagreg 523
        //TIMER2_Init();
41 ingob 524
        // initialize UART2 to FLIGHTCTRL
525
        UART2_Init();
526
        // initialize UART0 (to MKGPS or MK3MAG)
527
        UART0_Init();
134 killagreg 528
        // initialize adc
529
        Analog_Init();
41 ingob 530
        // initialize SPI0 to FC
531
        SPI0_Init();
469 killagreg 532
        // initialize i2c busses (needs Timer 1)
465 ingob 533
        I2C0_Init();
41 ingob 534
        I2C1_Init();
465 ingob 535
 
247 killagreg 536
        // initialize fat16 partition on sd card (needs Timer 1)
41 ingob 537
        Fat16_Init();
156 killagreg 538
        // initialize NC params
539
        NCParams_Init();
88 killagreg 540
        // initialize the settings
541
        Settings_Init();
92 killagreg 542
        // initialize logging (needs settings)
82 killagreg 543
        Logging_Init();
1 ingob 544
 
362 holgerb 545
        LED_GRN_ON;
41 ingob 546
        TimerCheckError = SetDelay(3000);
175 holgerb 547
        UART1_PutString("\r\n++++++++++++++++++++++++++++++++++++++++++");
110 killagreg 548
        UART1_PutString("\n\r Version information:");
116 killagreg 549
 
41 ingob 550
        GetNaviCtrlVersion();
338 holgerb 551
        DebugOut.StatusGreen = AMPEL_NC | AMPEL_COMPASS; // NC and MK3Mag
552
        DebugOut.StatusRed = 0x00;
400 holgerb 553
        UART1_PutString("\r\n++++++++++++++++++++++++++++++++++++++++++");
149 killagreg 554
 
242 killagreg 555
        Compass_Init();
270 killagreg 556
 
400 holgerb 557
        GPS_Init();
558
 
314 killagreg 559
#ifdef FOLLOW_ME
560
        TransmitAlsoToFC = 1;
561
        UART1_PutString("\r\n++++++++++++++++++++++++++++++++++++++++++");
562
        UART1_PutString("\n\r FOLLOW-ME Transmitter only!");
563
        UART1_PutString("\r\n++++++++++++++++++++++++++++++++++++++++++\r\n\r\n");
564
        TransmitAlsoToFC = 0;
565
#else
150 killagreg 566
        SPI0_GetFlightCtrlVersion();
567
        if(FC_Version.Compatible != FC_SPI_COMPATIBLE)
41 ingob 568
        {
461 holgerb 569
                UART1_PutString("\n\r Flight-Ctrl not compatible\n\r");
41 ingob 570
                LED_RED_ON;
571
        }
314 killagreg 572
#endif
213 killagreg 573
        // ---------- Prepare the isr driven
154 killagreg 574
        // set to absolute lowest priority
575
    VIC_Config(EXTIT3_ITLine, VIC_IRQ, PRIORITY_SW);
195 killagreg 576
        // enable interrupts
154 killagreg 577
    VIC_ITCmd(EXTIT3_ITLine, ENABLE);
244 killagreg 578
 
250 ingob 579
        Debug_OK("START");
362 holgerb 580
        UART1_PutString("\r\n");
380 holgerb 581
        fifo_purge(&UART1_rx_fifo); // flush the whole fifo init buffer
582
        LED_GRN_ON;
583
        LED_RED_OFF;
460 holgerb 584
    Settings_GetParamValue(PID_SEND_NMEA, &NMEA_Interval);
461 holgerb 585
        UART1_PutString("\r\n");
154 killagreg 586
        for (;;) // the endless main loop
41 ingob 587
        {
431 killagreg 588
                Polling();
378 holgerb 589
                PollingTimeout = 15;
431 killagreg 590
                // ---------------- Logging  ---------------------------------------
380 holgerb 591
                if(SD_WatchDog)
431 killagreg 592
                {
593
                        SD_WatchDog = 30000;
594
                        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
595
                        else if(FC.StatusFlags & FC_STATUS_START) SD_LoggingError = 100;
596
                        if(!SD_WatchDog) UART1_PutString("\n\rSD-Watchdog - Logging aborted\n\r");
597
                }
380 holgerb 598
 
432 holgerb 599
/*             
324 killagreg 600
                if(CheckDelay(ftimer))
601
                {
602
 
431 killagreg 603
                        static s8 filename[35];
604
                        static u8 i = 0;
605
                        s8 dbgmsg[40];
606
 
607
 
324 killagreg 608
 
609
                        ftimer = SetDelay(100);
610
                        if(FC.Poti[3]>100 && fstate == 0)
611
                        {
612
                                fstate = 1;
431 killagreg 613
                                sprintf(filename, "/toast/toasta/toast%02i.txt",i++);
324 killagreg 614
                        }
615
                        else if(FC.Poti[3]<100 && fstate == 2)
616
                        {
617
                                fstate = 3;
618
                        }
619
 
620
                        switch(fstate)
621
                        {
622
                                case 1:
431 killagreg 623
                                        sprintf(dbgmsg,"\r\nStart writing file: %s", filename);
624
                                        Debug(dbgmsg);
324 killagreg 625
                                        f = fopen_(filename, 'a');
626
                                        if(f== NULL) Fat16_Init();
627
                                        fstate = 2;
628
                                        break;
629
 
630
                                case 2:
631
                                        fputs_("\r\ntest edins sdv dsivbds iv dsivb disbv idsv bisd bv d suiv dsibsivbdis fbvisdöb visdbvisdb vidbfibds ibv", f);
632
                                        break;
633
 
634
                                case 3:
431 killagreg 635
                                        sprintf(dbgmsg,"\r\nClosing file: %s", filename);
636
                                        Debug(dbgmsg);
324 killagreg 637
                                        fclose_(f);
638
                                        fstate = 0;
639
                                        break;
640
 
641
                                default:
642
                                        break;
643
                        }
644
                }
432 holgerb 645
*/             
24 StephanB 646
        }
1 ingob 647
}
438 holgerb 648
//DebugOut.Analog[16]