Subversion Repositories NaviCtrl

Rev

Rev 469 | Rev 472 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 469 Rev 470
1
/*#######################################################################################*/
1
/*#######################################################################################*/
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
3
/*#######################################################################################*/
3
/*#######################################################################################*/
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// + www.MikroKopter.com
5
// + www.MikroKopter.com
6
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
7
// + Software Nutzungsbedingungen (english version: see below)
7
// + Software Nutzungsbedingungen (english version: see below)
8
// + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt -
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
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 
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.
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.
12
// + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig.
13
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
13
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
14
// + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im
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.
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
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.
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
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.
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
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 
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.
22
// + des Mitverschuldens offen.
23
// + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet.
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.
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.
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
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.
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.
28
// + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software.
29
// + #### ENDE DER NUTZUNGSBEDINGUNGEN ####'
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.
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.
31
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
32
// + Software LICENSING TERMS
32
// + Software LICENSING TERMS
33
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
33
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
34
// + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor -
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 
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*.
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.
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
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
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.
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
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.
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
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
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
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
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.
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
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.
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
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.
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.
52
// + *) The territory aspect only refers to the place where the Software is used, not its programmed range.
53
// + #### END OF LICENSING TERMS ####
53
// + #### END OF LICENSING TERMS ####
54
// + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de.
54
// + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de.
55
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
55
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
56
//#define MCLK96MHZ
56
//#define MCLK96MHZ
57
const unsigned long _Main_Crystal = 25000;
57
const unsigned long _Main_Crystal = 25000;
58
#include <stdio.h>
58
#include <stdio.h>
59
#include "91x_lib.h"
59
#include "91x_lib.h"
60
#include "led.h"
60
#include "led.h"
61
#include "uart0.h"
61
#include "uart0.h"
62
#include "uart1.h"
62
#include "uart1.h"
63
#include "uart2.h"
63
#include "uart2.h"
64
#include "gps.h"
64
#include "gps.h"
-
 
65
#include "i2c0.h"
65
#include "i2c.h"
66
#include "i2c1.h"       
66
#include "compass.h"
67
#include "compass.h"
67
#include "ncmag.h"
68
#include "ncmag.h"
68
#include "timer1.h"
69
#include "timer1.h"
69
#include "timer2.h"
70
#include "timer2.h"
70
#include "analog.h"
71
#include "analog.h"
71
#include "spi_slave.h"
72
#include "spi_slave.h"
72
#include "fat16.h"
73
#include "fat16.h"
73
#include "usb.h"
-
 
74
#include "sdc.h"
74
#include "sdc.h"
75
#include "logging.h"
75
#include "logging.h"
76
#include "params.h"
76
#include "params.h"
77
#include "settings.h"
77
#include "settings.h"
78
#include "config.h"
78
#include "config.h"
79
#include "main.h"
79
#include "main.h"
80
#include "debug.h"
80
#include "debug.h"
81
#include "eeprom.h"
81
#include "eeprom.h"
82
#include "ssc.h"
82
#include "ssc.h"
83
#include "sdc.h"
83
#include "sdc.h"
84
#include "uart1.h"
84
#include "uart1.h"
85
 
85
 
86
 
86
 
87
 
87
 
88
#ifdef FOLLOW_ME
88
#ifdef FOLLOW_ME
89
u8 TransmitAlsoToFC = 0;
89
u8 TransmitAlsoToFC = 0;
90
#endif
90
#endif
91
u32 TimerCheckError;
91
u32 TimerCheckError;
92
u8 ErrorCode = 0;                        
92
u8 ErrorCode = 0;                        
93
u16 BeepTime;
93
u16 BeepTime;
94
u8  NCFlags = 0;
94
u8  NCFlags = 0;
95
s16 GeoMagDec = 0; // local magnetic declination in 0.1 deg
95
s16 GeoMagDec = 0; // local magnetic declination in 0.1 deg
96
u8 ErrorGpsFixLost = 0;
96
u8 ErrorGpsFixLost = 0;
97
 
97
 
98
u8 ClearFCStatusFlags = 0;
98
u8 ClearFCStatusFlags = 0;
99
u8 StopNavigation = 0;
99
u8 StopNavigation = 0;
100
volatile u32 PollingTimeout = 10000;
100
volatile u32 PollingTimeout = 10000;
101
Param_t Parameter;
101
Param_t Parameter;
102
volatile FC_t FC;
102
volatile FC_t FC;
103
volatile u32 SPIWatchDog = 15000; // stop Navigation if this goes to zero
103
volatile u32 SPIWatchDog = 15000; // stop Navigation if this goes to zero
104
volatile u32 SD_WatchDog = 15000; // stop Logging if this goes to zero
104
volatile u32 SD_WatchDog = 15000; // stop Logging if this goes to zero
105
u32 CountGpsProcessedIn5Sec = 0,CountNewGpsDataIn5Sec = 0, FreqGpsProcessedIn5Sec = 0, FreqNewGpsDataIn5Sec = 0;
105
u32 CountGpsProcessedIn5Sec = 0,CountNewGpsDataIn5Sec = 0, FreqGpsProcessedIn5Sec = 0, FreqNewGpsDataIn5Sec = 0;
106
 
106
 
107
s8 ErrorMSG[25];
107
s8 ErrorMSG[25];
108
 
108
 
109
//----------------------------------------------------------------------------------------------------
109
//----------------------------------------------------------------------------------------------------
110
void SCU_Config(void)
110
void SCU_Config(void)
111
{
111
{
112
        /* configure PLL and set it as master clock source */
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
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
114
        SCU_PLLCmd(DISABLE);                                    // now disable the PLL
115
        #ifdef MCLK96MHZ
115
        #ifdef MCLK96MHZ
116
        SCU_BRCLKDivisorConfig(SCU_BRCLK_Div2); // set BRCLK to MCLK/2 = 48MHz
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)
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)
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)
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
120
        SCU_PLLFactorsConfig(192,25,2);                 // PLL = 96 MHz, Feedback Divider N=192, Pre-Divider M=25, Post-Divider P=2
121
        #else
121
        #else
122
        SCU_BRCLKDivisorConfig(SCU_BRCLK_Div1); // set BRCLK to MCLK = 48MHz
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)
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)
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)
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
126
        SCU_PLLFactorsConfig(192,25,3);                 // PLL = 48 MHz, Feedback Divider N=192, Pre-Divider M=25, Post-Divider P=3
127
        #endif
127
        #endif
128
        SCU_PLLCmd(ENABLE);                                     // Enable PLL (is disabled by SCU_PLLFactorsConfig)
128
        SCU_PLLCmd(ENABLE);                                     // Enable PLL (is disabled by SCU_PLLFactorsConfig)
129
        SCU_MCLKSourceConfig(SCU_MCLK_PLL);     // set master clock source to PLL
129
        SCU_MCLKSourceConfig(SCU_MCLK_PLL);     // set master clock source to PLL
130
}
130
}
131
 
131
 
132
//----------------------------------------------------------------------------------------------------
132
//----------------------------------------------------------------------------------------------------
133
void GetNaviCtrlVersion(void)
133
void GetNaviCtrlVersion(void)
134
{
134
{
135
        u8 msg[25];
135
        u8 msg[25];
136
 
136
 
137
        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);
137
        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);
138
        UART1_PutString(msg);
138
        UART1_PutString(msg);
139
}
139
}
140
 
140
 
141
//----------------------------------------------------------------------------------------------------
141
//----------------------------------------------------------------------------------------------------
142
 
142
 
143
void CheckErrors(void)
143
void CheckErrors(void)
144
{
144
{
145
    static s32 no_error_delay = 0;
145
    static s32 no_error_delay = 0;
146
        s32 newErrorCode = 0;
146
        s32 newErrorCode = 0;
147
        UART_VersionInfo.HardwareError[0] = 0;
147
        UART_VersionInfo.HardwareError[0] = 0;
148
 
148
 
149
        //ToDo
149
        //ToDo
150
        if(CheckDelay(I2C1_Timeout) || (Compass_Heading < 0)) DebugOut.StatusRed |= AMPEL_COMPASS;
150
        if(CheckDelay(I2C1_Timeout) || (Compass_Heading < 0)) DebugOut.StatusRed |= AMPEL_COMPASS;
151
        else DebugOut.StatusRed &= ~AMPEL_COMPASS; // MK3Mag green status
151
        else DebugOut.StatusRed &= ~AMPEL_COMPASS; // MK3Mag green status
152
 
152
 
153
        if((FC.Error[1] & FC_ERROR1_I2C) || (FC.Error[1] & FC_ERROR1_BL_MISSING)) DebugOut.StatusRed |= AMPEL_BL;
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
154
        else DebugOut.StatusRed &= ~AMPEL_BL; // BL-Ctrl green status
155
 
155
 
156
        if(UART_VersionInfo.HardwareError[0] || UART_VersionInfo.HardwareError[1]) DebugOut.StatusRed |= AMPEL_NC;
156
        if(UART_VersionInfo.HardwareError[0] || UART_VersionInfo.HardwareError[1]) DebugOut.StatusRed |= AMPEL_NC;
157
        else DebugOut.StatusRed &= ~AMPEL_NC;
157
        else DebugOut.StatusRed &= ~AMPEL_NC;
158
 
158
 
159
    if((FCCalibActive || CompassCalState) && FC_Version.Hardware)
159
    if((FCCalibActive || CompassCalState) && FC_Version.Hardware)
160
     {
160
     {
161
                sprintf(ErrorMSG,"Calibrate... ");
161
                sprintf(ErrorMSG,"Calibrate... ");
162
                newErrorCode = 0;
162
                newErrorCode = 0;
163
                ErrorCode = 0;
163
                ErrorCode = 0;
164
                no_error_delay = 1;
164
                no_error_delay = 1;
165
         }
165
         }
166
        else
166
        else
167
        if(CheckDelay(SPI0_Timeout))
167
        if(CheckDelay(SPI0_Timeout))
168
        {
168
        {
169
                LED_RED_ON;              
169
                LED_RED_ON;              
170
                sprintf(ErrorMSG,"no FC communication ");
170
                sprintf(ErrorMSG,"no FC communication ");
171
                newErrorCode = 3;
171
                newErrorCode = 3;
172
                StopNavigation = 1;
172
                StopNavigation = 1;
173
                DebugOut.StatusGreen &= ~AMPEL_FC; // status of FC Present
173
                DebugOut.StatusGreen &= ~AMPEL_FC; // status of FC Present
174
                DebugOut.StatusGreen &= ~AMPEL_BL; // status of BL Present
174
                DebugOut.StatusGreen &= ~AMPEL_BL; // status of BL Present
175
                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_SPI_RX;
175
                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_SPI_RX;
176
        }
176
        }
177
        //ToDo
177
        //ToDo
178
        else if(CheckDelay(I2C1_Timeout))
178
        else if(CheckDelay(I2C1_Timeout))
179
        {
179
        {
180
                LED_RED_ON;              
180
                LED_RED_ON;              
181
                sprintf(ErrorMSG,"no compass communica");
181
                sprintf(ErrorMSG,"no compass communica");
182
                //Reset I2CBus
182
                //Reset I2CBus
183
                I2C1_Deinit();
183
                I2C1_Deinit();
184
                I2C1_Init();
184
                I2C1_Init();
185
                newErrorCode = 4;
185
                newErrorCode = 4;
186
                StopNavigation = 1;
186
                StopNavigation = 1;
187
                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_RX;
187
                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_RX;
188
                DebugOut.StatusRed |= AMPEL_COMPASS;
188
                DebugOut.StatusRed |= AMPEL_COMPASS;
189
        }
189
        }
190
        else if(FC_Version.Compatible != FC_SPI_COMPATIBLE)
190
        else if(FC_Version.Compatible != FC_SPI_COMPATIBLE)
191
        {
191
        {
192
                LED_RED_ON;
192
                LED_RED_ON;
193
#ifndef FOLLOW_ME
193
#ifndef FOLLOW_ME
194
                sprintf(ErrorMSG,"FC not compatible ");
194
                sprintf(ErrorMSG,"FC not compatible ");
195
#else
195
#else
196
                sprintf(ErrorMSG,"! FollowMe only ! ");
196
                sprintf(ErrorMSG,"! FollowMe only ! ");
197
#endif
197
#endif
198
                newErrorCode = 1;
198
                newErrorCode = 1;
199
                StopNavigation = 1;
199
                StopNavigation = 1;
200
                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_FC_INCOMPATIBLE;
200
                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_FC_INCOMPATIBLE;
201
                DebugOut.StatusRed |= AMPEL_NC;
201
                DebugOut.StatusRed |= AMPEL_NC;
202
        }
202
        }
203
 
203
 
204
        else if(FC.Error[0] & FC_ERROR0_GYRO_NICK)
204
        else if(FC.Error[0] & FC_ERROR0_GYRO_NICK)
205
        {
205
        {
206
                LED_RED_ON;
206
                LED_RED_ON;
207
                sprintf(ErrorMSG,"ERR: FC Nick Gyro");
207
                sprintf(ErrorMSG,"ERR: FC Nick Gyro");
208
                newErrorCode = 10;
208
                newErrorCode = 10;
209
        }
209
        }
210
        else if(FC.Error[0] & FC_ERROR0_GYRO_ROLL)
210
        else if(FC.Error[0] & FC_ERROR0_GYRO_ROLL)
211
        {
211
        {
212
                LED_RED_ON;
212
                LED_RED_ON;
213
                sprintf(ErrorMSG,"ERR: FC Roll Gyro");
213
                sprintf(ErrorMSG,"ERR: FC Roll Gyro");
214
                newErrorCode = 11;
214
                newErrorCode = 11;
215
        }
215
        }
216
        else if(FC.Error[0] & FC_ERROR0_GYRO_YAW)
216
        else if(FC.Error[0] & FC_ERROR0_GYRO_YAW)
217
        {
217
        {
218
                LED_RED_ON;
218
                LED_RED_ON;
219
                sprintf(ErrorMSG,"ERR: FC Yaw Gyro");
219
                sprintf(ErrorMSG,"ERR: FC Yaw Gyro");
220
                newErrorCode = 12;
220
                newErrorCode = 12;
221
        }
221
        }
222
        else if(FC.Error[0] & FC_ERROR0_ACC_NICK)
222
        else if(FC.Error[0] & FC_ERROR0_ACC_NICK)
223
        {
223
        {
224
                LED_RED_ON;
224
                LED_RED_ON;
225
                sprintf(ErrorMSG,"ERR: FC Nick ACC");
225
                sprintf(ErrorMSG,"ERR: FC Nick ACC");
226
                newErrorCode = 13;
226
                newErrorCode = 13;
227
        }
227
        }
228
        else if(FC.Error[0] & FC_ERROR0_ACC_ROLL)
228
        else if(FC.Error[0] & FC_ERROR0_ACC_ROLL)
229
        {
229
        {
230
                LED_RED_ON;
230
                LED_RED_ON;
231
                sprintf(ErrorMSG,"ERR: FC Roll ACC");
231
                sprintf(ErrorMSG,"ERR: FC Roll ACC");
232
                newErrorCode = 14;
232
                newErrorCode = 14;
233
        }
233
        }
234
        else if(FC.Error[0] & FC_ERROR0_ACC_TOP)
234
        else if(FC.Error[0] & FC_ERROR0_ACC_TOP)
235
        {
235
        {
236
                LED_RED_ON;
236
                LED_RED_ON;
237
                sprintf(ErrorMSG,"ERR:FC Z-ACC");
237
                sprintf(ErrorMSG,"ERR:FC Z-ACC");
238
                newErrorCode = 15;
238
                newErrorCode = 15;
239
        }
239
        }
240
        else if(NC_To_FC_Flags & NC_TO_FC_FLYING_RANGE)
240
        else if(NC_To_FC_Flags & NC_TO_FC_FLYING_RANGE)
241
        {
241
        {
242
                LED_RED_ON;
242
                LED_RED_ON;
243
                sprintf(ErrorMSG,"ERR:Flying range!");
243
                sprintf(ErrorMSG,"ERR:Flying range!");
244
                newErrorCode = 28;
244
                newErrorCode = 28;
245
        }
245
        }
246
        else if(FC.Error[0] & FC_ERROR0_PRESSURE)
246
        else if(FC.Error[0] & FC_ERROR0_PRESSURE)
247
        {
247
        {
248
                LED_RED_ON;              
248
                LED_RED_ON;              
249
                sprintf(ErrorMSG,"ERR:Pressure sensor");
249
                sprintf(ErrorMSG,"ERR:Pressure sensor");
250
                newErrorCode = 16;
250
                newErrorCode = 16;
251
        }
251
        }
252
        else if(FC.Error[1] &  FC_ERROR1_I2C)
252
        else if(FC.Error[1] &  FC_ERROR1_I2C)
253
        {
253
        {
254
                LED_RED_ON;
254
                LED_RED_ON;
255
                sprintf(ErrorMSG,"ERR:I2C FC to BL");
255
                sprintf(ErrorMSG,"ERR:I2C FC to BL");
256
                newErrorCode = 17;
256
                newErrorCode = 17;
257
        }
257
        }
258
        else if(FC.Error[1] &  FC_ERROR1_BL_MISSING)
258
        else if(FC.Error[1] &  FC_ERROR1_BL_MISSING)
259
        {
259
        {
260
                LED_RED_ON;
260
                LED_RED_ON;
261
                sprintf(ErrorMSG,"ERR: Bl Missing");
261
                sprintf(ErrorMSG,"ERR: Bl Missing");
262
                newErrorCode = 18;
262
                newErrorCode = 18;
263
        }
263
        }
264
        else if(FC.Error[1] &  FC_ERROR1_MIXER)
264
        else if(FC.Error[1] &  FC_ERROR1_MIXER)
265
        {
265
        {
266
                LED_RED_ON;
266
                LED_RED_ON;
267
                sprintf(ErrorMSG,"Mixer Error");
267
                sprintf(ErrorMSG,"Mixer Error");
268
                newErrorCode = 19;
268
                newErrorCode = 19;
269
        }
269
        }
270
        else if(CheckDelay(UBX_Timeout) && Parameter.GlobalConfig & FC_CFG_GPS_AKTIV)
270
        else if(CheckDelay(UBX_Timeout) && Parameter.GlobalConfig & FC_CFG_GPS_AKTIV)
271
        {
271
        {
272
                LED_RED_ON;
272
                LED_RED_ON;
273
//      if(!(Parameter.GlobalConfig & FC_CFG_GPS_AKTIV)) sprintf(ErrorMSG,"GPS disconnected ");
273
//      if(!(Parameter.GlobalConfig & FC_CFG_GPS_AKTIV)) sprintf(ErrorMSG,"GPS disconnected ");
274
//              else 
274
//              else 
275
                 {                                     
275
                 {                                     
276
                  sprintf(ErrorMSG,"no GPS communication");
276
                  sprintf(ErrorMSG,"no GPS communication");
277
                  UART_VersionInfo.HardwareError[0] |= NC_ERROR0_GPS_RX;
277
                  UART_VersionInfo.HardwareError[0] |= NC_ERROR0_GPS_RX;
278
                  UART_VersionInfo.Flags &= ~NC_VERSION_FLAG_GPS_PRESENT;
278
                  UART_VersionInfo.Flags &= ~NC_VERSION_FLAG_GPS_PRESENT;
279
              newErrorCode = 5;
279
              newErrorCode = 5;
280
                 }
280
                 }
281
                StopNavigation = 1;
281
                StopNavigation = 1;
282
//              UBX_Timeout = SetDelay(500);
282
//              UBX_Timeout = SetDelay(500);
283
        }
283
        }
284
        else if(Compass_Heading < 0 && NCMAG_Present && !NCMAG_IsCalibrated)
284
        else if(Compass_Heading < 0 && NCMAG_Present && !NCMAG_IsCalibrated)
285
        {
285
        {
286
                LED_RED_ON;
286
                LED_RED_ON;
287
                sprintf(ErrorMSG,"compass not calibr.");
287
                sprintf(ErrorMSG,"compass not calibr.");
288
                newErrorCode = 31;
288
                newErrorCode = 31;
289
                StopNavigation = 1;
289
                StopNavigation = 1;
290
                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_VALUE;
290
                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_VALUE;
291
        }
291
        }
292
        else if(Compass_Heading < 0)
292
        else if(Compass_Heading < 0)
293
        {
293
        {
294
                LED_RED_ON;
294
                LED_RED_ON;
295
                sprintf(ErrorMSG,"bad compass value ");
295
                sprintf(ErrorMSG,"bad compass value ");
296
                newErrorCode = 6;
296
                newErrorCode = 6;
297
                StopNavigation = 1;
297
                StopNavigation = 1;
298
                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_VALUE;
298
                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_VALUE;
299
        }
299
        }
300
        else if((FC.Error[1] &  FC_ERROR1_SPI_RX))
300
        else if((FC.Error[1] &  FC_ERROR1_SPI_RX))
301
        {
301
        {
302
                LED_RED_ON;
302
                LED_RED_ON;
303
                sprintf(ErrorMSG,"FC spi rx error ");
303
                sprintf(ErrorMSG,"FC spi rx error ");
304
                newErrorCode = 8;
304
                newErrorCode = 8;
305
                StopNavigation = 1;
305
                StopNavigation = 1;
306
        }
306
        }
307
        else if(FC.Error[0] &  FC_ERROR0_CAREFREE)
307
        else if(FC.Error[0] &  FC_ERROR0_CAREFREE)
308
        {
308
        {
309
                LED_RED_ON;
309
                LED_RED_ON;
310
                sprintf(ErrorMSG,"FC: Carefree Error");
310
                sprintf(ErrorMSG,"FC: Carefree Error");
311
                newErrorCode = 20;
311
                newErrorCode = 20;
312
        }
312
        }
313
        else if(FC.Error[1] &  FC_ERROR1_PPM)
313
        else if(FC.Error[1] &  FC_ERROR1_PPM)
314
        {
314
        {
315
                LED_RED_ON;
315
                LED_RED_ON;
316
                sprintf(ErrorMSG,"RC Signal lost ");
316
                sprintf(ErrorMSG,"RC Signal lost ");
317
                newErrorCode = 7;
317
                newErrorCode = 7;
318
        }
318
        }
319
        else if(ErrorGpsFixLost)
319
        else if(ErrorGpsFixLost)
320
        {
320
        {
321
         LED_RED_ON;
321
         LED_RED_ON;
322
         sprintf(ErrorMSG,"GPS Fix lost    ");
322
         sprintf(ErrorMSG,"GPS Fix lost    ");
323
         newErrorCode = 21;
323
         newErrorCode = 21;
324
        }
324
        }
325
        else if(ErrorDisturbedEarthMagnetField)
325
        else if(ErrorDisturbedEarthMagnetField)
326
        {
326
        {
327
         LED_RED_ON;
327
         LED_RED_ON;
328
         sprintf(ErrorMSG,"Magnet error    ");
328
         sprintf(ErrorMSG,"Magnet error    ");
329
         newErrorCode = 22;
329
         newErrorCode = 22;
330
         DebugOut.StatusRed |= AMPEL_COMPASS | AMPEL_NC;
330
         DebugOut.StatusRed |= AMPEL_COMPASS | AMPEL_NC;
331
         UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_VALUE;
331
         UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_VALUE;
332
        }
332
        }
333
        else if(BL_MinOfMaxPWM == 40 && (FC.StatusFlags & FC_STATUS_FLY) && !ErrorCode)
333
        else if(BL_MinOfMaxPWM == 40 && (FC.StatusFlags & FC_STATUS_FLY) && !ErrorCode)
334
        {
334
        {
335
         LED_RED_ON;
335
         LED_RED_ON;
336
         sprintf(ErrorMSG,"ERR:Motor restart  ");
336
         sprintf(ErrorMSG,"ERR:Motor restart  ");
337
         newErrorCode = 23;
337
         newErrorCode = 23;
338
         DebugOut.StatusRed |= AMPEL_BL;
338
         DebugOut.StatusRed |= AMPEL_BL;
339
        }
339
        }
340
        else if(BL_MinOfMaxPWM != 255 && (BL_MinOfMaxPWM != 250) && (FC.StatusFlags & FC_STATUS_FLY) && !ErrorCode)
340
        else if(BL_MinOfMaxPWM != 255 && (BL_MinOfMaxPWM != 250) && (FC.StatusFlags & FC_STATUS_FLY) && !ErrorCode)
341
        {
341
        {
342
         LED_RED_ON;
342
         LED_RED_ON;
343
         sprintf(ErrorMSG,"ERR:BL Limitation   ");
343
         sprintf(ErrorMSG,"ERR:BL Limitation   ");
344
         newErrorCode = 24;
344
         newErrorCode = 24;
345
         DebugOut.StatusRed |= AMPEL_BL;
345
         DebugOut.StatusRed |= AMPEL_BL;
346
        }
346
        }
347
        else if(NCFlags & NC_FLAG_RANGE_LIMIT && (FC.StatusFlags & FC_STATUS_FLY) && !ErrorCode)
347
        else if(NCFlags & NC_FLAG_RANGE_LIMIT && (FC.StatusFlags & FC_STATUS_FLY) && !ErrorCode)
348
        {
348
        {
349
         LED_RED_ON;
349
         LED_RED_ON;
350
         sprintf(ErrorMSG,"ERR:GPS range  ");
350
         sprintf(ErrorMSG,"ERR:GPS range  ");
351
         newErrorCode = 25;
351
         newErrorCode = 25;
352
         DebugOut.StatusRed |= AMPEL_NC;
352
         DebugOut.StatusRed |= AMPEL_NC;
353
        }
353
        }
354
        else if((!SD_SWITCH || (SDCardInfo.Valid == 0)) && Parameter.GlobalConfig3 & CFG3_NO_SDCARD_NO_START && !(FC.StatusFlags & FC_STATUS_FLY))
354
        else if((!SD_SWITCH || (SDCardInfo.Valid == 0)) && Parameter.GlobalConfig3 & CFG3_NO_SDCARD_NO_START && !(FC.StatusFlags & FC_STATUS_FLY))
355
        {
355
        {
356
         LED_RED_ON;
356
         LED_RED_ON;
357
         sprintf(ErrorMSG,"ERR:No SD-Card  ");
357
         sprintf(ErrorMSG,"ERR:No SD-Card  ");
358
         newErrorCode = 26;
358
         newErrorCode = 26;
359
         DebugOut.StatusRed |= AMPEL_NC;
359
         DebugOut.StatusRed |= AMPEL_NC;
360
        }
360
        }
361
        else if((SD_LoggingError || (SD_WatchDog < 2000 && SD_WatchDog != 0)) && Parameter.GlobalConfig3 & CFG3_NO_SDCARD_NO_START)
361
        else if((SD_LoggingError || (SD_WatchDog < 2000 && SD_WatchDog != 0)) && Parameter.GlobalConfig3 & CFG3_NO_SDCARD_NO_START)
362
        {
362
        {
363
         LED_RED_ON;       
363
         LED_RED_ON;       
364
         sprintf(ErrorMSG,"ERR:SD Logging abort");
364
         sprintf(ErrorMSG,"ERR:SD Logging abort");
365
         newErrorCode = 27;
365
         newErrorCode = 27;
366
         DebugOut.StatusRed |= AMPEL_NC;
366
         DebugOut.StatusRed |= AMPEL_NC;
367
         SD_LoggingError = 0;
367
         SD_LoggingError = 0;
368
        }
368
        }
369
        else if(((AbsoluteFlyingAltitude) && (NaviData.Altimeter / 20 >= AbsoluteFlyingAltitude)) && (FC.StatusFlags & FC_STATUS_FLY))
369
        else if(((AbsoluteFlyingAltitude) && (NaviData.Altimeter / 20 >= AbsoluteFlyingAltitude)) && (FC.StatusFlags & FC_STATUS_FLY))
370
        {
370
        {
371
         LED_RED_ON;
371
         LED_RED_ON;
372
         sprintf(ErrorMSG,"ERR:Max Altitude ");
372
         sprintf(ErrorMSG,"ERR:Max Altitude ");
373
         newErrorCode = 29;
373
         newErrorCode = 29;
374
         DebugOut.StatusRed |= AMPEL_NC;
374
         DebugOut.StatusRed |= AMPEL_NC;
375
        }
375
        }
376
        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)))
376
        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)))
377
        {                                                                                                                                                  
377
        {                                                                                                                                                  
378
         LED_RED_ON;
378
         LED_RED_ON;
379
         sprintf(ErrorMSG,"No GPS Fix      ");
379
         sprintf(ErrorMSG,"No GPS Fix      ");
380
         newErrorCode = 30;
380
         newErrorCode = 30;
381
        }
381
        }
382
        else // no error occured
382
        else // no error occured
383
        {
383
        {
384
                StopNavigation = 0;
384
                StopNavigation = 0;
385
                LED_RED_OFF;
385
                LED_RED_OFF;
386
                if(no_error_delay) { no_error_delay--;  }
386
                if(no_error_delay) { no_error_delay--;  }
387
                else
387
                else
388
                 {                                     
388
                 {                                     
389
                  sprintf(ErrorMSG,"No Error            ");
389
                  sprintf(ErrorMSG,"No Error            ");
390
                  ErrorCode = 0;
390
                  ErrorCode = 0;
391
                 }
391
                 }
392
        }
392
        }
393
 
393
 
394
    if(newErrorCode)
394
    if(newErrorCode)
395
         {
395
         {
396
          if(FC.StatusFlags & FC_STATUS_MOTOR_RUN) no_error_delay = 8; // delay the errors if the motors are running
396
          if(FC.StatusFlags & FC_STATUS_MOTOR_RUN) no_error_delay = 8; // delay the errors if the motors are running
397
          ErrorCode = newErrorCode;
397
          ErrorCode = newErrorCode;
398
         }
398
         }
399
 FC.Error[0] = 0;
399
 FC.Error[0] = 0;
400
 FC.Error[1] = 0;
400
 FC.Error[1] = 0;
401
 FC.Error[2] = 0;
401
 FC.Error[2] = 0;
402
 FC.Error[3] = 0;
402
 FC.Error[3] = 0;
403
 FC.Error[4] = 0;
403
 FC.Error[4] = 0;
404
 ErrorGpsFixLost = 0;
404
 ErrorGpsFixLost = 0;
405
}
405
}
406
 
406
 
407
 
407
 
408
 
408
 
409
void Polling(void)
409
void Polling(void)
410
{
410
{
411
 static u8 running = 0, oldFcFlags = 0, count5sec;
411
 static u8 running = 0, oldFcFlags = 0, count5sec;
412
 if(running) return;
412
 if(running) return;
413
 running = 1;
413
 running = 1;
414
                SPI0_UpdateBuffer();    // also calls the GPS-functions
414
                SPI0_UpdateBuffer();    // also calls the GPS-functions
415
                UART0_ProcessRxData();  // GPS process request
415
                UART0_ProcessRxData();  // GPS process request
416
                UART0_TransmitTxData(); // GPS send answer
416
                UART0_TransmitTxData(); // GPS send answer
417
                UART1_ProcessRxData();  // PC process request
417
                UART1_ProcessRxData();  // PC process request
418
                UART1_TransmitTxData(); // PC send answer
418
                UART1_TransmitTxData(); // PC send answer
419
                UART2_TransmitTxData(); // FC send answer
419
                UART2_TransmitTxData(); // FC send answer
420
                CalcHeadFree();
420
                CalcHeadFree();
421
                // ---------------- Error Check Timing ----------------------------
421
                // ---------------- Error Check Timing ----------------------------
422
                if(CheckDelay(TimerCheckError) || (FC.StatusFlags & FC_STATUS_START && !(oldFcFlags & FC_STATUS_START))) // Timer or FY wants to start
422
                if(CheckDelay(TimerCheckError) || (FC.StatusFlags & FC_STATUS_START && !(oldFcFlags & FC_STATUS_START))) // Timer or FY wants to start
423
                {
423
                {
424
//CreateNmeaGGA();
424
//CreateNmeaGGA();
425
                        if(CheckDelay(TimerCheckError))
425
                        if(CheckDelay(TimerCheckError))
426
                         {
426
                         {
427
                          TimerCheckError = SetDelay(1000);
427
                          TimerCheckError = SetDelay(1000);
428
                          if(++count5sec == 5)
428
                          if(++count5sec == 5)
429
                           {
429
                           {
430
                                count5sec = 0;
430
                                count5sec = 0;
431
                                FreqGpsProcessedIn5Sec = CountGpsProcessedIn5Sec * 2;
431
                                FreqGpsProcessedIn5Sec = CountGpsProcessedIn5Sec * 2;
432
                                FreqNewGpsDataIn5Sec = CountNewGpsDataIn5Sec * 2;
432
                                FreqNewGpsDataIn5Sec = CountNewGpsDataIn5Sec * 2;
433
                                CountGpsProcessedIn5Sec = 0;
433
                                CountGpsProcessedIn5Sec = 0;
434
                                CountNewGpsDataIn5Sec = 0;  
434
                                CountNewGpsDataIn5Sec = 0;  
435
                           }
435
                           }
436
                         }
436
                         }
437
                        oldFcFlags = FC.StatusFlags;
437
                        oldFcFlags = FC.StatusFlags;
438
                        if(CheckDelay(SPI0_Timeout) && (DebugUART == UART1)) GPS_Navigation(&GPSData, &(ToFlightCtrl.GPSStick)); // process the GPS data even if the FC is not connected
438
                        if(CheckDelay(SPI0_Timeout) && (DebugUART == UART1)) GPS_Navigation(&GPSData, &(ToFlightCtrl.GPSStick)); // process the GPS data even if the FC is not connected
439
       
439
       
440
                        if(!CheckDelay(SPI0_Timeout) || (DebugUART == UART1)) CheckErrors();
440
                        if(!CheckDelay(SPI0_Timeout) || (DebugUART == UART1)) CheckErrors();
441
               
441
               
442
                        if(FC.StatusFlags & FC_STATUS_FLY) NaviData.FlyingTime++; // we want to count the battery-time
442
                        if(FC.StatusFlags & FC_STATUS_FLY) NaviData.FlyingTime++; // we want to count the battery-time
443
//                      else NaviData.FlyingTime = 0; // not the time per flight
443
//                      else NaviData.FlyingTime = 0; // not the time per flight
444
                        if(SerialLinkOkay) SerialLinkOkay--;
444
                        if(SerialLinkOkay) SerialLinkOkay--;
445
                        if(SerialLinkOkay < 250 - 5) NCFlags |= NC_FLAG_NOSERIALLINK; // 5 seconds timeout for serial communication
445
                        if(SerialLinkOkay < 250 - 5) NCFlags |= NC_FLAG_NOSERIALLINK; // 5 seconds timeout for serial communication
446
                        else NCFlags &= ~NC_FLAG_NOSERIALLINK;
446
                        else NCFlags &= ~NC_FLAG_NOSERIALLINK;
447
                        if(StopNavigation && (Parameter.NaviGpsModeControl >=  50) && (Parameter.GlobalConfig & FC_CFG_GPS_AKTIV)) BeepTime = 1000;
447
                        if(StopNavigation && (Parameter.NaviGpsModeControl >=  50) && (Parameter.GlobalConfig & FC_CFG_GPS_AKTIV)) BeepTime = 1000;
448
                }
448
                }
449
 running = 0;
449
 running = 0;
450
}
450
}
451
 
451
 
452
// the handler will be cyclic called by the timer 1 ISR
452
// the handler will be cyclic called by the timer 1 ISR
453
// used is for critical timing parts that normaly would handled
453
// used is for critical timing parts that normaly would handled
454
// within the main loop that could block longer at logging activities
454
// within the main loop that could block longer at logging activities
455
void EXTIT3_IRQHandler(void)
455
void EXTIT3_IRQHandler(void)
456
{
456
{
457
        IENABLE;
457
        IENABLE;
458
        VIC_ITCmd(EXTIT3_ITLine,DISABLE); // disable irq
458
        VIC_ITCmd(EXTIT3_ITLine,DISABLE); // disable irq
459
        Compass_Update();               // update compass communication
459
        Compass_Update();               // update compass communication
460
        Analog_Update();                // get new ADC values
460
        Analog_Update();                // get new ADC values
461
 
461
 
462
        if(!PollingTimeout)
462
        if(!PollingTimeout)
463
        {
463
        {
464
                PollingTimeout = 5;
464
                PollingTimeout = 5;
465
                Polling();
465
                Polling();
466
        }
466
        }
467
 
467
 
468
        VIC_SWITCmd(EXTIT3_ITLine,DISABLE); // clear pending bit
468
        VIC_SWITCmd(EXTIT3_ITLine,DISABLE); // clear pending bit
469
        VIC_ITCmd(EXTIT3_ITLine, ENABLE); // enable irq
469
        VIC_ITCmd(EXTIT3_ITLine, ENABLE); // enable irq
470
        IDISABLE;
470
        IDISABLE;
471
        VIC1->VAR = 0xFF; // write any value to VIC0 Vector address register
471
        VIC1->VAR = 0xFF; // write any value to VIC0 Vector address register
472
}
472
}
473
 
473
 
474
//----------------------------------------------------------------------------------------------------
474
//----------------------------------------------------------------------------------------------------
475
int main(void)
475
int main(void)
476
{
476
{
477
       
477
       
478
        static u32 ftimer =0;
478
        static u32 ftimer =0;
479
        static u8 fstate = 0;
479
        static u8 fstate = 0;
480
//      static File_t* f = NULL;
480
//      static File_t* f = NULL;
481
       
481
       
482
       
482
       
483
        /* Configure the system clocks */
483
        /* Configure the system clocks */
484
        SCU_Config();
484
        SCU_Config();
485
        /* init VIC (Vectored Interrupt Controller)     */
485
        /* init VIC (Vectored Interrupt Controller)     */
486
        SCU_AHBPeriphClockConfig(__VIC,ENABLE); // enable AHB bus clock for VIC
486
        SCU_AHBPeriphClockConfig(__VIC,ENABLE); // enable AHB bus clock for VIC
487
        SCU_AHBPeriphReset(__VIC, DISABLE);             // disable reset state for VIC
487
        SCU_AHBPeriphReset(__VIC, DISABLE);             // disable reset state for VIC
488
        VIC_DeInit();                                                   // deinitializes the VIC module registers to their default reset values.
488
        VIC_DeInit();                                                   // deinitializes the VIC module registers to their default reset values.
489
        VIC_InitDefaultVectors();
489
        VIC_InitDefaultVectors();
490
 
490
 
491
        // initialize timer 1 for System Clock and delay rountines
491
        // initialize timer 1 for System Clock and delay rountines
492
        TIMER1_Init();
492
        TIMER1_Init();
493
        // initialize the LEDs (needs Timer 1)
493
        // initialize the LEDs (needs Timer 1)
494
        Led_Init();
494
        Led_Init();
495
        // initialize the debug UART1
495
        // initialize the debug UART1
496
        UART1_Init();
496
        UART1_Init();
497
        UART1_PutString("\r\n---------------------------------------------");
497
        UART1_PutString("\r\n---------------------------------------------");
498
        // initialize usb
-
 
499
        // to do
-
 
500
        //USB_ConfigInit();       // disable because of unexpected side effects with I2C0 
-
 
501
        // initialize timer 2 for servo outputs
498
        // initialize timer 2 for servo outputs
502
        //TIMER2_Init();
499
        //TIMER2_Init();
503
        // initialize UART2 to FLIGHTCTRL
500
        // initialize UART2 to FLIGHTCTRL
504
        UART2_Init();
501
        UART2_Init();
505
        // initialize UART0 (to MKGPS or MK3MAG)
502
        // initialize UART0 (to MKGPS or MK3MAG)
506
        UART0_Init();
503
        UART0_Init();
507
        // initialize adc
504
        // initialize adc
508
        Analog_Init();
505
        Analog_Init();
509
        // initialize SPI0 to FC
506
        // initialize SPI0 to FC
510
        SPI0_Init();
507
        SPI0_Init();
511
        // initialize i2c busses (needs Timer 1)
508
        // initialize i2c busses (needs Timer 1)
512
        I2C0_Init();
509
        I2C0_Init();
513
        I2C1_Init();
510
        I2C1_Init();
514
 
511
 
515
        // initialize fat16 partition on sd card (needs Timer 1)
512
        // initialize fat16 partition on sd card (needs Timer 1)
516
        Fat16_Init();
513
        Fat16_Init();
517
        // initialize NC params
514
        // initialize NC params
518
        NCParams_Init();
515
        NCParams_Init();
519
        // initialize the settings
516
        // initialize the settings
520
        Settings_Init();
517
        Settings_Init();
521
        // initialize logging (needs settings)
518
        // initialize logging (needs settings)
522
        Logging_Init();
519
        Logging_Init();
523
 
520
 
524
        LED_GRN_ON;
521
        LED_GRN_ON;
525
        TimerCheckError = SetDelay(3000);
522
        TimerCheckError = SetDelay(3000);
526
        UART1_PutString("\r\n++++++++++++++++++++++++++++++++++++++++++");
523
        UART1_PutString("\r\n++++++++++++++++++++++++++++++++++++++++++");
527
        UART1_PutString("\n\r Version information:");
524
        UART1_PutString("\n\r Version information:");
528
 
525
 
529
        GetNaviCtrlVersion();
526
        GetNaviCtrlVersion();
530
        DebugOut.StatusGreen = AMPEL_NC | AMPEL_COMPASS; // NC and MK3Mag
527
        DebugOut.StatusGreen = AMPEL_NC | AMPEL_COMPASS; // NC and MK3Mag
531
        DebugOut.StatusRed = 0x00;
528
        DebugOut.StatusRed = 0x00;
532
        UART1_PutString("\r\n++++++++++++++++++++++++++++++++++++++++++");
529
        UART1_PutString("\r\n++++++++++++++++++++++++++++++++++++++++++");
533
 
530
 
534
        Compass_Init();
531
        Compass_Init();
535
 
532
 
536
        GPS_Init();
533
        GPS_Init();
537
 
534
 
538
#ifdef FOLLOW_ME
535
#ifdef FOLLOW_ME
539
        TransmitAlsoToFC = 1;
536
        TransmitAlsoToFC = 1;
540
        UART1_PutString("\r\n++++++++++++++++++++++++++++++++++++++++++");
537
        UART1_PutString("\r\n++++++++++++++++++++++++++++++++++++++++++");
541
        UART1_PutString("\n\r FOLLOW-ME Transmitter only!");
538
        UART1_PutString("\n\r FOLLOW-ME Transmitter only!");
542
        UART1_PutString("\r\n++++++++++++++++++++++++++++++++++++++++++\r\n\r\n");
539
        UART1_PutString("\r\n++++++++++++++++++++++++++++++++++++++++++\r\n\r\n");
543
        TransmitAlsoToFC = 0;
540
        TransmitAlsoToFC = 0;
544
#else
541
#else
545
        SPI0_GetFlightCtrlVersion();
542
        SPI0_GetFlightCtrlVersion();
546
        if(FC_Version.Compatible != FC_SPI_COMPATIBLE)
543
        if(FC_Version.Compatible != FC_SPI_COMPATIBLE)
547
        {
544
        {
548
                UART1_PutString("\n\r Flight-Ctrl not compatible\n\r");
545
                UART1_PutString("\n\r Flight-Ctrl not compatible\n\r");
549
                LED_RED_ON;
546
                LED_RED_ON;
550
        }
547
        }
551
#endif
548
#endif
552
        // ---------- Prepare the isr driven
549
        // ---------- Prepare the isr driven
553
        // set to absolute lowest priority
550
        // set to absolute lowest priority
554
    VIC_Config(EXTIT3_ITLine, VIC_IRQ, PRIORITY_SW);
551
    VIC_Config(EXTIT3_ITLine, VIC_IRQ, PRIORITY_SW);
555
        // enable interrupts
552
        // enable interrupts
556
    VIC_ITCmd(EXTIT3_ITLine, ENABLE);
553
    VIC_ITCmd(EXTIT3_ITLine, ENABLE);
557
 
554
 
558
        Debug_OK("START");
555
        Debug_OK("START");
559
        UART1_PutString("\r\n");
556
        UART1_PutString("\r\n");
560
        fifo_purge(&UART1_rx_fifo); // flush the whole fifo init buffer
557
        fifo_purge(&UART1_rx_fifo); // flush the whole fifo init buffer
561
        LED_GRN_ON;
558
        LED_GRN_ON;
562
        LED_RED_OFF;
559
        LED_RED_OFF;
563
    Settings_GetParamValue(PID_SEND_NMEA, &NMEA_Interval);
560
    Settings_GetParamValue(PID_SEND_NMEA, &NMEA_Interval);
564
        UART1_PutString("\r\n");
561
        UART1_PutString("\r\n");
565
        for (;;) // the endless main loop
562
        for (;;) // the endless main loop
566
        {
563
        {
567
                Polling();
564
                Polling();
568
                PollingTimeout = 15;
565
                PollingTimeout = 15;
569
                // ---------------- Logging  ---------------------------------------
566
                // ---------------- Logging  ---------------------------------------
570
                if(SD_WatchDog)
567
                if(SD_WatchDog)
571
                {
568
                {
572
                        SD_WatchDog = 30000;
569
                        SD_WatchDog = 30000;
573
                        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
570
                        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
574
                        else if(FC.StatusFlags & FC_STATUS_START) SD_LoggingError = 100;
571
                        else if(FC.StatusFlags & FC_STATUS_START) SD_LoggingError = 100;
575
                        if(!SD_WatchDog) UART1_PutString("\n\rSD-Watchdog - Logging aborted\n\r");
572
                        if(!SD_WatchDog) UART1_PutString("\n\rSD-Watchdog - Logging aborted\n\r");
576
                }
573
                }
577
               
574
               
578
/*             
575
/*             
579
                if(CheckDelay(ftimer))
576
                if(CheckDelay(ftimer))
580
                {
577
                {
581
 
578
 
582
                        static s8 filename[35];
579
                        static s8 filename[35];
583
                        static u8 i = 0;
580
                        static u8 i = 0;
584
                        s8 dbgmsg[40];
581
                        s8 dbgmsg[40];
585
                       
582
                       
586
                       
583
                       
587
 
584
 
588
                        ftimer = SetDelay(100);
585
                        ftimer = SetDelay(100);
589
                        if(FC.Poti[3]>100 && fstate == 0)
586
                        if(FC.Poti[3]>100 && fstate == 0)
590
                        {
587
                        {
591
                                fstate = 1;
588
                                fstate = 1;
592
                                sprintf(filename, "/toast/toasta/toast%02i.txt",i++);
589
                                sprintf(filename, "/toast/toasta/toast%02i.txt",i++);
593
                        }
590
                        }
594
                        else if(FC.Poti[3]<100 && fstate == 2)
591
                        else if(FC.Poti[3]<100 && fstate == 2)
595
                        {
592
                        {
596
                                fstate = 3;
593
                                fstate = 3;
597
                        }
594
                        }
598
 
595
 
599
                        switch(fstate)
596
                        switch(fstate)
600
                        {
597
                        {
601
                                case 1:
598
                                case 1:
602
                                        sprintf(dbgmsg,"\r\nStart writing file: %s", filename);
599
                                        sprintf(dbgmsg,"\r\nStart writing file: %s", filename);
603
                                        Debug(dbgmsg);
600
                                        Debug(dbgmsg);
604
                                        f = fopen_(filename, 'a');
601
                                        f = fopen_(filename, 'a');
605
                                        if(f== NULL) Fat16_Init();
602
                                        if(f== NULL) Fat16_Init();
606
                                        fstate = 2;
603
                                        fstate = 2;
607
                                        break;
604
                                        break;
608
 
605
 
609
                                case 2:
606
                                case 2:
610
                                        fputs_("\r\ntest edins sdv dsivbds iv dsivb disbv idsv bisd bv d suiv dsibsivbdis fbvisdöb visdbvisdb vidbfibds ibv", f);
607
                                        fputs_("\r\ntest edins sdv dsivbds iv dsivb disbv idsv bisd bv d suiv dsibsivbdis fbvisdöb visdbvisdb vidbfibds ibv", f);
611
                                        break;
608
                                        break;
612
                                       
609
                                       
613
                                case 3:
610
                                case 3:
614
                                        sprintf(dbgmsg,"\r\nClosing file: %s", filename);
611
                                        sprintf(dbgmsg,"\r\nClosing file: %s", filename);
615
                                        Debug(dbgmsg);
612
                                        Debug(dbgmsg);
616
                                        fclose_(f);
613
                                        fclose_(f);
617
                                        fstate = 0;
614
                                        fstate = 0;
618
                                        break;
615
                                        break;
619
 
616
 
620
                                default:
617
                                default:
621
                                        break;
618
                                        break;
622
                        }
619
                        }
623
                }
620
                }
624
*/             
621
*/             
625
        }
622
        }
626
}
623
}
627
//DebugOut.Analog[16]
624
//DebugOut.Analog[16]
628
 
625