Subversion Repositories NaviCtrl

Rev

Rev 615 | 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
489 killagreg 10
// + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool
360 holgerb 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
489 killagreg 21
// + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand
360 holgerb 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 -
489 killagreg 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
360 holgerb 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;
491 killagreg 58
//#include <stdio.h>
41 ingob 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"
489 killagreg 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 "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"
380 holgerb 83
#include "uart1.h"
254 killagreg 84
 
427 killagreg 85
 
314 killagreg 86
#ifdef FOLLOW_ME
87
u8 TransmitAlsoToFC = 0;
88
#endif
41 ingob 89
u32 TimerCheckError;
489 killagreg 90
u8 ErrorCode = 0;
41 ingob 91
u16 BeepTime;
92 killagreg 92
u8  NCFlags = 0;
153 killagreg 93
s16 GeoMagDec = 0; // local magnetic declination in 0.1 deg
328 holgerb 94
u8 ErrorGpsFixLost = 0;
513 killagreg 95
u8 FromFC_LoadWP_List = 0, FromFC_Load_SinglePoint = 0, FromFC_Save_SinglePoint = 0;
531 holgerb 96
u8 ToFC_MaxWpListIndex = 4;
255 killagreg 97
u8 ClearFCStatusFlags = 0;
41 ingob 98
u8 StopNavigation = 0;
378 holgerb 99
volatile u32 PollingTimeout = 10000;
41 ingob 100
Param_t Parameter;
78 holgerb 101
volatile FC_t FC;
378 holgerb 102
volatile u32 SPIWatchDog = 15000; // stop Navigation if this goes to zero
400 holgerb 103
volatile u32 SD_WatchDog = 15000; // stop Logging if this goes to zero
457 holgerb 104
u32 CountGpsProcessedIn5Sec = 0,CountNewGpsDataIn5Sec = 0, FreqGpsProcessedIn5Sec = 0, FreqNewGpsDataIn5Sec = 0;
504 holgerb 105
u8 NewWPL_Name = 0;
532 holgerb 106
u32 MaxWP_Radius_in_m = 0;
41 ingob 107
s8 ErrorMSG[25];
533 holgerb 108
u32 TimeSinceMotorStart = 0;
41 ingob 109
 
1 ingob 110
//----------------------------------------------------------------------------------------------------
41 ingob 111
void SCU_Config(void)
1 ingob 112
{
41 ingob 113
        /* configure PLL and set it as master clock source */
114
        SCU_MCLKSourceConfig(SCU_MCLK_OSC);             // set master clock source to external oscillator clock (25MHz) before diabling the PLL
115
        SCU_PLLCmd(DISABLE);                                    // now disable the PLL
116
        #ifdef MCLK96MHZ
117
        SCU_BRCLKDivisorConfig(SCU_BRCLK_Div2); // set BRCLK to MCLK/2 = 48MHz
118
        SCU_PCLKDivisorConfig(SCU_PCLK_Div4);   // set PCLK     (APB bus clock) divisor to 4 (half Reference Clock)
119
        SCU_RCLKDivisorConfig(SCU_RCLK_Div2);   // set RCLK     (Reference Clock) divisor to 1 (full PPL clock)
120
        SCU_HCLKDivisorConfig(SCU_HCLK_Div2);   // set HCLK     (AHB bus clock) divisor to 1 (full Reference Clock)
121
        SCU_PLLFactorsConfig(192,25,2);                 // PLL = 96 MHz, Feedback Divider N=192, Pre-Divider M=25, Post-Divider P=2
122
        #else
123
        SCU_BRCLKDivisorConfig(SCU_BRCLK_Div1); // set BRCLK to MCLK = 48MHz
124
        SCU_PCLKDivisorConfig(SCU_PCLK_Div2);   // set PCLK     (APB bus clock) divisor to 2 (half Reference Clock)
125
        SCU_RCLKDivisorConfig(SCU_RCLK_Div1);   // set RCLK     (Reference Clock) divisor to 1 (full PPL clock)
126
        SCU_HCLKDivisorConfig(SCU_HCLK_Div1);   // set HCLK     (AHB bus clock) divisor to 1 (full Reference Clock)
127
        SCU_PLLFactorsConfig(192,25,3);                 // PLL = 48 MHz, Feedback Divider N=192, Pre-Divider M=25, Post-Divider P=3
128
        #endif
129
        SCU_PLLCmd(ENABLE);                                     // Enable PLL (is disabled by SCU_PLLFactorsConfig)
130
        SCU_MCLKSourceConfig(SCU_MCLK_PLL);     // set master clock source to PLL
131
}
1 ingob 132
 
41 ingob 133
//----------------------------------------------------------------------------------------------------
134
void GetNaviCtrlVersion(void)
1 ingob 135
{
41 ingob 136
        u8 msg[25];
270 killagreg 137
 
530 holgerb 138
        sprintf(msg,"\n\r NaviCtrl HW: V%d.%d SW: V%d.%02d%c", UART_VersionInfo.HWMajor/10, UART_VersionInfo.HWMajor%10, VERSION_MAJOR,  VERSION_MINOR, 'a'+ VERSION_PATCH);
110 killagreg 139
        UART1_PutString(msg);
1 ingob 140
}
141
 
142
//----------------------------------------------------------------------------------------------------
231 holgerb 143
 
41 ingob 144
void CheckErrors(void)
145
{
516 holgerb 146
    static s32 no_error_delay = 0;
328 holgerb 147
        s32 newErrorCode = 0;
232 killagreg 148
        UART_VersionInfo.HardwareError[0] = 0;
231 holgerb 149
 
489 killagreg 150
        if(CheckDelay(I2CBus(Compass_I2CPort)->Timeout) || (Compass_Heading < 0)) DebugOut.StatusRed |= AMPEL_COMPASS;
338 holgerb 151
        else DebugOut.StatusRed &= ~AMPEL_COMPASS; // MK3Mag green status
231 holgerb 152
 
338 holgerb 153
        if((FC.Error[1] & FC_ERROR1_I2C) || (FC.Error[1] & FC_ERROR1_BL_MISSING)) DebugOut.StatusRed |= AMPEL_BL;
154
        else DebugOut.StatusRed &= ~AMPEL_BL; // BL-Ctrl green status
232 killagreg 155
 
338 holgerb 156
        if(UART_VersionInfo.HardwareError[0] || UART_VersionInfo.HardwareError[1]) DebugOut.StatusRed |= AMPEL_NC;
157
        else DebugOut.StatusRed &= ~AMPEL_NC;
158
 
483 holgerb 159
 
489 killagreg 160
        if(CheckDelay(I2CBus(Compass_I2CPort)->Timeout))
41 ingob 161
        {
489 killagreg 162
                LED_RED_ON;
472 holgerb 163
                sprintf(ErrorMSG,"no compass communica");
489 killagreg 164
                //Reset Compass communication
501 holgerb 165
                if(Compass_I2CPort == NCMAG_PORT_EXTERN) I2CBus_Init(I2C0);     else I2CBus_Init(I2C1);
489 killagreg 166
                Compass_Init();
472 holgerb 167
                newErrorCode = 4;
41 ingob 168
                StopNavigation = 1;
472 holgerb 169
                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_RX;
170
                DebugOut.StatusRed |= AMPEL_COMPASS;
41 ingob 171
        }
488 holgerb 172
        else if(CompassValueErrorCount > 30)
173
        {
174
                LED_RED_ON;
175
                sprintf(ErrorMSG,"compass sensor error");
176
                newErrorCode = 34;
177
                StopNavigation = 1;
178
                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_VALUE;
501 holgerb 179
                if(Compass_I2CPort == NCMAG_PORT_EXTERN) I2CBus_Init(I2C0);     else I2CBus_Init(I2C1);
489 killagreg 180
                Compass_Init();
488 holgerb 181
        }
489 killagreg 182
        else if((FCCalibActive || CompassCalState) && FC_Version.Hardware)
183
        {
488 holgerb 184
                sprintf(ErrorMSG,"Calibrate... ");
489 killagreg 185
                newErrorCode = 0;
488 holgerb 186
                ErrorCode = 0;
187
                no_error_delay = 1;
489 killagreg 188
        }
189
        else if(CheckDelay(SPI0_Timeout))
483 holgerb 190
        {
489 killagreg 191
                LED_RED_ON;
472 holgerb 192
                sprintf(ErrorMSG,"no FC communication ");
193
                newErrorCode = 3;
194
                StopNavigation = 1;
195
                DebugOut.StatusGreen &= ~AMPEL_FC; // status of FC Present
196
                DebugOut.StatusGreen &= ~AMPEL_BL; // status of BL Present
197
                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_SPI_RX;
198
        }
41 ingob 199
        else if(FC_Version.Compatible != FC_SPI_COMPATIBLE)
200
        {
201
                LED_RED_ON;
318 holgerb 202
#ifndef FOLLOW_ME
41 ingob 203
                sprintf(ErrorMSG,"FC not compatible ");
318 holgerb 204
#else
205
                sprintf(ErrorMSG,"! FollowMe only ! ");
206
#endif
328 holgerb 207
                newErrorCode = 1;
41 ingob 208
                StopNavigation = 1;
256 killagreg 209
                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_FC_INCOMPATIBLE;
338 holgerb 210
                DebugOut.StatusRed |= AMPEL_NC;
41 ingob 211
        }
255 killagreg 212
        else if(FC.Error[0] & FC_ERROR0_GYRO_NICK)
231 holgerb 213
        {
214
                LED_RED_ON;
215
                sprintf(ErrorMSG,"ERR: FC Nick Gyro");
328 holgerb 216
                newErrorCode = 10;
232 killagreg 217
        }
255 killagreg 218
        else if(FC.Error[0] & FC_ERROR0_GYRO_ROLL)
231 holgerb 219
        {
220
                LED_RED_ON;
221
                sprintf(ErrorMSG,"ERR: FC Roll Gyro");
328 holgerb 222
                newErrorCode = 11;
232 killagreg 223
        }
255 killagreg 224
        else if(FC.Error[0] & FC_ERROR0_GYRO_YAW)
231 holgerb 225
        {
226
                LED_RED_ON;
227
                sprintf(ErrorMSG,"ERR: FC Yaw Gyro");
328 holgerb 228
                newErrorCode = 12;
232 killagreg 229
        }
255 killagreg 230
        else if(FC.Error[0] & FC_ERROR0_ACC_NICK)
231 holgerb 231
        {
232
                LED_RED_ON;
233
                sprintf(ErrorMSG,"ERR: FC Nick ACC");
328 holgerb 234
                newErrorCode = 13;
232 killagreg 235
        }
255 killagreg 236
        else if(FC.Error[0] & FC_ERROR0_ACC_ROLL)
231 holgerb 237
        {
238
                LED_RED_ON;
239
                sprintf(ErrorMSG,"ERR: FC Roll ACC");
328 holgerb 240
                newErrorCode = 14;
232 killagreg 241
        }
255 killagreg 242
        else if(FC.Error[0] & FC_ERROR0_ACC_TOP)
231 holgerb 243
        {
244
                LED_RED_ON;
327 holgerb 245
                sprintf(ErrorMSG,"ERR:FC Z-ACC");
328 holgerb 246
                newErrorCode = 15;
232 killagreg 247
        }
351 holgerb 248
        else if(NC_To_FC_Flags & NC_TO_FC_FLYING_RANGE)
249
        {
250
                LED_RED_ON;
251
                sprintf(ErrorMSG,"ERR:Flying range!");
252
                newErrorCode = 28;
253
        }
255 killagreg 254
        else if(FC.Error[0] & FC_ERROR0_PRESSURE)
231 holgerb 255
        {
489 killagreg 256
                LED_RED_ON;
327 holgerb 257
                sprintf(ErrorMSG,"ERR:Pressure sensor");
328 holgerb 258
                newErrorCode = 16;
232 killagreg 259
        }
255 killagreg 260
        else if(FC.Error[1] &  FC_ERROR1_I2C)
231 holgerb 261
        {
262
                LED_RED_ON;
327 holgerb 263
                sprintf(ErrorMSG,"ERR:I2C FC to BL");
328 holgerb 264
                newErrorCode = 17;
232 killagreg 265
        }
255 killagreg 266
        else if(FC.Error[1] &  FC_ERROR1_BL_MISSING)
231 holgerb 267
        {
268
                LED_RED_ON;
269
                sprintf(ErrorMSG,"ERR: Bl Missing");
328 holgerb 270
                newErrorCode = 18;
232 killagreg 271
        }
255 killagreg 272
        else if(FC.Error[1] &  FC_ERROR1_MIXER)
231 holgerb 273
        {
274
                LED_RED_ON;
275
                sprintf(ErrorMSG,"Mixer Error");
328 holgerb 276
                newErrorCode = 19;
232 killagreg 277
        }
350 holgerb 278
        else if(CheckDelay(UBX_Timeout) && Parameter.GlobalConfig & FC_CFG_GPS_AKTIV)
41 ingob 279
        {
280
                LED_RED_ON;
548 holgerb 281
                sprintf(ErrorMSG,"no GPS communication");
282
                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_GPS_RX;
283
                UART_VersionInfo.Flags &= ~NC_VERSION_FLAG_GPS_PRESENT;
284
                newErrorCode = 5;
41 ingob 285
                StopNavigation = 1;
548 holgerb 286
//UBX_Setup();
287
//UBX_Timeout = SetDelay(500);
41 ingob 288
        }
426 holgerb 289
        else if(Compass_Heading < 0 && NCMAG_Present && !NCMAG_IsCalibrated)
290
        {
291
                LED_RED_ON;
292
                sprintf(ErrorMSG,"compass not calibr.");
293
                newErrorCode = 31;
294
                StopNavigation = 1;
295
                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_VALUE;
296
        }
254 killagreg 297
        else if(Compass_Heading < 0)
41 ingob 298
        {
299
                LED_RED_ON;
300
                sprintf(ErrorMSG,"bad compass value ");
328 holgerb 301
                newErrorCode = 6;
41 ingob 302
                StopNavigation = 1;
256 killagreg 303
                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_VALUE;
41 ingob 304
        }
255 killagreg 305
        else if((FC.Error[1] &  FC_ERROR1_SPI_RX))
199 killagreg 306
        {
307
                LED_RED_ON;
308
                sprintf(ErrorMSG,"FC spi rx error ");
328 holgerb 309
                newErrorCode = 8;
213 killagreg 310
                StopNavigation = 1;
199 killagreg 311
        }
255 killagreg 312
        else if(FC.Error[0] &  FC_ERROR0_CAREFREE)
231 holgerb 313
        {
314
                LED_RED_ON;
315
                sprintf(ErrorMSG,"FC: Carefree Error");
328 holgerb 316
                newErrorCode = 20;
232 killagreg 317
        }
255 killagreg 318
        else if(FC.Error[1] &  FC_ERROR1_PPM)
41 ingob 319
        {
320
                LED_RED_ON;
321
                sprintf(ErrorMSG,"RC Signal lost ");
328 holgerb 322
                newErrorCode = 7;
41 ingob 323
        }
489 killagreg 324
        else if(ErrorGpsFixLost)
328 holgerb 325
        {
489 killagreg 326
                LED_RED_ON;
327
                sprintf(ErrorMSG,"GPS Fix lost    ");
328
                newErrorCode = 21;
328 holgerb 329
        }
330 holgerb 330
        else if(ErrorDisturbedEarthMagnetField)
331
        {
489 killagreg 332
                LED_RED_ON;
333
                sprintf(ErrorMSG,"Magnet error    ");
334
                newErrorCode = 22;
335
                DebugOut.StatusRed |= AMPEL_COMPASS | AMPEL_NC;
336
                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_VALUE;
330 holgerb 337
        }
533 holgerb 338
        else if(((ErrorCheck_BL_MinOfMaxPWM == 40 && (TimeSinceMotorStart > 3))  || (ErrorCheck_BL_MinOfMaxPWM == 39)) && !ErrorCode)
338 holgerb 339
        {
489 killagreg 340
                LED_RED_ON;
341
                sprintf(ErrorMSG,"ERR:Motor restart  ");
342
                newErrorCode = 23;
343
                DebugOut.StatusRed |= AMPEL_BL;
338 holgerb 344
        }
472 holgerb 345
        else if(BL_MinOfMaxPWM < 30 && !ErrorCode)
338 holgerb 346
        {
489 killagreg 347
                u16 i;
516 holgerb 348
                for(i = 0; i < 12; i++) if(Motor[i].MaxPWM == BL_MinOfMaxPWM) break;
489 killagreg 349
                LED_RED_ON;
350
                sprintf(ErrorMSG,"ERR:BL%2d Test:%2d ",i+1,BL_MinOfMaxPWM);
516 holgerb 351
                newErrorCode = 32;
352
                DebugOut.StatusRed |= AMPEL_BL;
472 holgerb 353
        }
533 holgerb 354
        else if(ErrorCheck_BL_MinOfMaxPWM < 248 && (TimeSinceMotorStart > 3) && !ErrorCode)
472 holgerb 355
        {
489 killagreg 356
                LED_RED_ON;
357
                sprintf(ErrorMSG,"ERR:BL Limitation   ");
358
                newErrorCode = 24;
359
                DebugOut.StatusRed |= AMPEL_BL;
338 holgerb 360
        }
491 killagreg 361
        else if((NCFlags & NC_FLAG_RANGE_LIMIT) && (FC.StatusFlags & FC_STATUS_FLY) && !ErrorCode)
348 holgerb 362
        {
489 killagreg 363
                LED_RED_ON;
532 holgerb 364
                sprintf(ErrorMSG,"ERR:GPS WP range ");
489 killagreg 365
                newErrorCode = 25;
366
                DebugOut.StatusRed |= AMPEL_NC;
348 holgerb 367
        }
397 holgerb 368
        else if((!SD_SWITCH || (SDCardInfo.Valid == 0)) && Parameter.GlobalConfig3 & CFG3_NO_SDCARD_NO_START && !(FC.StatusFlags & FC_STATUS_FLY))
350 holgerb 369
        {
489 killagreg 370
                LED_RED_ON;
371
                sprintf(ErrorMSG,"ERR:No SD-Card  ");
372
                newErrorCode = 26;
373
                DebugOut.StatusRed |= AMPEL_NC;
350 holgerb 374
        }
383 holgerb 375
        else if((SD_LoggingError || (SD_WatchDog < 2000 && SD_WatchDog != 0)) && Parameter.GlobalConfig3 & CFG3_NO_SDCARD_NO_START)
351 holgerb 376
        {
489 killagreg 377
                LED_RED_ON;
378
                sprintf(ErrorMSG,"ERR:SD Logging abort");
379
                newErrorCode = 27;
380
                DebugOut.StatusRed |= AMPEL_NC;
381
                SD_LoggingError = 0;
351 holgerb 382
        }
355 holgerb 383
        else if(((AbsoluteFlyingAltitude) && (NaviData.Altimeter / 20 >= AbsoluteFlyingAltitude)) && (FC.StatusFlags & FC_STATUS_FLY))
384
        {
489 killagreg 385
                LED_RED_ON;
386
                sprintf(ErrorMSG,"ERR:Max Altitude ");
387
                newErrorCode = 29;
388
                DebugOut.StatusRed |= AMPEL_NC;
355 holgerb 389
        }
516 holgerb 390
        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)))
489 killagreg 391
        {
392
                LED_RED_ON;
393
                sprintf(ErrorMSG,"No GPS Fix      ");
394
                newErrorCode = 30;
398 holgerb 395
        }
615 holgerb 396
        else if(NC_GPS_ModeCharacter == 'F')
397
        {                                                                
398
                sprintf(ErrorMSG,"FAILSAFE pos.!  ");
399
                newErrorCode = 35;
400
        }
401
        else if(FC.StatusFlags3 & FC_STATUS3_REDUNDANCE_ERROR)
402
        {                                                                
403
                sprintf(ErrorMSG,"ERR:Redundancy  ");
404
                newErrorCode = 36;
405
        }
406
        else if(FC.StatusFlags3 & FC_STATUS3_REDUNDANCE_TEST)
407
        {                                                                
408
                sprintf(ErrorMSG,"Redundancy test ");
409
                newErrorCode = 37;
410
        }
41 ingob 411
        else // no error occured
412
        {
413
                StopNavigation = 0;
414
                LED_RED_OFF;
489 killagreg 415
                if(no_error_delay) { no_error_delay--;  }
416
                else
417
                {
418
                        sprintf(ErrorMSG,"No Error            ");
419
                        ErrorCode = 0;
420
                }
41 ingob 421
        }
328 holgerb 422
 
516 holgerb 423
    if(newErrorCode)
424
         {
425
          if(FC.StatusFlags & FC_STATUS_MOTOR_RUN) no_error_delay = 8; // delay the errors if the motors are running
426
          ErrorCode = newErrorCode;
427
         }
428
 FC.Error[0] = 0;
429
 FC.Error[1] = 0;
430
 FC.Error[2] = 0;
431
 FC.Error[3] = 0;
432
 FC.Error[4] = 0;
433
 ErrorGpsFixLost = 0;
530 holgerb 434
 ErrorCheck_BL_MinOfMaxPWM = BL_MinOfMaxPWM;
41 ingob 435
}
24 StephanB 436
 
378 holgerb 437
 
438
 
484 holgerb 439
u8 Polling(void)
378 holgerb 440
{
564 holgerb 441
        static u8 running = 0, oldFcFlags = 0, count5sec, TimeoutGPS_Process;
489 killagreg 442
        static u32 old_ms = 0;
443
 
516 holgerb 444
        if(running) {/*DebugOut.Analog[]++;*/ return(1);};
489 killagreg 445
        running = 1;
446
 
447
        if(CountMilliseconds != old_ms)  // 1 ms
448
        {
483 holgerb 449
                old_ms = CountMilliseconds;
450
                Compass_Update();               // update compass communication
451
                Analog_Update();                // get new ADC values
452
                CalcHeadFree();
564 holgerb 453
                if(!CheckDelay(SPI0_Timeout)) TimeoutGPS_Process = 0;
454
                else if(CountMilliseconds - SPI0_Timeout > 30000000L) SPI0_Timeout = CountMilliseconds; // avoid too long overflows
455
                if(++TimeoutGPS_Process >= 25)
456
                 {
457
                  GPS_Navigation(&GPSData, &(ToFlightCtrl.GPSStick)); // process the GPS data even if the FC is not connected
458
                  TimeoutGPS_Process = 0;
459
                 }
483 holgerb 460
        }
488 holgerb 461
 
489 killagreg 462
        SPI0_UpdateBuffer();    // also calls the GPS-functions
463
        UART0_ProcessRxData();  // GPS process request
464
        UART0_TransmitTxData(); // GPS send answer
465
        UART1_ProcessRxData();  // PC process request
466
        UART1_TransmitTxData(); // PC send answer
467
        UART2_TransmitTxData(); // FC send answer
468
 
533 holgerb 469
        if(!(FC.StatusFlags & FC_STATUS_MOTOR_RUN)) TimeSinceMotorStart = 0;
470
 
489 killagreg 471
        // ---------------- Error Check Timing ----------------------------
472
        if(CheckDelay(TimerCheckError) || (FC.StatusFlags & FC_STATUS_START && !(oldFcFlags & FC_STATUS_START))) // Timer or FY wants to start
473
        {
474
                if(CheckDelay(TimerCheckError))
380 holgerb 475
                {
489 killagreg 476
                        TimerCheckError = SetDelay(1000);
477
                        if(CompassValueErrorCount) CompassValueErrorCount--;
478
                        if(++count5sec == 5)
479
                        {
453 holgerb 480
                                count5sec = 0;
457 holgerb 481
                                FreqGpsProcessedIn5Sec = CountGpsProcessedIn5Sec * 2;
482
                                FreqNewGpsDataIn5Sec = CountNewGpsDataIn5Sec * 2;
489 killagreg 483
                                CountGpsProcessedIn5Sec = 0;
484
                                CountNewGpsDataIn5Sec = 0;
485
                        }
380 holgerb 486
                }
489 killagreg 487
                oldFcFlags = FC.StatusFlags;
564 holgerb 488
//              if(CheckDelay(SPI0_Timeout) && (DebugUART == UART1)) GPS_Navigation(&GPSData, &(ToFlightCtrl.GPSStick)); // process the GPS data even if the FC is not connected
489 killagreg 489
 
490
                if(!CheckDelay(SPI0_Timeout) || (DebugUART == UART1)) CheckErrors();
491
 
533 holgerb 492
                if(FC.StatusFlags & FC_STATUS_FLY)
493
                 {
494
                  NaviData.FlyingTime++; // we want to count the battery-time
495
                  TimeSinceMotorStart++;
496
                 }
497
 
489 killagreg 498
                if(SerialLinkOkay) SerialLinkOkay--;
594 holgerb 499
                if(SerialLinkOkay < 250 - 6) NCFlags |= NC_FLAG_NOSERIALLINK; // 6 seconds timeout for serial communication
489 killagreg 500
                else NCFlags &= ~NC_FLAG_NOSERIALLINK;
501
                if(StopNavigation && (Parameter.NaviGpsModeControl >=  50) && (Parameter.GlobalConfig & FC_CFG_GPS_AKTIV)) BeepTime = 1000;
502
        }
503
        running = 0;
514 holgerb 504
//      if(!PollingTimeout) DebugOut.Analog[17]++;
489 killagreg 505
        return(0);
378 holgerb 506
}
507
 
380 holgerb 508
// the handler will be cyclic called by the timer 1 ISR
509
// used is for critical timing parts that normaly would handled
510
// within the main loop that could block longer at logging activities
483 holgerb 511
void EXTIT3_IRQHandler(void)  // 1ms - Takt
154 killagreg 512
{
195 killagreg 513
        IENABLE;
154 killagreg 514
        VIC_ITCmd(EXTIT3_ITLine,DISABLE); // disable irq
489 killagreg 515
 
516 holgerb 516
        if(PollingTimeout == 0)
517
        {
518
                PollingTimeout = 5;
519
                //if(Polling() == 0) DebugOut.Analog[]++;
520
                Polling();
380 holgerb 521
        }
378 holgerb 522
 
523
        VIC_SWITCmd(EXTIT3_ITLine,DISABLE); // clear pending bit
195 killagreg 524
        VIC_ITCmd(EXTIT3_ITLine, ENABLE); // enable irq
525
        IDISABLE;
378 holgerb 526
        VIC1->VAR = 0xFF; // write any value to VIC0 Vector address register
154 killagreg 527
}
528
 
41 ingob 529
//----------------------------------------------------------------------------------------------------
530
int main(void)
531
{
489 killagreg 532
 
533
//      static u32 ftimer =0;
534
//      static u8 fstate = 0;
456 holgerb 535
//      static File_t* f = NULL;
489 killagreg 536
 
537
 
41 ingob 538
        /* Configure the system clocks */
539
        SCU_Config();
540
        /* init VIC (Vectored Interrupt Controller)     */
541
        SCU_AHBPeriphClockConfig(__VIC,ENABLE); // enable AHB bus clock for VIC
542
        SCU_AHBPeriphReset(__VIC, DISABLE);             // disable reset state for VIC
543
        VIC_DeInit();                                                   // deinitializes the VIC module registers to their default reset values.
196 killagreg 544
        VIC_InitDefaultVectors();
545
 
119 killagreg 546
        // initialize timer 1 for System Clock and delay rountines
41 ingob 547
        TIMER1_Init();
548
        // initialize the LEDs (needs Timer 1)
549
        Led_Init();
550
        // initialize the debug UART1
551
        UART1_Init();
110 killagreg 552
        UART1_PutString("\r\n---------------------------------------------");
120 killagreg 553
        // initialize timer 2 for servo outputs
180 killagreg 554
        //TIMER2_Init();
41 ingob 555
        // initialize UART2 to FLIGHTCTRL
556
        UART2_Init();
557
        // initialize UART0 (to MKGPS or MK3MAG)
558
        UART0_Init();
134 killagreg 559
        // initialize adc
560
        Analog_Init();
41 ingob 561
        // initialize SPI0 to FC
562
        SPI0_Init();
469 killagreg 563
        // initialize i2c busses (needs Timer 1)
489 killagreg 564
        I2CBus_Init(I2C0);
565
        I2CBus_Init(I2C1);
247 killagreg 566
        // initialize fat16 partition on sd card (needs Timer 1)
41 ingob 567
        Fat16_Init();
156 killagreg 568
        // initialize NC params
569
        NCParams_Init();
88 killagreg 570
        // initialize the settings
571
        Settings_Init();
92 killagreg 572
        // initialize logging (needs settings)
82 killagreg 573
        Logging_Init();
1 ingob 574
 
362 holgerb 575
        LED_GRN_ON;
41 ingob 576
        TimerCheckError = SetDelay(3000);
175 holgerb 577
        UART1_PutString("\r\n++++++++++++++++++++++++++++++++++++++++++");
110 killagreg 578
        UART1_PutString("\n\r Version information:");
116 killagreg 579
 
41 ingob 580
        GetNaviCtrlVersion();
338 holgerb 581
        DebugOut.StatusGreen = AMPEL_NC | AMPEL_COMPASS; // NC and MK3Mag
582
        DebugOut.StatusRed = 0x00;
400 holgerb 583
        UART1_PutString("\r\n++++++++++++++++++++++++++++++++++++++++++");
149 killagreg 584
 
516 holgerb 585
        Compass_Init();
270 killagreg 586
 
548 holgerb 587
        UBX_Setup(); // inits the GPS-Module via ubx
400 holgerb 588
        GPS_Init();
589
 
314 killagreg 590
#ifdef FOLLOW_ME
591
        TransmitAlsoToFC = 1;
592
        UART1_PutString("\r\n++++++++++++++++++++++++++++++++++++++++++");
593
        UART1_PutString("\n\r FOLLOW-ME Transmitter only!");
594
        UART1_PutString("\r\n++++++++++++++++++++++++++++++++++++++++++\r\n\r\n");
595
        TransmitAlsoToFC = 0;
596
#else
150 killagreg 597
        SPI0_GetFlightCtrlVersion();
598
        if(FC_Version.Compatible != FC_SPI_COMPATIBLE)
41 ingob 599
        {
461 holgerb 600
                UART1_PutString("\n\r Flight-Ctrl not compatible\n\r");
41 ingob 601
                LED_RED_ON;
602
        }
314 killagreg 603
#endif
528 holgerb 604
 
213 killagreg 605
        // ---------- Prepare the isr driven
154 killagreg 606
        // set to absolute lowest priority
516 holgerb 607
    VIC_Config(EXTIT3_ITLine, VIC_IRQ, PRIORITY_SW);
195 killagreg 608
        // enable interrupts
516 holgerb 609
    VIC_ITCmd(EXTIT3_ITLine, ENABLE);
244 killagreg 610
 
250 ingob 611
        Debug_OK("START");
362 holgerb 612
        UART1_PutString("\r\n");
380 holgerb 613
        fifo_purge(&UART1_rx_fifo); // flush the whole fifo init buffer
614
        LED_GRN_ON;
615
        LED_RED_OFF;
530 holgerb 616
    Settings_GetParamValue(PID_SEND_NMEA,(u16 *) &NMEA_Interval);
461 holgerb 617
        UART1_PutString("\r\n");
528 holgerb 618
 
487 holgerb 619
        CompassValueErrorCount = 0;
503 holgerb 620
        I2CBus(Compass_I2CPort)->Timeout = SetDelay(3000);
528 holgerb 621
// ++++++++++++++++++++++++++++++++++++++++++++++
154 killagreg 622
        for (;;) // the endless main loop
41 ingob 623
        {
484 holgerb 624
                PollingTimeout = 5;
431 killagreg 625
                Polling();
500 holgerb 626
// ++++++++++++++++++++++++++++++++++++++++++++++
627
                if(FromFC_LoadWP_List)
516 holgerb 628
                {
629
                   WPL_Store.Index = (FromFC_LoadWP_List & ~0x80);
630
                   if(WPL_Store.Index <= ToFC_MaxWpListIndex)
631
                   {
513 killagreg 632
                                if(PointList_ReadFromFile(&WPL_Store) == WPL_OK)
633
                                {
634
                                        if(FromFC_LoadWP_List & 0x80)// -> load relative
516 holgerb 635
                                        {
540 holgerb 636
                                         u32 angle;
637
                                         angle = (360 + GyroCompassCorrected/10 + Parameter.OrientationAngle * 15) % 360;
513 killagreg 638
                                                if(NCFlags & NC_FLAG_FREE || NaviData.TargetPositionDeviation.Distance > 7*10)
516 holgerb 639
                                        {  // take actual position
540 holgerb 640
                                                if(!PointList_Move(1,&(GPSData.Position),angle)) PointList_Clear();     // try to move wp-list so that 1st entry matches the current position 
516 holgerb 641
                                                }
642
                                        else
513 killagreg 643
                                                {  // take last target position
540 holgerb 644
                                                if(!PointList_Move(1, &(NaviData.TargetPosition),angle)) PointList_Clear();     // try to move wp-list so that 1st entry matches the current position 
513 killagreg 645
                                                }
646
                                        }
647
                                        if(FC.StatusFlags & FC_STATUS_FLY) PointList_WPActive(TRUE);
648
                                        GPS_pWaypoint = PointList_WPBegin(); // updates POI index
519 holgerb 649
                                        SpeakWaypointRached = 1;        // Speak once when the last Point is reached
650
                                        SpeakNextWaypoint = 1;          // Speak once as soon as the Points are active
651
                                        NCFlags &= ~NC_FLAG_TARGET_REACHED;
513 killagreg 652
                                        BeepTime = 150;
653
                                }
654
                        }
655
                        FromFC_LoadWP_List = 0;
656
                }
657
// ++++++++++++++++++++++++++++++++++++++++++++++
658
                if(FromFC_Save_SinglePoint)
659
                {
660
                        WPL_Store.Index = FromFC_Save_SinglePoint;
516 holgerb 661
                        if(WPL_Store.Index <= ToFC_MaxWpListIndex) if(PointList_SaveSinglePoint(&WPL_Store) == WPL_OK) BeepTime = 150;
513 killagreg 662
                        FromFC_Save_SinglePoint = 0;
663
                }
664
// ++++++++++++++++++++++++++++++++++++++++++++++
665
                if(FromFC_Load_SinglePoint)
666
                {
516 holgerb 667
                   WPL_Store.Index = FromFC_Load_SinglePoint;
668
                   if(WPL_Store.Index <= ToFC_MaxWpListIndex)
669
                   {
513 killagreg 670
                        if(PointList_LoadSinglePoint(&WPL_Store) == WPL_OK)
502 killagreg 671
                        {
672
                                if(FC.StatusFlags & FC_STATUS_FLY) PointList_WPActive(TRUE);
673
                                GPS_pWaypoint = PointList_WPBegin(); // updates POI index
519 holgerb 674
                                SpeakWaypointRached = 1;        // Speak once when the last Point is reached
675
                                SpeakNextWaypoint = 1;          // Speak once as soon as the Points are active
676
                                NCFlags &= ~NC_FLAG_TARGET_REACHED;
502 killagreg 677
                                BeepTime = 150;
678
                        }
516 holgerb 679
                   }
513 killagreg 680
                        FromFC_Load_SinglePoint = 0;
504 holgerb 681
                }
500 holgerb 682
// ++++++++++++++++++++++++++++++++++++++++++++++
431 killagreg 683
                // ---------------- Logging  ---------------------------------------
489 killagreg 684
                if(SD_WatchDog)
431 killagreg 685
                {
686
                        SD_WatchDog = 30000;
687
                        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
516 holgerb 688
                        else
689
                        {
690
                         ToFC_MaxWpListIndex = 0;
691
                         if(FC.StatusFlags & FC_STATUS_START) SD_LoggingError = 100;
503 holgerb 692
                        }
431 killagreg 693
                        if(!SD_WatchDog) UART1_PutString("\n\rSD-Watchdog - Logging aborted\n\r");
694
                }
489 killagreg 695
 
696
/*
324 killagreg 697
                if(CheckDelay(ftimer))
698
                {
699
 
431 killagreg 700
                        static s8 filename[35];
701
                        static u8 i = 0;
702
                        s8 dbgmsg[40];
324 killagreg 703
 
489 killagreg 704
 
705
 
324 killagreg 706
                        ftimer = SetDelay(100);
707
                        if(FC.Poti[3]>100 && fstate == 0)
708
                        {
709
                                fstate = 1;
431 killagreg 710
                                sprintf(filename, "/toast/toasta/toast%02i.txt",i++);
324 killagreg 711
                        }
712
                        else if(FC.Poti[3]<100 && fstate == 2)
713
                        {
714
                                fstate = 3;
715
                        }
716
 
717
                        switch(fstate)
718
                        {
719
                                case 1:
431 killagreg 720
                                        sprintf(dbgmsg,"\r\nStart writing file: %s", filename);
721
                                        Debug(dbgmsg);
324 killagreg 722
                                        f = fopen_(filename, 'a');
723
                                        if(f== NULL) Fat16_Init();
724
                                        fstate = 2;
725
                                        break;
726
 
727
                                case 2:
728
                                        fputs_("\r\ntest edins sdv dsivbds iv dsivb disbv idsv bisd bv d suiv dsibsivbdis fbvisdöb visdbvisdb vidbfibds ibv", f);
729
                                        break;
489 killagreg 730
 
324 killagreg 731
                                case 3:
431 killagreg 732
                                        sprintf(dbgmsg,"\r\nClosing file: %s", filename);
733
                                        Debug(dbgmsg);
324 killagreg 734
                                        fclose_(f);
735
                                        fstate = 0;
736
                                        break;
737
 
738
                                default:
739
                                        break;
740
                        }
741
                }
594 holgerb 742
*/                                                       
24 StephanB 743
        }
1 ingob 744
}
516 holgerb 745
//DebugOut.Analog[]