Subversion Repositories NaviCtrl

Rev

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

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