Subversion Repositories NaviCtrl

Rev

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

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