Subversion Repositories NaviCtrl

Rev

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

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