Subversion Repositories NaviCtrl

Rev

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

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