Subversion Repositories NaviCtrl

Rev

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

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