Subversion Repositories NaviCtrl

Rev

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