Subversion Repositories NaviCtrl

Rev

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

Rev 270 Rev 275
1
/*#######################################################################################*/
1
/*#######################################################################################*/
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
3
/*#######################################################################################*/
3
/*#######################################################################################*/
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// + Copyright (c) 2008 Ingo Busker, Holger Buss
5
// + Copyright (c) 2008 Ingo Busker, Holger Buss
6
// + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY
6
// + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY
7
// + FOR NON COMMERCIAL USE ONLY
7
// + FOR NON COMMERCIAL USE ONLY
8
// + www.MikroKopter.com
8
// + www.MikroKopter.com
9
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
9
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
10
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
10
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
11
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist.
11
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist.
12
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
12
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
13
// + bzgl. der Nutzungsbedingungen aufzunehmen.
13
// + bzgl. der Nutzungsbedingungen aufzunehmen.
14
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
14
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
15
// + Verkauf von Luftbildaufnahmen, usw.
15
// + Verkauf von Luftbildaufnahmen, usw.
16
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
16
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
17
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
17
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
18
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
18
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
20
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
20
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
21
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
21
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
22
// + eindeutig als Ursprung verlinkt werden
22
// + eindeutig als Ursprung verlinkt werden
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
24
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
24
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
25
// + Benutzung auf eigene Gefahr
25
// + Benutzung auf eigene Gefahr
26
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
26
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
27
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
27
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
28
// + Die Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
28
// + Die Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
29
// + mit unserer Zustimmung zulässig
29
// + mit unserer Zustimmung zulässig
30
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
30
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31
// + Teile, die nicht eindeutig mit unserer Lizenzvereinbarung gekennzeichnet sind, unterliegen u.U. eigenen Vereinbarungen:
31
// + Teile, die nicht eindeutig mit unserer Lizenzvereinbarung gekennzeichnet sind, unterliegen u.U. eigenen Vereinbarungen:
32
// + z.B. Die Funktion printf_P() unterliegt ihrer eigenen Lizenz
32
// + z.B. Die Funktion printf_P() unterliegt ihrer eigenen Lizenz
33
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
33
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
34
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
34
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
35
// + this list of conditions and the following disclaimer.
35
// + this list of conditions and the following disclaimer.
36
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
36
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
37
// +     from this software without specific prior written permission.
37
// +     from this software without specific prior written permission.
38
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permitted
38
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permitted
39
// +     for non-commercial use (directly or indirectly)
39
// +     for non-commercial use (directly or indirectly)
40
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
40
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
41
// +     with our written permission
41
// +     with our written permission
42
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
42
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
43
// +     clearly linked as origin
43
// +     clearly linked as origin
44
// +   * porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed
44
// +   * porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed
45
//
45
//
46
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
46
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
47
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
49
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
50
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
50
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
51
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
51
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
52
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
52
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
53
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
53
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
54
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
55
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
55
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
56
// +  POSSIBILITY OF SUCH DAMAGE.
56
// +  POSSIBILITY OF SUCH DAMAGE.
57
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
57
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
58
//#define MCLK96MHZ
58
//#define MCLK96MHZ
59
const unsigned long _Main_Crystal = 25000;
59
const unsigned long _Main_Crystal = 25000;
60
#include <stdio.h>
60
#include <stdio.h>
61
#include "91x_lib.h"
61
#include "91x_lib.h"
62
#include "led.h"
62
#include "led.h"
63
#include "uart0.h"
63
#include "uart0.h"
64
#include "uart1.h"
64
#include "uart1.h"
65
#include "uart2.h"
65
#include "uart2.h"
66
#include "gps.h"
66
#include "gps.h"
67
#include "i2c.h"
67
#include "i2c.h"
68
#include "compass.h"
68
#include "compass.h"
69
#include "timer1.h"
69
#include "timer1.h"
70
#include "timer2.h"
70
#include "timer2.h"
71
#include "analog.h"
71
#include "analog.h"
72
#include "spi_slave.h"
72
#include "spi_slave.h"
73
#include "fat16.h"
73
#include "fat16.h"
74
#include "usb.h"
74
#include "usb.h"
75
#include "sdc.h"
75
#include "sdc.h"
76
#include "logging.h"
76
#include "logging.h"
77
#include "params.h"
77
#include "params.h"
78
#include "settings.h"
78
#include "settings.h"
79
#include "config.h"
79
#include "config.h"
80
#include "main.h"
80
#include "main.h"
81
#include "debug.h"
81
#include "debug.h"
82
 
82
 
83
#include "eeprom.h"
83
#include "eeprom.h"
84
 
84
 
85
u32 TimerCheckError;
85
u32 TimerCheckError;
86
u8 ErrorCode = 0;
86
u8 ErrorCode = 0;
87
u16 BeepTime;
87
u16 BeepTime;
88
u8  NCFlags = 0;
88
u8  NCFlags = 0;
89
s16 GeoMagDec = 0; // local magnetic declination in 0.1 deg
89
s16 GeoMagDec = 0; // local magnetic declination in 0.1 deg
90
 
90
 
91
u8 ClearFCStatusFlags = 0;
91
u8 ClearFCStatusFlags = 0;
92
u8 StopNavigation = 0;
92
u8 StopNavigation = 0;
93
Param_t Parameter;
93
Param_t Parameter;
94
volatile FC_t FC;
94
volatile FC_t FC;
95
 
95
 
96
s8 ErrorMSG[25];
96
s8 ErrorMSG[25];
97
 
97
 
98
//----------------------------------------------------------------------------------------------------
98
//----------------------------------------------------------------------------------------------------
99
void SCU_Config(void)
99
void SCU_Config(void)
100
{
100
{
101
        /* configure PLL and set it as master clock source */
101
        /* configure PLL and set it as master clock source */
102
        SCU_MCLKSourceConfig(SCU_MCLK_OSC);             // set master clock source to external oscillator clock (25MHz) before diabling the PLL
102
        SCU_MCLKSourceConfig(SCU_MCLK_OSC);             // set master clock source to external oscillator clock (25MHz) before diabling the PLL
103
        SCU_PLLCmd(DISABLE);                                    // now disable the PLL
103
        SCU_PLLCmd(DISABLE);                                    // now disable the PLL
104
        #ifdef MCLK96MHZ
104
        #ifdef MCLK96MHZ
105
        SCU_BRCLKDivisorConfig(SCU_BRCLK_Div2); // set BRCLK to MCLK/2 = 48MHz
105
        SCU_BRCLKDivisorConfig(SCU_BRCLK_Div2); // set BRCLK to MCLK/2 = 48MHz
106
        SCU_PCLKDivisorConfig(SCU_PCLK_Div4);   // set PCLK     (APB bus clock) divisor to 4 (half Reference Clock)
106
        SCU_PCLKDivisorConfig(SCU_PCLK_Div4);   // set PCLK     (APB bus clock) divisor to 4 (half Reference Clock)
107
        SCU_RCLKDivisorConfig(SCU_RCLK_Div2);   // set RCLK     (Reference Clock) divisor to 1 (full PPL clock)
107
        SCU_RCLKDivisorConfig(SCU_RCLK_Div2);   // set RCLK     (Reference Clock) divisor to 1 (full PPL clock)
108
        SCU_HCLKDivisorConfig(SCU_HCLK_Div2);   // set HCLK     (AHB bus clock) divisor to 1 (full Reference Clock)
108
        SCU_HCLKDivisorConfig(SCU_HCLK_Div2);   // set HCLK     (AHB bus clock) divisor to 1 (full Reference Clock)
109
        SCU_PLLFactorsConfig(192,25,2);                 // PLL = 96 MHz, Feedback Divider N=192, Pre-Divider M=25, Post-Divider P=2
109
        SCU_PLLFactorsConfig(192,25,2);                 // PLL = 96 MHz, Feedback Divider N=192, Pre-Divider M=25, Post-Divider P=2
110
        #else
110
        #else
111
        SCU_BRCLKDivisorConfig(SCU_BRCLK_Div1); // set BRCLK to MCLK = 48MHz
111
        SCU_BRCLKDivisorConfig(SCU_BRCLK_Div1); // set BRCLK to MCLK = 48MHz
112
        SCU_PCLKDivisorConfig(SCU_PCLK_Div2);   // set PCLK     (APB bus clock) divisor to 2 (half Reference Clock)
112
        SCU_PCLKDivisorConfig(SCU_PCLK_Div2);   // set PCLK     (APB bus clock) divisor to 2 (half Reference Clock)
113
        SCU_RCLKDivisorConfig(SCU_RCLK_Div1);   // set RCLK     (Reference Clock) divisor to 1 (full PPL clock)
113
        SCU_RCLKDivisorConfig(SCU_RCLK_Div1);   // set RCLK     (Reference Clock) divisor to 1 (full PPL clock)
114
        SCU_HCLKDivisorConfig(SCU_HCLK_Div1);   // set HCLK     (AHB bus clock) divisor to 1 (full Reference Clock)
114
        SCU_HCLKDivisorConfig(SCU_HCLK_Div1);   // set HCLK     (AHB bus clock) divisor to 1 (full Reference Clock)
115
        SCU_PLLFactorsConfig(192,25,3);                 // PLL = 48 MHz, Feedback Divider N=192, Pre-Divider M=25, Post-Divider P=3
115
        SCU_PLLFactorsConfig(192,25,3);                 // PLL = 48 MHz, Feedback Divider N=192, Pre-Divider M=25, Post-Divider P=3
116
        #endif
116
        #endif
117
        SCU_PLLCmd(ENABLE);                                     // Enable PLL (is disabled by SCU_PLLFactorsConfig)
117
        SCU_PLLCmd(ENABLE);                                     // Enable PLL (is disabled by SCU_PLLFactorsConfig)
118
        SCU_MCLKSourceConfig(SCU_MCLK_PLL);     // set master clock source to PLL
118
        SCU_MCLKSourceConfig(SCU_MCLK_PLL);     // set master clock source to PLL
119
}
119
}
120
 
120
 
121
//----------------------------------------------------------------------------------------------------
121
//----------------------------------------------------------------------------------------------------
122
void GetNaviCtrlVersion(void)
122
void GetNaviCtrlVersion(void)
123
{
123
{
124
        u8 msg[25];
124
        u8 msg[25];
125
 
125
 
126
        sprintf(msg,"\n\r NaviCtrl HW: V%d.%d SW: V%d.%d%c",  Version_HW/10, Version_HW%10, VERSION_MAJOR,  VERSION_MINOR, 'a'+ VERSION_PATCH);
126
        sprintf(msg,"\n\r NaviCtrl HW: V%d.%d SW: V%d.%d%c",  Version_HW/10, Version_HW%10, VERSION_MAJOR,  VERSION_MINOR, 'a'+ VERSION_PATCH);
127
        UART1_PutString(msg);
127
        UART1_PutString(msg);
128
}
128
}
129
 
129
 
130
//----------------------------------------------------------------------------------------------------
130
//----------------------------------------------------------------------------------------------------
131
 
131
 
132
void CheckErrors(void)
132
void CheckErrors(void)
133
{
133
{
134
        UART_VersionInfo.HardwareError[0] = 0;
134
        UART_VersionInfo.HardwareError[0] = 0;
135
 
135
 
136
        if(/*(MK3MAG_Version.Compatible != MK3MAG_I2C_COMPATIBLE) ||*/ CheckDelay(I2C1_Timeout) || (Compass_Heading < 0)) DebugOut.Status[1] |= 0x08;
136
        if(/*(MK3MAG_Version.Compatible != MK3MAG_I2C_COMPATIBLE) ||*/ CheckDelay(I2C1_Timeout) || (Compass_Heading < 0)) DebugOut.Status[1] |= 0x08;
137
        else DebugOut.Status[1] &= ~0x08; // MK3Mag green status
137
        else DebugOut.Status[1] &= ~0x08; // MK3Mag green status
138
 
138
 
139
        if((FC.Error[1] & FC_ERROR1_I2C) || (FC.Error[1] & FC_ERROR1_BL_MISSING)) DebugOut.Status[1] |= 0x02;
139
        if((FC.Error[1] & FC_ERROR1_I2C) || (FC.Error[1] & FC_ERROR1_BL_MISSING)) DebugOut.Status[1] |= 0x02;
140
        else DebugOut.Status[1] &= ~0x02; // BL-Ctrl green status
140
        else DebugOut.Status[1] &= ~0x02; // BL-Ctrl green status
141
 
141
 
142
        if(CheckDelay(SPI0_Timeout))
142
        if(CheckDelay(SPI0_Timeout))
143
        {
143
        {
144
                LED_RED_ON;
144
                LED_RED_ON;
145
                sprintf(ErrorMSG,"no FC communication ");
145
                sprintf(ErrorMSG,"no FC communication ");
146
                ErrorCode = 3;
146
                ErrorCode = 3;
147
                StopNavigation = 1;
147
                StopNavigation = 1;
148
                DebugOut.Status[0] &= ~0x01; // status of FC Present
148
                DebugOut.Status[0] &= ~0x01; // status of FC Present
149
                DebugOut.Status[0] &= ~0x02; // status of BL Present
149
                DebugOut.Status[0] &= ~0x02; // status of BL Present
150
                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_SPI_RX;
150
                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_SPI_RX;
151
        }
151
        }
152
        else if(CheckDelay(I2C1_Timeout))
152
        else if(CheckDelay(I2C1_Timeout))
153
        {
153
        {
154
                LED_RED_ON;
154
                LED_RED_ON;
155
                sprintf(ErrorMSG,"no compass communication ");
155
                sprintf(ErrorMSG,"no compass communication ");
156
                //Reset I2CBus
156
                //Reset I2CBus
157
                I2C1_Deinit();
157
                I2C1_Deinit();
158
                I2C1_Init();
158
                I2C1_Init();
159
                ErrorCode = 4;
159
                ErrorCode = 4;
160
                StopNavigation = 1;
160
                StopNavigation = 1;
161
                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_RX;
161
                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_RX;
162
                DebugOut.Status[1] |= 0x08;
162
                DebugOut.Status[1] |= 0x08;
163
        }
163
        }
164
        else if(FC_Version.Compatible != FC_SPI_COMPATIBLE)
164
        else if(FC_Version.Compatible != FC_SPI_COMPATIBLE)
165
        {
165
        {
166
                LED_RED_ON;
166
                LED_RED_ON;
167
                sprintf(ErrorMSG,"FC not compatible ");
167
                sprintf(ErrorMSG,"FC not compatible ");
168
                ErrorCode = 1;
168
                ErrorCode = 1;
169
                StopNavigation = 1;
169
                StopNavigation = 1;
170
                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_FC_INCOMPATIBLE;
170
                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_FC_INCOMPATIBLE;
171
        }
171
        }
172
 
172
 
173
        else if(FC.Error[0] & FC_ERROR0_GYRO_NICK)
173
        else if(FC.Error[0] & FC_ERROR0_GYRO_NICK)
174
        {
174
        {
175
                LED_RED_ON;
175
                LED_RED_ON;
176
                sprintf(ErrorMSG,"ERR: FC Nick Gyro");
176
                sprintf(ErrorMSG,"ERR: FC Nick Gyro");
177
                ErrorCode = 10;
177
                ErrorCode = 10;
178
        }
178
        }
179
        else if(FC.Error[0] & FC_ERROR0_GYRO_ROLL)
179
        else if(FC.Error[0] & FC_ERROR0_GYRO_ROLL)
180
        {
180
        {
181
                LED_RED_ON;
181
                LED_RED_ON;
182
                sprintf(ErrorMSG,"ERR: FC Roll Gyro");
182
                sprintf(ErrorMSG,"ERR: FC Roll Gyro");
183
                ErrorCode = 11;
183
                ErrorCode = 11;
184
        }
184
        }
185
        else if(FC.Error[0] & FC_ERROR0_GYRO_YAW)
185
        else if(FC.Error[0] & FC_ERROR0_GYRO_YAW)
186
        {
186
        {
187
                LED_RED_ON;
187
                LED_RED_ON;
188
                sprintf(ErrorMSG,"ERR: FC Yaw Gyro");
188
                sprintf(ErrorMSG,"ERR: FC Yaw Gyro");
189
                ErrorCode = 12;
189
                ErrorCode = 12;
190
        }
190
        }
191
        else if(FC.Error[0] & FC_ERROR0_ACC_NICK)
191
        else if(FC.Error[0] & FC_ERROR0_ACC_NICK)
192
        {
192
        {
193
                LED_RED_ON;
193
                LED_RED_ON;
194
                sprintf(ErrorMSG,"ERR: FC Nick ACC");
194
                sprintf(ErrorMSG,"ERR: FC Nick ACC");
195
                ErrorCode = 13;
195
                ErrorCode = 13;
196
        }
196
        }
197
        else if(FC.Error[0] & FC_ERROR0_ACC_ROLL)
197
        else if(FC.Error[0] & FC_ERROR0_ACC_ROLL)
198
        {
198
        {
199
                LED_RED_ON;
199
                LED_RED_ON;
200
                sprintf(ErrorMSG,"ERR: FC Roll ACC");
200
                sprintf(ErrorMSG,"ERR: FC Roll ACC");
201
                ErrorCode = 14;
201
                ErrorCode = 14;
202
        }
202
        }
203
        else if(FC.Error[0] & FC_ERROR0_ACC_TOP)
203
        else if(FC.Error[0] & FC_ERROR0_ACC_TOP)
204
        {
204
        {
205
                LED_RED_ON;
205
                LED_RED_ON;
206
                sprintf(ErrorMSG,"ERR: FC Z-ACC");
206
                sprintf(ErrorMSG,"ERR: FC Z-ACC");
207
                ErrorCode = 15;
207
                ErrorCode = 15;
208
        }
208
        }
209
        else if(FC.Error[0] & FC_ERROR0_PRESSURE)
209
        else if(FC.Error[0] & FC_ERROR0_PRESSURE)
210
        {
210
        {
211
                LED_RED_ON;
211
                LED_RED_ON;
212
                sprintf(ErrorMSG,"ERR: Pressure sensor");
212
                sprintf(ErrorMSG,"ERR: Pressure sensor");
213
                ErrorCode = 16;
213
                ErrorCode = 16;
214
        }
214
        }
215
        else if(FC.Error[1] &  FC_ERROR1_I2C)
215
        else if(FC.Error[1] &  FC_ERROR1_I2C)
216
        {
216
        {
217
                LED_RED_ON;
217
                LED_RED_ON;
218
                sprintf(ErrorMSG,"ERR: FC I2C");
218
                sprintf(ErrorMSG,"ERR: FC I2C");
219
                ErrorCode = 17;
219
                ErrorCode = 17;
220
        }
220
        }
221
        else if(FC.Error[1] &  FC_ERROR1_BL_MISSING)
221
        else if(FC.Error[1] &  FC_ERROR1_BL_MISSING)
222
        {
222
        {
223
                LED_RED_ON;
223
                LED_RED_ON;
224
                sprintf(ErrorMSG,"ERR: Bl Missing");
224
                sprintf(ErrorMSG,"ERR: Bl Missing");
225
                ErrorCode = 18;
225
                ErrorCode = 18;
226
        }
226
        }
227
        else if(FC.Error[1] &  FC_ERROR1_MIXER)
227
        else if(FC.Error[1] &  FC_ERROR1_MIXER)
228
        {
228
        {
229
                LED_RED_ON;
229
                LED_RED_ON;
230
                sprintf(ErrorMSG,"Mixer Error");
230
                sprintf(ErrorMSG,"Mixer Error");
231
                ErrorCode = 19;
231
                ErrorCode = 19;
232
        }
232
        }
233
/*      else if(MK3MAG_Version.Compatible != MK3MAG_I2C_COMPATIBLE)
233
/*      else if(MK3MAG_Version.Compatible != MK3MAG_I2C_COMPATIBLE)
234
        {
234
        {
235
                sprintf(ErrorMSG,"MK3Mag not compatible ");
235
                sprintf(ErrorMSG,"MK3Mag not compatible ");
236
                LED_RED_ON;
236
                LED_RED_ON;
237
                ErrorCode = 2;
237
                ErrorCode = 2;
238
                StopNavigation = 1;
238
                StopNavigation = 1;
239
                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_INCOMPATIBLE;
239
                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_INCOMPATIBLE;
240
        } */
240
        } */
241
        else if(CheckDelay(UBX_Timeout))
241
        else if(CheckDelay(UBX_Timeout))
242
        {
242
        {
243
                LED_RED_ON;
243
                LED_RED_ON;
244
                sprintf(ErrorMSG,"no GPS communication ");
244
                sprintf(ErrorMSG,"no GPS communication ");
245
                ErrorCode = 5;
245
                ErrorCode = 5;
246
                StopNavigation = 1;
246
                StopNavigation = 1;
247
                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_GPS_RX;
247
                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_GPS_RX;
248
        }
248
        }
249
        else if(Compass_Heading < 0)
249
        else if(Compass_Heading < 0)
250
        {
250
        {
251
                LED_RED_ON;
251
                LED_RED_ON;
252
                sprintf(ErrorMSG,"bad compass value ");
252
                sprintf(ErrorMSG,"bad compass value ");
253
                ErrorCode = 6;
253
                ErrorCode = 6;
254
                StopNavigation = 1;
254
                StopNavigation = 1;
255
                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_VALUE;
255
                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_VALUE;
256
        }
256
        }
257
        else if((FC.Error[1] &  FC_ERROR1_SPI_RX))
257
        else if((FC.Error[1] &  FC_ERROR1_SPI_RX))
258
        {
258
        {
259
                LED_RED_ON;
259
                LED_RED_ON;
260
                sprintf(ErrorMSG,"FC spi rx error ");
260
                sprintf(ErrorMSG,"FC spi rx error ");
261
                ErrorCode = 8;
261
                ErrorCode = 8;
262
                StopNavigation = 1;
262
                StopNavigation = 1;
263
        }
263
        }
264
        else if(FC.Error[0] &  FC_ERROR0_CAREFREE)
264
        else if(FC.Error[0] &  FC_ERROR0_CAREFREE)
265
        {
265
        {
266
                LED_RED_ON;
266
                LED_RED_ON;
267
                sprintf(ErrorMSG,"FC: Carefree Error");
267
                sprintf(ErrorMSG,"FC: Carefree Error");
268
                ErrorCode = 20;
268
                ErrorCode = 20;
269
        }
269
        }
270
        else if(FC.Error[1] &  FC_ERROR1_PPM)
270
        else if(FC.Error[1] &  FC_ERROR1_PPM)
271
        {
271
        {
272
                LED_RED_ON;
272
                LED_RED_ON;
273
                sprintf(ErrorMSG,"RC Signal lost ");
273
                sprintf(ErrorMSG,"RC Signal lost ");
274
                ErrorCode = 7;
274
                ErrorCode = 7;
275
        }
275
        }
276
        else // no error occured
276
        else // no error occured
277
        {
277
        {
278
                sprintf(ErrorMSG,"No Error               ");
278
                sprintf(ErrorMSG,"No Error               ");
279
                ErrorCode = 0;
279
                ErrorCode = 0;
280
                StopNavigation = 0;
280
                StopNavigation = 0;
281
                LED_RED_OFF;
281
                LED_RED_OFF;
282
        }
282
        }
283
        if(UART_VersionInfo.HardwareError[0] || UART_VersionInfo.HardwareError[1]) DebugOut.Status[1] |= 0x04;
283
        if(UART_VersionInfo.HardwareError[0] || UART_VersionInfo.HardwareError[1]) DebugOut.Status[1] |= 0x04;
284
        else DebugOut.Status[1] &= ~0x04;
284
        else DebugOut.Status[1] &= ~0x04;
285
}
285
}
286
 
286
 
287
// the handler will be cyclic called by the timer 1 ISR
287
// the handler will be cyclic called by the timer 1 ISR
288
// used is for critical timing parts that normaly would handled
288
// used is for critical timing parts that normaly would handled
289
// within the main loop that could block longer at logging activities
289
// within the main loop that could block longer at logging activities
290
void EXTIT3_IRQHandler(void)
290
void EXTIT3_IRQHandler(void)
291
{
291
{
292
        IENABLE;
292
        IENABLE;
293
 
293
 
294
        VIC_ITCmd(EXTIT3_ITLine,DISABLE); // disable irq
294
        VIC_ITCmd(EXTIT3_ITLine,DISABLE); // disable irq
295
        VIC_SWITCmd(EXTIT3_ITLine,DISABLE); // clear pending bit
295
        VIC_SWITCmd(EXTIT3_ITLine,DISABLE); // clear pending bit
296
        Compass_UpdateHeading();        // update compass communication
296
        Compass_UpdateHeading();        // update compass communication
297
        Analog_Update();                // get new ADC values
297
        Analog_Update();                // get new ADC values
298
        VIC_ITCmd(EXTIT3_ITLine, ENABLE); // enable irq
298
        VIC_ITCmd(EXTIT3_ITLine, ENABLE); // enable irq
299
 
299
 
300
        IDISABLE;
300
        IDISABLE;
301
}
301
}
302
 
302
 
303
//----------------------------------------------------------------------------------------------------
303
//----------------------------------------------------------------------------------------------------
304
int main(void)
304
int main(void)
305
{
305
{
306
        /* Configure the system clocks */
306
        /* Configure the system clocks */
307
        SCU_Config();
307
        SCU_Config();
308
        /* init VIC (Vectored Interrupt Controller)     */
308
        /* init VIC (Vectored Interrupt Controller)     */
309
        SCU_AHBPeriphClockConfig(__VIC,ENABLE); // enable AHB bus clock for VIC
309
        SCU_AHBPeriphClockConfig(__VIC,ENABLE); // enable AHB bus clock for VIC
310
        SCU_AHBPeriphReset(__VIC, DISABLE);             // disable reset state for VIC
310
        SCU_AHBPeriphReset(__VIC, DISABLE);             // disable reset state for VIC
311
        VIC_DeInit();                                                   // deinitializes the VIC module registers to their default reset values.
311
        VIC_DeInit();                                                   // deinitializes the VIC module registers to their default reset values.
312
        VIC_InitDefaultVectors();
312
        VIC_InitDefaultVectors();
313
 
313
 
314
        // initialize timer 1 for System Clock and delay rountines
314
        // initialize timer 1 for System Clock and delay rountines
315
        TIMER1_Init();
315
        TIMER1_Init();
316
        // initialize the LEDs (needs Timer 1)
316
        // initialize the LEDs (needs Timer 1)
317
        Led_Init();
317
        Led_Init();
318
        // initialize the debug UART1
318
        // initialize the debug UART1
319
        UART1_Init();
319
        UART1_Init();
320
        UART1_PutString("\r\n---------------------------------------------");
320
        UART1_PutString("\r\n---------------------------------------------");
-
 
321
        // initialize usb
-
 
322
        USB_ConfigInit();
321
        // initialize timer 2 for servo outputs
323
        // initialize timer 2 for servo outputs
322
        //TIMER2_Init();
324
        //TIMER2_Init();
323
        // initialize UART2 to FLIGHTCTRL
325
        // initialize UART2 to FLIGHTCTRL
324
        UART2_Init();
326
        UART2_Init();
325
        // initialize UART0 (to MKGPS or MK3MAG)
327
        // initialize UART0 (to MKGPS or MK3MAG)
326
        UART0_Init();
328
        UART0_Init();
327
        // initialize adc
329
        // initialize adc
328
        Analog_Init();
330
        Analog_Init();
329
        // initialize usb
-
 
330
        USB_ConfigInit();
-
 
331
        // initialize SPI0 to FC
331
        // initialize SPI0 to FC
332
        SPI0_Init();
332
        SPI0_Init();
333
        // initialize i2c bus (needs Timer 1)
333
        // initialize i2c bus (needs Timer 1)
334
        I2C1_Init();
334
        I2C1_Init();
335
        // initialize fat16 partition on sd card (needs Timer 1)
335
        // initialize fat16 partition on sd card (needs Timer 1)
336
        Fat16_Init();
336
        Fat16_Init();
337
        // initialize NC params
337
        // initialize NC params
338
        NCParams_Init();
338
        NCParams_Init();
339
        // initialize the settings
339
        // initialize the settings
340
        Settings_Init();
340
        Settings_Init();
341
        // initialize logging (needs settings)
341
        // initialize logging (needs settings)
342
        Logging_Init();
342
        Logging_Init();
343
 
343
 
344
        TimerCheckError = SetDelay(3000);
344
        TimerCheckError = SetDelay(3000);
345
        UART1_PutString("\r\n++++++++++++++++++++++++++++++++++++++++++");
345
        UART1_PutString("\r\n++++++++++++++++++++++++++++++++++++++++++");
346
        UART1_PutString("\n\r Version information:");
346
        UART1_PutString("\n\r Version information:");
347
 
347
 
348
        GetNaviCtrlVersion();
348
        GetNaviCtrlVersion();
349
        DebugOut.Status[0] = 0x04 | 0x08; // NC and MK3Mag
349
        DebugOut.Status[0] = 0x04 | 0x08; // NC and MK3Mag
350
        DebugOut.Status[1] = 0x00;
350
        DebugOut.Status[1] = 0x00;
351
 
351
 
352
        Compass_Init();
352
        Compass_Init();
353
 
353
 
354
        SPI0_GetFlightCtrlVersion();
354
        SPI0_GetFlightCtrlVersion();
355
        if(FC_Version.Compatible != FC_SPI_COMPATIBLE)
355
        if(FC_Version.Compatible != FC_SPI_COMPATIBLE)
356
        {
356
        {
357
                UART1_PutString("\n\r Flight-Ctrl not compatible");
357
                UART1_PutString("\n\r Flight-Ctrl not compatible");
358
                LED_RED_ON;
358
                LED_RED_ON;
359
        }
359
        }
360
 
360
 
361
        GPS_Init();
361
        GPS_Init();
362
 
362
 
363
        // ---------- Prepare the isr driven
363
        // ---------- Prepare the isr driven
364
        // set to absolute lowest priority
364
        // set to absolute lowest priority
365
    VIC_Config(EXTIT3_ITLine, VIC_IRQ, PRIORITY_SW);
365
    VIC_Config(EXTIT3_ITLine, VIC_IRQ, PRIORITY_SW);
366
        // enable interrupts
366
        // enable interrupts
367
    VIC_ITCmd(EXTIT3_ITLine, ENABLE);
367
    VIC_ITCmd(EXTIT3_ITLine, ENABLE);
368
 
368
 
369
        Debug_OK("START");
369
        Debug_OK("START");
370
 
370
 
371
        for (;;) // the endless main loop
371
        for (;;) // the endless main loop
372
        {
372
        {
373
                UART0_ProcessRxData();  // process request
373
                UART0_ProcessRxData();  // process request
374
                UART1_ProcessRxData();  // process request
374
                UART1_ProcessRxData();  // process request
375
                USB_ProcessRxData();    // process request
375
                USB_ProcessRxData();    // process request
376
                UART0_TransmitTxData(); // send answer
376
                UART0_TransmitTxData(); // send answer
377
                UART1_TransmitTxData(); // send answer
377
                UART1_TransmitTxData(); // send answer
378
                UART2_TransmitTxData(); // send answer
378
                UART2_TransmitTxData(); // send answer
379
                USB_TransmitTxData();   // send answer
379
                USB_TransmitTxData();   // send answer
380
                SPI0_UpdateBuffer();    // handle new SPI Data
380
                SPI0_UpdateBuffer();    // handle new SPI Data
381
                // ---------------- Error Check Timing ----------------------------
381
                // ---------------- Error Check Timing ----------------------------
382
                if(CheckDelay(TimerCheckError))
382
                if(CheckDelay(TimerCheckError))
383
                {
383
                {
384
                        TimerCheckError = SetDelay(1000);
384
                        TimerCheckError = SetDelay(1000);
385
                        if(CheckDelay(SPI0_Timeout)) GPS_Navigation(&GPSData, &(ToFlightCtrl.GPSStick)); // process the GPS data even if the FC is not connected
385
                        if(CheckDelay(SPI0_Timeout)) GPS_Navigation(&GPSData, &(ToFlightCtrl.GPSStick)); // process the GPS data even if the FC is not connected
386
                        CheckErrors();
386
                        CheckErrors();
387
                        if(FC.StatusFlags & FC_STATUS_FLY) NaviData.FlyingTime++; // we want to count the battery-time
387
                        if(FC.StatusFlags & FC_STATUS_FLY) NaviData.FlyingTime++; // we want to count the battery-time
388
//                      else NaviData.FlyingTime = 0; // not the time per flight
388
//                      else NaviData.FlyingTime = 0; // not the time per flight
389
                        if(SerialLinkOkay) SerialLinkOkay--;
389
                        if(SerialLinkOkay) SerialLinkOkay--;
390
                        if(SerialLinkOkay < 250 - 5) NCFlags |= NC_FLAG_NOSERIALLINK; // 5 seconds timeout for serial communication
390
                        if(SerialLinkOkay < 250 - 5) NCFlags |= NC_FLAG_NOSERIALLINK; // 5 seconds timeout for serial communication
391
                        else NCFlags &= ~NC_FLAG_NOSERIALLINK;
391
                        else NCFlags &= ~NC_FLAG_NOSERIALLINK;
392
                        if(StopNavigation && (Parameter.NaviGpsModeControl >=  50)) BeepTime = 1000;
392
                        if(StopNavigation && (Parameter.NaviGpsModeControl >=  50)) BeepTime = 1000;
393
                }
393
                }
394
                // ---------------- Logging  ---------------------------------------
394
                // ---------------- Logging  ---------------------------------------
395
                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
395
                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
396
        }
396
        }
397
}
397
}
398
 
398
 
399
 
399
 
400
 
400